-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocs-examples.test.ts
More file actions
902 lines (743 loc) · 30.4 KB
/
Copy pathdocs-examples.test.ts
File metadata and controls
902 lines (743 loc) · 30.4 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
// Executable documentation examples.
//
// Every non-trivial snippet in `docs/` lives here as a real test that compiles
// the query and asserts the emitted SQL. Test names match the docs page and
// heading they back, and each fenced block in the docs points here.
//
// This exists because the README once documented a `castRows` API that had been
// deleted and an `.orderBy("count", "desc")` call that silently compiled to
// `ORDER BY c O, d E`. Both survived review because nothing ever executed the
// docs. A snippet that stops compiling now fails the suite instead of shipping.
//
// When changing an example here, change the matching docs block in the same
// commit — the pairing is the whole point.
import type { DateTime } from "effect"
import { describe, expect, it } from "@effect/vitest"
import { Effect, Exit, Option, Schema } from "effect"
import * as CH from "./ch/index"
import * as T from "./ch/types"
import * as CHE from "./clickhouse"
import { parseStatement, renderStatement, withSettings } from "./sql/statement"
import { compileCHUnsafe, compileUnionUnsafe } from "./ch/compile"
import { raw as rawFragment, compile as compileFragment } from "./sql/sql-fragment"
/** The compiler emits multi-line SQL with leading indentation; docs quote it
* collapsed to one line for readability, so assertions compare it collapsed. */
const oneLine = (sql: string) => sql.replace(/\s+/g, " ").trim()
// Shared fixtures — the tables every docs page builds on.
const Events = CH.table(
"events",
{
OrgId: T.string,
Name: T.string,
Timestamp: T.dateTime,
DurationMs: T.uint64,
Attributes: T.map(T.string, T.string),
},
{ tenantColumn: "OrgId" },
)
const Services = CH.table(
"services",
{ OrgId: T.string, Name: T.string, Team: T.string },
{ tenantColumn: "OrgId" },
)
// README.md
//
// The README once documented a deleted API (see the header comment); its
// snippets are executable here for the same reason the docs pages are.
describe("README.md", () => {
it("Quick start", () => {
const query = CH.from(Events)
.select(($) => ({
name: $.Name,
p95: CH.quantile(0.95)($.DurationMs),
count: CH.count(),
}))
.where(($) => [
$.OrgId.eq(CH.param.string("orgId")),
$.Timestamp.gte(CH.param.dateTime("startTime")),
CH.when(true, () => $.Name.like("checkout%")),
])
.groupBy("name")
.orderBy(["count", "desc"])
.limit(50)
const compiled = compileCHUnsafe(query, { orgId: "org_123", startTime: "2026-01-01 00:00:00" })
expect(oneLine(compiled.sql)).toBe(
"SELECT events.Name AS name, quantile(0.95)(events.DurationMs) AS p95, count() AS count " +
"FROM events WHERE events.OrgId = 'org_123' AND events.Timestamp >= '2026-01-01 00:00:00' " +
"AND events.Name LIKE 'checkout%' GROUP BY name ORDER BY count DESC LIMIT 50",
)
expect(compiled.tenantScope).toBe("single-tenant")
})
it.effect("Decoding results", () =>
Effect.gen(function* () {
const query = CH.from(Events)
.select(($) => ({ name: $.Name, count: CH.count() }))
.where(($) => [$.OrgId.eq(CH.param.string("orgId"))])
.groupBy("name")
const compiled = compileCHUnsafe(
query,
{ orgId: "org_123" },
{ rowSchema: Schema.Struct({ name: Schema.String, count: Schema.Number }) },
)
expect(compiled.rowSchemaSource).toBe("declared")
expect(yield* compiled.decodeRows([{ name: "checkout", count: 3 }])).toEqual([
{ name: "checkout", count: 3 },
])
}),
)
it("Extending with custom functions", () => {
const toStartOfFiveMinute = CH.defineFn<[CH.Expr<DateTime.Utc>], DateTime.Utc>(
"toStartOfFiveMinute",
T.dateTime,
)
const query = CH.from(Events)
.select(($) => ({ bucket: toStartOfFiveMinute($.Timestamp) }))
.where(($) => [$.OrgId.eq("org_123")])
expect(oneLine(compileCHUnsafe(query, {}).sql)).toContain("toStartOfFiveMinute(events.Timestamp) AS bucket")
})
})
// getting-started.md
describe("docs/getting-started.md", () => {
it("Your first query", () => {
const query = CH.from(Events)
.select(($) => ({
name: $.Name,
p95: CH.quantile(0.95)($.DurationMs),
count: CH.count(),
}))
.where(($) => [
$.OrgId.eq(CH.param.string("orgId")),
$.Timestamp.gte(CH.param.dateTime("startTime")),
])
.groupBy("name")
.orderBy(["count", "desc"])
.limit(50)
const compiled = CH.compileUnsafe(query, {
orgId: "org_123",
startTime: "2026-01-01 00:00:00",
})
expect(oneLine(compiled.sql)).toBe(
"SELECT events.Name AS name, quantile(0.95)(events.DurationMs) AS p95, count() AS count " +
"FROM events WHERE events.OrgId = 'org_123' AND events.Timestamp >= '2026-01-01 00:00:00' " +
"GROUP BY name ORDER BY count DESC LIMIT 50",
)
expect(compiled.tenantScope).toBe("single-tenant")
})
it.effect("Decoding the results", () =>
Effect.gen(function* () {
const compiled = CH.compileUnsafe(
CH.from(Events)
.select(($) => ({ name: $.Name, count: CH.count() }))
.where(($) => [$.OrgId.eq("org_123")])
.groupBy("name"),
{},
)
const rows = yield* compiled.decodeRows([{ name: "checkout", count: "3" }])
expect(rows).toEqual([{ name: "checkout", count: 3 }])
expect(compiled.rowSchemaSource).toBe("derived")
}),
)
})
// tables-and-types.md
describe("docs/tables-and-types.md", () => {
it("Reading a Map column", () => {
const query = CH.from(Events)
.select(($) => ({ method: $.Attributes.get("http.method") }))
.where(($) => [$.OrgId.eq("org_123")])
expect(oneLine(compileCHUnsafe(query, {}).sql)).toBe(
"SELECT events.Attributes['http.method'] AS method FROM events WHERE events.OrgId = 'org_123'",
)
})
it("Column types come from the dialect entry", () => {
// One import per database: types, the table and the builder all come
// from `/clickhouse`, the way the docs show them.
const Counters = CHE.table("counters", {
columns: { OrgId: CHE.string, Hits: CHE.uint16, Live: CHE.bool },
engine: CHE.engine.mergeTree(),
orderBy: ["OrgId"],
tenantColumn: "OrgId",
})
const query = CH.from(Counters)
.select(($) => ({ hits: $.Hits }))
.where(($) => [$.OrgId.eq("org_123"), $.Live.eq(true)])
expect(oneLine(compileCHUnsafe(query, {}).sql)).toBe(
"SELECT counters.Hits AS hits FROM counters WHERE counters.OrgId = 'org_123' AND counters.Live = 1",
)
})
})
// queries.md
describe("docs/queries.md", () => {
it("select by column name", () => {
const query = CH.from(Events).select("Name", "DurationMs")
// Each column is aliased to itself, so output keys match column names.
expect(oneLine(compileCHUnsafe(query, {}).sql)).toBe(
"SELECT events.Name AS Name, events.DurationMs AS DurationMs FROM events",
)
})
it("Queries are immutable", () => {
const base = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq("org_123")])
const limited = base.limit(10)
// `base` is untouched — `limit` returned a new query.
expect(oneLine(compileCHUnsafe(base, {}).sql)).not.toContain("LIMIT")
expect(oneLine(compileCHUnsafe(limited, {}).sql)).toContain("LIMIT 10")
})
it("orderBy takes tuples", () => {
const query = CH.from(Events)
.select(($) => ({ name: $.Name, count: CH.count() }))
.where(($) => [$.OrgId.eq("org_123")])
.groupBy("name")
.orderBy(["count", "desc"], ["name", "asc"])
expect(oneLine(compileCHUnsafe(query, {}).sql)).toContain("ORDER BY count DESC, name ASC")
})
it("orderBy rejects a bare string", () => {
const query = CH.from(Events).select(($) => ({ name: $.Name }))
// TypeScript rejects this; the runtime guard catches callers who bypass it.
expect(() => compileCHUnsafe((query as any).orderBy("name", "desc"), {})).toThrow(
/orderBy\(\) takes \[column, direction\] tuples/,
)
})
})
// expressions.md
describe("docs/expressions.md", () => {
// ClickHouse renders `1/0` and `0/0` as JSON null, and `CHNumber` is
// Schema.Finite-based — so a division has to decode the null it can produce.
it.effect("division decodes nullably and ifNotFinite guards it", () =>
Effect.gen(function* () {
const unguarded = compileCHUnsafe(
CH.from(Events)
.select(($) => ({ perMs: CH.count().div(CH.sum($.DurationMs)) }))
.where(($) => [$.OrgId.eq("org_123")]),
{},
)
expect(yield* unguarded.decodeRows([{ perMs: null }])).toEqual([{ perMs: null }])
const guarded = compileCHUnsafe(
CH.from(Events)
.select(($) => ({
perMs: CH.ifNull(CH.ifNotFinite(CH.count().div(CH.sum($.DurationMs)), 0), CH.lit(0)),
}))
.where(($) => [$.OrgId.eq("org_123")]),
{},
)
expect(oneLine(guarded.sql)).toContain(
"ifNull(ifNotFinite(count() / sum(events.DurationMs), 0), 0) AS perMs",
)
// The guard is in the SQL, so the column is a number again.
expect(Exit.isFailure(yield* Effect.exit(guarded.decodeRows([{ perMs: null }])))).toBe(true)
// Arithmetic can overflow finite inputs; ClickHouse's JSON null decodes as NaN.
const added = compileCHUnsafe(
CH.from(Events)
.select(($) => ({ total: CH.count().add(CH.sum($.DurationMs)) }))
.where(($) => [$.OrgId.eq("org_123")]),
{},
)
expect(Exit.isSuccess(yield* Effect.exit(added.decodeRows([{ total: null }])))).toBe(true)
}),
)
it("Optional predicates with when", () => {
const build = (nameFilter?: string) =>
CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq("org_123"), CH.when(nameFilter, (n) => $.Name.eq(n))])
expect(oneLine(compileCHUnsafe(build("checkout"), {}).sql)).toBe(
"SELECT events.Name AS name FROM events WHERE events.OrgId = 'org_123' AND events.Name = 'checkout'",
)
// `undefined` drops the predicate entirely rather than emitting `AND true`.
expect(oneLine(compileCHUnsafe(build(), {}).sql)).toBe(
"SELECT events.Name AS name FROM events WHERE events.OrgId = 'org_123'",
)
})
it("Arithmetic does not parenthesise", () => {
const query = CH.from(Events)
.select(($) => ({ ratio: $.DurationMs.sub(1).div(2) }))
.where(($) => [$.OrgId.eq("org_123")])
// `a.sub(1).div(2)` is `a - 1 / 2`, i.e. `a - (1 / 2)` — NOT `(a - 1) / 2`.
expect(oneLine(compileCHUnsafe(query, {}).sql)).toContain("DurationMs - 1 / 2 AS ratio")
})
it("Combining conditions with and/or", () => {
const query = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq("org_123"), $.Name.eq("checkout").or($.Name.eq("cart"))])
expect(oneLine(compileCHUnsafe(query, {}).sql)).toContain("AND (events.Name = 'checkout' OR events.Name = 'cart')")
})
it("Conditional aggregation", () => {
const query = CH.from(Events)
.select(($) => ({
total: CH.count(),
slow: CH.countIf($.DurationMs.gt(1000)),
}))
.where(($) => [$.OrgId.eq("org_123")])
expect(oneLine(compileCHUnsafe(query, {}).sql)).toContain(
"count() AS total, countIf(events.DurationMs > 1000) AS slow",
)
})
})
// joins-and-subqueries.md
describe("docs/joins-and-subqueries.md", () => {
// The claim the guide makes for these three over `compileUnsafe(inner).sql`:
// the inner query is compiled by the OUTER compile, so its params resolve
// from the outer set and its failures land in the outer error channel.
it.effect("subqueryExpr splices an inner query, compiled by the outer compile", () =>
Effect.gen(function* () {
const cheapScan = CH.from(Events)
.select(($) => ({ ts: $.Timestamp }))
.where(($) => [$.OrgId.eq(CH.param.string("orgId"))])
.orderBy(["ts", "desc"])
.limit(100)
const cutoff = CH.subqueryExpr(cheapScan, T.dateTime, (sql) => `(SELECT min(ts) FROM (${sql}))`)
const compiled = yield* CH.compile(
CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq(CH.param.string("orgId")), $.Timestamp.gte(cutoff)]),
{ orgId: "org_123" },
)
expect(oneLine(compiled.sql)).toContain("Timestamp >= (SELECT min(ts) FROM (")
// One param, passed once, resolved in both halves by the outer pass.
expect(compiled.sql).not.toContain("__PARAM_")
expect(compiled.sql.match(/OrgId = 'org_123'/g)).toHaveLength(2)
// And the failure lands in the outer compile rather than throwing out
// of the function that built the expression.
const bad = CH.subqueryExpr(
CH.from(Events)
.select(($) => ({ ts: $.Timestamp }))
.where(($) => [$.DurationMs.eq("lots" as never)]),
T.dateTime,
)
const error = yield* Effect.flip(
CH.compile(
CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq("org_123"), $.Timestamp.gte(bad)]),
{},
),
)
expect(error.code).toBe("InvalidLiteral")
}),
)
it("Joining a table", () => {
const query = CH.from(Events, "e")
.innerJoin(Services, "s", (main, joined) =>
main.Name.eq(joined.Name).and(main.OrgId.eq(joined.OrgId)),
)
.select(($) => ({ name: $.Name, team: $.s.Team }))
.where(($) => [$.OrgId.eq("org_123")])
expect(oneLine(compileCHUnsafe(query, {}).sql)).toBe(
"SELECT e.Name AS name, s.Team AS team FROM events AS e " +
"INNER JOIN services AS s ON (e.Name = s.Name AND e.OrgId = s.OrgId) WHERE e.OrgId = 'org_123'",
)
expect(compileCHUnsafe(query, {}).tenantScope).toBe("single-tenant")
})
it("Subquery in FROM uses flat accessors", () => {
const inner = CH.from(Events)
.select(($) => ({ name: $.Name, ms: $.DurationMs }))
.where(($) => [$.OrgId.eq("org_123")])
// NOTE: columns of the subquery are accessed FLAT (`$.name`), not under
// the alias (`$.sub.name`) — the alias only names the derived table.
const outer = CH.fromQuery(inner, "sub")
.select(($) => ({ name: $.name, worst: CH.max($.ms) }))
.groupBy("name")
expect(oneLine(compileCHUnsafe(outer, {}).sql)).toBe(
"SELECT sub.name AS name, max(sub.ms) AS worst FROM (SELECT events.Name AS name, events.DurationMs AS ms " +
"FROM events WHERE events.OrgId = 'org_123') AS sub GROUP BY name",
)
})
it("Joining a subquery", () => {
const perTeam = CH.from(Services)
.select(($) => ({ name: $.Name, team: $.Team }))
.where(($) => [$.OrgId.eq("org_123")])
const query = CH.from(Events, "e")
.innerJoinQuery(perTeam, "s", (main, joined) => main.Name.eq(joined.name))
.select(($) => ({ team: $.s.team }))
.where(($) => [$.OrgId.eq("org_123")])
expect(oneLine(compileCHUnsafe(query, {}).sql)).toContain(
"INNER JOIN (SELECT services.Name AS name, services.Team AS team FROM services " +
"WHERE services.OrgId = 'org_123') AS s ON e.Name = s.name",
)
expect(compileCHUnsafe(query, {}).tenantScope).toBe("single-tenant")
})
it("Correlated subquery with outerRef", () => {
const inner = CH.from(Events)
.select(($) => ({ n: $.Name }))
.where(($) => [$.OrgId.eq("org_123"), $.Name.eq(CH.outerRef("s.Name"))])
const query = CH.from(Services, "s")
.select(($) => ({ team: $.Team }))
.where(($) => [$.OrgId.eq("org_123"), CH.exists(inner)])
const sql = oneLine(compileCHUnsafe(query, {}).sql)
expect(sql).toContain("EXISTS (")
expect(sql).toContain("Name = s.Name")
})
it("notInSubquery splices a query and resolves its params from the outer set", () => {
const excluded = CH.from(Events)
.select(($) => ({ n: $.Name }))
.where(($) => [$.OrgId.eq(CH.param.string("orgId"))])
const query = CH.from(Services, "s")
.select(($) => ({ team: $.Team }))
.where(($) => [$.OrgId.eq(CH.param.string("orgId")), CH.notInSubquery($.Team, excluded)])
const sql = oneLine(compileCHUnsafe(query, { orgId: "org_123" }).sql)
expect(sql).toContain("s.Team NOT IN (SELECT events.Name AS n FROM events WHERE events.OrgId = 'org_123')")
expect(sql).not.toContain("__PARAM_")
})
it("A subquery does not scope its outer query", () => {
const scopedInner = CH.from(Events)
.select(($) => ({ n: $.Name }))
.where(($) => [$.OrgId.eq("org_123")])
const query = CH.from(Services, "s")
.select(($) => ({ team: $.Team }))
.where(($) => [CH.inSubquery($.Team, scopedInner)])
// The inner filter confines the subquery, not the outer scan.
expect(compileCHUnsafe(query, {}).tenantScope).toBe("cross-tenant")
})
it("A scoped subquery keeps the outer query scoped", () => {
const inner = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq("org_123")])
const outer = CH.fromQuery(inner, "sub").select(($) => ({ name: $.name }))
expect(compileCHUnsafe(outer, {}).tenantScope).toBe("single-tenant")
})
})
// unions-and-ctes.md
describe("docs/unions-and-ctes.md", () => {
it("unionAll with an outer ORDER BY", () => {
const recent = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq("org_123"), $.Timestamp.gte("2026-01-01 00:00:00")])
const archived = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq("org_123"), $.Timestamp.lt("2026-01-01 00:00:00")])
const combined = CH.unionAll(recent, archived).orderBy(["name", "asc"]).limit(100)
const sql = oneLine(compileUnionUnsafe(combined, {}).sql)
expect(sql).toContain("UNION ALL")
expect(sql).toContain("ORDER BY name ASC LIMIT 100")
})
it("fromUnion aggregates across branches", () => {
const branch = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq("org_123")])
const combined = CH.unionAll(branch, branch)
const outer = CH.fromUnion(combined, "branches")
.select(($) => ({ name: $.name, total: CH.count() }))
.groupBy("name")
const compiled = compileCHUnsafe(outer, {})
expect(oneLine(compiled.sql)).toContain(") AS branches GROUP BY name")
// A union of scoped branches keeps the outer query scoped.
expect(compiled.tenantScope).toBe("single-tenant")
})
it("Selecting from a CTE", () => {
// To read a CTE, declare a table whose name matches it and start there.
const Recent = CH.table("recent", { Name: T.string })
const cte = CH.from(Events)
.select(($) => ({ Name: $.Name }))
.where(($) => [$.OrgId.eq("org_123")])
const query = CH.from(Recent)
.withCTE("recent", cte)
.select(($) => ({ name: $.Name }))
const compiled = compileCHUnsafe(query, {})
expect(oneLine(compiled.sql)).toBe(
"WITH recent AS ( SELECT events.Name AS Name FROM events WHERE events.OrgId = 'org_123' ) " +
"SELECT recent.Name AS name FROM recent",
)
// Derived off the CTE — the outer query has no OrgId predicate of its own.
expect(compiled.tenantScope).toBe("single-tenant")
})
it("A CTE needs its tenantScope declared", () => {
const Recent = CH.table("recent", { Name: T.string })
const cteSql = "SELECT Name FROM events WHERE OrgId = 'org_123'"
const declared = CH.from(Recent)
.withCTE("recent", cteSql, { tenantScope: "single-tenant" })
.select(($) => ({ name: $.Name }))
const undeclared = CH.from(Recent)
.withCTE("recent", cteSql)
.select(($) => ({ name: $.Name }))
// The CTE body is an opaque string, so the declaration is the only thing
// that can carry its scope. Omit it and the query reads as cross-tenant even
// though the SQL filters OrgId.
expect(compileCHUnsafe(declared, {}).tenantScope).toBe("single-tenant")
expect(compileCHUnsafe(undeclared, {}).tenantScope).toBe("cross-tenant")
})
})
// params-and-compilation.md
describe("docs/params-and-compilation.md", () => {
it("Params are resolved at compile time", () => {
const query = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq(CH.param.string("orgId"))])
// The value lands in the SQL text — this is not server-side binding.
expect(oneLine(compileCHUnsafe(query, { orgId: "org_123" }).sql)).toContain("OrgId = 'org_123'")
})
it("String params are escaped", () => {
const query = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq(CH.param.string("orgId"))])
expect(oneLine(compileCHUnsafe(query, { orgId: "a'b\\c" }).sql)).toContain("OrgId = 'a\\'b\\\\c'")
})
it("One query, many parameter sets", () => {
const query = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq(CH.param.string("orgId"))])
expect(oneLine(compileCHUnsafe(query, { orgId: "org_a" }).sql)).toContain("'org_a'")
expect(oneLine(compileCHUnsafe(query, { orgId: "org_b" }).sql)).toContain("'org_b'")
})
})
// decoding-results.md
describe("docs/decoding-results.md", () => {
const rowSchema = Schema.Struct({
name: Schema.String,
count: Schema.Union([Schema.Finite, Schema.FiniteFromString]),
})
const compiled = () =>
CH.compileUnsafe(
CH.from(Events)
.select(($) => ({ name: $.Name, count: CH.count() }))
.where(($) => [$.OrgId.eq("org_123")])
.groupBy("name"),
{},
{ rowSchema },
)
it.effect("decodeRows validates every row", () =>
Effect.gen(function* () {
// `count` arrives quoted from some backends; the schema accepts both.
const rows = yield* compiled().decodeRows([{ name: "checkout", count: "42" }])
expect(rows).toEqual([{ name: "checkout", count: 42 }])
}),
)
it.effect("decodeFirstRow returns an Option", () =>
Effect.gen(function* () {
const first = yield* compiled().decodeFirstRow([{ name: "checkout", count: 42 }])
const empty = yield* compiled().decodeFirstRow([])
expect(Option.getOrNull(first)).toEqual({ name: "checkout", count: 42 })
expect(Option.isNone(empty)).toBe(true)
}),
)
it.effect("A bad row fails with CompiledQueryDecodeError", () =>
Effect.gen(function* () {
const result = yield* Effect.flip(compiled().decodeRows([{ name: 42, count: 1 }]))
expect(result._tag).toBe("@maple-dev/effect-orm/CompiledQueryDecodeError")
expect(result.rowIndex).toBe(0)
}),
)
it.effect("The row schema is derived from the SELECT", () =>
Effect.gen(function* () {
const derived = CH.compileUnsafe(
CH.from(Events)
.select(($) => ({ name: $.Name, calls: CH.count() }))
.where(($) => [$.OrgId.eq("org_123")])
.groupBy("name"),
{},
)
expect(derived.rowSchemaSource).toBe("derived")
// `count()` is a UInt64: quoted by ClickHouse, bare when the client sets
// output_format_json_quote_64bit_integers=0, and
// the column type knows it either way.
expect(yield* derived.decodeRows([{ name: "checkout", calls: "42" }])).toEqual([
{ name: "checkout", calls: 42 },
])
}),
)
it.effect("An untyped expression leaves the query undecoded", () =>
Effect.gen(function* () {
const noSchema = CH.compileUnsafe(
CH.from(Events)
.select(($) => ({ name: $.Name, odd: CH.untypedExpr("anyLast(Whatever)") }))
.where(($) => [$.OrgId.eq("org_123")]),
{},
)
expect(noSchema.rowSchemaSource).toBe("none")
// And it names the alias responsible, so the fix is one column away.
expect(noSchema.untypedColumns).toEqual(["odd"])
// Nothing is validated — the wrong-typed value passes straight through.
expect(yield* noSchema.decodeRows([{ name: 42, odd: 1 }])).toEqual([{ name: 42, odd: 1 }])
}),
)
// docs/decoding-results.md > "Going back to the wire"
it.effect("Rows go back to the wire through the same schema", () =>
Effect.gen(function* () {
const compiled = CH.compileUnsafe(
CH.from(Events)
.select(($) => ({ at: $.Timestamp, name: $.Name }))
.where(($) => [$.OrgId.eq("org_123")]),
{},
)
const wire = [{ at: "2026-05-24 14:30:00", name: "checkout" }]
const rows = yield* compiled.decodeRows(wire)
expect(yield* compiled.encodeRows(rows)).toEqual(wire)
}),
)
})
// tenant-scoping.md
describe("docs/tenant-scoping.md", () => {
it("An OrgId equality scopes the query", () => {
const query = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq("org_123")])
expect(compileCHUnsafe(query, {}).tenantScope).toBe("single-tenant")
})
it("in_ also scopes; neq does not", () => {
const scoped = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.in_("org_a")])
const unscoped = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.neq("org_123")])
expect(compileCHUnsafe(scoped, {}).tenantScope).toBe("single-tenant")
// `OrgId != 'x'` narrows nothing, so it must not read as scoped.
expect(compileCHUnsafe(unscoped, {}).tenantScope).toBe("cross-tenant")
})
it("The marker does not survive or()", () => {
const query = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq("org_123").or($.Name.eq("checkout"))])
// `OrgId = x OR anything` can match other tenants' rows.
expect(compileCHUnsafe(query, {}).tenantScope).toBe("cross-tenant")
})
it("A table without a declared tenant column is untenanted", () => {
const Untenanted = CH.table("untenanted", { tenant_id: T.string, Name: T.string })
const query = CH.from(Untenanted)
.select(($) => ({ name: $.Name }))
.where(($) => [$.tenant_id.eq("org_123")])
// Nothing declared row-level tenancy, so there is nothing to pin — and
// nothing to leak either, which is why this is not `"cross-tenant"`.
expect(compileCHUnsafe(query, {}).tenantScope).toBe("untenanted")
})
it("Declare the tenant column", () => {
const Tenanted = CH.table(
"tenanted",
{ tenant_id: T.string, Name: T.string },
{ tenantColumn: "tenant_id" },
)
const query = CH.from(Tenanted)
.select(($) => ({ name: $.Name }))
.where(($) => [$.tenant_id.eq("org_123")])
expect(compileCHUnsafe(query, {}).tenantScope).toBe("single-tenant")
})
it("crossTenant() is the explicit opt-out", () => {
const query = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq("org_123")])
.crossTenant()
// Explicit intent wins over the inferred scope.
expect(compileCHUnsafe(query, {}).tenantScope).toBe("cross-tenant")
})
it("route is carried onto the compiled query", () => {
const query = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq("org_123")])
.route("archive")
expect(compileCHUnsafe(query, {}).route).toBe("archive")
})
})
// reference.md
describe("docs/reference.md", () => {
it("Window functions", () => {
const query = CH.from(Events)
.select(($) => ({
name: $.Name,
previous: CH.over(
CH.lagInFrame($.DurationMs, 1, 0),
CH.windowSpec({
partitionBy: [$.Name],
orderBy: [[$.Timestamp, "asc"]],
frame: CH.rowsBetween(CH.unboundedPreceding, CH.currentRow),
}),
),
}))
.where(($) => [$.OrgId.eq("org_123")])
expect(oneLine(compileCHUnsafe(query, {}).sql)).toContain(
"lagInFrame(events.DurationMs, 1, 0) OVER (PARTITION BY events.Name ORDER BY events.Timestamp ASC " +
"ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS previous",
)
})
})
// extending.md
describe("docs/extending.md", () => {
it("defineFn declares a missing function", () => {
const toStartOfFiveMinute = CH.defineFn<[CH.Expr<DateTime.Utc>], DateTime.Utc>(
"toStartOfFiveMinute",
T.dateTime,
)
const query = CH.from(Events)
.select(($) => ({ bucket: toStartOfFiveMinute($.Timestamp) }))
.where(($) => [$.OrgId.eq("org_123")])
expect(oneLine(compileCHUnsafe(query, {}).sql)).toContain("toStartOfFiveMinute(events.Timestamp) AS bucket")
})
// docs/extending.md > "Results that depend on the arguments"
it("a result rule reads the type off an argument", () => {
const anyLast = CH.defineFn<[CH.Expr<string>], string>("anyLast", CH.sameAs(0))
const compiled = compileCHUnsafe(
CH.from(Events)
.select(($) => ({ last: anyLast($.Name) }))
.where(($) => [$.OrgId.eq("org_123")]),
{},
)
// `anyLast` declared no type of its own, and the query still decodes:
// the rule says the result is whatever `Name` is.
expect(compiled.rowSchemaSource).toBe("derived")
expect(oneLine(compiled.sql)).toContain("anyLast(events.Name) AS last")
})
it("defineCondFn declares a predicate", () => {
const matchesRegex = CH.defineCondFn<[CH.Expr<string>, string]>("match")
const query = CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq("org_123"), matchesRegex($.Name, "^checkout")])
expect(oneLine(compileCHUnsafe(query, {}).sql)).toContain("match(events.Name, '^checkout')")
})
it("rawExpr and rawCond are the last resort", () => {
const query = CH.from(Events)
.select(($) => ({ odd: CH.rawExpr("DurationMs % 2", T.float64) }))
.where(($) => [$.OrgId.eq("org_123"), CH.rawCond("Name GLOBAL IN (SELECT 1)")])
const sql = oneLine(compileCHUnsafe(query, {}).sql)
expect(sql).toContain("DurationMs % 2 AS odd")
expect(sql).toContain("Name GLOBAL IN (SELECT 1)")
})
it("compileFnCall handles variadic shapes", () => {
const greatestOf = <T>(...exprs: CH.Expr<T>[]) => CH.compileFnCall<T>("greatest", ...exprs)
const query = CH.from(Events)
.select(($) => ({ worst: greatestOf($.DurationMs, CH.lit(100)) }))
.where(($) => [$.OrgId.eq("org_123")])
expect(oneLine(compileCHUnsafe(query, {}).sql)).toContain("greatest(events.DurationMs, 100) AS worst")
})
it("makeExpr builds custom call syntax", () => {
const quantileExact = (q: number) => (expr: CH.Expr<number>) =>
CH.makeExpr(
rawFragment(`quantileExact(${q})(${compileFragment(expr.toFragment())})`),
T.float64.schema,
)
const query = CH.from(Events)
.select(($) => ({ p99: quantileExact(0.99)($.DurationMs) }))
.where(($) => [$.OrgId.eq("org_123")])
expect(oneLine(compileCHUnsafe(query, {}).sql)).toContain("quantileExact(0.99)(events.DurationMs) AS p99")
})
it.effect("rawCompiledQuery wraps handwritten SQL", () =>
Effect.gen(function* () {
const compiled = CH.rawCompiledQuery<{ readonly name: string }>({
sql: "SELECT Name AS name FROM events WHERE OrgId = 'org_123'",
reason: "user-authored-sql",
justification: "The SQL came from a user; there is no AST to build.",
// Cannot be inferred from a raw string — the caller must assert it.
tenantScope: "single-tenant",
rowSchema: Schema.Struct({ name: Schema.String }),
})
expect(yield* compiled.decodeRows([{ name: "checkout" }])).toEqual([{ name: "checkout" }])
expect(compiled.tenantScope).toBe("single-tenant")
}),
)
})
describe("docs/running-queries.md", () => {
const Events = CH.table("events", { OrgId: T.string, Name: T.string }, { tenantColumn: "OrgId" })
// The reason the guide tells you not to append `SETTINGS …` by hand: a query
// that already carries a FORMAT clause ends up with the two in the order
// ClickHouse rejects.
it("SETTINGS precede FORMAT whatever order you add them in", () => {
const compiled = compileCHUnsafe(
CH.from(Events)
.select(($) => ({ name: $.Name }))
.where(($) => [$.OrgId.eq("org_123")])
.format("JSON"),
{},
)
const statement = withSettings(parseStatement(compiled.sql), "SETTINGS max_execution_time = 30")
expect(statement.text.endsWith("SETTINGS max_execution_time = 30\nFORMAT JSON")).toBe(true)
// Total: a statement with no terminal clauses round-trips unchanged.
expect(renderStatement(parseStatement("SELECT 1")).trim()).toBe("SELECT 1")
})
})