Skip to content

Commit

Permalink
chore(zero): Change AST for orderBy (#1975)
Browse files Browse the repository at this point in the history
This changes the AST for orderBy to:

```ts
type Ordering = readonly (readonly [Selector, Direction])[];
```

This is in preparation for supporting things like

```ts
q.orderBy('modified', 'desc').orderBy('id', 'asc');
```

or written as SQL:

```sql
ORDER BY modified DESC, id ASC
```

This PR does not change any semantics
  • Loading branch information
arv authored Jun 4, 2024
1 parent 8e0d971 commit 1c40c67
Show file tree
Hide file tree
Showing 33 changed files with 469 additions and 553 deletions.
14 changes: 4 additions & 10 deletions packages/zero-cache/src/services/view-syncer/queries.pg-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,8 @@ describe('view-syncer/queries', () => {
[['parent', 'owner'], 'parent_owner'],
],
orderBy: [
[
['issues', 'id'],
['issues', 'title'],
],
'desc',
[['issues', 'id'], 'desc'],
[['issues', 'title'], 'desc'],
],
table: 'issues',
joins: [
Expand Down Expand Up @@ -575,11 +572,8 @@ describe('view-syncer/queries', () => {
[['parent', 'owner'], 'parent_owner'],
],
orderBy: [
[
['issues', 'id'],
['issues', 'title'],
],
'desc',
[['issues', 'id'], 'desc'],
[['issues', 'title'], 'desc'],
],
table: 'issues',
joins: [
Expand Down
2 changes: 1 addition & 1 deletion packages/zero-cache/src/zql/deaggregation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('zql/deaggregation', () => {
},
],
groupBy: [['issues', 'id']],
orderBy: [[['issues', 'modified']], 'desc'],
orderBy: [[['issues', 'modified'], 'desc']],
},
original: `
SELECT
Expand Down
8 changes: 4 additions & 4 deletions packages/zero-cache/src/zql/deaggregation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ function getSupportedGroupByTable(
});

if (orderBy) {
orderBy[0].forEach(s => {
for (const [selector] of orderBy) {
assert(
s[0] === groupByTable,
`ORDER BY ${s} does not match GROUP BY table ${groupByTable}`,
selector[0] === groupByTable,
`ORDER BY ${selector} does not match GROUP BY table ${groupByTable}`,
);
});
}
}

assertAllWheresAgainst(groupByTable, where);
Expand Down
25 changes: 8 additions & 17 deletions packages/zero-cache/src/zql/expansion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,8 @@ describe('zql/expansion', () => {
),
),
orderBy: [
[
['issues', 'date'],
['issues', 'priority'],
],
'asc',
[['issues', 'date'], 'asc'],
[['issues', 'priority'], 'asc'],
],
},
original: `
Expand Down Expand Up @@ -142,11 +139,8 @@ describe('zql/expansion', () => {
),
),
orderBy: [
[
['issues', 'date'],
['issues', 'priority'],
],
'asc',
[['issues', 'date'], 'asc'],
[['issues', 'priority'], 'asc'],
],
},
},
Expand Down Expand Up @@ -214,7 +208,7 @@ describe('zql/expansion', () => {
],
},
],
orderBy: [[['owner', 'level']], 'asc'],
orderBy: [[['owner', 'level'], 'asc']],
},
original: `
SELECT
Expand Down Expand Up @@ -283,11 +277,8 @@ describe('zql/expansion', () => {
),
),
orderBy: [
[
['issues', 'date'],
['issues', 'priority'],
],
'asc',
[['issues', 'date'], 'asc'],
[['issues', 'priority'], 'asc'],
],
},
},
Expand Down Expand Up @@ -465,7 +456,7 @@ describe('zql/expansion', () => {
],
},
],
orderBy: [[['owner', 'awesomeness']], 'desc'],
orderBy: [[['owner', 'awesomeness'], 'desc']],
},
original: `
SELECT
Expand Down
19 changes: 16 additions & 3 deletions packages/zero-cache/src/zql/expansion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type {AST, Condition, Selector} from '@rocicorp/zql/src/zql/ast/ast.js';
import type {
AST,
Condition,
Ordering,
Selector,
} from '@rocicorp/zql/src/zql/ast/ast.js';
import {assert} from 'shared/src/asserts.js';
import type {ServerAST, SubQuery} from './server-ast.js';

Expand Down Expand Up @@ -235,6 +240,9 @@ export function expandSubqueries(
const [from] = selector;
selectors.get(from)?.push(selector) ?? selectors.set(from, [selector]);
};
const addOrderBySelector = (orderingElement: Ordering[number]) =>
addSelector(orderingElement[0]);

const selected = new Set<string>();
// Add all referenced fields / selectors.
select?.forEach(([selector, alias]) => {
Expand All @@ -246,7 +254,7 @@ export function expandSubqueries(
getWhereColumns(where, []).forEach(selector => addSelector(selector));
joins?.forEach(({on}) => on.forEach(part => addSelector(part)));
groupBy?.forEach(grouping => addSelector(grouping));
orderBy?.[0].forEach(ordering => addSelector(ordering));
orderBy?.forEach(addOrderBySelector);
aggregate?.forEach(agg => {
if (agg.field !== undefined) {
addSelector(agg.field);
Expand Down Expand Up @@ -384,6 +392,11 @@ export function reAliasAndBubbleSelections(
: [from, newCol];
};

const renameSelectorsInOrderingElement = ([
selector,
dir,
]: Ordering[number]): Ordering[number] => [renameSelector(selector), dir];

// Return a modified AST with all selectors realiased (SELECT, ON, GROUP BY, ORDER BY),
// and bubble up all selected aliases to the `exports` Map.
const exported = new Set<string>();
Expand Down Expand Up @@ -416,6 +429,6 @@ export function reAliasAndBubbleSelections(
on: [renameSelector(join.on[0]), renameSelector(join.on[1])],
})),
groupBy: groupBy?.map(renameSelector),
orderBy: orderBy ? [orderBy[0].map(renameSelector), orderBy[1]] : undefined,
orderBy: orderBy?.map(renameSelectorsInOrderingElement),
};
}
26 changes: 13 additions & 13 deletions packages/zero-cache/src/zql/invalidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ describe('zql/invalidation hashes filters and hashes', () => {
schema: 'zero',
table: 'foo',
select: [[['foo', 'id'], 'id']],
orderBy: [[['foo', 'id']], 'asc'],
orderBy: [[['foo', 'id'], 'asc']],
},
filters: [
{
Expand Down Expand Up @@ -258,7 +258,7 @@ describe('zql/invalidation hashes filters and hashes', () => {
schema: 'zero',
table: 'foo',
select: [[['foo', 'id'], 'id']],
orderBy: [[['foo', 'id']], 'asc'],
orderBy: [[['foo', 'id'], 'asc']],
},
alias: 'foo',
},
Expand Down Expand Up @@ -288,7 +288,7 @@ describe('zql/invalidation hashes filters and hashes', () => {
aggregate: [
{aggregate: 'min', field: ['foo', 'priority'], alias: 'ignored'},
],
orderBy: [[['foo', 'ignored']], 'asc'],
orderBy: [[['foo', 'ignored'], 'asc']],
},
filters: [
{
Expand All @@ -313,7 +313,7 @@ describe('zql/invalidation hashes filters and hashes', () => {
ast: {
table: 'foo',
aggregate: [{aggregate: 'count', alias: 'ignored'}],
orderBy: [[['foo', 'ignored']], 'asc'],
orderBy: [[['foo', 'ignored'], 'asc']],
},
filters: [
{
Expand All @@ -336,7 +336,7 @@ describe('zql/invalidation hashes filters and hashes', () => {
ast: {
table: 'foo',
select: [[['foo', 'id'], 'id']],
orderBy: [[['foo', 'id']], 'asc'],
orderBy: [[['foo', 'id'], 'asc']],
where: and(
cond(['foo', 'foo'], '=', 'bar'),
cond(['foo', 'bar'], '=', 2),
Expand Down Expand Up @@ -367,7 +367,7 @@ describe('zql/invalidation hashes filters and hashes', () => {
ast: {
table: 'foo',
select: [[['foo', 'id'], 'id']],
orderBy: [[['foo', 'id']], 'asc'],
orderBy: [[['foo', 'id'], 'asc']],
where: and(
cond(['foo', 'foo'], '=', 'bar'),
cond(['join.alias', 'baz'], '=', 3), // Ignored
Expand Down Expand Up @@ -399,7 +399,7 @@ describe('zql/invalidation hashes filters and hashes', () => {
ast: {
table: 'foo',
select: [[['foo', 'id'], 'id']],
orderBy: [[['foo', 'id']], 'asc'],
orderBy: [[['foo', 'id'], 'asc']],
where: or(
cond(['foo', 'foo'], '=', 'bar'),
cond(['foo', 'bar'], '=', 2),
Expand Down Expand Up @@ -456,7 +456,7 @@ describe('zql/invalidation hashes filters and hashes', () => {
ast: {
table: 'foo',
select: [[['foo', 'id'], 'id']],
orderBy: [[['foo', 'id']], 'asc'],
orderBy: [[['foo', 'id'], 'asc']],
where: or(
cond(['foo', 'foo'], '=', 'bar'),
cond(['foo', 'bar'], '=', 2),
Expand Down Expand Up @@ -500,7 +500,7 @@ describe('zql/invalidation hashes filters and hashes', () => {
ast: {
table: 'foo',
select: [[['foo', 'id'], 'id']],
orderBy: [[['foo', 'id']], 'asc'],
orderBy: [[['foo', 'id'], 'asc']],
where: or(
cond(['foo', 'foo'], '=', 'bar'),
cond(['foo', 'foo'], '=', 'baz'),
Expand Down Expand Up @@ -543,7 +543,7 @@ describe('zql/invalidation hashes filters and hashes', () => {
ast: {
table: 'foo',
select: [[['foo', 'id'], 'id']],
orderBy: [[['foo', 'id']], 'asc'],
orderBy: [[['foo', 'id'], 'asc']],
where: and(
or(cond(['foo', 'a'], '=', 1), cond(['foo', 'b'], '=', 2)),
or(cond(['foo', 'c'], '=', 3), cond(['foo', 'd'], '=', 4)),
Expand Down Expand Up @@ -665,7 +665,7 @@ describe('zql/invalidation hashes filters and hashes', () => {
ast: {
table: 'foo',
select: [[['foo', 'id'], 'id']],
orderBy: [[['foo', 'id']], 'asc'],
orderBy: [[['foo', 'id'], 'asc']],
where: and(
or(cond(['foo', 'foo'], '=', 'bar'), cond(['foo', 'bar'], '=', 1)),
or(cond(['foo', 'bar'], '=', 2), cond(['foo', 'do'], '=', 'foo')),
Expand Down Expand Up @@ -702,7 +702,7 @@ describe('zql/invalidation hashes filters and hashes', () => {
ast: {
table: 'foo',
select: [[['foo', 'id'], 'id']],
orderBy: [[['foo', 'id']], 'asc'],
orderBy: [[['foo', 'id'], 'asc']],
where: and(
cond(['foo', 'foo'], '=', 'bar'),
cond(['foo', 'bar'], '=', 2),
Expand All @@ -718,7 +718,7 @@ describe('zql/invalidation hashes filters and hashes', () => {
{
table: 'foo',
select: [[['foo', 'id'], 'id']],
orderBy: [[['foo', 'id']], 'asc'],
orderBy: [[['foo', 'id'], 'asc']],
where: and(
or(cond(['foo', 'a'], '=', 1), cond(['foo', 'b'], '=', 2)),
or(cond(['foo', 'c'], '=', 3), cond(['foo', 'd'], '=', 4)),
Expand Down
Loading

0 comments on commit 1c40c67

Please sign in to comment.