@powersync/service-sync-rules 0.29.10 → 0.31.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 (188) 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 +11 -53
  28. package/dist/SqlSyncRules.js +11 -376
  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/SyncConfig.d.ts +43 -0
  34. package/dist/SyncConfig.js +102 -0
  35. package/dist/SyncConfig.js.map +1 -0
  36. package/dist/TablePattern.d.ts +22 -4
  37. package/dist/TablePattern.js +57 -19
  38. package/dist/TablePattern.js.map +1 -1
  39. package/dist/TableValuedFunctionSqlParameterQuery.d.ts +14 -4
  40. package/dist/TableValuedFunctionSqlParameterQuery.js +41 -7
  41. package/dist/TableValuedFunctionSqlParameterQuery.js.map +1 -1
  42. package/dist/compatibility.d.ts +7 -0
  43. package/dist/compatibility.js +34 -0
  44. package/dist/compatibility.js.map +1 -1
  45. package/dist/compiler/bucket_resolver.d.ts +70 -0
  46. package/dist/compiler/bucket_resolver.js +131 -0
  47. package/dist/compiler/bucket_resolver.js.map +1 -0
  48. package/dist/compiler/compatibility.d.ts +16 -0
  49. package/dist/compiler/compatibility.js +12 -0
  50. package/dist/compiler/compatibility.js.map +1 -0
  51. package/dist/compiler/compiler.d.ts +110 -0
  52. package/dist/compiler/compiler.js +130 -0
  53. package/dist/compiler/compiler.js.map +1 -0
  54. package/dist/compiler/equality.d.ts +99 -0
  55. package/dist/compiler/equality.js +284 -0
  56. package/dist/compiler/equality.js.map +1 -0
  57. package/dist/compiler/expression.d.ts +77 -0
  58. package/dist/compiler/expression.js +122 -0
  59. package/dist/compiler/expression.js.map +1 -0
  60. package/dist/compiler/filter.d.ts +71 -0
  61. package/dist/compiler/filter.js +110 -0
  62. package/dist/compiler/filter.js.map +1 -0
  63. package/dist/compiler/filter_simplifier.d.ts +26 -0
  64. package/dist/compiler/filter_simplifier.js +119 -0
  65. package/dist/compiler/filter_simplifier.js.map +1 -0
  66. package/dist/compiler/ir_to_sync_plan.d.ts +37 -0
  67. package/dist/compiler/ir_to_sync_plan.js +163 -0
  68. package/dist/compiler/ir_to_sync_plan.js.map +1 -0
  69. package/dist/compiler/parser.d.ts +99 -0
  70. package/dist/compiler/parser.js +556 -0
  71. package/dist/compiler/parser.js.map +1 -0
  72. package/dist/compiler/querier_graph.d.ts +42 -0
  73. package/dist/compiler/querier_graph.js +365 -0
  74. package/dist/compiler/querier_graph.js.map +1 -0
  75. package/dist/compiler/rows.d.ts +113 -0
  76. package/dist/compiler/rows.js +156 -0
  77. package/dist/compiler/rows.js.map +1 -0
  78. package/dist/compiler/scope.d.ts +22 -0
  79. package/dist/compiler/scope.js +47 -0
  80. package/dist/compiler/scope.js.map +1 -0
  81. package/dist/compiler/sqlite.d.ts +77 -0
  82. package/dist/compiler/sqlite.js +412 -0
  83. package/dist/compiler/sqlite.js.map +1 -0
  84. package/dist/compiler/table.d.ts +66 -0
  85. package/dist/compiler/table.js +67 -0
  86. package/dist/compiler/table.js.map +1 -0
  87. package/dist/errors.d.ts +4 -2
  88. package/dist/errors.js +16 -1
  89. package/dist/errors.js.map +1 -1
  90. package/dist/events/SqlEventDescriptor.js +1 -1
  91. package/dist/events/SqlEventDescriptor.js.map +1 -1
  92. package/dist/events/SqlEventSourceQuery.d.ts +1 -1
  93. package/dist/events/SqlEventSourceQuery.js +1 -2
  94. package/dist/events/SqlEventSourceQuery.js.map +1 -1
  95. package/dist/from_yaml.d.ts +28 -0
  96. package/dist/from_yaml.js +411 -0
  97. package/dist/from_yaml.js.map +1 -0
  98. package/dist/index.d.ts +9 -0
  99. package/dist/index.js +9 -0
  100. package/dist/index.js.map +1 -1
  101. package/dist/json_schema.js +17 -1
  102. package/dist/json_schema.js.map +1 -1
  103. package/dist/request_functions.js.map +1 -1
  104. package/dist/schema-generators/DartSchemaGenerator.d.ts +3 -3
  105. package/dist/schema-generators/DartSchemaGenerator.js.map +1 -1
  106. package/dist/schema-generators/DotNetSchemaGenerator.d.ts +2 -2
  107. package/dist/schema-generators/DotNetSchemaGenerator.js.map +1 -1
  108. package/dist/schema-generators/JsLegacySchemaGenerator.d.ts +2 -2
  109. package/dist/schema-generators/JsLegacySchemaGenerator.js.map +1 -1
  110. package/dist/schema-generators/KotlinSchemaGenerator.d.ts +2 -2
  111. package/dist/schema-generators/KotlinSchemaGenerator.js.map +1 -1
  112. package/dist/schema-generators/RoomSchemaGenerator.d.ts +2 -2
  113. package/dist/schema-generators/RoomSchemaGenerator.js.map +1 -1
  114. package/dist/schema-generators/SchemaGenerator.d.ts +8 -3
  115. package/dist/schema-generators/SchemaGenerator.js +21 -14
  116. package/dist/schema-generators/SchemaGenerator.js.map +1 -1
  117. package/dist/schema-generators/SqlSchemaGenerator.d.ts +2 -2
  118. package/dist/schema-generators/SqlSchemaGenerator.js.map +1 -1
  119. package/dist/schema-generators/SwiftSchemaGenerator.d.ts +2 -2
  120. package/dist/schema-generators/SwiftSchemaGenerator.js.map +1 -1
  121. package/dist/schema-generators/TsSchemaGenerator.d.ts +2 -2
  122. package/dist/schema-generators/TsSchemaGenerator.js.map +1 -1
  123. package/dist/sql_functions.d.ts +4 -3
  124. package/dist/sql_functions.js +1 -1
  125. package/dist/sql_functions.js.map +1 -1
  126. package/dist/streams/filter.d.ts +34 -4
  127. package/dist/streams/filter.js +93 -23
  128. package/dist/streams/filter.js.map +1 -1
  129. package/dist/streams/from_sql.js +2 -5
  130. package/dist/streams/from_sql.js.map +1 -1
  131. package/dist/streams/parameter.d.ts +7 -6
  132. package/dist/streams/stream.d.ts +25 -15
  133. package/dist/streams/stream.js +59 -87
  134. package/dist/streams/stream.js.map +1 -1
  135. package/dist/streams/variant.d.ts +14 -21
  136. package/dist/streams/variant.js +68 -46
  137. package/dist/streams/variant.js.map +1 -1
  138. package/dist/sync_plan/engine/javascript.d.ts +6 -0
  139. package/dist/sync_plan/engine/javascript.js +208 -0
  140. package/dist/sync_plan/engine/javascript.js.map +1 -0
  141. package/dist/sync_plan/engine/scalar_expression_engine.d.ts +48 -0
  142. package/dist/sync_plan/engine/scalar_expression_engine.js +99 -0
  143. package/dist/sync_plan/engine/scalar_expression_engine.js.map +1 -0
  144. package/dist/sync_plan/engine/sqlite.d.ts +12 -0
  145. package/dist/sync_plan/engine/sqlite.js +53 -0
  146. package/dist/sync_plan/engine/sqlite.js.map +1 -0
  147. package/dist/sync_plan/evaluator/bucket_data_source.d.ts +24 -0
  148. package/dist/sync_plan/evaluator/bucket_data_source.js +139 -0
  149. package/dist/sync_plan/evaluator/bucket_data_source.js.map +1 -0
  150. package/dist/sync_plan/evaluator/bucket_source.d.ts +19 -0
  151. package/dist/sync_plan/evaluator/bucket_source.js +145 -0
  152. package/dist/sync_plan/evaluator/bucket_source.js.map +1 -0
  153. package/dist/sync_plan/evaluator/index.d.ts +17 -0
  154. package/dist/sync_plan/evaluator/index.js +32 -0
  155. package/dist/sync_plan/evaluator/index.js.map +1 -0
  156. package/dist/sync_plan/evaluator/parameter_evaluator.d.ts +138 -0
  157. package/dist/sync_plan/evaluator/parameter_evaluator.js +359 -0
  158. package/dist/sync_plan/evaluator/parameter_evaluator.js.map +1 -0
  159. package/dist/sync_plan/evaluator/parameter_index_lookup_creator.d.ts +20 -0
  160. package/dist/sync_plan/evaluator/parameter_index_lookup_creator.js +64 -0
  161. package/dist/sync_plan/evaluator/parameter_index_lookup_creator.js.map +1 -0
  162. package/dist/sync_plan/expression.d.ts +109 -0
  163. package/dist/sync_plan/expression.js +85 -0
  164. package/dist/sync_plan/expression.js.map +1 -0
  165. package/dist/sync_plan/expression_to_sql.d.ts +43 -0
  166. package/dist/sync_plan/expression_to_sql.js +190 -0
  167. package/dist/sync_plan/expression_to_sql.js.map +1 -0
  168. package/dist/sync_plan/expression_visitor.d.ts +57 -0
  169. package/dist/sync_plan/expression_visitor.js +181 -0
  170. package/dist/sync_plan/expression_visitor.js.map +1 -0
  171. package/dist/sync_plan/plan.d.ts +196 -0
  172. package/dist/sync_plan/plan.js +2 -0
  173. package/dist/sync_plan/plan.js.map +1 -0
  174. package/dist/sync_plan/schema_inference.d.ts +16 -0
  175. package/dist/sync_plan/schema_inference.js +123 -0
  176. package/dist/sync_plan/schema_inference.js.map +1 -0
  177. package/dist/sync_plan/serialize.d.ts +82 -0
  178. package/dist/sync_plan/serialize.js +214 -0
  179. package/dist/sync_plan/serialize.js.map +1 -0
  180. package/dist/types/custom_sqlite_value.d.ts +1 -1
  181. package/dist/types.d.ts +72 -29
  182. package/dist/types.js +30 -5
  183. package/dist/types.js.map +1 -1
  184. package/dist/utils.d.ts +10 -3
  185. package/dist/utils.js +26 -3
  186. package/dist/utils.js.map +1 -1
  187. package/package.json +4 -3
  188. package/schema/sync_rules.json +19 -3
@@ -0,0 +1,145 @@
1
+ import { BucketSourceType } from '../../BucketSource.js';
2
+ import { mapExternalDataToInstantiation } from '../engine/scalar_expression_engine.js';
3
+ import { parametersForRequest, RequestParameterEvaluators } from './parameter_evaluator.js';
4
+ import { buildBucketName, JSONBucketNameSerialize } from '../../utils.js';
5
+ export class StreamBucketSource {
6
+ stream;
7
+ input;
8
+ dataSources = [];
9
+ parameterIndexLookupCreators = [];
10
+ constructor(stream, input) {
11
+ this.stream = stream;
12
+ this.input = input;
13
+ for (const querier of stream.queriers) {
14
+ const mappedSource = input.preparedBuckets.get(querier.bucket);
15
+ this.dataSources.push(mappedSource);
16
+ }
17
+ }
18
+ get name() {
19
+ return this.stream.stream.name;
20
+ }
21
+ get subscribedToByDefault() {
22
+ return this.stream.stream.isSubscribedByDefault;
23
+ }
24
+ get type() {
25
+ return BucketSourceType.SYNC_STREAM;
26
+ }
27
+ debugRepresentation() {
28
+ // TODO: Implement debugRepresentation for compiled sync streams
29
+ return `stream ${this.stream.stream.name}`;
30
+ }
31
+ hydrate(params) {
32
+ const queriers = this.stream.queriers.map((q) => new PreparedQuerier(this.stream.stream, q, this.input));
33
+ return {
34
+ definition: this,
35
+ pushBucketParameterQueriers: (result, options) => {
36
+ const subscriptions = options.streams[this.name] ?? [];
37
+ if (!this.subscribedToByDefault && !subscriptions.length) {
38
+ // The client is not subscribing to this stream, so don't query buckets related to it.
39
+ return;
40
+ }
41
+ let hasExplicitDefaultSubscription = false;
42
+ for (const subscription of subscriptions) {
43
+ let subscriptionParams = options.globalParameters;
44
+ if (subscription.parameters != null) {
45
+ subscriptionParams = subscriptionParams.withAddedStreamParameters(subscription.parameters);
46
+ }
47
+ else {
48
+ hasExplicitDefaultSubscription = true;
49
+ }
50
+ for (const querier of queriers) {
51
+ querier.querierForSubscription(params, result, subscriptionParams, subscription);
52
+ }
53
+ }
54
+ // If the stream is subscribed to by default and there is no explicit subscription that would match the default
55
+ // subscription, also include the default querier.
56
+ if (this.subscribedToByDefault && !hasExplicitDefaultSubscription) {
57
+ for (const querier of queriers) {
58
+ querier.querierForSubscription(params, result, options.globalParameters, null);
59
+ }
60
+ }
61
+ }
62
+ };
63
+ }
64
+ }
65
+ class PreparedQuerier {
66
+ stream;
67
+ matchesParameters;
68
+ lookupStages;
69
+ dataSource;
70
+ constructor(stream, querier, options) {
71
+ this.stream = stream;
72
+ this.dataSource = options.preparedBuckets.get(querier.bucket);
73
+ this.matchesParameters = PreparedQuerier.prepareFilters(options.engine, querier.requestFilters);
74
+ this.lookupStages = RequestParameterEvaluators.prepare(querier.lookupStages, querier.sourceInstantiation, options);
75
+ }
76
+ querierForSubscription(hydration, result, params, subscription) {
77
+ const reason = subscription != null ? { subscription: subscription.opaque_id } : 'default';
78
+ try {
79
+ if (!this.matchesParameters(params)) {
80
+ return;
81
+ }
82
+ const subscriptionEvaluators = this.lookupStages.clone();
83
+ subscriptionEvaluators.partiallyInstantiate({ request: params });
84
+ if (subscriptionEvaluators.isDefinitelyUninstantiable()) {
85
+ return;
86
+ }
87
+ const bucketScope = hydration.hydrationState.getBucketSourceScope(this.dataSource);
88
+ const parametersToBucket = (instantiation) => {
89
+ return {
90
+ definition: this.stream.name,
91
+ inclusion_reasons: [reason],
92
+ bucket: buildBucketName(bucketScope, JSONBucketNameSerialize.stringify(instantiation)),
93
+ priority: this.stream.priority
94
+ };
95
+ };
96
+ // Do we need parameter lookups to resolve parameters?
97
+ const staticInstantiation = subscriptionEvaluators.extractFullInstantiation();
98
+ if (staticInstantiation) {
99
+ // We don't! Return static querier.
100
+ result.queriers.push({
101
+ staticBuckets: staticInstantiation.map(parametersToBucket),
102
+ hasDynamicBuckets: false,
103
+ async queryDynamicBucketDescriptions() {
104
+ return [];
105
+ }
106
+ });
107
+ }
108
+ else {
109
+ result.queriers.push({
110
+ staticBuckets: [],
111
+ hasDynamicBuckets: true,
112
+ async queryDynamicBucketDescriptions(source) {
113
+ const evaluators = subscriptionEvaluators.clone();
114
+ const instantiation = await evaluators.instantiate({
115
+ hydrationState: hydration.hydrationState,
116
+ source,
117
+ request: params
118
+ });
119
+ return [...instantiation].map(parametersToBucket);
120
+ }
121
+ });
122
+ }
123
+ }
124
+ catch (e) {
125
+ result.errors.push({
126
+ descriptor: this.stream.name,
127
+ message: `Error evaluating bucket ids: ${e.message}`,
128
+ subscription: subscription ?? undefined
129
+ });
130
+ }
131
+ }
132
+ static prepareFilters(engine, requestFilters) {
133
+ const mapExpressions = mapExternalDataToInstantiation();
134
+ const evaluator = engine.prepareEvaluator({
135
+ outputs: [],
136
+ filters: requestFilters.map((f) => mapExpressions.transform(f))
137
+ });
138
+ const instantiation = mapExpressions.instantiation;
139
+ return (parameters) => {
140
+ // We've added request filters as filters to prepareEvaluator, so if we get a row then the subscription matches.
141
+ return evaluator.evaluate(parametersForRequest(parameters, instantiation)).length != 0;
142
+ };
143
+ }
144
+ }
145
+ //# sourceMappingURL=bucket_source.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bucket_source.js","sourceRoot":"","sources":["../../../src/sync_plan/evaluator/bucket_source.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,gBAAgB,EAIjB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EAAE,8BAA8B,EAA0B,MAAM,uCAAuC,CAAC;AAG/G,OAAO,EAAE,oBAAoB,EAAE,0BAA0B,EAAE,MAAM,0BAA0B,CAAC;AAI5F,OAAO,EAAE,eAAe,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AAO1E,MAAM,OAAO,kBAAkB;IAKlB;IACQ;IALV,WAAW,GAAuB,EAAE,CAAC;IACrC,4BAA4B,GAAkC,EAAE,CAAC;IAE1E,YACW,MAA+B,EACvB,KAAkB;QAD1B,WAAM,GAAN,MAAM,CAAyB;QACv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AAE,EAAE;YACvC,gHAAgH;YAChH,OAAO,SAAS,CAAC,QAAQ,CAAC,oBAAoB,CAAC,UAAU,EAAE,aAAa,CAAC,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC;QACzF,CAAC,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,17 @@
1
+ import * as plan from '../plan.js';
2
+ import { ScalarExpressionEngine } from '../engine/scalar_expression_engine.js';
3
+ import { SyncConfig } from '../../SyncConfig.js';
4
+ export interface StreamEvaluationContext {
5
+ defaultSchema: string;
6
+ engine: ScalarExpressionEngine;
7
+ /**
8
+ * Source contents that were used to compile the sync plan.
9
+ *
10
+ * This is not used to evaluate rows, but required for all sync config instances.
11
+ */
12
+ sourceText: string;
13
+ }
14
+ export declare class PrecompiledSyncConfig extends SyncConfig {
15
+ readonly plan: plan.SyncPlan;
16
+ constructor(plan: plan.SyncPlan, context: StreamEvaluationContext);
17
+ }
@@ -0,0 +1,32 @@
1
+ import { PreparedStreamBucketDataSource } from './bucket_data_source.js';
2
+ import { PreparedParameterIndexLookupCreator } from './parameter_index_lookup_creator.js';
3
+ import { StreamBucketSource } from './bucket_source.js';
4
+ import { SyncConfig } from '../../SyncConfig.js';
5
+ export class PrecompiledSyncConfig extends SyncConfig {
6
+ plan;
7
+ constructor(plan, context) {
8
+ super(context.sourceText);
9
+ this.plan = plan;
10
+ const preparedBuckets = new Map();
11
+ const preparedLookups = new Map();
12
+ for (const bucket of plan.buckets) {
13
+ const prepared = new PreparedStreamBucketDataSource(bucket, context);
14
+ preparedBuckets.set(bucket, prepared);
15
+ this.bucketDataSources.push(prepared);
16
+ }
17
+ for (const parameter of plan.parameterIndexes) {
18
+ const prepared = new PreparedParameterIndexLookupCreator(parameter, context);
19
+ preparedLookups.set(parameter, prepared);
20
+ this.bucketParameterLookupSources.push(prepared);
21
+ }
22
+ const streamInput = {
23
+ ...context,
24
+ preparedBuckets,
25
+ preparedLookups
26
+ };
27
+ for (const stream of plan.streams) {
28
+ this.bucketSources.push(new StreamBucketSource(stream, streamInput));
29
+ }
30
+ }
31
+ }
32
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/sync_plan/evaluator/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,8BAA8B,EAAE,MAAM,yBAAyB,CAAC;AACzE,OAAO,EAAE,mCAAmC,EAAE,MAAM,qCAAqC,CAAC;AAC1F,OAAO,EAAE,kBAAkB,EAAe,MAAM,oBAAoB,CAAC;AAErE,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAcjD,MAAM,OAAO,qBAAsB,SAAQ,UAAU;IAExC;IADX,YACW,IAAmB,EAC5B,OAAgC;QAEhC,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;QAHjB,SAAI,GAAJ,IAAI,CAAe;QAK5B,MAAM,eAAe,GAAG,IAAI,GAAG,EAA+D,CAAC;QAC/F,MAAM,eAAe,GAAG,IAAI,GAAG,EAA+E,CAAC;QAE/G,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YAClC,MAAM,QAAQ,GAAG,IAAI,8BAA8B,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;YACrE,eAAe,CAAC,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;YACtC,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACxC,CAAC;QAED,KAAK,MAAM,SAAS,IAAI,IAAI,CAAC,gBAAgB,EAAE,CAAC;YAC9C,MAAM,QAAQ,GAAG,IAAI,mCAAmC,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;YAC7E,eAAe,CAAC,GAAG,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;YACzC,IAAI,CAAC,4BAA4B,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QACnD,CAAC;QAED,MAAM,WAAW,GAAgB;YAC/B,GAAG,OAAO;YACV,eAAe;YACf,eAAe;SAChB,CAAC;QACF,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YAClC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,kBAAkB,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;QACvE,CAAC;IACH,CAAC;CACF"}
@@ -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[];