effect-qb 0.14.0 → 0.15.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 (141) hide show
  1. package/dist/mysql.js +61270 -4259
  2. package/dist/postgres/metadata.js +176 -82
  3. package/dist/postgres.js +5903 -3316
  4. package/package.json +14 -1
  5. package/src/internal/aggregation-validation.ts +3 -3
  6. package/src/internal/case-analysis.d.ts +18 -0
  7. package/src/internal/case-analysis.ts +4 -4
  8. package/src/internal/coercion/analysis.d.ts +7 -0
  9. package/src/internal/{coercion-analysis.ts → coercion/analysis.ts} +3 -3
  10. package/src/internal/coercion/errors.d.ts +17 -0
  11. package/src/internal/{coercion-errors.ts → coercion/errors.ts} +1 -1
  12. package/src/internal/coercion/kind.d.ts +4 -0
  13. package/src/internal/{coercion-kind.ts → coercion/kind.ts} +2 -2
  14. package/src/internal/{coercion-normalize.ts → coercion/normalize.ts} +1 -1
  15. package/src/internal/coercion/rules.d.ts +6 -0
  16. package/src/internal/{coercion-rules.ts → coercion/rules.ts} +2 -2
  17. package/src/internal/column-state.d.ts +190 -0
  18. package/src/internal/column-state.ts +36 -47
  19. package/src/internal/column.ts +21 -305
  20. package/src/internal/datatypes/define.d.ts +17 -0
  21. package/src/internal/datatypes/define.ts +18 -4
  22. package/src/internal/datatypes/lookup.d.ts +44 -0
  23. package/src/internal/datatypes/lookup.ts +61 -152
  24. package/src/internal/datatypes/shape.d.ts +16 -0
  25. package/src/internal/datatypes/shape.ts +1 -1
  26. package/src/internal/derived-table.d.ts +4 -0
  27. package/src/internal/derived-table.ts +21 -16
  28. package/src/internal/dsl-mutation-runtime.ts +378 -0
  29. package/src/internal/dsl-plan-runtime.ts +387 -0
  30. package/src/internal/dsl-query-runtime.ts +160 -0
  31. package/src/internal/dsl-transaction-ddl-runtime.ts +263 -0
  32. package/src/internal/executor.ts +132 -31
  33. package/src/internal/expression-ast.ts +15 -5
  34. package/src/internal/grouping-key.d.ts +3 -0
  35. package/src/internal/grouping-key.ts +1 -1
  36. package/src/internal/implication-runtime.d.ts +15 -0
  37. package/src/internal/implication-runtime.ts +4 -4
  38. package/src/internal/json/ast.d.ts +30 -0
  39. package/src/internal/json/ast.ts +1 -1
  40. package/src/internal/json/errors.d.ts +8 -0
  41. package/src/internal/json/path.d.ts +75 -0
  42. package/src/internal/json/path.ts +1 -1
  43. package/src/internal/json/types.d.ts +62 -0
  44. package/src/internal/predicate/analysis.d.ts +20 -0
  45. package/src/internal/{predicate-analysis.ts → predicate/analysis.ts} +3 -3
  46. package/src/internal/predicate/atom.d.ts +28 -0
  47. package/src/internal/{predicate-branches.ts → predicate/branches.ts} +2 -2
  48. package/src/internal/predicate/context.d.ts +67 -0
  49. package/src/internal/{predicate-context.ts → predicate/context.ts} +8 -5
  50. package/src/internal/predicate/formula.d.ts +35 -0
  51. package/src/internal/{predicate-formula.ts → predicate/formula.ts} +1 -1
  52. package/src/internal/predicate/key.d.ts +11 -0
  53. package/src/internal/{predicate-key.ts → predicate/key.ts} +2 -2
  54. package/src/internal/{predicate-nnf.ts → predicate/nnf.ts} +2 -2
  55. package/src/internal/predicate/normalize.d.ts +53 -0
  56. package/src/internal/{predicate-normalize.ts → predicate/normalize.ts} +19 -19
  57. package/src/internal/predicate/runtime.d.ts +31 -0
  58. package/src/internal/{predicate-runtime.ts → predicate/runtime.ts} +9 -6
  59. package/src/internal/projection-alias.d.ts +13 -0
  60. package/src/internal/projections.d.ts +31 -0
  61. package/src/internal/projections.ts +1 -1
  62. package/src/internal/query-ast.d.ts +217 -0
  63. package/src/internal/query-ast.ts +1 -1
  64. package/src/internal/query-requirements.d.ts +20 -0
  65. package/src/internal/query.d.ts +775 -0
  66. package/src/internal/query.ts +389 -313
  67. package/src/internal/renderer.ts +7 -21
  68. package/src/internal/row-set.d.ts +53 -0
  69. package/src/internal/{plan.ts → row-set.ts} +11 -9
  70. package/src/internal/{runtime-normalize.ts → runtime/normalize.ts} +9 -31
  71. package/src/internal/{runtime-schema.ts → runtime/schema.ts} +13 -38
  72. package/src/internal/runtime/value.d.ts +22 -0
  73. package/src/internal/{runtime-value.ts → runtime/value.ts} +2 -2
  74. package/src/internal/scalar.d.ts +107 -0
  75. package/src/internal/scalar.ts +191 -0
  76. package/src/internal/schema-derivation.d.ts +105 -0
  77. package/src/internal/schema-expression.d.ts +18 -0
  78. package/src/internal/schema-expression.ts +38 -7
  79. package/src/internal/table-options.d.ts +94 -0
  80. package/src/internal/table-options.ts +8 -2
  81. package/src/internal/table.d.ts +173 -0
  82. package/src/internal/table.ts +32 -14
  83. package/src/mysql/column.ts +94 -18
  84. package/src/mysql/datatypes/index.ts +47 -7
  85. package/src/mysql/errors/generated.ts +57336 -0
  86. package/src/mysql/errors/index.ts +1 -0
  87. package/src/mysql/errors/normalize.ts +55 -53
  88. package/src/mysql/errors/types.ts +74 -0
  89. package/src/mysql/executor.ts +69 -7
  90. package/src/mysql/function/aggregate.ts +1 -5
  91. package/src/mysql/function/core.ts +1 -4
  92. package/src/mysql/function/index.ts +0 -1
  93. package/src/mysql/function/string.ts +1 -5
  94. package/src/mysql/function/temporal.ts +12 -15
  95. package/src/mysql/function/window.ts +1 -6
  96. package/src/{internal/mysql-dialect.ts → mysql/internal/dialect.ts} +1 -1
  97. package/src/{internal/mysql-query.ts → mysql/internal/dsl.ts} +1024 -2082
  98. package/src/{internal/mysql-renderer.ts → mysql/internal/renderer.ts} +6 -6
  99. package/src/mysql/internal/sql-expression-renderer.ts +1455 -0
  100. package/src/mysql/json.ts +2 -0
  101. package/src/mysql/query.ts +111 -91
  102. package/src/mysql/renderer.ts +1 -1
  103. package/src/mysql/table.ts +1 -1
  104. package/src/mysql.ts +6 -4
  105. package/src/postgres/cast.ts +11 -12
  106. package/src/postgres/column.ts +178 -46
  107. package/src/postgres/datatypes/index.d.ts +515 -0
  108. package/src/postgres/datatypes/index.ts +22 -13
  109. package/src/postgres/datatypes/spec.d.ts +412 -0
  110. package/src/postgres/errors/generated.ts +2636 -0
  111. package/src/postgres/errors/index.ts +1 -0
  112. package/src/postgres/errors/normalize.ts +47 -62
  113. package/src/postgres/errors/types.ts +92 -34
  114. package/src/postgres/executor.ts +37 -5
  115. package/src/postgres/function/aggregate.ts +1 -5
  116. package/src/postgres/function/core.ts +12 -6
  117. package/src/postgres/function/index.ts +0 -1
  118. package/src/postgres/function/string.ts +1 -5
  119. package/src/postgres/function/temporal.ts +12 -15
  120. package/src/postgres/function/window.ts +1 -6
  121. package/src/{internal/postgres-dialect.ts → postgres/internal/dialect.ts} +1 -1
  122. package/src/{internal/postgres-query.ts → postgres/internal/dsl.ts} +1085 -2089
  123. package/src/{internal/postgres-renderer.ts → postgres/internal/renderer.ts} +6 -6
  124. package/src/postgres/internal/schema-ddl.ts +108 -0
  125. package/src/{internal/postgres-schema-model.ts → postgres/internal/schema-model.ts} +12 -6
  126. package/src/{internal → postgres/internal}/sql-expression-renderer.ts +19 -17
  127. package/src/postgres/{function/json.ts → json.ts} +77 -85
  128. package/src/postgres/metadata.ts +2 -2
  129. package/src/postgres/query.ts +113 -89
  130. package/src/postgres/renderer.ts +3 -13
  131. package/src/postgres/schema-expression.ts +2 -1
  132. package/src/postgres/schema-management.ts +1 -1
  133. package/src/postgres/table.ts +12 -4
  134. package/src/postgres/type.ts +29 -2
  135. package/src/postgres.ts +6 -4
  136. package/src/internal/expression.ts +0 -327
  137. package/src/internal/schema-ddl.ts +0 -55
  138. package/src/mysql/function/json.ts +0 -4
  139. package/src/mysql/private/query.ts +0 -1
  140. package/src/postgres/private/query.ts +0 -1
  141. /package/src/internal/{predicate-atom.ts → predicate/atom.ts} +0 -0
@@ -1,14 +1,14 @@
1
1
  import { pipeArguments } from "effect/Pipeable"
2
2
  import * as Schema from "effect/Schema"
3
3
 
4
- import { postgresDatatypes } from "../postgres/datatypes/index.js"
5
-
6
- import * as Expression from "./expression.js"
7
- import * as Plan from "./plan.js"
8
- import * as Table from "./table.js"
9
- import type { CastTargetError, OperandCompatibilityError } from "./coercion-errors.js"
10
- import type { RuntimeOfDbType } from "./coercion-analysis.js"
11
- import type { CanCastDbType, CanCompareDbTypes, CanContainDbTypes, CanTextuallyCoerceDbType } from "./coercion-rules.js"
4
+ import { postgresDatatypes } from "../datatypes/index.js"
5
+
6
+ import * as Expression from "../../internal/scalar.js"
7
+ import * as Plan from "../../internal/row-set.js"
8
+ import * as Table from "../../internal/table.js"
9
+ import type { CastTargetError, OperandCompatibilityError } from "../../internal/coercion/errors.js"
10
+ import type { RuntimeOfDbType } from "../../internal/coercion/analysis.js"
11
+ import type { CanCastDbType, CanCompareDbTypes, CanContainDbTypes, CanTextuallyCoerceDbType } from "../../internal/coercion/rules.js"
12
12
  import {
13
13
  currentRequiredList,
14
14
  extractRequiredRuntime,
@@ -21,15 +21,12 @@ import {
21
21
  mergeAggregationRuntime,
22
22
  mergeDependencies,
23
23
  mergeManyDependencies,
24
- mergeManySources,
25
24
  mergeNullabilityManyRuntime,
26
- mergeSources,
27
25
  type AddAvailable,
28
26
  type AddAvailableMany,
29
27
  type AvailableAfterJoin,
30
28
  type AddExpressionRequired,
31
29
  type AddJoinRequired,
32
- type AggregationOf,
33
30
  type AssumptionsOfPlan,
34
31
  type AvailableOfPlan,
35
32
  type CapabilitiesOfPlan,
@@ -58,6 +55,7 @@ import {
58
55
  type PlanDialectOf,
59
56
  type PresenceWitnessKeysOfSource,
60
57
  type PredicateInput,
58
+ type KindOf,
61
59
  type QueryPlan,
62
60
  type OutputOfSelection,
63
61
  type ScalarOutputOfPlan,
@@ -80,7 +78,6 @@ import {
80
78
  type MergeCapabilities,
81
79
  type MutationTargetInput,
82
80
  type MutationValuesInput,
83
- type SourceOf,
84
81
  type SourceDialectOf,
85
82
  type SourceLike,
86
83
  type SourceNameOf,
@@ -89,6 +86,7 @@ import {
89
86
  type ValuesInput,
90
87
  type AnyValuesSource,
91
88
  type AnyUnnestSource,
89
+ type AnyTableFunctionSource,
92
90
  type UnnestSource,
93
91
  type TableFunctionSource,
94
92
  type StringExpressionInput,
@@ -98,14 +96,13 @@ import {
98
96
  type MutationTargetTuple,
99
97
  type TupleDependencies,
100
98
  type TupleDialect,
101
- type TupleSource,
102
99
  type ResultRow
103
- } from "./query.js"
104
- import * as ExpressionAst from "./expression-ast.js"
105
- import { presenceWitnessesOfSourceLike } from "./implication-runtime.js"
106
- import type { JsonNode } from "./json/ast.js"
107
- import type { JsonPathUsageError } from "./json/errors.js"
108
- import * as JsonPath from "./json/path.js"
100
+ } from "../../internal/query.js"
101
+ import * as ExpressionAst from "../../internal/expression-ast.js"
102
+ import { presenceWitnessesOfSourceLike } from "../../internal/implication-runtime.js"
103
+ import type { JsonNode } from "../../internal/json/ast.js"
104
+ import type { JsonPathUsageError } from "../../internal/json/errors.js"
105
+ import * as JsonPath from "../../internal/json/path.js"
109
106
  import type {
110
107
  JsonConcatResult,
111
108
  JsonDeleteAtPath,
@@ -119,16 +116,20 @@ import type {
119
116
  JsonTypeName,
120
117
  JsonValueAtPath,
121
118
  NormalizeJsonLiteral
122
- } from "./json/types.js"
123
- import type { AssumeTrue } from "./predicate-analysis.js"
124
- import type { FormulaOfPredicate } from "./predicate-normalize.js"
125
- import type { TrueFormula } from "./predicate-formula.js"
126
- import { assumeFormulaTrue, formulaOfExpression as formulaOfExpressionRuntime, trueFormula } from "./predicate-runtime.js"
127
- import { dedupeGroupedExpressions } from "./grouping-key.js"
128
- import { makeCteSource, makeDerivedSource, makeLateralSource } from "./derived-table.js"
129
- import * as ProjectionAlias from "./projection-alias.js"
130
- import * as QueryAst from "./query-ast.js"
131
- import { normalizeColumnList } from "./table-options.js"
119
+ } from "../../internal/json/types.js"
120
+ import type { AssumeTrue } from "../../internal/predicate/analysis.js"
121
+ import type { FormulaOfPredicate } from "../../internal/predicate/normalize.js"
122
+ import type { TrueFormula } from "../../internal/predicate/formula.js"
123
+ import { assumeFormulaTrue, formulaOfExpression as formulaOfExpressionRuntime, trueFormula } from "../../internal/predicate/runtime.js"
124
+ import { dedupeGroupedExpressions } from "../../internal/grouping-key.js"
125
+ import { makeDslMutationRuntime } from "../../internal/dsl-mutation-runtime.js"
126
+ import { makeDslPlanRuntime } from "../../internal/dsl-plan-runtime.js"
127
+ import { makeDslQueryRuntime } from "../../internal/dsl-query-runtime.js"
128
+ import { makeDslTransactionDdlRuntime } from "../../internal/dsl-transaction-ddl-runtime.js"
129
+ import { makeCteSource, makeDerivedSource, makeLateralSource } from "../../internal/derived-table.js"
130
+ import * as ProjectionAlias from "../../internal/projection-alias.js"
131
+ import * as QueryAst from "../../internal/query-ast.js"
132
+ import { normalizeColumnList } from "../../internal/table-options.js"
132
133
 
133
134
  /**
134
135
  * Dialect-specific DB type profile used to specialize the shared query
@@ -190,7 +191,7 @@ type DialectLiteralExpression<
190
191
  BoolDb extends Expression.DbType.Any,
191
192
  TimestampDb extends Expression.DbType.Any,
192
193
  NullDb extends Expression.DbType.Any
193
- > = Expression.Expression<
194
+ > = Expression.Scalar<
194
195
  DialectLiteralRuntime<Value, TimestampDb>,
195
196
  DialectLiteralDbType<Value, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
196
197
  LiteralNullability<Value>,
@@ -325,15 +326,13 @@ type CastResult<
325
326
  BoolDb extends Expression.DbType.Any,
326
327
  TimestampDb extends Expression.DbType.Any,
327
328
  NullDb extends Expression.DbType.Any
328
- > = Expression.Expression<
329
+ > = Expression.Scalar<
329
330
  RuntimeOfDbType<Target>,
330
331
  Target,
331
332
  Expression.NullabilityOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
332
333
  Dialect,
333
- AggregationOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
334
- SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
335
- DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
336
- Expression.SourceNullabilityOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
334
+ KindOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
335
+ DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
337
336
  > & {
338
337
  readonly [ExpressionAst.TypeId]: ExpressionAst.CastNode<
339
338
  DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
@@ -370,17 +369,39 @@ type ComparableGuard<
370
369
  TimestampDb extends Expression.DbType.Any,
371
370
  NullDb extends Expression.DbType.Any,
372
371
  Operator extends string
373
- > = CanCompareDbTypes<
374
- DialectDbTypeOfInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
375
- DialectDbTypeOfInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
376
- Dialect
377
- > extends true ? true : OperandCompatibilityError<
378
- Operator,
379
- DialectDbTypeOfInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
380
- DialectDbTypeOfInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
372
+ > = 0 extends (1 & Left)
373
+ ? true
374
+ : 0 extends (1 & Right)
375
+ ? true
376
+ : DialectDbTypeOfInput<
377
+ Left,
381
378
  Dialect,
382
- "the same db type family"
383
- >
379
+ TextDb,
380
+ NumericDb,
381
+ BoolDb,
382
+ TimestampDb,
383
+ NullDb
384
+ > extends infer LeftDb extends Expression.DbType.Any
385
+ ? DialectDbTypeOfInput<
386
+ Right,
387
+ Dialect,
388
+ TextDb,
389
+ NumericDb,
390
+ BoolDb,
391
+ TimestampDb,
392
+ NullDb
393
+ > extends infer RightDb extends Expression.DbType.Any
394
+ ? CanCompareDbTypes<LeftDb, RightDb, Dialect> extends true
395
+ ? true
396
+ : OperandCompatibilityError<
397
+ Operator,
398
+ LeftDb,
399
+ RightDb,
400
+ Dialect,
401
+ "the same db type family"
402
+ >
403
+ : never
404
+ : never
384
405
 
385
406
  type TextGuard<
386
407
  Value extends ExpressionInput,
@@ -465,9 +486,10 @@ type ComparableArgs<
465
486
  TimestampDb extends Expression.DbType.Any,
466
487
  NullDb extends Expression.DbType.Any,
467
488
  Operator extends string
468
- > = ComparableGuard<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator> extends true
469
- ? readonly [left: Left, right: Right]
470
- : readonly [ComparableGuard<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator>]
489
+ > = readonly [
490
+ left: Left,
491
+ right: Right & ComparableInput<NoInfer<Left>, NoInfer<Right>, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator>
492
+ ]
471
493
 
472
494
  type ContainmentGuard<
473
495
  Left extends ExpressionInput,
@@ -515,11 +537,10 @@ type TextArgs<
515
537
  TimestampDb extends Expression.DbType.Any,
516
538
  NullDb extends Expression.DbType.Any,
517
539
  Operator extends string
518
- > = TextGuard<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator> extends true
519
- ? TextGuard<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator> extends true
520
- ? readonly [left: Left, right: Right]
521
- : readonly [TextGuard<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator>]
522
- : readonly [TextGuard<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator>]
540
+ > = readonly [
541
+ left: Left & TextInput<NoInfer<Left>, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator>,
542
+ right: Right & TextInput<NoInfer<Right>, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, Operator>
543
+ ]
523
544
 
524
545
  type MembershipArgs<
525
546
  Values extends readonly [ExpressionInput, ExpressionInput, ...ExpressionInput[]],
@@ -590,17 +611,6 @@ type BetweenArgs<
590
611
  "between"
591
612
  >]
592
613
 
593
- /** Provenance carried by a dialect-specialized scalar input. */
594
- type SourceOfDialectInput<
595
- Value extends ExpressionInput,
596
- Dialect extends string,
597
- TextDb extends Expression.DbType.Any,
598
- NumericDb extends Expression.DbType.Any,
599
- BoolDb extends Expression.DbType.Any,
600
- TimestampDb extends Expression.DbType.Any,
601
- NullDb extends Expression.DbType.Any
602
- > = SourceOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
603
-
604
614
  /** Dialect carried by a dialect-specialized scalar input. */
605
615
  type DialectOfDialectInput<
606
616
  Value extends ExpressionInput,
@@ -634,17 +644,6 @@ type RequiredFromDialectInput<
634
644
  NullDb extends Expression.DbType.Any
635
645
  > = RequiredFromDependencies<DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
636
646
 
637
- /** Provenance carried by a dialect-specialized string input. */
638
- type SourceOfDialectStringInput<
639
- Value extends ExpressionInput,
640
- Dialect extends string,
641
- TextDb extends Expression.DbType.Any,
642
- NumericDb extends Expression.DbType.Any,
643
- BoolDb extends Expression.DbType.Any,
644
- TimestampDb extends Expression.DbType.Any,
645
- NullDb extends Expression.DbType.Any
646
- > = SourceOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
647
-
648
647
  /** Dialect carried by a dialect-specialized string input. */
649
648
  type DialectOfDialectStringInput<
650
649
  Value extends ExpressionInput,
@@ -714,9 +713,9 @@ type RequiredFromDialectNumericInput<
714
713
  /** Folds aggregation kinds across a tuple of expressions. */
715
714
  type MergeAggregationTuple<
716
715
  Values extends readonly Expression.Any[],
717
- Current extends Expression.AggregationKind = "scalar"
716
+ Current extends Expression.ScalarKind = "scalar"
718
717
  > = Values extends readonly [infer Head extends Expression.Any, ...infer Tail extends readonly Expression.Any[]]
719
- ? MergeAggregationTuple<Tail, MergeAggregation<Current, AggregationOf<Head>>>
718
+ ? MergeAggregationTuple<Tail, MergeAggregation<Current, KindOf<Head>>>
720
719
  : Current
721
720
 
722
721
  /** Result nullability for binary `coalesce(...)`. */
@@ -836,20 +835,16 @@ type AstBackedExpression<
836
835
  Db extends Expression.DbType.Any,
837
836
  Nullable extends Expression.Nullability,
838
837
  Dialect extends string,
839
- Aggregation extends Expression.AggregationKind,
840
- Source,
841
- Dependencies extends Expression.SourceDependencies,
842
- Ast extends ExpressionAst.Any,
843
- SourceNullability extends Expression.SourceNullabilityMode = "propagate"
844
- > = Expression.Expression<
838
+ Aggregation extends Expression.ScalarKind,
839
+ Dependencies extends Expression.BindingId,
840
+ Ast extends ExpressionAst.Any
841
+ > = Expression.Scalar<
845
842
  Runtime,
846
843
  Db,
847
844
  Nullable,
848
845
  Dialect,
849
846
  Aggregation,
850
- Source,
851
- Dependencies,
852
- SourceNullability
847
+ Dependencies
853
848
  > & {
854
849
  readonly [ExpressionAst.TypeId]: Ast
855
850
  }
@@ -882,8 +877,7 @@ type VariadicBooleanExpression<
882
877
  BoolDb,
883
878
  MergeNullabilityTuple<Values>,
884
879
  TupleDialect<Values>,
885
- MergeAggregationTuple<Values>,
886
- TupleSource<Values>,
880
+ "scalar",
887
881
  TupleDependencies<Values>,
888
882
  ExpressionAst.VariadicNode<Kind, Values>
889
883
  > & {
@@ -915,32 +909,26 @@ type JsonExpression<
915
909
  Db extends Expression.DbType.Any,
916
910
  Nullability extends Expression.Nullability,
917
911
  Dialect extends string,
918
- Aggregation extends Expression.AggregationKind,
919
- Source,
920
- Dependencies extends Expression.SourceDependencies,
921
- Ast extends ExpressionAst.Any,
922
- SourceNullability extends Expression.SourceNullabilityMode = "propagate"
912
+ Aggregation extends Expression.ScalarKind,
913
+ Dependencies extends Expression.BindingId,
914
+ Ast extends ExpressionAst.Any
923
915
  > = AstBackedExpression<
924
916
  Runtime,
925
917
  Db,
926
918
  Nullability,
927
919
  Dialect,
928
920
  Aggregation,
929
- Source,
930
921
  Dependencies,
931
- Ast,
932
- SourceNullability
922
+ Ast
933
923
  >
934
924
 
935
- type JsonExpressionLike<Runtime = unknown> = Expression.Expression<
925
+ type JsonExpressionLike<Runtime = unknown> = Expression.Scalar<
936
926
  Runtime,
937
927
  Expression.DbType.Json<any, any>,
938
928
  Expression.Nullability,
939
929
  string,
940
- Expression.AggregationKind,
941
- any,
942
- Expression.SourceDependencies,
943
- Expression.SourceNullabilityMode
930
+ Expression.ScalarKind,
931
+ Expression.BindingId
944
932
  >
945
933
 
946
934
  type JsonDbOfExpression<Value extends JsonExpressionLike<any>> = Expression.DbTypeOf<Value>
@@ -1100,8 +1088,8 @@ type CaseResultTupleWithElse<
1100
1088
  > = [...CaseResultTuple<Branches>, Else]
1101
1089
 
1102
1090
  type MergeAggregationUnion<Value extends Expression.Any> =
1103
- Extract<AggregationOf<Value>, "window"> extends never
1104
- ? Extract<AggregationOf<Value>, "aggregate"> extends never ? "scalar" : "aggregate"
1091
+ Extract<KindOf<Value>, "window"> extends never
1092
+ ? Extract<KindOf<Value>, "aggregate"> extends never ? "scalar" : "aggregate"
1105
1093
  : "window"
1106
1094
 
1107
1095
  type CaseNullabilityOfUnion<Value extends Expression.Any> =
@@ -1149,10 +1137,8 @@ type CaseBuilder<
1149
1137
  CaseNullabilityOfUnion<CaseResultTupleWithElse<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>[number]>,
1150
1138
  TupleDialect<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1151
1139
  MergeAggregationTuple<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1152
- TupleSource<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1153
1140
  TupleDependencies<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1154
- ExpressionAst.CaseNode<CaseAstBranches<Branches>, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1155
- "resolved"
1141
+ ExpressionAst.CaseNode<CaseAstBranches<Branches>, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1156
1142
  >
1157
1143
  }
1158
1144
 
@@ -1167,7 +1153,7 @@ type MatchBuilder<
1167
1153
  NullDb extends Expression.DbType.Any
1168
1154
  > = {
1169
1155
  when<Compare extends ExpressionInput, Then extends ExpressionInput>(
1170
- compare: Compare & ComparableInput<Subject, Compare, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "match">,
1156
+ compare: Compare & ComparableInput<NoInfer<Subject>, NoInfer<Compare>, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "match">,
1171
1157
  result: Then
1172
1158
  ): MatchBuilder<
1173
1159
  Subject,
@@ -1190,10 +1176,8 @@ type MatchBuilder<
1190
1176
  CaseNullabilityOfUnion<CaseResultTupleWithElse<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>[number]>,
1191
1177
  TupleDialect<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1192
1178
  MergeAggregationTuple<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1193
- TupleSource<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1194
1179
  TupleDependencies<CaseAllTuple<Branches, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1195
- ExpressionAst.CaseNode<CaseAstBranches<Branches>, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1196
- "resolved"
1180
+ ExpressionAst.CaseNode<CaseAstBranches<Branches>, DialectAsExpression<Else, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1197
1181
  >
1198
1182
  }
1199
1183
 
@@ -1232,7 +1216,7 @@ type MatchStarter<
1232
1216
  NullDb extends Expression.DbType.Any
1233
1217
  > = {
1234
1218
  when<Compare extends ExpressionInput, Then extends ExpressionInput>(
1235
- compare: Compare & ComparableInput<Subject, Compare, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "match">,
1219
+ compare: Compare & ComparableInput<NoInfer<Subject>, NoInfer<Compare>, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "match">,
1236
1220
  result: Then
1237
1221
  ): MatchBuilder<
1238
1222
  Subject,
@@ -1249,15 +1233,13 @@ type MatchStarter<
1249
1233
  >
1250
1234
  }
1251
1235
 
1252
- type WindowPartitionInput = Expression.Expression<
1236
+ type WindowPartitionInput = Expression.Scalar<
1253
1237
  any,
1254
1238
  Expression.DbType.Any,
1255
1239
  Expression.Nullability,
1256
1240
  string,
1257
1241
  "scalar",
1258
- any,
1259
- Expression.SourceDependencies,
1260
- Expression.SourceNullabilityMode
1242
+ Expression.BindingId
1261
1243
  >
1262
1244
 
1263
1245
  type WindowOrderInput = WindowPartitionInput
@@ -1316,12 +1298,6 @@ type WindowDialectOf<
1316
1298
  OrderBy extends readonly WindowOrderTermInput[]
1317
1299
  > = DialectOf<Value> | TupleDialect<PartitionBy> | TupleDialect<WindowOrderExpressionTuple<OrderBy>>
1318
1300
 
1319
- type WindowSourceOf<
1320
- Value extends Expression.Any,
1321
- PartitionBy extends readonly WindowPartitionInput[],
1322
- OrderBy extends readonly WindowOrderTermInput[]
1323
- > = SourceOf<Value> | TupleSource<PartitionBy> | TupleSource<WindowOrderExpressionTuple<OrderBy>>
1324
-
1325
1301
  type WindowDependenciesOf<
1326
1302
  Value extends Expression.Any,
1327
1303
  PartitionBy extends readonly WindowPartitionInput[],
@@ -1337,11 +1313,6 @@ type NumberWindowDialectOf<
1337
1313
  OrderBy extends readonly WindowOrderTermInput[]
1338
1314
  > = TupleDialect<PartitionBy> | TupleDialect<WindowOrderExpressionTuple<OrderBy>>
1339
1315
 
1340
- type NumberWindowSourceOf<
1341
- PartitionBy extends readonly WindowPartitionInput[],
1342
- OrderBy extends readonly WindowOrderTermInput[]
1343
- > = TupleSource<PartitionBy> | TupleSource<WindowOrderExpressionTuple<OrderBy>>
1344
-
1345
1316
  type NumberWindowDependenciesOf<
1346
1317
  PartitionBy extends readonly WindowPartitionInput[],
1347
1318
  OrderBy extends readonly WindowOrderTermInput[]
@@ -1360,10 +1331,8 @@ type WindowedExpression<
1360
1331
  Expression.NullabilityOf<Value>,
1361
1332
  WindowDialectOf<Value, PartitionBy, OrderBy>,
1362
1333
  "window",
1363
- WindowSourceOf<Value, PartitionBy, OrderBy>,
1364
1334
  WindowDependenciesOf<Value, PartitionBy, OrderBy>,
1365
- WindowNodeOf<"over", Value, PartitionBy, OrderBy>,
1366
- "resolved"
1335
+ WindowNodeOf<"over", Value, PartitionBy, OrderBy>
1367
1336
  >
1368
1337
 
1369
1338
  type NumberWindowExpression<
@@ -1376,10 +1345,8 @@ type NumberWindowExpression<
1376
1345
  "never",
1377
1346
  NumberWindowDialectOf<PartitionBy, OrderBy>,
1378
1347
  "window",
1379
- NumberWindowSourceOf<PartitionBy, OrderBy>,
1380
1348
  NumberWindowDependenciesOf<PartitionBy, OrderBy>,
1381
- WindowNodeOf<Kind, undefined, PartitionBy, OrderBy>,
1382
- "resolved"
1349
+ WindowNodeOf<Kind, undefined, PartitionBy, OrderBy>
1383
1350
  >
1384
1351
 
1385
1352
  /**
@@ -1389,22 +1356,33 @@ type NumberWindowExpression<
1389
1356
  * that manufacture new expressions are specialized to the supplied dialect DB
1390
1357
  * types instead of relying on the Postgres-default root module.
1391
1358
  */
1392
- export const postgresQuery = (() => {
1393
1359
  type Dialect = "postgres"
1394
- type TextDb = Expression.DbType.PgText
1395
- type NumericDb = Expression.DbType.PgFloat8
1396
- type BoolDb = Expression.DbType.PgBool
1397
- type TimestampDb = Expression.DbType.PgTimestamp
1398
- type NullDb = Expression.DbType.Base<"postgres", "null">
1360
+ type TextDb = ReturnType<typeof postgresDatatypes.text>
1361
+ type NumericDb = ReturnType<typeof postgresDatatypes.float8>
1362
+ type BoolDb = ReturnType<typeof postgresDatatypes.boolean>
1363
+ type TimestampDb = ReturnType<typeof postgresDatatypes.timestamp>
1364
+ type NullDb = Expression.DbType.Base<"postgres", "null"> & {
1365
+ readonly family: "null"
1366
+ readonly runtime: "unknown"
1367
+ readonly compareGroup: "null"
1368
+ readonly traits: {}
1369
+ }
1399
1370
  type TypeWitnesses = typeof postgresDatatypes
1400
1371
 
1401
1372
  const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, TypeWitnesses> = {
1402
1373
  dialect: "postgres",
1403
- textDb: { dialect: "postgres", kind: "text" } as TextDb,
1404
- numericDb: { dialect: "postgres", kind: "float8" } as NumericDb,
1405
- boolDb: { dialect: "postgres", kind: "bool" } as BoolDb,
1406
- timestampDb: { dialect: "postgres", kind: "timestamp" } as TimestampDb,
1407
- nullDb: { dialect: "postgres", kind: "null" } as NullDb,
1374
+ textDb: postgresDatatypes.text() as TextDb,
1375
+ numericDb: postgresDatatypes.float8() as NumericDb,
1376
+ boolDb: postgresDatatypes.boolean() as BoolDb,
1377
+ timestampDb: postgresDatatypes.timestamp() as TimestampDb,
1378
+ nullDb: {
1379
+ dialect: "postgres",
1380
+ kind: "null",
1381
+ family: "null",
1382
+ runtime: "unknown",
1383
+ compareGroup: "null",
1384
+ traits: {}
1385
+ } as NullDb,
1408
1386
  type: postgresDatatypes
1409
1387
  }
1410
1388
  const ValuesInputProto = {
@@ -1437,9 +1415,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1437
1415
  runtimeSchema: literalSchemaOf(value),
1438
1416
  nullability: (value === null ? "always" : "never") as LiteralNullability<Value>,
1439
1417
  dialect: profile.dialect as Dialect,
1440
- aggregation: "scalar",
1441
- source: undefined as never,
1442
- sourceNullability: "propagate" as const,
1418
+ kind: "scalar",
1419
+
1443
1420
  dependencies: {}
1444
1421
  }, {
1445
1422
  kind: "literal",
@@ -1453,15 +1430,13 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1453
1430
  name: Name,
1454
1431
  dbType: Db,
1455
1432
  nullable = false
1456
- ): Expression.Expression<
1433
+ ): Expression.Scalar<
1457
1434
  Expression.RuntimeOfDbType<Db> | null,
1458
1435
  Db,
1459
1436
  Expression.Nullability,
1460
1437
  Dialect,
1461
1438
  "scalar",
1462
- never,
1463
- {},
1464
- "resolved"
1439
+ never
1465
1440
  > & {
1466
1441
  readonly [ExpressionAst.TypeId]: ExpressionAst.ColumnNode<"", Name>
1467
1442
  } =>
@@ -1470,23 +1445,20 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1470
1445
  dbType,
1471
1446
  nullability: (nullable ? "maybe" : "never") as typeof nullable extends true ? "maybe" : "never",
1472
1447
  dialect: profile.dialect as Dialect,
1473
- aggregation: "scalar",
1474
- source: undefined as never,
1475
- sourceNullability: "resolved" as const,
1448
+ kind: "scalar",
1449
+
1476
1450
  dependencies: {}
1477
1451
  }, {
1478
1452
  kind: "column",
1479
1453
  tableName: "",
1480
1454
  columnName: name
1481
- }) as Expression.Expression<
1455
+ }) as Expression.Scalar<
1482
1456
  Expression.RuntimeOfDbType<Db> | null,
1483
1457
  Db,
1484
1458
  Expression.Nullability,
1485
1459
  Dialect,
1486
1460
  "scalar",
1487
- never,
1488
- {},
1489
- "resolved"
1461
+ never
1490
1462
  > & {
1491
1463
  readonly [ExpressionAst.TypeId]: ExpressionAst.ColumnNode<"", Name>
1492
1464
  }
@@ -1554,10 +1526,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1554
1526
  dbType: profile.boolDb as BoolDb,
1555
1527
  nullability: mergeNullabilityManyRuntime(expressions) as MergeNullabilityTuple<Values>,
1556
1528
  dialect: (expressions.find((value) => value[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as TupleDialect<Values>,
1557
- aggregation: mergeAggregationManyRuntime(expressions) as MergeAggregationTuple<Values>,
1558
- source: mergeManySources(expressions) as TupleSource<Values>,
1559
- sourceNullability: "propagate" as const,
1560
- dependencies: mergeManyDependencies(expressions) as TupleDependencies<Values>
1529
+ kind: "scalar",
1530
+
1531
+ dependencies: mergeManyDependencies(expressions)
1561
1532
  }, {
1562
1533
  kind,
1563
1534
  values: expressions
@@ -1624,39 +1595,32 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1624
1595
  return Object.keys(expression[Expression.TypeId].dependencies)
1625
1596
  }
1626
1597
 
1627
- type BinaryPredicateExpression<
1628
- Left extends ExpressionInput,
1629
- Right extends ExpressionInput,
1630
- Kind extends ExpressionAst.BinaryKind,
1631
- Nullability extends Expression.Nullability = "maybe",
1632
- SourceNullability extends Expression.SourceNullabilityMode = "propagate"
1633
- > = AstBackedExpression<
1634
- boolean,
1635
- BoolDb,
1636
- Nullability,
1637
- DialectOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | DialectOfDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1638
- MergeAggregation<
1639
- AggregationOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1640
- AggregationOf<DialectAsExpression<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1641
- >,
1642
- SourceOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | SourceOfDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1643
- DependencyRecord<
1644
- RequiredFromDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> |
1645
- RequiredFromDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
1646
- >,
1647
- ExpressionAst.BinaryNode<
1648
- Kind,
1649
- DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1650
- DialectAsExpression<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
1651
- >,
1652
- SourceNullability
1598
+ type BinaryPredicateExpression<
1599
+ Left extends ExpressionInput,
1600
+ Right extends ExpressionInput,
1601
+ Kind extends ExpressionAst.BinaryKind,
1602
+ Nullability extends Expression.Nullability = "maybe"
1603
+ > = AstBackedExpression<
1604
+ boolean,
1605
+ BoolDb,
1606
+ Nullability,
1607
+ DialectOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | DialectOfDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1608
+ "scalar",
1609
+ DependencyRecord<
1610
+ RequiredFromDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> |
1611
+ RequiredFromDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
1612
+ >,
1613
+ ExpressionAst.BinaryNode<
1614
+ Kind,
1615
+ DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1616
+ DialectAsExpression<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
1653
1617
  >
1618
+ >
1654
1619
 
1655
1620
  type SubqueryPredicateExpression<
1656
1621
  Dialect extends string,
1657
- Aggregation extends Expression.AggregationKind,
1658
- Source,
1659
- Dependencies extends Expression.SourceDependencies,
1622
+ Aggregation extends Expression.ScalarKind,
1623
+ Dependencies extends Expression.BindingId,
1660
1624
  Ast extends ExpressionAst.Any
1661
1625
  > = AstBackedExpression<
1662
1626
  boolean,
@@ -1664,7 +1628,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1664
1628
  "maybe",
1665
1629
  Dialect,
1666
1630
  Aggregation,
1667
- Source,
1668
1631
  Dependencies,
1669
1632
  Ast
1670
1633
  >
@@ -1672,29 +1635,27 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1672
1635
  type VariadicPredicateExpression<
1673
1636
  Values extends readonly ExpressionInput[],
1674
1637
  Kind extends ExpressionAst.VariadicKind
1675
- > = AstBackedExpression<
1676
- boolean,
1677
- BoolDb,
1678
- "maybe",
1679
- TupleDialect<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1680
- MergeAggregationTuple<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1681
- TupleSource<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1682
- TupleDependencies<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1683
- ExpressionAst.VariadicNode<Kind, DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1684
- >
1638
+ > = AstBackedExpression<
1639
+ boolean,
1640
+ BoolDb,
1641
+ "maybe",
1642
+ TupleDialect<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1643
+ "scalar",
1644
+ TupleDependencies<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1645
+ ExpressionAst.VariadicNode<Kind, DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1646
+ >
1685
1647
 
1686
1648
  const buildBinaryPredicate = <
1687
1649
  Left extends ExpressionInput,
1688
1650
  Right extends ExpressionInput,
1689
1651
  Kind extends ExpressionAst.BinaryKind,
1690
1652
  Nullability extends Expression.Nullability = "maybe",
1691
- SourceNullability extends Expression.SourceNullabilityMode = "propagate"
1653
+ SourceNullability extends string = "propagate"
1692
1654
  >(
1693
1655
  left: Left,
1694
1656
  right: Right,
1695
1657
  kind: Kind,
1696
1658
  nullability: Nullability = "maybe" as Nullability,
1697
- sourceNullability: SourceNullability = "propagate" as SourceNullability
1698
1659
  ): any => {
1699
1660
  const leftExpression = toDialectExpression(left)
1700
1661
  const rightExpression = toDialectExpression(right)
@@ -1703,16 +1664,11 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1703
1664
  dbType: profile.boolDb as BoolDb,
1704
1665
  nullability,
1705
1666
  dialect: (leftExpression[Expression.TypeId].dialect ?? rightExpression[Expression.TypeId].dialect) as DialectOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | DialectOfDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1706
- aggregation: mergeAggregationRuntime(
1707
- leftExpression[Expression.TypeId].aggregation,
1708
- rightExpression[Expression.TypeId].aggregation
1709
- ) as MergeAggregation<AggregationOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>, AggregationOf<DialectAsExpression<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>>,
1710
- source: mergeSources(leftExpression[Expression.TypeId].source, rightExpression[Expression.TypeId].source) as SourceOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | SourceOfDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1711
- sourceNullability,
1667
+ kind: "scalar",
1712
1668
  dependencies: mergeDependencies(
1713
1669
  leftExpression[Expression.TypeId].dependencies,
1714
1670
  rightExpression[Expression.TypeId].dependencies
1715
- ) as DependencyRecord<RequiredFromDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | RequiredFromDialectInput<Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1671
+ )
1716
1672
  } as any, {
1717
1673
  kind,
1718
1674
  left: leftExpression,
@@ -1730,9 +1686,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1730
1686
  dbType: profile.boolDb as BoolDb,
1731
1687
  nullability: "maybe",
1732
1688
  dialect: (expressions.find((value) => value[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as Dialect,
1733
- aggregation: mergeAggregationManyRuntime(expressions) as Expression.AggregationKind,
1734
- source: mergeManySources(expressions),
1735
- sourceNullability: "propagate" as const,
1689
+ kind: "scalar",
1690
+
1736
1691
  dependencies: mergeManyDependencies(expressions)
1737
1692
  }, {
1738
1693
  kind,
@@ -1746,7 +1701,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1746
1701
  >(
1747
1702
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "eq">
1748
1703
  ): BinaryPredicateExpression<Left, Right, "eq"> => {
1749
- const [left, right] = args as [Left, Right]
1704
+ const [left, right] = args as unknown as [Left, Right]
1750
1705
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "eq")
1751
1706
  }
1752
1707
 
@@ -1756,7 +1711,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1756
1711
  >(
1757
1712
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "neq">
1758
1713
  ): BinaryPredicateExpression<Left, Right, "neq"> => {
1759
- const [left, right] = args as [Left, Right]
1714
+ const [left, right] = args as unknown as [Left, Right]
1760
1715
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "neq")
1761
1716
  }
1762
1717
 
@@ -1766,7 +1721,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1766
1721
  >(
1767
1722
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "lt">
1768
1723
  ): BinaryPredicateExpression<Left, Right, "lt"> => {
1769
- const [left, right] = args as [Left, Right]
1724
+ const [left, right] = args as unknown as [Left, Right]
1770
1725
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "lt")
1771
1726
  }
1772
1727
 
@@ -1776,7 +1731,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1776
1731
  >(
1777
1732
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "lte">
1778
1733
  ): BinaryPredicateExpression<Left, Right, "lte"> => {
1779
- const [left, right] = args as [Left, Right]
1734
+ const [left, right] = args as unknown as [Left, Right]
1780
1735
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "lte")
1781
1736
  }
1782
1737
 
@@ -1786,7 +1741,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1786
1741
  >(
1787
1742
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "gt">
1788
1743
  ): BinaryPredicateExpression<Left, Right, "gt"> => {
1789
- const [left, right] = args as [Left, Right]
1744
+ const [left, right] = args as unknown as [Left, Right]
1790
1745
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "gt")
1791
1746
  }
1792
1747
 
@@ -1796,7 +1751,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1796
1751
  >(
1797
1752
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "gte">
1798
1753
  ): BinaryPredicateExpression<Left, Right, "gte"> => {
1799
- const [left, right] = args as [Left, Right]
1754
+ const [left, right] = args as unknown as [Left, Right]
1800
1755
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "gte")
1801
1756
  }
1802
1757
 
@@ -1806,7 +1761,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1806
1761
  >(
1807
1762
  ...args: TextArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "like">
1808
1763
  ): BinaryPredicateExpression<Left, Right, "like"> => {
1809
- const [left, right] = args as [Left, Right]
1764
+ const [left, right] = args as unknown as [Left, Right]
1810
1765
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "like")
1811
1766
  }
1812
1767
 
@@ -1816,7 +1771,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1816
1771
  >(
1817
1772
  ...args: TextArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "ilike">
1818
1773
  ): BinaryPredicateExpression<Left, Right, "ilike"> => {
1819
- const [left, right] = args as [Left, Right]
1774
+ const [left, right] = args as unknown as [Left, Right]
1820
1775
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "ilike")
1821
1776
  }
1822
1777
 
@@ -1826,7 +1781,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1826
1781
  >(
1827
1782
  ...args: TextArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "regexMatch">
1828
1783
  ): BinaryPredicateExpression<Left, Right, "regexMatch"> => {
1829
- const [left, right] = args as [Left, Right]
1784
+ const [left, right] = args as unknown as [Left, Right]
1830
1785
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "regexMatch")
1831
1786
  }
1832
1787
 
@@ -1836,7 +1791,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1836
1791
  >(
1837
1792
  ...args: TextArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "regexIMatch">
1838
1793
  ): BinaryPredicateExpression<Left, Right, "regexIMatch"> => {
1839
- const [left, right] = args as [Left, Right]
1794
+ const [left, right] = args as unknown as [Left, Right]
1840
1795
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "regexIMatch")
1841
1796
  }
1842
1797
 
@@ -1846,7 +1801,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1846
1801
  >(
1847
1802
  ...args: TextArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "regexNotMatch">
1848
1803
  ): BinaryPredicateExpression<Left, Right, "regexNotMatch"> => {
1849
- const [left, right] = args as [Left, Right]
1804
+ const [left, right] = args as unknown as [Left, Right]
1850
1805
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "regexNotMatch")
1851
1806
  }
1852
1807
 
@@ -1856,7 +1811,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1856
1811
  >(
1857
1812
  ...args: TextArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "regexNotIMatch">
1858
1813
  ): BinaryPredicateExpression<Left, Right, "regexNotIMatch"> => {
1859
- const [left, right] = args as [Left, Right]
1814
+ const [left, right] = args as unknown as [Left, Right]
1860
1815
  return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "regexNotIMatch")
1861
1816
  }
1862
1817
 
@@ -1865,9 +1820,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1865
1820
  Right extends ExpressionInput
1866
1821
  >(
1867
1822
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "isDistinctFrom">
1868
- ): BinaryPredicateExpression<Left, Right, "isDistinctFrom", "never", "resolved"> => {
1869
- const [left, right] = args as [Left, Right]
1870
- return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "isDistinctFrom", "never", "resolved")
1823
+ ): BinaryPredicateExpression<Left, Right, "isDistinctFrom", "never"> => {
1824
+ const [left, right] = args as unknown as [Left, Right]
1825
+ return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "isDistinctFrom", "never")
1871
1826
  }
1872
1827
 
1873
1828
  const isNotDistinctFrom = <
@@ -1875,9 +1830,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1875
1830
  Right extends ExpressionInput
1876
1831
  >(
1877
1832
  ...args: ComparableArgs<Left, Right, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "isNotDistinctFrom">
1878
- ): BinaryPredicateExpression<Left, Right, "isNotDistinctFrom", "never", "resolved"> => {
1879
- const [left, right] = args as [Left, Right]
1880
- return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "isNotDistinctFrom", "never", "resolved")
1833
+ ): BinaryPredicateExpression<Left, Right, "isNotDistinctFrom", "never"> => {
1834
+ const [left, right] = args as unknown as [Left, Right]
1835
+ return buildBinaryPredicate(left as ExpressionInput, right as ExpressionInput, "isNotDistinctFrom", "never")
1881
1836
  }
1882
1837
 
1883
1838
  const isNull = <Value extends ExpressionInput>(
@@ -1887,11 +1842,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1887
1842
  BoolDb,
1888
1843
  "never",
1889
1844
  DialectOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1890
- AggregationOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1891
- SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1845
+ "scalar",
1892
1846
  DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1893
- ExpressionAst.UnaryNode<"isNull", DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1894
- "resolved"
1847
+ ExpressionAst.UnaryNode<"isNull", DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1895
1848
  > => {
1896
1849
  const expression = toDialectExpression(value)
1897
1850
  return makeExpression({
@@ -1899,10 +1852,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1899
1852
  dbType: profile.boolDb as BoolDb,
1900
1853
  nullability: "never",
1901
1854
  dialect: expression[Expression.TypeId].dialect,
1902
- aggregation: expression[Expression.TypeId].aggregation as AggregationOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1903
- source: expression[Expression.TypeId].source as SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1904
- sourceNullability: "resolved" as const,
1905
- dependencies: expression[Expression.TypeId].dependencies as DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
1855
+ kind: "scalar",
1856
+
1857
+ dependencies: expression[Expression.TypeId].dependencies
1906
1858
  }, {
1907
1859
  kind: "isNull",
1908
1860
  value: expression
@@ -1916,11 +1868,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1916
1868
  BoolDb,
1917
1869
  "never",
1918
1870
  DialectOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1919
- AggregationOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1920
- SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1871
+ "scalar",
1921
1872
  DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1922
- ExpressionAst.UnaryNode<"isNotNull", DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1923
- "resolved"
1873
+ ExpressionAst.UnaryNode<"isNotNull", DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1924
1874
  > => {
1925
1875
  const expression = toDialectExpression(value)
1926
1876
  return makeExpression({
@@ -1928,10 +1878,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1928
1878
  dbType: profile.boolDb as BoolDb,
1929
1879
  nullability: "never",
1930
1880
  dialect: expression[Expression.TypeId].dialect,
1931
- aggregation: expression[Expression.TypeId].aggregation as AggregationOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1932
- source: expression[Expression.TypeId].source as SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1933
- sourceNullability: "resolved" as const,
1934
- dependencies: expression[Expression.TypeId].dependencies as DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
1881
+ kind: "scalar",
1882
+
1883
+ dependencies: expression[Expression.TypeId].dependencies
1935
1884
  }, {
1936
1885
  kind: "isNotNull",
1937
1886
  value: expression
@@ -1945,8 +1894,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1945
1894
  TextDb,
1946
1895
  NullabilityOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1947
1896
  DialectOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1948
- AggregationOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1949
- SourceOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1897
+ KindOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1950
1898
  DependenciesOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1951
1899
  ExpressionAst.UnaryNode<"upper", DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1952
1900
  > => {
@@ -1956,10 +1904,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1956
1904
  dbType: profile.textDb as TextDb,
1957
1905
  nullability: expression[Expression.TypeId].nullability as NullabilityOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1958
1906
  dialect: expression[Expression.TypeId].dialect,
1959
- aggregation: expression[Expression.TypeId].aggregation as AggregationOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1960
- source: expression[Expression.TypeId].source as SourceOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1961
- sourceNullability: "propagate" as const,
1962
- dependencies: expression[Expression.TypeId].dependencies as DependenciesOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
1907
+ kind: expression[Expression.TypeId].kind as KindOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1908
+
1909
+ dependencies: expression[Expression.TypeId].dependencies
1963
1910
  }, {
1964
1911
  kind: "upper",
1965
1912
  value: expression
@@ -1973,8 +1920,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1973
1920
  TextDb,
1974
1921
  NullabilityOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1975
1922
  DialectOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1976
- AggregationOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1977
- SourceOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1923
+ KindOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1978
1924
  DependenciesOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1979
1925
  ExpressionAst.UnaryNode<"lower", DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1980
1926
  > => {
@@ -1984,16 +1930,52 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
1984
1930
  dbType: profile.textDb as TextDb,
1985
1931
  nullability: expression[Expression.TypeId].nullability as NullabilityOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1986
1932
  dialect: expression[Expression.TypeId].dialect,
1987
- aggregation: expression[Expression.TypeId].aggregation as AggregationOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1988
- source: expression[Expression.TypeId].source as SourceOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1989
- sourceNullability: "propagate" as const,
1990
- dependencies: expression[Expression.TypeId].dependencies as DependenciesOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
1933
+ kind: expression[Expression.TypeId].kind as KindOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1934
+
1935
+ dependencies: expression[Expression.TypeId].dependencies
1991
1936
  }, {
1992
1937
  kind: "lower",
1993
1938
  value: expression
1994
1939
  })
1995
1940
  }
1996
1941
 
1942
+ const collate = <Value extends ExpressionInput>(
1943
+ value: Value & TextInput<NoInfer<Value>, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb, "collate">,
1944
+ collation: string | readonly [string, ...string[]]
1945
+ ): AstBackedExpression<
1946
+ Expression.RuntimeOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1947
+ Expression.DbTypeOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1948
+ NullabilityOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1949
+ DialectOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1950
+ KindOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1951
+ DependenciesOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1952
+ ExpressionAst.CollateNode<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1953
+ > => {
1954
+ const expression = toDialectStringExpression(value as any)
1955
+ const normalizedCollation: readonly [string, ...string[]] = typeof collation === "string" ? [collation] : collation
1956
+ return makeExpression({
1957
+ runtime: expression[Expression.TypeId].runtime as Expression.RuntimeOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1958
+ dbType: expression[Expression.TypeId].dbType as Expression.DbTypeOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1959
+ nullability: expression[Expression.TypeId].nullability as NullabilityOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1960
+ dialect: expression[Expression.TypeId].dialect as DialectOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1961
+ kind: expression[Expression.TypeId].kind as KindOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1962
+
1963
+ dependencies: expression[Expression.TypeId].dependencies
1964
+ }, {
1965
+ kind: "collate",
1966
+ value: expression,
1967
+ collation: normalizedCollation
1968
+ }) as AstBackedExpression<
1969
+ Expression.RuntimeOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1970
+ Expression.DbTypeOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1971
+ NullabilityOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1972
+ DialectOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1973
+ KindOf<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
1974
+ DependenciesOfDialectStringInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
1975
+ ExpressionAst.CollateNode<DialectAsStringExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
1976
+ >
1977
+ }
1978
+
1997
1979
  const cast = <
1998
1980
  Value extends ExpressionInput,
1999
1981
  Target extends CastTarget<Dialect, TextDb, NumericDb, BoolDb, TimestampDb>
@@ -2017,9 +1999,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2017
1999
  runtimeSchema: undefined,
2018
2000
  nullability: expression[Expression.TypeId].nullability,
2019
2001
  dialect: expression[Expression.TypeId].dialect,
2020
- aggregation: expression[Expression.TypeId].aggregation,
2021
- source: expression[Expression.TypeId].source,
2022
- sourceNullability: expression[Expression.TypeId].sourceNullability,
2002
+ kind: expression[Expression.TypeId].kind,
2003
+
2023
2004
  dependencies: expression[Expression.TypeId].dependencies
2024
2005
  }, {
2025
2006
  kind: "cast",
@@ -2143,15 +2124,13 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2143
2124
  Runtime,
2144
2125
  Db extends Expression.DbType.Any,
2145
2126
  Nullability extends Expression.Nullability,
2146
- Ast extends ExpressionAst.Any,
2147
- SourceNullability extends Expression.SourceNullabilityMode = "propagate"
2127
+ Ast extends ExpressionAst.Any
2148
2128
  >(
2149
2129
  expressions: readonly Expression.Any[],
2150
2130
  state: {
2151
2131
  readonly runtime: Runtime
2152
2132
  readonly dbType: Db
2153
2133
  readonly nullability: Nullability
2154
- readonly sourceNullability?: SourceNullability
2155
2134
  },
2156
2135
  ast: Ast
2157
2136
  ): AstBackedExpression<
@@ -2160,29 +2139,24 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2160
2139
  Nullability,
2161
2140
  TupleDialect<typeof expressions>,
2162
2141
  MergeAggregationTuple<typeof expressions>,
2163
- TupleSource<typeof expressions>,
2164
2142
  TupleDependencies<typeof expressions>,
2165
- Ast,
2166
- SourceNullability
2143
+ Ast
2167
2144
  > => makeExpression({
2168
2145
  runtime: state.runtime,
2169
2146
  dbType: state.dbType,
2170
2147
  nullability: state.nullability,
2171
2148
  dialect: (expressions.find((expression) => expression[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as TupleDialect<typeof expressions>,
2172
- aggregation: mergeAggregationManyRuntime(expressions) as MergeAggregationTuple<typeof expressions>,
2173
- source: mergeManySources(expressions) as TupleSource<typeof expressions>,
2174
- sourceNullability: (state.sourceNullability ?? "propagate") as SourceNullability,
2175
- dependencies: mergeManyDependencies(expressions) as TupleDependencies<typeof expressions>
2149
+ kind: mergeAggregationManyRuntime(expressions) as MergeAggregationTuple<typeof expressions>,
2150
+
2151
+ dependencies: mergeManyDependencies(expressions)
2176
2152
  }, ast) as AstBackedExpression<
2177
2153
  Runtime,
2178
2154
  Db,
2179
2155
  Nullability,
2180
2156
  TupleDialect<typeof expressions>,
2181
2157
  MergeAggregationTuple<typeof expressions>,
2182
- TupleSource<typeof expressions>,
2183
2158
  TupleDependencies<typeof expressions>,
2184
- Ast,
2185
- SourceNullability
2159
+ Ast
2186
2160
  >
2187
2161
 
2188
2162
  const jsonDbTypeOf = <Base extends JsonExpressionLike<any>>(
@@ -2201,9 +2175,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2201
2175
  dbType,
2202
2176
  nullability: (value === null ? "always" : "never") as JsonNullabilityOf<Value>,
2203
2177
  dialect: profile.dialect as Dialect,
2204
- aggregation: "scalar",
2205
- source: undefined as never,
2206
- sourceNullability: "resolved" as const,
2178
+ kind: "scalar",
2179
+
2207
2180
  dependencies: {}
2208
2181
  }, {
2209
2182
  kind: "literal",
@@ -2220,7 +2193,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2220
2193
  runtime: undefined as unknown as JsonRuntime<Expression.RuntimeOf<typeof value>>,
2221
2194
  dbType,
2222
2195
  nullability: value[Expression.TypeId].nullability as JsonNullabilityOf<JsonRuntime<Expression.RuntimeOf<typeof value>>>,
2223
- sourceNullability: value[Expression.TypeId].sourceNullability
2224
2196
  },
2225
2197
  {
2226
2198
  kind,
@@ -2256,8 +2228,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2256
2228
  JsonDbOfExpression<Base>,
2257
2229
  JsonNullabilityOf<JsonPathOutputOf<Expression.RuntimeOf<Base>, Target, "json.get">>,
2258
2230
  DialectOf<Base>,
2259
- AggregationOf<Base>,
2260
- SourceOf<Base>,
2231
+ KindOf<Base>,
2261
2232
  DependenciesOf<Base>,
2262
2233
  JsonNode
2263
2234
  > => {
@@ -2282,8 +2253,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2282
2253
  JsonDbOfExpression<Base>,
2283
2254
  JsonNullabilityOf<JsonPathOutputOf<Expression.RuntimeOf<Base>, Target, "json.get">>,
2284
2255
  DialectOf<Base>,
2285
- AggregationOf<Base>,
2286
- SourceOf<Base>,
2256
+ KindOf<Base>,
2287
2257
  DependenciesOf<Base>,
2288
2258
  JsonNode
2289
2259
  >
@@ -2301,8 +2271,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2301
2271
  TextDb,
2302
2272
  JsonNullabilityOf<JsonPathOutputOf<Expression.RuntimeOf<Base>, Target, "json.text">>,
2303
2273
  DialectOf<Base>,
2304
- AggregationOf<Base>,
2305
- SourceOf<Base>,
2274
+ KindOf<Base>,
2306
2275
  DependenciesOf<Base>,
2307
2276
  JsonNode
2308
2277
  > => {
@@ -2329,8 +2298,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2329
2298
  TextDb,
2330
2299
  JsonNullabilityOf<JsonPathOutputOf<Expression.RuntimeOf<Base>, Target, "json.text">>,
2331
2300
  DialectOf<Base>,
2332
- AggregationOf<Base>,
2333
- SourceOf<Base>,
2301
+ KindOf<Base>,
2334
2302
  DependenciesOf<Base>,
2335
2303
  JsonNode
2336
2304
  >
@@ -2458,7 +2426,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2458
2426
  runtime: true as boolean,
2459
2427
  dbType: profile.boolDb as BoolDb,
2460
2428
  nullability: "never" as const,
2461
- sourceNullability: "resolved" as const
2462
2429
  },
2463
2430
  {
2464
2431
  kind: "jsonHasKey",
@@ -2479,7 +2446,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2479
2446
  runtime: true as boolean,
2480
2447
  dbType: profile.boolDb as BoolDb,
2481
2448
  nullability: "never" as const,
2482
- sourceNullability: "resolved" as const
2483
2449
  },
2484
2450
  {
2485
2451
  kind: "jsonHasAnyKeys",
@@ -2500,7 +2466,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2500
2466
  runtime: true as boolean,
2501
2467
  dbType: profile.boolDb as BoolDb,
2502
2468
  nullability: "never" as const,
2503
- sourceNullability: "resolved" as const
2504
2469
  },
2505
2470
  {
2506
2471
  kind: "jsonHasAllKeys",
@@ -2520,8 +2485,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2520
2485
  JsonDbOfExpression<Base>,
2521
2486
  JsonNullabilityOf<JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.delete">>,
2522
2487
  DialectOf<Base>,
2523
- AggregationOf<Base>,
2524
- SourceOf<Base>,
2488
+ KindOf<Base>,
2525
2489
  DependenciesOf<Base>,
2526
2490
  JsonNode
2527
2491
  > => {
@@ -2543,8 +2507,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2543
2507
  JsonDbOfExpression<Base>,
2544
2508
  JsonNullabilityOf<JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.delete">>,
2545
2509
  DialectOf<Base>,
2546
- AggregationOf<Base>,
2547
- SourceOf<Base>,
2510
+ KindOf<Base>,
2548
2511
  DependenciesOf<Base>,
2549
2512
  JsonNode
2550
2513
  >
@@ -2561,8 +2524,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2561
2524
  JsonDbOfExpression<Base>,
2562
2525
  JsonNullabilityOf<JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.remove">>,
2563
2526
  DialectOf<Base>,
2564
- AggregationOf<Base>,
2565
- SourceOf<Base>,
2527
+ KindOf<Base>,
2566
2528
  DependenciesOf<Base>,
2567
2529
  JsonNode
2568
2530
  > => {
@@ -2584,8 +2546,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2584
2546
  JsonDbOfExpression<Base>,
2585
2547
  JsonNullabilityOf<JsonDeleteOutputOf<Expression.RuntimeOf<Base>, Target, "json.remove">>,
2586
2548
  DialectOf<Base>,
2587
- AggregationOf<Base>,
2588
- SourceOf<Base>,
2549
+ KindOf<Base>,
2589
2550
  DependenciesOf<Base>,
2590
2551
  JsonNode
2591
2552
  >
@@ -2607,8 +2568,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2607
2568
  JsonDbOfExpression<Base>,
2608
2569
  JsonNullabilityOf<JsonSetOutputOf<Expression.RuntimeOf<Base>, Target, Next, "json.set">>,
2609
2570
  DialectOf<Base>,
2610
- AggregationOf<Base>,
2611
- SourceOf<Base>,
2571
+ KindOf<Base>,
2612
2572
  DependenciesOf<Base>,
2613
2573
  JsonNode
2614
2574
  > => {
@@ -2633,8 +2593,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2633
2593
  JsonDbOfExpression<Base>,
2634
2594
  JsonNullabilityOf<JsonSetOutputOf<Expression.RuntimeOf<Base>, Target, Next, "json.set">>,
2635
2595
  DialectOf<Base>,
2636
- AggregationOf<Base>,
2637
- SourceOf<Base>,
2596
+ KindOf<Base>,
2638
2597
  DependenciesOf<Base>,
2639
2598
  JsonNode
2640
2599
  >
@@ -2657,8 +2616,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2657
2616
  JsonDbOfExpression<Base>,
2658
2617
  JsonNullabilityOf<JsonInsertOutputOf<Expression.RuntimeOf<Base>, Target, Next, InsertAfter, "json.insert">>,
2659
2618
  DialectOf<Base>,
2660
- AggregationOf<Base>,
2661
- SourceOf<Base>,
2619
+ KindOf<Base>,
2662
2620
  DependenciesOf<Base>,
2663
2621
  JsonNode
2664
2622
  > => {
@@ -2684,8 +2642,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2684
2642
  JsonDbOfExpression<Base>,
2685
2643
  JsonNullabilityOf<JsonInsertOutputOf<Expression.RuntimeOf<Base>, Target, Next, InsertAfter, "json.insert">>,
2686
2644
  DialectOf<Base>,
2687
- AggregationOf<Base>,
2688
- SourceOf<Base>,
2645
+ KindOf<Base>,
2689
2646
  DependenciesOf<Base>,
2690
2647
  JsonNode
2691
2648
  >
@@ -2706,9 +2663,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2706
2663
  Db,
2707
2664
  "maybe",
2708
2665
  Dialect,
2709
- Expression.AggregationKind,
2666
+ Expression.ScalarKind,
2710
2667
  never,
2711
- {},
2712
2668
  JsonNode
2713
2669
  > => {
2714
2670
  const leftExpression = toJsonValueExpression(left)
@@ -2730,9 +2686,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2730
2686
  Db,
2731
2687
  "maybe",
2732
2688
  Dialect,
2733
- Expression.AggregationKind,
2689
+ Expression.ScalarKind,
2734
2690
  never,
2735
- {},
2736
2691
  JsonNode
2737
2692
  >
2738
2693
  }
@@ -2780,7 +2735,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2780
2735
  runtime: true as boolean,
2781
2736
  dbType: profile.boolDb as BoolDb,
2782
2737
  nullability: "never" as const,
2783
- sourceNullability: "resolved" as const
2784
2738
  },
2785
2739
  {
2786
2740
  kind: "jsonKeyExists",
@@ -2808,7 +2762,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2808
2762
  runtime: {} as JsonObjectOutput<Shape>,
2809
2763
  dbType,
2810
2764
  nullability: "never" as const,
2811
- sourceNullability: "resolved" as const
2812
2765
  },
2813
2766
  {
2814
2767
  kind: "jsonBuildObject",
@@ -2833,7 +2786,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2833
2786
  runtime: [] as JsonArrayOutput<Values>,
2834
2787
  dbType,
2835
2788
  nullability: "never" as const,
2836
- sourceNullability: "resolved" as const
2837
2789
  },
2838
2790
  {
2839
2791
  kind: "jsonBuildArray",
@@ -2854,9 +2806,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2854
2806
  JsonDb<Dialect>,
2855
2807
  JsonNullabilityOf<JsonOutputOfInput<Value>>,
2856
2808
  string,
2857
- Expression.AggregationKind,
2858
- unknown,
2859
- Expression.SourceDependencies,
2809
+ Expression.ScalarKind,
2810
+ Expression.BindingId,
2860
2811
  JsonNode
2861
2812
  >
2862
2813
 
@@ -2867,9 +2818,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2867
2818
  JsonDb<Dialect, JsonbKindForDialect<Dialect>>,
2868
2819
  JsonNullabilityOf<JsonOutputOfInput<Value>>,
2869
2820
  string,
2870
- Expression.AggregationKind,
2871
- unknown,
2872
- Expression.SourceDependencies,
2821
+ Expression.ScalarKind,
2822
+ Expression.BindingId,
2873
2823
  JsonNode
2874
2824
  >
2875
2825
 
@@ -2929,7 +2879,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2929
2879
  runtime: true as boolean,
2930
2880
  dbType: profile.boolDb as BoolDb,
2931
2881
  nullability: "never" as const,
2932
- sourceNullability: "resolved" as const
2933
2882
  },
2934
2883
  {
2935
2884
  kind: "jsonPathExists",
@@ -2945,7 +2894,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2945
2894
  runtime: true as boolean,
2946
2895
  dbType: profile.boolDb as BoolDb,
2947
2896
  nullability: "never" as const,
2948
- sourceNullability: "resolved" as const
2949
2897
  },
2950
2898
  {
2951
2899
  kind: "jsonPathExists",
@@ -2981,7 +2929,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2981
2929
  runtime: true as boolean,
2982
2930
  dbType: profile.boolDb as BoolDb,
2983
2931
  nullability: "never" as const,
2984
- sourceNullability: "resolved" as const
2985
2932
  },
2986
2933
  {
2987
2934
  kind: "jsonPathMatch",
@@ -2997,7 +2944,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
2997
2944
  runtime: true as boolean,
2998
2945
  dbType: profile.boolDb as BoolDb,
2999
2946
  nullability: "never" as const,
3000
- sourceNullability: "resolved" as const
3001
2947
  },
3002
2948
  {
3003
2949
  kind: "jsonPathMatch",
@@ -3125,8 +3071,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3125
3071
  BoolDb,
3126
3072
  Expression.NullabilityOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3127
3073
  DialectOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3128
- AggregationOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3129
- SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3074
+ KindOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3130
3075
  DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3131
3076
  ExpressionAst.UnaryNode<"not", DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
3132
3077
  > => {
@@ -3136,10 +3081,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3136
3081
  dbType: profile.boolDb as BoolDb,
3137
3082
  nullability: expression[Expression.TypeId].nullability as Expression.NullabilityOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3138
3083
  dialect: expression[Expression.TypeId].dialect,
3139
- aggregation: expression[Expression.TypeId].aggregation as AggregationOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3140
- source: expression[Expression.TypeId].source as SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3141
- sourceNullability: "propagate" as const,
3142
- dependencies: expression[Expression.TypeId].dependencies as DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
3084
+ kind: expression[Expression.TypeId].kind as KindOf<DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3085
+
3086
+ dependencies: expression[Expression.TypeId].dependencies
3143
3087
  }, {
3144
3088
  kind: "not",
3145
3089
  value: expression
@@ -3153,8 +3097,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3153
3097
  head: Head,
3154
3098
  ...tail: {
3155
3099
  readonly [K in keyof Tail]: Tail[K] & ComparableInput<
3156
- Head,
3157
- Tail[K],
3100
+ NoInfer<Head>,
3101
+ NoInfer<Tail[K]>,
3158
3102
  Dialect,
3159
3103
  TextDb,
3160
3104
  NumericDb,
@@ -3174,8 +3118,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3174
3118
  head: Head,
3175
3119
  ...tail: {
3176
3120
  readonly [K in keyof Tail]: Tail[K] & ComparableInput<
3177
- Head,
3178
- Tail[K],
3121
+ NoInfer<Head>,
3122
+ NoInfer<Tail[K]>,
3179
3123
  Dialect,
3180
3124
  TextDb,
3181
3125
  NumericDb,
@@ -3237,7 +3181,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3237
3181
  MergeNullabilityTuple<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3238
3182
  TupleDialect<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3239
3183
  MergeAggregationTuple<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3240
- TupleSource<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3241
3184
  TupleDependencies<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3242
3185
  ExpressionAst.VariadicNode<"concat", DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
3243
3186
  > => {
@@ -3247,10 +3190,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3247
3190
  dbType: profile.textDb as TextDb,
3248
3191
  nullability: mergeNullabilityManyRuntime(expressions) as MergeNullabilityTuple<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3249
3192
  dialect: (expressions.find((value) => value[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as TupleDialect<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3250
- aggregation: mergeAggregationManyRuntime(expressions) as MergeAggregationTuple<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3251
- source: mergeManySources(expressions) as TupleSource<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3252
- sourceNullability: "propagate" as const,
3253
- dependencies: mergeManyDependencies(expressions) as TupleDependencies<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
3193
+ kind: mergeAggregationManyRuntime(expressions) as MergeAggregationTuple<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3194
+
3195
+ dependencies: mergeManyDependencies(expressions)
3254
3196
  }, {
3255
3197
  kind: "concat",
3256
3198
  values: expressions
@@ -3260,7 +3202,6 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3260
3202
  MergeNullabilityTuple<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3261
3203
  TupleDialect<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3262
3204
  MergeAggregationTuple<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3263
- TupleSource<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3264
3205
  TupleDependencies<DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3265
3206
  ExpressionAst.VariadicNode<"concat", DialectStringExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
3266
3207
  >
@@ -3286,10 +3227,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3286
3227
  "never",
3287
3228
  DialectOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3288
3229
  "aggregate",
3289
- SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3290
3230
  DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3291
- ExpressionAst.UnaryNode<"count", DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3292
- "resolved"
3231
+ ExpressionAst.UnaryNode<"count", DialectAsExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
3293
3232
  > => {
3294
3233
  const expression = toDialectExpression(value)
3295
3234
  return makeExpression({
@@ -3297,10 +3236,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3297
3236
  dbType: profile.numericDb as NumericDb,
3298
3237
  nullability: "never",
3299
3238
  dialect: expression[Expression.TypeId].dialect,
3300
- aggregation: "aggregate",
3301
- source: expression[Expression.TypeId].source as SourceOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3302
- sourceNullability: "resolved" as const,
3303
- dependencies: expression[Expression.TypeId].dependencies as DependenciesOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
3239
+ kind: "aggregate",
3240
+
3241
+ dependencies: expression[Expression.TypeId].dependencies
3304
3242
  }, {
3305
3243
  kind: "count",
3306
3244
  value: expression
@@ -3317,10 +3255,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3317
3255
  "never",
3318
3256
  Dialect,
3319
3257
  "scalar",
3320
- never,
3321
3258
  DependencyRecord<OutstandingOfPlan<PlanValue>>,
3322
- ExpressionAst.ExistsNode<PlanValue>,
3323
- "resolved"
3259
+ ExpressionAst.ExistsNode<PlanValue>
3324
3260
  > => {
3325
3261
  const dependencies = Object.fromEntries(
3326
3262
  currentRequiredList(plan[Plan.TypeId].required).map((name) => [name, true] as const)
@@ -3330,9 +3266,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3330
3266
  dbType: profile.boolDb as BoolDb,
3331
3267
  nullability: "never",
3332
3268
  dialect: profile.dialect as Dialect,
3333
- aggregation: "scalar",
3334
- source: undefined as never,
3335
- sourceNullability: "resolved" as const,
3269
+ kind: "scalar",
3270
+
3336
3271
  dependencies
3337
3272
  }, {
3338
3273
  kind: "exists",
@@ -3350,10 +3285,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3350
3285
  "maybe",
3351
3286
  Dialect,
3352
3287
  "scalar",
3353
- never,
3354
3288
  DependencyRecord<OutstandingOfPlan<PlanValue>>,
3355
- ExpressionAst.ScalarSubqueryNode<PlanValue>,
3356
- "resolved"
3289
+ ExpressionAst.ScalarSubqueryNode<PlanValue>
3357
3290
  > => {
3358
3291
  const dependencies = Object.fromEntries(
3359
3292
  currentRequiredList(plan[Plan.TypeId].required).map((name) => [name, true] as const)
@@ -3364,9 +3297,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3364
3297
  dbType: expression[Expression.TypeId].dbType as Expression.DbTypeOf<ScalarOutputOfPlan<PlanValue>>,
3365
3298
  nullability: "maybe",
3366
3299
  dialect: profile.dialect as Dialect,
3367
- aggregation: "scalar",
3368
- source: undefined as never,
3369
- sourceNullability: "resolved" as const,
3300
+ kind: "scalar",
3301
+
3370
3302
  dependencies
3371
3303
  }, {
3372
3304
  kind: "scalarSubquery",
@@ -3404,8 +3336,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3404
3336
  )
3405
3337
  ): SubqueryPredicateExpression<
3406
3338
  Dialect,
3407
- AggregationOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3408
- SourceOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3339
+ KindOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3409
3340
  DependencyRecord<RequiredFromDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | OutstandingOfPlan<PlanValue>>,
3410
3341
  ExpressionAst.InSubqueryNode<
3411
3342
  DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
@@ -3421,12 +3352,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3421
3352
  dbType: profile.boolDb as BoolDb,
3422
3353
  nullability: "maybe",
3423
3354
  dialect: (leftExpression[Expression.TypeId].dialect ?? profile.dialect) as Dialect,
3424
- aggregation: leftExpression[Expression.TypeId].aggregation as AggregationOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3425
- source: leftExpression[Expression.TypeId].source as SourceOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3426
- sourceNullability: "propagate" as const,
3427
- dependencies: mergeDependencies(leftExpression[Expression.TypeId].dependencies, dependencies) as DependencyRecord<
3428
- RequiredFromDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | OutstandingOfPlan<PlanValue>
3429
- >
3355
+ kind: leftExpression[Expression.TypeId].kind as KindOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3356
+
3357
+ dependencies: mergeDependencies(leftExpression[Expression.TypeId].dependencies, dependencies)
3430
3358
  }, {
3431
3359
  kind: "inSubquery",
3432
3360
  left: leftExpression,
@@ -3454,12 +3382,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3454
3382
  dbType: profile.boolDb as BoolDb,
3455
3383
  nullability: "maybe",
3456
3384
  dialect: (leftExpression[Expression.TypeId].dialect ?? profile.dialect) as Dialect,
3457
- aggregation: leftExpression[Expression.TypeId].aggregation as AggregationOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3458
- source: leftExpression[Expression.TypeId].source as SourceOfDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
3459
- sourceNullability: "propagate" as const,
3460
- dependencies: mergeDependencies(leftExpression[Expression.TypeId].dependencies, dependencies) as DependencyRecord<
3461
- RequiredFromDialectInput<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb> | OutstandingOfPlan<PlanValue>
3462
- >
3385
+ kind: leftExpression[Expression.TypeId].kind as KindOf<DialectAsExpression<Left, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3386
+
3387
+ dependencies: mergeDependencies(leftExpression[Expression.TypeId].dependencies, dependencies)
3463
3388
  }, renderQuantifiedComparisonAst(leftExpression, plan, operator, quantifier) as ExpressionAst.QuantifiedComparisonNode<
3464
3389
  Quantifier extends "any" ? "comparisonAny" : "comparisonAll",
3465
3390
  Operator,
@@ -3489,15 +3414,13 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3489
3414
  ): Expression.Any => quantifiedComparison(left, plan, operator, "all") as Expression.Any
3490
3415
 
3491
3416
  const over = <
3492
- Value extends Expression.Expression<
3417
+ Value extends Expression.Scalar<
3493
3418
  any,
3494
3419
  Expression.DbType.Any,
3495
3420
  Expression.Nullability,
3496
3421
  string,
3497
3422
  "aggregate",
3498
- any,
3499
- Expression.SourceDependencies,
3500
- Expression.SourceNullabilityMode
3423
+ Expression.BindingId
3501
3424
  >,
3502
3425
  PartitionBy extends readonly WindowPartitionInput[] = [],
3503
3426
  OrderBy extends readonly WindowOrderTermInput[] = []
@@ -3512,10 +3435,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3512
3435
  dbType: value[Expression.TypeId].dbType as Expression.DbTypeOf<Value>,
3513
3436
  nullability: value[Expression.TypeId].nullability as Expression.NullabilityOf<Value>,
3514
3437
  dialect: (expressions.find((expression) => expression[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as WindowDialectOf<Value, PartitionBy, OrderBy>,
3515
- aggregation: "window",
3516
- source: mergeManySources(expressions) as WindowSourceOf<Value, PartitionBy, OrderBy>,
3517
- sourceNullability: "resolved" as const,
3518
- dependencies: mergeManyDependencies(expressions) as WindowDependenciesOf<Value, PartitionBy, OrderBy>
3438
+ kind: "window",
3439
+
3440
+ dependencies: mergeManyDependencies(expressions)
3519
3441
  }, {
3520
3442
  kind: "window",
3521
3443
  function: "over",
@@ -3540,10 +3462,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3540
3462
  dbType: profile.numericDb as Expression.DbType.Any,
3541
3463
  nullability: "never",
3542
3464
  dialect: (expressions.find((expression) => expression[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as NumberWindowDialectOf<PartitionBy, OrderBy>,
3543
- aggregation: "window",
3544
- source: mergeManySources(expressions) as NumberWindowSourceOf<PartitionBy, OrderBy>,
3545
- sourceNullability: "resolved" as const,
3546
- dependencies: mergeManyDependencies(expressions) as NumberWindowDependenciesOf<PartitionBy, OrderBy>
3465
+ kind: "window",
3466
+
3467
+ dependencies: mergeManyDependencies(expressions)
3547
3468
  }, {
3548
3469
  kind: "window",
3549
3470
  function: kind,
@@ -3584,20 +3505,17 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3584
3505
  "maybe",
3585
3506
  DialectOf<Value>,
3586
3507
  "aggregate",
3587
- SourceOf<Value>,
3588
3508
  DependenciesOf<Value>,
3589
- ExpressionAst.UnaryNode<"max", Value>,
3590
- "resolved"
3509
+ ExpressionAst.UnaryNode<"max", Value>
3591
3510
  > =>
3592
3511
  makeExpression({
3593
3512
  runtime: undefined as Expression.RuntimeOf<Value>,
3594
3513
  dbType: value[Expression.TypeId].dbType as Expression.DbTypeOf<Value>,
3595
3514
  nullability: "maybe",
3596
3515
  dialect: value[Expression.TypeId].dialect,
3597
- aggregation: "aggregate",
3598
- source: value[Expression.TypeId].source as SourceOf<Value>,
3599
- sourceNullability: "resolved" as const,
3600
- dependencies: value[Expression.TypeId].dependencies as DependenciesOf<Value>
3516
+ kind: "aggregate",
3517
+
3518
+ dependencies: value[Expression.TypeId].dependencies
3601
3519
  }, {
3602
3520
  kind: "max",
3603
3521
  value
@@ -3611,20 +3529,17 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3611
3529
  "maybe",
3612
3530
  DialectOf<Value>,
3613
3531
  "aggregate",
3614
- SourceOf<Value>,
3615
3532
  DependenciesOf<Value>,
3616
- ExpressionAst.UnaryNode<"min", Value>,
3617
- "resolved"
3533
+ ExpressionAst.UnaryNode<"min", Value>
3618
3534
  > =>
3619
3535
  makeExpression({
3620
3536
  runtime: undefined as Expression.RuntimeOf<Value>,
3621
3537
  dbType: value[Expression.TypeId].dbType as Expression.DbTypeOf<Value>,
3622
3538
  nullability: "maybe",
3623
3539
  dialect: value[Expression.TypeId].dialect,
3624
- aggregation: "aggregate",
3625
- source: value[Expression.TypeId].source as SourceOf<Value>,
3626
- sourceNullability: "resolved" as const,
3627
- dependencies: value[Expression.TypeId].dependencies as DependenciesOf<Value>
3540
+ kind: "aggregate",
3541
+
3542
+ dependencies: value[Expression.TypeId].dependencies
3628
3543
  }, {
3629
3544
  kind: "min",
3630
3545
  value
@@ -3649,10 +3564,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3649
3564
  CoalesceNullabilityTuple<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3650
3565
  TupleDialect<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3651
3566
  MergeAggregationTuple<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3652
- TupleSource<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3653
3567
  TupleDependencies<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3654
- ExpressionAst.VariadicNode<"coalesce", DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3655
- "resolved"
3568
+ ExpressionAst.VariadicNode<"coalesce", DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
3656
3569
  > => {
3657
3570
  const expressions = values.map((value) => toDialectExpression(value)) as readonly Expression.Any[]
3658
3571
  const representative = expressions.find((value) =>
@@ -3662,10 +3575,9 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3662
3575
  dbType: representative[Expression.TypeId].dbType as any,
3663
3576
  nullability: resolveCoalesceNullabilityRuntime(expressions) as any,
3664
3577
  dialect: (expressions.find((value) => value[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as any,
3665
- aggregation: mergeAggregationManyRuntime(expressions) as any,
3666
- source: mergeManySources(expressions) as any,
3667
- sourceNullability: "resolved" as const,
3668
- dependencies: mergeManyDependencies(expressions) as any
3578
+ kind: mergeAggregationManyRuntime(expressions) as any,
3579
+
3580
+ dependencies: mergeManyDependencies(expressions)
3669
3581
  }, {
3670
3582
  kind: "coalesce",
3671
3583
  values: expressions
@@ -3675,10 +3587,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3675
3587
  CoalesceNullabilityTuple<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3676
3588
  TupleDialect<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3677
3589
  MergeAggregationTuple<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3678
- TupleSource<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3679
3590
  TupleDependencies<DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3680
- ExpressionAst.VariadicNode<"coalesce", DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
3681
- "resolved"
3591
+ ExpressionAst.VariadicNode<"coalesce", DialectExpressionTuple<Values, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>
3682
3592
  >
3683
3593
  }
3684
3594
 
@@ -3695,9 +3605,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3695
3605
  dbType: profile.textDb,
3696
3606
  nullability: "maybe",
3697
3607
  dialect: (expressions.find((value) => value[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect) as Dialect,
3698
- aggregation: mergeAggregationManyRuntime(expressions),
3699
- source: mergeManySources(expressions),
3700
- sourceNullability: "resolved" as const,
3608
+ kind: mergeAggregationManyRuntime(expressions),
3609
+
3701
3610
  dependencies: mergeManyDependencies(expressions)
3702
3611
  }, {
3703
3612
  kind: "function",
@@ -3706,25 +3615,22 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3706
3615
  }) as Expression.Any
3707
3616
  }
3708
3617
 
3709
- const uuidGenerateV4 = (): Expression.Expression<
3618
+ const uuidGenerateV4 = (): Expression.Scalar<
3710
3619
  string,
3711
- Expression.DbType.PgUuid,
3620
+ Expression.DbType.Base<"postgres", "uuid">,
3712
3621
  "never",
3713
3622
  Dialect,
3714
3623
  "scalar",
3715
- never,
3716
- {},
3717
- "resolved"
3624
+ never
3718
3625
  > & {
3719
3626
  readonly [ExpressionAst.TypeId]: ExpressionAst.FunctionCallNode<"uuid_generate_v4", readonly []>
3720
3627
  } => makeExpression({
3721
3628
  runtime: undefined as unknown as string,
3722
- dbType: { dialect: "postgres", kind: "uuid" } as Expression.DbType.PgUuid,
3629
+ dbType: postgresDatatypes.uuid(),
3723
3630
  nullability: "never",
3724
3631
  dialect: profile.dialect as Dialect,
3725
- aggregation: "scalar",
3726
- source: undefined as never,
3727
- sourceNullability: "resolved" as const,
3632
+ kind: "scalar",
3633
+
3728
3634
  dependencies: {}
3729
3635
  }, {
3730
3636
  kind: "function",
@@ -3734,25 +3640,22 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3734
3640
 
3735
3641
  const nextVal = <Value extends ExpressionInput>(
3736
3642
  value: Value
3737
- ): Expression.Expression<
3738
- Expression.RuntimeOfDbType<Expression.DbType.PgInt8>,
3739
- Expression.DbType.PgInt8,
3643
+ ): Expression.Scalar<
3644
+ Expression.RuntimeOfDbType<Expression.DbType.Base<"postgres", "int8">>,
3645
+ Expression.DbType.Base<"postgres", "int8">,
3740
3646
  "never",
3741
3647
  Dialect,
3742
3648
  "scalar",
3743
- never,
3744
- {},
3745
- "resolved"
3649
+ never
3746
3650
  > & {
3747
3651
  readonly [ExpressionAst.TypeId]: ExpressionAst.FunctionCallNode<"nextval", readonly [Expression.Any]>
3748
3652
  } => makeExpression({
3749
- runtime: undefined as unknown as Expression.RuntimeOfDbType<Expression.DbType.PgInt8>,
3750
- dbType: { dialect: "postgres", kind: "int8" } as Expression.DbType.PgInt8,
3653
+ runtime: undefined as unknown as Expression.RuntimeOfDbType<Expression.DbType.Base<"postgres", "int8">>,
3654
+ dbType: postgresDatatypes.int8(),
3751
3655
  nullability: "never",
3752
3656
  dialect: profile.dialect as Dialect,
3753
- aggregation: "scalar",
3754
- source: undefined as never,
3755
- sourceNullability: "resolved" as const,
3657
+ kind: "scalar",
3658
+
3756
3659
  dependencies: {}
3757
3660
  }, {
3758
3661
  kind: "function",
@@ -3802,9 +3705,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3802
3705
  dbType: representative[Expression.TypeId].dbType,
3803
3706
  nullability: resolveCaseNullabilityRuntime(resultExpressions),
3804
3707
  dialect: (allExpressions.find((value) => value[Expression.TypeId].dialect !== undefined)?.[Expression.TypeId].dialect ?? profile.dialect),
3805
- aggregation: mergeAggregationManyRuntime(allExpressions),
3806
- source: mergeManySources(allExpressions),
3807
- sourceNullability: "resolved" as const,
3708
+ kind: mergeAggregationManyRuntime(allExpressions),
3709
+
3808
3710
  dependencies: mergeManyDependencies(allExpressions)
3809
3711
  }, {
3810
3712
  kind: "case",
@@ -3909,15 +3811,13 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3909
3811
  }
3910
3812
 
3911
3813
  const excluded = <
3912
- Value extends Expression.Expression<
3814
+ Value extends Expression.Scalar<
3913
3815
  any,
3914
3816
  Expression.DbType.Any,
3915
3817
  Expression.Nullability,
3916
3818
  string,
3917
3819
  "scalar",
3918
- any,
3919
- Expression.SourceDependencies,
3920
- Expression.SourceNullabilityMode
3820
+ Expression.BindingId
3921
3821
  >
3922
3822
  >(
3923
3823
  value: Value
@@ -3928,9 +3828,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3928
3828
  Dialect,
3929
3829
  "scalar",
3930
3830
  never,
3931
- {},
3932
- ExpressionAst.ExcludedNode<AstOf<Value> extends ExpressionAst.ColumnNode<any, infer ColumnName extends string> ? ColumnName : string>,
3933
- "resolved"
3831
+ ExpressionAst.ExcludedNode<AstOf<Value> extends ExpressionAst.ColumnNode<any, infer ColumnName extends string> ? ColumnName : string>
3934
3832
  > => {
3935
3833
  const ast = ((value as unknown) as Expression.Any & { readonly [ExpressionAst.TypeId]: ExpressionAst.Any })[ExpressionAst.TypeId]
3936
3834
  if (ast.kind !== "column") {
@@ -3942,9 +3840,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3942
3840
  runtimeSchema: value[Expression.TypeId].runtimeSchema,
3943
3841
  nullability: value[Expression.TypeId].nullability as Expression.NullabilityOf<Value>,
3944
3842
  dialect: profile.dialect as Dialect,
3945
- aggregation: "scalar",
3946
- source: undefined as never,
3947
- sourceNullability: "resolved" as const,
3843
+ kind: "scalar",
3844
+
3948
3845
  dependencies: {}
3949
3846
  }, {
3950
3847
  kind: "excluded",
@@ -3956,9 +3853,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3956
3853
  Dialect,
3957
3854
  "scalar",
3958
3855
  never,
3959
- {},
3960
- ExpressionAst.ExcludedNode<AstOf<Value> extends ExpressionAst.ColumnNode<any, infer ColumnName extends string> ? ColumnName : string>,
3961
- "resolved"
3856
+ ExpressionAst.ExcludedNode<AstOf<Value> extends ExpressionAst.ColumnNode<any, infer ColumnName extends string> ? ColumnName : string>
3962
3857
  >
3963
3858
  }
3964
3859
 
@@ -3974,9 +3869,8 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
3974
3869
  dbType: column[Expression.TypeId].dbType,
3975
3870
  nullability: value === null ? "always" : "never",
3976
3871
  dialect: column[Expression.TypeId].dialect,
3977
- aggregation: "scalar",
3978
- source: undefined as never,
3979
- sourceNullability: "propagate" as const,
3872
+ kind: "scalar",
3873
+
3980
3874
  dependencies: {}
3981
3875
  }, {
3982
3876
  kind: "literal",
@@ -4049,13 +3943,7 @@ const profile: QueryDialectProfile<Dialect, TextDb, NumericDb, BoolDb, Timestamp
4049
3943
  runtimeSchema: state.runtimeSchema,
4050
3944
  nullability: state.nullability,
4051
3945
  dialect: state.dialect,
4052
- aggregation: "scalar",
4053
- source: {
4054
- tableName: alias,
4055
- columnName,
4056
- baseTableName: alias
4057
- },
4058
- sourceNullability: "propagate" as const,
3946
+ kind: "scalar",
4059
3947
  dependencies: {
4060
3948
  [alias]: true
4061
3949
  } as Record<Alias, true>
@@ -4690,8 +4578,6 @@ type AstOf<Value extends Expression.Any> =
4690
4578
  ? Ast
4691
4579
  : never
4692
4580
 
4693
- type AvailableNames<Available extends Record<string, Plan.AnySource>> = Extract<keyof Available, string>
4694
-
4695
4581
  type RequiredFromInput<Value extends ExpressionInput> =
4696
4582
  Value extends Expression.Any
4697
4583
  ? RequiredFromDependencies<DependenciesOf<Value>>
@@ -4843,6 +4729,74 @@ type FromPlanResult<
4843
4729
  : never
4844
4730
  : never
4845
4731
 
4732
+ export type PublicStructuredFromConstraint<
4733
+ PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
4734
+ CurrentSource extends AnyValuesSource | AnyUnnestSource | AnyTableFunctionSource,
4735
+ Dialect extends string
4736
+ > =
4737
+ StatementOfPlan<PlanValue> extends "insert"
4738
+ ? CurrentSource extends AnyValuesSource | AnyUnnestSource
4739
+ ? RequirePendingInsertStatement<PlanValue>
4740
+ : FromPlanConstraint<PlanValue, CurrentSource, Dialect>
4741
+ : FromPlanConstraint<PlanValue, CurrentSource, Dialect>
4742
+
4743
+ export type PublicStructuredFromResult<
4744
+ PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
4745
+ CurrentSource extends AnyValuesSource | AnyUnnestSource | AnyTableFunctionSource,
4746
+ Dialect extends string
4747
+ > =
4748
+ StatementOfPlan<PlanValue> extends "select"
4749
+ ? QueryPlan<
4750
+ SelectionOfPlan<PlanValue>,
4751
+ Exclude<RequiredOfPlan<PlanValue>, SourceNameOf<CurrentSource>>,
4752
+ AddAvailable<{}, SourceNameOf<CurrentSource>>,
4753
+ PlanDialectOf<PlanValue> | SourceDialectOf<CurrentSource>,
4754
+ GroupedOfPlan<PlanValue>,
4755
+ SourceNameOf<CurrentSource>,
4756
+ Exclude<OutstandingOfPlan<PlanValue>, SourceNameOf<CurrentSource>>,
4757
+ AssumptionsOfPlan<PlanValue>,
4758
+ MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentSource>>,
4759
+ StatementOfPlan<PlanValue>
4760
+ >
4761
+ : StatementOfPlan<PlanValue> extends "update"
4762
+ ? QueryPlan<
4763
+ SelectionOfPlan<PlanValue>,
4764
+ Exclude<RequiredOfPlan<PlanValue>, SourceNameOf<CurrentSource>>,
4765
+ AddAvailable<AvailableOfPlan<PlanValue>, SourceNameOf<CurrentSource>>,
4766
+ PlanDialectOf<PlanValue> | SourceDialectOf<CurrentSource>,
4767
+ GroupedOfPlan<PlanValue>,
4768
+ ScopedNamesOfPlan<PlanValue> | SourceNameOf<CurrentSource>,
4769
+ Exclude<OutstandingOfPlan<PlanValue>, SourceNameOf<CurrentSource>>,
4770
+ AssumptionsOfPlan<PlanValue>,
4771
+ MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentSource>>,
4772
+ StatementOfPlan<PlanValue>
4773
+ >
4774
+ : StatementOfPlan<PlanValue> extends "insert"
4775
+ ? CurrentSource extends AnyValuesSource | AnyUnnestSource
4776
+ ? QueryPlan<
4777
+ SelectionOfPlan<PlanValue>,
4778
+ never,
4779
+ AvailableOfPlan<PlanValue>,
4780
+ PlanDialectOf<PlanValue>,
4781
+ GroupedOfPlan<PlanValue>,
4782
+ ScopedNamesOfPlan<PlanValue>,
4783
+ never,
4784
+ AssumptionsOfPlan<PlanValue>,
4785
+ CapabilitiesOfPlan<PlanValue>,
4786
+ StatementOfPlan<PlanValue>,
4787
+ MutationTargetOfPlan<PlanValue>,
4788
+ "ready"
4789
+ >
4790
+ : FromPlanResult<PlanValue, CurrentSource, Dialect>
4791
+ : FromPlanResult<PlanValue, CurrentSource, Dialect>
4792
+
4793
+ export type PublicNonStructuredFromApi = <CurrentSource extends Exclude<FromInput, AnyValuesSource | AnyUnnestSource | AnyTableFunctionSource>>(
4794
+ source: CurrentSource
4795
+ ) =>
4796
+ <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
4797
+ plan: PlanValue & FromPlanConstraint<PlanValue, CurrentSource, Dialect>
4798
+ ) => FromPlanResult<PlanValue, CurrentSource, Dialect>
4799
+
4846
4800
  type MergeRequiredFromPredicate<
4847
4801
  Predicate extends PredicateInput | undefined,
4848
4802
  Available extends Record<string, Plan.AnySource>
@@ -4920,7 +4874,7 @@ type AsCurriedResult<
4920
4874
  if (typeof value !== "object" || value === null || Expression.TypeId in value) {
4921
4875
  const expression = toDialectExpression(value as ExpressionInput)
4922
4876
  const projected = Object.create(Object.getPrototypeOf(expression)) as {
4923
- [Expression.TypeId]: Expression.State<any, any, any, any, any, any, any, Expression.SourceNullabilityMode>
4877
+ [Expression.TypeId]: Expression.State<any, any, any, any, any, any>
4924
4878
  [ExpressionAst.TypeId]: ExpressionAst.Any
4925
4879
  [ProjectionAlias.TypeId]: ProjectionAlias.State<string>
4926
4880
  schema?: unknown
@@ -4956,14 +4910,14 @@ type AsCurriedResult<
4956
4910
  alias: Alias
4957
4911
  ): <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
4958
4912
  value: CompletePlan<PlanValue>
4959
- ) => import("./query.js").CteSource<PlanValue, Alias>
4913
+ ) => import("../../internal/query.js").CteSource<PlanValue, Alias>
4960
4914
  function with_<
4961
4915
  PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
4962
4916
  Alias extends string
4963
4917
  >(
4964
4918
  value: CompletePlan<PlanValue>,
4965
4919
  alias: Alias
4966
- ): import("./query.js").CteSource<PlanValue, Alias>
4920
+ ): import("../../internal/query.js").CteSource<PlanValue, Alias>
4967
4921
  function with_(valueOrAlias: unknown, alias?: string): unknown {
4968
4922
  if (alias === undefined) {
4969
4923
  return (value: unknown) => with_(value as any, valueOrAlias as string)
@@ -4980,14 +4934,14 @@ type AsCurriedResult<
4980
4934
  alias: Alias
4981
4935
  ): <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
4982
4936
  value: CompletePlan<PlanValue>
4983
- ) => import("./query.js").CteSource<PlanValue, Alias>
4937
+ ) => import("../../internal/query.js").CteSource<PlanValue, Alias>
4984
4938
  function withRecursive_<
4985
4939
  PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
4986
4940
  Alias extends string
4987
4941
  >(
4988
4942
  value: CompletePlan<PlanValue>,
4989
4943
  alias: Alias
4990
- ): import("./query.js").CteSource<PlanValue, Alias>
4944
+ ): import("../../internal/query.js").CteSource<PlanValue, Alias>
4991
4945
  function withRecursive_(valueOrAlias: unknown, alias?: string): unknown {
4992
4946
  if (alias === undefined) {
4993
4947
  return (value: unknown) => withRecursive_(value as any, valueOrAlias as string)
@@ -5005,14 +4959,14 @@ type AsCurriedResult<
5005
4959
  alias: Alias
5006
4960
  ): <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5007
4961
  value: PlanValue
5008
- ) => import("./query.js").LateralSource<PlanValue, Alias>
4962
+ ) => import("../../internal/query.js").LateralSource<PlanValue, Alias>
5009
4963
  function lateral<
5010
4964
  PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5011
4965
  Alias extends string
5012
4966
  >(
5013
4967
  value: PlanValue,
5014
4968
  alias: Alias
5015
- ): import("./query.js").LateralSource<PlanValue, Alias>
4969
+ ): import("../../internal/query.js").LateralSource<PlanValue, Alias>
5016
4970
  function lateral(valueOrAlias: unknown, alias?: string): unknown {
5017
4971
  if (alias === undefined) {
5018
4972
  return (value: unknown) => lateral(value as any, valueOrAlias as string)
@@ -5023,105 +4977,29 @@ type AsCurriedResult<
5023
4977
  )
5024
4978
  }
5025
4979
 
5026
- const values = <
4980
+ export type ValuesApi = <
5027
4981
  Rows extends ValuesRowsInput
5028
4982
  >(
5029
4983
  rows: Rows
5030
- ): ValuesInput<
4984
+ ) => ValuesInput<
5031
4985
  Rows,
5032
4986
  ValuesOutputShape<Rows[0], Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5033
4987
  Dialect
5034
- > => {
5035
- if (rows.length === 0) {
5036
- throw new Error("values(...) requires at least one row")
5037
- }
5038
- const normalizedRows: readonly [
5039
- Record<string, Expression.Any>,
5040
- ...Record<string, Expression.Any>[]
5041
- ] = rows.map((row) => normalizeValuesRow(row)) as any
5042
- const columnNames = Object.keys(normalizedRows[0]!)
5043
- for (const row of normalizedRows) {
5044
- const rowKeys = Object.keys(row)
5045
- if (rowKeys.length !== columnNames.length || !rowKeys.every((key, index) => key === columnNames[index])) {
5046
- throw new Error("values(...) rows must project the same columns in the same order")
5047
- }
5048
- }
5049
- return Object.assign(Object.create(ValuesInputProto), {
5050
- kind: "values",
5051
- dialect: profile.dialect,
5052
- rows: normalizedRows,
5053
- selection: normalizedRows[0]! as ValuesOutputShape<
5054
- Rows[0],
5055
- Dialect,
5056
- TextDb,
5057
- NumericDb,
5058
- BoolDb,
5059
- TimestampDb,
5060
- NullDb
5061
- >
5062
- }) as ValuesInput<
5063
- Rows,
5064
- ValuesOutputShape<Rows[0], Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5065
- Dialect
5066
- >
5067
- }
4988
+ >
5068
4989
 
5069
- const unnest = <
4990
+ export type UnnestApi = <
5070
4991
  Columns extends UnnestColumnsInput,
5071
4992
  Alias extends string
5072
4993
  >(
5073
4994
  columns: Columns,
5074
4995
  alias: Alias
5075
- ): UnnestSource<
4996
+ ) => UnnestSource<
5076
4997
  UnnestOutputShape<Columns, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5077
4998
  Alias,
5078
4999
  Dialect
5079
- > => {
5080
- const normalizedColumns = normalizeUnnestColumns(columns)
5081
- const columnNames = Object.keys(normalizedColumns)
5082
- if (columnNames.length === 0) {
5083
- throw new Error("unnest(...) requires at least one column array")
5084
- }
5085
- const firstColumn = normalizedColumns[columnNames[0] as keyof typeof normalizedColumns]
5086
- const rowCount = firstColumn?.length ?? 0
5087
- if (rowCount === 0) {
5088
- throw new Error("unnest(...) requires at least one row")
5089
- }
5090
- for (const columnName of columnNames) {
5091
- const values = normalizedColumns[columnName]!
5092
- if (values.length !== rowCount) {
5093
- throw new Error("unnest(...) column arrays must have the same length")
5094
- }
5095
- }
5096
- const firstRow = Object.fromEntries(
5097
- columnNames.map((columnName) => [columnName, normalizedColumns[columnName]![0]!])
5098
- ) as Record<string, Expression.Any>
5099
- const columnsSelection = makeColumnReferenceSelection(alias, firstRow) as any as UnnestOutputShape<
5100
- Columns,
5101
- Dialect,
5102
- TextDb,
5103
- NumericDb,
5104
- BoolDb,
5105
- TimestampDb,
5106
- NullDb
5107
- >
5108
- const source = {
5109
- kind: "unnest",
5110
- name: alias,
5111
- baseName: alias,
5112
- dialect: profile.dialect,
5113
- values: columns,
5114
- arrays: normalizedColumns,
5115
- columns: columnsSelection
5116
- }
5117
- return Object.assign(source, columnsSelection) as unknown as UnnestSource<
5118
- UnnestOutputShape<Columns, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5119
- Alias,
5120
- Dialect
5121
- >
5122
- }
5000
+ >
5123
5001
 
5124
- const generateSeries = <
5002
+ export type GenerateSeriesApi = <
5125
5003
  Start extends NumericExpressionInput,
5126
5004
  Stop extends NumericExpressionInput,
5127
5005
  Step extends NumericExpressionInput | undefined = undefined,
@@ -5130,54 +5008,35 @@ type AsCurriedResult<
5130
5008
  start: Start,
5131
5009
  stop: Stop,
5132
5010
  step?: Step,
5133
- alias: Alias = "series" as Alias
5134
- ): Dialect extends "postgres"
5011
+ alias?: Alias
5012
+ ) => Dialect extends "postgres"
5135
5013
  ? TableFunctionSource<
5136
5014
  GenerateSeriesOutputShape<Start, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5137
5015
  Alias,
5138
5016
  Dialect,
5139
5017
  "generate_series"
5140
5018
  >
5141
- : GenerateSeriesUnsupportedError<Dialect> => {
5142
- const startExpression = toDialectNumericExpression(start)
5143
- const stopExpression = toDialectNumericExpression(stop)
5144
- const stepExpression = step === undefined ? undefined : toDialectNumericExpression(step)
5145
- const valueSelection = {
5146
- value: startExpression
5147
- } as Record<string, Expression.Any>
5148
- const columns = makeColumnReferenceSelection(alias, valueSelection) as any as GenerateSeriesOutputShape<
5149
- Start,
5150
- Dialect,
5151
- TextDb,
5152
- NumericDb,
5153
- BoolDb,
5154
- TimestampDb,
5155
- NullDb
5156
- >
5157
- const source = {
5158
- kind: "tableFunction",
5159
- name: alias,
5160
- baseName: alias,
5161
- dialect: profile.dialect,
5162
- functionName: "generate_series",
5163
- args: stepExpression === undefined
5164
- ? [startExpression, stopExpression] as readonly Expression.Any[]
5165
- : [startExpression, stopExpression, stepExpression] as readonly Expression.Any[],
5166
- columns
5167
- }
5168
- return Object.assign(source, columns) as unknown as Dialect extends "postgres"
5169
- ? TableFunctionSource<
5170
- GenerateSeriesOutputShape<Start, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5171
- Alias,
5172
- Dialect,
5173
- "generate_series"
5174
- >
5175
- : GenerateSeriesUnsupportedError<Dialect>
5176
- }
5019
+ : GenerateSeriesUnsupportedError<Dialect>
5177
5020
 
5178
- const select = <Selection extends SelectionShape>(
5021
+ export type PublicGenerateSeriesApi = <
5022
+ Alias extends string = "series"
5023
+ >(
5024
+ start: NumericExpressionInput,
5025
+ stop: NumericExpressionInput,
5026
+ step?: NumericExpressionInput,
5027
+ alias?: Alias
5028
+ ) => TableFunctionSource<
5029
+ {
5030
+ readonly value: Expression.Scalar<number, NumericDb, "never", Dialect, "scalar", never, string>
5031
+ },
5032
+ Alias,
5033
+ Dialect,
5034
+ "generate_series"
5035
+ >
5036
+
5037
+ export type SelectApi = <Selection extends SelectionShape>(
5179
5038
  selection: Selection
5180
- ): QueryPlan<
5039
+ ) => QueryPlan<
5181
5040
  Selection,
5182
5041
  ExtractRequired<Selection>,
5183
5042
  {},
@@ -5188,214 +5047,41 @@ type AsCurriedResult<
5188
5047
  TrueFormula,
5189
5048
  "read",
5190
5049
  "select"
5191
- > =>
5192
- makePlan({
5193
- selection,
5194
- required: extractRequiredRuntime(selection) as ExtractRequired<Selection>,
5195
- available: {},
5196
- dialect: profile.dialect as ExtractDialect<Selection> extends never ? Dialect : ExtractDialect<Selection>
5197
- }, {
5198
- kind: "select",
5199
- select: selection,
5200
- where: [],
5201
- having: [],
5202
- joins: [],
5203
- groupBy: [],
5204
- orderBy: []
5205
- }, undefined as unknown as TrueFormula, "read", "select")
5206
-
5207
- const buildSetOperation = <
5208
- Operator extends QueryAst.SetOperatorKind,
5209
- LeftPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5210
- RightPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>
5211
- >(
5212
- kind: Operator,
5213
- all: boolean,
5214
- left: SetCompatiblePlan<LeftPlanValue, Dialect>,
5215
- right: SetCompatibleRightPlan<LeftPlanValue, RightPlanValue, Dialect>
5216
- ): QueryPlan<
5217
- SelectionOfPlan<LeftPlanValue>,
5218
- never,
5219
- {},
5220
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5221
- GroupedOfPlan<LeftPlanValue>,
5222
- never,
5223
- never,
5224
- TrueFormula,
5225
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5226
- "set"
5227
- > => {
5228
- const leftState = left[Plan.TypeId]
5229
- const leftAst = getAst(left)
5230
- const basePlan = leftAst.kind === "set"
5231
- ? leftAst.setBase ?? left
5232
- : left
5233
- const leftOperations = leftAst.kind === "set"
5234
- ? [...(leftAst.setOperations ?? [])]
5235
- : []
5236
- return makePlan({
5237
- selection: leftState.selection as SelectionOfPlan<LeftPlanValue>,
5238
- required: undefined as never,
5239
- available: {},
5240
- dialect: (leftState.dialect ?? right[Plan.TypeId].dialect) as PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>
5241
- }, {
5242
- kind: "set",
5243
- select: leftState.selection,
5244
- where: [],
5245
- having: [],
5246
- joins: [],
5247
- groupBy: [],
5248
- orderBy: [],
5249
- setBase: basePlan,
5250
- setOperations: [
5251
- ...leftOperations,
5252
- {
5253
- kind,
5254
- all,
5255
- query: right
5256
- }
5257
- ]
5258
- }, undefined as unknown as TrueFormula, undefined as unknown as CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>, "set")
5259
- }
5260
-
5261
- const union = <
5262
- LeftPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5263
- RightPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>
5264
- >(
5265
- left: SetCompatiblePlan<LeftPlanValue, Dialect>,
5266
- right: SetCompatibleRightPlan<LeftPlanValue, RightPlanValue, Dialect>
5267
- ): QueryPlan<
5268
- SelectionOfPlan<LeftPlanValue>,
5269
- never,
5270
- {},
5271
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5272
- GroupedOfPlan<LeftPlanValue>,
5273
- never,
5274
- never,
5275
- TrueFormula,
5276
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5277
- "set"
5278
- > => buildSetOperation("union", false, left as never, right as never) as QueryPlan<
5279
- SelectionOfPlan<LeftPlanValue>,
5280
- never,
5281
- {},
5282
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5283
- GroupedOfPlan<LeftPlanValue>,
5284
- never,
5285
- never,
5286
- TrueFormula,
5287
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5288
- "set"
5289
5050
  >
5290
5051
 
5291
- const unionAll = <
5292
- LeftPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5293
- RightPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>
5294
- >(
5295
- left: SetCompatiblePlan<LeftPlanValue, Dialect>,
5296
- right: SetCompatibleRightPlan<LeftPlanValue, RightPlanValue, Dialect>
5297
- ): QueryPlan<
5298
- SelectionOfPlan<LeftPlanValue>,
5299
- never,
5300
- {},
5301
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5302
- GroupedOfPlan<LeftPlanValue>,
5303
- never,
5304
- never,
5305
- TrueFormula,
5306
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5307
- "set"
5308
- > => buildSetOperation("union", true, left as never, right as never) as QueryPlan<
5309
- SelectionOfPlan<LeftPlanValue>,
5310
- never,
5311
- {},
5312
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5313
- GroupedOfPlan<LeftPlanValue>,
5314
- never,
5315
- never,
5316
- TrueFormula,
5317
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5318
- "set"
5319
- >
5320
-
5321
- const intersect = <
5322
- LeftPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5323
- RightPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>
5324
- >(
5325
- left: SetCompatiblePlan<LeftPlanValue, Dialect>,
5326
- right: SetCompatibleRightPlan<LeftPlanValue, RightPlanValue, Dialect>
5327
- ): QueryPlan<
5328
- SelectionOfPlan<LeftPlanValue>,
5329
- never,
5330
- {},
5331
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5332
- GroupedOfPlan<LeftPlanValue>,
5333
- never,
5334
- never,
5335
- TrueFormula,
5336
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5337
- "set"
5338
- > => buildSetOperation("intersect", false, left as never, right as never) as QueryPlan<
5339
- SelectionOfPlan<LeftPlanValue>,
5340
- never,
5341
- {},
5342
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5343
- GroupedOfPlan<LeftPlanValue>,
5344
- never,
5345
- never,
5346
- TrueFormula,
5347
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5348
- "set"
5349
- >
5350
-
5351
- const intersectAll = <
5352
- LeftPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5353
- RightPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>
5354
- >(
5355
- left: SetCompatiblePlan<LeftPlanValue, Dialect>,
5356
- right: SetCompatibleRightPlan<LeftPlanValue, RightPlanValue, Dialect>
5357
- ): QueryPlan<
5358
- SelectionOfPlan<LeftPlanValue>,
5359
- never,
5360
- {},
5361
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5362
- GroupedOfPlan<LeftPlanValue>,
5363
- never,
5364
- never,
5365
- TrueFormula,
5366
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5367
- "set"
5368
- > => buildSetOperation("intersect", true, left as never, right as never) as QueryPlan<
5369
- SelectionOfPlan<LeftPlanValue>,
5370
- never,
5371
- {},
5372
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5373
- GroupedOfPlan<LeftPlanValue>,
5374
- never,
5375
- never,
5376
- TrueFormula,
5377
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5378
- "set"
5379
- >
5052
+ const {
5053
+ values,
5054
+ unnest,
5055
+ generateSeries,
5056
+ select,
5057
+ groupBy,
5058
+ returning
5059
+ } = makeDslQueryRuntime({
5060
+ profile,
5061
+ ValuesInputProto,
5062
+ normalizeValuesRow,
5063
+ normalizeUnnestColumns,
5064
+ makeColumnReferenceSelection,
5065
+ toDialectNumericExpression,
5066
+ extractRequiredRuntime,
5067
+ makePlan,
5068
+ getAst,
5069
+ getQueryState,
5070
+ currentRequiredList,
5071
+ dedupeGroupedExpressions
5072
+ }) as {
5073
+ readonly values: ValuesApi
5074
+ readonly unnest: UnnestApi
5075
+ readonly generateSeries: GenerateSeriesApi
5076
+ readonly select: SelectApi
5077
+ readonly groupBy: GroupByApi
5078
+ readonly returning: ReturningApi
5079
+ }
5380
5080
 
5381
- const except = <
5081
+ type SetOperationResult<
5382
5082
  LeftPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5383
5083
  RightPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>
5384
- >(
5385
- left: SetCompatiblePlan<LeftPlanValue, Dialect>,
5386
- right: SetCompatibleRightPlan<LeftPlanValue, RightPlanValue, Dialect>
5387
- ): QueryPlan<
5388
- SelectionOfPlan<LeftPlanValue>,
5389
- never,
5390
- {},
5391
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5392
- GroupedOfPlan<LeftPlanValue>,
5393
- never,
5394
- never,
5395
- TrueFormula,
5396
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5397
- "set"
5398
- > => buildSetOperation("except", false, left as never, right as never) as QueryPlan<
5084
+ > = QueryPlan<
5399
5085
  SelectionOfPlan<LeftPlanValue>,
5400
5086
  never,
5401
5087
  {},
@@ -5408,42 +5094,20 @@ type AsCurriedResult<
5408
5094
  "set"
5409
5095
  >
5410
5096
 
5411
- const exceptAll = <
5097
+ type SetOperationApi = <
5412
5098
  LeftPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>,
5413
5099
  RightPlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>
5414
5100
  >(
5415
5101
  left: SetCompatiblePlan<LeftPlanValue, Dialect>,
5416
5102
  right: SetCompatibleRightPlan<LeftPlanValue, RightPlanValue, Dialect>
5417
- ): QueryPlan<
5418
- SelectionOfPlan<LeftPlanValue>,
5419
- never,
5420
- {},
5421
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5422
- GroupedOfPlan<LeftPlanValue>,
5423
- never,
5424
- never,
5425
- TrueFormula,
5426
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5427
- "set"
5428
- > => buildSetOperation("except", true, left as never, right as never) as QueryPlan<
5429
- SelectionOfPlan<LeftPlanValue>,
5430
- never,
5431
- {},
5432
- PlanDialectOf<LeftPlanValue> | PlanDialectOf<RightPlanValue>,
5433
- GroupedOfPlan<LeftPlanValue>,
5434
- never,
5435
- never,
5436
- TrueFormula,
5437
- CapabilitiesOfPlan<LeftPlanValue> | CapabilitiesOfPlan<RightPlanValue>,
5438
- "set"
5439
- >
5103
+ ) => SetOperationResult<LeftPlanValue, RightPlanValue>
5440
5104
 
5441
- const where = <Predicate extends PredicateInput>(
5105
+ type WhereApi = <Predicate extends PredicateInput>(
5442
5106
  predicate: Predicate
5443
5107
  ) =>
5444
5108
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5445
5109
  plan: PlanValue & RequireWhereStatement<PlanValue>
5446
- ): QueryPlan<
5110
+ ) => QueryPlan<
5447
5111
  SelectionOfPlan<PlanValue>,
5448
5112
  AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Predicate>,
5449
5113
  AvailableOfPlan<PlanValue>,
@@ -5454,131 +5118,21 @@ type AsCurriedResult<
5454
5118
  PlanAssumptionsAfterWhere<PlanValue, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5455
5119
  CapabilitiesOfPlan<PlanValue>,
5456
5120
  StatementOfPlan<PlanValue>
5457
- > => {
5458
- const current = plan[Plan.TypeId]
5459
- const currentAst = getAst(plan)
5460
- const currentQuery = getQueryState(plan)
5461
- const predicateExpression = toDialectExpression(predicate)
5462
- const predicateRequired = extractRequiredFromDialectInputRuntime(predicate)
5463
- return makePlan({
5464
- selection: current.selection,
5465
- required: [...currentRequiredList(current.required), ...predicateRequired].filter((name, index, values) =>
5466
- !(name in current.available) && values.indexOf(name) === index) as AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Predicate>,
5467
- available: current.available,
5468
- dialect: current.dialect as PlanDialectOf<PlanValue> | DialectOfDialectInput<Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
5469
- }, {
5470
- ...currentAst,
5471
- where: [...currentAst.where, {
5472
- kind: "where",
5473
- predicate: predicateExpression
5474
- }]
5475
- },
5476
- assumeFormulaTrue(
5477
- currentQuery.assumptions,
5478
- formulaOfExpressionRuntime(predicateExpression)
5479
- ) as PlanAssumptionsAfterWhere<PlanValue, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5480
- currentQuery.capabilities,
5481
- currentQuery.statement as StatementOfPlan<PlanValue>)
5482
- }
5121
+ >
5483
5122
 
5484
- const from = <CurrentSource extends FromInput>(
5123
+ export type FromApi = <CurrentSource extends FromInput>(
5485
5124
  source: CurrentSource
5486
5125
  ) =>
5487
5126
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5488
5127
  plan: PlanValue & FromPlanConstraint<PlanValue, CurrentSource, Dialect>
5489
- ): FromPlanResult<PlanValue, CurrentSource, Dialect> => {
5490
- const current = plan[Plan.TypeId]
5491
- const currentAst = getAst(plan)
5492
- const currentQuery = getQueryState(plan)
5493
-
5494
- if (currentQuery.statement === "insert") {
5495
- return attachInsertSource(
5496
- plan as QueryPlan<any, any, any, any, any, any, any, any, any, "insert", MutationTargetLike, "missing">,
5497
- source as AnyValuesInput | AnyValuesSource | AnyUnnestSource | CompletePlan<QueryPlan<any, any, any, any, any, any, any, any, any, any>>
5498
- ) as FromPlanResult<PlanValue, CurrentSource, Dialect>
5499
- }
5128
+ ) => FromPlanResult<PlanValue, CurrentSource, Dialect>
5500
5129
 
5501
- if (
5502
- typeof source !== "object" ||
5503
- source === null ||
5504
- ("kind" in source && source.kind === "values" && !("name" in source)) ||
5505
- (!(Table.TypeId in source) && !("name" in source && "baseName" in source))
5506
- ) {
5507
- throw new Error("from(...) requires an aliased source in select/update statements")
5508
- }
5509
-
5510
- const sourceLike = source as SourceLike
5511
- const { sourceName, sourceBaseName } = sourceDetails(sourceLike)
5512
- const presenceWitnesses = presenceWitnessesOfSourceLike(sourceLike)
5513
-
5514
- if (currentQuery.statement === "select") {
5515
- const nextAst = {
5516
- ...currentAst,
5517
- from: {
5518
- kind: "from",
5519
- tableName: sourceName,
5520
- baseTableName: sourceBaseName,
5521
- source: sourceLike
5522
- }
5523
- } as QueryAst.Ast<Record<string, unknown>, any, "select">
5524
- return makePlan({
5525
- selection: current.selection,
5526
- required: currentRequiredList(current.required).filter((name) =>
5527
- name !== sourceName),
5528
- available: {
5529
- [sourceName]: {
5530
- name: sourceName,
5531
- mode: "required",
5532
- baseName: sourceBaseName,
5533
- _presentFormula: trueFormula(),
5534
- _presenceWitnesses: presenceWitnesses
5535
- }
5536
- } as AddAvailable<{}, string, "required", TrueFormula, PresenceWitnessKeysOfSource<Extract<CurrentSource, SourceLike>>>,
5537
- dialect: current.dialect
5538
- }, nextAst, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement) as FromPlanResult<PlanValue, CurrentSource, Dialect>
5539
- }
5540
-
5541
- if (currentQuery.statement === "update") {
5542
- const nextAvailable = {
5543
- ...current.available,
5544
- [sourceName]: {
5545
- name: sourceName,
5546
- mode: "required" as const,
5547
- baseName: sourceBaseName,
5548
- _presentFormula: trueFormula(),
5549
- _presenceWitnesses: presenceWitnesses
5550
- }
5551
- }
5552
- const nextAst = {
5553
- ...currentAst,
5554
- fromSources: [
5555
- ...(currentAst.fromSources ?? []),
5556
- {
5557
- kind: "from" as const,
5558
- tableName: sourceName,
5559
- baseTableName: sourceBaseName,
5560
- source: sourceLike
5561
- }
5562
- ]
5563
- } as QueryAst.Ast<Record<string, unknown>, any, "update">
5564
- return makePlan({
5565
- selection: current.selection,
5566
- required: currentRequiredList(current.required).filter((name) =>
5567
- !(name in nextAvailable)),
5568
- available: nextAvailable,
5569
- dialect: current.dialect
5570
- }, nextAst, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement) as FromPlanResult<PlanValue, CurrentSource, Dialect>
5571
- }
5572
-
5573
- throw new Error(`from(...) is not supported for ${currentQuery.statement} statements`)
5574
- }
5575
-
5576
- const having = <Predicate extends HavingPredicateInput>(
5130
+ type HavingApi = <Predicate extends HavingPredicateInput>(
5577
5131
  predicate: Predicate
5578
5132
  ) =>
5579
5133
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5580
5134
  plan: PlanValue & RequireSelectStatement<PlanValue>
5581
- ): QueryPlan<
5135
+ ) => QueryPlan<
5582
5136
  SelectionOfPlan<PlanValue>,
5583
5137
  AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Predicate>,
5584
5138
  AvailableOfPlan<PlanValue>,
@@ -5589,57 +5143,9 @@ type AsCurriedResult<
5589
5143
  PlanAssumptionsAfterHaving<PlanValue, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5590
5144
  CapabilitiesOfPlan<PlanValue>,
5591
5145
  StatementOfPlan<PlanValue>
5592
- > => {
5593
- const current = plan[Plan.TypeId]
5594
- const currentAst = getAst(plan)
5595
- const currentQuery = getQueryState(plan)
5596
- const predicateExpression = toDialectExpression(predicate)
5597
- const predicateRequired = extractRequiredFromDialectInputRuntime(predicate)
5598
- return makePlan({
5599
- selection: current.selection,
5600
- required: [...currentRequiredList(current.required), ...predicateRequired].filter((name, index, values) =>
5601
- !(name in current.available) && values.indexOf(name) === index) as AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Predicate>,
5602
- available: current.available,
5603
- dialect: current.dialect as PlanDialectOf<PlanValue> | DialectOfDialectInput<Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
5604
- }, {
5605
- ...currentAst,
5606
- having: [...currentAst.having, {
5607
- kind: "having",
5608
- predicate: predicateExpression
5609
- }]
5610
- }, assumeFormulaTrue(
5611
- currentQuery.assumptions,
5612
- formulaOfExpressionRuntime(predicateExpression)
5613
- ) as PlanAssumptionsAfterHaving<PlanValue, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5614
- currentQuery.capabilities,
5615
- currentQuery.statement as StatementOfPlan<PlanValue>)
5616
- }
5617
-
5618
- const innerJoin = <CurrentTable extends SourceLike, Predicate extends PredicateInput>(
5619
- table: CurrentTable,
5620
- on: Predicate
5621
- ) =>
5622
- join("inner", table, on)
5623
-
5624
- const leftJoin = <CurrentTable extends SourceLike, Predicate extends PredicateInput>(
5625
- table: CurrentTable,
5626
- on: Predicate
5627
- ) =>
5628
- join("left", table, on)
5629
-
5630
- const rightJoin = <CurrentTable extends SourceLike, Predicate extends PredicateInput>(
5631
- table: CurrentTable,
5632
- on: Predicate
5633
- ) =>
5634
- join("right", table, on)
5635
-
5636
- const fullJoin = <CurrentTable extends SourceLike, Predicate extends PredicateInput>(
5637
- table: CurrentTable,
5638
- on: Predicate
5639
- ) =>
5640
- join("full", table, on)
5146
+ >
5641
5147
 
5642
- const crossJoin = <CurrentTable extends SourceLike>(
5148
+ type CrossJoinApi = <CurrentTable extends SourceLike>(
5643
5149
  table: CurrentTable
5644
5150
  ) =>
5645
5151
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
@@ -5648,7 +5154,7 @@ type AsCurriedResult<
5648
5154
  ) & (
5649
5155
  SourceNameOf<CurrentTable> extends ScopedNamesOfPlan<PlanValue> ? never : unknown
5650
5156
  )
5651
- ): QueryPlan<
5157
+ ) => QueryPlan<
5652
5158
  SelectionOfPlan<PlanValue>,
5653
5159
  AddJoinRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, SourceNameOf<CurrentTable>, never, "cross">,
5654
5160
  AddAvailable<
@@ -5665,49 +5171,9 @@ type AsCurriedResult<
5665
5171
  AssumptionsOfPlan<PlanValue>,
5666
5172
  MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentTable>>,
5667
5173
  StatementOfPlan<PlanValue>
5668
- > => {
5669
- const current = plan[Plan.TypeId]
5670
- const currentAst = getAst(plan)
5671
- const currentQuery = getQueryState(plan)
5672
- const { sourceName, sourceBaseName } = sourceDetails(table)
5673
- const presenceWitnesses = presenceWitnessesOfSourceLike(table)
5674
- const nextAvailable = Object.assign(
5675
- {},
5676
- current.available as AvailableOfPlan<PlanValue>,
5677
- {
5678
- [sourceName]: {
5679
- name: sourceName,
5680
- mode: "required",
5681
- baseName: sourceBaseName,
5682
- _presentFormula: trueFormula(),
5683
- _presenceWitnesses: presenceWitnesses
5684
- }
5685
- }
5686
- ) as AddAvailable<
5687
- AvailableOfPlan<PlanValue>,
5688
- SourceNameOf<CurrentTable>,
5689
- "required",
5690
- TrueFormula,
5691
- PresenceWitnessKeysOfSource<CurrentTable>
5692
- >
5693
- return makePlan({
5694
- selection: current.selection,
5695
- required: currentRequiredList(current.required).filter((name) =>
5696
- !(name in nextAvailable)) as AddJoinRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, SourceNameOf<CurrentTable>, never, "cross">,
5697
- available: nextAvailable,
5698
- dialect: current.dialect as PlanDialectOf<PlanValue> | SourceDialectOf<CurrentTable>
5699
- }, {
5700
- ...currentAst,
5701
- joins: [...currentAst.joins, {
5702
- kind: "cross",
5703
- tableName: sourceName,
5704
- baseTableName: sourceBaseName,
5705
- source: table
5706
- }]
5707
- }, currentQuery.assumptions, currentQuery.capabilities as MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentTable>>, currentQuery.statement as StatementOfPlan<PlanValue>)
5708
- }
5174
+ >
5709
5175
 
5710
- const join = <
5176
+ type JoinApi = <
5711
5177
  Kind extends QueryAst.JoinKind,
5712
5178
  CurrentTable extends SourceLike,
5713
5179
  Predicate extends PredicateInput
@@ -5722,7 +5188,7 @@ type AsCurriedResult<
5722
5188
  ) & (
5723
5189
  SourceNameOf<CurrentTable> extends ScopedNamesOfPlan<PlanValue> ? never : unknown
5724
5190
  )
5725
- ): QueryPlan<
5191
+ ) => QueryPlan<
5726
5192
  SelectionOfPlan<PlanValue>,
5727
5193
  AddJoinRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, SourceNameOf<CurrentTable>, Predicate, Kind>,
5728
5194
  AvailableAfterJoin<
@@ -5739,72 +5205,47 @@ type AsCurriedResult<
5739
5205
  PlanAssumptionsAfterJoin<PlanValue, Predicate, Kind, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5740
5206
  MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentTable>>,
5741
5207
  StatementOfPlan<PlanValue>
5742
- > => {
5743
- const current = plan[Plan.TypeId]
5744
- const currentAst = getAst(plan)
5745
- const currentQuery = getQueryState(plan)
5746
- const onExpression = toDialectExpression(on)
5747
- const onFormula = formulaOfExpressionRuntime(onExpression)
5748
- const { sourceName, sourceBaseName } = sourceDetails(table)
5749
- const presenceWitnesses = presenceWitnessesOfSourceLike(table)
5750
- const baseAvailable = (kind === "right" || kind === "full"
5751
- ? Object.fromEntries(
5752
- Object.entries(current.available as Record<string, Plan.AnySource>).map(([name, source]) => [name, {
5753
- name: source.name,
5754
- mode: "optional" as const,
5755
- baseName: source.baseName,
5756
- _presentFormula: source._presentFormula,
5757
- _presenceWitnesses: source._presenceWitnesses
5758
- }])
5759
- )
5760
- : current.available) as AvailableOfPlan<PlanValue>
5761
- const nextAvailable = {
5762
- ...baseAvailable,
5763
- [sourceName]: {
5764
- name: sourceName,
5765
- mode: (kind === "left" || kind === "full" ? "optional" : "required") as JoinSourceMode<Kind>,
5766
- baseName: sourceBaseName,
5767
- _presentFormula: (kind === "inner" || kind === "left") ? onFormula : trueFormula(),
5768
- _presenceWitnesses: presenceWitnesses
5769
- }
5770
- } as AvailableAfterJoin<
5208
+ >
5209
+
5210
+ type BinaryJoinApi<Kind extends QueryAst.JoinKind> = <
5211
+ CurrentTable extends SourceLike,
5212
+ Predicate extends PredicateInput
5213
+ >(
5214
+ table: CurrentTable,
5215
+ on: Predicate
5216
+ ) =>
5217
+ <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5218
+ plan: PlanValue & RequireJoinStatement<PlanValue> & (
5219
+ keyof AvailableOfPlan<PlanValue> extends never ? never : unknown
5220
+ ) & (
5221
+ SourceNameOf<CurrentTable> extends ScopedNamesOfPlan<PlanValue> ? never : unknown
5222
+ )
5223
+ ) => QueryPlan<
5224
+ SelectionOfPlan<PlanValue>,
5225
+ AddJoinRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, SourceNameOf<CurrentTable>, Predicate, Kind>,
5226
+ AvailableAfterJoin<
5771
5227
  AvailableOfPlan<PlanValue>,
5772
5228
  SourceNameOf<CurrentTable>,
5773
5229
  Kind,
5774
5230
  JoinPresenceFormula<Kind, Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5775
5231
  PresenceWitnessKeysOfSource<CurrentTable>
5776
- >
5777
- return makePlan({
5778
- selection: current.selection,
5779
- required: [...currentRequiredList(current.required), ...extractRequiredFromDialectInputRuntime(on)].filter((name, index, values) =>
5780
- !(name in nextAvailable) && values.indexOf(name) === index) as AddJoinRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, SourceNameOf<CurrentTable>, Predicate, Kind>,
5781
- available: nextAvailable,
5782
- dialect: current.dialect as PlanDialectOf<PlanValue> | SourceDialectOf<CurrentTable> | DialectOfDialectInput<Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
5783
- }, {
5784
- ...currentAst,
5785
- joins: [...currentAst.joins, {
5786
- kind,
5787
- tableName: sourceName,
5788
- baseTableName: sourceBaseName,
5789
- source: table,
5790
- on: onExpression
5791
- }]
5792
- }, (
5793
- kind === "inner"
5794
- ? assumeFormulaTrue(currentQuery.assumptions, onFormula)
5795
- : currentQuery.assumptions
5796
- ) as PlanAssumptionsAfterJoin<PlanValue, Predicate, Kind, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5797
- currentQuery.capabilities as MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentTable>>,
5798
- currentQuery.statement as StatementOfPlan<PlanValue>)
5799
- }
5232
+ >,
5233
+ PlanDialectOf<PlanValue> | SourceDialectOf<CurrentTable> | DialectOfDialectInput<Predicate, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5234
+ GroupedOfPlan<PlanValue>,
5235
+ ScopedNamesOfPlan<PlanValue> | SourceNameOf<CurrentTable>,
5236
+ AddJoinRequired<OutstandingOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, SourceNameOf<CurrentTable>, Predicate, Kind>,
5237
+ PlanAssumptionsAfterJoin<PlanValue, Predicate, Kind, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>,
5238
+ MergeCapabilities<CapabilitiesOfPlan<PlanValue>, SourceCapabilitiesOf<CurrentTable>>,
5239
+ StatementOfPlan<PlanValue>
5240
+ >
5800
5241
 
5801
- const orderBy = <Value extends ExpressionInput>(
5242
+ type OrderByApi = <Value extends ExpressionInput>(
5802
5243
  value: Value,
5803
- direction: OrderDirection = "asc"
5244
+ direction?: OrderDirection
5804
5245
  ) =>
5805
5246
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5806
5247
  plan: PlanValue & MutationOrderLimitSupported<PlanValue, Dialect>
5807
- ): QueryPlan<
5248
+ ) => QueryPlan<
5808
5249
  SelectionOfPlan<PlanValue>,
5809
5250
  AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Value>,
5810
5251
  AvailableOfPlan<PlanValue>,
@@ -5815,79 +5256,12 @@ type AsCurriedResult<
5815
5256
  AssumptionsOfPlan<PlanValue>,
5816
5257
  CapabilitiesOfPlan<PlanValue>,
5817
5258
  StatementOfPlan<PlanValue>
5818
- > => {
5819
- const current = plan[Plan.TypeId]
5820
- const currentAst = getAst(plan)
5821
- const currentQuery = getQueryState(plan)
5822
- const expression = toDialectExpression(value)
5823
- const required = extractRequiredFromDialectInputRuntime(value)
5824
- return makePlan({
5825
- selection: current.selection,
5826
- required: [...currentRequiredList(current.required), ...required].filter((name, index, values) =>
5827
- !(name in current.available) && values.indexOf(name) === index) as AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, Value>,
5828
- available: current.available,
5829
- dialect: current.dialect as PlanDialectOf<PlanValue> | DialectOfDialectInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
5830
- }, {
5831
- ...currentAst,
5832
- orderBy: [...currentAst.orderBy, {
5833
- kind: "orderBy",
5834
- value: expression,
5835
- direction
5836
- }]
5837
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>)
5838
- }
5839
-
5840
- function lock(
5841
- mode: "update" | "share",
5842
- options?: LockOptions
5843
- ): <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5844
- plan: PlanValue & (StatementOfPlan<PlanValue> extends "select" ? unknown : never)
5845
- ) => QueryPlan<
5846
- SelectionOfPlan<PlanValue>,
5847
- RequiredOfPlan<PlanValue>,
5848
- AvailableOfPlan<PlanValue>,
5849
- PlanDialectOf<PlanValue>,
5850
- GroupedOfPlan<PlanValue>,
5851
- ScopedNamesOfPlan<PlanValue>,
5852
- OutstandingOfPlan<PlanValue>,
5853
- AssumptionsOfPlan<PlanValue>,
5854
- MergeCapabilities<CapabilitiesOfPlan<PlanValue>, "transaction">,
5855
- StatementOfPlan<PlanValue>
5856
- >
5857
- function lock<
5858
- Mode extends Dialect extends "mysql" ? "lowPriority" | "ignore" | "quick" : never
5859
- >(
5860
- mode: Mode,
5861
- options?: LockOptions
5862
- ): <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5863
- plan: PlanValue & (
5864
- Dialect extends "mysql"
5865
- ? StatementOfPlan<PlanValue> extends "update"
5866
- ? Mode extends MutationLockModeForStatement<"update", Dialect> ? unknown : never
5867
- : StatementOfPlan<PlanValue> extends "delete"
5868
- ? Mode extends MutationLockModeForStatement<"delete", Dialect> ? unknown : never
5869
- : never
5870
- : never
5871
- )
5872
- ) => QueryPlan<
5873
- SelectionOfPlan<PlanValue>,
5874
- RequiredOfPlan<PlanValue>,
5875
- AvailableOfPlan<PlanValue>,
5876
- PlanDialectOf<PlanValue>,
5877
- GroupedOfPlan<PlanValue>,
5878
- ScopedNamesOfPlan<PlanValue>,
5879
- OutstandingOfPlan<PlanValue>,
5880
- AssumptionsOfPlan<PlanValue>,
5881
- MergeCapabilities<CapabilitiesOfPlan<PlanValue>, "transaction">,
5882
- StatementOfPlan<PlanValue>
5883
- >
5884
- function lock(
5885
- mode: QueryAst.LockClause["mode"],
5886
- options: LockOptions = {}
5887
- ) {
5888
- return <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5889
- plan: PlanValue
5890
- ): QueryPlan<
5259
+ >
5260
+
5261
+ interface LockApi {
5262
+ (mode: "update" | "share", options?: LockOptions): <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5263
+ plan: PlanValue & (StatementOfPlan<PlanValue> extends "select" ? unknown : never)
5264
+ ) => QueryPlan<
5891
5265
  SelectionOfPlan<PlanValue>,
5892
5266
  RequiredOfPlan<PlanValue>,
5893
5267
  AvailableOfPlan<PlanValue>,
@@ -5898,31 +5272,21 @@ type AsCurriedResult<
5898
5272
  AssumptionsOfPlan<PlanValue>,
5899
5273
  MergeCapabilities<CapabilitiesOfPlan<PlanValue>, "transaction">,
5900
5274
  StatementOfPlan<PlanValue>
5901
- > => {
5902
- const current = plan[Plan.TypeId]
5903
- const currentAst = getAst(plan)
5904
- const currentQuery = getQueryState(plan)
5905
- return makePlan({
5906
- selection: current.selection,
5907
- required: current.required as RequiredOfPlan<PlanValue>,
5908
- available: current.available,
5909
- dialect: current.dialect as PlanDialectOf<PlanValue>
5910
- }, {
5911
- ...currentAst,
5912
- lock: {
5913
- kind: "lock",
5914
- mode,
5915
- nowait: options.nowait ?? false,
5916
- skipLocked: options.skipLocked ?? false
5917
- }
5918
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>)
5919
- }
5920
- }
5921
-
5922
- const distinct = () =>
5923
- <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5924
- plan: PlanValue & RequireSelectStatement<PlanValue>
5925
- ): QueryPlan<
5275
+ >
5276
+ <Mode extends Dialect extends "mysql" ? "lowPriority" | "ignore" | "quick" : never>(
5277
+ mode: Mode,
5278
+ options?: LockOptions
5279
+ ): <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5280
+ plan: PlanValue & (
5281
+ Dialect extends "mysql"
5282
+ ? StatementOfPlan<PlanValue> extends "update"
5283
+ ? Mode extends MutationLockModeForStatement<"update", Dialect> ? unknown : never
5284
+ : StatementOfPlan<PlanValue> extends "delete"
5285
+ ? Mode extends MutationLockModeForStatement<"delete", Dialect> ? unknown : never
5286
+ : never
5287
+ : never
5288
+ )
5289
+ ) => QueryPlan<
5926
5290
  SelectionOfPlan<PlanValue>,
5927
5291
  RequiredOfPlan<PlanValue>,
5928
5292
  AvailableOfPlan<PlanValue>,
@@ -5931,28 +5295,15 @@ type AsCurriedResult<
5931
5295
  ScopedNamesOfPlan<PlanValue>,
5932
5296
  OutstandingOfPlan<PlanValue>,
5933
5297
  AssumptionsOfPlan<PlanValue>,
5934
- CapabilitiesOfPlan<PlanValue>,
5298
+ MergeCapabilities<CapabilitiesOfPlan<PlanValue>, "transaction">,
5935
5299
  StatementOfPlan<PlanValue>
5936
- > => {
5937
- const current = plan[Plan.TypeId]
5938
- const currentAst = getAst(plan)
5939
- const currentQuery = getQueryState(plan)
5940
- return makePlan({
5941
- selection: current.selection,
5942
- required: current.required as RequiredOfPlan<PlanValue>,
5943
- available: current.available,
5944
- dialect: current.dialect as PlanDialectOf<PlanValue>
5945
- }, {
5946
- ...currentAst,
5947
- distinct: true
5948
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>)
5949
- }
5300
+ >
5301
+ }
5950
5302
 
5951
- const distinctOn = ((...values: readonly ExpressionInput[]) => {
5952
- const expressions = values.map((value) => toDialectExpression(value)) as Expression.Any[]
5953
- return <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5303
+ type DistinctApi = () =>
5304
+ <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5954
5305
  plan: PlanValue & RequireSelectStatement<PlanValue>
5955
- ): QueryPlan<
5306
+ ) => QueryPlan<
5956
5307
  SelectionOfPlan<PlanValue>,
5957
5308
  RequiredOfPlan<PlanValue>,
5958
5309
  AvailableOfPlan<PlanValue>,
@@ -5963,29 +5314,14 @@ type AsCurriedResult<
5963
5314
  AssumptionsOfPlan<PlanValue>,
5964
5315
  CapabilitiesOfPlan<PlanValue>,
5965
5316
  StatementOfPlan<PlanValue>
5966
- > => {
5967
- const current = plan[Plan.TypeId]
5968
- const currentAst = getAst(plan)
5969
- const currentQuery = getQueryState(plan)
5970
- return makePlan({
5971
- selection: current.selection,
5972
- required: current.required as RequiredOfPlan<PlanValue>,
5973
- available: current.available,
5974
- dialect: current.dialect as PlanDialectOf<PlanValue>
5975
- }, {
5976
- ...currentAst,
5977
- distinct: true,
5978
- distinctOn: expressions
5979
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>)
5980
- }
5981
- }) as DistinctOnApi<Dialect>
5317
+ >
5982
5318
 
5983
- const limit = <Value extends NumericExpressionInput>(
5319
+ type LimitApi = <Value extends NumericExpressionInput>(
5984
5320
  value: Value
5985
5321
  ) =>
5986
5322
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5987
5323
  plan: PlanValue & MutationOrderLimitSupported<PlanValue, Dialect>
5988
- ): QueryPlan<
5324
+ ) => QueryPlan<
5989
5325
  SelectionOfPlan<PlanValue>,
5990
5326
  AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, DialectAsNumericExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
5991
5327
  AvailableOfPlan<PlanValue>,
@@ -5996,30 +5332,14 @@ type AsCurriedResult<
5996
5332
  AssumptionsOfPlan<PlanValue>,
5997
5333
  CapabilitiesOfPlan<PlanValue>,
5998
5334
  StatementOfPlan<PlanValue>
5999
- > => {
6000
- const current = plan[Plan.TypeId]
6001
- const currentAst = getAst(plan)
6002
- const currentQuery = getQueryState(plan)
6003
- const expression = toDialectNumericExpression(value)
6004
- const required = extractRequiredFromDialectNumericInputRuntime(value)
6005
- return makePlan({
6006
- selection: current.selection,
6007
- required: [...currentRequiredList(current.required), ...required].filter((name, index, values) =>
6008
- !(name in current.available) && values.indexOf(name) === index) as AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, DialectAsNumericExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
6009
- available: current.available,
6010
- dialect: current.dialect as PlanDialectOf<PlanValue> | DialectOfDialectNumericInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
6011
- }, {
6012
- ...currentAst,
6013
- limit: expression
6014
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>)
6015
- }
5335
+ >
6016
5336
 
6017
- const offset = <Value extends NumericExpressionInput>(
5337
+ type OffsetApi = <Value extends NumericExpressionInput>(
6018
5338
  value: Value
6019
5339
  ) =>
6020
5340
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
6021
5341
  plan: PlanValue & RequireSelectStatement<PlanValue>
6022
- ): QueryPlan<
5342
+ ) => QueryPlan<
6023
5343
  SelectionOfPlan<PlanValue>,
6024
5344
  AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, DialectAsNumericExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
6025
5345
  AvailableOfPlan<PlanValue>,
@@ -6030,30 +5350,114 @@ type AsCurriedResult<
6030
5350
  AssumptionsOfPlan<PlanValue>,
6031
5351
  CapabilitiesOfPlan<PlanValue>,
6032
5352
  StatementOfPlan<PlanValue>
5353
+ >
5354
+
5355
+ const {
5356
+ buildSetOperation,
5357
+ where,
5358
+ from,
5359
+ having,
5360
+ crossJoin,
5361
+ join,
5362
+ orderBy,
5363
+ lock,
5364
+ distinct,
5365
+ limit,
5366
+ offset
5367
+ } = makeDslPlanRuntime({
5368
+ profile,
5369
+ makePlan,
5370
+ getAst,
5371
+ getQueryState,
5372
+ currentRequiredList,
5373
+ toDialectExpression,
5374
+ toDialectNumericExpression,
5375
+ extractRequiredFromDialectInputRuntime,
5376
+ extractRequiredFromDialectNumericInputRuntime,
5377
+ formulaOfExpressionRuntime,
5378
+ assumeFormulaTrue,
5379
+ trueFormula,
5380
+ sourceDetails,
5381
+ presenceWitnessesOfSourceLike,
5382
+ attachInsertSource: (...args) => attachInsertSource(args[0], args[1])
5383
+ }) as {
5384
+ readonly buildSetOperation: (kind: QueryAst.SetOperatorKind, all: boolean, left: any, right: any) => any
5385
+ readonly where: WhereApi
5386
+ readonly from: FromApi
5387
+ readonly having: HavingApi
5388
+ readonly crossJoin: CrossJoinApi
5389
+ readonly join: JoinApi
5390
+ readonly orderBy: OrderByApi
5391
+ readonly lock: LockApi
5392
+ readonly distinct: DistinctApi
5393
+ readonly limit: LimitApi
5394
+ readonly offset: OffsetApi
5395
+ }
5396
+
5397
+ const union = ((left, right) =>
5398
+ buildSetOperation("union", false, left as never, right as never)) as SetOperationApi
5399
+
5400
+ const unionAll = ((left, right) =>
5401
+ buildSetOperation("union", true, left as never, right as never)) as SetOperationApi
5402
+
5403
+ const intersect = ((left, right) =>
5404
+ buildSetOperation("intersect", false, left as never, right as never)) as SetOperationApi
5405
+
5406
+ const intersectAll = ((left, right) =>
5407
+ buildSetOperation("intersect", true, left as never, right as never)) as SetOperationApi
5408
+
5409
+ const except = ((left, right) =>
5410
+ buildSetOperation("except", false, left as never, right as never)) as SetOperationApi
5411
+
5412
+ const exceptAll = ((left, right) =>
5413
+ buildSetOperation("except", true, left as never, right as never)) as SetOperationApi
5414
+
5415
+ const innerJoin = ((table, on) => (join as any)("inner", table, on)) as BinaryJoinApi<"inner">
5416
+
5417
+ const leftJoin = ((table, on) => (join as any)("left", table, on)) as BinaryJoinApi<"left">
5418
+
5419
+ const rightJoin = ((table, on) => (join as any)("right", table, on)) as BinaryJoinApi<"right">
5420
+
5421
+ const fullJoin = ((table, on) => (join as any)("full", table, on)) as BinaryJoinApi<"full">
5422
+
5423
+ const distinctOn = ((...values: readonly ExpressionInput[]) => {
5424
+ const expressions = values.map((value) => toDialectExpression(value)) as Expression.Any[]
5425
+ return <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5426
+ plan: PlanValue & RequireSelectStatement<PlanValue>
5427
+ ): QueryPlan<
5428
+ SelectionOfPlan<PlanValue>,
5429
+ RequiredOfPlan<PlanValue>,
5430
+ AvailableOfPlan<PlanValue>,
5431
+ PlanDialectOf<PlanValue>,
5432
+ GroupedOfPlan<PlanValue>,
5433
+ ScopedNamesOfPlan<PlanValue>,
5434
+ OutstandingOfPlan<PlanValue>,
5435
+ AssumptionsOfPlan<PlanValue>,
5436
+ CapabilitiesOfPlan<PlanValue>,
5437
+ StatementOfPlan<PlanValue>
6033
5438
  > => {
6034
5439
  const current = plan[Plan.TypeId]
6035
5440
  const currentAst = getAst(plan)
6036
5441
  const currentQuery = getQueryState(plan)
6037
- const expression = toDialectNumericExpression(value)
6038
- const required = extractRequiredFromDialectNumericInputRuntime(value)
6039
5442
  return makePlan({
6040
5443
  selection: current.selection,
6041
- required: [...currentRequiredList(current.required), ...required].filter((name, index, values) =>
6042
- !(name in current.available) && values.indexOf(name) === index) as AddExpressionRequired<RequiredOfPlan<PlanValue>, AvailableOfPlan<PlanValue>, DialectAsNumericExpression<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>>,
5444
+ required: current.required as RequiredOfPlan<PlanValue>,
6043
5445
  available: current.available,
6044
- dialect: current.dialect as PlanDialectOf<PlanValue> | DialectOfDialectNumericInput<Value, Dialect, TextDb, NumericDb, BoolDb, TimestampDb, NullDb>
5446
+ dialect: current.dialect as PlanDialectOf<PlanValue>
6045
5447
  }, {
6046
5448
  ...currentAst,
6047
- offset: expression
5449
+ distinct: true,
5450
+ distinctOn: expressions
6048
5451
  }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>)
6049
5452
  }
5453
+ }) as DistinctOnApi<Dialect>
6050
5454
 
6051
- const groupBy = <Values extends readonly [GroupByInput, ...GroupByInput[]]>(
5455
+ type GroupByApi = <Values extends readonly [GroupByInput, ...GroupByInput[]]>(
6052
5456
  ...values: Values
6053
5457
  ) =>
6054
5458
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
6055
5459
  plan: PlanValue & RequireSelectStatement<PlanValue>
6056
- ): QueryPlan<
5460
+ ) => QueryPlan<
6057
5461
  SelectionOfPlan<PlanValue>,
6058
5462
  Exclude<RequiredOfPlan<PlanValue> | RequiredFromDependencies<TupleDependencies<Values>>, AvailableNames<AvailableOfPlan<PlanValue>>>,
6059
5463
  AvailableOfPlan<PlanValue>,
@@ -6064,30 +5468,14 @@ type AsCurriedResult<
6064
5468
  AssumptionsOfPlan<PlanValue>,
6065
5469
  CapabilitiesOfPlan<PlanValue>,
6066
5470
  StatementOfPlan<PlanValue>
6067
- > => {
6068
- const current = plan[Plan.TypeId]
6069
- const currentAst = getAst(plan)
6070
- const currentQuery = getQueryState(plan)
6071
- const required = [...values.flatMap((value) => Object.keys(value[Expression.TypeId].dependencies))].filter((name, index, list) =>
6072
- !(name in current.available) && list.indexOf(name) === index)
6073
- return makePlan({
6074
- selection: current.selection,
6075
- required: [...currentRequiredList(current.required), ...required].filter((name, index, list) =>
6076
- !(name in current.available) && list.indexOf(name) === index) as Exclude<RequiredOfPlan<PlanValue> | RequiredFromDependencies<TupleDependencies<Values>>, AvailableNames<AvailableOfPlan<PlanValue>>>,
6077
- available: current.available,
6078
- dialect: current.dialect as PlanDialectOf<PlanValue> | TupleDialect<Values>
6079
- }, {
6080
- ...currentAst,
6081
- groupBy: dedupeGroupedExpressions([...currentAst.groupBy, ...values])
6082
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>)
6083
- }
5471
+ >
6084
5472
 
6085
- const returning = <Selection extends SelectionShape>(
5473
+ type ReturningApi = <Selection extends SelectionShape>(
6086
5474
  selection: Selection
6087
5475
  ) =>
6088
5476
  <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
6089
5477
  plan: PlanValue & RequireMutationStatement<PlanValue>
6090
- ): QueryPlan<
5478
+ ) => QueryPlan<
6091
5479
  Selection,
6092
5480
  Exclude<RequiredOfPlan<PlanValue> | ExtractRequired<Selection>, AvailableNames<AvailableOfPlan<PlanValue>>>,
6093
5481
  AvailableOfPlan<PlanValue>,
@@ -6100,169 +5488,264 @@ type AsCurriedResult<
6100
5488
  StatementOfPlan<PlanValue>,
6101
5489
  MutationTargetOfPlan<PlanValue>,
6102
5490
  InsertSourceStateOfPlan<PlanValue>
6103
- > => {
6104
- const current = plan[Plan.TypeId]
6105
- const currentAst = getAst(plan)
6106
- const currentQuery = getQueryState(plan)
6107
- return makePlan({
6108
- selection,
6109
- required: [...currentRequiredList(current.required), ...extractRequiredRuntime(selection)].filter((name, index, list) =>
6110
- !(name in current.available) && list.indexOf(name) === index) as Exclude<RequiredOfPlan<PlanValue> | ExtractRequired<Selection>, AvailableNames<AvailableOfPlan<PlanValue>>>,
6111
- available: current.available,
6112
- dialect: current.dialect as PlanDialectOf<PlanValue> | ExtractDialect<Selection>
6113
- }, {
6114
- ...currentAst,
6115
- select: selection
6116
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>, currentQuery.target, currentQuery.insertSource)
6117
- }
5491
+ >
6118
5492
 
6119
- function insert<
6120
- Target extends MutationTargetLike
5493
+ export interface InsertApi {
5494
+ <Target extends MutationTargetLike>(
5495
+ target: Target
5496
+ ): QueryPlan<
5497
+ {},
5498
+ never,
5499
+ AddAvailable<{}, SourceNameOf<Target>>,
5500
+ TableDialectOf<Target>,
5501
+ never,
5502
+ SourceNameOf<Target>,
5503
+ never,
5504
+ TrueFormula,
5505
+ "write",
5506
+ "insert",
5507
+ Target,
5508
+ "missing"
5509
+ >
5510
+ <Target extends MutationTargetLike, Values extends Record<string, unknown>>(
5511
+ target: Target,
5512
+ values: MutationValuesInput<"insert", Target, Values>
5513
+ ): QueryPlan<
5514
+ {},
5515
+ Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
5516
+ AddAvailable<{}, SourceNameOf<Target>>,
5517
+ TableDialectOf<Target>,
5518
+ never,
5519
+ SourceNameOf<Target>,
5520
+ Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
5521
+ TrueFormula,
5522
+ "write",
5523
+ "insert",
5524
+ Target,
5525
+ "ready"
5526
+ >
5527
+ }
5528
+
5529
+ type AttachInsertSourceApi = (
5530
+ plan: QueryPlan<any, any, any, any, any, any, any, any, any, "insert", MutationTargetLike, "missing">,
5531
+ source: AnyValuesInput | AnyValuesSource | AnyUnnestSource | CompletePlan<QueryPlan<any, any, any, any, any, any, any, any, any, any>>
5532
+ ) => QueryPlan<any, any, any, any, any, any, any, any, any, "insert", MutationTargetLike, "ready">
5533
+
5534
+ type OnConflictApi = <
5535
+ Target extends MutationTargetLike,
5536
+ const Columns extends DdlColumnInput,
5537
+ UpdateValues extends MutationInputOf<Table.UpdateOf<Target>> | undefined = MutationInputOf<Table.UpdateOf<Target>> | undefined,
5538
+ Options extends ConflictActionInput<Target, Dialect, UpdateValues> = ConflictActionInput<Target, Dialect, UpdateValues>
6121
5539
  >(
6122
- target: Target
6123
- ): QueryPlan<
5540
+ target: ConflictTargetInput<Target, Dialect, Columns>,
5541
+ options?: Options
5542
+ ) =>
5543
+ <PlanValue extends QueryPlan<any, any, any, any, any, any, any, any, any, any>>(
5544
+ plan: PlanValue & RequireInsertStatement<PlanValue>
5545
+ ) => QueryPlan<
5546
+ SelectionOfPlan<PlanValue>,
5547
+ Exclude<RequiredOfPlan<PlanValue> | MutationRequiredFromValues<Exclude<UpdateValues, undefined>> | RequiredFromInput<Extract<Options["where"], PredicateInput>>, AvailableNames<AvailableOfPlan<PlanValue>>>,
5548
+ AvailableOfPlan<PlanValue>,
5549
+ PlanDialectOf<PlanValue>,
5550
+ GroupedOfPlan<PlanValue>,
5551
+ ScopedNamesOfPlan<PlanValue>,
5552
+ Exclude<OutstandingOfPlan<PlanValue> | MutationRequiredFromValues<Exclude<UpdateValues, undefined>> | RequiredFromInput<Extract<Options["where"], PredicateInput>>, AvailableNames<AvailableOfPlan<PlanValue>>>,
5553
+ AssumptionsOfPlan<PlanValue>,
5554
+ CapabilitiesOfPlan<PlanValue>,
5555
+ StatementOfPlan<PlanValue>,
5556
+ MutationTargetOfPlan<PlanValue>,
5557
+ InsertSourceStateOfPlan<PlanValue>
5558
+ >
5559
+
5560
+ interface UpdateApi {
5561
+ <Targets extends MutationTargetTuple, Values extends UpdateInputOfTarget<Targets>>(
5562
+ target: Dialect extends "mysql" ? Targets : never,
5563
+ values: Values
5564
+ ): QueryPlan<
5565
+ {},
5566
+ Exclude<NestedMutationRequiredFromValues<Values>, MutationTargetNamesOf<Targets>>,
5567
+ AddAvailableMany<{}, MutationTargetNamesOf<Targets>>,
5568
+ TableDialectOf<Targets[0]>,
5569
+ never,
5570
+ MutationTargetNamesOf<Targets>,
5571
+ Exclude<NestedMutationRequiredFromValues<Values>, MutationTargetNamesOf<Targets>>,
5572
+ TrueFormula,
5573
+ "write",
5574
+ "update"
5575
+ >
5576
+ <Target extends MutationTargetLike, Values extends Record<string, unknown>>(
5577
+ target: Target,
5578
+ values: MutationValuesInput<"update", Target, Values>
5579
+ ): QueryPlan<
5580
+ {},
5581
+ Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
5582
+ AddAvailable<{}, SourceNameOf<Target>>,
5583
+ TableDialectOf<Target>,
5584
+ never,
5585
+ SourceNameOf<Target>,
5586
+ Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
5587
+ TrueFormula,
5588
+ "write",
5589
+ "update"
5590
+ >
5591
+ }
5592
+
5593
+ type UpsertApi = <
5594
+ Target extends MutationTargetLike,
5595
+ Values extends MutationInputOf<Table.InsertOf<Target>>,
5596
+ const Columns extends DdlColumnInput,
5597
+ UpdateValues extends MutationInputOf<Table.UpdateOf<Target>>
5598
+ >(
5599
+ target: Target,
5600
+ values: Values,
5601
+ conflictColumns: ValidateTargetColumns<Target, NormalizeDdlColumns<Columns>>,
5602
+ updateValues?: UpdateValues
5603
+ ) => QueryPlan<
6124
5604
  {},
6125
- never,
5605
+ Exclude<MutationRequiredFromValues<Values> | MutationRequiredFromValues<UpdateValues>, SourceNameOf<Target>>,
6126
5606
  AddAvailable<{}, SourceNameOf<Target>>,
6127
5607
  TableDialectOf<Target>,
6128
5608
  never,
6129
5609
  SourceNameOf<Target>,
6130
- never,
5610
+ Exclude<MutationRequiredFromValues<Values> | MutationRequiredFromValues<UpdateValues>, SourceNameOf<Target>>,
6131
5611
  TrueFormula,
6132
5612
  "write",
6133
5613
  "insert",
6134
5614
  Target,
6135
- "missing"
5615
+ "ready"
5616
+ >
5617
+
5618
+ interface DeleteApi {
5619
+ <Target extends MutationTargetLike>(
5620
+ target: Target
5621
+ ): QueryPlan<
5622
+ {},
5623
+ never,
5624
+ AddAvailable<{}, SourceNameOf<Target>>,
5625
+ TableDialectOf<Target>,
5626
+ never,
5627
+ SourceNameOf<Target>,
5628
+ never,
5629
+ TrueFormula,
5630
+ "write",
5631
+ "delete"
5632
+ >
5633
+ <Targets extends MutationTargetTuple>(
5634
+ target: Dialect extends "mysql" ? Targets : never
5635
+ ): QueryPlan<
5636
+ {},
5637
+ never,
5638
+ AddAvailableMany<{}, MutationTargetNamesOf<Targets>>,
5639
+ TableDialectOf<Targets[0]>,
5640
+ never,
5641
+ MutationTargetNamesOf<Targets>,
5642
+ never,
5643
+ TrueFormula,
5644
+ "write",
5645
+ "delete"
5646
+ >
5647
+ }
5648
+
5649
+ type TruncateApi = <Target extends MutationTargetLike>(
5650
+ target: Target,
5651
+ options?: TruncateOptions
5652
+ ) => QueryPlan<
5653
+ {},
5654
+ never,
5655
+ {},
5656
+ TableDialectOf<Target>,
5657
+ never,
5658
+ never,
5659
+ never,
5660
+ TrueFormula,
5661
+ "write",
5662
+ "truncate"
6136
5663
  >
6137
- function insert<
5664
+
5665
+ type MergeApi = <
6138
5666
  Target extends MutationTargetLike,
6139
- Values extends Record<string, unknown>
5667
+ Source extends SourceLike,
5668
+ On extends PredicateInput,
5669
+ MatchedValues extends MutationInputOf<Table.UpdateOf<Target>> = MutationInputOf<Table.UpdateOf<Target>>,
5670
+ InsertValues extends MutationInputOf<Table.InsertOf<Target>> = MutationInputOf<Table.InsertOf<Target>>,
5671
+ MatchedPredicate extends PredicateInput | undefined = undefined,
5672
+ NotMatchedPredicate extends PredicateInput | undefined = undefined
6140
5673
  >(
6141
5674
  target: Target,
6142
- values: MutationValuesInput<"insert", Target, Values>
6143
- ): QueryPlan<
5675
+ source: Source & (
5676
+ SourceRequiredOf<Source> extends never ? unknown : SourceRequirementError<Source>
5677
+ ),
5678
+ on: On,
5679
+ options?: MergeOptions<Target, MatchedValues, InsertValues, MatchedPredicate, NotMatchedPredicate>
5680
+ ) => QueryPlan<
6144
5681
  {},
6145
- Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
6146
- AddAvailable<{}, SourceNameOf<Target>>,
6147
- TableDialectOf<Target>,
5682
+ Exclude<
5683
+ AddExpressionRequired<
5684
+ MergeRequiredFromPredicate<
5685
+ MatchedPredicate,
5686
+ AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>
5687
+ > | MergeRequiredFromPredicate<
5688
+ NotMatchedPredicate,
5689
+ AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>
5690
+ > | MutationRequiredFromValues<MatchedValues> | MutationRequiredFromValues<InsertValues>,
5691
+ AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>,
5692
+ On
5693
+ >,
5694
+ SourceNameOf<Target> | SourceNameOf<Source>
5695
+ >,
5696
+ AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>,
5697
+ TableDialectOf<Target> | SourceDialectOf<Source>,
6148
5698
  never,
6149
- SourceNameOf<Target>,
6150
- Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
5699
+ SourceNameOf<Target> | SourceNameOf<Source>,
5700
+ Exclude<
5701
+ AddExpressionRequired<
5702
+ MergeRequiredFromPredicate<
5703
+ MatchedPredicate,
5704
+ AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>
5705
+ > | MergeRequiredFromPredicate<
5706
+ NotMatchedPredicate,
5707
+ AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>
5708
+ > | MutationRequiredFromValues<MatchedValues> | MutationRequiredFromValues<InsertValues>,
5709
+ AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>,
5710
+ On
5711
+ >,
5712
+ SourceNameOf<Target> | SourceNameOf<Source>
5713
+ >,
6151
5714
  TrueFormula,
6152
- "write",
6153
- "insert",
6154
- Target,
6155
- "ready"
5715
+ MergeCapabilities<"write", SourceCapabilitiesOf<Source>>,
5716
+ "merge"
6156
5717
  >
6157
- function insert(
5718
+
5719
+ const mutationRuntime = makeDslMutationRuntime({
5720
+ makePlan,
5721
+ getAst,
5722
+ getQueryState,
5723
+ currentRequiredList,
5724
+ toDialectExpression,
5725
+ buildMutationAssignments,
5726
+ buildInsertValuesRows,
5727
+ normalizeInsertUnnestValues,
5728
+ normalizeInsertSelectColumns,
5729
+ buildConflictTarget,
5730
+ mutationTargetClauses,
5731
+ mutationAvailableSources,
5732
+ normalizeColumnList,
5733
+ targetSourceDetails,
5734
+ sourceDetails
5735
+ })
5736
+
5737
+ const insert: InsertApi = ((
6158
5738
  target: MutationTargetLike,
6159
5739
  values?: Record<string, unknown>
6160
- ): QueryPlan<any, any, any, any, any, any, any, any, any, "insert", MutationTargetLike, "missing" | "ready"> {
6161
- const { sourceName, sourceBaseName } = targetSourceDetails(target)
6162
- const assignments = values === undefined
6163
- ? []
6164
- : buildMutationAssignments(target, values)
6165
- const required = assignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies))
6166
- const insertState = values === undefined ? "missing" : "ready"
6167
- return makePlan({
6168
- selection: {},
6169
- required: required.filter((name, index, list) => name !== sourceName && list.indexOf(name) === index),
6170
- available: {
6171
- [sourceName]: {
6172
- name: sourceName,
6173
- mode: "required",
6174
- baseName: sourceBaseName
6175
- }
6176
- } as AddAvailable<{}, string>,
6177
- dialect: target[Plan.TypeId].dialect
6178
- }, {
6179
- kind: "insert",
6180
- select: {},
6181
- into: {
6182
- kind: "from",
6183
- tableName: sourceName,
6184
- baseTableName: sourceBaseName,
6185
- source: target
6186
- },
6187
- values: assignments,
6188
- conflict: undefined,
6189
- where: [],
6190
- having: [],
6191
- joins: [],
6192
- groupBy: [],
6193
- orderBy: []
6194
- }, undefined as unknown as TrueFormula, "write", "insert", target, insertState)
6195
- }
5740
+ ) => mutationRuntime.insert(target, values)) as InsertApi
6196
5741
 
6197
5742
  const attachInsertSource = (
6198
5743
  plan: QueryPlan<any, any, any, any, any, any, any, any, any, "insert", MutationTargetLike, "missing">,
6199
5744
  source: AnyValuesInput | AnyValuesSource | AnyUnnestSource | CompletePlan<QueryPlan<any, any, any, any, any, any, any, any, any, any>>
6200
- ): QueryPlan<any, any, any, any, any, any, any, any, any, "insert", MutationTargetLike, "ready"> => {
6201
- const current = plan[Plan.TypeId]
6202
- const currentAst = getAst(plan)
6203
- const currentQuery = getQueryState(plan)
6204
- const target = currentQuery.target as MutationTargetLike
6205
- const targetSource = currentAst.into!
6206
- const sourceName = targetSource.tableName
6207
-
6208
- if (typeof source === "object" && source !== null && "kind" in source && source.kind === "values") {
6209
- const valuesSource = source as AnyValuesInput | AnyValuesSource
6210
- const normalized = buildInsertValuesRows(target, valuesSource.rows as readonly [InsertRowInput<MutationTargetLike>, ...InsertRowInput<MutationTargetLike>[]])
6211
- return makePlan({
6212
- selection: current.selection,
6213
- required: normalized.required.filter((name) => name !== sourceName),
6214
- available: current.available,
6215
- dialect: current.dialect
6216
- }, {
6217
- ...currentAst,
6218
- values: [],
6219
- insertSource: {
6220
- kind: "values",
6221
- columns: normalized.columns,
6222
- rows: normalized.rows
6223
- }
6224
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement, currentQuery.target, "ready")
6225
- }
6226
-
6227
- if (typeof source === "object" && source !== null && "kind" in source && source.kind === "unnest") {
6228
- const unnestSource = source as AnyUnnestSource
6229
- const normalized = normalizeInsertUnnestValues(target, unnestSource.values as any)
6230
- return makePlan({
6231
- selection: current.selection,
6232
- required: [] as never,
6233
- available: current.available,
6234
- dialect: current.dialect
6235
- }, {
6236
- ...currentAst,
6237
- values: [],
6238
- insertSource: {
6239
- kind: "unnest",
6240
- columns: normalized.columns,
6241
- values: normalized.values
6242
- }
6243
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement, currentQuery.target, "ready")
6244
- }
6245
-
6246
- const sourcePlan = source as CompletePlan<QueryPlan<any, any, any, any, any, any, any, any, any, any>>
6247
- const selection = sourcePlan[Plan.TypeId].selection as Record<string, Expression.Any>
6248
- const columns = normalizeInsertSelectColumns(selection)
6249
- return makePlan({
6250
- selection: current.selection,
6251
- required: currentRequiredList(sourcePlan[Plan.TypeId].required).filter((name) => name !== sourceName),
6252
- available: current.available,
6253
- dialect: current.dialect
6254
- }, {
6255
- ...currentAst,
6256
- values: [],
6257
- insertSource: {
6258
- kind: "query",
6259
- columns,
6260
- query: sourcePlan
6261
- }
6262
- }, currentQuery.assumptions, currentQuery.capabilities as MergeCapabilities<typeof currentQuery.capabilities, CapabilitiesOfPlan<typeof sourcePlan>>, currentQuery.statement, currentQuery.target, "ready")
6263
- }
5745
+ ): QueryPlan<any, any, any, any, any, any, any, any, any, "insert", MutationTargetLike, "ready"> =>
5746
+ mutationRuntime.attachInsertSource(plan, source)
6264
5747
 
6265
- const onConflict = <
5748
+ const onConflict: OnConflictApi = <
6266
5749
  Target extends MutationTargetLike,
6267
5750
  const Columns extends DdlColumnInput,
6268
5751
  UpdateValues extends MutationInputOf<Table.UpdateOf<Target>> | undefined = MutationInputOf<Table.UpdateOf<Target>> | undefined,
@@ -6286,108 +5769,14 @@ type AsCurriedResult<
6286
5769
  StatementOfPlan<PlanValue>,
6287
5770
  MutationTargetOfPlan<PlanValue>,
6288
5771
  InsertSourceStateOfPlan<PlanValue>
6289
- > => {
6290
- const current = plan[Plan.TypeId]
6291
- const currentAst = getAst(plan)
6292
- const currentQuery = getQueryState(plan)
6293
- const insertTarget = currentAst.into!.source as Target
6294
- const conflictTarget = buildConflictTarget(insertTarget, target as readonly string[] | { readonly columns: readonly string[]; readonly where?: PredicateInput } | { readonly constraint: string })
6295
- const updateAssignments = options.update
6296
- ? buildMutationAssignments(insertTarget, options.update)
6297
- : []
6298
- const updateWhere = options.where === undefined
6299
- ? undefined
6300
- : toDialectExpression(options.where as PredicateInput)
6301
- const required = [
6302
- ...currentRequiredList(current.required),
6303
- ...updateAssignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies)),
6304
- ...(updateWhere ? Object.keys(updateWhere[Expression.TypeId].dependencies) : [])
6305
- ].filter((name, index, list) =>
6306
- !(name in current.available) && list.indexOf(name) === index)
6307
- return makePlan({
6308
- selection: current.selection,
6309
- required: required as Exclude<RequiredOfPlan<PlanValue> | MutationRequiredFromValues<Exclude<UpdateValues, undefined>> | RequiredFromInput<Extract<Options["where"], PredicateInput>>, AvailableNames<AvailableOfPlan<PlanValue>>>,
6310
- available: current.available,
6311
- dialect: current.dialect as PlanDialectOf<PlanValue>
6312
- }, {
6313
- ...currentAst,
6314
- conflict: {
6315
- kind: "conflict",
6316
- target: conflictTarget,
6317
- action: updateAssignments.length === 0 ? "doNothing" : "doUpdate",
6318
- values: updateAssignments.length === 0 ? undefined : updateAssignments,
6319
- where: updateWhere
6320
- }
6321
- }, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement as StatementOfPlan<PlanValue>, currentQuery.target, currentQuery.insertSource)
6322
- }
5772
+ > => mutationRuntime.onConflict(target, options)(plan)
6323
5773
 
6324
- function update<
6325
- Targets extends MutationTargetTuple,
6326
- Values extends UpdateInputOfTarget<Targets>
6327
- >(
6328
- target: Dialect extends "mysql" ? Targets : never,
6329
- values: Values
6330
- ): QueryPlan<
6331
- {},
6332
- Exclude<NestedMutationRequiredFromValues<Values>, MutationTargetNamesOf<Targets>>,
6333
- AddAvailableMany<{}, MutationTargetNamesOf<Targets>>,
6334
- TableDialectOf<Targets[0]>,
6335
- never,
6336
- MutationTargetNamesOf<Targets>,
6337
- Exclude<NestedMutationRequiredFromValues<Values>, MutationTargetNamesOf<Targets>>,
6338
- TrueFormula,
6339
- "write",
6340
- "update"
6341
- >
6342
- function update<
6343
- Target extends MutationTargetLike,
6344
- Values extends Record<string, unknown>
6345
- >(
6346
- target: Target,
6347
- values: MutationValuesInput<"update", Target, Values>
6348
- ): QueryPlan<
6349
- {},
6350
- Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
6351
- AddAvailable<{}, SourceNameOf<Target>>,
6352
- TableDialectOf<Target>,
6353
- never,
6354
- SourceNameOf<Target>,
6355
- Exclude<MutationRequiredFromValues<Values>, SourceNameOf<Target>>,
6356
- TrueFormula,
6357
- "write",
6358
- "update"
6359
- >
6360
- function update(
5774
+ const update: UpdateApi = ((
6361
5775
  target: MutationTargetInput,
6362
5776
  values: Record<string, unknown>
6363
- ): QueryPlan<any, any, any, any, any, any, any, any, any, "update"> {
6364
- const targets = mutationTargetClauses(target)
6365
- const primaryTarget = targets[0]!
6366
- const assignments = buildMutationAssignments(target, values)
6367
- const targetNames = new Set(targets.map((entry) => entry.tableName))
6368
- const required = assignments
6369
- .flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies))
6370
- .filter((name, index, list) => !targetNames.has(name) && list.indexOf(name) === index)
6371
- return makePlan({
6372
- selection: {},
6373
- required,
6374
- available: mutationAvailableSources(target),
6375
- dialect: (primaryTarget.source as MutationTargetLike)[Plan.TypeId].dialect
6376
- }, {
6377
- kind: "update",
6378
- select: {},
6379
- target: primaryTarget,
6380
- targets,
6381
- set: assignments,
6382
- where: [],
6383
- having: [],
6384
- joins: [],
6385
- groupBy: [],
6386
- orderBy: []
6387
- }, undefined as unknown as TrueFormula, "write", "update")
6388
- }
5777
+ ) => mutationRuntime.update(target, values)) as UpdateApi
6389
5778
 
6390
- const upsert = <
5779
+ const upsert: UpsertApi = <
6391
5780
  Target extends MutationTargetLike,
6392
5781
  Values extends MutationInputOf<Table.InsertOf<Target>>,
6393
5782
  const Columns extends DdlColumnInput,
@@ -6410,108 +5799,13 @@ type AsCurriedResult<
6410
5799
  "insert",
6411
5800
  Target,
6412
5801
  "ready"
6413
- > => {
6414
- const { sourceName, sourceBaseName } = targetSourceDetails(target)
6415
- const assignments = buildMutationAssignments(target, values)
6416
- const updateAssignments = updateValues ? buildMutationAssignments(target, updateValues) : []
6417
- const required = [
6418
- ...assignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies)),
6419
- ...updateAssignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies))
6420
- ]
6421
- return makePlan({
6422
- selection: {},
6423
- required: required.filter((name, index, list) => name !== sourceName && list.indexOf(name) === index) as unknown as Exclude<MutationRequiredFromValues<Values> | MutationRequiredFromValues<UpdateValues>, SourceNameOf<Target>>,
6424
- available: {
6425
- [sourceName]: {
6426
- name: sourceName,
6427
- mode: "required",
6428
- baseName: sourceBaseName
6429
- }
6430
- } as AddAvailable<{}, SourceNameOf<Target>>,
6431
- dialect: target[Plan.TypeId].dialect as TableDialectOf<Target>
6432
- }, {
6433
- kind: "insert",
6434
- select: {},
6435
- into: {
6436
- kind: "from",
6437
- tableName: sourceName,
6438
- baseTableName: sourceBaseName,
6439
- source: target
6440
- },
6441
- values: assignments,
6442
- conflict: {
6443
- kind: "conflict",
6444
- target: {
6445
- kind: "columns",
6446
- columns: normalizeColumnList(conflictColumns as string | readonly string[]) as readonly [string, ...string[]]
6447
- },
6448
- action: updateAssignments.length > 0 ? "doUpdate" : "doNothing",
6449
- values: updateAssignments.length > 0 ? updateAssignments : undefined
6450
- },
6451
- where: [],
6452
- having: [],
6453
- joins: [],
6454
- groupBy: [],
6455
- orderBy: []
6456
- }, undefined as unknown as TrueFormula, "write", "insert", target, "ready")
6457
- }
5802
+ > => mutationRuntime.upsert(target, values, conflictColumns as string | readonly string[], updateValues)
6458
5803
 
6459
- function delete_<
6460
- Target extends MutationTargetLike
6461
- >(
6462
- target: Target
6463
- ): QueryPlan<
6464
- {},
6465
- never,
6466
- AddAvailable<{}, SourceNameOf<Target>>,
6467
- TableDialectOf<Target>,
6468
- never,
6469
- SourceNameOf<Target>,
6470
- never,
6471
- TrueFormula,
6472
- "write",
6473
- "delete"
6474
- >
6475
- function delete_<
6476
- Targets extends MutationTargetTuple
6477
- >(
6478
- target: Dialect extends "mysql" ? Targets : never
6479
- ): QueryPlan<
6480
- {},
6481
- never,
6482
- AddAvailableMany<{}, MutationTargetNamesOf<Targets>>,
6483
- TableDialectOf<Targets[0]>,
6484
- never,
6485
- MutationTargetNamesOf<Targets>,
6486
- never,
6487
- TrueFormula,
6488
- "write",
6489
- "delete"
6490
- >
6491
- function delete_(
5804
+ const delete_: DeleteApi = ((
6492
5805
  target: MutationTargetInput
6493
- ): QueryPlan<any, any, any, any, any, any, any, any, any, "delete"> {
6494
- const targets = mutationTargetClauses(target)
6495
- const primaryTarget = targets[0]!
6496
- return makePlan({
6497
- selection: {},
6498
- required: [] as never,
6499
- available: mutationAvailableSources(target),
6500
- dialect: (primaryTarget.source as MutationTargetLike)[Plan.TypeId].dialect
6501
- }, {
6502
- kind: "delete",
6503
- select: {},
6504
- target: primaryTarget,
6505
- targets,
6506
- where: [],
6507
- having: [],
6508
- joins: [],
6509
- groupBy: [],
6510
- orderBy: []
6511
- }, undefined as unknown as TrueFormula, "write", "delete")
6512
- }
5806
+ ) => mutationRuntime.delete_(target)) as DeleteApi
6513
5807
 
6514
- const truncate = <
5808
+ const truncate: TruncateApi = <
6515
5809
  Target extends MutationTargetLike
6516
5810
  >(
6517
5811
  target: Target,
@@ -6527,36 +5821,9 @@ type AsCurriedResult<
6527
5821
  TrueFormula,
6528
5822
  "write",
6529
5823
  "truncate"
6530
- > => {
6531
- const { sourceName, sourceBaseName } = targetSourceDetails(target)
6532
- return makePlan({
6533
- selection: {},
6534
- required: [] as never,
6535
- available: {},
6536
- dialect: target[Plan.TypeId].dialect as TableDialectOf<Target>
6537
- }, {
6538
- kind: "truncate",
6539
- select: {},
6540
- target: {
6541
- kind: "from",
6542
- tableName: sourceName,
6543
- baseTableName: sourceBaseName,
6544
- source: target
6545
- },
6546
- truncate: {
6547
- kind: "truncate",
6548
- restartIdentity: options.restartIdentity ?? false,
6549
- cascade: options.cascade ?? false
6550
- },
6551
- where: [],
6552
- having: [],
6553
- joins: [],
6554
- groupBy: [],
6555
- orderBy: []
6556
- }, undefined as unknown as TrueFormula, "write", "truncate")
6557
- }
5824
+ > => mutationRuntime.truncate(target, options)
6558
5825
 
6559
- const merge = <
5826
+ const merge: MergeApi = <
6560
5827
  Target extends MutationTargetLike,
6561
5828
  Source extends SourceLike,
6562
5829
  On extends PredicateInput,
@@ -6608,109 +5875,9 @@ type AsCurriedResult<
6608
5875
  TrueFormula,
6609
5876
  MergeCapabilities<"write", SourceCapabilitiesOf<Source>>,
6610
5877
  "merge"
6611
- > => {
6612
- const { sourceName: targetName, sourceBaseName: targetBaseName } = targetSourceDetails(target)
6613
- const { sourceName: usingName, sourceBaseName: usingBaseName } = sourceDetails(source)
6614
- const onExpression = toDialectExpression(on)
6615
- const matched = options.whenMatched
6616
- const notMatched = options.whenNotMatched
6617
- if (matched && "delete" in matched && "update" in matched) {
6618
- throw new Error("merge whenMatched cannot specify both update and delete")
6619
- }
6620
- const matchedPredicate = matched?.predicate ? toDialectExpression(matched.predicate) : undefined
6621
- const matchedAssignments = matched && "update" in matched && matched.update
6622
- ? buildMutationAssignments(target, matched.update)
6623
- : []
6624
- const notMatchedPredicate = notMatched?.predicate ? toDialectExpression(notMatched.predicate) : undefined
6625
- const notMatchedAssignments = notMatched
6626
- ? buildMutationAssignments(target, notMatched.values)
6627
- : []
6628
- const required = [
6629
- ...Object.keys(onExpression[Expression.TypeId].dependencies),
6630
- ...matchedAssignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies)),
6631
- ...notMatchedAssignments.flatMap((entry) => Object.keys(entry.value[Expression.TypeId].dependencies)),
6632
- ...(matchedPredicate ? Object.keys(matchedPredicate[Expression.TypeId].dependencies) : []),
6633
- ...(notMatchedPredicate ? Object.keys(notMatchedPredicate[Expression.TypeId].dependencies) : [])
6634
- ].filter((name, index, values) =>
6635
- name !== targetName && name !== usingName && values.indexOf(name) === index)
6636
- return makePlan({
6637
- selection: {},
6638
- required: required as unknown as Exclude<
6639
- AddExpressionRequired<
6640
- MergeRequiredFromPredicate<
6641
- MatchedPredicate,
6642
- AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>
6643
- > | MergeRequiredFromPredicate<
6644
- NotMatchedPredicate,
6645
- AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>
6646
- > | MutationRequiredFromValues<MatchedValues> | MutationRequiredFromValues<InsertValues>,
6647
- AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>,
6648
- On
6649
- >,
6650
- SourceNameOf<Target> | SourceNameOf<Source>
6651
- >,
6652
- available: {
6653
- [targetName]: {
6654
- name: targetName,
6655
- mode: "required",
6656
- baseName: targetBaseName
6657
- },
6658
- [usingName]: {
6659
- name: usingName,
6660
- mode: "required",
6661
- baseName: usingBaseName
6662
- }
6663
- } as AddAvailable<AddAvailable<{}, SourceNameOf<Target>>, SourceNameOf<Source>>,
6664
- dialect: target[Plan.TypeId].dialect as TableDialectOf<Target> | SourceDialectOf<Source>
6665
- }, {
6666
- kind: "merge",
6667
- select: {},
6668
- target: {
6669
- kind: "from",
6670
- tableName: targetName,
6671
- baseTableName: targetBaseName,
6672
- source: target
6673
- },
6674
- using: {
6675
- kind: "from",
6676
- tableName: usingName,
6677
- baseTableName: usingBaseName,
6678
- source
6679
- },
6680
- merge: {
6681
- kind: "merge",
6682
- on: onExpression,
6683
- whenMatched: matched
6684
- ? ("delete" in matched && matched.delete
6685
- ? {
6686
- kind: "delete",
6687
- predicate: matchedPredicate
6688
- }
6689
- : {
6690
- kind: "update",
6691
- values: matchedAssignments,
6692
- predicate: matchedPredicate
6693
- })
6694
- : undefined,
6695
- whenNotMatched: notMatched
6696
- ? {
6697
- kind: "insert",
6698
- values: notMatchedAssignments,
6699
- predicate: notMatchedPredicate
6700
- }
6701
- : undefined
6702
- },
6703
- where: [],
6704
- having: [],
6705
- joins: [],
6706
- groupBy: [],
6707
- orderBy: []
6708
- }, undefined as unknown as TrueFormula, "write" as MergeCapabilities<"write", SourceCapabilitiesOf<Source>>, "merge")
6709
- }
5878
+ > => mutationRuntime.merge(target, source, on, options)
6710
5879
 
6711
- const transaction = (
6712
- options: TransactionOptions = {}
6713
- ): QueryPlan<
5880
+ type TransactionApi = (options?: TransactionOptions) => QueryPlan<
6714
5881
  {},
6715
5882
  never,
6716
5883
  {},
@@ -6721,28 +5888,9 @@ type AsCurriedResult<
6721
5888
  TrueFormula,
6722
5889
  "transaction",
6723
5890
  "transaction"
6724
- > =>
6725
- makePlan({
6726
- selection: {},
6727
- required: [] as never,
6728
- available: {},
6729
- dialect: profile.dialect as Dialect
6730
- }, {
6731
- kind: "transaction",
6732
- select: {},
6733
- transaction: {
6734
- kind: "transaction",
6735
- isolationLevel: options.isolationLevel,
6736
- readOnly: options.readOnly
6737
- },
6738
- where: [],
6739
- having: [],
6740
- joins: [],
6741
- groupBy: [],
6742
- orderBy: []
6743
- }, undefined as unknown as TrueFormula, "transaction", "transaction")
6744
-
6745
- const commit = (): QueryPlan<
5891
+ >
5892
+
5893
+ type CommitApi = () => QueryPlan<
6746
5894
  {},
6747
5895
  never,
6748
5896
  {},
@@ -6753,26 +5901,9 @@ type AsCurriedResult<
6753
5901
  TrueFormula,
6754
5902
  "transaction",
6755
5903
  "commit"
6756
- > =>
6757
- makePlan({
6758
- selection: {},
6759
- required: [] as never,
6760
- available: {},
6761
- dialect: profile.dialect as Dialect
6762
- }, {
6763
- kind: "commit",
6764
- select: {},
6765
- transaction: {
6766
- kind: "commit"
6767
- },
6768
- where: [],
6769
- having: [],
6770
- joins: [],
6771
- groupBy: [],
6772
- orderBy: []
6773
- }, undefined as unknown as TrueFormula, "transaction", "commit")
6774
-
6775
- const rollback = (): QueryPlan<
5904
+ >
5905
+
5906
+ type RollbackApi = () => QueryPlan<
6776
5907
  {},
6777
5908
  never,
6778
5909
  {},
@@ -6783,28 +5914,9 @@ type AsCurriedResult<
6783
5914
  TrueFormula,
6784
5915
  "transaction",
6785
5916
  "rollback"
6786
- > =>
6787
- makePlan({
6788
- selection: {},
6789
- required: [] as never,
6790
- available: {},
6791
- dialect: profile.dialect as Dialect
6792
- }, {
6793
- kind: "rollback",
6794
- select: {},
6795
- transaction: {
6796
- kind: "rollback"
6797
- },
6798
- where: [],
6799
- having: [],
6800
- joins: [],
6801
- groupBy: [],
6802
- orderBy: []
6803
- }, undefined as unknown as TrueFormula, "transaction", "rollback")
6804
-
6805
- const savepoint = <Name extends string>(
6806
- name: Name
6807
- ): QueryPlan<
5917
+ >
5918
+
5919
+ type SavepointApi = <Name extends string>(name: Name) => QueryPlan<
6808
5920
  {},
6809
5921
  never,
6810
5922
  {},
@@ -6815,29 +5927,9 @@ type AsCurriedResult<
6815
5927
  TrueFormula,
6816
5928
  "transaction",
6817
5929
  "savepoint"
6818
- > =>
6819
- makePlan({
6820
- selection: {},
6821
- required: [] as never,
6822
- available: {},
6823
- dialect: profile.dialect as Dialect
6824
- }, {
6825
- kind: "savepoint",
6826
- select: {},
6827
- transaction: {
6828
- kind: "savepoint",
6829
- name
6830
- },
6831
- where: [],
6832
- having: [],
6833
- joins: [],
6834
- groupBy: [],
6835
- orderBy: []
6836
- }, undefined as unknown as TrueFormula, "transaction", "savepoint")
6837
-
6838
- const rollbackTo = <Name extends string>(
6839
- name: Name
6840
- ): QueryPlan<
5930
+ >
5931
+
5932
+ type RollbackToApi = <Name extends string>(name: Name) => QueryPlan<
6841
5933
  {},
6842
5934
  never,
6843
5935
  {},
@@ -6848,29 +5940,9 @@ type AsCurriedResult<
6848
5940
  TrueFormula,
6849
5941
  "transaction",
6850
5942
  "rollbackTo"
6851
- > =>
6852
- makePlan({
6853
- selection: {},
6854
- required: [] as never,
6855
- available: {},
6856
- dialect: profile.dialect as Dialect
6857
- }, {
6858
- kind: "rollbackTo",
6859
- select: {},
6860
- transaction: {
6861
- kind: "rollbackTo",
6862
- name
6863
- },
6864
- where: [],
6865
- having: [],
6866
- joins: [],
6867
- groupBy: [],
6868
- orderBy: []
6869
- }, undefined as unknown as TrueFormula, "transaction", "rollbackTo")
6870
-
6871
- const releaseSavepoint = <Name extends string>(
6872
- name: Name
6873
- ): QueryPlan<
5943
+ >
5944
+
5945
+ type ReleaseSavepointApi = <Name extends string>(name: Name) => QueryPlan<
6874
5946
  {},
6875
5947
  never,
6876
5948
  {},
@@ -6881,32 +5953,12 @@ type AsCurriedResult<
6881
5953
  TrueFormula,
6882
5954
  "transaction",
6883
5955
  "releaseSavepoint"
6884
- > =>
6885
- makePlan({
6886
- selection: {},
6887
- required: [] as never,
6888
- available: {},
6889
- dialect: profile.dialect as Dialect
6890
- }, {
6891
- kind: "releaseSavepoint",
6892
- select: {},
6893
- transaction: {
6894
- kind: "releaseSavepoint",
6895
- name
6896
- },
6897
- where: [],
6898
- having: [],
6899
- joins: [],
6900
- groupBy: [],
6901
- orderBy: []
6902
- }, undefined as unknown as TrueFormula, "transaction", "releaseSavepoint")
6903
-
6904
- const createTable = <
6905
- Target extends SchemaTableLike
6906
- >(
5956
+ >
5957
+
5958
+ type CreateTableApi = <Target extends SchemaTableLike>(
6907
5959
  target: Target,
6908
- options: CreateTableOptions = {}
6909
- ): QueryPlan<
5960
+ options?: CreateTableOptions
5961
+ ) => QueryPlan<
6910
5962
  {},
6911
5963
  never,
6912
5964
  {},
@@ -6917,40 +5969,12 @@ type AsCurriedResult<
6917
5969
  TrueFormula,
6918
5970
  "ddl",
6919
5971
  "createTable"
6920
- > => {
6921
- const { sourceName, sourceBaseName } = targetSourceDetails(target)
6922
- return makePlan({
6923
- selection: {},
6924
- required: [] as never,
6925
- available: {},
6926
- dialect: target[Plan.TypeId].dialect as TableDialectOf<Target>
6927
- }, {
6928
- kind: "createTable",
6929
- select: {},
6930
- target: {
6931
- kind: "from",
6932
- tableName: sourceName,
6933
- baseTableName: sourceBaseName,
6934
- source: target
6935
- },
6936
- ddl: {
6937
- kind: "createTable",
6938
- ifNotExists: options.ifNotExists ?? false
6939
- },
6940
- where: [],
6941
- having: [],
6942
- joins: [],
6943
- groupBy: [],
6944
- orderBy: []
6945
- }, undefined as unknown as TrueFormula, "ddl", "createTable")
6946
- }
5972
+ >
6947
5973
 
6948
- const dropTable = <
6949
- Target extends SchemaTableLike
6950
- >(
5974
+ type DropTableApi = <Target extends SchemaTableLike>(
6951
5975
  target: Target,
6952
- options: DropTableOptions = {}
6953
- ): QueryPlan<
5976
+ options?: DropTableOptions
5977
+ ) => QueryPlan<
6954
5978
  {},
6955
5979
  never,
6956
5980
  {},
@@ -6961,42 +5985,13 @@ type AsCurriedResult<
6961
5985
  TrueFormula,
6962
5986
  "ddl",
6963
5987
  "dropTable"
6964
- > => {
6965
- const { sourceName, sourceBaseName } = targetSourceDetails(target)
6966
- return makePlan({
6967
- selection: {},
6968
- required: [] as never,
6969
- available: {},
6970
- dialect: target[Plan.TypeId].dialect as TableDialectOf<Target>
6971
- }, {
6972
- kind: "dropTable",
6973
- select: {},
6974
- target: {
6975
- kind: "from",
6976
- tableName: sourceName,
6977
- baseTableName: sourceBaseName,
6978
- source: target
6979
- },
6980
- ddl: {
6981
- kind: "dropTable",
6982
- ifExists: options.ifExists ?? false
6983
- },
6984
- where: [],
6985
- having: [],
6986
- joins: [],
6987
- groupBy: [],
6988
- orderBy: []
6989
- }, undefined as unknown as TrueFormula, "ddl", "dropTable")
6990
- }
5988
+ >
6991
5989
 
6992
- const createIndex = <
6993
- Target extends SchemaTableLike,
6994
- const Columns extends DdlColumnInput
6995
- >(
5990
+ type CreateIndexApi = <Target extends SchemaTableLike, const Columns extends DdlColumnInput>(
6996
5991
  target: Target,
6997
5992
  columns: Columns & ValidateDdlColumns<Target, NormalizeDdlColumns<Columns>>,
6998
- options: CreateIndexOptions = {}
6999
- ): QueryPlan<
5993
+ options?: CreateIndexOptions
5994
+ ) => QueryPlan<
7000
5995
  {},
7001
5996
  never,
7002
5997
  {},
@@ -7007,46 +6002,13 @@ type AsCurriedResult<
7007
6002
  TrueFormula,
7008
6003
  "ddl",
7009
6004
  "createIndex"
7010
- > => {
7011
- const normalizedColumns = normalizeColumnList(columns as string | readonly string[])
7012
- const { sourceName, sourceBaseName } = targetSourceDetails(target)
7013
- return makePlan({
7014
- selection: {},
7015
- required: [] as never,
7016
- available: {},
7017
- dialect: target[Plan.TypeId].dialect as TableDialectOf<Target>
7018
- }, {
7019
- kind: "createIndex",
7020
- select: {},
7021
- target: {
7022
- kind: "from",
7023
- tableName: sourceName,
7024
- baseTableName: sourceBaseName,
7025
- source: target
7026
- },
7027
- ddl: {
7028
- kind: "createIndex",
7029
- name: options.name ?? defaultIndexName(sourceBaseName, normalizedColumns, options.unique ?? false),
7030
- columns: normalizedColumns,
7031
- unique: options.unique ?? false,
7032
- ifNotExists: options.ifNotExists ?? false
7033
- },
7034
- where: [],
7035
- having: [],
7036
- joins: [],
7037
- groupBy: [],
7038
- orderBy: []
7039
- }, undefined as unknown as TrueFormula, "ddl", "createIndex")
7040
- }
6005
+ >
7041
6006
 
7042
- const dropIndex = <
7043
- Target extends SchemaTableLike,
7044
- const Columns extends DdlColumnInput
7045
- >(
6007
+ type DropIndexApi = <Target extends SchemaTableLike, const Columns extends DdlColumnInput>(
7046
6008
  target: Target,
7047
6009
  columns: Columns & ValidateDdlColumns<Target, NormalizeDdlColumns<Columns>>,
7048
- options: DropIndexOptions = {}
7049
- ): QueryPlan<
6010
+ options?: DropIndexOptions
6011
+ ) => QueryPlan<
7050
6012
  {},
7051
6013
  never,
7052
6014
  {},
@@ -7057,107 +6019,9 @@ type AsCurriedResult<
7057
6019
  TrueFormula,
7058
6020
  "ddl",
7059
6021
  "dropIndex"
7060
- > => {
7061
- const normalizedColumns = normalizeColumnList(columns as string | readonly string[])
7062
- const { sourceName, sourceBaseName } = targetSourceDetails(target)
7063
- return makePlan({
7064
- selection: {},
7065
- required: [] as never,
7066
- available: {},
7067
- dialect: target[Plan.TypeId].dialect as TableDialectOf<Target>
7068
- }, {
7069
- kind: "dropIndex",
7070
- select: {},
7071
- target: {
7072
- kind: "from",
7073
- tableName: sourceName,
7074
- baseTableName: sourceBaseName,
7075
- source: target
7076
- },
7077
- ddl: {
7078
- kind: "dropIndex",
7079
- name: options.name ?? defaultIndexName(sourceBaseName, normalizedColumns, false),
7080
- ifExists: options.ifExists ?? false
7081
- },
7082
- where: [],
7083
- having: [],
7084
- joins: [],
7085
- groupBy: [],
7086
- orderBy: []
7087
- }, undefined as unknown as TrueFormula, "ddl", "dropIndex")
7088
- }
6022
+ >
7089
6023
 
7090
- const api = {
7091
- literal,
7092
- column,
7093
- cast,
7094
- type,
7095
- json,
7096
- jsonb,
7097
- eq,
7098
- neq,
7099
- lt,
7100
- lte,
7101
- gt,
7102
- gte,
7103
- isNull,
7104
- isNotNull,
7105
- upper,
7106
- lower,
7107
- like,
7108
- ilike,
7109
- regexMatch,
7110
- regexIMatch,
7111
- regexNotMatch,
7112
- regexNotIMatch,
7113
- and,
7114
- or,
7115
- not,
7116
- all: all_,
7117
- any: any_,
7118
- case: case_,
7119
- match,
7120
- coalesce,
7121
- call,
7122
- uuidGenerateV4,
7123
- nextVal,
7124
- in: in_,
7125
- notIn,
7126
- between,
7127
- contains,
7128
- containedBy,
7129
- overlaps,
7130
- concat,
7131
- exists,
7132
- over,
7133
- rowNumber,
7134
- rank,
7135
- denseRank,
7136
- count,
7137
- max,
7138
- min,
7139
- isDistinctFrom,
7140
- isNotDistinctFrom,
7141
- excluded,
7142
- as,
7143
- with: with_,
7144
- withRecursive: withRecursive_,
7145
- lateral,
7146
- scalar,
7147
- inSubquery,
7148
- compareAny,
7149
- compareAll,
7150
- values,
7151
- unnest,
7152
- generateSeries,
7153
- returning,
7154
- onConflict,
7155
- insert,
7156
- update,
7157
- upsert,
7158
- delete: delete_,
7159
- truncate,
7160
- merge,
6024
+ const {
7161
6025
  transaction,
7162
6026
  commit,
7163
6027
  rollback,
@@ -7167,30 +6031,162 @@ type AsCurriedResult<
7167
6031
  createTable,
7168
6032
  dropTable,
7169
6033
  createIndex,
7170
- dropIndex,
7171
- union,
7172
- unionAll,
7173
- intersect,
7174
- intersectAll,
7175
- except,
7176
- exceptAll,
6034
+ dropIndex
6035
+ } = makeDslTransactionDdlRuntime({
6036
+ profile,
6037
+ makePlan,
6038
+ targetSourceDetails,
6039
+ normalizeColumnList,
6040
+ defaultIndexName
6041
+ }) as {
6042
+ readonly transaction: TransactionApi
6043
+ readonly commit: CommitApi
6044
+ readonly rollback: RollbackApi
6045
+ readonly savepoint: SavepointApi
6046
+ readonly rollbackTo: RollbackToApi
6047
+ readonly releaseSavepoint: ReleaseSavepointApi
6048
+ readonly createTable: CreateTableApi
6049
+ readonly dropTable: DropTableApi
6050
+ readonly createIndex: CreateIndexApi
6051
+ readonly dropIndex: DropIndexApi
6052
+ }
6053
+
6054
+ const {
6055
+ values: exportedValues,
6056
+ unnest: exportedUnnest,
6057
+ select: exportedSelect,
6058
+ from: exportedFrom,
6059
+ insert: exportedInsert
6060
+ } = {
6061
+ values,
6062
+ unnest,
7177
6063
  select,
7178
- where,
7179
- having,
7180
6064
  from,
7181
- innerJoin,
7182
- leftJoin,
7183
- rightJoin,
7184
- fullJoin,
7185
- crossJoin,
7186
- distinct,
7187
- distinctOn,
7188
- limit,
7189
- offset,
7190
- lock,
7191
- orderBy,
7192
- groupBy
6065
+ insert
6066
+ } as {
6067
+ readonly values: ValuesApi
6068
+ readonly unnest: UnnestApi
6069
+ readonly select: SelectApi
6070
+ readonly from: FromApi
6071
+ readonly insert: InsertApi
7193
6072
  }
7194
6073
 
7195
- return api
7196
- })()
6074
+ export const postgresDsl = {
6075
+ values: exportedValues,
6076
+ unnest: exportedUnnest,
6077
+ select: exportedSelect,
6078
+ from: exportedFrom,
6079
+ insert: exportedInsert
6080
+ } as {
6081
+ readonly values: ValuesApi
6082
+ readonly unnest: UnnestApi
6083
+ readonly select: SelectApi
6084
+ readonly from: FromApi
6085
+ readonly insert: InsertApi
6086
+ }
6087
+
6088
+ export {
6089
+ literal,
6090
+ column,
6091
+ cast,
6092
+ type,
6093
+ json,
6094
+ jsonb,
6095
+ eq,
6096
+ neq,
6097
+ lt,
6098
+ lte,
6099
+ gt,
6100
+ gte,
6101
+ isNull,
6102
+ isNotNull,
6103
+ upper,
6104
+ lower,
6105
+ collate,
6106
+ like,
6107
+ ilike,
6108
+ regexMatch,
6109
+ regexIMatch,
6110
+ regexNotMatch,
6111
+ regexNotIMatch,
6112
+ and,
6113
+ or,
6114
+ not,
6115
+ all_ as all,
6116
+ any_ as any,
6117
+ case_,
6118
+ match,
6119
+ coalesce,
6120
+ call,
6121
+ uuidGenerateV4,
6122
+ nextVal,
6123
+ in_,
6124
+ notIn,
6125
+ between,
6126
+ contains,
6127
+ containedBy,
6128
+ overlaps,
6129
+ concat,
6130
+ exists,
6131
+ over,
6132
+ rowNumber,
6133
+ rank,
6134
+ denseRank,
6135
+ count,
6136
+ max,
6137
+ min,
6138
+ isDistinctFrom,
6139
+ isNotDistinctFrom,
6140
+ excluded,
6141
+ as,
6142
+ with_,
6143
+ withRecursive_ as withRecursive,
6144
+ lateral,
6145
+ scalar,
6146
+ inSubquery,
6147
+ compareAny,
6148
+ compareAll,
6149
+ exportedValues as values,
6150
+ exportedUnnest as unnest,
6151
+ generateSeries,
6152
+ returning,
6153
+ onConflict,
6154
+ exportedInsert as insert,
6155
+ update,
6156
+ upsert,
6157
+ delete_,
6158
+ truncate,
6159
+ merge,
6160
+ transaction,
6161
+ commit,
6162
+ rollback,
6163
+ savepoint,
6164
+ rollbackTo,
6165
+ releaseSavepoint,
6166
+ createTable,
6167
+ dropTable,
6168
+ createIndex,
6169
+ dropIndex,
6170
+ union,
6171
+ unionAll,
6172
+ intersect,
6173
+ intersectAll,
6174
+ except,
6175
+ exceptAll,
6176
+ exportedSelect as select,
6177
+ where,
6178
+ having,
6179
+ exportedFrom as from,
6180
+ innerJoin,
6181
+ leftJoin,
6182
+ rightJoin,
6183
+ fullJoin,
6184
+ crossJoin,
6185
+ distinct,
6186
+ distinctOn,
6187
+ limit,
6188
+ offset,
6189
+ lock,
6190
+ orderBy,
6191
+ groupBy
6192
+ }