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 3 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
265 changes: 260 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,257 @@ 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 NoUndefined<T> = T extends undefined ? never : T;

type Case = {
where: NoUndefined<AST['where']>;
values: E[];
expected: string[];
};

const cases: Case[] = [
{
where: {
op: 'OR',
conditions: [
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
{op: '=', field: 'b', 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: 2, b: 2},
],
expected: ['a', 'c', 'd'],
},
{
where: {
op: 'OR',
conditions: [{op: '=', field: 'a', value: {type: 'literal', value: 1}}],
},
values: [
{id: 'a', a: 1, b: 1},
{id: 'b', a: 2, b: 1},
{id: 'c', a: 1, b: 2},
{id: 'd', a: 2, b: 2},
],
expected: ['a', 'c'],
},
{
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: ['a', 'b', 'c'],
},
{
where: {
op: 'OR',
conditions: [
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
{op: '=', field: 'a', value: {type: 'literal', value: 1}},
],
},
values: [
{id: 'a', a: 1, b: 1},
{id: 'b', a: 2, b: 1},
{id: 'c', a: 1, b: 2},
{id: 'd', a: 2, b: 2},
],
expected: ['a', 'c'],
},
{
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}},
],
},
],
},
values: [
{id: 'a', a: 1, b: 1},
{id: 'b', a: 2, b: 1},
{id: 'c', a: 1, b: 2},
{id: 'd', a: 2, b: 2},
],
expected: ['a', 'd'],
},

{
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}},
],
},
],
},
values: [
{id: 'a', a: 1, b: 1},
{id: 'b', a: 2, b: 1},
{id: 'c', a: 1, b: 2},
{id: 'd', a: 2, b: 2},
],
expected: ['b', 'c'],
},

{
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}},
],
},
],
},
values: [
{id: 'a', a: 1, b: 1},
{id: 'b', a: 2, b: 1},
{id: 'c', a: 1, b: 2},
{id: 'd', a: 2, b: 2},
],
expected: ['a', 'b', 'c'],
},

{
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}},
],
},
values: [
{id: 'a', a: 1, b: 1},
{id: 'b', a: 2, b: 1},
{id: 'c', a: 1, b: 2},
{id: 'd', a: 2, b: 2},
],
expected: ['a', 'c'],
},

{
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}},
],
},
values: [
{id: 'a', a: 1, b: 1},
{id: 'b', a: 2, b: 1},
{id: 'c', a: 1, b: 2},
{id: 'd', a: 2, b: 2},
],
expected: [],
},
];

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

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

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

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

const log: unknown[] = [];
pipeline.effect(x => {
log.push(x.id);
});

for (const value of values) {
s.add(value);
}
arv marked this conversation as resolved.
Show resolved Hide resolved

expect(log).toEqual(c.expected);
});
}
});
52 changes: 38 additions & 14 deletions src/zql/ast-to-ivm/pipeline-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ function addOrdering(
});
}

function applyWhere(stream: DifferenceStream<Entity>, where: Condition) {
let ret = stream;
function applyWhere<T extends Entity>(
stream: DifferenceStream<T>,
where: Condition,
) {
// We'll handle `OR` and parentheticals like so:
// OR: We'll create a new stream for the LHS and RHS of the OR then merge together.
// Parentheticals: We'll create a new stream for the LHS and RHS of the operator involved in combining the parenthetical then merge together.
Expand All @@ -123,25 +125,47 @@ function applyWhere(stream: DifferenceStream<Entity>, where: Condition) {
//
// So `ORs` cause a fork (two branches that need to be evaluated) and then that fork is combined.

if (where.op === 'AND') {
for (const condition of where.conditions) {
ret = applyWhere(ret, condition);
}
} else {
ret = applySimpleCondition(ret, where);
switch (where.op) {
case 'AND':
return applyAnd(stream, where.conditions);
case 'OR':
return applyOr(stream, where.conditions);
default:
return applySimpleCondition(stream, where);
}
}

return ret;
function applyAnd<T extends Entity>(
stream: DifferenceStream<T>,
conditions: Condition[],
) {
for (const condition of conditions) {
stream = applyWhere(stream, condition);
}
return stream;
}

function applySimpleCondition(
stream: DifferenceStream<Entity>,
function applyOr<T extends Entity>(
stream: DifferenceStream<T>,
conditions: Condition[],
): DifferenceStream<T> {
// Or is done by branching the stream and then applying the conditions to each
// branch. Then we merge the branches back together. At this point we need to
// ensure we do not get duplicate entries so we add a distinct operator
const [first, ...rest] = conditions.map(c => applyWhere(stream, c));
return first.concat(...rest).distinct();
}

function applySimpleCondition<T extends Entity>(
stream: DifferenceStream<T>,
condition: SimpleCondition,
) {
const operator = getOperator(condition.op);
return stream.filter(x =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
operator((x as any)[condition.field], condition.value.value),
operator(
(x as Record<string, unknown>)[condition.field],
condition.value.value,
),
);
}

Expand Down Expand Up @@ -264,7 +288,7 @@ function makeKeyFunction(columns: string[]) {
for (const column of columns) {
ret.push(x[column]);
}
// Would it be better to come up with someh hash function
// Would it be better to come up with some hash function
// which can handle complex types?
return JSON.stringify(ret);
};
Expand Down
2 changes: 1 addition & 1 deletion src/zql/ast/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type AST = {

export type Condition = SimpleCondition | Conjunction;
export type Conjunction = {
op: 'AND'; // future OR
op: 'AND' | 'OR';
conditions: Condition[];
};
export type SimpleOperator =
Expand Down
Loading
Loading