effect-qb 0.16.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/README.md +4 -0
  2. package/dist/index.js +8065 -0
  3. package/dist/mysql.js +4036 -2418
  4. package/dist/postgres/metadata.js +2536 -625
  5. package/dist/postgres.js +8248 -7857
  6. package/dist/sqlite.js +8854 -0
  7. package/dist/standard.js +8019 -0
  8. package/package.json +15 -3
  9. package/src/casing.ts +71 -0
  10. package/src/index.ts +2 -0
  11. package/src/internal/casing.ts +89 -0
  12. package/src/internal/column-state.ts +11 -6
  13. package/src/internal/column.ts +44 -7
  14. package/src/internal/datatypes/define.ts +2 -1
  15. package/src/internal/datatypes/enrich.ts +23 -0
  16. package/src/internal/datatypes/lookup.ts +14 -7
  17. package/src/internal/derived-table.ts +7 -13
  18. package/src/internal/dialect-renderers/mysql.ts +2046 -0
  19. package/src/{postgres/internal/sql-expression-renderer.ts → internal/dialect-renderers/postgres.ts} +867 -283
  20. package/src/{mysql/internal/sql-expression-renderer.ts → internal/dialect-renderers/sqlite.ts} +834 -358
  21. package/src/internal/dialect.ts +37 -0
  22. package/src/internal/dsl-mutation-runtime.ts +29 -10
  23. package/src/internal/dsl-plan-runtime.ts +41 -24
  24. package/src/internal/dsl-query-runtime.ts +11 -31
  25. package/src/internal/dsl-transaction-ddl-runtime.ts +61 -15
  26. package/src/internal/executor.ts +57 -15
  27. package/src/internal/expression-ast.ts +3 -2
  28. package/src/internal/grouping-key.ts +216 -9
  29. package/src/internal/implication-runtime.ts +3 -2
  30. package/src/internal/json/types.ts +155 -40
  31. package/src/internal/predicate/context.ts +14 -1
  32. package/src/internal/predicate/key.ts +19 -2
  33. package/src/internal/predicate/runtime.ts +30 -3
  34. package/src/internal/query.d.ts +38 -11
  35. package/src/internal/query.ts +315 -54
  36. package/src/internal/renderer.ts +51 -6
  37. package/src/internal/runtime/driver-value-mapping.ts +58 -0
  38. package/src/internal/runtime/normalize.ts +74 -43
  39. package/src/internal/runtime/schema.ts +5 -3
  40. package/src/internal/runtime/value.ts +153 -30
  41. package/src/internal/scalar.ts +6 -1
  42. package/src/internal/schema-derivation.d.ts +12 -61
  43. package/src/internal/schema-derivation.ts +90 -38
  44. package/src/internal/schema-expression.ts +2 -2
  45. package/src/internal/sql-expression-renderer.ts +19 -0
  46. package/src/internal/standard-dsl.ts +6885 -0
  47. package/src/internal/table-options.ts +229 -62
  48. package/src/internal/table.d.ts +33 -32
  49. package/src/internal/table.ts +469 -160
  50. package/src/mysql/column-extension.ts +3 -0
  51. package/src/mysql/column.ts +27 -12
  52. package/src/mysql/datatypes/index.ts +24 -2
  53. package/src/mysql/errors/catalog.ts +5 -5
  54. package/src/mysql/errors/normalize.ts +2 -2
  55. package/src/mysql/executor.ts +7 -5
  56. package/src/mysql/internal/dialect.ts +9 -4
  57. package/src/mysql/internal/dsl.ts +906 -324
  58. package/src/mysql/internal/renderer.ts +7 -2
  59. package/src/mysql/json.ts +37 -0
  60. package/src/mysql/query-extension.ts +16 -0
  61. package/src/mysql/query.ts +9 -2
  62. package/src/mysql/renderer.ts +31 -4
  63. package/src/mysql.ts +4 -12
  64. package/src/postgres/column-extension.ts +28 -0
  65. package/src/postgres/column.ts +9 -13
  66. package/src/postgres/datatypes/index.d.ts +2 -1
  67. package/src/postgres/datatypes/index.ts +3 -2
  68. package/src/postgres/errors/normalize.ts +2 -2
  69. package/src/postgres/executor.ts +55 -10
  70. package/src/postgres/function/core.ts +20 -4
  71. package/src/postgres/function/index.ts +1 -17
  72. package/src/postgres/internal/dialect.ts +9 -4
  73. package/src/postgres/internal/dsl.ts +850 -359
  74. package/src/postgres/internal/renderer.ts +7 -2
  75. package/src/postgres/internal/schema-ddl.ts +22 -9
  76. package/src/postgres/internal/schema-model.ts +244 -10
  77. package/src/postgres/json.ts +100 -24
  78. package/src/postgres/jsonb.ts +38 -0
  79. package/src/postgres/query-extension.ts +2 -0
  80. package/src/postgres/query.ts +9 -2
  81. package/src/postgres/renderer.ts +31 -4
  82. package/src/postgres/schema-management.ts +108 -16
  83. package/src/postgres/schema.ts +98 -15
  84. package/src/postgres/table.ts +203 -398
  85. package/src/postgres/type.ts +8 -7
  86. package/src/postgres.ts +9 -11
  87. package/src/sqlite/column-extension.ts +3 -0
  88. package/src/sqlite/column.ts +127 -0
  89. package/src/sqlite/datatypes/index.ts +80 -0
  90. package/src/sqlite/datatypes/spec.ts +98 -0
  91. package/src/sqlite/errors/catalog.ts +103 -0
  92. package/src/sqlite/errors/fields.ts +19 -0
  93. package/src/sqlite/errors/index.ts +19 -0
  94. package/src/sqlite/errors/normalize.ts +229 -0
  95. package/src/sqlite/errors/requirements.ts +71 -0
  96. package/src/sqlite/errors/types.ts +29 -0
  97. package/src/sqlite/executor.ts +229 -0
  98. package/src/sqlite/function/aggregate.ts +2 -0
  99. package/src/sqlite/function/core.ts +2 -0
  100. package/src/sqlite/function/index.ts +19 -0
  101. package/src/sqlite/function/string.ts +2 -0
  102. package/src/sqlite/function/temporal.ts +100 -0
  103. package/src/sqlite/function/window.ts +2 -0
  104. package/src/sqlite/internal/dialect.ts +42 -0
  105. package/src/sqlite/internal/dsl.ts +6979 -0
  106. package/src/sqlite/internal/renderer.ts +51 -0
  107. package/src/sqlite/json.ts +39 -0
  108. package/src/sqlite/query-extension.ts +2 -0
  109. package/src/sqlite/query.ts +196 -0
  110. package/src/sqlite/renderer.ts +51 -0
  111. package/src/sqlite.ts +14 -0
  112. package/src/standard/column.ts +163 -0
  113. package/src/standard/datatypes/index.ts +83 -0
  114. package/src/standard/datatypes/spec.ts +98 -0
  115. package/src/standard/dialect.ts +40 -0
  116. package/src/standard/function/aggregate.ts +2 -0
  117. package/src/standard/function/core.ts +2 -0
  118. package/src/standard/function/index.ts +18 -0
  119. package/src/standard/function/string.ts +2 -0
  120. package/src/standard/function/temporal.ts +78 -0
  121. package/src/standard/function/window.ts +2 -0
  122. package/src/standard/internal/renderer.ts +45 -0
  123. package/src/standard/query.ts +152 -0
  124. package/src/standard/renderer.ts +21 -0
  125. package/src/standard/table.ts +147 -0
  126. package/src/standard.ts +18 -0
  127. package/src/internal/aggregation-validation.ts +0 -57
  128. package/src/mysql/table.ts +0 -157
@@ -63,6 +63,23 @@ type TupleIndex<
63
63
  : TupleIndex<Tail, Index, [...Count, unknown]>
64
64
  : never
65
65
 
66
+ type TupleIndexFromEnd<
67
+ Values extends readonly unknown[],
68
+ Distance extends number,
69
+ Count extends readonly unknown[] = [unknown]
70
+ > = Values extends readonly [...infer Rest, infer Last]
71
+ ? Count["length"] extends Distance
72
+ ? Last
73
+ : TupleIndexFromEnd<Rest, Distance, [...Count, unknown]>
74
+ : never
75
+
76
+ type TupleIndexAt<
77
+ Values extends readonly unknown[],
78
+ Index extends number
79
+ > = `${Index}` extends `-${infer Distance extends number}`
80
+ ? TupleIndexFromEnd<Values, Distance>
81
+ : TupleIndex<Values, Index>
82
+
66
83
  type DropTupleIndex<
67
84
  Values extends readonly unknown[],
68
85
  Index extends number,
@@ -73,6 +90,23 @@ type DropTupleIndex<
73
90
  : readonly [Head, ...DropTupleIndex<Tail, Index, [...Count, unknown]>]
74
91
  : readonly []
75
92
 
93
+ type DropTupleIndexFromEnd<
94
+ Values extends readonly unknown[],
95
+ Distance extends number,
96
+ Count extends readonly unknown[] = [unknown]
97
+ > = Values extends readonly [...infer Rest, infer Last]
98
+ ? Count["length"] extends Distance
99
+ ? readonly [...Rest]
100
+ : readonly [...DropTupleIndexFromEnd<Rest, Distance, [...Count, unknown]>, Last]
101
+ : readonly []
102
+
103
+ type DropTupleIndexAt<
104
+ Values extends readonly unknown[],
105
+ Index extends number
106
+ > = `${Index}` extends `-${infer Distance extends number}`
107
+ ? DropTupleIndexFromEnd<Values, Distance>
108
+ : DropTupleIndex<Values, Index>
109
+
76
110
  type SetTupleIndex<
77
111
  Values extends readonly unknown[],
78
112
  Index extends number,
@@ -84,6 +118,25 @@ type SetTupleIndex<
84
118
  : readonly [Head, ...SetTupleIndex<Tail, Index, Next, [...Count, unknown]>]
85
119
  : readonly NormalizeJsonLiteral<Next>[]
86
120
 
121
+ type SetTupleIndexFromEnd<
122
+ Values extends readonly unknown[],
123
+ Distance extends number,
124
+ Next,
125
+ Count extends readonly unknown[] = [unknown]
126
+ > = Values extends readonly [...infer Rest, infer Last]
127
+ ? Count["length"] extends Distance
128
+ ? readonly [...Rest, NormalizeJsonLiteral<Next>]
129
+ : readonly [...SetTupleIndexFromEnd<Rest, Distance, Next, [...Count, unknown]>, Last]
130
+ : readonly NormalizeJsonLiteral<Next>[]
131
+
132
+ type SetTupleIndexAt<
133
+ Values extends readonly unknown[],
134
+ Index extends number,
135
+ Next
136
+ > = `${Index}` extends `-${infer Distance extends number}`
137
+ ? SetTupleIndexFromEnd<Values, Distance, Next>
138
+ : SetTupleIndex<Values, Index, Next>
139
+
87
140
  type InsertTupleIndex<
88
141
  Values extends readonly unknown[],
89
142
  Index extends number,
@@ -98,6 +151,29 @@ type InsertTupleIndex<
98
151
  : readonly [Head, ...InsertTupleIndex<Tail, Index, Next, After, [...Count, unknown]>]
99
152
  : readonly NormalizeJsonLiteral<Next>[]
100
153
 
154
+ type InsertTupleIndexFromEnd<
155
+ Values extends readonly unknown[],
156
+ Distance extends number,
157
+ Next,
158
+ After extends boolean,
159
+ Count extends readonly unknown[] = [unknown]
160
+ > = Values extends readonly [...infer Rest, infer Last]
161
+ ? Count["length"] extends Distance
162
+ ? After extends true
163
+ ? readonly [...Rest, Last, NormalizeJsonLiteral<Next>]
164
+ : readonly [...Rest, NormalizeJsonLiteral<Next>, Last]
165
+ : readonly [...InsertTupleIndexFromEnd<Rest, Distance, Next, After, [...Count, unknown]>, Last]
166
+ : readonly NormalizeJsonLiteral<Next>[]
167
+
168
+ type InsertTupleIndexAt<
169
+ Values extends readonly unknown[],
170
+ Index extends number,
171
+ Next,
172
+ After extends boolean
173
+ > = `${Index}` extends `-${infer Distance extends number}`
174
+ ? InsertTupleIndexFromEnd<Values, Distance, Next, After>
175
+ : InsertTupleIndex<Values, Index, Next, After>
176
+
101
177
  type IndexStep<
102
178
  Root,
103
179
  Index extends number,
@@ -105,7 +181,7 @@ type IndexStep<
105
181
  > = Root extends readonly unknown[]
106
182
  ? number extends Root["length"]
107
183
  ? NormalizeJsonLiteral<Root[number]> | null
108
- : NormalizeJsonLiteral<TupleIndex<Root, Index>> | (TupleIndex<Root, Index> extends never ? null : never)
184
+ : NormalizeJsonLiteral<TupleIndexAt<Root, Index>> | (TupleIndexAt<Root, Index> extends never ? null : never)
109
185
  : JsonPathUsageError<Operation, Root, JsonPath.IndexSegment<Index>, "index segments require an array-like json value">
110
186
 
111
187
  type NonExactStep<
@@ -146,9 +222,9 @@ type StepSet<
146
222
  ? Root extends readonly unknown[]
147
223
  ? number extends Root["length"]
148
224
  ? readonly (NormalizeJsonLiteral<Root[number]> | NormalizeJsonLiteral<Next>)[]
149
- : SetTupleIndex<Root, Index, Next>
225
+ : SetTupleIndexAt<Root, Index, Next>
150
226
  : JsonPathUsageError<Operation, Root, Segment, "index segments require an array-like json value">
151
- : NormalizeJsonLiteral<Root>
227
+ : JsonPathUsageError<Operation, Root, Segment, "mutation paths require exact key/index segments">
152
228
 
153
229
  type StepDelete<
154
230
  Root,
@@ -164,9 +240,9 @@ type StepDelete<
164
240
  ? Root extends readonly unknown[]
165
241
  ? number extends Root["length"]
166
242
  ? Root
167
- : DropTupleIndex<Root, Index>
243
+ : DropTupleIndexAt<Root, Index>
168
244
  : JsonPathUsageError<Operation, Root, Segment, "index segments require an array-like json value">
169
- : NormalizeJsonLiteral<Root>
245
+ : JsonPathUsageError<Operation, Root, Segment, "mutation paths require exact key/index segments">
170
246
 
171
247
  type StepInsert<
172
248
  Root,
@@ -178,7 +254,7 @@ type StepInsert<
178
254
  ? Root extends readonly unknown[]
179
255
  ? number extends Root["length"]
180
256
  ? readonly (NormalizeJsonLiteral<Root[number]> | NormalizeJsonLiteral<Next>)[]
181
- : InsertTupleIndex<Root, Index, Next, After>
257
+ : InsertTupleIndexAt<Root, Index, Next, After>
182
258
  : JsonPathUsageError<Operation, Root, Segment, "index segments require an array-like json value">
183
259
  : Segment extends JsonPath.KeySegment<infer Key extends string>
184
260
  ? Root extends readonly unknown[]
@@ -190,7 +266,7 @@ type StepInsert<
190
266
  readonly [K in Key]: NormalizeJsonLiteral<Next>
191
267
  }
192
268
  : JsonPathUsageError<Operation, Root, Segment, "key segments require an object-like json value">
193
- : NormalizeJsonLiteral<Root>
269
+ : JsonPathUsageError<Operation, Root, Segment, "mutation paths require exact key/index segments">
194
270
 
195
271
  type RecurseValue<
196
272
  Current,
@@ -219,12 +295,16 @@ type RecurseSet<
219
295
  : StepValue<Current, Head, Operation> extends infer Child
220
296
  ? Child extends JsonPathUsageError<any, any, any, any>
221
297
  ? Child
222
- : StepSet<
223
- Current,
224
- Head,
225
- RecurseSet<StripNull<Child>, Tail, Next, Operation>,
226
- Operation
227
- >
298
+ : RecurseSet<StripNull<Child>, Tail, Next, Operation> extends infer UpdatedChild
299
+ ? UpdatedChild extends JsonPathUsageError<any, any, any, any>
300
+ ? UpdatedChild
301
+ : StepSet<
302
+ Current,
303
+ Head,
304
+ UpdatedChild,
305
+ Operation
306
+ >
307
+ : never
228
308
  : never
229
309
  : NormalizeJsonLiteral<Next>
230
310
 
@@ -238,12 +318,16 @@ type RecurseDelete<
238
318
  : StepValue<Current, Head, Operation> extends infer Child
239
319
  ? Child extends JsonPathUsageError<any, any, any, any>
240
320
  ? Child
241
- : StepSet<
242
- Current,
243
- Head,
244
- RecurseDelete<StripNull<Child>, Tail, Operation>,
245
- Operation
246
- >
321
+ : RecurseDelete<StripNull<Child>, Tail, Operation> extends infer UpdatedChild
322
+ ? UpdatedChild extends JsonPathUsageError<any, any, any, any>
323
+ ? UpdatedChild
324
+ : StepSet<
325
+ Current,
326
+ Head,
327
+ UpdatedChild,
328
+ Operation
329
+ >
330
+ : never
247
331
  : never
248
332
  : NormalizeJsonLiteral<Current>
249
333
 
@@ -259,12 +343,16 @@ type RecurseInsert<
259
343
  : StepValue<Current, Head, Operation> extends infer Child
260
344
  ? Child extends JsonPathUsageError<any, any, any, any>
261
345
  ? Child
262
- : StepSet<
263
- Current,
264
- Head,
265
- RecurseInsert<StripNull<Child>, Tail, Next, After, Operation>,
266
- Operation
267
- >
346
+ : RecurseInsert<StripNull<Child>, Tail, Next, After, Operation> extends infer UpdatedChild
347
+ ? UpdatedChild extends JsonPathUsageError<any, any, any, any>
348
+ ? UpdatedChild
349
+ : StepSet<
350
+ Current,
351
+ Head,
352
+ UpdatedChild,
353
+ Operation
354
+ >
355
+ : never
268
356
  : never
269
357
  : NormalizeJsonLiteral<Current>
270
358
 
@@ -364,23 +452,50 @@ export type JsonBuildArray<
364
452
 
365
453
  export type JsonTextResult<Value> = Value extends JsonPrimitive ? `${Value}` : string
366
454
 
367
- export type JsonTypeName<Value> =
368
- NormalizeJsonLiteral<Value> extends null ? "null"
369
- : NormalizeJsonLiteral<Value> extends string ? "string"
370
- : NormalizeJsonLiteral<Value> extends number ? "number"
371
- : NormalizeJsonLiteral<Value> extends boolean ? "boolean"
372
- : NormalizeJsonLiteral<Value> extends readonly unknown[] ? "array"
373
- : NormalizeJsonLiteral<Value> extends object ? "object"
455
+ type JsonTypeNameOfNormalized<Value> =
456
+ Value extends null ? "null"
457
+ : Value extends string ? "string"
458
+ : Value extends number ? "number"
459
+ : Value extends boolean ? "boolean"
460
+ : Value extends readonly unknown[] ? "array"
461
+ : Value extends object ? "object"
374
462
  : "unknown"
375
463
 
376
- export type JsonLengthResult<Value> =
377
- NormalizeJsonLiteral<Value> extends readonly unknown[] ? number :
378
- NormalizeJsonLiteral<Value> extends object ? number :
464
+ export type JsonTypeName<Value> =
465
+ [NormalizeJsonLiteral<Value>] extends [never]
466
+ ? "unknown"
467
+ : NormalizeJsonLiteral<Value> extends infer Normalized
468
+ ? Normalized extends unknown
469
+ ? JsonTypeNameOfNormalized<Normalized>
470
+ : never
471
+ : never
472
+
473
+ type JsonLengthResultOfNormalized<Value> =
474
+ Value extends readonly unknown[] ? number :
475
+ Value extends object ? number :
379
476
  null
380
477
 
478
+ export type JsonLengthResult<Value> =
479
+ [NormalizeJsonLiteral<Value>] extends [never]
480
+ ? null
481
+ : NormalizeJsonLiteral<Value> extends infer Normalized
482
+ ? Normalized extends unknown
483
+ ? JsonLengthResultOfNormalized<Normalized>
484
+ : never
485
+ : never
486
+
487
+ type JsonKeysResultOfNormalized<Value> =
488
+ Value extends readonly unknown[]
489
+ ? null
490
+ : Value extends object
491
+ ? readonly Extract<keyof Value, string>[]
492
+ : null
493
+
381
494
  export type JsonKeysResult<Value> =
382
- NormalizeJsonLiteral<Value> extends object
383
- ? NormalizeJsonLiteral<Value> extends readonly unknown[]
384
- ? readonly []
385
- : readonly Extract<keyof NormalizeJsonLiteral<Value>, string>[]
386
- : null
495
+ [NormalizeJsonLiteral<Value>] extends [never]
496
+ ? null
497
+ : NormalizeJsonLiteral<Value> extends infer Normalized
498
+ ? Normalized extends unknown
499
+ ? JsonKeysResultOfNormalized<Normalized>
500
+ : never
501
+ : never
@@ -46,7 +46,20 @@ type Frame<
46
46
  readonly polarity: Direction
47
47
  }
48
48
 
49
- type SourceNameOfKey<Key extends string> = Key extends `${infer SourceName}.${string}` ? SourceName : never
49
+ type SourceNameOfKey<
50
+ Key extends string,
51
+ Current extends string = ""
52
+ > = string extends Key
53
+ ? string
54
+ : Key extends `\\.${infer Rest}`
55
+ ? SourceNameOfKey<Rest, `${Current}.`>
56
+ : Key extends `\\\\${infer Rest}`
57
+ ? SourceNameOfKey<Rest, `${Current}\\`>
58
+ : Key extends `.${string}`
59
+ ? Current
60
+ : Key extends `${infer Character}${infer Rest}`
61
+ ? SourceNameOfKey<Rest, `${Current}${Character}`>
62
+ : never
50
63
 
51
64
  type EqLiteralValueOf<
52
65
  EqLiterals,
@@ -2,10 +2,27 @@ import type * as Expression from "../scalar.js"
2
2
  import type * as ExpressionAst from "../expression-ast.js"
3
3
  import type * as JsonPath from "../json/path.js"
4
4
 
5
+ type EscapePredicateBackslashes<Value extends string> = string extends Value
6
+ ? string
7
+ : Value extends `${infer Head}\\${infer Tail}`
8
+ ? `${Head}\\\\${EscapePredicateBackslashes<Tail>}`
9
+ : Value
10
+
11
+ type EscapePredicateDots<Value extends string> = string extends Value
12
+ ? string
13
+ : Value extends `${infer Head}.${infer Tail}`
14
+ ? `${Head}\\.${EscapePredicateDots<Tail>}`
15
+ : Value
16
+
17
+ type EscapePredicateSegment<Value extends string> =
18
+ EscapePredicateDots<EscapePredicateBackslashes<Value>>
19
+
20
+ type EscapeJsonPathSegment<Value extends string> = EscapePredicateSegment<Value>
21
+
5
22
  export type ColumnKey<
6
23
  TableName extends string,
7
24
  ColumnName extends string
8
- > = `${TableName}.${ColumnName}`
25
+ > = `${EscapePredicateSegment<TableName>}.${EscapePredicateSegment<ColumnName>}`
9
26
 
10
27
  export type ColumnKeyOfAst<Ast extends ExpressionAst.Any> =
11
28
  [Ast] extends [ExpressionAst.ColumnNode<infer TableName extends string, infer ColumnName extends string>]
@@ -25,7 +42,7 @@ type JsonPathKey<
25
42
  ? Segment extends JsonPath.KeySegment<infer Key extends string>
26
43
  ? JsonPathKey<
27
44
  Tail,
28
- [Current] extends [never] ? Key : `${Current}.${Key}`,
45
+ [Current] extends [never] ? EscapeJsonPathSegment<Key> : `${Current}.${EscapeJsonPathSegment<Key>}`,
29
46
  readonly [...Seen, unknown]
30
47
  >
31
48
  : never
@@ -98,7 +98,26 @@ const cloneContext = (context: MutableContext): MutableContext => ({
98
98
 
99
99
  const freezeContext = (context: MutableContext): RuntimeContext => context
100
100
 
101
- const sourceNameOfKey = (key: string): string => key.split(".", 1)[0] ?? key
101
+ export const columnPredicateKey = (tableName: string, columnName: string): string =>
102
+ JSON.stringify([tableName, columnName])
103
+
104
+ const columnPredicateKeyParts = (key: string): readonly [string, string] | undefined => {
105
+ const jsonSeparator = key.indexOf("#json:")
106
+ const columnKey = jsonSeparator === -1 ? key : key.slice(0, jsonSeparator)
107
+ try {
108
+ const parsed = JSON.parse(columnKey) as unknown
109
+ return Array.isArray(parsed) &&
110
+ parsed.length === 2 &&
111
+ typeof parsed[0] === "string" &&
112
+ typeof parsed[1] === "string"
113
+ ? [parsed[0], parsed[1]]
114
+ : undefined
115
+ } catch {
116
+ return undefined
117
+ }
118
+ }
119
+
120
+ const sourceNameOfKey = (key: string): string => columnPredicateKeyParts(key)?.[0] ?? key.split(".", 1)[0] ?? key
102
121
 
103
122
  const addSourceName = (context: MutableContext, key: string): void => {
104
123
  context.sourceNames.add(sourceNameOfKey(key))
@@ -386,12 +405,15 @@ const astOf = (value: Expression.Any): ExpressionAst.Any =>
386
405
 
387
406
  const columnKeyOfExpression = (value: Expression.Any): string | undefined => {
388
407
  const ast = astOf(value)
389
- return ast.kind === "column" ? `${ast.tableName}.${ast.columnName}` : undefined
408
+ return ast.kind === "column" ? columnPredicateKey(ast.tableName, ast.columnName) : undefined
390
409
  }
391
410
 
392
411
  const sameDbType = (left: Expression.DbType.Any, right: Expression.DbType.Any): boolean =>
393
412
  left.dialect === right.dialect && left.kind === right.kind
394
413
 
414
+ const escapeJsonPathPredicateKeySegment = (value: string): string =>
415
+ value.replaceAll("\\", "\\\\").replaceAll(".", "\\.")
416
+
395
417
  const jsonPathPredicateKeyOfExpression = (value: Expression.Any): string | undefined => {
396
418
  const ast = astOf(value)
397
419
  switch (ast.kind) {
@@ -415,7 +437,9 @@ const jsonPathPredicateKeyOfExpression = (value: Expression.Any): string | undef
415
437
  return undefined
416
438
  }
417
439
  const baseKey = columnKeyOfExpression(jsonAst.base)
418
- return baseKey === undefined ? undefined : `${baseKey}#json:${path.join(".")}`
440
+ return baseKey === undefined
441
+ ? undefined
442
+ : `${baseKey}#json:${path.map(escapeJsonPathPredicateKeySegment).join(".")}`
419
443
  }
420
444
  default:
421
445
  return undefined
@@ -450,6 +474,9 @@ const valueKeyOfLiteral = (value: unknown): string => {
450
474
  return "null"
451
475
  }
452
476
  if (value instanceof Date) {
477
+ if (Number.isNaN(value.getTime())) {
478
+ throw new Error("Expected a valid Date value")
479
+ }
453
480
  return `date:${value.toISOString()}`
454
481
  }
455
482
  return "unknown"
@@ -37,6 +37,31 @@ export interface QueryState<Outstanding extends string, AvailableNames extends s
37
37
  }
38
38
  /** Effective SQL dialect carried by an expression. */
39
39
  export type DialectOf<Value extends Expression.Any> = Value[typeof Expression.TypeId]["dialect"];
40
+ type ConcreteDialect<D> = D extends "standard" ? never : D;
41
+ type IsDialectUnion<Union, All = Union> = Union extends any ? [
42
+ All
43
+ ] extends [Union] ? false : true : false;
44
+ export type DialectConflictError<A extends string, B extends string> = string & {
45
+ readonly _tag: "DialectConflict";
46
+ readonly left: A;
47
+ readonly right: B;
48
+ };
49
+ export type MergeDialect<A extends string, B extends string> = [
50
+ ConcreteDialect<A>,
51
+ ConcreteDialect<B>
52
+ ] extends [never, never] ? "standard" : [
53
+ ConcreteDialect<A>,
54
+ ConcreteDialect<B>
55
+ ] extends [never, infer C extends string] ? C : [
56
+ ConcreteDialect<A>,
57
+ ConcreteDialect<B>
58
+ ] extends [infer C extends string, never] ? C : ConcreteDialect<A> extends ConcreteDialect<B> ? ConcreteDialect<B> extends ConcreteDialect<A> ? A : DialectConflictError<A, B> : DialectConflictError<A, B>;
59
+ /** Collapses the portable standard tag out of a dialect union. */
60
+ export type NormalizeDialect<Dialect extends string> = [
61
+ Dialect
62
+ ] extends [never] ? never : [
63
+ Exclude<Dialect, "standard">
64
+ ] extends [never] ? "standard" : IsDialectUnion<Exclude<Dialect, "standard">> extends true ? DialectConflictError<Exclude<Dialect, "standard">, Exclude<Dialect, "standard">> : Exclude<Dialect, "standard">;
40
65
  /** Source dependency union carried by an expression. */
41
66
  export type DependenciesOf<Value extends Expression.Any> = Expression.DependenciesOf<Value>;
42
67
  /** Aggregation kind carried by an expression. */
@@ -135,10 +160,11 @@ export type SelectionShape = Expression.Any | {
135
160
  export type ExtractRequired<Selection> = Selection extends Expression.Any ? RequiredFromDependencies<DependenciesOf<Selection>> : Selection extends Record<string, any> ? {
136
161
  [K in keyof Selection]: ExtractRequired<Selection[K]>;
137
162
  }[keyof Selection] : never;
138
- /** Walks a selection tree and unions the dialects referenced by its leaves. */
139
- export type ExtractDialect<Selection> = Selection extends Expression.Any ? DialectOf<Selection> : Selection extends Record<string, any> ? {
140
- [K in keyof Selection]: ExtractDialect<Selection[K]>;
163
+ type ExtractDialectRaw<Selection> = Selection extends Expression.Any ? DialectOf<Selection> : Selection extends Record<string, any> ? {
164
+ [K in keyof Selection]: ExtractDialectRaw<Selection[K]>;
141
165
  }[keyof Selection] : never;
166
+ /** Walks a selection tree and extracts the effective dialects referenced by its leaves. */
167
+ export type ExtractDialect<Selection> = NormalizeDialect<Extract<ExtractDialectRaw<Selection>, string>>;
142
168
  /**
143
169
  * Minimal table-like shape required by `from(...)` and joins.
144
170
  *
@@ -419,8 +445,7 @@ export type MutationTargetNamesOf<Target extends MutationTargetInput> = Target e
419
445
  export type UpdateInputOfTarget<Target extends MutationTargetInput> = Target extends MutationTargetLike ? MutationInputOf<Table.UpdateOf<Target>> : Target extends MutationTargetTuple ? Simplify<{
420
446
  readonly [K in MutationTargetNamesOf<Target>]?: MutationInputOf<Table.UpdateOf<MutationTargetByName<Target, K>>>;
421
447
  }> : never;
422
- /** Extracts the effective dialect from a source. */
423
- export type SourceDialectOf<Source extends SourceLike> = Source extends TableLike<any, infer Dialect> ? Dialect : Source extends {
448
+ type SourceDialectOfRaw<Source extends SourceLike> = Source extends TableLike<any, infer Dialect> ? Dialect : Source extends {
424
449
  readonly kind: "derived";
425
450
  readonly plan: infer PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>;
426
451
  } ? PlanDialectOf<PlanValue> : Source extends {
@@ -432,6 +457,8 @@ export type SourceDialectOf<Source extends SourceLike> = Source extends TableLik
432
457
  } ? PlanDialectOf<PlanValue> : Source extends {
433
458
  readonly dialect: infer Dialect extends string;
434
459
  } ? Dialect : never;
460
+ /** Extracts the effective dialect from a source. */
461
+ export type SourceDialectOf<Source extends SourceLike> = NormalizeDialect<SourceDialectOfRaw<Source>>;
435
462
  /** Extracts the base table name from a source. */
436
463
  export type SourceBaseNameOf<Source extends SourceLike> = Source extends TableLike<any, any> ? Source[typeof Table.TypeId]["baseName"] : Source extends {
437
464
  readonly kind: "derived";
@@ -493,7 +520,7 @@ export type RequiredOfPlan<PlanValue extends QueryPlan<any, any, any, any, any,
493
520
  /** Extracts the available-source scope carried by a query plan. */
494
521
  export type AvailableOfPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanParts<PlanValue>["available"];
495
522
  /** Extracts the effective dialect carried by a query plan. */
496
- export type PlanDialectOf<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanParts<PlanValue>["dialect"];
523
+ export type PlanDialectOf<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = NormalizeDialect<QueryPlanParts<PlanValue>["dialect"]>;
497
524
  /** Extracts the grouped-source phantom carried by a query plan. */
498
525
  export type GroupedOfPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any, any, any>> = QueryPlanState<PlanValue>["grouped"];
499
526
  /** Extracts the available-name phantom carried by a query plan. */
@@ -562,7 +589,7 @@ type MergeNullability<Left extends Expression.Nullability, Right extends Express
562
589
  /** Folds nullability across a tuple for null-propagating scalar operators. */
563
590
  export type MergeNullabilityTuple<Values extends readonly Expression.Any[], Current extends Expression.Nullability = "never"> = Values extends readonly [infer Head extends Expression.Any, ...infer Tail extends readonly Expression.Any[]] ? MergeNullabilityTuple<Tail, MergeNullability<Current, Expression.NullabilityOf<Head>>> : Current;
564
591
  /** Dialect union across a tuple of expressions. */
565
- export type TupleDialect<Values extends readonly Expression.Any[]> = Values[number] extends never ? never : DialectOf<Values[number]>;
592
+ export type TupleDialect<Values extends readonly Expression.Any[]> = Values[number] extends never ? never : NormalizeDialect<DialectOf<Values[number]>>;
566
593
  /** Dependency union across a tuple of scalar expressions. */
567
594
  export type TupleDependencies<Values extends readonly Expression.Any[]> = Values[number] extends never ? never : DependenciesOf<Values[number]>;
568
595
  /** Builds the canonical dependency union from a string union of table names. */
@@ -661,7 +688,7 @@ type AggregationCompatibilityError<PlanValue extends QueryPlan<any, any, any, an
661
688
  };
662
689
  type DialectCompatibilityError<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, EngineDialect extends string> = PlanValue & {
663
690
  readonly __effect_qb_error__: "effect-qb: plan dialect is not compatible with the target renderer or executor";
664
- readonly __effect_qb_plan_dialect__: PlanValue[typeof RowSet.TypeId]["dialect"];
691
+ readonly __effect_qb_plan_dialect__: PlanDialectOf<PlanValue>;
665
692
  readonly __effect_qb_target_dialect__: EngineDialect;
666
693
  readonly __effect_qb_hint__: "Use the matching dialect module or renderer/executor";
667
694
  };
@@ -678,11 +705,11 @@ export type AggregationCompatiblePlan<PlanValue extends QueryPlan<any, any, any,
678
705
  /** Narrows a query plan to aggregate-compatible, source-complete plans. */
679
706
  export type CompletePlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = StatementOfPlan<PlanValue> extends "insert" ? InsertSourceStateOfPlan<PlanValue> extends "missing" ? InsertHasOptionalDefaults<PlanValue> extends true ? PlanValue : InsertSourceCompletenessError<PlanValue> : HasKnownOutstanding<RequiredOfPlan<PlanValue>> extends true ? SourceCompletenessError<PlanValue, Extract<RequiredOfPlan<PlanValue>, string>> : IsAggregationCompatibleSelection<SelectionOfPlan<PlanValue>, GroupedOfPlan<PlanValue>> extends true ? PlanValue : AggregationCompatibilityError<PlanValue> : HasKnownOutstanding<RequiredOfPlan<PlanValue>> extends true ? SourceCompletenessError<PlanValue, Extract<RequiredOfPlan<PlanValue>, string>> : IsAggregationCompatibleSelection<SelectionOfPlan<PlanValue>, GroupedOfPlan<PlanValue>> extends true ? PlanValue : AggregationCompatibilityError<PlanValue>;
680
707
  /** Whether a plan dialect is compatible with a target engine dialect. */
681
- type IsDialectCompatible<PlanDialect extends string, EngineDialect extends string> = [PlanDialect] extends [never] ? true : Extract<PlanDialect, EngineDialect> extends never ? false : true;
708
+ type IsDialectCompatible<PlanDialect extends string, EngineDialect extends string> = [PlanDialect] extends [never] ? true : Exclude<PlanDialect, EngineDialect | "standard"> extends never ? true : false;
682
709
  /** Narrows a complete plan to those compatible with a target engine dialect. */
683
- export type DialectCompatiblePlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, EngineDialect extends string> = IsDialectCompatible<PlanValue[typeof RowSet.TypeId]["dialect"], EngineDialect> extends true ? CompletePlan<PlanValue> : DialectCompatibilityError<PlanValue, EngineDialect>;
710
+ export type DialectCompatiblePlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, EngineDialect extends string> = IsDialectCompatible<PlanDialectOf<PlanValue>, EngineDialect> extends true ? CompletePlan<PlanValue> : DialectCompatibilityError<PlanValue, EngineDialect>;
684
711
  /** Nested-plan compatibility used by subquery expressions such as `exists(...)`. */
685
- export type DialectCompatibleNestedPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, EngineDialect extends string> = IsDialectCompatible<PlanValue[typeof RowSet.TypeId]["dialect"], EngineDialect> extends true ? AggregationCompatiblePlan<PlanValue> : DialectCompatibilityError<PlanValue, EngineDialect>;
712
+ export type DialectCompatibleNestedPlan<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>, EngineDialect extends string> = IsDialectCompatible<PlanDialectOf<PlanValue>, EngineDialect> extends true ? AggregationCompatiblePlan<PlanValue> : DialectCompatibilityError<PlanValue, EngineDialect>;
686
713
  type SetOperandStatement = "select" | "set";
687
714
  type IsUnion<Value, All = Value> = Value extends any ? ([All] extends [Value] ? false : true) : never;
688
715
  type SingleSelectedExpressionError<PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>> = PlanValue & {