Skip to content

Commit 2df4413

Browse files
authored
chore(tests): Ensure interface contract between types.XArray.ValueStr & types.XBuilder.AppendValueFromString (#849)
Follow-up to apache/arrow#35457 BEGIN_COMMIT_OVERRIDE chore(tests): Ensure interface contract between `types.XArray.ValueStr` & `types.XBuilder.AppendValueFromString` (#849) feat(arrow): Add `types.XBuilder.NewXArray` helpers END_COMMIT_OVERRIDE
1 parent 79cf330 commit 2df4413

File tree

6 files changed

+119
-1
lines changed

6 files changed

+119
-1
lines changed

types/extensions_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package types
2+
3+
import (
4+
"testing"
5+
6+
"github.com/apache/arrow/go/v12/arrow"
7+
"github.com/apache/arrow/go/v12/arrow/array"
8+
"github.com/apache/arrow/go/v12/arrow/memory"
9+
"github.com/google/uuid"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestValueStrRoundTrip(t *testing.T) {
15+
mem := memory.NewCheckedAllocator(memory.NewGoAllocator())
16+
defer mem.AssertSize(t, 0)
17+
18+
cases := []struct {
19+
arr arrow.Array
20+
builder array.Builder
21+
}{
22+
{
23+
arr: func() arrow.Array {
24+
b := NewInetBuilder(array.NewExtensionBuilder(mem, NewInetType()))
25+
defer b.Release()
26+
27+
b.AppendNull()
28+
b.Append(mustParseInet("192.168.0.0/24"))
29+
b.AppendNull()
30+
b.Append(mustParseInet("192.168.0.0/25"))
31+
b.AppendNull()
32+
33+
return b.NewInetArray()
34+
}(),
35+
builder: NewInetBuilder(array.NewExtensionBuilder(mem, NewInetType())),
36+
},
37+
{
38+
arr: func() arrow.Array {
39+
b := NewJSONBuilder(array.NewExtensionBuilder(mem, NewJSONType()))
40+
defer b.Release()
41+
42+
b.AppendNull()
43+
b.Append(map[string]any{"a": 1, "b": 2})
44+
b.AppendNull()
45+
b.Append([]any{1, 2, 3})
46+
b.AppendNull()
47+
b.Append(map[string]any{"MyKey": "A\u0026B"})
48+
b.AppendNull()
49+
50+
return b.NewJSONArray()
51+
}(),
52+
builder: NewJSONBuilder(array.NewExtensionBuilder(mem, NewJSONType())),
53+
},
54+
{
55+
arr: func() arrow.Array {
56+
b := NewMacBuilder(array.NewExtensionBuilder(mem, NewMacType()))
57+
defer b.Release()
58+
59+
b.AppendNull()
60+
b.Append(mustParseMac("00:00:00:00:00:01"))
61+
b.AppendNull()
62+
b.Append(mustParseMac("00:00:00:00:00:02"))
63+
b.AppendNull()
64+
65+
return b.NewMacArray()
66+
}(),
67+
builder: NewMacBuilder(array.NewExtensionBuilder(mem, NewMacType())),
68+
},
69+
{
70+
arr: func() arrow.Array {
71+
b := NewUUIDBuilder(array.NewExtensionBuilder(mem, NewUUIDType()))
72+
defer b.Release()
73+
74+
b.AppendNull()
75+
b.Append(uuid.NameSpaceURL)
76+
b.AppendNull()
77+
b.Append(uuid.NameSpaceDNS)
78+
b.AppendNull()
79+
80+
return b.NewUUIDArray()
81+
}(),
82+
builder: NewUUIDBuilder(array.NewExtensionBuilder(mem, NewUUIDType())),
83+
},
84+
}
85+
86+
for _, tc := range cases {
87+
t.Run(tc.arr.DataType().(arrow.ExtensionType).ExtensionName(), func(t *testing.T) {
88+
defer tc.arr.Release()
89+
defer tc.builder.Release()
90+
t.Helper()
91+
92+
for i := 0; i < tc.arr.Len(); i++ {
93+
assert.NoError(t, tc.builder.AppendValueFromString(tc.arr.ValueStr(i)))
94+
}
95+
96+
arr := tc.builder.NewArray()
97+
defer arr.Release()
98+
99+
require.True(t, array.Equal(tc.arr, arr))
100+
})
101+
}
102+
}

types/inet.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ func (b *InetBuilder) UnmarshalJSON(data []byte) error {
117117
return b.Unmarshal(dec)
118118
}
119119

120+
func (b *InetBuilder) NewInetArray() *InetArray {
121+
return b.NewExtensionArray().(*InetArray)
122+
}
123+
120124
// InetArray is a simple array which is a FixedSizeBinary(16)
121125
type InetArray struct {
122126
array.ExtensionArrayBase

types/json.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ func (b *JSONBuilder) UnmarshalJSON(data []byte) error {
110110
return b.Unmarshal(dec)
111111
}
112112

113+
func (b *JSONBuilder) NewJSONArray() *JSONArray {
114+
return b.NewExtensionArray().(*JSONArray)
115+
}
116+
113117
// JSONArray is a simple array which is a Binary
114118
type JSONArray struct {
115119
array.ExtensionArrayBase

types/json_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func TestJSONArray_GetOneForMarshal(t *testing.T) {
172172
}
173173
}
174174

175-
func TestJSONArray_ValueStr(t *testing.T) {
175+
func TestJSONArray_ValueStrParse(t *testing.T) {
176176
cases := []struct {
177177
name string
178178
data string

types/mac.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ func (b *MacBuilder) UnmarshalJSON(data []byte) error {
111111
return b.Unmarshal(dec)
112112
}
113113

114+
func (b *MacBuilder) NewMacArray() *MacArray {
115+
return b.NewExtensionArray().(*MacArray)
116+
}
117+
114118
// MacArray is a simple array which is a wrapper around a BinaryArray
115119
type MacArray struct {
116120
array.ExtensionArrayBase

types/uuid.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ func (b *UUIDBuilder) UnmarshalJSON(data []byte) error {
113113
return b.Unmarshal(dec)
114114
}
115115

116+
func (b *UUIDBuilder) NewUUIDArray() *UUIDArray {
117+
return b.NewExtensionArray().(*UUIDArray)
118+
}
119+
116120
// UUIDArray is a simple array which is a FixedSizeBinary(16)
117121
type UUIDArray struct {
118122
array.ExtensionArrayBase

0 commit comments

Comments
 (0)