-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadbc_test.go
More file actions
552 lines (482 loc) · 15.1 KB
/
adbc_test.go
File metadata and controls
552 lines (482 loc) · 15.1 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
package gospice
import (
"context"
"os"
"testing"
"github.com/apache/arrow-go/v18/arrow/array"
)
// TestADBCCloudBasicQuery tests basic ADBC query functionality against Spice Cloud
func TestADBCCloudBasicQuery(t *testing.T) {
// Uses SPICE_FLIGHT_URL and SPICE_HTTP_URL env vars if set
// e.g., SPICE_FLIGHT_URL="flight.spiceai.io:443" SPICE_HTTP_URL="https://data.spiceai.io"
spice := NewSpiceClient()
defer func() {
if err := spice.Close(); err != nil {
t.Logf("warning: failed to close SpiceClient: %v", err)
}
}()
ApiKey, exists := os.LookupEnv("SPICE_API_KEY")
if !exists || ApiKey == "" {
t.Skip("SPICE_API_KEY not set, skipping cloud authentication test")
}
if err := spice.Init(WithApiKey(ApiKey), WithSpiceCloudAddress()); err != nil {
t.Fatalf("error initializing SpiceClient: %v", err)
}
// Check if Spice Cloud is healthy and ready
ctx := context.Background()
if !spice.IsSpiceHealthy(ctx) {
t.Fatal("Spice Cloud is not healthy")
}
if !spice.IsSpiceReady(ctx) {
t.Fatal("Spice Cloud is not ready (check API key)")
}
t.Run("Cloud - Simple ADBC Query", func(t *testing.T) {
reader, err := spice.SqlWithParams(context.Background(), "SELECT 1 as number")
if err != nil {
t.Fatalf("error querying: %v", err)
}
defer reader.Release()
schema := reader.Schema()
if !schema.HasField("number") {
t.Fatalf("Schema does not have field 'number'")
}
for reader.Next() {
record := reader.RecordBatch()
if record.NumRows() == 0 {
record.Release()
t.Fatalf("Expected at least 1 row, got %d", record.NumRows())
}
record.Release()
}
})
t.Run("Cloud - Parameterized Query", func(t *testing.T) {
reader, err := spice.SqlWithParams(
context.Background(),
"SELECT trip_distance, fare_amount FROM taxi_trips WHERE trip_distance > $1 ORDER BY trip_distance LIMIT 5",
5.0,
)
if err != nil {
t.Fatalf("error querying: %v", err)
}
defer reader.Release()
schema := reader.Schema()
if !schema.HasField("trip_distance") {
t.Fatalf("Schema does not have field 'trip_distance'")
}
if !schema.HasField("fare_amount") {
t.Fatalf("Schema does not have field 'fare_amount'")
}
recordCount := 0
for reader.Next() {
record := reader.RecordBatch()
recordCount++
// Validate trip_distance > 5.0
tripDistCol := record.Column(0)
for i := 0; i < int(record.NumRows()); i++ {
if !tripDistCol.IsNull(i) {
switch v := tripDistCol.(type) {
case *array.Float64:
if v.Value(i) <= 5.0 {
t.Errorf("Expected trip_distance > 5.0, got %f", v.Value(i))
}
case *array.Float32:
if v.Value(i) <= 5.0 {
t.Errorf("Expected trip_distance > 5.0, got %f", v.Value(i))
}
}
}
}
record.Release()
}
if recordCount > 5 {
t.Fatalf("Expected at most 5 records, got %d", recordCount)
}
})
t.Run("Cloud - Multiple Parameter Types", func(t *testing.T) {
reader, err := spice.SqlWithParams(
context.Background(),
"SELECT trip_distance, fare_amount, payment_type FROM taxi_trips WHERE trip_distance > $1 AND fare_amount > $2 AND payment_type = $3 ORDER BY trip_distance LIMIT 10",
5.0,
20.0,
int64(1),
)
if err != nil {
t.Fatalf("error querying: %v", err)
}
defer reader.Release()
schema := reader.Schema()
fields := []string{"trip_distance", "fare_amount", "payment_type"}
for _, field := range fields {
if !schema.HasField(field) {
t.Fatalf("Schema does not have field '%s'", field)
}
}
recordCount := 0
for reader.Next() {
record := reader.RecordBatch()
recordCount++
// Validate all parameter conditions
for i := 0; i < int(record.NumRows()); i++ {
// trip_distance > 5.0
tripDistCol := record.Column(0)
if !tripDistCol.IsNull(i) {
switch v := tripDistCol.(type) {
case *array.Float64:
if v.Value(i) <= 5.0 {
t.Errorf("Expected trip_distance > 5.0, got %f", v.Value(i))
}
case *array.Float32:
if v.Value(i) <= 5.0 {
t.Errorf("Expected trip_distance > 5.0, got %f", v.Value(i))
}
}
}
// fare_amount > 20.0
fareCol := record.Column(1)
if !fareCol.IsNull(i) {
switch v := fareCol.(type) {
case *array.Float64:
if v.Value(i) <= 20.0 {
t.Errorf("Expected fare_amount > 20.0, got %f", v.Value(i))
}
case *array.Float32:
if v.Value(i) <= 20.0 {
t.Errorf("Expected fare_amount > 20.0, got %f", v.Value(i))
}
}
}
// payment_type = 1
paymentTypeCol := record.Column(2)
if !paymentTypeCol.IsNull(i) {
switch v := paymentTypeCol.(type) {
case *array.Int64:
if v.Value(i) != 1 {
t.Errorf("Expected payment_type 1, got %d", v.Value(i))
}
case *array.Int32:
if v.Value(i) != 1 {
t.Errorf("Expected payment_type 1, got %d", v.Value(i))
}
}
}
}
record.Release()
}
if recordCount == 0 {
t.Log("Note: Query returned 0 rows, which is valid if no data matches the criteria")
}
})
}
// TestADBCLocalParameterizedQuery tests parameterized query functionality with local runtime
func TestADBCLocalParameterizedQuery(t *testing.T) {
spice := NewSpiceClient()
defer func() {
if err := spice.Close(); err != nil {
t.Logf("warning: failed to close SpiceClient: %v", err)
}
}()
if err := spice.Init(); err != nil {
t.Fatalf("error initializing SpiceClient: %v", err)
}
// Check if local Spice runtime is healthy
ctx := context.Background()
if !spice.IsSpiceHealthy(ctx) {
t.Fatal("local Spice runtime is not healthy")
}
t.Run("Local - Parameterized Query with Float", func(t *testing.T) {
sql := "SELECT trip_distance, fare_amount FROM taxi_trips WHERE trip_distance > $1 ORDER BY trip_distance LIMIT 5"
reader, err := spice.SqlWithParams(context.Background(), sql, 10.0)
if err != nil {
t.Fatalf("error executing parameterized query with float: %v", err)
}
defer reader.Release()
schema := reader.Schema()
if !schema.HasField("trip_distance") {
t.Fatalf("Schema does not have field 'trip_distance'")
}
if !schema.HasField("fare_amount") {
t.Fatalf("Schema does not have field 'fare_amount'")
}
rowCount := 0
for reader.Next() {
record := reader.RecordBatch()
// Validate trip_distance is > 10.0 (the parameterized value)
col0 := record.Column(0)
for i := 0; i < int(record.NumRows()); i++ {
rowCount++
if col0.IsNull(i) {
continue
}
switch v := col0.(type) {
case *array.Float64:
if v.Value(i) <= 10.0 {
t.Errorf("Parameterized query failed: Expected trip_distance > 10.0, got %f", v.Value(i))
}
case *array.Float32:
if v.Value(i) <= 10.0 {
t.Errorf("Parameterized query failed: Expected trip_distance > 10.0, got %f", v.Value(i))
}
}
}
record.Release()
}
if rowCount == 0 {
t.Fatalf("Expected at least 1 row from parameterized query, got 0")
}
if rowCount > 5 {
t.Fatalf("Expected at most 5 rows (LIMIT 5), got %d", rowCount)
}
t.Logf("Parameterized query with float returned %d rows", rowCount)
})
t.Run("Local - Parameterized Query with Multiple Parameters", func(t *testing.T) {
sql := "SELECT trip_distance, fare_amount, payment_type FROM taxi_trips WHERE trip_distance > $1 AND fare_amount > $2 ORDER BY trip_distance, fare_amount LIMIT 3"
reader, err := spice.SqlWithParams(context.Background(), sql, 5.0, 20.0)
if err != nil {
t.Fatalf("error executing parameterized query with multiple params: %v", err)
}
defer reader.Release()
schema := reader.Schema()
if !schema.HasField("trip_distance") {
t.Fatalf("Schema does not have field 'trip_distance'")
}
if !schema.HasField("fare_amount") {
t.Fatalf("Schema does not have field 'fare_amount'")
}
rowCount := 0
for reader.Next() {
record := reader.RecordBatch()
// Validate both parameters are correctly applied
tripDistCol := record.Column(0)
fareCol := record.Column(1)
for i := 0; i < int(record.NumRows()); i++ {
rowCount++
// Validate trip_distance > 5.0 ($1 parameter)
if !tripDistCol.IsNull(i) {
switch v := tripDistCol.(type) {
case *array.Float64:
if v.Value(i) <= 5.0 {
t.Errorf("Parameterized query failed ($1): Expected trip_distance > 5.0, got %f", v.Value(i))
}
case *array.Float32:
if v.Value(i) <= 5.0 {
t.Errorf("Parameterized query failed ($1): Expected trip_distance > 5.0, got %f", v.Value(i))
}
}
}
// Validate fare_amount > 20.0 ($2 parameter)
if !fareCol.IsNull(i) {
switch v := fareCol.(type) {
case *array.Float64:
if v.Value(i) <= 20.0 {
t.Errorf("Parameterized query failed ($2): Expected fare_amount > 20.0, got %f", v.Value(i))
}
case *array.Float32:
if v.Value(i) <= 20.0 {
t.Errorf("Parameterized query failed ($2): Expected fare_amount > 20.0, got %f", v.Value(i))
}
}
}
}
record.Release()
}
if rowCount == 0 {
t.Fatalf("Expected at least 1 row from parameterized query, got 0")
}
if rowCount > 3 {
t.Fatalf("Expected at most 3 rows (LIMIT 3), got %d", rowCount)
}
t.Logf("Parameterized query with multiple params returned %d rows", rowCount)
})
t.Run("Local - Parameterized Query with String", func(t *testing.T) {
// Use store_and_fwd_flag which is the string column in taxi_trips (large_utf8)
// "N" means not a store-and-forward trip, "Y" means it was
sql := "SELECT trip_distance, fare_amount, store_and_fwd_flag FROM taxi_trips WHERE store_and_fwd_flag = $1 ORDER BY trip_distance LIMIT 5"
reader, err := spice.SqlWithParams(context.Background(), sql, "N")
if err != nil {
t.Fatalf("error executing parameterized query with string: %v", err)
}
defer reader.Release()
schema := reader.Schema()
if !schema.HasField("store_and_fwd_flag") {
t.Fatalf("Schema does not have field 'store_and_fwd_flag'")
}
rowCount := 0
for reader.Next() {
record := reader.RecordBatch()
// Validate store_and_fwd_flag matches the string parameter exactly
flagCol := record.Column(2)
for i := 0; i < int(record.NumRows()); i++ {
rowCount++
if !flagCol.IsNull(i) {
var flag string
switch v := flagCol.(type) {
case *array.String:
flag = v.Value(i)
case *array.LargeString:
flag = v.Value(i)
}
if flag != "N" {
t.Errorf("Parameterized query failed: Expected store_and_fwd_flag 'N', got '%s'", flag)
}
}
}
record.Release()
}
if rowCount == 0 {
t.Fatalf("Expected at least 1 row from parameterized query, got 0")
}
if rowCount > 5 {
t.Fatalf("Expected at most 5 rows (LIMIT 5), got %d", rowCount)
}
t.Logf("Parameterized query with string returned %d rows", rowCount)
})
t.Run("Local - Parameterized Query with Integer", func(t *testing.T) {
// Use payment_type which is an integer column (int64)
// payment_type: 1=Credit Card, 2=Cash, 3=No Charge, 4=Dispute, 0=Unknown
sql := "SELECT trip_distance, fare_amount, payment_type FROM taxi_trips WHERE payment_type = $1 ORDER BY trip_distance LIMIT 5"
reader, err := spice.SqlWithParams(context.Background(), sql, int64(1))
if err != nil {
t.Fatalf("error executing parameterized query with integer: %v", err)
}
defer reader.Release()
schema := reader.Schema()
if !schema.HasField("payment_type") {
t.Fatalf("Schema does not have field 'payment_type'")
}
rowCount := 0
for reader.Next() {
record := reader.RecordBatch()
// Validate payment_type matches the integer parameter exactly
paymentTypeCol := record.Column(2)
for i := 0; i < int(record.NumRows()); i++ {
rowCount++
if !paymentTypeCol.IsNull(i) {
switch v := paymentTypeCol.(type) {
case *array.Int64:
if v.Value(i) != 1 {
t.Errorf("Parameterized query failed: Expected payment_type 1, got %d", v.Value(i))
}
case *array.Int32:
if v.Value(i) != 1 {
t.Errorf("Parameterized query failed: Expected payment_type 1, got %d", v.Value(i))
}
}
}
}
record.Release()
}
if rowCount == 0 {
t.Fatalf("Expected at least 1 row from parameterized query, got 0")
}
if rowCount > 5 {
t.Fatalf("Expected at most 5 rows (LIMIT 5), got %d", rowCount)
}
t.Logf("Parameterized query with integer returned %d rows", rowCount)
})
}
// TestADBCLocalBasicQuery tests basic ADBC query functionality with local Spice runtime
func TestADBCLocalBasicQuery(t *testing.T) {
spice := NewSpiceClient()
defer func() {
if err := spice.Close(); err != nil {
t.Logf("warning: failed to close SpiceClient: %v", err)
}
}()
if err := spice.Init(); err != nil {
t.Fatalf("error initializing SpiceClient: %v", err)
}
// Check if local Spice runtime is healthy
ctx := context.Background()
if !spice.IsSpiceHealthy(ctx) {
t.Fatal("local Spice runtime is not healthy")
}
t.Run("Local - Query Dataset with ADBC", func(t *testing.T) {
reader, err := spice.SqlWithParams(context.Background(),
"SELECT trip_distance, fare_amount FROM taxi_trips WHERE trip_distance > 0 ORDER BY trip_distance LIMIT 3")
if err != nil {
t.Fatalf("error executing ADBC query: %v", err)
}
defer reader.Release()
schema := reader.Schema()
if !schema.HasField("trip_distance") {
t.Fatalf("Schema does not have field 'trip_distance'")
}
if !schema.HasField("fare_amount") {
t.Fatalf("Schema does not have field 'fare_amount'")
}
rowCount := 0
for reader.Next() {
record := reader.RecordBatch()
// Validate trip_distance is positive (query condition)
col0 := record.Column(0)
for i := 0; i < int(record.NumRows()); i++ {
rowCount++
if col0.IsNull(i) {
continue
}
switch v := col0.(type) {
case *array.Float64:
if v.Value(i) <= 0 {
t.Errorf("Expected trip_distance > 0, got %f", v.Value(i))
}
case *array.Float32:
if v.Value(i) <= 0 {
t.Errorf("Expected trip_distance > 0, got %f", v.Value(i))
}
}
}
record.Release()
}
if rowCount == 0 {
t.Fatalf("Expected at least 1 row, got 0")
}
t.Logf("ADBC query returned %d rows", rowCount)
})
t.Run("Local - Simple SELECT 1", func(t *testing.T) {
reader, err := spice.SqlWithParams(context.Background(), "SELECT 1 as num")
if err != nil {
t.Fatalf("error executing ADBC query: %v", err)
}
defer reader.Release()
schema := reader.Schema()
if !schema.HasField("num") {
t.Fatalf("Schema does not have field 'num'")
}
rowCount := 0
for reader.Next() {
record := reader.RecordBatch()
rowCount += int(record.NumRows())
record.Release()
}
if rowCount != 1 {
t.Fatalf("Expected exactly 1 row for SELECT 1, got %d", rowCount)
}
})
}
// TestParameterTypes tests different parameter types
func TestParameterTypes(t *testing.T) {
tests := []struct {
name string
value interface{}
}{
{"int32", int32(42)},
{"int64", int64(9223372036854775807)},
{"float32", float32(3.14)},
{"float64", float64(2.718281828)},
{"string", "hello world"},
{"bool", true},
{"uint32", uint32(123)},
{"uint64", uint64(456)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dataType, err := inferArrowType(tt.value)
if err != nil {
t.Fatalf("error inferring type for %v: %v", tt.value, err)
}
if dataType == nil {
t.Fatalf("got nil data type for %v", tt.value)
}
})
}
}