Repository navigation
Expand file tree
/
Copy pathengine_test.go
More file actions
752 lines (697 loc) · 29.7 KB
/
Copy pathengine_test.go
File metadata and controls
752 lines (697 loc) · 29.7 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
package sqlb_test
import (
"errors"
"strings"
"testing"
"github.com/ubgo/sqlb"
"github.com/ubgo/sqlb/ch"
"github.com/ubgo/sqlb/pg"
"github.com/ubgo/sqlb/sqlite"
)
// A complete engine package, written as if it lived in another repository.
//
// This file is in package sqlb_test, so it can reach nothing unexported. That
// is the point: if it compiles and passes, a community package has everything
// the engine packages in this module have. Nothing here is privileged, and no
// list inside sqlb was edited to make this engine exist.
//
// The engine is DuckDB, chosen because it has a clause Postgres does not,
// QUALIFY, which filters on a window result without a subquery. It needs a
// position no other engine uses, which is exactly the case that used to be
// impossible from outside.
const duckEngine = "duckdb"
type duckDialect struct{}
func (duckDialect) Placeholder(int) string { return "?" }
func (duckDialect) QuoteIdent(id string) string { return sqlb.Quote(id, '"') }
func (duckDialect) Name() string { return duckEngine }
func (duckDialect) SupportsReturning() bool { return true }
// QuoteLiteral is what an outside dialect writes when its constructs take
// literals, as read_parquet takes a path. DuckDB strings take no backslash
// escapes, so doubling the quote is the whole rule. A dialect that leaves this
// out cannot be handed a literal: EngineQuoteLit fails closed rather than
// guessing a quoting rule, because the rule a guess would pick, doubling the
// quote, is an injection on any engine that treats a backslash as an escape.
func (duckDialect) QuoteLiteral(v any) (string, error) {
s, ok := v.(string)
if !ok {
return "", sqlb.ErrNotInlinable
}
return "'" + strings.ReplaceAll(s, "'", "''") + "'", nil
}
// bareDialect is an outside dialect with no LiteralQuoting at all.
type bareDialect struct{}
func (bareDialect) Placeholder(int) string { return "?" }
func (bareDialect) QuoteIdent(id string) string { return sqlb.Quote(id, '"') }
func (bareDialect) Name() string { return duckEngine }
// The whole of the engine package's plumbing: one line each, over the core.
func duckExpr[T any](c string, r func(*sqlb.Args) string) sqlb.Expr[T] {
return sqlb.EngineExpr[T](duckEngine, c, r)
}
func duckFn[T any](c, name string, args ...sqlb.SQLer) sqlb.Expr[T] {
return sqlb.EngineFn[T](duckEngine, c, name, args...)
}
func duckPred(c string, r func(*sqlb.Args) string) sqlb.Predicate {
return sqlb.EnginePred(duckEngine, c, r)
}
func duckBinPred(c, op string, l, r sqlb.SQLer) sqlb.Predicate {
return sqlb.EngineBinPred(duckEngine, c, op, l, r)
}
func duckClause[K any](c string, slot sqlb.Slot, r func(*sqlb.Args) string) sqlb.Clause[duckDialect, K] {
return sqlb.EngineClause[duckDialect, K](c, slot, r)
}
// Constructs only this engine has.
// ListValue renders list_value(items...), DuckDB's array constructor.
func duckListValue[T any](items ...sqlb.Expr[T]) sqlb.Expr[[]T] {
args := make([]sqlb.SQLer, len(items))
for i, it := range items {
args[i] = it
}
return duckFn[[]T]("duck.ListValue", "list_value", args...)
}
// Qualify renders QUALIFY, which filters on a window result. It goes between
// WINDOW and ORDER BY, a position the core gained for exactly this shape.
// duckProbe is a marker clause typed to the statement kind K, for the tests
// that walk every slot of the write statements.
func duckProbe[K any](slot sqlb.Slot, marker string) sqlb.Clause[duckDialect, K] {
return duckClause[K]("duck.Probe", slot, func(*sqlb.Args) string { return marker })
}
func duckQualify(p sqlb.Predicate) sqlb.Clause[duckDialect, sqlb.SelectKind] {
return duckClause[sqlb.SelectKind]("duck.Qualify", sqlb.SlotBeforeOrder, func(a *sqlb.Args) string {
return "QUALIFY " + sqlb.PredicateSQL(p, a)
})
}
// TryCast renders TRY_CAST(e AS type), which returns NULL instead of raising
// on a value that will not convert. It is built with the general expression
// constructor because its shape is not a function call.
func duckTryCast[R, T any](e sqlb.Expr[T], sqlType string) sqlb.Expr[R] {
return duckExpr[R]("duck.TryCast", func(a *sqlb.Args) string {
return "TRY_CAST(" + e.SQL(a) + " AS " + sqlType + ")"
})
}
// AllNotNull renders (a IS NOT NULL AND b IS NOT NULL ...), a predicate whose
// shape is neither a function nor a binary operator, so it uses the general
// predicate constructor.
func duckAllNotNull(cols ...sqlb.SQLer) sqlb.Predicate {
return duckPred("duck.AllNotNull", func(a *sqlb.Args) string {
parts := make([]string, len(cols))
for i, c := range cols {
parts[i] = c.SQL(a) + " IS NOT NULL"
}
return "(" + strings.Join(parts, " AND ") + ")"
})
}
// Similar renders a ~~~ b, so the test covers a binary predicate too.
func duckSimilar(a, b sqlb.Expr[string]) sqlb.Predicate {
return duckBinPred("duck.Similar", "~~~", a, b)
}
// ReadParquet renders read_parquet('path') as a FROM source.
func duckReadParquet(path, alias string) sqlb.Source {
return sqlb.EngineSource(duckEngine, "duck.ReadParquet", alias, func(a *sqlb.Args) string {
return "read_parquet(" + sqlb.EngineQuoteLit(path).SQL(a) + ") AS " + a.Dialect().QuoteIdent(alias)
})
}
var (
duckTable = sqlb.Table{Name: "events", Alias: "e"}
duckName = sqlb.Col[string](duckTable, "name")
duckN = sqlb.Col[int32](duckTable, "n")
)
// A third-party dialect is a first-class dialect: the core identifies it by
// name without any entry in the core's own constant block, and it opts into
// capabilities through the same interfaces.
func TestExternalDialect_IsFirstClass(t *testing.T) {
var d sqlb.Dialect = duckDialect{}
if got := sqlb.DialectName(d); got != duckEngine {
t.Errorf("DialectName = %q; the core must not need its own constant for an outside engine", got)
}
if _, ok := d.(sqlb.Returning); !ok {
t.Error("a third-party dialect must be able to satisfy a capability interface")
}
// A dialect that declines to name itself is reported as unknown rather
// than mistaken for one of the built-in three.
if got := sqlb.DialectName(anonymousDialect{}); got != sqlb.DialectUnknown {
t.Errorf("unnamed dialect = %q, want %q", got, sqlb.DialectUnknown)
}
}
type anonymousDialect struct{}
func (anonymousDialect) Placeholder(int) string { return "?" }
func (anonymousDialect) QuoteIdent(id string) string { return id }
// Constructs render under their own engine and fail closed under every other,
// with no code in this module knowing the engine exists.
func TestExternalConstructs_RenderAndGuard(t *testing.T) {
cases := map[string]sqlb.SQLer{
"ListValue": duckListValue(duckName, sqlb.Param("x")),
"Similar": sqlb.BoolExpr(duckSimilar(duckName, sqlb.Param("y"))),
"TryCast": duckTryCast[int64](duckName, "BIGINT"),
"AllNotNull": sqlb.BoolExpr(duckAllNotNull(duckName, duckN)),
}
for name, e := range cases {
q, err := sqlb.NewSelect(duckDialect{}).Exprs(e).From(duckTable).Build()
if err != nil {
t.Errorf("%s under its own dialect: %v", name, err)
}
if q.SQL == "" {
t.Errorf("%s rendered nothing", name)
}
for _, other := range []sqlb.Dialect{sqlb.PostgresDialect{}, sqlb.SQLiteDialect{}, sqlb.ClickHouseDialect{}} {
_, err := sqlb.NewSelect(other).Exprs(e).From(duckTable).Build()
if !errors.Is(err, sqlb.ErrUnsupportedDialect) {
t.Errorf("%s under %s: want ErrUnsupportedDialect, got %v", name, sqlb.DialectName(other), err)
}
}
}
q, err := sqlb.NewSelect(duckDialect{}).Exprs(duckListValue(duckName)).From(duckTable).Build()
if err != nil {
t.Fatal(err)
}
if want := `SELECT list_value("e"."name") FROM "events" AS "e"`; q.SQL != want {
t.Fatalf("got %s\nwant %s", q.SQL, want)
}
}
// QUALIFY, the case that proves the slot set is usable from outside. It needs
// a position between WINDOW and ORDER BY, which no engine in this module uses.
func TestExternalClause_InItsOwnPosition(t *testing.T) {
rn := sqlb.RowNumber().Over(sqlb.NewWindow().
PartitionBy(duckName).
OrderBy(sqlb.OrderBy(duckN, sqlb.SortDesc)))
q, err := sqlb.NewSelect(duckDialect{}).
Exprs(duckName, duckN, sqlb.As(rn, "rn")).
From(duckTable).
// PredicateSQL renders a bare predicate, without the parentheses a
// WHERE group adds. The clause supplies its own grouping if it needs
// any, which is what lets QUALIFY read as the engine spells it.
Clause(duckQualify(sqlb.Eq(sqlb.OutputCol[int64]("rn"), int64(1)))).
Order(sqlb.OrderBy(duckName, sqlb.SortAsc)).
Build()
if err != nil {
t.Fatalf("build: %v", err)
}
want := `SELECT "e"."name", "e"."n", row_number() OVER (PARTITION BY "e"."name" ORDER BY "e"."n" DESC) AS "rn" FROM "events" AS "e" QUALIFY "rn" = ? ORDER BY "e"."name" ASC`
if q.SQL != want {
t.Fatalf("got %s\nwant %s", q.SQL, want)
}
// The exact string above already fixes the position. This states the
// invariant separately, against the LAST "ORDER BY": the first one in
// this statement belongs to the window definition, not to the statement,
// and comparing against it would pass for the wrong reason.
if strings.Index(q.SQL, "QUALIFY") > strings.LastIndex(q.SQL, "ORDER BY") {
t.Error("QUALIFY must render before the statement's ORDER BY")
}
// And it is guarded like everything else: a duck clause on a Postgres
// statement is a compile error now, pinned in examples/typecheck, which
// is exactly the guarantee a community engine gets from EngineClause.
}
// A source from outside the module.
func TestExternalSource(t *testing.T) {
src := duckReadParquet("s3://bucket/events.parquet", "p")
ref := sqlb.Ref(src)
q, err := sqlb.NewSelect(duckDialect{}).
Exprs(sqlb.Col[string](ref, "name")).From(src).Build()
if err != nil {
t.Fatalf("build: %v", err)
}
want := `SELECT "p"."name" FROM read_parquet('s3://bucket/events.parquet') AS "p"`
if q.SQL != want {
t.Fatalf("got %s\nwant %s", q.SQL, want)
}
if _, err := sqlb.NewSelect(sqlb.SQLiteDialect{}).
Exprs(sqlb.Col[string](ref, "name")).From(src).Build(); !errors.Is(err, sqlb.ErrUnsupportedDialect) {
t.Errorf("source under sqlite: got %v", err)
}
}
// TestEngineQuoteLit_FailsClosedWithoutTheCapability pins what an outside
// dialect gets when its constructs need a literal and it has not said how to
// write one: Build fails naming the dialect, and no statement text exists.
//
// Doubling the quote would have kept such a dialect working, and it was what
// EngineQuoteLit did until 2026-09-21. It is also an injection on MySQL, whose
// strings take backslash escapes by default, and it silently changed a
// jsonpath's meaning on a Postgres server with standard_conforming_strings
// off. A community dialect for either would have inherited that without
// knowing, so the capability is required rather than assumed.
func TestEngineQuoteLit_FailsClosedWithoutTheCapability(t *testing.T) {
src := duckReadParquet("s3://bucket/events.parquet", "p")
q, err := sqlb.NewSelect(bareDialect{}).Exprs(sqlb.Col[string](sqlb.Ref(src), "name")).From(src).Build()
if !errors.Is(err, sqlb.ErrNoLiteralQuoting) {
t.Fatalf("got %v, want ErrNoLiteralQuoting", err)
}
if !strings.Contains(err.Error(), duckEngine) {
t.Errorf("the error should name the dialect: %v", err)
}
if q.SQL != "" {
t.Errorf("a refused statement must carry no SQL, got %q", q.SQL)
}
}
// A clause aimed at a slot the statement does not render must fail at Build.
//
// This is the failure that used to be silent, and the one that made an
// outside dialect second-class: a third party cannot add a slot, so a clause
// in the wrong place simply vanished from the statement. It is now an error
// naming the slot.
func TestUnknownSlot_FailsRatherThanVanishing(t *testing.T) {
// A slot number no statement renders.
invented := duckClause[sqlb.SelectKind]("duck.Invented", sqlb.Slot(99), func(*sqlb.Args) string {
return "SOMETHING"
})
_, err := sqlb.NewSelect(duckDialect{}).Exprs(duckName).From(duckTable).Clause(invented).Build()
if !errors.Is(err, sqlb.ErrUnknownSlot) {
t.Fatalf("want ErrUnknownSlot, got %v", err)
}
if !strings.Contains(err.Error(), "Slot(99)") {
t.Errorf("the error must name the slot, got %q", err)
}
// A real slot aimed at the wrong statement kind. SlotAfterSet belongs to
// UPDATE; a SELECT carrying one used to drop it without a word.
misplaced := duckClause[sqlb.SelectKind]("duck.Misplaced", sqlb.SlotAfterSet, func(*sqlb.Args) string {
return "FROM other"
})
_, err = sqlb.NewSelect(duckDialect{}).Exprs(duckName).From(duckTable).Clause(misplaced).Build()
if !errors.Is(err, sqlb.ErrUnknownSlot) {
t.Fatalf("select with an UPDATE slot: want ErrUnknownSlot, got %v", err)
}
if !strings.Contains(err.Error(), "SlotAfterSet") {
t.Errorf("the error must name the slot, got %q", err)
}
// The write statements check the same way. SlotAfterInsert belongs to
// INSERT, which is the only statement whose keyword is followed by INTO.
insertSlotOnUpdate := duckClause[sqlb.UpdateKind]("duck.Misfit", sqlb.SlotAfterInsert, func(*sqlb.Args) string { return "X" })
_, err = sqlb.NewUpdate(duckDialect{}).Table(duckTable).
Set(sqlb.Set(duckName, "a")).All().Clause(insertSlotOnUpdate).Build()
if !errors.Is(err, sqlb.ErrUnknownSlot) {
t.Fatalf("update with an INSERT slot: want ErrUnknownSlot, got %v", err)
}
if !strings.Contains(err.Error(), "SlotAfterInsert") {
t.Errorf("the error must name the slot, got %q", err)
}
}
// Every slot the core defines is either rendered by a statement or is an
// error, and no slot is silently ignored. This walks the whole set so a slot
// added later cannot be forgotten.
func TestEverySlot_IsRenderedOrRejected(t *testing.T) {
for _, slot := range sqlb.SlotValues {
// One probe per statement kind: a clause carries its kind in its type,
// so a single value cannot be offered to all four builders any more.
var rendered, rejected int
for _, build := range []func() (sqlb.Query, error){
func() (sqlb.Query, error) {
return sqlb.NewSelect(duckDialect{}).Exprs(duckName).From(duckTable).Clause(duckProbe[sqlb.SelectKind](slot, "PROBE")).Build()
},
func() (sqlb.Query, error) {
return sqlb.NewInsert(duckDialect{}).Into(duckTable).Row(sqlb.Set(duckName, "a")).Clause(duckProbe[sqlb.InsertKind](slot, "PROBE")).Build()
},
func() (sqlb.Query, error) {
return sqlb.NewUpdate(duckDialect{}).Table(duckTable).Set(sqlb.Set(duckName, "a")).All().Clause(duckProbe[sqlb.UpdateKind](slot, "PROBE")).Build()
},
func() (sqlb.Query, error) {
return sqlb.NewDelete(duckDialect{}).From(duckTable).All().Clause(duckProbe[sqlb.DeleteKind](slot, "PROBE")).Build()
},
} {
q, err := build()
switch {
case err == nil && strings.Contains(q.SQL, "PROBE"):
rendered++
case errors.Is(err, sqlb.ErrUnknownSlot):
rejected++
case err != nil:
// Some combinations fail for an unrelated reason, such as a
// clause replacing the INSERT body. Those are not this test's
// business.
default:
t.Errorf("%s: built cleanly without rendering the clause; that is the silent drop this test exists to catch", slot)
}
}
if rendered == 0 {
t.Errorf("%s: no statement renders it, so no clause can ever use it", slot)
}
}
}
// Slot.String names every slot, because the unknown-slot error is only useful
// if it can say which one.
func TestSlotString(t *testing.T) {
for _, slot := range sqlb.SlotValues {
if got := slot.String(); got == "" || strings.HasPrefix(got, "Slot(") {
t.Errorf("slot %d has no name: %q", int(slot), got)
}
}
if got := sqlb.Slot(99).String(); got != "Slot(99)" {
t.Errorf("unknown slot = %q, want Slot(99)", got)
}
}
// Every boundary of a SELECT is reachable from outside this module.
//
// This is the claim that makes an outside engine package first class. The
// slot set is the one extension point a community package cannot add to, so
// the set has to be complete: a clause must be placeable at every position
// the renderer emits. The test puts a marker in each SELECT slot and asserts
// they come out in grammar order, which fails if a slot is unrendered, in the
// wrong place, or missing from the middle of the statement.
func TestEverySelectBoundary_IsReachable(t *testing.T) {
selectSlots := []struct {
slot sqlb.Slot
marker string
}{
{sqlb.SlotAfterSelect, "M_AFTER_SELECT"},
{sqlb.SlotAfterColumns, "M_AFTER_COLUMNS"},
{sqlb.SlotAfterFrom, "M_AFTER_FROM"},
{sqlb.SlotBeforeWhere, "M_BEFORE_WHERE"},
{sqlb.SlotAfterWhere, "M_AFTER_WHERE"},
{sqlb.SlotAfterGroupBy, "M_AFTER_GROUPBY"},
{sqlb.SlotAfterHaving, "M_AFTER_HAVING"},
{sqlb.SlotBeforeOrder, "M_BEFORE_ORDER"},
{sqlb.SlotAfterOrder, "M_AFTER_ORDER"},
{sqlb.SlotAfterOffset, "M_AFTER_OFFSET"},
{sqlb.SlotTail, "M_TAIL"},
}
sel := sqlb.NewSelect(duckDialect{}).
Exprs(duckName, sqlb.Count()).
From(duckTable).
Where(sqlb.Eq(duckName, "a")).
GroupBy(duckName).
Having(sqlb.Greater(sqlb.Count(), int64(1))).
Window("w", sqlb.NewWindow().PartitionBy(duckName)).
Order(sqlb.OrderBy(duckName, sqlb.SortAsc)).
Limit(10).Offset(5)
for _, sl := range selectSlots {
marker := sl.marker
sel = sel.Clause(duckClause[sqlb.SelectKind]("duck.Probe", sl.slot, func(*sqlb.Args) string { return marker }))
}
q, err := sel.Build()
if err != nil {
t.Fatalf("build: %v", err)
}
prev := -1
for _, sl := range selectSlots {
at := strings.Index(q.SQL, sl.marker)
if at < 0 {
t.Fatalf("%s: no clause reached %s, so that position is unreachable from outside", sl.slot, sl.marker)
}
if at < prev {
t.Errorf("%s rendered out of grammar order", sl.slot)
}
prev = at
}
// Spot-check three positions against the keywords they must sit between,
// since ordering alone would pass if two slots rendered in one place.
for _, c := range []struct{ marker, after, before string }{
{"M_AFTER_SELECT", "SELECT", `"e"."name"`},
{"M_AFTER_WHERE", "WHERE", "GROUP BY"},
{"M_BEFORE_ORDER", "WINDOW", "ORDER BY"},
} {
at := strings.Index(q.SQL, c.marker)
if a := strings.Index(q.SQL, c.after); a > at {
t.Errorf("%s must come after %s", c.marker, c.after)
}
if b := strings.LastIndex(q.SQL, c.before); b < at {
t.Errorf("%s must come before %s", c.marker, c.before)
}
}
}
// The same for the write statements, so no boundary is SELECT-only.
func TestEveryWriteBoundary_IsReachable(t *testing.T) {
q, err := sqlb.NewInsert(duckDialect{}).Into(duckTable).
Row(sqlb.Set(duckName, "a")).
Clause(duckProbe[sqlb.InsertKind](sqlb.SlotAfterInsert, "M_AFTER_INSERT")).
Clause(duckProbe[sqlb.InsertKind](sqlb.SlotAfterTarget, "M_AFTER_TARGET")).
Clause(duckProbe[sqlb.InsertKind](sqlb.SlotBeforeValues, "M_BEFORE_VALUES")).
Clause(duckProbe[sqlb.InsertKind](sqlb.SlotTail, "M_TAIL")).
Build()
if err != nil {
t.Fatalf("insert: %v", err)
}
for _, m := range []string{"M_AFTER_INSERT", "M_AFTER_TARGET", "M_BEFORE_VALUES", "M_TAIL"} {
if !strings.Contains(q.SQL, m) {
t.Errorf("insert: %s unreachable", m)
}
}
q, err = sqlb.NewUpdate(duckDialect{}).Table(duckTable).
Set(sqlb.Set(duckName, "a")).
Where(sqlb.Eq(duckN, int32(1))).
Clause(duckProbe[sqlb.UpdateKind](sqlb.SlotAfterVerb, "M_AFTER_VERB")).
Clause(duckProbe[sqlb.UpdateKind](sqlb.SlotAfterTarget, "M_AFTER_TARGET")).
Clause(duckProbe[sqlb.UpdateKind](sqlb.SlotAfterSet, "M_AFTER_SET")).
Clause(duckProbe[sqlb.UpdateKind](sqlb.SlotAfterWhere, "M_AFTER_WHERE")).
Clause(duckProbe[sqlb.UpdateKind](sqlb.SlotTail, "M_TAIL")).
Build()
if err != nil {
t.Fatalf("update: %v", err)
}
for _, m := range []string{"M_AFTER_VERB", "M_AFTER_TARGET", "M_AFTER_SET", "M_AFTER_WHERE", "M_TAIL"} {
if !strings.Contains(q.SQL, m) {
t.Errorf("update: %s unreachable", m)
}
}
q, err = sqlb.NewDelete(duckDialect{}).From(duckTable).
Where(sqlb.Eq(duckN, int32(1))).
Clause(duckProbe[sqlb.DeleteKind](sqlb.SlotAfterVerb, "M_AFTER_VERB")).
Clause(duckProbe[sqlb.DeleteKind](sqlb.SlotAfterTarget, "M_AFTER_TARGET")).
Clause(duckProbe[sqlb.DeleteKind](sqlb.SlotAfterWhere, "M_AFTER_WHERE")).
Clause(duckProbe[sqlb.DeleteKind](sqlb.SlotTail, "M_TAIL")).
Build()
if err != nil {
t.Fatalf("delete: %v", err)
}
if !strings.HasPrefix(q.SQL, "DELETE M_AFTER_VERB FROM") {
t.Errorf("the verb slot must sit between DELETE and FROM: %s", q.SQL)
}
for _, m := range []string{"M_AFTER_TARGET", "M_AFTER_WHERE", "M_TAIL"} {
if !strings.Contains(q.SQL, m) {
t.Errorf("delete: %s unreachable", m)
}
}
}
// Where a clause lands in a compound statement.
//
// Slots are a per-statement mechanism, and a statement can contain others: a
// join, a set operation, a CTE, a subquery. This pins which statement each
// clause belongs to, because "it rendered somewhere" is not the same as "it
// rendered in the right branch", and the difference is invisible until the
// database disagrees.
func TestSlotScope_InCompoundStatements(t *testing.T) {
a := sqlb.Table{Name: "a", Alias: "a"}
bt := sqlb.Table{Name: "b", Alias: "b"}
an, bn := sqlb.Col[string](a, "n"), sqlb.Col[string](bt, "n")
mark := func(slot sqlb.Slot, text string) sqlb.Clause[duckDialect, sqlb.SelectKind] {
return duckClause[sqlb.SelectKind]("duck.Mark", slot, func(*sqlb.Args) string { return text })
}
// A join does not move the statement's slots. SlotAfterFrom stays with
// the driving table, before the joins; SlotBeforeWhere comes after them.
t.Run("join", func(t *testing.T) {
q, err := sqlb.NewSelect(duckDialect{}).Exprs(an).From(a).
Join(sqlb.Join{Type: sqlb.JoinInner, Table: bt, On: []sqlb.Predicate{sqlb.EqExpr(an, bn)}}).
Clause(mark(sqlb.SlotAfterFrom, "M_FROM")).
Clause(mark(sqlb.SlotBeforeWhere, "M_JOINS")).
Build()
if err != nil {
t.Fatal(err)
}
want := `SELECT "a"."n" FROM "a" AS "a" M_FROM INNER JOIN "b" AS "b" ON ("a"."n" = "b"."n") M_JOINS`
if q.SQL != want {
t.Fatalf("got %s\nwant %s", q.SQL, want)
}
})
// A set operation's receiver is the compound. So a clause added to the
// receiver applies to the whole statement, exactly as its ORDER BY and
// LIMIT do, and a clause on an argument branch stays inside that branch's
// scope, a derived table, which every engine accepts where SQLite refuses
// a parenthesised compound member.
t.Run("set operation", func(t *testing.T) {
left := sqlb.NewSelect(duckDialect{}).Exprs(an).From(a)
right := sqlb.NewSelect(duckDialect{}).Exprs(bn).From(bt).
Clause(mark(sqlb.SlotTail, "M_BRANCH"))
q, err := left.Union(right).Clause(mark(sqlb.SlotTail, "M_COMPOUND")).Build()
if err != nil {
t.Fatal(err)
}
want := `SELECT "a"."n" FROM "a" AS "a" UNION SELECT * FROM (SELECT "b"."n" FROM "b" AS "b" M_BRANCH) AS "sqlb_setop" M_COMPOUND`
if q.SQL != want {
t.Fatalf("got %s\nwant %s", q.SQL, want)
}
// Union returns a derived statement rather than the receiver, like
// every other builder method, so the compound is the returned value
// and the left branch is still usable on its own. A clause added
// before the call belongs to the compound because it was copied into
// it; one added after belongs to whichever statement received it.
compound := left.Union(right)
if compound == left {
t.Error("Union must not return the receiver")
}
alone, err := left.Build()
if err != nil {
t.Fatal(err)
}
if strings.Contains(alone.SQL, "UNION") {
t.Errorf("the left branch carries the set operation: %s", alone.SQL)
}
})
// A CTE body is its own statement and keeps its own slots.
t.Run("cte", func(t *testing.T) {
body := sqlb.NewSelect(duckDialect{}).Exprs(an).From(a).
Clause(mark(sqlb.SlotTail, "M_BODY"))
c := sqlb.Table{Name: "c", Alias: "c"}
q, err := sqlb.NewSelect(duckDialect{}).With("c", body).
Exprs(sqlb.Col[string](c, "n")).From(c).
Clause(mark(sqlb.SlotTail, "M_OUTER")).Build()
if err != nil {
t.Fatal(err)
}
want := `WITH "c" AS (SELECT "a"."n" FROM "a" AS "a" M_BODY) SELECT "c"."n" FROM "c" AS "c" M_OUTER`
if q.SQL != want {
t.Fatalf("got %s\nwant %s", q.SQL, want)
}
})
// A recursive CTE has two branches, and the recursive one keeps its own.
t.Run("recursive cte", func(t *testing.T) {
r := sqlb.Table{Name: "r", Alias: "r"}
seed := sqlb.NewSelect(duckDialect{}).Exprs(an).From(a)
step := sqlb.NewSelect(duckDialect{}).Exprs(an).From(a).
Clause(mark(sqlb.SlotTail, "M_STEP"))
q, err := sqlb.NewSelect(duckDialect{}).WithRecursive("r", seed, step).
Exprs(sqlb.Col[string](r, "n")).From(r).Build()
if err != nil {
t.Fatal(err)
}
// The step is written bare: a derived table around it would put its
// reference to the CTE inside a subquery, which Postgres refuses.
if !strings.Contains(q.SQL, `UNION ALL SELECT "a"."n" FROM "a" AS "a" M_STEP`) {
t.Fatalf("the recursive branch lost its clause: %s", q.SQL)
}
})
// A derived table is a statement inside a FROM, and keeps its own slots.
t.Run("subquery", func(t *testing.T) {
inner := sqlb.NewSelect(duckDialect{}).Exprs(an).From(a).
Clause(mark(sqlb.SlotTail, "M_INNER"))
src := sqlb.Derived(inner, "s")
q, err := sqlb.NewSelect(duckDialect{}).
Exprs(sqlb.Col[string](sqlb.Ref(src), "n")).From(src).Build()
if err != nil {
t.Fatal(err)
}
want := `SELECT "s"."n" FROM (SELECT "a"."n" FROM "a" AS "a" M_INNER) AS "s"`
if q.SQL != want {
t.Fatalf("got %s\nwant %s", q.SQL, want)
}
})
// A per-table hint on a joined table. This is the one position a Slot
// cannot name, because there is one per join rather than one per
// statement, so Join carries its own clauses.
t.Run("per-join clause", func(t *testing.T) {
q, err := sqlb.NewSelect(duckDialect{}).Exprs(an).From(a).
Join(sqlb.Join{
Type: sqlb.JoinInner,
Table: bt,
On: []sqlb.Predicate{sqlb.EqExpr(an, bn)},
Clauses: []sqlb.AnyClause{mark(sqlb.SlotAfterFrom, "M_HINT")},
}).Build()
if err != nil {
t.Fatal(err)
}
want := `SELECT "a"."n" FROM "a" AS "a" INNER JOIN "b" AS "b" M_HINT ON ("a"."n" = "b"."n")`
if q.SQL != want {
t.Fatalf("got %s\nwant %s", q.SQL, want)
}
// It is still a Clause, so it still carries its guard.
_, err = sqlb.NewSelect(sqlb.PostgresDialect{}).Exprs(an).From(a).
Join(sqlb.Join{
Type: sqlb.JoinInner,
Table: bt,
On: []sqlb.Predicate{sqlb.EqExpr(an, bn)},
Clauses: []sqlb.AnyClause{mark(sqlb.SlotAfterFrom, "M_HINT")},
}).Build()
if !errors.Is(err, sqlb.ErrUnsupportedDialect) {
t.Fatalf("a join clause must be guarded like any other: got %v", err)
}
})
}
// The per-table-reference class, all of it.
//
// TABLESAMPLE, FINAL, SAMPLE and INDEXED BY are all per table reference
// rather than per statement. A statement slot names one position, so a clause
// meant for a joined table used to attach to the driving table and produce a
// different query that still runs. sqlb.Join.Clauses is the position for
// them, and this walks the whole class so the next one added is covered by a
// test rather than by a reader noticing.
func TestPerTableClauses_TargetTheirOwnTable(t *testing.T) {
a := sqlb.Table{Name: "a", Alias: "a"}
b := sqlb.Table{Name: "b", Alias: "b"}
an, bn := sqlb.Col[string](a, "n"), sqlb.Col[string](b, "n")
join := sqlb.Join{Type: sqlb.JoinInner, Table: b, On: []sqlb.Predicate{sqlb.EqExpr(an, bn)}}
// Each case builds both placements itself, because the statement placement
// goes through Clause, which is typed to the engine: a table-driven loop
// over a runtime dialect could not offer pg.TableSample to a Select and
// compile. The join placement is an AnyClause and stays loose by design.
type placement struct {
onJoin, onStatement func() (sqlb.Query, error)
}
for _, tc := range []struct {
name string
want string
p placement
}{
{"pg.TableSample",
`SELECT "a"."n" FROM "a" AS "a" INNER JOIN "b" AS "b" TABLESAMPLE SYSTEM (10) ON ("a"."n" = "b"."n")`,
placement{
func() (sqlb.Query, error) {
j := join
j.Clauses = []sqlb.AnyClause{pg.TableSample(pg.SampleSystem, 10, -1)}
return sqlb.NewSelect(sqlb.PostgresDialect{}).Exprs(an).From(a).Join(j).Build()
},
func() (sqlb.Query, error) {
return sqlb.NewSelect(sqlb.PostgresDialect{}).Exprs(an).From(a).Join(join).Clause(pg.TableSample(pg.SampleSystem, 10, -1)).Build()
}}},
{"ch.Final",
"SELECT `a`.`n` FROM `a` AS `a` INNER JOIN `b` AS `b` FINAL ON (`a`.`n` = `b`.`n`)",
placement{
func() (sqlb.Query, error) {
j := join
j.Clauses = []sqlb.AnyClause{ch.Final()}
return sqlb.NewSelect(ch.Dialect{}).Exprs(an).From(a).Join(j).Build()
},
func() (sqlb.Query, error) {
return sqlb.NewSelect(ch.Dialect{}).Exprs(an).From(a).Join(join).Clause(ch.Final()).Build()
}}},
{"ch.Sample",
"SELECT `a`.`n` FROM `a` AS `a` INNER JOIN `b` AS `b` SAMPLE 0.1 ON (`a`.`n` = `b`.`n`)",
placement{
func() (sqlb.Query, error) {
j := join
j.Clauses = []sqlb.AnyClause{ch.Sample(0.1)}
return sqlb.NewSelect(ch.Dialect{}).Exprs(an).From(a).Join(j).Build()
},
func() (sqlb.Query, error) {
return sqlb.NewSelect(ch.Dialect{}).Exprs(an).From(a).Join(join).Clause(ch.Sample(0.1)).Build()
}}},
{"sqlite.IndexedBy",
`SELECT "a"."n" FROM "a" AS "a" INNER JOIN "b" AS "b" INDEXED BY "idx_b" ON ("a"."n" = "b"."n")`,
placement{
func() (sqlb.Query, error) {
j := join
j.Clauses = []sqlb.AnyClause{sqlite.IndexedBy("idx_b")}
return sqlb.NewSelect(sqlb.SQLiteDialect{}).Exprs(an).From(a).Join(j).Build()
},
func() (sqlb.Query, error) {
return sqlb.NewSelect(sqlb.SQLiteDialect{}).Exprs(an).From(a).Join(join).Clause(sqlite.IndexedBy("idx_b")).Build()
}}},
} {
t.Run(tc.name, func(t *testing.T) {
q, err := tc.p.onJoin()
if err != nil {
t.Fatalf("on a join: %v", err)
}
if q.SQL != tc.want {
t.Fatalf("got %s\nwant %s", q.SQL, tc.want)
}
// The same clause in the statement targets the driving table.
// Both placements are legal SQL, which is exactly why the
// distinction has to be tested rather than left to the reader.
q, err = tc.p.onStatement()
if err != nil {
t.Fatalf("on the statement: %v", err)
}
if strings.Index(q.SQL, "JOIN") < strings.Index(q.SQL, `"a"`)+len(`"a"`) {
t.Error("the statement placement must bind to the driving table")
}
if !strings.Contains(q.SQL, "FROM") {
t.Error("unexpected shape")
}
})
}
}