-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparams_test.go
More file actions
292 lines (269 loc) · 7.46 KB
/
params_test.go
File metadata and controls
292 lines (269 loc) · 7.46 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
package gospice
import (
"context"
"testing"
"github.com/apache/arrow-go/v18/arrow"
)
func TestParamType(t *testing.T) {
tests := []struct {
name string
param Param
expectedType arrow.DataType
expectedValue interface{}
}{
{
name: "NewParam with int",
param: NewParam(int32(42)),
expectedType: nil, // Type should be inferred
expectedValue: int32(42),
},
{
name: "Int32Param",
param: Int32Param(42),
expectedType: arrow.PrimitiveTypes.Int32,
expectedValue: int32(42),
},
{
name: "StringParam",
param: StringParam("test"),
expectedType: arrow.BinaryTypes.String,
expectedValue: "test",
},
{
name: "Float64Param",
param: Float64Param(3.14),
expectedType: arrow.PrimitiveTypes.Float64,
expectedValue: float64(3.14),
},
{
name: "BoolParam",
param: BoolParam(true),
expectedType: arrow.FixedWidthTypes.Boolean,
expectedValue: true,
},
{
name: "Date32Param",
param: Date32Param(arrow.Date32(18628)),
expectedType: arrow.PrimitiveTypes.Date32,
expectedValue: arrow.Date32(18628),
},
{
name: "TimestampParam with microseconds",
param: TimestampParam(arrow.Timestamp(1609459200000000), arrow.Microsecond, "UTC"),
expectedType: &arrow.TimestampType{Unit: arrow.Microsecond, TimeZone: "UTC"},
expectedValue: arrow.Timestamp(1609459200000000),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.expectedType != nil {
// Compare type IDs instead of exact equality since some types like Timestamp are pointers
if tt.param.Type.ID() != tt.expectedType.ID() {
t.Errorf("expected type %v, got %v", tt.expectedType, tt.param.Type)
}
}
if tt.param.Value != tt.expectedValue {
t.Errorf("expected value %v, got %v", tt.expectedValue, tt.param.Value)
}
})
}
}
func TestTypedParamInference(t *testing.T) {
tests := []struct {
name string
param interface{}
wantType arrow.DataType
}{
{
name: "Inferred int32",
param: int32(42),
wantType: arrow.PrimitiveTypes.Int32,
},
{
name: "Explicit LargeString",
param: LargeStringParam("test"),
wantType: arrow.BinaryTypes.LargeString,
},
{
name: "Explicit Time32 with seconds",
param: Time32Param(arrow.Time32(43200), arrow.Second),
wantType: &arrow.Time32Type{Unit: arrow.Second},
},
{
name: "Explicit MonthInterval",
param: MonthIntervalParam(arrow.MonthInterval(12)),
wantType: arrow.FixedWidthTypes.MonthInterval,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Extract value and type
var val interface{}
var dataType arrow.DataType
if p, ok := tt.param.(Param); ok {
val = p.Value
if p.Type != nil {
dataType = p.Type
} else {
var err error
dataType, err = inferArrowType(p.Value)
if err != nil {
t.Fatalf("error inferring type: %v", err)
}
}
} else {
val = tt.param
var err error
dataType, err = inferArrowType(tt.param)
if err != nil {
t.Fatalf("error inferring type: %v", err)
}
}
// Verify we got a type
if dataType == nil {
t.Error("expected non-nil data type")
}
// Verify type matches expected
if dataType.ID() != tt.wantType.ID() {
t.Errorf("expected type ID %v, got %v", tt.wantType.ID(), dataType.ID())
}
// Verify value is not nil (except for explicit null)
if val == nil && tt.name != "Explicit null" {
t.Error("expected non-nil value")
}
})
}
}
func TestExtendedArrowTypes(t *testing.T) {
tests := []struct {
name string
value interface{}
expectedType arrow.DataType
shouldSucceed bool
}{
// Interval types
{
name: "MonthInterval",
value: arrow.MonthInterval(12),
expectedType: arrow.FixedWidthTypes.MonthInterval,
shouldSucceed: true,
},
{
name: "DayTimeInterval",
value: arrow.DayTimeInterval{Days: 1, Milliseconds: 1000},
expectedType: arrow.FixedWidthTypes.DayTimeInterval,
shouldSucceed: true,
},
{
name: "MonthDayNanoInterval",
value: arrow.MonthDayNanoInterval{Months: 1, Days: 2, Nanoseconds: 3000},
expectedType: arrow.FixedWidthTypes.MonthDayNanoInterval,
shouldSucceed: true,
},
// Decimal types (as byte arrays)
{
name: "Decimal128 bytes",
value: [16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
expectedType: &arrow.Decimal128Type{Precision: 38, Scale: 10},
shouldSucceed: true,
},
{
name: "Decimal256 bytes",
value: [32]byte{0, 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},
expectedType: &arrow.Decimal256Type{Precision: 76, Scale: 10},
shouldSucceed: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dataType, err := inferArrowType(tt.value)
if tt.shouldSucceed && err != nil {
t.Errorf("expected success, got error: %v", err)
}
if !tt.shouldSucceed && err == nil {
t.Errorf("expected error, got success")
}
if tt.shouldSucceed && dataType.ID() != tt.expectedType.ID() {
t.Errorf("expected type %v, got %v", tt.expectedType, dataType)
}
})
}
}
func TestSqlWithParamsUsingTypedParams(t *testing.T) {
spice := NewSpiceClient()
defer func() {
if err := spice.Close(); err != nil {
t.Errorf("error closing SpiceClient: %v", err)
}
}()
if err := spice.Init(); err != nil {
t.Skipf("Skipping - cannot initialize SpiceClient: %v", err)
}
ctx := context.Background()
tests := []struct {
name string
sql string
params []interface{}
}{
{
name: "Simple query with explicit Int32",
sql: "SELECT $1 as value",
params: []interface{}{Int32Param(42)},
},
{
name: "Query with mixed inferred and explicit params",
sql: "SELECT $1 as num, $2 as str",
params: []interface{}{42, StringParam("test")},
},
{
name: "Query with temporal param",
sql: "SELECT $1 as ts",
params: []interface{}{TimestampParam(arrow.Timestamp(1609459200000000), arrow.Microsecond, "UTC")},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reader, err := spice.SqlWithParams(ctx, tt.sql, tt.params...)
if err != nil {
t.Logf("Query failed (may be expected): %v", err)
return
}
defer reader.Release()
// Verify we can read results
if reader.Next() {
rec := reader.RecordBatch()
defer rec.Release()
if rec.NumRows() == 0 {
t.Error("expected at least one row")
}
}
})
}
}
func TestSqlWithParamsAlias(t *testing.T) {
spice := NewSpiceClient()
defer func() {
if err := spice.Close(); err != nil {
t.Errorf("error closing SpiceClient: %v", err)
}
}()
if err := spice.Init(); err != nil {
t.Skipf("Skipping - cannot initialize SpiceClient: %v", err)
}
ctx := context.Background()
// Test that SqlWithParams works with simple parameterized query
reader, err := spice.SqlWithParams(ctx, "SELECT $1 as value", 42)
if err != nil {
// Local instances may not support parameterized queries properly
// Skip the test gracefully instead of failing
t.Skipf("Skipping - parameterized queries may not be supported on local instance: %v", err)
}
defer reader.Release()
if reader.Next() {
rec := reader.RecordBatch()
defer rec.Release()
if rec.NumRows() == 0 {
t.Error("expected at least one row")
}
}
}