Skip to content

Commit 1132c02

Browse files
authored
fix(types): Extensions conversion with storage (#948)
This change ensures that we can construct a working extension array from the underlying storage. Specifically, there should be no difference between the array constructed from storage & the one constructed from the extension builder. Additionally, the constructed array should be intact (so that it son't panic if the underlying storage doesn't).
1 parent 32a0c05 commit 1132c02

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

types/extensions_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,46 @@ func TestValueStrRoundTrip(t *testing.T) {
100100
})
101101
}
102102
}
103+
104+
func TestStorageArrayConv(t *testing.T) {
105+
const amount = 100
106+
cases := []arrow.ExtensionType{ExtensionTypes.UUID, ExtensionTypes.MAC, ExtensionTypes.JSON, ExtensionTypes.Inet}
107+
for _, dt := range cases {
108+
t.Run(dt.String(), func(t *testing.T) {
109+
storageBuilder := array.NewBuilder(memory.DefaultAllocator, dt.StorageType())
110+
defer storageBuilder.Release()
111+
builder := array.NewBuilder(memory.DefaultAllocator, dt)
112+
defer builder.Release()
113+
114+
for i := 0; i < amount; i++ {
115+
if i%2 == 0 {
116+
storageBuilder.AppendNull()
117+
builder.AppendNull()
118+
continue
119+
}
120+
storageBuilder.AppendEmptyValue()
121+
builder.AppendEmptyValue()
122+
}
123+
124+
storage := storageBuilder.NewArray()
125+
defer storage.Release()
126+
arr := builder.NewArray().(array.ExtensionArray)
127+
defer arr.Release()
128+
129+
// check matching
130+
assert.True(t, array.Equal(storage, arr.Storage()))
131+
132+
// check that creating extension from storage matches
133+
fromStorage := array.NewExtensionArrayWithStorage(dt, storage)
134+
defer fromStorage.Release()
135+
136+
assert.True(t, array.Equal(arr, fromStorage))
137+
138+
// assert that no issues are in the fromStorage array
139+
for i := 0; i < fromStorage.Len(); i++ {
140+
assert.NotPanics(t, func() { fromStorage.ValueStr(i) })
141+
assert.Equal(t, arr.ValueStr(i), fromStorage.ValueStr(i))
142+
}
143+
})
144+
}
145+
}

types/inet.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ func NewInetBuilder(builder *array.ExtensionBuilder) *InetBuilder {
2020
return &InetBuilder{ExtensionBuilder: builder}
2121
}
2222

23-
func (b *InetBuilder) AppendEmptyValue() {
24-
const zeroIPNet = "0.0.0.0/0"
25-
b.ExtensionBuilder.Builder.(*array.BinaryBuilder).Append([]byte(zeroIPNet))
26-
}
27-
2823
func (b *InetBuilder) Append(v *net.IPNet) {
2924
if v == nil {
3025
b.AppendNull()
@@ -154,7 +149,14 @@ func (a *InetArray) Value(i int) *net.IPNet {
154149
if a.IsNull(i) {
155150
return nil
156151
}
157-
_, ipnet, err := net.ParseCIDR(string(a.Storage().(*array.Binary).Value(i)))
152+
cidr := string(a.Storage().(*array.Binary).Value(i))
153+
if len(cidr) == 0 {
154+
return &net.IPNet{
155+
IP: net.IPv4zero,
156+
Mask: make(net.IPMask, len(net.IPv4zero)),
157+
}
158+
}
159+
_, ipnet, err := net.ParseCIDR(cidr)
158160
if err != nil {
159161
panic(fmt.Errorf("invalid ip+net: %w", err))
160162
}

types/mac.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ func NewMACBuilder(builder *array.ExtensionBuilder) *MACBuilder {
2020
return &MACBuilder{ExtensionBuilder: builder}
2121
}
2222

23-
func (b *MACBuilder) AppendEmptyValue() {
24-
b.Append(make(net.HardwareAddr, 6))
25-
}
26-
2723
func (b *MACBuilder) Append(v net.HardwareAddr) {
2824
b.ExtensionBuilder.Builder.(*array.BinaryBuilder).Append(v[:])
2925
}
@@ -147,7 +143,13 @@ func (a *MACArray) Value(i int) net.HardwareAddr {
147143
if a.IsNull(i) {
148144
return nil
149145
}
150-
return net.HardwareAddr(a.Storage().(*array.Binary).Value(i))
146+
b := a.Storage().(*array.Binary).Value(i)
147+
if len(b) == 0 {
148+
const minMACLen = 6
149+
return make(net.HardwareAddr, minMACLen)
150+
}
151+
152+
return net.HardwareAddr(b)
151153
}
152154

153155
func (a *MACArray) ValueStr(i int) string {

0 commit comments

Comments
 (0)