@powersync/service-sync-rules 0.29.9 → 0.30.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 (170) hide show
  1. package/dist/BaseSqlDataQuery.d.ts +3 -9
  2. package/dist/BaseSqlDataQuery.js +11 -21
  3. package/dist/BaseSqlDataQuery.js.map +1 -1
  4. package/dist/BucketParameterQuerier.d.ts +42 -9
  5. package/dist/BucketParameterQuerier.js +30 -7
  6. package/dist/BucketParameterQuerier.js.map +1 -1
  7. package/dist/BucketSource.d.ts +94 -25
  8. package/dist/BucketSource.js +67 -0
  9. package/dist/BucketSource.js.map +1 -1
  10. package/dist/ExpressionType.d.ts +4 -2
  11. package/dist/ExpressionType.js.map +1 -1
  12. package/dist/HydratedSyncRules.d.ts +46 -0
  13. package/dist/HydratedSyncRules.js +88 -0
  14. package/dist/HydratedSyncRules.js.map +1 -0
  15. package/dist/HydrationState.d.ts +45 -0
  16. package/dist/HydrationState.js +41 -0
  17. package/dist/HydrationState.js.map +1 -0
  18. package/dist/SqlBucketDescriptor.d.ts +31 -28
  19. package/dist/SqlBucketDescriptor.js +89 -112
  20. package/dist/SqlBucketDescriptor.js.map +1 -1
  21. package/dist/SqlDataQuery.d.ts +4 -4
  22. package/dist/SqlDataQuery.js +5 -6
  23. package/dist/SqlDataQuery.js.map +1 -1
  24. package/dist/SqlParameterQuery.d.ts +17 -9
  25. package/dist/SqlParameterQuery.js +49 -23
  26. package/dist/SqlParameterQuery.js.map +1 -1
  27. package/dist/SqlSyncRules.d.ts +13 -29
  28. package/dist/SqlSyncRules.js +48 -77
  29. package/dist/SqlSyncRules.js.map +1 -1
  30. package/dist/StaticSqlParameterQuery.d.ts +13 -3
  31. package/dist/StaticSqlParameterQuery.js +38 -4
  32. package/dist/StaticSqlParameterQuery.js.map +1 -1
  33. package/dist/TablePattern.d.ts +4 -1
  34. package/dist/TablePattern.js +11 -0
  35. package/dist/TablePattern.js.map +1 -1
  36. package/dist/TableValuedFunctionSqlParameterQuery.d.ts +14 -4
  37. package/dist/TableValuedFunctionSqlParameterQuery.js +41 -7
  38. package/dist/TableValuedFunctionSqlParameterQuery.js.map +1 -1
  39. package/dist/compatibility.d.ts +23 -1
  40. package/dist/compatibility.js +32 -4
  41. package/dist/compatibility.js.map +1 -1
  42. package/dist/compiler/bucket_resolver.d.ts +70 -0
  43. package/dist/compiler/bucket_resolver.js +131 -0
  44. package/dist/compiler/bucket_resolver.js.map +1 -0
  45. package/dist/compiler/compatibility.d.ts +16 -0
  46. package/dist/compiler/compatibility.js +12 -0
  47. package/dist/compiler/compatibility.js.map +1 -0
  48. package/dist/compiler/compiler.d.ts +104 -0
  49. package/dist/compiler/compiler.js +113 -0
  50. package/dist/compiler/compiler.js.map +1 -0
  51. package/dist/compiler/equality.d.ts +99 -0
  52. package/dist/compiler/equality.js +284 -0
  53. package/dist/compiler/equality.js.map +1 -0
  54. package/dist/compiler/expression.d.ts +77 -0
  55. package/dist/compiler/expression.js +122 -0
  56. package/dist/compiler/expression.js.map +1 -0
  57. package/dist/compiler/filter.d.ts +71 -0
  58. package/dist/compiler/filter.js +110 -0
  59. package/dist/compiler/filter.js.map +1 -0
  60. package/dist/compiler/filter_simplifier.d.ts +26 -0
  61. package/dist/compiler/filter_simplifier.js +119 -0
  62. package/dist/compiler/filter_simplifier.js.map +1 -0
  63. package/dist/compiler/ir_to_sync_plan.d.ts +37 -0
  64. package/dist/compiler/ir_to_sync_plan.js +163 -0
  65. package/dist/compiler/ir_to_sync_plan.js.map +1 -0
  66. package/dist/compiler/parser.d.ts +99 -0
  67. package/dist/compiler/parser.js +556 -0
  68. package/dist/compiler/parser.js.map +1 -0
  69. package/dist/compiler/querier_graph.d.ts +42 -0
  70. package/dist/compiler/querier_graph.js +365 -0
  71. package/dist/compiler/querier_graph.js.map +1 -0
  72. package/dist/compiler/rows.d.ts +113 -0
  73. package/dist/compiler/rows.js +156 -0
  74. package/dist/compiler/rows.js.map +1 -0
  75. package/dist/compiler/scope.d.ts +22 -0
  76. package/dist/compiler/scope.js +47 -0
  77. package/dist/compiler/scope.js.map +1 -0
  78. package/dist/compiler/sqlite.d.ts +77 -0
  79. package/dist/compiler/sqlite.js +412 -0
  80. package/dist/compiler/sqlite.js.map +1 -0
  81. package/dist/compiler/table.d.ts +67 -0
  82. package/dist/compiler/table.js +67 -0
  83. package/dist/compiler/table.js.map +1 -0
  84. package/dist/errors.d.ts +4 -2
  85. package/dist/errors.js +16 -1
  86. package/dist/errors.js.map +1 -1
  87. package/dist/events/SqlEventDescriptor.js +1 -1
  88. package/dist/events/SqlEventDescriptor.js.map +1 -1
  89. package/dist/events/SqlEventSourceQuery.d.ts +1 -1
  90. package/dist/events/SqlEventSourceQuery.js +1 -2
  91. package/dist/events/SqlEventSourceQuery.js.map +1 -1
  92. package/dist/index.d.ts +8 -0
  93. package/dist/index.js +8 -0
  94. package/dist/index.js.map +1 -1
  95. package/dist/json_schema.js +5 -1
  96. package/dist/json_schema.js.map +1 -1
  97. package/dist/request_functions.js +1 -1
  98. package/dist/request_functions.js.map +1 -1
  99. package/dist/schema-generators/SchemaGenerator.d.ts +5 -0
  100. package/dist/schema-generators/SchemaGenerator.js +21 -14
  101. package/dist/schema-generators/SchemaGenerator.js.map +1 -1
  102. package/dist/sql_functions.d.ts +2 -1
  103. package/dist/sql_functions.js +1 -1
  104. package/dist/sql_functions.js.map +1 -1
  105. package/dist/streams/filter.d.ts +34 -4
  106. package/dist/streams/filter.js +93 -23
  107. package/dist/streams/filter.js.map +1 -1
  108. package/dist/streams/from_sql.js +2 -5
  109. package/dist/streams/from_sql.js.map +1 -1
  110. package/dist/streams/parameter.d.ts +7 -6
  111. package/dist/streams/stream.d.ts +25 -15
  112. package/dist/streams/stream.js +59 -87
  113. package/dist/streams/stream.js.map +1 -1
  114. package/dist/streams/variant.d.ts +14 -21
  115. package/dist/streams/variant.js +68 -46
  116. package/dist/streams/variant.js.map +1 -1
  117. package/dist/sync_plan/engine/javascript.d.ts +6 -0
  118. package/dist/sync_plan/engine/javascript.js +208 -0
  119. package/dist/sync_plan/engine/javascript.js.map +1 -0
  120. package/dist/sync_plan/engine/scalar_expression_engine.d.ts +48 -0
  121. package/dist/sync_plan/engine/scalar_expression_engine.js +99 -0
  122. package/dist/sync_plan/engine/scalar_expression_engine.js.map +1 -0
  123. package/dist/sync_plan/engine/sqlite.d.ts +12 -0
  124. package/dist/sync_plan/engine/sqlite.js +53 -0
  125. package/dist/sync_plan/engine/sqlite.js.map +1 -0
  126. package/dist/sync_plan/evaluator/bucket_data_source.d.ts +23 -0
  127. package/dist/sync_plan/evaluator/bucket_data_source.js +137 -0
  128. package/dist/sync_plan/evaluator/bucket_data_source.js.map +1 -0
  129. package/dist/sync_plan/evaluator/bucket_source.d.ts +19 -0
  130. package/dist/sync_plan/evaluator/bucket_source.js +145 -0
  131. package/dist/sync_plan/evaluator/bucket_source.js.map +1 -0
  132. package/dist/sync_plan/evaluator/index.d.ts +7 -0
  133. package/dist/sync_plan/evaluator/index.js +26 -0
  134. package/dist/sync_plan/evaluator/index.js.map +1 -0
  135. package/dist/sync_plan/evaluator/parameter_evaluator.d.ts +138 -0
  136. package/dist/sync_plan/evaluator/parameter_evaluator.js +359 -0
  137. package/dist/sync_plan/evaluator/parameter_evaluator.js.map +1 -0
  138. package/dist/sync_plan/evaluator/parameter_index_lookup_creator.d.ts +19 -0
  139. package/dist/sync_plan/evaluator/parameter_index_lookup_creator.js +62 -0
  140. package/dist/sync_plan/evaluator/parameter_index_lookup_creator.js.map +1 -0
  141. package/dist/sync_plan/expression.d.ts +109 -0
  142. package/dist/sync_plan/expression.js +85 -0
  143. package/dist/sync_plan/expression.js.map +1 -0
  144. package/dist/sync_plan/expression_to_sql.d.ts +43 -0
  145. package/dist/sync_plan/expression_to_sql.js +190 -0
  146. package/dist/sync_plan/expression_to_sql.js.map +1 -0
  147. package/dist/sync_plan/expression_visitor.d.ts +57 -0
  148. package/dist/sync_plan/expression_visitor.js +181 -0
  149. package/dist/sync_plan/expression_visitor.js.map +1 -0
  150. package/dist/sync_plan/plan.d.ts +196 -0
  151. package/dist/sync_plan/plan.js +2 -0
  152. package/dist/sync_plan/plan.js.map +1 -0
  153. package/dist/sync_plan/schema_inference.d.ts +15 -0
  154. package/dist/sync_plan/schema_inference.js +121 -0
  155. package/dist/sync_plan/schema_inference.js.map +1 -0
  156. package/dist/sync_plan/serialize.d.ts +82 -0
  157. package/dist/sync_plan/serialize.js +209 -0
  158. package/dist/sync_plan/serialize.js.map +1 -0
  159. package/dist/types/custom_sqlite_value.d.ts +1 -1
  160. package/dist/types/time.d.ts +24 -5
  161. package/dist/types/time.js +67 -12
  162. package/dist/types/time.js.map +1 -1
  163. package/dist/types.d.ts +52 -24
  164. package/dist/types.js +4 -1
  165. package/dist/types.js.map +1 -1
  166. package/dist/utils.d.ts +9 -2
  167. package/dist/utils.js +26 -3
  168. package/dist/utils.js.map +1 -1
  169. package/package.json +4 -3
  170. package/schema/sync_rules.json +9 -0
@@ -0,0 +1,138 @@
1
+ import { ParameterLookupSource } from '../../BucketParameterQuerier.js';
2
+ import { ParameterIndexLookupCreator } from '../../BucketSource.js';
3
+ import { HydrationState } from '../../HydrationState.js';
4
+ import { RequestParameters, SqliteParameterValue, SqliteValue } from '../../types.js';
5
+ import * as plan from '../plan.js';
6
+ import { StreamInput } from './bucket_source.js';
7
+ /**
8
+ * Finds bucket parameters for a given request or subscription.
9
+ *
10
+ * In sync streams, queriers are represented as a DAG structure describing how to get from connection data to bucket
11
+ * parameters.
12
+ *
13
+ * As an example, consider the following stream:
14
+ *
15
+ * ```
16
+ * SELECT projects.* FROM projects
17
+ * INNER JOIN orgs ON orgs.id = projects.org_id
18
+ * WHERE orgs.name = auth.parameter('org')
19
+ * ```
20
+ *
21
+ * This would partition data into a bucket with a single parameter (grouping by `projects.org_id`). It would also
22
+ * prepare a lookup from `orgs.name` to `orgs.id`.
23
+ *
24
+ * The querier for this would have:
25
+ *
26
+ * 1. A single lookup stage with a single {@link plan.ParameterLookup}. That lookup would have an instantiation
27
+ * reflecting `auth.parameter('org')` as a `request` {@link plan.ParameterValue}.
28
+ * 2. A single {@link plan.StreamQuerier.sourceInstantiation}, a `lookup` {@link plan.ParameterValue} referencing the
29
+ * lookup from step 1.
30
+ *
31
+ * On this prepared evaluator, lookup stages and parameter values are tracked as {@link PreparedExpandingLookup}s and
32
+ * {@link PreparedParameterValue}s, respectively. These correspond to their definitions on sync plans, except that:
33
+ *
34
+ * 1. Instead of being a description of the parameter, they're a JavaScript function that can be invoked to compute
35
+ * parameters.
36
+ * 2. After being called once, we can replace them with a cached value. This enables a partial instantiation, and
37
+ * avoids recomputing everything whenever a parameter lookup changes. In the example stream, we would run and cache
38
+ * the outputs of `auth.parameter('org')` for a given connection. This sub-expression would not get re-evaluated
39
+ * when the `org-name` -> `org.id` lookup changes.
40
+ *
41
+ * For queriers that don't use parameter lookups, e.g. for streams like `SELECT * FROM users WHERE id = auth.user_id()`,
42
+ * the partial instantiation based on connection data happens to be a complete instantiation. We use this when building
43
+ * queriers by indicating that no lookups will be used.
44
+ */
45
+ export declare class RequestParameterEvaluators {
46
+ /**
47
+ * Pending lookup stages, or their cached outputs.
48
+ */
49
+ readonly lookupStages: PreparedExpandingLookup[][];
50
+ /**
51
+ * Pending parameter values, or their cached outputs.
52
+ */
53
+ readonly parameterValues: PreparedParameterValue[];
54
+ private constructor();
55
+ /**
56
+ * Returns a copy of this instance.
57
+ *
58
+ * Since resolved values are replaced with their instantiation, we need to use closed evaluators before evaluating
59
+ * them on inputs that might change (like parameter lookups).
60
+ *
61
+ * Static data (like connection parameters) can be resolved sooner, and cloning that partially-instantiated evaluator
62
+ * graph essentially forks it. This allows us to cache connection parameters for the lifetime of the connection
63
+ * instead of re-evaluating them on every parameter lookup change.
64
+ */
65
+ clone(): RequestParameterEvaluators;
66
+ /**
67
+ * Evaluates those lookups and parameter values that be evaluated without looking up parameter indexes.
68
+ *
69
+ * This is also used to determine whether a querier is static - if the partial instantiation depending on request data
70
+ * fully resolves the stream, we don't need to lookup any parameters.
71
+ */
72
+ partiallyInstantiate(input: PartialInstantiationInput): void;
73
+ /**
74
+ * Resolves and caches all lookup stages and parameter values.
75
+ *
76
+ * Because this needs to lookup parameter indexes, it is asynchronous.
77
+ */
78
+ instantiate(input: InstantiationInput): Promise<Generator<SqliteParameterValue[]>>;
79
+ /**
80
+ * Whether these evaluators are known to not result in any buckets, for instance because parameters are instanted to
81
+ * `NULL` values that aren't equal to anything.
82
+ *
83
+ * This is fairly efficient to compute and can be used to short-circuit further evaluation.
84
+ */
85
+ isDefinitelyUninstantiable(): boolean;
86
+ extractFullInstantiation(): SqliteParameterValue[][] | undefined;
87
+ /**
88
+ * Prepares evaluators for a description of parameter values obtained from a compiled querier in the sync plan.
89
+ *
90
+ * @param lookupStages The {@link plan.StreamQuerier.lookupStages} of the querier to compile.
91
+ * @param values The {@link plan.StreamQuerier.sourceInstantiation} of the querier to compile.
92
+ * @param input Access to bucket and parameter sources generated for buckets and parameter lookups referenced by the
93
+ * querier.
94
+ */
95
+ static prepare(lookupStages: plan.ExpandingLookup[][], values: plan.ParameterValue[], input: StreamInput): RequestParameterEvaluators;
96
+ }
97
+ export type PreparedExpandingLookup = {
98
+ type: 'parameter';
99
+ lookup: ParameterIndexLookupCreator;
100
+ instantiation: PreparedParameterValue[];
101
+ } | {
102
+ type: 'table_valued';
103
+ read(request: RequestParameters): SqliteParameterValue[][];
104
+ } | {
105
+ type: 'cached';
106
+ values: SqliteParameterValue[][];
107
+ };
108
+ /**
109
+ * A {@link plan.ParameterValue} that can be evaluated against request parameters.
110
+ *
111
+ * Additionally, this includes the `cached` variant which allows partially instantiating parameters.
112
+ */
113
+ export type PreparedParameterValue = {
114
+ type: 'request';
115
+ read(request: RequestParameters): SqliteValue;
116
+ } | {
117
+ type: 'lookup';
118
+ lookup: {
119
+ stage: number;
120
+ index: number;
121
+ };
122
+ resultIndex: number;
123
+ } | {
124
+ type: 'intersection';
125
+ values: PreparedParameterValue[];
126
+ } | {
127
+ type: 'cached';
128
+ values: SqliteParameterValue[];
129
+ };
130
+ export interface PartialInstantiationInput {
131
+ request: RequestParameters;
132
+ }
133
+ export interface InstantiationInput extends PartialInstantiationInput {
134
+ hydrationState: HydrationState;
135
+ source: ParameterLookupSource;
136
+ }
137
+ export declare function isValidParameterValueRow(row: SqliteValue[]): row is SqliteParameterValue[];
138
+ export declare function parametersForRequest(parameters: RequestParameters, values: plan.SqlParameterValue[]): string[];
@@ -0,0 +1,359 @@
1
+ import { ScopedParameterLookup, UnscopedParameterLookup } from '../../BucketParameterQuerier.js';
2
+ import { cartesianProduct } from '../../streams/utils.js';
3
+ import { isValidParameterValue } from '../../utils.js';
4
+ import { MapSourceVisitor, visitExpr } from '../expression_visitor.js';
5
+ import { mapExternalDataToInstantiation } from '../engine/scalar_expression_engine.js';
6
+ /**
7
+ * Finds bucket parameters for a given request or subscription.
8
+ *
9
+ * In sync streams, queriers are represented as a DAG structure describing how to get from connection data to bucket
10
+ * parameters.
11
+ *
12
+ * As an example, consider the following stream:
13
+ *
14
+ * ```
15
+ * SELECT projects.* FROM projects
16
+ * INNER JOIN orgs ON orgs.id = projects.org_id
17
+ * WHERE orgs.name = auth.parameter('org')
18
+ * ```
19
+ *
20
+ * This would partition data into a bucket with a single parameter (grouping by `projects.org_id`). It would also
21
+ * prepare a lookup from `orgs.name` to `orgs.id`.
22
+ *
23
+ * The querier for this would have:
24
+ *
25
+ * 1. A single lookup stage with a single {@link plan.ParameterLookup}. That lookup would have an instantiation
26
+ * reflecting `auth.parameter('org')` as a `request` {@link plan.ParameterValue}.
27
+ * 2. A single {@link plan.StreamQuerier.sourceInstantiation}, a `lookup` {@link plan.ParameterValue} referencing the
28
+ * lookup from step 1.
29
+ *
30
+ * On this prepared evaluator, lookup stages and parameter values are tracked as {@link PreparedExpandingLookup}s and
31
+ * {@link PreparedParameterValue}s, respectively. These correspond to their definitions on sync plans, except that:
32
+ *
33
+ * 1. Instead of being a description of the parameter, they're a JavaScript function that can be invoked to compute
34
+ * parameters.
35
+ * 2. After being called once, we can replace them with a cached value. This enables a partial instantiation, and
36
+ * avoids recomputing everything whenever a parameter lookup changes. In the example stream, we would run and cache
37
+ * the outputs of `auth.parameter('org')` for a given connection. This sub-expression would not get re-evaluated
38
+ * when the `org-name` -> `org.id` lookup changes.
39
+ *
40
+ * For queriers that don't use parameter lookups, e.g. for streams like `SELECT * FROM users WHERE id = auth.user_id()`,
41
+ * the partial instantiation based on connection data happens to be a complete instantiation. We use this when building
42
+ * queriers by indicating that no lookups will be used.
43
+ */
44
+ export class RequestParameterEvaluators {
45
+ lookupStages;
46
+ parameterValues;
47
+ constructor(
48
+ /**
49
+ * Pending lookup stages, or their cached outputs.
50
+ */
51
+ lookupStages,
52
+ /**
53
+ * Pending parameter values, or their cached outputs.
54
+ */
55
+ parameterValues) {
56
+ this.lookupStages = lookupStages;
57
+ this.parameterValues = parameterValues;
58
+ }
59
+ /**
60
+ * Returns a copy of this instance.
61
+ *
62
+ * Since resolved values are replaced with their instantiation, we need to use closed evaluators before evaluating
63
+ * them on inputs that might change (like parameter lookups).
64
+ *
65
+ * Static data (like connection parameters) can be resolved sooner, and cloning that partially-instantiated evaluator
66
+ * graph essentially forks it. This allows us to cache connection parameters for the lifetime of the connection
67
+ * instead of re-evaluating them on every parameter lookup change.
68
+ */
69
+ clone() {
70
+ return new RequestParameterEvaluators(this.lookupStages.map((stage) => [...stage]), [...this.parameterValues]);
71
+ }
72
+ /**
73
+ * Evaluates those lookups and parameter values that be evaluated without looking up parameter indexes.
74
+ *
75
+ * This is also used to determine whether a querier is static - if the partial instantiation depending on request data
76
+ * fully resolves the stream, we don't need to lookup any parameters.
77
+ */
78
+ partiallyInstantiate(input) {
79
+ const helper = new PartialInstantiator(input, this);
80
+ this.lookupStages.forEach((stage, stageIndex) => {
81
+ stage.forEach((_, indexInStage) => helper.expandingLookupSync(stageIndex, indexInStage));
82
+ });
83
+ this.parameterValues.forEach((_, i) => helper.parameterSync(this.parameterValues, i));
84
+ }
85
+ /**
86
+ * Resolves and caches all lookup stages and parameter values.
87
+ *
88
+ * Because this needs to lookup parameter indexes, it is asynchronous.
89
+ */
90
+ async instantiate(input) {
91
+ const helper = new FullInstantiator(input, this);
92
+ for (let i = 0; i < this.lookupStages.length; i++) {
93
+ // Within a stage, we can resolve lookups concurrently.
94
+ await Promise.all(this.lookupStages[i].map((_, j) => helper.expandingLookup(i, j)));
95
+ }
96
+ // At this point, all lookups have been resolved and we can synchronously evaluate parameters which might depend on
97
+ // those lookups.
98
+ return helper.resolveInputs(this.parameterValues);
99
+ }
100
+ /**
101
+ * Whether these evaluators are known to not result in any buckets, for instance because parameters are instanted to
102
+ * `NULL` values that aren't equal to anything.
103
+ *
104
+ * This is fairly efficient to compute and can be used to short-circuit further evaluation.
105
+ */
106
+ isDefinitelyUninstantiable() {
107
+ for (const parameter of this.parameterValues) {
108
+ if (parameter.type != 'cached') {
109
+ return false; // Unknown
110
+ }
111
+ if (parameter.values.length === 0) {
112
+ // Missing parameter.
113
+ return true;
114
+ }
115
+ }
116
+ return false;
117
+ }
118
+ extractFullInstantiation() {
119
+ // All lookup stages need to be resolved, even if they're not used in a parameter. The reason is that queries like
120
+ // `WHERE 'static_value' IN (SELECT name FROM users WHERE id = auth.user_id())` are implemented as lookup stages,
121
+ // so we can't ignore them.
122
+ for (const stage of this.lookupStages) {
123
+ for (const element of stage) {
124
+ if (element.type !== 'cached') {
125
+ return undefined;
126
+ }
127
+ }
128
+ }
129
+ // Outer array represents parameters, inner array represents values for a given parameter.
130
+ const parameters = [];
131
+ for (const parameter of this.parameterValues) {
132
+ if (parameter.type !== 'cached') {
133
+ return undefined;
134
+ }
135
+ parameters.push(parameter.values);
136
+ }
137
+ // Transform to array of complete instantiations.
138
+ return [...cartesianProduct(...parameters)];
139
+ }
140
+ /**
141
+ * Prepares evaluators for a description of parameter values obtained from a compiled querier in the sync plan.
142
+ *
143
+ * @param lookupStages The {@link plan.StreamQuerier.lookupStages} of the querier to compile.
144
+ * @param values The {@link plan.StreamQuerier.sourceInstantiation} of the querier to compile.
145
+ * @param input Access to bucket and parameter sources generated for buckets and parameter lookups referenced by the
146
+ * querier.
147
+ */
148
+ static prepare(lookupStages, values, input) {
149
+ const mappedStages = [];
150
+ const lookupToStage = new Map();
151
+ function mapParameterValue(value) {
152
+ if (value.type == 'request') {
153
+ // Prepare an expression evaluating the expression derived from request data.
154
+ const mapper = mapExternalDataToInstantiation();
155
+ const prepared = input.engine.prepareEvaluator({ filters: [], outputs: [mapper.transform(value.expr)] });
156
+ const instantiation = mapper.instantiation;
157
+ return {
158
+ type: 'request',
159
+ read(request) {
160
+ return prepared.evaluate(parametersForRequest(request, instantiation))[0][0];
161
+ }
162
+ };
163
+ }
164
+ else if (value.type == 'lookup') {
165
+ const stagePosition = lookupToStage.get(value.lookup);
166
+ return { type: 'lookup', lookup: stagePosition, resultIndex: value.resultIndex };
167
+ }
168
+ else {
169
+ return { type: 'intersection', values: mapParameterValues(value.values) };
170
+ }
171
+ }
172
+ function mapParameterValues(values) {
173
+ return values.map(mapParameterValue);
174
+ }
175
+ for (const stage of lookupStages) {
176
+ const stageIndex = mappedStages.length;
177
+ const mappedStage = [];
178
+ mappedStages.push(mappedStage);
179
+ for (const lookup of stage) {
180
+ const index = mappedStage.length;
181
+ lookupToStage.set(lookup, { stage: stageIndex, index });
182
+ if (lookup.type == 'parameter') {
183
+ mappedStage.push({
184
+ type: 'parameter',
185
+ lookup: input.preparedLookups.get(lookup.lookup),
186
+ instantiation: mapParameterValues(lookup.instantiation)
187
+ });
188
+ }
189
+ else {
190
+ // Create an expression like SELECT <output> FROM table_valued(<functionInputs>) WHERE <filters>
191
+ const mapInputs = mapExternalDataToInstantiation();
192
+ const fn = {
193
+ name: lookup.functionName,
194
+ inputs: lookup.functionInputs.map((e) => mapInputs.transform(e))
195
+ };
196
+ const mapOutputs = new MapSourceVisitor(({ column }) => ({
197
+ function: fn,
198
+ column
199
+ }));
200
+ const prepared = input.engine.prepareEvaluator({
201
+ tableValuedFunctions: [fn],
202
+ outputs: lookup.outputs.map((e) => visitExpr(mapOutputs, e, null)),
203
+ filters: lookup.filters.map((e) => visitExpr(mapOutputs, e, null))
204
+ });
205
+ mappedStage.push({
206
+ type: 'table_valued',
207
+ read(request) {
208
+ return [
209
+ ...filterParameterRows(prepared.evaluate(parametersForRequest(request, mapInputs.instantiation)))
210
+ ];
211
+ }
212
+ });
213
+ }
214
+ }
215
+ }
216
+ return new RequestParameterEvaluators(mappedStages, mapParameterValues(values));
217
+ }
218
+ }
219
+ class PartialInstantiator {
220
+ input;
221
+ evaluators;
222
+ constructor(input, evaluators) {
223
+ this.input = input;
224
+ this.evaluators = evaluators;
225
+ }
226
+ /**
227
+ * If possible, evaluates an element in an array of parameter values and replaces the parameter with a marker
228
+ * indicating it as cached.
229
+ */
230
+ parameterSync(parent, index) {
231
+ const current = parent[index];
232
+ if (current.type === 'cached') {
233
+ return current.values;
234
+ }
235
+ else if (current.type === 'intersection') {
236
+ let intersection = null;
237
+ for (let i = 0; i < current.values.length; i++) {
238
+ const evaluated = this.parameterSync(current.values, i);
239
+ if (evaluated == null) {
240
+ return undefined; // Can't evaluate sub-parameter
241
+ }
242
+ if (intersection == null) {
243
+ intersection = new Set(evaluated);
244
+ }
245
+ else {
246
+ // TODO: Remove as any once we can use ES2025 in TypeScript
247
+ intersection = intersection.intersection(new Set(evaluated));
248
+ }
249
+ if (intersection.size == 0) {
250
+ // Empty intersection, we don't even need to evaluate the rest.
251
+ break;
252
+ }
253
+ }
254
+ let values = [];
255
+ if (intersection) {
256
+ values.push(...intersection.keys());
257
+ }
258
+ parent[index] = { type: 'cached', values };
259
+ return values;
260
+ }
261
+ else if (current.type === 'lookup') {
262
+ const resolvedLookup = this.expandingLookupSync(current.lookup.stage, current.lookup.index);
263
+ if (resolvedLookup) {
264
+ const values = resolvedLookup.map((row) => row[current.resultIndex]);
265
+ parent[index] = { type: 'cached', values };
266
+ return values;
267
+ }
268
+ }
269
+ else if (current.type === 'request') {
270
+ const value = current.read(this.input.request);
271
+ const values = isValidParameterValue(value) ? [value] : [];
272
+ parent[index] = { type: 'cached', values };
273
+ return values;
274
+ }
275
+ return undefined;
276
+ }
277
+ expandingLookupSync(stage, index) {
278
+ const lookup = this.evaluators.lookupStages[stage][index];
279
+ if (lookup.type == 'table_valued') {
280
+ // We can evaluate this table-valued function already.
281
+ const values = lookup.read(this.input.request);
282
+ this.evaluators.lookupStages[stage][index] = { type: 'cached', values };
283
+ return values;
284
+ }
285
+ else if (lookup.type == 'cached') {
286
+ return lookup.values;
287
+ }
288
+ return undefined;
289
+ }
290
+ }
291
+ class FullInstantiator extends PartialInstantiator {
292
+ *resolveInputs(params) {
293
+ const parameterValues = params.map((_, index) => {
294
+ const cached = this.parameterSync(params, index);
295
+ if (cached == null) {
296
+ // This method is only called for inputs from an earlier stage, which should have been resolved at this point.
297
+ throw new Error('Should have been able to resolve parameter from earlier stage synchronously.');
298
+ }
299
+ return cached;
300
+ });
301
+ yield* cartesianProduct(...parameterValues);
302
+ }
303
+ async expandingLookup(stage, index) {
304
+ const lookup = this.evaluators.lookupStages[stage][index];
305
+ if (lookup.type == 'parameter') {
306
+ const scope = this.input.hydrationState.getParameterIndexLookupScope(lookup.lookup);
307
+ const outputs = await this.input.source.getParameterSets([...this.resolveInputs(lookup.instantiation)].map((instantiation) => ScopedParameterLookup.normalized(scope, UnscopedParameterLookup.normalized(instantiation))));
308
+ // Stream parameters generate an output row like {0: <expr>, 1: <expr>, ...}.
309
+ const values = outputs.map((row) => {
310
+ const length = Object.entries(row).length;
311
+ const asArray = [];
312
+ for (let i = 0; i < length; i++) {
313
+ asArray.push(row[i.toString()]);
314
+ }
315
+ return asArray;
316
+ });
317
+ this.evaluators.lookupStages[stage][index] = { type: 'cached', values };
318
+ return values;
319
+ }
320
+ const other = this.expandingLookupSync(stage, index);
321
+ if (other == null) {
322
+ throw new Error('internal error: Unable to resolve non-parameter lookup synchronously?');
323
+ }
324
+ return other;
325
+ }
326
+ }
327
+ export function isValidParameterValueRow(row) {
328
+ for (const value of row) {
329
+ if (!isValidParameterValue(value)) {
330
+ return false;
331
+ }
332
+ }
333
+ return true;
334
+ }
335
+ export function parametersForRequest(parameters, values) {
336
+ return values.map((v) => {
337
+ if ('request' in v) {
338
+ switch (v.request) {
339
+ case 'auth':
340
+ return parameters.rawTokenPayload;
341
+ case 'subscription':
342
+ return parameters.rawStreamParameters;
343
+ case 'connection':
344
+ return parameters.rawUserParameters;
345
+ }
346
+ }
347
+ else {
348
+ throw new Error('Illegal column reference in request filter');
349
+ }
350
+ });
351
+ }
352
+ function* filterParameterRows(rows) {
353
+ for (const row of rows) {
354
+ if (isValidParameterValueRow(row)) {
355
+ yield row;
356
+ }
357
+ }
358
+ }
359
+ //# sourceMappingURL=parameter_evaluator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parameter_evaluator.js","sourceRoot":"","sources":["../../../src/sync_plan/evaluator/parameter_evaluator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,qBAAqB,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAGxH,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAGvE,OAAO,EACL,8BAA8B,EAG/B,MAAM,uCAAuC,CAAC;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,MAAM,OAAO,0BAA0B;IAK1B;IAIA;IARX;IACE;;OAEG;IACM,YAAyC;IAClD;;OAEG;IACM,eAAyC;QAJzC,iBAAY,GAAZ,YAAY,CAA6B;QAIzC,oBAAe,GAAf,eAAe,CAA0B;IACjD,CAAC;IAEJ;;;;;;;;;OASG;IACH,KAAK;QACH,OAAO,IAAI,0BAA0B,CACnC,IAAI,CAAC,YAAY,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M,GAAG,CAAC;QACZ,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { ParameterIndexLookupCreator } from '../../BucketSource.js';
2
+ import { ParameterLookupScope } from '../../HydrationState.js';
3
+ import * as plan from '../plan.js';
4
+ import { StreamEvaluationContext } from './index.js';
5
+ import { TablePattern } from '../../TablePattern.js';
6
+ import { SourceTableInterface } from '../../SourceTableInterface.js';
7
+ import { SqliteRow, UnscopedEvaluatedParametersResult } from '../../types.js';
8
+ export declare class PreparedParameterIndexLookupCreator implements ParameterIndexLookupCreator {
9
+ private readonly source;
10
+ readonly defaultLookupScope: ParameterLookupScope;
11
+ private readonly evaluator;
12
+ private readonly evaluatorInputs;
13
+ private readonly numberOfOutputs;
14
+ private readonly numberOfParameters;
15
+ constructor(source: plan.StreamParameterIndexLookupCreator, { engine }: StreamEvaluationContext);
16
+ getSourceTables(): Set<TablePattern>;
17
+ evaluateParameterRow(sourceTable: SourceTableInterface, row: SqliteRow): UnscopedEvaluatedParametersResult[];
18
+ tableSyncsParameters(table: SourceTableInterface): boolean;
19
+ }
@@ -0,0 +1,62 @@
1
+ import { mapExternalDataToInstantiation } from '../engine/scalar_expression_engine.js';
2
+ import { isValidParameterValueRow } from './parameter_evaluator.js';
3
+ import { UnscopedParameterLookup } from '../../BucketParameterQuerier.js';
4
+ export class PreparedParameterIndexLookupCreator {
5
+ source;
6
+ defaultLookupScope;
7
+ evaluator;
8
+ evaluatorInputs;
9
+ numberOfOutputs;
10
+ numberOfParameters;
11
+ constructor(source, { engine }) {
12
+ this.source = source;
13
+ this.defaultLookupScope = source.defaultLookupScope;
14
+ const mapExpressions = mapExternalDataToInstantiation();
15
+ const expressions = source.outputs.map((o) => mapExpressions.transform(o));
16
+ this.numberOfOutputs = expressions.length;
17
+ for (const parameter of source.parameters) {
18
+ expressions.push(mapExpressions.transform(parameter.expr));
19
+ }
20
+ this.numberOfParameters = source.parameters.length;
21
+ this.evaluator = engine.prepareEvaluator({
22
+ outputs: expressions,
23
+ filters: source.filters.map((f) => mapExpressions.transform(f))
24
+ });
25
+ this.evaluatorInputs = mapExpressions.instantiation;
26
+ }
27
+ getSourceTables() {
28
+ const set = new Set();
29
+ set.add(this.source.sourceTable);
30
+ return set;
31
+ }
32
+ evaluateParameterRow(sourceTable, row) {
33
+ const results = [];
34
+ if (!this.source.sourceTable.matches(sourceTable)) {
35
+ return results;
36
+ }
37
+ try {
38
+ const inputInstantiation = this.evaluatorInputs.map((input) => row[input.column]);
39
+ for (const outputRow of this.evaluator.evaluate(inputInstantiation)) {
40
+ if (!isValidParameterValueRow(outputRow)) {
41
+ continue;
42
+ }
43
+ const outputs = {};
44
+ for (let i = 0; i < this.numberOfOutputs; i++) {
45
+ outputs[i.toString()] = outputRow[i];
46
+ }
47
+ // source is [...outputs, ...partitionValues]
48
+ const partitionValues = outputRow.splice(this.numberOfOutputs, this.numberOfParameters);
49
+ const lookup = UnscopedParameterLookup.normalized(partitionValues);
50
+ results.push({ lookup, bucketParameters: [outputs] });
51
+ }
52
+ }
53
+ catch (e) {
54
+ results.push({ error: e.message });
55
+ }
56
+ return results;
57
+ }
58
+ tableSyncsParameters(table) {
59
+ return this.source.sourceTable.matches(table);
60
+ }
61
+ }
62
+ //# sourceMappingURL=parameter_index_lookup_creator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parameter_index_lookup_creator.js","sourceRoot":"","sources":["../../../src/sync_plan/evaluator/parameter_index_lookup_creator.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,8BAA8B,EAA6B,MAAM,uCAAuC,CAAC;AAMlH,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,EAAE,uBAAuB,EAAE,MAAM,iCAAiC,CAAC;AAE1E,MAAM,OAAO,mCAAmC;IAQ3B;IAPV,kBAAkB,CAAuB;IACjC,SAAS,CAA4B;IACrC,eAAe,CAAiC;IAChD,eAAe,CAAS;IACxB,kBAAkB,CAAS;IAE5C,YACmB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