Skip to content

Commit

Permalink
apacheGH-35027: [Go]: Use base64.StdEncoding in FixedSizeBinaryBuilde…
Browse files Browse the repository at this point in the history
…r Unmarshal (apache#35028)

This changes `FixedSizeBinaryBuilder`'s Unmarshal to use `base64.StdEncoding` instead of `base64.RawStdEncoding`. The previous implementation caused an error when unmarshaling from JSON previously produced by a `FixedSizeBinary` array instance.

I also added a test for Marshal/Unmarshal to set the expectation that these operations should mirror one another.
* Closes: apache#35027

Lead-authored-by: Herman Schaaf <hermanschaaf@gmail.com>
Co-authored-by: Kemal Hadimli <disq@users.noreply.github.com>
Signed-off-by: Matt Topol <zotthewizard@gmail.com>
  • Loading branch information
2 people authored and liujiacheng777 committed May 11, 2023
1 parent d7fb310 commit 5089304
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
40 changes: 40 additions & 0 deletions go/arrow/array/fixedsize_binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,43 @@ func TestFixedSizeBinarySlice(t *testing.T) {
t.Fatalf("got=%q, want=%q", got, want)
}
}

func TestFixedSizeBinary_MarshalUnmarshalJSON(t *testing.T) {
mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
defer mem.AssertSize(t, 0)

dtype := &arrow.FixedSizeBinaryType{ByteWidth: 4}
b := array.NewFixedSizeBinaryBuilder(mem, dtype)
defer b.Release()

var data = [][]byte{
[]byte("ABCD"),
[]byte("1234"),
nil,
[]byte("AZER"),
}
b.AppendValues(data[:2], nil)
b.AppendNull()
b.Append(data[3])

arr := b.NewFixedSizeBinaryArray()
defer arr.Release()

jsonBytes, err := arr.MarshalJSON()
if err != nil {
t.Fatalf("failed to marshal json: %v", err)
}

err = b.UnmarshalJSON(jsonBytes)
if err != nil {
t.Fatalf("failed to unmarshal json: %v", err)
}
gotArr := b.NewFixedSizeBinaryArray()
defer gotArr.Release()

gotString := gotArr.String()
wantString := arr.String()
if gotString != wantString {
t.Fatalf("got=%q, want=%q", gotString, wantString)
}
}
2 changes: 1 addition & 1 deletion go/arrow/array/fixedsize_binarybuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (b *FixedSizeBinaryBuilder) UnmarshalOne(dec *json.Decoder) error {
var val []byte
switch v := t.(type) {
case string:
data, err := base64.RawStdEncoding.DecodeString(v)
data, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return err
}
Expand Down
23 changes: 9 additions & 14 deletions go/arrow/compute/scalar_compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,17 +873,16 @@ func (c *CompareFixedSizeBinary) TestArrayScalar() {
valAba = `YWJh`
valAbc = `YWJj`
valAbd = `YWJk`
valA = `YQ`
valB = `Yg`
valC = `Yw`
valA = `YQ==`
valB = `Yg==`
valC = `Yw==`
)

const (
lhs1bin = `["` + valAba + `","` + valAbc + `","` + valAbd + `", null]`
lhs1 = `["aba", "abc", "abd", null]`
rhs1 = "abc"
lhs2bin = `["` + valA + `","` + valB + `","` + valC + `", null]`
lhs2 = `["a", "b", "c", null]`
rhs2 = "b"
)

Expand Down Expand Up @@ -968,9 +967,9 @@ func (c *CompareFixedSizeBinary) TestScalarArray() {
valAba = `YWJh`
valAbc = `YWJj`
valAbd = `YWJk`
valA = `YQ`
valB = `Yg`
valC = `Yw`
valA = `YQ==`
valB = `Yg==`
valC = `Yw==`
)

const (
Expand Down Expand Up @@ -1078,22 +1077,18 @@ func (c *CompareFixedSizeBinary) TestArrayArray() {

// base64 encoding
const (
valAba = `YWJh`
valAbc = `YWJj`
valAbd = `YWJk`
valA = `YQ`
valB = `Yg`
valC = `Yw`
valD = `ZA`
valA = `YQ==`
valC = `Yw==`
valD = `ZA==`
)

const (
lhs1bin = `["` + valAbc + `","` + valAbc + `","` + valAbd + `", null, "` + valAbc + `"]`
rhs1bin = `["` + valAbc + `","` + valAbd + `","` + valAbc + `","` + valAbc + `", null]`
lhs1 = `["abc", "abc", "abd", null, "abc"]`
rhs1 = `["abc", "abd", "abc", "abc", null]`
lhs2 = `["a", "a", "d", null, "a"]`
rhs2 = `["a", "d", "c", "a", null]`
lhs2bin = `["` + valA + `","` + valA + `","` + valD + `", null, "` + valA + `"]`
rhs2bin = `["` + valA + `","` + valD + `","` + valC + `","` + valA + `", null]`
)
Expand Down

0 comments on commit 5089304

Please sign in to comment.