-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscalar_functions_test.go
More file actions
1151 lines (944 loc) · 31.3 KB
/
scalar_functions_test.go
File metadata and controls
1151 lines (944 loc) · 31.3 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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package airport_test
import (
"context"
"fmt"
"strings"
"testing"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/apache/arrow-go/v18/arrow/memory"
"github.com/hugr-lab/airport-go"
"github.com/hugr-lab/airport-go/catalog"
)
// TestScalarFunctions verifies that scalar functions can be discovered
// and invoked through the Flight server.
func TestScalarFunctions(t *testing.T) {
cat := catalogWithScalarFunctions()
server := newTestServer(t, cat, nil)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
attachName := connectToFlightServer(t, db, server.address, "")
// Test 1: Discover scalar functions
t.Run("DiscoverFunctions", func(t *testing.T) {
// DuckDB may expose functions through system tables
// This test documents expected behavior
query := "SELECT function_name FROM duckdb_functions() WHERE schema_name = 'some_schema' AND function_type = 'scalar'"
rows, err := db.Query(query)
if err != nil {
t.Fatalf("Function discovery query failed: %v", err)
}
defer rows.Close()
functions := []string{}
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
t.Fatalf("Failed to scan: %v", err)
}
functions = append(functions, name)
}
t.Logf("Discovered functions: %v", functions)
})
// Test 2: Call scalar function
t.Run("CallFunction", func(t *testing.T) {
// Scalar functions are supported via Airport DoExchange protocol.
// This test verifies that scalar functions can be called from DuckDB.
// Call UPPERCASE function
query := "SELECT " + attachName + ".some_schema.UPPERCASE(name) as upper_name FROM " + attachName + ".some_schema.users WHERE id = 1"
var upperName string
err := db.QueryRow(query).Scan(&upperName)
if err != nil {
t.Fatalf("Scalar function calls failed in Airport extension: %v", err)
}
if upperName != "ALICE" {
t.Errorf("Expected 'ALICE', got '%s'", upperName)
}
})
// Test 3: Function metadata
t.Run("FunctionMetadata", func(t *testing.T) {
// Verify that function comments and signatures are accessible via catalog
// We can test this through the Airport schema serialization
// Query the catalog to get function information
// The functions should be discoverable in the catalog metadata
query := `
SELECT function_name, parameter_types, return_type
FROM duckdb_functions()
WHERE schema_name = 'some_schema'
AND function_type = 'scalar'
ORDER BY function_name
`
rows, err := db.Query(query)
if err != nil {
t.Fatalf("Function metadata query failed: %v", err)
}
defer rows.Close()
foundFunctions := make(map[string]bool)
for rows.Next() {
var funcName, returnType string
var paramTypes []any
if err := rows.Scan(&funcName, ¶mTypes, &returnType); err != nil {
t.Fatalf("Failed to scan function metadata: %v", err)
}
foundFunctions[funcName] = true
t.Logf("Found function: %s, params: %s, return: %s", funcName, paramTypes, returnType)
}
// Verify expected functions are present
expectedFunctions := []string{"UPPERCASE", "LENGTH"}
for _, expected := range expectedFunctions {
if !foundFunctions[expected] {
t.Errorf("Expected function %s not found in catalog", expected)
}
}
if len(foundFunctions) < len(expectedFunctions) {
t.Logf("Note: Only %d of %d expected functions found - DuckDB may not fully expose Airport function metadata yet",
len(foundFunctions), len(expectedFunctions))
}
})
}
// TestFunctionSignatures verifies that function signatures are correctly
// validated and enforced.
func TestFunctionSignatures(t *testing.T) {
cat := catalogWithScalarFunctions()
server := newTestServer(t, cat, nil)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
attachName := connectToFlightServer(t, db, server.address, "")
// Test 1: Verify function accepts correct input types
t.Run("CorrectInputType", func(t *testing.T) {
// Test that UPPERCASE accepts string input
query := fmt.Sprintf("SELECT %s.some_schema.UPPERCASE('hello') as result", attachName)
var result string
err := db.QueryRow(query).Scan(&result)
if err != nil {
t.Fatalf("Scalar function call failed: %v", err)
return
}
if result != "HELLO" {
t.Errorf("Expected 'HELLO', got '%s'", result)
}
// Test that LENGTH accepts string input
query = fmt.Sprintf("SELECT %s.some_schema.LENGTH('test') as result", attachName)
var length int64
err = db.QueryRow(query).Scan(&length)
if err != nil {
t.Errorf("LENGTH function failed with correct type: %v", err)
}
if length != 4 {
t.Errorf("Expected length 4, got %d", length)
}
})
// Test 2: Verify function rejects incorrect input types
t.Run("IncorrectInputType", func(t *testing.T) {
// Try to call UPPERCASE with an integer (should fail)
query := fmt.Sprintf("SELECT %s.some_schema.UPPERCASE(123) as result", attachName)
var result string
err := db.QueryRow(query).Scan(&result)
// We expect this to fail with a type error
if err == nil {
t.Fatalf("Note: DuckDB may perform automatic type coercion (123 -> '123')")
}
})
// Test 3: Verify return type is correct
t.Run("ReturnType", func(t *testing.T) {
// Verify UPPERCASE returns string
query := fmt.Sprintf("SELECT typeof(%s.some_schema.UPPERCASE('test')) as result_type", attachName)
var resultType string
err := db.QueryRow(query).Scan(&resultType)
if err != nil {
t.Fatalf("Query failed: %v", err)
}
// DuckDB should report VARCHAR or STRING type
if resultType != "VARCHAR" {
t.Logf("Note: Return type is '%s' (expected VARCHAR or STRING)", resultType)
}
// Verify LENGTH returns integer
query = fmt.Sprintf("SELECT typeof(%s.some_schema.LENGTH('test')) as result_type", attachName)
err = db.QueryRow(query).Scan(&resultType)
if err != nil {
t.Errorf("typeof() failed for LENGTH: %v", err)
}
// DuckDB should report BIGINT or INTEGER type
if resultType != "BIGINT" && resultType != "INTEGER" {
t.Logf("Note: Return type is '%s' (expected BIGINT or INTEGER)", resultType)
}
})
}
// TestVectorizedExecution verifies that scalar functions process large batches
// efficiently (100k+ rows) rather than row-by-row.
func TestVectorizedExecution(t *testing.T) {
cat := catalogWithScalarFunctions()
server := newTestServer(t, cat, nil)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
attachName := connectToFlightServer(t, db, server.address, "")
t.Run("LargeBatchProcessing", func(t *testing.T) {
// Generate 100k rows and process them through scalar function
// This verifies vectorized execution at scale
const numRows = 100000
// Use DuckDB's generate_series to create large dataset
// Then apply scalar function to each row
query := fmt.Sprintf(`
SELECT COUNT(*) as cnt
FROM (
SELECT %s.some_schema.LENGTH(CAST(i AS VARCHAR)) as len
FROM generate_series(1, %d) AS t(i)
)
`, attachName, numRows)
var count int64
err := db.QueryRow(query).Scan(&count)
if err != nil {
t.Fatalf("Scalar function call failed: %v", err)
return
}
// Should process all rows
if count != numRows {
t.Errorf("Expected %d rows, got %d", numRows, count)
}
t.Logf("Successfully processed %d rows through vectorized scalar function", count)
})
t.Run("MultipleOperations", func(t *testing.T) {
// Test multiple scalar function calls in same query with large dataset
const numRows = 50000
query := fmt.Sprintf(`
SELECT COUNT(*) as cnt
FROM (
SELECT
%s.some_schema.UPPERCASE(CAST(i AS VARCHAR)) as upper_val,
%s.some_schema.LENGTH(CAST(i AS VARCHAR)) as len_val
FROM generate_series(1, %d) AS t(i)
)
`, attachName, attachName, numRows)
var count int64
err := db.QueryRow(query).Scan(&count)
if err != nil {
t.Fatalf("Multiple scalar function calls failed: %v", err)
}
if count != numRows {
t.Errorf("Expected %d rows, got %d", numRows, count)
}
t.Logf("Successfully processed %d rows through multiple vectorized functions", count)
})
}
// TestFunctionErrors verifies that function errors are properly propagated.
func TestFunctionErrors(t *testing.T) {
var _ catalog.ScalarFunction = (*errorFunc)(nil)
ef := &errorFunc{}
t.Run("ErrorPropagation", func(t *testing.T) {
// Create catalog with error-throwing function
usersSchema := arrow.NewSchema([]arrow.Field{
{Name: "id", Type: arrow.PrimitiveTypes.Int64},
}, nil)
testData := [][]any{{int64(1)}, {int64(2)}, {int64(3)}}
cat, err := airport.NewCatalogBuilder().
Schema("some_schema").
SimpleTable(airport.SimpleTableDef{
Name: "test",
Schema: usersSchema,
ScanFunc: makeScanFunc(usersSchema, testData),
}).
ScalarFunc(ef).
Build()
if err != nil {
t.Fatalf("Failed to build catalog: %v", err)
}
server := newTestServer(t, cat, nil)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
attachName := connectToFlightServer(t, db, server.address, "")
// Try to call the error function - should get error from server
query := fmt.Sprintf("SELECT %s.some_schema.ERROR_FUNC(id) FROM %s.some_schema.test",
attachName, attachName)
var result int64
err = db.QueryRow(query).Scan(&result)
// Should receive an error (either from function execution or connection)
if err == nil {
t.Fatalf("Expected error from ERROR_FUNC, but query succeeded")
}
t.Logf("Error correctly propagated: %v", err)
// Verify error message contains our intentional error
if !strings.Contains(err.Error(), "intentional error") &&
!strings.Contains(err.Error(), "function execution failed") {
t.Logf("Note: Error message doesn't contain expected text, but error was propagated")
}
// enshure that the duckdb is still working after the error
var count int64
err = db.QueryRow("SELECT COUNT(*) FROM " + attachName + ".some_schema.test").Scan(&count)
if err != nil {
t.Errorf("Failed to query after error: %v", err)
}
})
}
// TestTypeMismatch verifies that type mismatch is detected.
func TestTypeMismatch(t *testing.T) {
var _ catalog.ScalarFunction = (*wrongTypeFunc)(nil)
wtf := &wrongTypeFunc{}
t.Run("TypeMismatchDetection", func(t *testing.T) {
// Create catalog with type-mismatching function
usersSchema := arrow.NewSchema([]arrow.Field{
{Name: "id", Type: arrow.PrimitiveTypes.Int64},
}, nil)
testData := [][]any{{int64(1)}, {int64(2)}}
cat, err := airport.NewCatalogBuilder().
Schema("some_schema").
SimpleTable(airport.SimpleTableDef{
Name: "test",
Schema: usersSchema,
ScanFunc: makeScanFunc(usersSchema, testData),
}).
ScalarFunc(wtf).
Build()
if err != nil {
t.Fatalf("Failed to build catalog: %v", err)
}
server := newTestServer(t, cat, nil)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
attachName := connectToFlightServer(t, db, server.address, "")
// Try to call the function with wrong return type
// Our DoExchange should detect type mismatch and return error
query := fmt.Sprintf("SELECT %s.some_schema.WRONG_TYPE(id) FROM %s.some_schema.test",
attachName, attachName)
var result int64
err = db.QueryRow(query).Scan(&result)
// Should receive an error due to type mismatch
if err == nil {
t.Errorf("Expected error from type mismatch, but query succeeded")
} else {
t.Logf("Type mismatch correctly detected: %v", err)
// Verify error message mentions type mismatch
if strings.Contains(err.Error(), "type mismatch") ||
strings.Contains(err.Error(), "expected int64") ||
strings.Contains(err.Error(), "got utf8") {
t.Logf("Error message correctly indicates type mismatch")
} else {
t.Logf("Note: Error occurred but message doesn't explicitly mention type mismatch")
}
}
})
}
// TestScalarFunctionDataTypes tests scalar functions with different Arrow data types.
func TestScalarFunctionDataTypes(t *testing.T) {
testData := [][]any{
{int64(10), int64(5), 2.5, 3.0, "hello", "world"},
{int64(-5), int64(3), 1.5, 2.0, "foo", "bar"},
{int64(0), int64(0), 0.0, 0.0, "test", "case"},
}
testSchema := arrow.NewSchema([]arrow.Field{
{Name: "int_a", Type: arrow.PrimitiveTypes.Int64},
{Name: "int_b", Type: arrow.PrimitiveTypes.Int64},
{Name: "float_a", Type: arrow.PrimitiveTypes.Float64},
{Name: "float_b", Type: arrow.PrimitiveTypes.Float64},
{Name: "str_a", Type: arrow.BinaryTypes.String},
{Name: "str_b", Type: arrow.BinaryTypes.String},
}, nil)
cat, err := airport.NewCatalogBuilder().
Schema("test_schema").
SimpleTable(airport.SimpleTableDef{
Name: "test_data",
Schema: testSchema,
ScanFunc: makeScanFunc(testSchema, testData),
}).
ScalarFunc(&addFunc{}).
ScalarFunc(&multiplyFunc{}).
ScalarFunc(&concatFunc{}).
ScalarFunc(&isPositiveFunc{}).
Build()
if err != nil {
t.Fatalf("Failed to build catalog: %v", err)
}
server := newTestServer(t, cat, nil)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
attachName := connectToFlightServer(t, db, server.address, "")
t.Run("IntegerAddition", func(t *testing.T) {
query := fmt.Sprintf("SELECT %s.test_schema.ADD(int_a, int_b) as sum FROM %s.test_schema.test_data", attachName, attachName)
rows, err := db.Query(query)
if err != nil {
t.Fatalf("Scalar function call failed: %v", err)
return
}
defer rows.Close()
expected := []int64{15, -2, 0}
idx := 0
for rows.Next() {
var sum int64
if err := rows.Scan(&sum); err != nil {
t.Fatalf("Failed to scan: %v", err)
}
if sum != expected[idx] {
t.Errorf("Row %d: expected %d, got %d", idx, expected[idx], sum)
}
idx++
}
if idx != len(expected) {
t.Errorf("Expected %d rows, got %d", len(expected), idx)
}
})
t.Run("FloatMultiplication", func(t *testing.T) {
query := fmt.Sprintf("SELECT %s.test_schema.MULTIPLY(float_a, float_b) as product FROM %s.test_schema.test_data", attachName, attachName)
rows, err := db.Query(query)
if err != nil {
t.Fatalf("Scalar function call failed: %v", err)
}
defer rows.Close()
expected := []float64{7.5, 3.0, 0.0}
idx := 0
for rows.Next() {
var product float64
if err := rows.Scan(&product); err != nil {
t.Fatalf("Failed to scan: %v", err)
}
if product != expected[idx] {
t.Errorf("Row %d: expected %f, got %f", idx, expected[idx], product)
}
idx++
}
if idx != len(expected) {
t.Errorf("Expected %d rows, got %d", len(expected), idx)
}
})
t.Run("StringConcatenation", func(t *testing.T) {
query := fmt.Sprintf("SELECT %s.test_schema.CONCAT(str_a, str_b) as concatenated FROM %s.test_schema.test_data", attachName, attachName)
rows, err := db.Query(query)
if err != nil {
t.Fatalf("Scalar function calls failed: %v", err)
}
defer rows.Close()
expected := []string{"hello world", "foo bar", "test case"}
idx := 0
for rows.Next() {
var concatenated string
if err := rows.Scan(&concatenated); err != nil {
t.Fatalf("Failed to scan: %v", err)
}
if concatenated != expected[idx] {
t.Errorf("Row %d: expected %s, got %s", idx, expected[idx], concatenated)
}
idx++
}
if idx != len(expected) {
t.Errorf("Expected %d rows, got %d", len(expected), idx)
}
})
t.Run("BooleanReturn", func(t *testing.T) {
query := fmt.Sprintf("SELECT %s.test_schema.IS_POSITIVE(int_a) as is_pos FROM %s.test_schema.test_data", attachName, attachName)
rows, err := db.Query(query)
if err != nil {
t.Fatalf("Scalar function call failed: %v", err)
return
}
defer rows.Close()
expected := []bool{true, false, false}
idx := 0
for rows.Next() {
var isPos bool
if err := rows.Scan(&isPos); err != nil {
t.Fatalf("Failed to scan: %v", err)
}
if isPos != expected[idx] {
t.Errorf("Row %d: expected %t, got %t", idx, expected[idx], isPos)
}
idx++
}
if idx != len(expected) {
t.Errorf("Expected %d rows, got %d", len(expected), idx)
}
})
}
// TestScalarFunctionNullHandling tests NULL value handling in scalar functions.
func TestScalarFunctionNullHandling(t *testing.T) {
// Create test data with NULLs
testSchema := arrow.NewSchema([]arrow.Field{
{Name: "value", Type: arrow.BinaryTypes.String, Nullable: true},
}, nil)
// Build record with NULLs
builder := array.NewRecordBuilder(memory.DefaultAllocator, testSchema)
defer builder.Release()
strBuilder := builder.Field(0).(*array.StringBuilder)
strBuilder.Append("hello")
strBuilder.AppendNull()
strBuilder.Append("world")
record := builder.NewRecordBatch()
defer record.Release()
scanFunc := func(ctx context.Context, opts *catalog.ScanOptions) (array.RecordReader, error) {
record.Retain() // Retain for reader
return array.NewRecordReader(testSchema, []arrow.RecordBatch{record})
}
cat, err := airport.NewCatalogBuilder().
Schema("test_schema").
SimpleTable(airport.SimpleTableDef{
Name: "nullable_data",
Schema: testSchema,
ScanFunc: scanFunc,
}).
ScalarFunc(&uppercaseFunc{}).
ScalarFunc(&lengthFunc{}).
Build()
if err != nil {
t.Fatalf("Failed to build catalog: %v", err)
}
server := newTestServer(t, cat, nil)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
attachName := connectToFlightServer(t, db, server.address, "")
t.Run("UppercaseWithNulls", func(t *testing.T) {
query := fmt.Sprintf("SELECT %s.test_schema.UPPERCASE(value) as upper_val FROM %s.test_schema.nullable_data", attachName, attachName)
rows, err := db.Query(query)
if err != nil {
t.Fatalf("Scalar function call failed: %v", err)
return
}
defer rows.Close()
idx := 0
for rows.Next() {
var upperVal *string
if err := rows.Scan(&upperVal); err != nil {
t.Fatalf("Failed to scan: %v", err)
}
switch idx {
case 0:
if upperVal == nil || *upperVal != "HELLO" {
t.Errorf("Row 0: expected 'HELLO', got %v", upperVal)
}
case 1:
if upperVal != nil {
t.Errorf("Row 1: expected NULL, got %v", upperVal)
}
case 2:
if upperVal == nil || *upperVal != "WORLD" {
t.Errorf("Row 2: expected 'WORLD', got %v", upperVal)
}
}
idx++
}
if idx != 3 {
t.Errorf("Expected 3 rows, got %d", idx)
}
})
t.Run("LengthWithNulls", func(t *testing.T) {
query := fmt.Sprintf("SELECT %s.test_schema.LENGTH(value) as len FROM %s.test_schema.nullable_data", attachName, attachName)
rows, err := db.Query(query)
if err != nil {
t.Fatalf("Scalar function call failed: %v", err)
return
}
defer rows.Close()
idx := 0
for rows.Next() {
var length *int64
if err := rows.Scan(&length); err != nil {
t.Fatalf("Failed to scan: %v", err)
}
switch idx {
case 0:
if length == nil || *length != 5 {
t.Errorf("Row 0: expected 5, got %v", length)
}
case 1:
if length != nil {
t.Errorf("Row 1: expected NULL, got %v", length)
}
case 2:
if length == nil || *length != 5 {
t.Errorf("Row 2: expected 5, got %v", length)
}
}
idx++
}
if idx != 3 {
t.Errorf("Expected 3 rows, got %d", idx)
}
})
}
// TestScalarFunctionEmptyBatch tests scalar function behavior with empty batches.
func TestScalarFunctionEmptyBatch(t *testing.T) {
testSchema := arrow.NewSchema([]arrow.Field{
{Name: "value", Type: arrow.BinaryTypes.String},
}, nil)
// Create empty record
builder := array.NewRecordBuilder(memory.DefaultAllocator, testSchema)
defer builder.Release()
emptyRecord := builder.NewRecordBatch()
defer emptyRecord.Release()
scanFunc := func(ctx context.Context, opts *catalog.ScanOptions) (array.RecordReader, error) {
emptyRecord.Retain() // Retain for reader
return array.NewRecordReader(testSchema, []arrow.RecordBatch{emptyRecord})
}
cat, err := airport.NewCatalogBuilder().
Schema("test_schema").
SimpleTable(airport.SimpleTableDef{
Name: "empty_data",
Schema: testSchema,
ScanFunc: scanFunc,
}).
ScalarFunc(&uppercaseFunc{}).
Build()
if err != nil {
t.Fatalf("Failed to build catalog: %v", err)
}
server := newTestServer(t, cat, nil)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
attachName := connectToFlightServer(t, db, server.address, "")
t.Run("EmptyBatch", func(t *testing.T) {
query := fmt.Sprintf("SELECT %s.test_schema.UPPERCASE(value) as upper_val FROM %s.test_schema.empty_data", attachName, attachName)
rows, err := db.Query(query)
if err != nil {
t.Fatalf("Scalar function call failed: %v", err)
return
}
defer rows.Close()
rowCount := 0
for rows.Next() {
rowCount++
}
if rowCount != 0 {
t.Errorf("Expected 0 rows, got %d", rowCount)
}
})
}
// TestScalarFunctionMultipleBatches tests scalar function with streaming/multiple batches.
func TestScalarFunctionMultipleBatches(t *testing.T) {
testSchema := arrow.NewSchema([]arrow.Field{
{Name: "value", Type: arrow.PrimitiveTypes.Int64},
}, nil)
// Create multiple batches
var records []arrow.RecordBatch
for batchNum := 0; batchNum < 3; batchNum++ {
builder := array.NewRecordBuilder(memory.DefaultAllocator, testSchema)
intBuilder := builder.Field(0).(*array.Int64Builder)
for i := 0; i < 100; i++ {
intBuilder.Append(int64(batchNum*100 + i))
}
record := builder.NewRecordBatch()
records = append(records, record)
builder.Release()
}
scanFunc := func(ctx context.Context, opts *catalog.ScanOptions) (array.RecordReader, error) {
// Retain all records for the reader
for _, r := range records {
r.Retain()
}
return array.NewRecordReader(testSchema, records)
}
cat, err := airport.NewCatalogBuilder().
Schema("test_schema").
SimpleTable(airport.SimpleTableDef{
Name: "large_data",
Schema: testSchema,
ScanFunc: scanFunc,
}).
ScalarFunc(&isPositiveFunc{}).
Build()
if err != nil {
t.Fatalf("Failed to build catalog: %v", err)
}
// Clean up records
defer func() {
for _, r := range records {
r.Release()
}
}()
server := newTestServer(t, cat, nil)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
attachName := connectToFlightServer(t, db, server.address, "")
t.Run("MultipleBatches", func(t *testing.T) {
query := fmt.Sprintf("SELECT COUNT(*) as cnt FROM %s.test_schema.large_data WHERE %s.test_schema.IS_POSITIVE(value) = true", attachName, attachName)
var count int64
err := db.QueryRow(query).Scan(&count)
if err != nil {
t.Fatalf("Scalar function call failed: %v", err)
return
}
// All values except the first one (0) are positive
expected := int64(299) // (100 * 3) - 1
if count != expected {
t.Errorf("Expected %d positive values, got %d", expected, count)
}
})
}
// uppercaseFunc is a simple scalar function that converts strings to uppercase.
type uppercaseFunc struct{}
func (f *uppercaseFunc) Name() string {
return "UPPERCASE"
}
func (f *uppercaseFunc) Comment() string {
return "Converts all characters in a string to uppercase"
}
func (f *uppercaseFunc) Signature() catalog.FunctionSignature {
return catalog.FunctionSignature{
Parameters: []arrow.DataType{arrow.BinaryTypes.String},
ReturnType: arrow.BinaryTypes.String,
}
}
func (f *uppercaseFunc) Execute(ctx context.Context, input arrow.RecordBatch) (arrow.Array, error) {
if input.NumCols() != 1 {
return nil, fmt.Errorf("UPPERCASE expects exactly 1 column, got %d", input.NumCols())
}
inputCol := input.Column(0)
inputArray, ok := inputCol.(*array.String)
if !ok {
return nil, fmt.Errorf("UPPERCASE expects string column, got %T", inputCol)
}
builder := array.NewStringBuilder(memory.DefaultAllocator)
defer builder.Release()
for i := 0; i < inputArray.Len(); i++ {
if inputArray.IsNull(i) {
builder.AppendNull()
} else {
value := inputArray.Value(i)
builder.Append(strings.ToUpper(value))
}
}
return builder.NewStringArray(), nil
}
// lengthFunc returns the length of strings.
type lengthFunc struct{}
func (f *lengthFunc) Name() string {
return "LENGTH"
}
func (f *lengthFunc) Comment() string {
return "Returns the length of a string in characters"
}
func (f *lengthFunc) Signature() catalog.FunctionSignature {
return catalog.FunctionSignature{
Parameters: []arrow.DataType{arrow.BinaryTypes.String},
ReturnType: arrow.PrimitiveTypes.Int64,
}
}
func (f *lengthFunc) Execute(ctx context.Context, input arrow.RecordBatch) (arrow.Array, error) {
if input.NumCols() != 1 {
return nil, fmt.Errorf("LENGTH expects exactly 1 column, got %d", input.NumCols())
}
inputCol := input.Column(0)
inputArray, ok := inputCol.(*array.String)
if !ok {
return nil, fmt.Errorf("LENGTH expects string column, got %T", inputCol)
}
builder := array.NewInt64Builder(memory.DefaultAllocator)
defer builder.Release()
for i := 0; i < inputArray.Len(); i++ {
if inputArray.IsNull(i) {
builder.AppendNull()
} else {
value := inputArray.Value(i)
builder.Append(int64(len(value)))
}
}
return builder.NewInt64Array(), nil
}
// concatFunc concatenates two strings (multi-parameter function).
type concatFunc struct{}
func (f *concatFunc) Name() string {
return "CONCAT"
}
func (f *concatFunc) Comment() string {
return "Concatenates two strings with a space separator"
}
func (f *concatFunc) Signature() catalog.FunctionSignature {
return catalog.FunctionSignature{
Parameters: []arrow.DataType{arrow.BinaryTypes.String, arrow.BinaryTypes.String},
ReturnType: arrow.BinaryTypes.String,
}
}
func (f *concatFunc) Execute(ctx context.Context, input arrow.RecordBatch) (arrow.Array, error) {
if input.NumCols() != 2 {
return nil, fmt.Errorf("CONCAT expects exactly 2 columns, got %d", input.NumCols())
}
inputCol1, ok1 := input.Column(0).(*array.String)
inputCol2, ok2 := input.Column(1).(*array.String)
if !ok1 || !ok2 {
return nil, fmt.Errorf("CONCAT expects string columns, got %T and %T", input.Column(0), input.Column(1))
}
builder := array.NewStringBuilder(memory.DefaultAllocator)
defer builder.Release()
for i := 0; i < inputCol1.Len(); i++ {
if inputCol1.IsNull(i) || inputCol2.IsNull(i) {
builder.AppendNull()
} else {
result := inputCol1.Value(i) + " " + inputCol2.Value(i)
builder.Append(result)
}
}
return builder.NewStringArray(), nil
}
// addFunc adds two int64 values (numeric function).
type addFunc struct{}
func (f *addFunc) Name() string {
return "ADD"
}
func (f *addFunc) Comment() string {
return "Adds two integer values"
}
func (f *addFunc) Signature() catalog.FunctionSignature {
return catalog.FunctionSignature{
Parameters: []arrow.DataType{arrow.PrimitiveTypes.Int64, arrow.PrimitiveTypes.Int64},
ReturnType: arrow.PrimitiveTypes.Int64,
}
}
func (f *addFunc) Execute(ctx context.Context, input arrow.RecordBatch) (arrow.Array, error) {
if input.NumCols() != 2 {
return nil, fmt.Errorf("ADD expects exactly 2 columns, got %d", input.NumCols())
}
inputCol1, ok1 := input.Column(0).(*array.Int64)
inputCol2, ok2 := input.Column(1).(*array.Int64)
if !ok1 || !ok2 {
return nil, fmt.Errorf("ADD expects int64 columns, got %T and %T", input.Column(0), input.Column(1))
}
builder := array.NewInt64Builder(memory.DefaultAllocator)
defer builder.Release()
for i := 0; i < inputCol1.Len(); i++ {
if inputCol1.IsNull(i) || inputCol2.IsNull(i) {
builder.AppendNull()
} else {
result := inputCol1.Value(i) + inputCol2.Value(i)
builder.Append(result)
}
}
return builder.NewInt64Array(), nil
}
// multiplyFunc multiplies two float64 values.
type multiplyFunc struct{}
func (f *multiplyFunc) Name() string {
return "MULTIPLY"
}
func (f *multiplyFunc) Comment() string {
return "Multiplies two float values"
}
func (f *multiplyFunc) Signature() catalog.FunctionSignature {
return catalog.FunctionSignature{
Parameters: []arrow.DataType{arrow.PrimitiveTypes.Float64, arrow.PrimitiveTypes.Float64},
ReturnType: arrow.PrimitiveTypes.Float64,
}
}
func (f *multiplyFunc) Execute(ctx context.Context, input arrow.RecordBatch) (arrow.Array, error) {