forked from parquet-go/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumn_buffer_int96_test.go
More file actions
299 lines (271 loc) · 7.5 KB
/
Copy pathcolumn_buffer_int96_test.go
File metadata and controls
299 lines (271 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package parquet
import (
"math"
"testing"
"github.com/parquet-go/parquet-go/deprecated"
)
// TestInt96ColumnBufferWriteTypes tests that int96ColumnBuffer
// correctly converts various types to int96
func TestColumnBufferInt96WriteTypes(t *testing.T) {
tests := []struct {
name string
writeOp func(*int96ColumnBuffer)
expected deprecated.Int96
}{
{
name: "writeBoolean_false",
writeOp: func(col *int96ColumnBuffer) {
col.writeBoolean(columnLevels{}, false)
},
expected: deprecated.Int96{0, 0, 0},
},
{
name: "writeBoolean_true",
writeOp: func(col *int96ColumnBuffer) {
col.writeBoolean(columnLevels{}, true)
},
expected: deprecated.Int96{1, 0, 0},
},
{
name: "writeInt32_positive",
writeOp: func(col *int96ColumnBuffer) {
col.writeInt32(columnLevels{}, 42)
},
expected: deprecated.Int32ToInt96(42),
},
{
name: "writeInt32_negative",
writeOp: func(col *int96ColumnBuffer) {
col.writeInt32(columnLevels{}, -123)
},
expected: deprecated.Int32ToInt96(-123),
},
{
name: "writeInt32_zero",
writeOp: func(col *int96ColumnBuffer) {
col.writeInt32(columnLevels{}, 0)
},
expected: deprecated.Int96{0, 0, 0},
},
{
name: "writeInt32_max",
writeOp: func(col *int96ColumnBuffer) {
col.writeInt32(columnLevels{}, math.MaxInt32)
},
expected: deprecated.Int32ToInt96(math.MaxInt32),
},
{
name: "writeInt32_min",
writeOp: func(col *int96ColumnBuffer) {
col.writeInt32(columnLevels{}, math.MinInt32)
},
expected: deprecated.Int32ToInt96(math.MinInt32),
},
{
name: "writeInt64_positive",
writeOp: func(col *int96ColumnBuffer) {
col.writeInt64(columnLevels{}, 12345)
},
expected: deprecated.Int64ToInt96(12345),
},
{
name: "writeInt64_negative",
writeOp: func(col *int96ColumnBuffer) {
col.writeInt64(columnLevels{}, -9876)
},
expected: deprecated.Int64ToInt96(-9876),
},
{
name: "writeInt64_max",
writeOp: func(col *int96ColumnBuffer) {
col.writeInt64(columnLevels{}, math.MaxInt64)
},
expected: deprecated.Int64ToInt96(math.MaxInt64),
},
{
name: "writeInt64_min",
writeOp: func(col *int96ColumnBuffer) {
col.writeInt64(columnLevels{}, math.MinInt64)
},
expected: deprecated.Int64ToInt96(math.MinInt64),
},
{
name: "writeInt96",
writeOp: func(col *int96ColumnBuffer) {
col.writeInt96(columnLevels{}, deprecated.Int96{0x12345678, 0xABCDEF01, 0x87654321})
},
expected: deprecated.Int96{0x12345678, 0xABCDEF01, 0x87654321},
},
{
name: "writeFloat_positive",
writeOp: func(col *int96ColumnBuffer) {
col.writeFloat(columnLevels{}, 3.14)
},
expected: deprecated.Int64ToInt96(3),
},
{
name: "writeFloat_negative",
writeOp: func(col *int96ColumnBuffer) {
col.writeFloat(columnLevels{}, -2.9)
},
expected: deprecated.Int64ToInt96(-2),
},
{
name: "writeDouble_positive",
writeOp: func(col *int96ColumnBuffer) {
col.writeDouble(columnLevels{}, 42.7)
},
expected: deprecated.Int64ToInt96(42),
},
{
name: "writeDouble_negative",
writeOp: func(col *int96ColumnBuffer) {
col.writeDouble(columnLevels{}, -100.5)
},
expected: deprecated.Int64ToInt96(-100),
},
{
name: "writeByteArray_valid_positive",
writeOp: func(col *int96ColumnBuffer) {
col.writeByteArray(columnLevels{}, []byte("42"))
},
expected: deprecated.Int64ToInt96(42),
},
{
name: "writeByteArray_valid_negative",
writeOp: func(col *int96ColumnBuffer) {
col.writeByteArray(columnLevels{}, []byte("-999"))
},
expected: deprecated.Int64ToInt96(-999),
},
{
name: "writeByteArray_zero",
writeOp: func(col *int96ColumnBuffer) {
col.writeByteArray(columnLevels{}, []byte("0"))
},
expected: deprecated.Int96{0, 0, 0},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
col := newInt96ColumnBuffer(Int96Type, 0, 10)
tt.writeOp(col)
if col.Len() != 1 {
t.Fatalf("expected 1 value, got %d", col.Len())
}
if col.values[0] != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, col.values[0])
}
})
}
}
// TestInt96ColumnBufferWriteByteArrayInvalid tests that invalid strings panic
func TestColumnBufferInt96WriteByteArrayInvalid(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expected panic for invalid byte array")
}
}()
col := newInt96ColumnBuffer(Int96Type, 0, 10)
col.writeByteArray(columnLevels{}, []byte("not a number"))
}
// TestInt96ColumnBufferWriteMultipleValues tests writing multiple values
func TestColumnBufferInt96WriteMultipleValues(t *testing.T) {
col := newInt96ColumnBuffer(Int96Type, 0, 10)
col.writeInt32(columnLevels{}, 1)
col.writeInt64(columnLevels{}, 2)
col.writeFloat(columnLevels{}, 3.9)
col.writeBoolean(columnLevels{}, true)
col.writeInt96(columnLevels{}, deprecated.Int96{42, 0, 0})
if col.Len() != 5 {
t.Fatalf("expected 5 values, got %d", col.Len())
}
expected := []deprecated.Int96{
deprecated.Int32ToInt96(1),
deprecated.Int64ToInt96(2),
deprecated.Int64ToInt96(3),
deprecated.Int96{1, 0, 0},
deprecated.Int96{42, 0, 0},
}
for i, exp := range expected {
if col.values[i] != exp {
t.Errorf("index %d: expected %v, got %v", i, exp, col.values[i])
}
}
}
// TestInt96ColumnBufferWriteNull tests that writeNull still panics
func TestColumnBufferInt96WriteNull(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("expected panic for writeNull")
}
}()
col := newInt96ColumnBuffer(Int96Type, 0, 10)
col.writeNull(columnLevels{})
}
// TestInt96ColumnBufferPrecisionLoss documents precision loss for large values
func TestColumnBufferInt96PrecisionLoss(t *testing.T) {
t.Run("large_float", func(t *testing.T) {
col := newInt96ColumnBuffer(Int96Type, 0, 10)
col.writeFloat(columnLevels{}, 1e10)
// Float32 loses precision when converted to int64
// This test just documents the behavior
_ = col.values[0]
})
t.Run("large_double", func(t *testing.T) {
col := newInt96ColumnBuffer(Int96Type, 0, 10)
col.writeDouble(columnLevels{}, 1e15)
// Conversion to int64 may lose precision for very large doubles
_ = col.values[0]
})
}
// TestInt96ColumnBufferReadWriteRoundTrip tests round-trip conversion
func TestColumnBufferInt96ReadWriteRoundTrip(t *testing.T) {
col := newInt96ColumnBuffer(Int96Type, 0, 10)
original := deprecated.Int96{0x12345678, 0xABCDEF01, 0x87654321}
col.writeInt96(columnLevels{}, original)
if col.Len() != 1 {
t.Fatalf("expected 1 value, got %d", col.Len())
}
values := make([]Value, 1)
n, err := col.ReadValuesAt(values, 0)
if err != nil || n != 1 {
t.Fatalf("failed to read value: %v", err)
}
result := values[0].Int96()
if result != original {
t.Errorf("round-trip failed: expected %v, got %v", original, result)
}
}
// TestInt96ColumnBufferSignedConversion tests signed int conversions
func TestColumnBufferInt96SignedConversion(t *testing.T) {
tests := []struct {
name string
write func(*int96ColumnBuffer)
}{
{
name: "negative_int32",
write: func(col *int96ColumnBuffer) {
col.writeInt32(columnLevels{}, -42)
},
},
{
name: "negative_int64",
write: func(col *int96ColumnBuffer) {
col.writeInt64(columnLevels{}, -12345)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
col := newInt96ColumnBuffer(Int96Type, 0, 10)
tt.write(col)
// Just verify that negative values are handled
if col.Len() != 1 {
t.Fatalf("expected 1 value, got %d", col.Len())
}
// The value should be stored correctly
_ = col.values[0]
})
}
}