@powersync/service-sync-rules 0.0.0-dev-20260129132348 → 0.0.0-dev-20260202103935

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 (79) hide show
  1. package/dist/BucketParameterQuerier.d.ts +28 -5
  2. package/dist/BucketParameterQuerier.js +12 -4
  3. package/dist/BucketParameterQuerier.js.map +1 -1
  4. package/dist/SqlParameterQuery.js +0 -2
  5. package/dist/SqlParameterQuery.js.map +1 -1
  6. package/dist/StaticSqlParameterQuery.js +0 -1
  7. package/dist/StaticSqlParameterQuery.js.map +1 -1
  8. package/dist/TablePattern.d.ts +4 -1
  9. package/dist/TablePattern.js +11 -0
  10. package/dist/TablePattern.js.map +1 -1
  11. package/dist/TableValuedFunctionSqlParameterQuery.js +0 -1
  12. package/dist/TableValuedFunctionSqlParameterQuery.js.map +1 -1
  13. package/dist/compiler/bucket_resolver.d.ts +70 -0
  14. package/dist/compiler/bucket_resolver.js +131 -0
  15. package/dist/compiler/bucket_resolver.js.map +1 -0
  16. package/dist/compiler/compatibility.d.ts +16 -0
  17. package/dist/compiler/compatibility.js +12 -0
  18. package/dist/compiler/compatibility.js.map +1 -0
  19. package/dist/compiler/compiler.d.ts +91 -0
  20. package/dist/compiler/compiler.js +113 -0
  21. package/dist/compiler/compiler.js.map +1 -0
  22. package/dist/compiler/equality.d.ts +99 -0
  23. package/dist/compiler/equality.js +284 -0
  24. package/dist/compiler/equality.js.map +1 -0
  25. package/dist/compiler/expression.d.ts +77 -0
  26. package/dist/compiler/expression.js +122 -0
  27. package/dist/compiler/expression.js.map +1 -0
  28. package/dist/compiler/filter.d.ts +71 -0
  29. package/dist/compiler/filter.js +110 -0
  30. package/dist/compiler/filter.js.map +1 -0
  31. package/dist/compiler/filter_simplifier.d.ts +26 -0
  32. package/dist/compiler/filter_simplifier.js +119 -0
  33. package/dist/compiler/filter_simplifier.js.map +1 -0
  34. package/dist/compiler/ir_to_sync_plan.d.ts +37 -0
  35. package/dist/compiler/ir_to_sync_plan.js +163 -0
  36. package/dist/compiler/ir_to_sync_plan.js.map +1 -0
  37. package/dist/compiler/parser.d.ts +99 -0
  38. package/dist/compiler/parser.js +547 -0
  39. package/dist/compiler/parser.js.map +1 -0
  40. package/dist/compiler/querier_graph.d.ts +42 -0
  41. package/dist/compiler/querier_graph.js +365 -0
  42. package/dist/compiler/querier_graph.js.map +1 -0
  43. package/dist/compiler/rows.d.ts +113 -0
  44. package/dist/compiler/rows.js +156 -0
  45. package/dist/compiler/rows.js.map +1 -0
  46. package/dist/compiler/scope.d.ts +22 -0
  47. package/dist/compiler/scope.js +47 -0
  48. package/dist/compiler/scope.js.map +1 -0
  49. package/dist/compiler/sqlite.d.ts +77 -0
  50. package/dist/compiler/sqlite.js +394 -0
  51. package/dist/compiler/sqlite.js.map +1 -0
  52. package/dist/compiler/table.d.ts +50 -0
  53. package/dist/compiler/table.js +57 -0
  54. package/dist/compiler/table.js.map +1 -0
  55. package/dist/errors.d.ts +4 -2
  56. package/dist/errors.js +16 -1
  57. package/dist/errors.js.map +1 -1
  58. package/dist/index.d.ts +3 -0
  59. package/dist/index.js +3 -0
  60. package/dist/index.js.map +1 -1
  61. package/dist/sql_functions.d.ts +1 -1
  62. package/dist/streams/variant.js +0 -1
  63. package/dist/streams/variant.js.map +1 -1
  64. package/dist/sync_plan/expression.d.ts +109 -0
  65. package/dist/sync_plan/expression.js +85 -0
  66. package/dist/sync_plan/expression.js.map +1 -0
  67. package/dist/sync_plan/expression_to_sql.d.ts +41 -0
  68. package/dist/sync_plan/expression_to_sql.js +185 -0
  69. package/dist/sync_plan/expression_to_sql.js.map +1 -0
  70. package/dist/sync_plan/expression_visitor.d.ts +57 -0
  71. package/dist/sync_plan/expression_visitor.js +171 -0
  72. package/dist/sync_plan/expression_visitor.js.map +1 -0
  73. package/dist/sync_plan/plan.d.ts +196 -0
  74. package/dist/sync_plan/plan.js +2 -0
  75. package/dist/sync_plan/plan.js.map +1 -0
  76. package/dist/sync_plan/serialize.d.ts +68 -0
  77. package/dist/sync_plan/serialize.js +110 -0
  78. package/dist/sync_plan/serialize.js.map +1 -0
  79. package/package.json +1 -1
@@ -0,0 +1,163 @@
1
+ import * as resolver from './bucket_resolver.js';
2
+ import { HashMap, StableHasher, unorderedEquality } from './equality.js';
3
+ import { ColumnInRow } from './expression.js';
4
+ import * as rows from './rows.js';
5
+ import { MapSourceVisitor, visitExpr } from '../sync_plan/expression_visitor.js';
6
+ export class CompilerModelToSyncPlan {
7
+ static evaluatorHash = unorderedEquality({
8
+ hash: (hasher, value) => value.buildBehaviorHashCode(hasher),
9
+ equals: (a, b) => a.behavesIdenticalTo(b)
10
+ });
11
+ mappedObjects = new Map();
12
+ buckets = [];
13
+ /**
14
+ * Mapping of row evaluators to buckets.
15
+ *
16
+ * One might expect one stream to result in one bucket, but that is not generally the case. First, a stream might
17
+ * define multiple buckets. For instance, `SELECT * FROM notes WHERE notes.is_public OR auth.parameter('admin')`
18
+ * requires two buckets since there are different ways a row in `notes` might be synced (we have one bucket with
19
+ * public notes and one bucket of all notes, and then decide which ones a given user has access to by inspecting the
20
+ * token).
21
+ *
22
+ * Further, a stream might have multiple evaluators but only a single bucket. A stream with queries
23
+ * `SELECT * FROM foo` and `SELECT * FROM bar` is an example for that, we want to merge `foo` and `bar` into the same
24
+ * bucket in this case. This is represented by a {@link resolver.StreamResolver} having multiple evaluators attached
25
+ * to it.
26
+ *
27
+ * Finally, we may even be able to re-use buckets between streams. This is not possible in many cases, but can be done
28
+ * if e.g. one stream is `SELECT * FROM profiles WHERE user = auth.user_id()` and another one is
29
+ * `SELECT * FROM profiles WHERE user IN (SELECT member FROM orgs WHERE id = auth.parameter('org'))`. Because the
30
+ * partitioning on `profiles` is the same in both cases, it doesn't matter how the buckets are instantiated.
31
+ */
32
+ evaluatorsToBuckets = new HashMap(CompilerModelToSyncPlan.evaluatorHash);
33
+ translateStatefulObject(source, map) {
34
+ const mapped = map();
35
+ this.mappedObjects.set(source, mapped);
36
+ return mapped;
37
+ }
38
+ translate(source) {
39
+ const queriersByStream = Object.groupBy(source.resolvers, (r) => r.options.name);
40
+ return {
41
+ dataSources: source.evaluators.map((e) => this.translateRowEvaluator(e)),
42
+ parameterIndexes: source.pointLookups.map((p, i) => this.translatePointLookup(p, i)),
43
+ // Note: data sources and parameter indexes must be translated first because we reference them in stream
44
+ // resolvers.
45
+ streams: Object.values(queriersByStream).map((resolvers) => {
46
+ return {
47
+ stream: resolvers[0].options,
48
+ queriers: resolvers.map((e) => this.translateStreamResolver(e))
49
+ };
50
+ }),
51
+ buckets: this.buckets
52
+ };
53
+ }
54
+ createBucketSource(evaluators, uniqueName) {
55
+ return this.evaluatorsToBuckets.putIfAbsent(evaluators, () => {
56
+ const hash = StableHasher.hashWith(CompilerModelToSyncPlan.evaluatorHash, evaluators);
57
+ const source = {
58
+ hashCode: hash,
59
+ sources: evaluators.map((e) => this.mappedObjects.get(e)),
60
+ uniqueName
61
+ };
62
+ this.buckets.push(source);
63
+ return source;
64
+ });
65
+ }
66
+ translatePartitionKey(value) {
67
+ return { expr: this.translateExpression(value.expression.expression) };
68
+ }
69
+ translateRowEvaluator(value) {
70
+ return this.translateStatefulObject(value, () => {
71
+ const hasher = new StableHasher();
72
+ value.buildBehaviorHashCode(hasher);
73
+ const mapped = {
74
+ sourceTable: value.tablePattern,
75
+ hashCode: hasher.buildHashCode(),
76
+ columns: value.columns.map((e) => {
77
+ if (e instanceof rows.StarColumnSource) {
78
+ return 'star';
79
+ }
80
+ else {
81
+ return { expr: this.translateExpression(e.expression.expression), alias: e.alias ?? null };
82
+ }
83
+ }),
84
+ outputTableName: value.outputName,
85
+ filters: value.filters.map((e) => this.translateExpression(e.expression)),
86
+ parameters: value.partitionBy.map((e) => this.translatePartitionKey(e))
87
+ };
88
+ return mapped;
89
+ });
90
+ }
91
+ translatePointLookup(value, index) {
92
+ return this.translateStatefulObject(value, () => {
93
+ const hasher = new StableHasher();
94
+ value.buildBehaviorHashCode(hasher);
95
+ return {
96
+ sourceTable: value.tablePattern,
97
+ defaultLookupScope: {
98
+ // This just needs to be unique, and isn't visible to users (unlike bucket names). We might want to use a
99
+ // more stable naming scheme in the future.
100
+ lookupName: 'lookup',
101
+ queryId: index.toString()
102
+ },
103
+ hashCode: hasher.buildHashCode(),
104
+ outputs: value.result.map((e) => this.translateExpression(e.expression)),
105
+ filters: value.filters.map((e) => this.translateExpression(e.expression)),
106
+ parameters: value.partitionBy.map((e) => this.translatePartitionKey(e))
107
+ };
108
+ });
109
+ }
110
+ translateExpression(expression) {
111
+ const mapper = new MapSourceVisitor((value) => {
112
+ if (value instanceof ColumnInRow) {
113
+ return { column: value.column };
114
+ }
115
+ else {
116
+ return { request: value.source };
117
+ }
118
+ });
119
+ return visitExpr(mapper, expression.node, null);
120
+ }
121
+ translateStreamResolver(value) {
122
+ return {
123
+ requestFilters: value.requestFilters.map((e) => this.translateExpression(e.expression)),
124
+ lookupStages: value.lookupStages.map((stage) => {
125
+ return stage.map((e) => this.translateExpandingLookup(e));
126
+ }),
127
+ bucket: this.createBucketSource([...value.resolvedBucket.evaluators], value.uniqueName),
128
+ sourceInstantiation: value.resolvedBucket.instantiation.map((e) => this.translateParameterValue(e))
129
+ };
130
+ }
131
+ translateExpandingLookup(value) {
132
+ return this.translateStatefulObject(value, () => {
133
+ if (value instanceof resolver.ParameterLookup) {
134
+ return {
135
+ type: 'parameter',
136
+ lookup: this.mappedObjects.get(value.lookup),
137
+ instantiation: value.instantiation.map((e) => this.translateParameterValue(e))
138
+ };
139
+ }
140
+ else {
141
+ return {
142
+ type: 'table_valued',
143
+ functionName: value.tableValuedFunction.tableValuedFunctionName,
144
+ functionInputs: value.tableValuedFunction.parameters.map((e) => this.translateExpression(e.expression)),
145
+ outputs: value.outputs.map((e) => this.translateExpression(e.expression)),
146
+ filters: value.filters.map((e) => this.translateExpression(e.expression))
147
+ };
148
+ }
149
+ });
150
+ }
151
+ translateParameterValue(value) {
152
+ if (value instanceof resolver.RequestParameterValue) {
153
+ return { type: 'request', expr: this.translateExpression(value.expression.expression) };
154
+ }
155
+ else if (value instanceof resolver.LookupResultParameterValue) {
156
+ return { type: 'lookup', resultIndex: value.resultIndex, lookup: this.mappedObjects.get(value.lookup) };
157
+ }
158
+ else {
159
+ return { type: 'intersection', values: value.inner.map((e) => this.translateParameterValue(e)) };
160
+ }
161
+ }
162
+ }
163
+ //# sourceMappingURL=ir_to_sync_plan.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ir_to_sync_plan.js","sourceRoot":"","sources":["../../src/compiler/ir_to_sync_plan.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,QAAQ,MAAM,sBAAsB,CAAC;AAEjD,OAAO,EAAY,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AACnF,OAAO,EAAE,WAAW,EAAmC,MAAM,iBAAiB,CAAC;AAC/E,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,oCAAoC,CAAC;AAEjF,MAAM,OAAO,uBAAuB;IAC1B,MAAM,CAAU,aAAa,GAAkC,iBAAiB,CAAC;QACvF,IAAI,EAAE,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,qBAAqB,CAAC,MAAM,CAAC;QAC5D,MAAM,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC;KAC1C,CAAC,CAAC;IAEK,aAAa,GAAG,IAAI,GAAG,EAAe,CAAC;IACvC,OAAO,GAAkC,EAAE,CAAC;IAEpD;;;;;;;;;;;;;;;;;;OAkBG;IACK,mBAAmB,GAAG,IAAI,OAAO,CACvC,uBAAuB,CAAC,aAAa,CACtC,CAAC;IAEM,uBAAuB,CAAsB,MAAS,EAAE,GAAY;QAC1E,MAAM,MAAM,GAAG,GAAG,EAAE,CAAC;QACrB,IAAI,CAAC,a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G,CAAC;aAAM,CAAC;YACN,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,EAAE,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,uBAAuB,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACnG,CAAC;IACH,CAAC"}
@@ -0,0 +1,99 @@
1
+ import { Statement } from 'pgsql-ast-parser';
2
+ import { PhysicalSourceResultSet, SourceResultSet } from './table.js';
3
+ import { ColumnSource } from './rows.js';
4
+ import { NodeLocations } from './expression.js';
5
+ import { Or } from './filter.js';
6
+ import { PreparedSubquery } from './sqlite.js';
7
+ import { SqlScope } from './scope.js';
8
+ import { ParsingErrorListener, SyncStreamsCompiler } from './compiler.js';
9
+ /**
10
+ * A parsed stream query in its canonical form.
11
+ *
12
+ * The canonical form of stream queries is as follows:
13
+ *
14
+ * ```SQL
15
+ * SELECT <expr>* FROM sourceTable a
16
+ * JOIN table2 b
17
+ * -- ... additional joins
18
+ * WHERE <expr>
19
+ * ```
20
+ *
21
+ * Expressions in the result set must all refer to columns in the {@link sourceTable}.
22
+ *
23
+ * Additionally, the where clause is in disjunctive normal form. Inner terms are either scalar expressions only
24
+ * depending on data from a single source result set, or match clauses.
25
+ *
26
+ * Subqueries are not allowed in the canonical form. Instead, they are lowered into joins. So e.g. the query:
27
+ *
28
+ * ```SQL
29
+ * SELECT * FROM users WHERE id IN (SELECT user FROM org WHERE id = auth.user_id())
30
+ * ```
31
+ *
32
+ * would get lowered to:
33
+ * ```SQL
34
+ * SELECT tmp0.* FROM users tmp0
35
+ * JOIN org tmp1
36
+ * WHERE tmp0.id = tmp1.user AND tmp1.id = (:auth_token) ->> '$.sub';
37
+ * ```
38
+ *
39
+ * Unlike shown here, this lowering doesn't generate aliases that would have to be applied to expressions though.
40
+ * References in expressions would get resolved earlier, and they continue being valid after the transformation.
41
+ */
42
+ export interface ParsedStreamQuery {
43
+ resultColumns: ColumnSource[];
44
+ sourceTable: PhysicalSourceResultSet;
45
+ joined: SourceResultSet[];
46
+ /**
47
+ * All filters, in disjunctive normal form (an OR of ANDs).
48
+ */
49
+ where: Or;
50
+ }
51
+ export interface StreamQueryParserOptions {
52
+ compiler: SyncStreamsCompiler;
53
+ originalText: string;
54
+ errors: ParsingErrorListener;
55
+ parentScope: SqlScope;
56
+ locations: NodeLocations;
57
+ }
58
+ export declare class StreamQueryParser {
59
+ readonly errors: ParsingErrorListener;
60
+ private readonly compiler;
61
+ readonly originalText: string;
62
+ private readonly statementScope;
63
+ private readonly resultSets;
64
+ private readonly subqueryResultSets;
65
+ private readonly resultColumns;
66
+ private where;
67
+ /** The result set for which rows are synced. Set when analyzing result columns. */
68
+ private primaryResultSet?;
69
+ private syntheticSubqueryCounter;
70
+ private nodeLocations;
71
+ private exprParser;
72
+ constructor(options: StreamQueryParserOptions);
73
+ parse(stmt: Statement): ParsedStreamQuery | null;
74
+ parseAsSubquery(stmt: Statement, columnNames?: string[]): PreparedSubquery | null;
75
+ private nestedParser;
76
+ /**
77
+ * Process the AST of a statement, returning whether it's close enough to a valid select statement to be supported for
78
+ * sync streams (allowing us to process an invalid statement further to perhaps collect more errors).
79
+ */
80
+ private processAst;
81
+ private addAndTermToWhereClause;
82
+ private addSubquery;
83
+ private processFrom;
84
+ private resolveTableValued;
85
+ private inferColumnName;
86
+ private processResultColumns;
87
+ private parseExpression;
88
+ private resolveTableName;
89
+ private resolveSoure;
90
+ private warnUnsupported;
91
+ private compileFilterClause;
92
+ private mapBaseExpression;
93
+ /**
94
+ * Emit diagnostics if the expression references data from more than one table, otherwise returns it as a
95
+ * single-dependency expression.
96
+ */
97
+ private mustBeSingleDependency;
98
+ private extractBooleanOperators;
99
+ }