effect-qb 0.15.0 → 0.17.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 (88) hide show
  1. package/dist/mysql.js +1957 -595
  2. package/dist/postgres/metadata.js +2507 -182
  3. package/dist/postgres.js +9587 -8201
  4. package/dist/sqlite.js +8360 -0
  5. package/package.json +7 -2
  6. package/src/internal/column-state.ts +7 -0
  7. package/src/internal/column.ts +22 -0
  8. package/src/internal/derived-table.ts +29 -3
  9. package/src/internal/dialect.ts +14 -1
  10. package/src/internal/dsl-mutation-runtime.ts +173 -4
  11. package/src/internal/dsl-plan-runtime.ts +165 -20
  12. package/src/internal/dsl-query-runtime.ts +60 -6
  13. package/src/internal/dsl-transaction-ddl-runtime.ts +72 -2
  14. package/src/internal/executor.ts +62 -13
  15. package/src/internal/expression-ast.ts +3 -2
  16. package/src/internal/grouping-key.ts +141 -1
  17. package/src/internal/implication-runtime.ts +2 -1
  18. package/src/internal/json/types.ts +155 -40
  19. package/src/internal/predicate/analysis.ts +103 -1
  20. package/src/internal/predicate/atom.ts +7 -0
  21. package/src/internal/predicate/context.ts +170 -17
  22. package/src/internal/predicate/key.ts +64 -2
  23. package/src/internal/predicate/normalize.ts +115 -34
  24. package/src/internal/predicate/runtime.ts +144 -13
  25. package/src/internal/query.ts +563 -103
  26. package/src/internal/renderer.ts +39 -2
  27. package/src/internal/runtime/driver-value-mapping.ts +244 -0
  28. package/src/internal/runtime/normalize.ts +62 -38
  29. package/src/internal/runtime/schema.ts +5 -3
  30. package/src/internal/runtime/value.ts +153 -30
  31. package/src/internal/scalar.ts +11 -0
  32. package/src/internal/table-options.ts +108 -1
  33. package/src/internal/table.ts +87 -29
  34. package/src/mysql/column.ts +19 -2
  35. package/src/mysql/datatypes/index.ts +21 -0
  36. package/src/mysql/errors/catalog.ts +5 -5
  37. package/src/mysql/errors/normalize.ts +2 -2
  38. package/src/mysql/executor.ts +20 -5
  39. package/src/mysql/internal/dialect.ts +12 -6
  40. package/src/mysql/internal/dsl.ts +995 -263
  41. package/src/mysql/internal/renderer.ts +13 -3
  42. package/src/mysql/internal/sql-expression-renderer.ts +530 -128
  43. package/src/mysql/query.ts +9 -2
  44. package/src/mysql/renderer.ts +7 -2
  45. package/src/mysql/table.ts +38 -12
  46. package/src/postgres/cast.ts +22 -7
  47. package/src/postgres/column.ts +5 -2
  48. package/src/postgres/errors/normalize.ts +2 -2
  49. package/src/postgres/executor.ts +68 -10
  50. package/src/postgres/function/core.ts +19 -1
  51. package/src/postgres/internal/dialect.ts +12 -6
  52. package/src/postgres/internal/dsl.ts +958 -288
  53. package/src/postgres/internal/renderer.ts +13 -3
  54. package/src/postgres/internal/schema-ddl.ts +2 -1
  55. package/src/postgres/internal/schema-model.ts +6 -3
  56. package/src/postgres/internal/sql-expression-renderer.ts +477 -96
  57. package/src/postgres/json.ts +57 -17
  58. package/src/postgres/query.ts +9 -2
  59. package/src/postgres/renderer.ts +7 -2
  60. package/src/postgres/schema-management.ts +91 -4
  61. package/src/postgres/schema.ts +1 -1
  62. package/src/postgres/table.ts +189 -53
  63. package/src/postgres/type.ts +4 -0
  64. package/src/sqlite/column.ts +128 -0
  65. package/src/sqlite/datatypes/index.ts +79 -0
  66. package/src/sqlite/datatypes/spec.ts +98 -0
  67. package/src/sqlite/errors/catalog.ts +103 -0
  68. package/src/sqlite/errors/fields.ts +19 -0
  69. package/src/sqlite/errors/index.ts +19 -0
  70. package/src/sqlite/errors/normalize.ts +229 -0
  71. package/src/sqlite/errors/requirements.ts +71 -0
  72. package/src/sqlite/errors/types.ts +29 -0
  73. package/src/sqlite/executor.ts +227 -0
  74. package/src/sqlite/function/aggregate.ts +2 -0
  75. package/src/sqlite/function/core.ts +2 -0
  76. package/src/sqlite/function/index.ts +19 -0
  77. package/src/sqlite/function/string.ts +2 -0
  78. package/src/sqlite/function/temporal.ts +100 -0
  79. package/src/sqlite/function/window.ts +2 -0
  80. package/src/sqlite/internal/dialect.ts +37 -0
  81. package/src/sqlite/internal/dsl.ts +6926 -0
  82. package/src/sqlite/internal/renderer.ts +47 -0
  83. package/src/sqlite/internal/sql-expression-renderer.ts +1821 -0
  84. package/src/sqlite/json.ts +2 -0
  85. package/src/sqlite/query.ts +196 -0
  86. package/src/sqlite/renderer.ts +24 -0
  87. package/src/sqlite/table.ts +183 -0
  88. package/src/sqlite.ts +22 -0
@@ -1,4 +1,4 @@
1
- import type { EqColumnAtom, EqLiteralAtom, NeqLiteralAtom, NonNullAtom, NullAtom, PredicateAtom, UnknownAtom } from "./atom.js"
1
+ import type { EqColumnAtom, EqLiteralAtom, LiteralSetAtom, NeqLiteralAtom, NonNullAtom, NullAtom, PredicateAtom, UnknownAtom } from "./atom.js"
2
2
  import type { AllFormula, AnyFormula, AtomFormula, FalseFormula, NotFormula, PredicateFormula, TrueFormula } from "./formula.js"
3
3
 
4
4
  type Polarity = "positive" | "negative"
@@ -8,6 +8,8 @@ export interface Context<
8
8
  NullKeys extends string = never,
9
9
  EqLiterals = {},
10
10
  NeqLiterals = {},
11
+ LiteralSets = {},
12
+ JsonLiteralSets = {},
11
13
  SourceNames extends string = never,
12
14
  Contradiction extends boolean = false,
13
15
  Unknown extends boolean = false
@@ -16,6 +18,8 @@ export interface Context<
16
18
  readonly nullKeys: NullKeys
17
19
  readonly eqLiterals: EqLiterals
18
20
  readonly neqLiterals: NeqLiterals
21
+ readonly literalSets: LiteralSets
22
+ readonly jsonLiteralSets: JsonLiteralSets
19
23
  readonly sourceNames: SourceNames
20
24
  readonly contradiction: Contradiction
21
25
  readonly unknown: Unknown
@@ -27,6 +31,8 @@ type AnyContext = Context<
27
31
  string,
28
32
  Record<string, string>,
29
33
  Record<string, string>,
34
+ Record<string, string>,
35
+ Record<string, Record<string, string>>,
30
36
  string,
31
37
  boolean,
32
38
  boolean
@@ -40,7 +46,20 @@ type Frame<
40
46
  readonly polarity: Direction
41
47
  }
42
48
 
43
- 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
44
63
 
45
64
  type EqLiteralValueOf<
46
65
  EqLiterals,
@@ -84,15 +103,75 @@ type MergeNeqLiteralMaps<Left, Right> = {
84
103
  : never
85
104
  }
86
105
 
106
+ type MergeLiteralSetMaps<Left, Right> = {
107
+ readonly [K in Extract<keyof Left | keyof Right, string>]:
108
+ K extends keyof Left
109
+ ? K extends keyof Right
110
+ ? Extract<Left[K], Right[K]>
111
+ : Left[K]
112
+ : K extends keyof Right
113
+ ? Right[K]
114
+ : never
115
+ }
116
+
117
+ type MergeJsonLiteralSetPathMaps<Left, Right> = {
118
+ readonly [K in Extract<keyof Left | keyof Right, string>]:
119
+ K extends keyof Left
120
+ ? K extends keyof Right
121
+ ? Extract<Left[K], Right[K]>
122
+ : Left[K]
123
+ : K extends keyof Right
124
+ ? Right[K]
125
+ : never
126
+ }
127
+
128
+ type MergeJsonLiteralSetMaps<Left, Right> = {
129
+ readonly [K in Extract<keyof Left | keyof Right, string>]:
130
+ K extends keyof Left
131
+ ? K extends keyof Right
132
+ ? MergeJsonLiteralSetPathMaps<Left[K], Right[K]>
133
+ : Left[K]
134
+ : K extends keyof Right
135
+ ? Right[K]
136
+ : never
137
+ }
138
+
87
139
  type FilterNeverValues<Map> = {
88
140
  readonly [K in keyof Map as Map[K] extends never ? never : K]: Map[K]
89
141
  }
90
142
 
143
+ type JsonKeyParts<Key extends string> = string extends Key
144
+ ? never
145
+ : Key extends `${infer ColumnKey}#json:${infer Path}`
146
+ ? readonly [ColumnKey, Path]
147
+ : never
148
+
149
+ type LiteralSetMapAfterAdd<
150
+ LiteralSets,
151
+ Key extends string,
152
+ Values extends string
153
+ > = [JsonKeyParts<Key>] extends [never]
154
+ ? FilterNeverValues<MergeLiteralSetMaps<LiteralSets, { readonly [K in Key]: Values }>>
155
+ : LiteralSets
156
+
157
+ type JsonLiteralSetMapAfterAdd<
158
+ JsonLiteralSets,
159
+ Key extends string,
160
+ Values extends string
161
+ > = JsonKeyParts<Key> extends readonly [infer ColumnKey extends string, infer Path extends string]
162
+ ? MergeJsonLiteralSetMaps<
163
+ JsonLiteralSets,
164
+ { readonly [C in ColumnKey]: { readonly [P in Path]: Values } }
165
+ >
166
+ : JsonLiteralSets
167
+
91
168
  type MarkContradiction<Ctx extends AnyContext> = Context<
92
169
  Ctx["nonNullKeys"],
93
170
  Ctx["nullKeys"],
94
171
  Ctx["eqLiterals"],
95
172
  Ctx["neqLiterals"],
173
+ Ctx["literalSets"],
174
+ Ctx["jsonLiteralSets"],
96
175
  Ctx["sourceNames"],
97
176
  true,
98
177
  Ctx["unknown"]
@@ -103,6 +182,8 @@ type MarkUnknown<Ctx extends AnyContext> = Context<
103
182
  Ctx["nullKeys"],
104
183
  Ctx["eqLiterals"],
105
184
  Ctx["neqLiterals"],
185
+ Ctx["literalSets"],
186
+ Ctx["jsonLiteralSets"],
106
187
  Ctx["sourceNames"],
107
188
  Ctx["contradiction"],
108
189
  true
@@ -116,6 +197,8 @@ type AddNonNull<
116
197
  Ctx["nullKeys"],
117
198
  Ctx["eqLiterals"],
118
199
  Ctx["neqLiterals"],
200
+ Ctx["literalSets"],
201
+ Ctx["jsonLiteralSets"],
119
202
  Ctx["sourceNames"] | SourceNameOfKey<Key>,
120
203
  Key extends Ctx["nullKeys"] ? true : Ctx["contradiction"],
121
204
  Ctx["unknown"]
@@ -129,11 +212,35 @@ type AddNull<
129
212
  Ctx["nullKeys"] | Key,
130
213
  Ctx["eqLiterals"],
131
214
  Ctx["neqLiterals"],
215
+ Ctx["literalSets"],
216
+ Ctx["jsonLiteralSets"],
132
217
  Ctx["sourceNames"] | SourceNameOfKey<Key>,
133
218
  Key extends Ctx["nonNullKeys"] ? true : Ctx["contradiction"],
134
219
  Ctx["unknown"]
135
220
  >
136
221
 
222
+ type AddLiteralSet<
223
+ Ctx extends AnyContext,
224
+ Key extends string,
225
+ Values extends string
226
+ > = Context<
227
+ Ctx["nonNullKeys"] | Key,
228
+ Ctx["nullKeys"],
229
+ Ctx["eqLiterals"],
230
+ Ctx["neqLiterals"],
231
+ LiteralSetMapAfterAdd<Ctx["literalSets"], Key, Values>,
232
+ JsonLiteralSetMapAfterAdd<Ctx["jsonLiteralSets"], Key, Values>,
233
+ Ctx["sourceNames"] | SourceNameOfKey<Key>,
234
+ EqLiteralValueOf<Ctx["eqLiterals"], Key> extends infer EqValue
235
+ ? [EqValue] extends [never]
236
+ ? Key extends Ctx["nullKeys"] ? true : Ctx["contradiction"]
237
+ : EqValue extends Values
238
+ ? Key extends Ctx["nullKeys"] ? true : Ctx["contradiction"]
239
+ : true
240
+ : true,
241
+ Ctx["unknown"]
242
+ >
243
+
137
244
  type AddEqLiteral<
138
245
  Ctx extends AnyContext,
139
246
  Key extends string,
@@ -143,6 +250,8 @@ type AddEqLiteral<
143
250
  Ctx["nullKeys"],
144
251
  FilterNeverValues<MergeEqLiteralMaps<Ctx["eqLiterals"], { readonly [K in Key]: Value }>>,
145
252
  Ctx["neqLiterals"],
253
+ LiteralSetMapAfterAdd<Ctx["literalSets"], Key, Value>,
254
+ JsonLiteralSetMapAfterAdd<Ctx["jsonLiteralSets"], Key, Value>,
146
255
  Ctx["sourceNames"] | SourceNameOfKey<Key>,
147
256
  EqLiteralValueOf<Ctx["eqLiterals"], Key> extends never
148
257
  ? NeqLiteralValuesOf<Ctx["neqLiterals"], Key> extends infer NeqValues
@@ -169,6 +278,8 @@ type AddNeqLiteral<
169
278
  Ctx["nullKeys"],
170
279
  Ctx["eqLiterals"],
171
280
  FilterNeverValues<MergeNeqLiteralMaps<Ctx["neqLiterals"], { readonly [K in Key]: Value }>>,
281
+ Ctx["literalSets"],
282
+ Ctx["jsonLiteralSets"],
172
283
  Ctx["sourceNames"] | SourceNameOfKey<Key>,
173
284
  EqLiteralValueOf<Ctx["eqLiterals"], Key> extends infer EqValue
174
285
  ? [EqValue] extends [never]
@@ -219,11 +330,13 @@ type ApplyAtom<
219
330
  ? AddEqLiteral<Ctx, Key, Value>
220
331
  : Atom extends NeqLiteralAtom<infer Key extends string, infer Value extends string>
221
332
  ? AddNeqLiteral<Ctx, Key, Value>
222
- : Atom extends EqColumnAtom<infer Left extends string, infer Right extends string>
223
- ? ApplyEqColumn<Ctx, Left, Right>
224
- : Atom extends UnknownAtom<any>
225
- ? MarkUnknown<Ctx>
226
- : Ctx
333
+ : Atom extends LiteralSetAtom<infer Key extends string, infer Values extends string>
334
+ ? AddLiteralSet<Ctx, Key, Values>
335
+ : Atom extends EqColumnAtom<infer Left extends string, infer Right extends string>
336
+ ? ApplyEqColumn<Ctx, Left, Right>
337
+ : Atom extends UnknownAtom<any>
338
+ ? MarkUnknown<Ctx>
339
+ : Ctx
227
340
 
228
341
  type ApplyNegativeAtom<
229
342
  Ctx extends AnyContext,
@@ -237,11 +350,13 @@ type ApplyNegativeAtom<
237
350
  ? AddNeqLiteral<Ctx, Key, Value>
238
351
  : Atom extends NeqLiteralAtom<infer Key extends string, infer Value extends string>
239
352
  ? AddEqLiteral<Ctx, Key, Value>
240
- : Atom extends EqColumnAtom<infer Left extends string, infer Right extends string>
241
- ? AddNonNull<AddNonNull<Ctx, Left>, Right>
242
- : Atom extends UnknownAtom<any>
243
- ? MarkUnknown<Ctx>
244
- : Ctx
353
+ : Atom extends LiteralSetAtom<infer Key extends string, any>
354
+ ? AddNonNull<Ctx, Key>
355
+ : Atom extends EqColumnAtom<infer Left extends string, infer Right extends string>
356
+ ? AddNonNull<AddNonNull<Ctx, Left>, Right>
357
+ : Atom extends UnknownAtom<any>
358
+ ? MarkUnknown<Ctx>
359
+ : Ctx
245
360
 
246
361
  type FramesFromItems<
247
362
  Items extends readonly PredicateFormula[],
@@ -268,6 +383,27 @@ type IntersectNeqLiteralMaps<
268
383
  Extract<Left[K], Right[K]> extends never ? never : Extract<Left[K], Right[K]>
269
384
  }>
270
385
 
386
+ type UnionLiteralSetMaps<
387
+ Left,
388
+ Right
389
+ > = FilterNeverValues<{
390
+ readonly [K in Extract<keyof Left, keyof Right>]: Left[K] | Right[K]
391
+ }>
392
+
393
+ type UnionJsonLiteralSetPathMaps<
394
+ Left,
395
+ Right
396
+ > = FilterNeverValues<{
397
+ readonly [K in Extract<keyof Left, keyof Right>]: Left[K] | Right[K]
398
+ }>
399
+
400
+ type UnionJsonLiteralSetMaps<
401
+ Left,
402
+ Right
403
+ > = {
404
+ readonly [K in Extract<keyof Left, keyof Right>]: UnionJsonLiteralSetPathMaps<Left[K], Right[K]>
405
+ }
406
+
271
407
  type IntersectContexts<
272
408
  Left extends AnyContext,
273
409
  Right extends AnyContext
@@ -276,6 +412,8 @@ type IntersectContexts<
276
412
  Extract<Left["nullKeys"], Right["nullKeys"]>,
277
413
  IntersectEqLiteralMaps<Left["eqLiterals"], Right["eqLiterals"]>,
278
414
  IntersectNeqLiteralMaps<Left["neqLiterals"], Right["neqLiterals"]>,
415
+ UnionLiteralSetMaps<Left["literalSets"], Right["literalSets"]>,
416
+ UnionJsonLiteralSetMaps<Left["jsonLiteralSets"], Right["jsonLiteralSets"]>,
279
417
  Extract<Left["sourceNames"], Right["sourceNames"]>,
280
418
  Left["contradiction"] extends true
281
419
  ? Right["contradiction"]
@@ -289,17 +427,20 @@ type AnalyzeAnyBranches<
289
427
  Ctx extends AnyContext,
290
428
  Items extends readonly PredicateFormula[],
291
429
  Direction extends Polarity,
292
- Current extends AnyContext | never = never
293
- > = Items extends readonly [
430
+ Current extends AnyContext | never = never,
431
+ Seen extends readonly unknown[] = []
432
+ > = Seen["length"] extends 20
433
+ ? MarkUnknown<Ctx>
434
+ : Items extends readonly [
294
435
  infer Head extends PredicateFormula,
295
436
  ...infer Tail extends readonly PredicateFormula[]
296
437
  ]
297
438
  ? AnalyzeBranch<Ctx, Head, Direction> extends infer Branch extends AnyContext
298
439
  ? Branch["contradiction"] extends true
299
- ? AnalyzeAnyBranches<Ctx, Tail, Direction, Current>
440
+ ? AnalyzeAnyBranches<Ctx, Tail, Direction, Current, readonly [...Seen, unknown]>
300
441
  : [Current] extends [never]
301
- ? AnalyzeAnyBranches<Ctx, Tail, Direction, Branch>
302
- : AnalyzeAnyBranches<Ctx, Tail, Direction, IntersectContexts<Current, Branch>>
442
+ ? AnalyzeAnyBranches<Ctx, Tail, Direction, Branch, readonly [...Seen, unknown]>
443
+ : AnalyzeAnyBranches<Ctx, Tail, Direction, IntersectContexts<Current, Branch>, readonly [...Seen, unknown]>
303
444
  : never
304
445
  : [Current] extends [never]
305
446
  ? MarkContradiction<Ctx>
@@ -356,3 +497,15 @@ export type AnalyzeStack<
356
497
 
357
498
  export type AnalyzeFormula<Formula extends PredicateFormula> =
358
499
  AnalyzeStack<EmptyContext, readonly [Frame<Formula, "positive">]>
500
+
501
+ export type PredicateContext = AnyContext
502
+
503
+ export type AssumeFactsTrue<
504
+ Ctx extends PredicateContext,
505
+ Formula extends PredicateFormula
506
+ > = AnalyzeStack<Ctx, readonly [Frame<Formula, "positive">]>
507
+
508
+ export type AssumeFactsFalse<
509
+ Ctx extends PredicateContext,
510
+ Formula extends PredicateFormula
511
+ > = AnalyzeStack<Ctx, readonly [Frame<Formula, "negative">]>
@@ -1,21 +1,83 @@
1
1
  import type * as Expression from "../scalar.js"
2
2
  import type * as ExpressionAst from "../expression-ast.js"
3
+ import type * as JsonPath from "../json/path.js"
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>
3
21
 
4
22
  export type ColumnKey<
5
23
  TableName extends string,
6
24
  ColumnName extends string
7
- > = `${TableName}.${ColumnName}`
25
+ > = `${EscapePredicateSegment<TableName>}.${EscapePredicateSegment<ColumnName>}`
8
26
 
9
27
  export type ColumnKeyOfAst<Ast extends ExpressionAst.Any> =
10
- Ast extends ExpressionAst.ColumnNode<infer TableName extends string, infer ColumnName extends string>
28
+ [Ast] extends [ExpressionAst.ColumnNode<infer TableName extends string, infer ColumnName extends string>]
11
29
  ? ColumnKey<TableName, ColumnName>
12
30
  : never
13
31
 
32
+ type JsonPathKey<
33
+ Segments extends ExpressionAst.JsonSegmentTuple,
34
+ Current extends string = never,
35
+ Seen extends readonly unknown[] = []
36
+ > = Seen["length"] extends 8
37
+ ? never
38
+ : Segments extends readonly [
39
+ infer Segment,
40
+ ...infer Tail extends ExpressionAst.JsonSegmentTuple
41
+ ]
42
+ ? Segment extends JsonPath.KeySegment<infer Key extends string>
43
+ ? JsonPathKey<
44
+ Tail,
45
+ [Current] extends [never] ? EscapeJsonPathSegment<Key> : `${Current}.${EscapeJsonPathSegment<Key>}`,
46
+ readonly [...Seen, unknown]
47
+ >
48
+ : never
49
+ : Current
50
+
51
+ export type JsonPathPredicateKey<
52
+ Base extends Expression.Any,
53
+ Segments extends ExpressionAst.JsonSegmentTuple
54
+ > = [ColumnKeyOfExpression<Base>] extends [never]
55
+ ? never
56
+ : [JsonPathKey<Segments>] extends [never]
57
+ ? never
58
+ : `${ColumnKeyOfExpression<Base>}#json:${JsonPathKey<Segments>}`
59
+
60
+ export type PredicateKeyOfAst<Ast extends ExpressionAst.Any> =
61
+ [Ast] extends [ExpressionAst.ColumnNode<infer TableName extends string, infer ColumnName extends string>]
62
+ ? ColumnKey<TableName, ColumnName>
63
+ : [Ast] extends [ExpressionAst.CastNode<infer Value extends Expression.Any, infer Target extends Expression.DbType.Any>]
64
+ ? [Target] extends [Expression.DbTypeOf<Value>]
65
+ ? [Expression.DbTypeOf<Value>] extends [Target]
66
+ ? PredicateKeyOfExpression<Value>
67
+ : never
68
+ : never
69
+ : [Ast] extends [ExpressionAst.JsonAccessNode<infer Kind, infer Base extends Expression.Any, infer Segments extends ExpressionAst.JsonSegmentTuple>]
70
+ ? Kind extends "jsonGetText" | "jsonPathText" | "jsonAccessText" | "jsonTraverseText"
71
+ ? JsonPathPredicateKey<Base, Segments>
72
+ : never
73
+ : never
74
+
14
75
  type AstOf<Value extends Expression.Any> = Value extends {
15
76
  readonly [ExpressionAst.TypeId]: infer Ast extends ExpressionAst.Any
16
77
  } ? Ast : never
17
78
 
18
79
  export type ColumnKeyOfExpression<Value extends Expression.Any> = ColumnKeyOfAst<AstOf<Value>>
80
+ export type PredicateKeyOfExpression<Value extends Expression.Any> = PredicateKeyOfAst<AstOf<Value>>
19
81
 
20
82
  export type LiteralKey<Value> =
21
83
  Value extends string ? `string:${Value}` :
@@ -1,8 +1,8 @@
1
1
  import type * as Expression from "../scalar.js"
2
2
  import type * as ExpressionAst from "../expression-ast.js"
3
- import type { ColumnKeyOfExpression, ValueKey } from "./key.js"
3
+ import type { PredicateKeyOfExpression, ValueKey } from "./key.js"
4
4
  import type { AllFormula, AnyFormula, AtomFormula, FalseFormula, NotFormula, PredicateFormula, TrueFormula } from "./formula.js"
5
- import type { EqColumnAtom, EqLiteralAtom, NeqLiteralAtom, NonNullAtom, NullAtom, UnknownAtom } from "./atom.js"
5
+ import type { EqColumnAtom, EqLiteralAtom, LiteralSetAtom, NeqLiteralAtom, NonNullAtom, NullAtom, UnknownAtom } from "./atom.js"
6
6
 
7
7
  type AstOf<Value extends Expression.Any> = Value extends {
8
8
  readonly [ExpressionAst.TypeId]: infer Ast extends ExpressionAst.Any
@@ -20,9 +20,9 @@ type AtomOf<Atom extends import("./atom.js").PredicateAtom> = AtomFormula<Atom>
20
20
  type FactOf<Atom extends import("./atom.js").PredicateAtom> = AtomFormula<Atom>
21
21
 
22
22
  type NonNullFactsOfExpression<Value extends Expression.Any> =
23
- [ColumnKeyOfExpression<Value>] extends [never]
23
+ [PredicateKeyOfExpression<Value>] extends [never]
24
24
  ? never
25
- : FactOf<NonNullAtom<ColumnKeyOfExpression<Value>>>
25
+ : FactOf<NonNullAtom<PredicateKeyOfExpression<Value>>>
26
26
 
27
27
  type CombineFacts<
28
28
  Left extends PredicateFormula,
@@ -45,8 +45,8 @@ type FormulaOfEq<
45
45
  Left extends Expression.Any,
46
46
  Right extends Expression.Any
47
47
  > =
48
- [ColumnKeyOfExpression<Left>] extends [never]
49
- ? [ColumnKeyOfExpression<Right>] extends [never]
48
+ [PredicateKeyOfExpression<Left>] extends [never]
49
+ ? [PredicateKeyOfExpression<Right>] extends [never]
50
50
  ? LiteralValueOfExpression<Left> extends infer LeftLiteral
51
51
  ? LiteralValueOfExpression<Right> extends infer RightLiteral
52
52
  ? [LeftLiteral] extends [never]
@@ -67,24 +67,24 @@ type FormulaOfEq<
67
67
  ? UnknownTag<"eq:unsupported">
68
68
  : LeftLiteral extends null
69
69
  ? False
70
- : AtomOf<EqLiteralAtom<ColumnKeyOfExpression<Right>, ValueKey<LeftLiteral>>>
70
+ : AtomOf<EqLiteralAtom<PredicateKeyOfExpression<Right>, ValueKey<LeftLiteral>>>
71
71
  : UnknownTag<"eq:unsupported">
72
- : [ColumnKeyOfExpression<Right>] extends [never]
72
+ : [PredicateKeyOfExpression<Right>] extends [never]
73
73
  ? LiteralValueOfExpression<Right> extends infer RightLiteral
74
74
  ? [RightLiteral] extends [never]
75
75
  ? UnknownTag<"eq:unsupported">
76
76
  : RightLiteral extends null
77
77
  ? False
78
- : AtomOf<EqLiteralAtom<ColumnKeyOfExpression<Left>, ValueKey<RightLiteral>>>
78
+ : AtomOf<EqLiteralAtom<PredicateKeyOfExpression<Left>, ValueKey<RightLiteral>>>
79
79
  : UnknownTag<"eq:unsupported">
80
- : AtomOf<import("./atom.js").EqColumnAtom<ColumnKeyOfExpression<Left>, ColumnKeyOfExpression<Right>>>
80
+ : AtomOf<import("./atom.js").EqColumnAtom<PredicateKeyOfExpression<Left>, PredicateKeyOfExpression<Right>>>
81
81
 
82
82
  type FormulaOfNeq<
83
83
  Left extends Expression.Any,
84
84
  Right extends Expression.Any
85
85
  > =
86
- [ColumnKeyOfExpression<Left>] extends [never]
87
- ? [ColumnKeyOfExpression<Right>] extends [never]
86
+ [PredicateKeyOfExpression<Left>] extends [never]
87
+ ? [PredicateKeyOfExpression<Right>] extends [never]
88
88
  ? LiteralValueOfExpression<Left> extends infer LeftLiteral
89
89
  ? LiteralValueOfExpression<Right> extends infer RightLiteral
90
90
  ? [LeftLiteral] extends [never]
@@ -105,15 +105,15 @@ type FormulaOfNeq<
105
105
  ? UnknownTag<"neq:unsupported">
106
106
  : LeftLiteral extends null
107
107
  ? False
108
- : AtomOf<NeqLiteralAtom<ColumnKeyOfExpression<Right>, ValueKey<LeftLiteral>>>
108
+ : AtomOf<NeqLiteralAtom<PredicateKeyOfExpression<Right>, ValueKey<LeftLiteral>>>
109
109
  : UnknownTag<"neq:unsupported">
110
- : [ColumnKeyOfExpression<Right>] extends [never]
110
+ : [PredicateKeyOfExpression<Right>] extends [never]
111
111
  ? LiteralValueOfExpression<Right> extends infer RightLiteral
112
112
  ? [RightLiteral] extends [never]
113
113
  ? UnknownTag<"neq:unsupported">
114
114
  : RightLiteral extends null
115
115
  ? False
116
- : AtomOf<NeqLiteralAtom<ColumnKeyOfExpression<Left>, ValueKey<RightLiteral>>>
116
+ : AtomOf<NeqLiteralAtom<PredicateKeyOfExpression<Left>, ValueKey<RightLiteral>>>
117
117
  : UnknownTag<"neq:unsupported">
118
118
  : CombineFacts<NonNullFactsOfExpression<Left>, NonNullFactsOfExpression<Right>>
119
119
 
@@ -127,23 +127,23 @@ type FormulaOfIsNotDistinctFrom<
127
127
  ? [RightLiteral] extends [never]
128
128
  ? UnknownTag<"isNotDistinctFrom:unsupported">
129
129
  : RightLiteral extends null
130
- ? [ColumnKeyOfExpression<Left>] extends [never]
130
+ ? [PredicateKeyOfExpression<Left>] extends [never]
131
131
  ? UnknownTag<"isNotDistinctFrom:unsupported">
132
- : AtomOf<NullAtom<ColumnKeyOfExpression<Left>>>
132
+ : AtomOf<NullAtom<PredicateKeyOfExpression<Left>>>
133
133
  : UnknownTag<"isNotDistinctFrom:unsupported">
134
134
  : LeftLiteral extends null
135
- ? [ColumnKeyOfExpression<Right>] extends [never]
135
+ ? [PredicateKeyOfExpression<Right>] extends [never]
136
136
  ? UnknownTag<"isNotDistinctFrom:unsupported">
137
- : AtomOf<NullAtom<ColumnKeyOfExpression<Right>>>
137
+ : AtomOf<NullAtom<PredicateKeyOfExpression<Right>>>
138
138
  : RightLiteral extends null
139
- ? [ColumnKeyOfExpression<Left>] extends [never]
139
+ ? [PredicateKeyOfExpression<Left>] extends [never]
140
140
  ? UnknownTag<"isNotDistinctFrom:unsupported">
141
- : AtomOf<NullAtom<ColumnKeyOfExpression<Left>>>
142
- : [ColumnKeyOfExpression<Left>] extends [never]
143
- ? [ColumnKeyOfExpression<Right>] extends [never]
141
+ : AtomOf<NullAtom<PredicateKeyOfExpression<Left>>>
142
+ : [PredicateKeyOfExpression<Left>] extends [never]
143
+ ? [PredicateKeyOfExpression<Right>] extends [never]
144
144
  ? CombineFacts<NonNullFactsOfExpression<Left>, NonNullFactsOfExpression<Right>>
145
- : AtomOf<EqLiteralAtom<ColumnKeyOfExpression<Right>, ValueKey<LeftLiteral>>>
146
- : AtomOf<EqLiteralAtom<ColumnKeyOfExpression<Left>, ValueKey<RightLiteral>>>
145
+ : AtomOf<EqLiteralAtom<PredicateKeyOfExpression<Right>, ValueKey<LeftLiteral>>>
146
+ : AtomOf<EqLiteralAtom<PredicateKeyOfExpression<Left>, ValueKey<RightLiteral>>>
147
147
  : UnknownTag<"isNotDistinctFrom:unsupported">
148
148
  : UnknownTag<"isNotDistinctFrom:unsupported">
149
149
 
@@ -157,9 +157,12 @@ type AndFormulas<
157
157
 
158
158
  type FormulaTupleOf<
159
159
  Values extends readonly Expression.Any[]
160
- > = {
161
- readonly [K in keyof Values]: Values[K] extends Expression.Any ? FormulaOfExpression<Values[K]> : never
162
- } & readonly PredicateFormula[]
160
+ > = Values extends readonly [
161
+ infer Head extends Expression.Any,
162
+ ...infer Tail extends readonly Expression.Any[]
163
+ ]
164
+ ? readonly [FormulaOfExpression<Head>, ...FormulaTupleOf<Tail>]
165
+ : readonly []
163
166
 
164
167
  type AllFormulaOfValues<
165
168
  Values extends readonly Expression.Any[]
@@ -167,7 +170,53 @@ type AllFormulaOfValues<
167
170
 
168
171
  type AnyFormulaOfValues<
169
172
  Values extends readonly Expression.Any[]
170
- > = import("./formula.js").NormalizeBooleanConstants<AnyFormula<FormulaTupleOf<Values>>>
173
+ > = FormulaTupleOf<Values> extends infer Formulas extends readonly PredicateFormula[]
174
+ ? [CompactOrLiteralSet<Formulas>] extends [infer Compact extends PredicateFormula]
175
+ ? [Compact] extends [never]
176
+ ? import("./formula.js").NormalizeBooleanConstants<AnyFormula<Formulas>>
177
+ : Compact
178
+ : import("./formula.js").NormalizeBooleanConstants<AnyFormula<Formulas>>
179
+ : import("./formula.js").NormalizeBooleanConstants<AnyFormula<FormulaTupleOf<Values>>>
180
+
181
+ type LiteralSetDetails<Formula extends PredicateFormula> =
182
+ Formula extends AtomFormula<EqLiteralAtom<infer Key extends string, infer Value extends string>>
183
+ ? readonly [Key, Value]
184
+ : Formula extends AtomFormula<LiteralSetAtom<infer Key extends string, infer Values extends string>>
185
+ ? readonly [Key, Values]
186
+ : never
187
+
188
+ type IsUnion<Value, Candidate = Value> =
189
+ [Value] extends [never]
190
+ ? false
191
+ : Value extends unknown
192
+ ? [Candidate] extends [Value] ? false : true
193
+ : false
194
+
195
+ type CompactOrLiteralSet<
196
+ Items extends readonly PredicateFormula[],
197
+ Key extends string = never,
198
+ Values extends string = never,
199
+ Seen extends readonly unknown[] = []
200
+ > = Seen["length"] extends 20
201
+ ? never
202
+ : Items extends readonly [
203
+ infer Head extends PredicateFormula,
204
+ ...infer Tail extends readonly PredicateFormula[]
205
+ ]
206
+ ? LiteralSetDetails<Head> extends readonly [infer HeadKey extends string, infer HeadValues extends string]
207
+ ? IsUnion<HeadKey> extends true
208
+ ? never
209
+ : [Key] extends [never]
210
+ ? CompactOrLiteralSet<Tail, HeadKey, HeadValues, readonly [...Seen, unknown]>
211
+ : [HeadKey] extends [Key]
212
+ ? [Key] extends [HeadKey]
213
+ ? CompactOrLiteralSet<Tail, Key, Values | HeadValues, readonly [...Seen, unknown]>
214
+ : never
215
+ : never
216
+ : never
217
+ : [Key] extends [never]
218
+ ? never
219
+ : AtomOf<LiteralSetAtom<Key, Values>>
171
220
 
172
221
  type FormulaOfInValues<
173
222
  Left extends Expression.Any,
@@ -180,6 +229,38 @@ type FormulaOfInValues<
180
229
  ? FormulaOfInValues<Left, Tail, [...Current, FormulaOfEq<Left, Head>]>
181
230
  : Current
182
231
 
232
+ type LiteralSetValuesOf<
233
+ Values extends readonly Expression.Any[],
234
+ Current extends string = never,
235
+ Seen extends readonly unknown[] = []
236
+ > = Seen["length"] extends 20
237
+ ? string
238
+ : Values extends readonly [
239
+ infer Head extends Expression.Any,
240
+ ...infer Tail extends readonly Expression.Any[]
241
+ ]
242
+ ? LiteralValueOfExpression<Head> extends infer Literal
243
+ ? [Literal] extends [never]
244
+ ? never
245
+ : Literal extends null
246
+ ? never
247
+ : LiteralSetValuesOf<Tail, Current | ValueKey<Literal>, readonly [...Seen, unknown]>
248
+ : never
249
+ : Current
250
+
251
+ type FormulaOfIn<
252
+ Left extends Expression.Any,
253
+ Values extends readonly Expression.Any[]
254
+ > = [PredicateKeyOfExpression<Left>] extends [never]
255
+ ? OrFormulas<FormulaOfInValues<Left, Values>>
256
+ : LiteralSetValuesOf<Values> extends infer ValueSet extends string
257
+ ? [ValueSet] extends [never]
258
+ ? OrFormulas<FormulaOfInValues<Left, Values>>
259
+ : string extends ValueSet
260
+ ? CombineFacts<NonNullFactsOfExpression<Left>, UnknownTag<"in:literal-set-too-large">>
261
+ : AtomOf<LiteralSetAtom<PredicateKeyOfExpression<Left>, ValueSet>>
262
+ : OrFormulas<FormulaOfInValues<Left, Values>>
263
+
183
264
  type FormulaOfNotInValues<
184
265
  Left extends Expression.Any,
185
266
  Values extends readonly Expression.Any[],
@@ -200,7 +281,7 @@ type FormulaOfVariadic<
200
281
  ? AnyFormulaOfValues<Values>
201
282
  : Kind extends "in"
202
283
  ? Values extends readonly [infer Left extends Expression.Any, ...infer Tail extends readonly Expression.Any[]]
203
- ? OrFormulas<FormulaOfInValues<Left, Tail>>
284
+ ? FormulaOfIn<Left, Tail>
204
285
  : False
205
286
  : Kind extends "notIn"
206
287
  ? Values extends readonly [infer Left extends Expression.Any, ...infer Tail extends readonly Expression.Any[]]
@@ -218,13 +299,13 @@ type FormulaOfUnary<
218
299
  Kind extends ExpressionAst.UnaryKind,
219
300
  Inner extends Expression.Any
220
301
  > = Kind extends "isNull"
221
- ? [ColumnKeyOfExpression<Inner>] extends [never]
302
+ ? [PredicateKeyOfExpression<Inner>] extends [never]
222
303
  ? UnknownTag<"isNull:unsupported">
223
- : AtomOf<NullAtom<ColumnKeyOfExpression<Inner>>>
304
+ : AtomOf<NullAtom<PredicateKeyOfExpression<Inner>>>
224
305
  : Kind extends "isNotNull"
225
- ? [ColumnKeyOfExpression<Inner>] extends [never]
306
+ ? [PredicateKeyOfExpression<Inner>] extends [never]
226
307
  ? UnknownTag<"isNotNull:unsupported">
227
- : AtomOf<NonNullAtom<ColumnKeyOfExpression<Inner>>>
308
+ : AtomOf<NonNullAtom<PredicateKeyOfExpression<Inner>>>
228
309
  : Kind extends "not"
229
310
  ? import("./formula.js").Not<FormulaOfExpression<Inner>>
230
311
  : UnknownTag<`unary:${Kind}`>