Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zql): Implement OR #56

Merged
merged 4 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
321 changes: 316 additions & 5 deletions src/zql/ast-to-ivm/pipeline-builder.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {expect, test} from 'vitest';
import {compareUTF8} from 'compare-utf8';
import {describe, expect, test} from 'vitest';
import {z} from 'zod';
import {Entity} from '../../generate.js';
import {AST, Condition} from '../ast/ast.js';
import {makeTestContext} from '../context/context.js';
import {DifferenceStream} from '../ivm/graph/difference-stream.js';
import {Materialite} from '../ivm/materialite.js';
import * as agg from '../query/agg.js';
import {EntityQuery, astForTesting as ast} from '../query/entity-query.js';
import {buildPipeline} from './pipeline-builder.js';
import {compareUTF8} from 'compare-utf8';
import * as agg from '../query/agg.js';
import {DifferenceStream} from '../ivm/graph/difference-stream.js';
import {Entity} from '../../generate.js';

const e1 = z.object({
id: z.string(),
Expand Down Expand Up @@ -116,3 +117,313 @@ test('Where', () => {

// order-by and limit are properties of the materialize view
// and not a part of the pipeline.

function conditionToString(c: Condition, paren = false): string {
if (c.op === 'AND' || c.op === 'OR') {
let s = '';
if (paren) {
s += '(';
}
{
const paren = c.op === 'AND' && c.conditions.length > 1;
s += c.conditions.map(c => conditionToString(c, paren)).join(` ${c.op} `);
}
if (paren) {
s += ')';
}
return s;
}
return `${(c as {field: string}).field} ${c.op} ${(c as {value: {value: unknown}}).value.value}`;
}

describe('OR', () => {
type E = {
id: string;
a: number;
b: number;
};

type DeleteE = {
delete: E;
};

type NoUndefined<T> = T extends undefined ? never : T;

type Case = {
name?: string | undefined;
where: NoUndefined<AST['where']>;
values?: (E | DeleteE)[] | undefined;
expected: (E | [v: E, multiplicity: number])[];
};

const defaultValues: (E | DeleteE)[] = [
{id: 'a', a: 1, b: 1},
{id: 'b', a: 2, b: 1},
{id: 'c', a: 1, b: 2},
{id: 'd', a: 2, b: 2},
];

const cases: Case[] = [
{
where: {
op: 'OR',
conditions: [
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
{op: '=', field: 'b', value: {type: 'literal', value: 2}},
],
},
expected: [
{id: 'a', a: 1, b: 1},
{id: 'c', a: 1, b: 2},
{id: 'd', a: 2, b: 2},
],
},
{
where: {
op: 'OR',
conditions: [{op: '=', field: 'a', value: {type: 'literal', value: 1}}],
},
expected: [
{id: 'a', a: 1, b: 1},
{id: 'c', a: 1, b: 2},
],
},
{
where: {
op: 'OR',
conditions: [
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
{op: '=', field: 'b', value: {type: 'literal', value: 2}},
{op: '=', field: 'a', value: {type: 'literal', value: 2}},
],
},
values: [
{id: 'a', a: 1, b: 1},
{id: 'b', a: 2, b: 1},
{id: 'c', a: 1, b: 2},
{id: 'd', a: 3, b: 3},
],
expected: [
{id: 'a', a: 1, b: 1},
{id: 'b', a: 2, b: 1},
{id: 'c', a: 1, b: 2},
],
},
{
where: {
op: 'OR',
conditions: [
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
],
},
expected: [
{id: 'a', a: 1, b: 1},
{id: 'c', a: 1, b: 2},
],
},
{
where: {
op: 'OR',
conditions: [
{
op: 'AND',
conditions: [
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
{op: '=', field: 'b', value: {type: 'literal', value: 1}},
],
},
{
op: 'AND',
conditions: [
{op: '=', field: 'a', value: {type: 'literal', value: 2}},
{op: '=', field: 'b', value: {type: 'literal', value: 2}},
],
},
],
},
expected: [
{id: 'a', a: 1, b: 1},
{id: 'd', a: 2, b: 2},
],
},

{
where: {
op: 'AND',
conditions: [
{
op: 'OR',
conditions: [
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
{op: '=', field: 'b', value: {type: 'literal', value: 1}},
],
},
{
op: 'OR',
conditions: [
{op: '=', field: 'a', value: {type: 'literal', value: 2}},
{op: '=', field: 'b', value: {type: 'literal', value: 2}},
],
},
],
},
expected: [
{id: 'b', a: 2, b: 1},
{id: 'c', a: 1, b: 2},
],
},

{
where: {
op: 'AND',
conditions: [
{
op: 'OR',
conditions: [
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
{op: '=', field: 'b', value: {type: 'literal', value: 1}},
],
},
{
op: 'OR',
conditions: [
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
{op: '=', field: 'b', value: {type: 'literal', value: 1}},
],
},
],
},
expected: [
{id: 'a', a: 1, b: 1},
{id: 'b', a: 2, b: 1},
{id: 'c', a: 1, b: 2},
],
},

{
name: 'Repeat identical conditions',
where: {
op: 'OR',
conditions: [
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
],
},
expected: [
{id: 'a', a: 1, b: 1},
{id: 'c', a: 1, b: 2},
],
},

{
where: {
op: 'OR',
conditions: [
{op: '=', field: 'a', value: {type: 'literal', value: 3}},
{op: '=', field: 'a', value: {type: 'literal', value: 4}},
{op: '=', field: 'a', value: {type: 'literal', value: 5}},
],
},
expected: [],
},

{
where: {
op: 'AND',
conditions: [
{
op: 'OR',
conditions: [
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
{op: '=', field: 'a', value: {type: 'literal', value: 2}},
{op: '=', field: 'a', value: {type: 'literal', value: 3}},
],
},
{
op: '=',
field: 'b',
value: {type: 'literal', value: 1},
},
],
},
values: [
{id: 'a', a: 1, b: 1},
{id: 'b', a: 2, b: 2},
{id: 'c', a: 3, b: 1},
{id: 'd', a: 4, b: 1},
],
expected: [
{id: 'a', a: 1, b: 1},
{id: 'c', a: 3, b: 1},
],
},

{
name: 'With delete',
where: {
op: 'OR',
conditions: [
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
{op: '=', field: 'a', value: {type: 'literal', value: 2}},
],
},
values: [
{id: 'a', a: 1, b: 1},
// Even though it is really nonsensical to delete this entry since this
// entry does not exist in the model it should still work.
{delete: {id: 'a', a: 1, b: 3}},
{id: 'a', a: 2, b: 2},
{delete: {id: 'c', a: 3, b: 2}},
],
expected: [
{id: 'a', a: 1, b: 1},
[{id: 'a', a: 1, b: 3}, -1],
{id: 'a', a: 2, b: 2},
],
},
];

const comparator = (l: E, r: E) => compareUTF8(l.id, r.id);

for (const c of cases) {
test((c.name ? c.name + ': ' : '') + conditionToString(c.where), () => {
const {values = defaultValues} = c;
const m = new Materialite();
const s = m.newSetSource<E>(comparator);

const ast: AST = {
table: 'items',
select: ['id', 'a', 'b'],
where: c.where,
orderBy: [['id'], 'asc'],
};

const pipeline = buildPipeline(
() => s.stream as unknown as DifferenceStream<Entity>,
ast,
);

const log: unknown[] = [];
pipeline.effect((value, multiplicity) => {
if (multiplicity === 1) {
log.push(value);
} else {
log.push([value, multiplicity]);
}
});

for (const value of values) {
if ('delete' in value) {
s.delete(value.delete);
continue;
} else {
s.add(value);
}
}

expect(log).toEqual(c.expected);
});
}
});
Loading
Loading