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

work #3601

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

work #3601

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
44 changes: 26 additions & 18 deletions packages/zql/src/ivm/array-view.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,12 @@ test('collapse', () => {

view.push({
type: 'child',
row: {
id: 1,
name: 'issue',
},
rows: [
{
id: 1,
name: 'issue',
},
],
child: {
relationshipName: 'labels',
change: {
Expand Down Expand Up @@ -681,10 +683,12 @@ test('collapse', () => {
// edit the hidden row
view.push({
type: 'child',
row: {
id: 1,
name: 'issue',
},
rows: [
{
id: 1,
name: 'issue',
},
],
child: {
relationshipName: 'labels',
change: {
Expand Down Expand Up @@ -732,20 +736,24 @@ test('collapse', () => {
// edit the leaf
view.push({
type: 'child',
row: {
id: 1,
name: 'issue',
},
rows: [
{
id: 1,
name: 'issue',
},
],
child: {
relationshipName: 'labels',
change: {
type: 'child',
row: {
id: 2,
issueId: 1,
labelId: 2,
extra: 'b2',
},
rows: [
{
id: 2,
issueId: 1,
labelId: 2,
extra: 'b2',
},
],
child: {
relationshipName: 'labels',
change: {
Expand Down
2 changes: 1 addition & 1 deletion packages/zql/src/ivm/catch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {FetchRequest, Input, Output} from './operator.js';

export type CaughtChildChange = {
type: 'child';
row: Row;
rows: Row[];
child: {
relationshipName: string;
change: CaughtChange;
Expand Down
7 changes: 1 addition & 6 deletions packages/zql/src/ivm/change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type RemoveChange = {
*/
export type ChildChange = {
type: 'child';
row: Row;
rows: Row[];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eventually this could be a Stream

child: {
relationshipName: string;
change: Change;
Expand Down Expand Up @@ -62,8 +62,3 @@ export type EditChange = {
node: Node;
oldNode: Node;
};

export function rowForChange(change: Change): Row {
const {type} = change;
return type === 'child' ? change.row : change.node.row;
}
15 changes: 10 additions & 5 deletions packages/zql/src/ivm/filter-push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ export function filterPush(
output.push(change);
}
break;
case 'child':
if (predicate(change.row)) {
output.push(change);
}
break;
case 'edit':
maybeSplitAndPushEditChange(change, predicate, output);
break;
case 'child': {
const filteredRows = change.rows.filter(predicate);
if (filteredRows.length > 0) {
output.push({
...change,
rows: filteredRows,
});
}
break;
}
default:
unreachable(change);
}
Expand Down
35 changes: 22 additions & 13 deletions packages/zql/src/ivm/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,26 @@ export class Join implements Input {
}

#pushChild(change: Change): void {
const pushChildChange = (childRow: Row, change: Change) => {
const parentNodes = this.#parent.fetch({
constraint: Object.fromEntries(
this.#parentKey.map((key, i) => [key, childRow[this.#childKey[i]]]),
),
});
const pushChildChange = (childRows: Row[], change: Change) => {
const parentNodes = [];
for (const childRow of childRows) {
parentNodes.push(
...this.#parent.fetch({
constraint: Object.fromEntries(
this.#parentKey.map((key, i) => [
key,
childRow[this.#childKey[i]],
]),
),
}),
);
}

for (const parentNode of parentNodes) {
const rows = [...parentNodes].map(n => n.row);
if (rows.length > 0) {
const childChange: ChildChange = {
type: 'child',
row: parentNode.row,
rows,
child: {
relationshipName: this.#relationshipName,
change,
Expand All @@ -220,27 +229,27 @@ export class Join implements Input {
switch (change.type) {
case 'add':
case 'remove':
pushChildChange(change.node.row, change);
pushChildChange([change.node.row], change);
break;
case 'child':
pushChildChange(change.row, change);
pushChildChange(change.rows, change);
break;
case 'edit': {
const childRow = change.node.row;
const oldChildRow = change.oldNode.row;
if (rowEqualsForCompoundKey(oldChildRow, childRow, this.#childKey)) {
// The child row was edited in a way that does not change the relationship.
// We can therefore just push the change down (wrapped in a child change).
pushChildChange(childRow, change);
pushChildChange([childRow], change);
} else {
// The child row was edited in a way that changes the relationship. We
// therefore treat this as a remove from the old row followed by an
// add to the new row.
pushChildChange(oldChildRow, {
pushChildChange([oldChildRow], {
type: 'remove',
node: change.oldNode,
});
pushChildChange(childRow, {
pushChildChange([childRow], {
type: 'add',
node: change.node,
});
Expand Down
15 changes: 12 additions & 3 deletions packages/zql/src/ivm/skip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,18 @@ export class Skip implements Operator {

change satisfies AddChange | RemoveChange | ChildChange;

const changeRow = change.type === 'child' ? change.row : change.node.row;
if (shouldBePresent(changeRow)) {
this.#output.push(change);
if (change.type === 'child') {
const filteredRows = change.rows.filter(shouldBePresent);
if (filteredRows.length > 0) {
this.#output.push({
...change,
rows: filteredRows,
});
}
} else {
if (shouldBePresent(change.node.row)) {
this.#output.push(change);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/zql/src/ivm/snitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function toChangeRecord(change: Change): ChangeRecord {
case 'child':
return {
type: 'child',
row: change.row,
rows: change.rows,
child: toChangeRecord(change.child.change),
};
default:
Expand Down Expand Up @@ -136,7 +136,7 @@ export type RemoveChangeRecord = {

export type ChildChangeRecord = {
type: 'child';
row: Row;
rows: Row[];
child: ChangeRecord;
};

Expand Down
40 changes: 24 additions & 16 deletions packages/zql/src/ivm/take.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import {must} from '../../../shared/src/must.js';
import type {Row, Value} from '../../../zero-protocol/src/data.js';
import type {PrimaryKey} from '../../../zero-protocol/src/primary-key.js';
import {assertOrderingIncludesPK} from '../builder/builder.js';
import {
rowForChange,
type Change,
type EditChange,
type RemoveChange,
} from './change.js';
import {type Change, type EditChange, type RemoveChange} from './change.js';
import type {Constraint} from './constraint.js';
import {compareValues, type Comparator, type Node} from './data.js';
import {
Expand Down Expand Up @@ -232,15 +227,14 @@ export class Take implements Operator {
return;
}

const {takeState, takeStateKey, maxBound, constraint} =
this.#getStateAndConstraint(rowForChange(change));
if (!takeState) {
return;
}

const {compareRows} = this.getSchema();

if (change.type === 'add') {
const {takeState, takeStateKey, maxBound, constraint} =
this.#getStateAndConstraint(change.node.row);
if (!takeState) {
return;
}
if (takeState.size < this.#limit) {
this.#setTakeState(
takeStateKey,
Expand Down Expand Up @@ -305,6 +299,11 @@ export class Take implements Operator {
this.#output.push(removeChange);
this.#output.push(change);
} else if (change.type === 'remove') {
const {takeState, takeStateKey, maxBound, constraint} =
this.#getStateAndConstraint(change.node.row);
if (!takeState) {
return;
}
if (takeState.bound === undefined) {
// change is after bound
return;
Expand Down Expand Up @@ -375,10 +374,19 @@ export class Take implements Operator {
);
this.#output.push(change);
} else if (change.type === 'child') {
// A 'child' change should be pushed to output if its row
// is <= bound.
if (takeState.bound && compareRows(change.row, takeState.bound) <= 0) {
this.#output.push(change);
if (change.type === 'child') {
const filteredRows = change.rows.filter(r => {
const {takeState} = this.#getStateAndConstraint(r);
return (
takeState && takeState.bound && compareRows(r, takeState.bound) <= 0
);
});
if (filteredRows.length > 0) {
this.#output.push({
...change,
rows: filteredRows,
});
}
}
}
}
Expand Down
29 changes: 17 additions & 12 deletions packages/zql/src/ivm/view-apply-change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,34 @@ export function applyChange(
break;
}
case 'child': {
let existing: Entry;
let existing: Entry[];
if (singular) {
assert(change.rows.length === 1);
assertObject(parentEntry[relationship]);
existing = parentEntry[relationship];
existing = [parentEntry[relationship]];
} else {
const view = getChildEntryList(parentEntry, relationship);
const {pos, found} = binarySearch(view, change.row, schema.compareRows);
assert(found, 'node does not exist');
existing = view[pos];
existing = change.rows.map(r => {
const {pos, found} = binarySearch(view, r, schema.compareRows);
assert(found, 'node does not exist');
return view[pos];
});
}

const childSchema = must(
schema.relationships[change.child.relationshipName],
);
const childFormat = format.relationships[change.child.relationshipName];
if (childFormat !== undefined) {
applyChange(
existing,
change.child.change,
childSchema,
change.child.relationshipName,
childFormat,
);
for (const entry of existing) {
applyChange(
entry,
change.child.change,
childSchema,
change.child.relationshipName,
childFormat,
);
}
}
break;
}
Expand Down
Loading