-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtableref_test.go
More file actions
441 lines (379 loc) · 11.2 KB
/
tableref_test.go
File metadata and controls
441 lines (379 loc) · 11.2 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
package airport_test
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/apache/arrow-go/v18/arrow"
"github.com/hugr-lab/airport-go"
"github.com/hugr-lab/airport-go/catalog"
)
// Integration tests for TableRef support.
// Tests validate:
// - Data query via read_csv function call (SELECT from table ref)
// - Catalog discovery (table visible via duckdb_tables())
// - Column metadata correctness (via duckdb_columns())
// - Coexistence with regular tables
// - Multiple table refs in the same schema
// simpleTableRef implements catalog.TableRef for integration testing.
type simpleTableRef struct {
name string
comment string
schema *arrow.Schema
csvURL string
}
func (t *simpleTableRef) Name() string { return t.name }
func (t *simpleTableRef) Comment() string { return t.comment }
func (t *simpleTableRef) ArrowSchema() *arrow.Schema { return t.schema }
func (t *simpleTableRef) FunctionCalls(ctx context.Context, req *catalog.FunctionCallRequest) ([]catalog.FunctionCall, error) {
return []catalog.FunctionCall{
{
FunctionName: "read_csv",
Args: []catalog.FunctionCallArg{
{Value: t.csvURL, Type: arrow.BinaryTypes.String},
{Name: "header", Value: true, Type: arrow.FixedWidthTypes.Boolean},
},
},
}, nil
}
// staticFuncRef implements catalog.TableRef with a generate_series call.
type staticFuncRef struct {
name string
comment string
schema *arrow.Schema
}
func (t *staticFuncRef) Name() string { return t.name }
func (t *staticFuncRef) Comment() string { return t.comment }
func (t *staticFuncRef) ArrowSchema() *arrow.Schema { return t.schema }
func (t *staticFuncRef) FunctionCalls(ctx context.Context, req *catalog.FunctionCallRequest) ([]catalog.FunctionCall, error) {
return []catalog.FunctionCall{
{
FunctionName: "generate_series",
Args: []catalog.FunctionCallArg{
{Value: int64(1), Type: arrow.PrimitiveTypes.Int64},
{Value: int64(10), Type: arrow.PrimitiveTypes.Int64},
},
},
}, nil
}
// TestTableRefSelectData verifies that SELECT queries against a table ref
// return data from the DuckDB function call (read_csv).
func TestTableRefSelectData(t *testing.T) {
// Create a temp CSV file with test data
tmpDir := t.TempDir()
csvPath := filepath.Join(tmpDir, "data.csv")
csvContent := "id,name,value\n1,Alice,10.5\n2,Bob,20.3\n3,Charlie,30.1\n"
if err := os.WriteFile(csvPath, []byte(csvContent), 0o644); err != nil {
t.Fatalf("Failed to write CSV: %v", err)
}
refSchema := arrow.NewSchema([]arrow.Field{
{Name: "id", Type: arrow.PrimitiveTypes.Int64},
{Name: "name", Type: arrow.BinaryTypes.String},
{Name: "value", Type: arrow.PrimitiveTypes.Float64},
}, nil)
ref := &simpleTableRef{
name: "csv_data",
schema: refSchema,
csvURL: csvPath,
}
cat, err := airport.NewCatalogBuilder().
Schema("refs").
TableRef(ref).
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, "")
query := fmt.Sprintf("SELECT * FROM %s.refs.csv_data ORDER BY id", attachName)
rows, err := db.Query(query)
if err != nil {
t.Fatalf("Query failed: %v", err)
}
defer rows.Close()
expected := []struct {
id int64
name string
value float64
}{
{1, "Alice", 10.5},
{2, "Bob", 20.3},
{3, "Charlie", 30.1},
}
idx := 0
for rows.Next() {
var id int64
var name string
var value float64
if err := rows.Scan(&id, &name, &value); err != nil {
t.Fatalf("Scan failed: %v", err)
}
if idx < len(expected) {
if id != expected[idx].id {
t.Errorf("Row %d: expected id %d, got %d", idx, expected[idx].id, id)
}
if name != expected[idx].name {
t.Errorf("Row %d: expected name %q, got %q", idx, expected[idx].name, name)
}
if value != expected[idx].value {
t.Errorf("Row %d: expected value %f, got %f", idx, expected[idx].value, value)
}
}
idx++
}
if idx != len(expected) {
t.Fatalf("Expected %d rows, got %d", len(expected), idx)
}
}
// TestTableRefDiscovery verifies that a TableRef appears as a normal table
// in DuckDB's catalog with correct metadata.
func TestTableRefDiscovery(t *testing.T) {
refSchema := arrow.NewSchema([]arrow.Field{
{Name: "id", Type: arrow.PrimitiveTypes.Int64},
{Name: "name", Type: arrow.BinaryTypes.String},
{Name: "value", Type: arrow.PrimitiveTypes.Float64},
}, nil)
ref := &simpleTableRef{
name: "csv_data",
comment: "Remote CSV data",
schema: refSchema,
csvURL: "https://example.com/data.csv",
}
cat, err := airport.NewCatalogBuilder().
Schema("refs").
TableRef(ref).
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("TableVisible", func(t *testing.T) {
query := "SELECT table_name FROM duckdb_tables() WHERE database_name = ? AND schema_name = 'refs'"
rows, err := db.Query(query, attachName)
if err != nil {
t.Fatalf("Query failed: %v", err)
}
defer rows.Close()
found := false
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
t.Fatalf("Scan failed: %v", err)
}
if name == "csv_data" {
found = true
}
}
if !found {
t.Error("Table 'csv_data' not found in duckdb_tables()")
}
})
t.Run("DescribeColumns", func(t *testing.T) {
query := `SELECT column_name, data_type
FROM duckdb_columns()
WHERE database_name = ?
AND schema_name = 'refs'
AND table_name = 'csv_data'
ORDER BY column_index`
rows, err := db.Query(query, attachName)
if err != nil {
t.Fatalf("Query failed: %v", err)
}
defer rows.Close()
expectedColumns := []struct {
name string
dataType string
}{
{"id", "BIGINT"},
{"name", "VARCHAR"},
{"value", "DOUBLE"},
}
idx := 0
for rows.Next() {
var colName, colType string
if err := rows.Scan(&colName, &colType); err != nil {
t.Fatalf("Scan failed: %v", err)
}
if idx < len(expectedColumns) {
if colName != expectedColumns[idx].name {
t.Errorf("Column %d: expected name %q, got %q", idx, expectedColumns[idx].name, colName)
}
if colType != expectedColumns[idx].dataType {
t.Errorf("Column %d: expected type %q, got %q", idx, expectedColumns[idx].dataType, colType)
}
}
idx++
}
if idx != len(expectedColumns) {
t.Errorf("Expected %d columns, got %d", len(expectedColumns), idx)
}
})
}
// TestTableRefCoexistsWithRegularTables verifies that table refs and regular
// tables can coexist in the same schema. Regular table queries must work
// even when table refs are present.
func TestTableRefCoexistsWithRegularTables(t *testing.T) {
tableSchema := arrow.NewSchema([]arrow.Field{
{Name: "id", Type: arrow.PrimitiveTypes.Int64},
{Name: "name", Type: arrow.BinaryTypes.String},
}, nil)
usersData := [][]any{
{int64(1), "Alice"},
{int64(2), "Bob"},
}
refSchema := arrow.NewSchema([]arrow.Field{
{Name: "generate_series", Type: arrow.PrimitiveTypes.Int64},
}, nil)
ref := &staticFuncRef{
name: "series_data",
schema: refSchema,
}
cat, err := airport.NewCatalogBuilder().
Schema("refs").
SimpleTable(airport.SimpleTableDef{
Name: "users",
Comment: "User accounts",
Schema: tableSchema,
ScanFunc: makeScanFunc(tableSchema, usersData),
}).
TableRef(ref).
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, "")
// Regular table queries must still work alongside table refs
t.Run("QueryRegularTable", func(t *testing.T) {
query := fmt.Sprintf("SELECT * FROM %s.refs.users ORDER BY id", attachName)
rows, err := db.Query(query)
if err != nil {
t.Fatalf("Query failed: %v", err)
}
defer rows.Close()
count := 0
for rows.Next() {
var id int64
var name string
if err := rows.Scan(&id, &name); err != nil {
t.Fatalf("Scan failed: %v", err)
}
count++
}
if count != 2 {
t.Errorf("Expected 2 rows, got %d", count)
}
})
// Both table and table ref should be visible via duckdb_tables()
t.Run("BothVisible", func(t *testing.T) {
query := "SELECT table_name FROM duckdb_tables() WHERE database_name = ? AND schema_name = 'refs' ORDER BY table_name"
rows, err := db.Query(query, attachName)
if err != nil {
t.Fatalf("Query failed: %v", err)
}
defer rows.Close()
var tables []string
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
t.Fatalf("Scan failed: %v", err)
}
tables = append(tables, name)
}
if len(tables) != 2 {
t.Errorf("Expected 2 tables, got %d: %v", len(tables), tables)
}
})
}
// TestTableRefMultipleInSchema verifies that multiple table refs can be
// registered in the same schema.
func TestTableRefMultipleInSchema(t *testing.T) {
schema1 := arrow.NewSchema([]arrow.Field{
{Name: "id", Type: arrow.PrimitiveTypes.Int64},
}, nil)
schema2 := arrow.NewSchema([]arrow.Field{
{Name: "value", Type: arrow.PrimitiveTypes.Float64},
}, nil)
ref1 := &staticFuncRef{name: "series_a", schema: schema1}
ref2 := &staticFuncRef{name: "series_b", schema: schema2}
cat, err := airport.NewCatalogBuilder().
Schema("refs").
TableRef(ref1).
TableRef(ref2).
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, "")
query := "SELECT table_name FROM duckdb_tables() WHERE database_name = ? AND schema_name = 'refs' ORDER BY table_name"
rows, err := db.Query(query, attachName)
if err != nil {
t.Fatalf("Query failed: %v", err)
}
defer rows.Close()
var tables []string
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
t.Fatalf("Scan failed: %v", err)
}
tables = append(tables, name)
}
if len(tables) != 2 {
t.Fatalf("Expected 2 table refs, got %d: %v", len(tables), tables)
}
if tables[0] != "series_a" || tables[1] != "series_b" {
t.Errorf("Expected [series_a, series_b], got %v", tables)
}
// Verify each table ref has correct columns
for _, tc := range []struct {
table string
colName string
colType string
}{
{"series_a", "id", "BIGINT"},
{"series_b", "value", "DOUBLE"},
} {
q := `SELECT column_name, data_type
FROM duckdb_columns()
WHERE database_name = ?
AND schema_name = 'refs'
AND table_name = ?`
r, err := db.Query(q, attachName, tc.table)
if err != nil {
t.Fatalf("Query failed for %s: %v", tc.table, err)
}
if !r.Next() {
r.Close()
t.Errorf("No columns found for table %s", tc.table)
continue
}
var colName, colType string
if err := r.Scan(&colName, &colType); err != nil {
r.Close()
t.Fatalf("Scan failed: %v", err)
}
r.Close()
if colName != tc.colName {
t.Errorf("Table %s: expected column %q, got %q", tc.table, tc.colName, colName)
}
if colType != tc.colType {
t.Errorf("Table %s: expected type %q, got %q", tc.table, tc.colType, colType)
}
}
}