Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,11 +668,13 @@ func buildCodecForTypeDescribedByString(st map[string]*Codec, enclosingNamespace
// For "bytes.decimal" types verify that the scale and precision in this schema map match a cached codec before
// using the cached codec in favor of creating a new codec.
if searchType == "bytes.decimal" {
precision, ok1 := schemaMap["precision"].(float64)
scale, ok2 := schemaMap["scale"].(float64)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this default to 0 instead, otherwise we never get a cache hit for something without an explicit scale?


// Search the cached codecs for a "bytes.decimal" codec with a "precision" and "scale" specified in the key,
// only if that matches return the cached codec. Otherwise, create a new codec for this "bytes.decimal".
decimalSearchType := fmt.Sprintf("bytes.decimal.%d.%d", int(schemaMap["precision"].(float64)), int(schemaMap["scale"].(float64)))
if cd2, ok := st[decimalSearchType]; ok {
decimalSearchType := fmt.Sprintf("bytes.decimal.%d.%d", int(precision), int(scale))
if cd2, ok := st[decimalSearchType]; ok && ok1 && ok2 {
return cd2, nil
}

Expand Down
8 changes: 8 additions & 0 deletions codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,12 @@ func Test_buildCodecForTypeDescribedByString_CacheRespectsPrecisionScale(t *test
if cacheMiss.schemaOriginal == cachedCodecIdentifier {
t.Errorf("GOT: %v; WANT: %v", cacheMiss.schemaOriginal, "!= "+cachedCodecIdentifier)
}

// Scale does not exist in schema, so cache miss
delete(schemaMap, "scale")
cacheMiss, err = buildCodecForTypeDescribedByString(cache, "", "bytes", schemaMap, nil)
ensureError(t, err)
if cacheMiss.schemaOriginal == cachedCodecIdentifier {
t.Errorf("GOT: %v; WANT: %v", cacheMiss.schemaOriginal, "!= "+cachedCodecIdentifier)
}
}