-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdynamic_test.go
More file actions
420 lines (338 loc) · 9.82 KB
/
dynamic_test.go
File metadata and controls
420 lines (338 loc) · 9.82 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
package airport_test
import (
"context"
"sync"
"testing"
"time"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/array"
"github.com/hugr-lab/airport-go/catalog"
)
// dynamicTestCatalog implements a catalog that can change at runtime.
type dynamicTestCatalog struct {
mu sync.RWMutex
schemas map[string]*dynamicTestSchema
}
func newDynamicTestCatalog() *dynamicTestCatalog {
return &dynamicTestCatalog{
schemas: make(map[string]*dynamicTestSchema),
}
}
func (c *dynamicTestCatalog) addSchema(name string, schema *dynamicTestSchema) {
c.mu.Lock()
defer c.mu.Unlock()
c.schemas[name] = schema
}
func (c *dynamicTestCatalog) removeSchema(name string) {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.schemas, name)
}
func (c *dynamicTestCatalog) Schemas(ctx context.Context) ([]catalog.Schema, error) {
c.mu.RLock()
defer c.mu.RUnlock()
result := make([]catalog.Schema, 0, len(c.schemas))
for _, schema := range c.schemas {
result = append(result, schema)
}
return result, nil
}
func (c *dynamicTestCatalog) Schema(ctx context.Context, name string) (catalog.Schema, error) {
c.mu.RLock()
defer c.mu.RUnlock()
schema, ok := c.schemas[name]
if !ok {
return nil, nil
}
return schema, nil
}
// dynamicTestSchema implements a schema with mutable tables.
type dynamicTestSchema struct {
name string
comment string
mu sync.RWMutex
tables map[string]catalog.Table
}
func newDynamicTestSchema(name, comment string) *dynamicTestSchema {
return &dynamicTestSchema{
name: name,
comment: comment,
tables: make(map[string]catalog.Table),
}
}
func (s *dynamicTestSchema) addTable(table catalog.Table) {
s.mu.Lock()
defer s.mu.Unlock()
s.tables[table.Name()] = table
}
func (s *dynamicTestSchema) removeTable(name string) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.tables, name)
}
func (s *dynamicTestSchema) Name() string {
return s.name
}
func (s *dynamicTestSchema) Comment() string {
return s.comment
}
func (s *dynamicTestSchema) Tables(ctx context.Context) ([]catalog.Table, error) {
s.mu.RLock()
defer s.mu.RUnlock()
result := make([]catalog.Table, 0, len(s.tables))
for _, table := range s.tables {
result = append(result, table)
}
return result, nil
}
func (s *dynamicTestSchema) Table(ctx context.Context, name string) (catalog.Table, error) {
s.mu.RLock()
defer s.mu.RUnlock()
table, ok := s.tables[name]
if !ok {
return nil, nil
}
return table, nil
}
func (s *dynamicTestSchema) ScalarFunctions(ctx context.Context) ([]catalog.ScalarFunction, error) {
return nil, nil
}
func (s *dynamicTestSchema) TableFunctions(ctx context.Context) ([]catalog.TableFunction, error) {
return nil, nil
}
func (s *dynamicTestSchema) TableFunctionsInOut(ctx context.Context) ([]catalog.TableFunctionInOut, error) {
return nil, nil
}
// liveTable is a table whose data changes on each scan.
type liveTable struct {
name string
comment string
schema *arrow.Schema
getData func() [][]any
}
func (t *liveTable) Name() string {
return t.name
}
func (t *liveTable) Comment() string {
return t.comment
}
func (t *liveTable) ArrowSchema(columns []string) *arrow.Schema {
return catalog.ProjectSchema(t.schema, columns)
}
func (t *liveTable) Scan(ctx context.Context, opts *catalog.ScanOptions) (array.RecordReader, error) {
data := t.getData()
record := buildTestRecord(t.schema, data)
defer record.Release()
return array.NewRecordReader(t.schema, []arrow.RecordBatch{record})
}
// TestDynamicCatalog verifies that catalogs can change at runtime
// and clients see the updates.
func TestDynamicCatalog(t *testing.T) {
cat := newDynamicTestCatalog()
// Start with one schema
schema1 := newDynamicTestSchema("initial", "Initial schema")
cat.addSchema("initial", schema1)
server := newTestServer(t, cat, nil)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
attachName := connectToFlightServer(t, db, server.address, "")
// Test 1: Initial schema is visible
t.Run("InitialSchema", func(t *testing.T) {
query := "SELECT schema_name FROM duckdb_schemas() WHERE database_name = ?"
rows, err := db.Query(query, attachName)
if err != nil {
t.Fatalf("Query failed: %v", err)
}
defer rows.Close()
schemas := []string{}
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
t.Fatalf("Failed to scan: %v", err)
}
schemas = append(schemas, name)
}
found := false
for _, s := range schemas {
if s == "initial" {
found = true
break
}
}
if !found {
t.Errorf("Expected to find 'initial' schema, got: %v", schemas)
}
})
// Test 2: Add schema at runtime
t.Run("AddSchema", func(t *testing.T) {
schema2 := newDynamicTestSchema("dynamic", "Dynamically added schema")
cat.addSchema("dynamic", schema2)
// Note: DuckDB may cache catalog metadata, so newly added schemas
// might not be immediately visible without reconnection
// This test documents the expected behavior
t.Log("Schema added dynamically - may require reconnection to see")
})
// Test 3: Remove schema at runtime
t.Run("RemoveSchema", func(t *testing.T) {
cat.removeSchema("initial")
// Note: DuckDB may cache catalog metadata
t.Log("Schema removed dynamically - may require reconnection to see")
})
}
// TestLiveData verifies that table data can change between queries.
func TestLiveData(t *testing.T) {
cat := newDynamicTestCatalog()
schema := newDynamicTestSchema("live", "Schema with live data")
// Create table with counter that increments on each scan
counter := 0
counterMu := sync.Mutex{}
metricsSchema := arrow.NewSchema([]arrow.Field{
{Name: "metric", Type: arrow.BinaryTypes.String},
{Name: "value", Type: arrow.PrimitiveTypes.Int64},
}, nil)
liveMetrics := &liveTable{
name: "metrics",
comment: "Live metrics",
schema: metricsSchema,
getData: func() [][]any {
counterMu.Lock()
defer counterMu.Unlock()
counter++
return [][]any{
{"scan_count", int64(counter)},
}
},
}
schema.addTable(liveMetrics)
cat.addSchema("live", schema)
server := newTestServer(t, cat, nil)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
attachName := connectToFlightServer(t, db, server.address, "")
// Query multiple times - should get different values
t.Run("ChangingData", func(t *testing.T) {
query := "SELECT value FROM " + attachName + ".live.metrics WHERE metric = 'scan_count'"
// First query
var value1 int64
if err := db.QueryRow(query).Scan(&value1); err != nil {
t.Fatalf("First query failed: %v", err)
}
// Second query
var value2 int64
if err := db.QueryRow(query).Scan(&value2); err != nil {
t.Fatalf("Second query failed: %v", err)
}
// Values should be different (counter increments)
if value1 == value2 {
t.Logf("Note: Values are the same (%d), DuckDB may be caching results", value1)
} else {
t.Logf("Live data working: first=%d, second=%d", value1, value2)
}
})
}
// TestDynamicTables verifies that tables can be added/removed at runtime.
func TestDynamicTables(t *testing.T) {
cat := newDynamicTestCatalog()
schema := newDynamicTestSchema("dynamic", "Dynamic schema")
cat.addSchema("dynamic", schema)
server := newTestServer(t, cat, nil)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
attachName := connectToFlightServer(t, db, server.address, "")
// Test 1: No tables initially
t.Run("NoTables", func(t *testing.T) {
query := "SELECT table_name FROM duckdb_tables() WHERE schema_name = 'dynamic' AND database_name = ?"
rows, err := db.Query(query, attachName)
if err != nil {
t.Fatalf("Query failed: %v", err)
}
defer rows.Close()
tableCount := 0
for rows.Next() {
tableCount++
}
if tableCount != 0 {
t.Errorf("Expected 0 tables, got %d", tableCount)
}
})
// Test 2: Add table at runtime
t.Run("AddTable", func(t *testing.T) {
tableSchema := arrow.NewSchema([]arrow.Field{
{Name: "id", Type: arrow.PrimitiveTypes.Int64},
}, nil)
testData := [][]any{{int64(1)}}
newTable := catalog.NewStaticTable(
"test_table",
"Test table",
tableSchema,
makeScanFunc(tableSchema, testData),
)
schema.addTable(newTable)
// Note: May require reconnection to see new table
t.Log("Table added dynamically - may require reconnection to see")
})
// Test 3: Remove table at runtime
t.Run("RemoveTable", func(t *testing.T) {
schema.removeTable("test_table")
// Note: May require reconnection to see removal
t.Log("Table removed dynamically - may require reconnection to see")
})
}
// TestConcurrentCatalogAccess verifies that the catalog is thread-safe.
func TestConcurrentCatalogAccess(t *testing.T) {
cat := newDynamicTestCatalog()
schema := newDynamicTestSchema("concurrent", "Concurrent access test")
// Add a simple table
tableSchema := arrow.NewSchema([]arrow.Field{
{Name: "id", Type: arrow.PrimitiveTypes.Int64},
}, nil)
testData := [][]any{{int64(1)}}
table := catalog.NewStaticTable(
"test",
"Test table",
tableSchema,
makeScanFunc(tableSchema, testData),
)
schema.addTable(table)
cat.addSchema("concurrent", schema)
server := newTestServer(t, cat, nil)
defer server.stop()
db := openDuckDB(t)
defer db.Close()
attachName := connectToFlightServer(t, db, server.address, "")
// Run concurrent queries
t.Run("ConcurrentQueries", func(t *testing.T) {
var wg sync.WaitGroup
errors := make(chan error, 10)
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
query := "SELECT COUNT(*) FROM " + attachName + ".concurrent.test"
var count int64
if err := db.QueryRow(query).Scan(&count); err != nil {
errors <- err
}
}()
}
// Wait with timeout
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
// Success
case <-time.After(5 * time.Second):
t.Fatal("Concurrent queries timed out")
}
close(errors)
for err := range errors {
t.Errorf("Concurrent query error: %v", err)
}
})
}