// // Copyright 2025 DXOS.org // import * as Schema from 'effect/Schema'; import { describe, expect, test } from 'vitest'; import { Aggregate, Annotation, DXN, Feed, Filter, Obj, Order, Query, Ref } from '@dxos/echo'; import { type QueryAST } from '@dxos/echo-protocol'; import { TestSchema } from '@dxos/echo/testing'; import { invariant } from '@dxos/invariant'; import { EID, EntityId, SpaceId, URI } from '@dxos/keys'; import { type QueryPlan } from './plan.ts'; import { QueryPlanner, filterContainsInQuery } from './query-planner.ts'; describe('QueryPlanner', () => { const planner = new QueryPlanner(); test('get all people', () => { const query = Query.select(Filter.type(TestSchema.Person)); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('get all people ordered by name', () => { const query = Query.select(Filter.type(TestSchema.Person)).orderBy(Order.property('name', 'asc')); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "property", "property": "name", }, ], }, ], } `); }); test('get all people named Fred', () => { const query = Query.select(Filter.type(TestSchema.Person, { name: 'Fred' })); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": { "name": { "operator": "eq", "type": "compare", "value": "Fred", }, }, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('get all orgs Fred worked for since 2020', () => { const query = Query.select(Filter.type(TestSchema.Person, { id: '01JVS9YYT5VMVJW0GGTM1YHCCH' })) .sourceOf(TestSchema.EmployedBy, { since: Filter.gt('2020'), }) .target(); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "IdSelector", "objectIds": [ "01JVS9YYT5VMVJW0GGTM1YHCCH", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "TraverseStep", "traversal": { "_tag": "RelationTraversal", "direction": "source-to-relation", }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": { "since": { "operator": "gt", "type": "compare", "value": "2020", }, }, "type": "object", "typename": "dxn:com.example.type.employedBy:0.1.0", }, }, { "_tag": "TraverseStep", "traversal": { "_tag": "RelationTraversal", "direction": "relation-to-target", }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('get all tasks for Fred', () => { const query = Query.select(Filter.type(TestSchema.Person, { id: '01JVS9YYT7H6A6DXRN56RSHT6Z' })).referencedBy( TestSchema.Task, 'assignee', ); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "IdSelector", "objectIds": [ "01JVS9YYT7H6A6DXRN56RSHT6Z", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "TraverseStep", "traversal": { "_tag": "ReferenceTraversal", "direction": "incoming", "property": "assignee", }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "props": {}, "type": "object", "typename": "dxn:com.example.type.task:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('get all tasks for employees of Cyberdyne', () => { const query = Query.select(Filter.type(TestSchema.Organization, { name: 'Cyberdyne' })) .targetOf(TestSchema.EmployedBy) .source() .referencedBy(TestSchema.Task, 'assignee'); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.organization:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": { "name": { "operator": "eq", "type": "compare", "value": "Cyberdyne", }, }, "type": "object", "typename": "dxn:com.example.type.organization:0.1.0", }, }, { "_tag": "TraverseStep", "traversal": { "_tag": "RelationTraversal", "direction": "target-to-relation", }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.employedBy:0.1.0", }, }, { "_tag": "TraverseStep", "traversal": { "_tag": "RelationTraversal", "direction": "relation-to-source", }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "TraverseStep", "traversal": { "_tag": "ReferenceTraversal", "direction": "incoming", "property": "assignee", }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "props": {}, "type": "object", "typename": "dxn:com.example.type.task:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('get all people or orgs', () => { const query = Query.all( Query.select(Filter.type(TestSchema.Person)), Query.select(Filter.type(TestSchema.Organization)), ); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "UnionStep", "plans": [ { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], }, { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.organization:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.organization:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], }, ], }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('get all people not in orgs', () => { const query = Query.without( Query.select(Filter.type(TestSchema.Person)), Query.select(Filter.type(TestSchema.Person)).sourceOf(TestSchema.EmployedBy).source(), ); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SetDifferenceStep", "exclude": { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "TraverseStep", "traversal": { "_tag": "RelationTraversal", "direction": "source-to-relation", }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.employedBy:0.1.0", }, }, { "_tag": "TraverseStep", "traversal": { "_tag": "RelationTraversal", "direction": "relation-to-source", }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], }, "source": { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('get assignees of all tasks created after 2020', () => { const query = Query.select( Filter.type(TestSchema.Task, { deadline: Filter.gt('2020'), }), ).reference('assignee'); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.task:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": { "deadline": { "operator": "gt", "type": "compare", "value": "2020", }, }, "type": "object", "typename": "dxn:com.example.type.task:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, { "_tag": "TraverseStep", "traversal": { "_tag": "ReferenceTraversal", "direction": "outgoing", "property": "assignee", }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, ], } `); }); test('contact full-text search', () => { const query = Query.select(Filter.text('Bill')).select(Filter.type(TestSchema.Person)); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TextSelector", "searchKind": "full-text", "text": "Bill", "typename": null, }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('vector search', () => { const query = Query.select(Filter.text('Bill', { type: 'vector' })); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TextSelector", "searchKind": "vector", "text": "Bill", "typename": null, }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('type AND has-parent AND negated annotation keeps the type-indexed select', () => { const Archived = Annotation.make({ id: 'org.dxos.annotation.test-archived', schema: Schema.Boolean }); const query = Query.select( Filter.and( Filter.type(TestSchema.Person), Filter.hasParent(false), Filter.not(Filter.annotation(Archived, true)), ), ); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); const select = plan.steps.find((step) => step._tag === 'SelectStep'); expect(select?._tag === 'SelectStep' && select.selector._tag).toEqual('TypeSelector'); const filters = plan.steps.flatMap((step) => (step._tag === 'FilterStep' ? [step.filter.type] : [])); expect(filters).toEqual(expect.arrayContaining(['has-parent', 'not'])); }); test('full-text search AND type pushes the typename into the selector', () => { const query = Query.select(Filter.and(Filter.text('Bill'), Filter.type(TestSchema.Person))); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TextSelector", "searchKind": "full-text", "text": "Bill", "typename": [ "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('full-text search AND or-of-types pushes all typenames into the selector', () => { const query = Query.select( Filter.and(Filter.text('Bill'), Filter.or(Filter.type(TestSchema.Person), Filter.type(TestSchema.Organization))), ); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TextSelector", "searchKind": "full-text", "text": "Bill", "typename": [ "dxn:com.example.type.person:0.1.0", "dxn:com.example.type.organization:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "filters": [ { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.organization:0.1.0", }, ], "type": "or", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('full-text search AND type-with-props narrows by type and re-checks props', () => { const query = Query.select(Filter.and(Filter.text('Bill'), Filter.type(TestSchema.Person, { name: 'Bill' }))); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TextSelector", "searchKind": "full-text", "text": "Bill", "typename": [ "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": { "name": { "operator": "eq", "type": "compare", "value": "Bill", }, }, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('negated full-text search AND type throws query too complex', () => { const query = Query.select(Filter.not(Filter.and(Filter.text('Bill'), Filter.type(TestSchema.Person)))); expect(() => planner.createPlan(withSpaceIdOptions(query.ast))).toThrow('Query too complex'); }); test('two full-text searches AND-ed throw query too complex', () => { const query = Query.select(Filter.and(Filter.text('Bill'), Filter.text('Ted'))); expect(() => planner.createPlan(withSpaceIdOptions(query.ast))).toThrow('Query too complex'); }); test('select multiple types', () => { const query = Query.select(Filter.or(Filter.type(TestSchema.Organization), Filter.type(TestSchema.Person))); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.organization:0.1.0", "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "filters": [ { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.organization:0.1.0", }, { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, ], "type": "or", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('select everything but the type', () => { const query = Query.select(Filter.not(Filter.type(TestSchema.Person))); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": true, "typename": [ "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, "type": "not", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('select excluding multiple types', () => { const query = Query.select( Filter.not(Filter.or(Filter.type(TestSchema.Organization), Filter.type(TestSchema.Person))), ); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": true, "typename": [ "dxn:com.example.type.organization:0.1.0", "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "filter": { "filters": [ { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.organization:0.1.0", }, { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, ], "type": "or", }, "type": "not", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('select deleted tasks', () => { const query = Query.select(Filter.type(TestSchema.Task)).options({ deleted: 'only' }); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.task:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.task:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('select items from a specific feed', () => { const query = Query.select(Filter.type(TestSchema.Task)).from([{ _tag: 'feed', feedUri: QUEUE_DXN }]); const plan = planner.createPlan(query.ast); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "feed", "feedUri": "echo://B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO/01JJRA86VK4H1TEB6QQVSWXP0E", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.task:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.task:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('select items from all feeds in a space', () => { const query = Query.select(Filter.type(TestSchema.Task)).from([ { _tag: 'space', spaceId: SPACE_ID, includeAllFeeds: true }, ]); const plan = planner.createPlan(query.ast); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "includeAllFeeds": true, "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.task:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.task:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('limit results', () => { const query = Query.select(Filter.type(TestSchema.Task)).limit(10); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "limit": 10, "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.task:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.task:0.1.0", }, }, { "_tag": "OrderStep", "limit": 10, "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('ordered and limited results', () => { const query = Query.select(Filter.type(TestSchema.Task)).orderBy(Order.property('title', 'asc')).limit(10); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.task:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.task:0.1.0", }, }, { "_tag": "OrderStep", "limit": 10, "order": [ { "direction": "asc", "kind": "property", "property": "title", }, ], }, ], } `); }); test('ordered, skipped, and limited results: skip is added to the propagated cap; the limit step is removed', () => { const query = Query.select(Filter.type(TestSchema.Task)).orderBy(Order.property('title', 'asc')).skip(5).limit(10); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); // The OrderStep caps to skip(5) + limit(10) = 15 so that 10 survive the skip (propagating a bare // 10 would truncate before the skip and yield only 5). The SkipStep remains to trim the top-15 // back to the [5, 15) window; the LimitStep is redundant once the cap is propagated and is removed. const orderStep = plan.steps.find((step) => step._tag === 'OrderStep'); expect(orderStep?._tag === 'OrderStep' && orderStep.limit).toBe(15); expect(plan.steps.some((step) => step._tag === 'SkipStep' && step.skip === 5)).toBe(true); expect(plan.steps.some((step) => step._tag === 'LimitStep')).toBe(false); }); test('union of limited queries', () => { const query = Query.all( Query.select(Filter.type(TestSchema.Person)).limit(5), Query.select(Filter.type(TestSchema.Organization)).limit(5), ); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "UnionStep", "plans": [ { "steps": [ { "_tag": "SelectStep", "limit": 5, "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "OrderStep", "limit": 5, "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], }, { "steps": [ { "_tag": "SelectStep", "limit": 5, "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.organization:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.organization:0.1.0", }, }, { "_tag": "OrderStep", "limit": 5, "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], }, ], }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('throws when query has no from clause', () => { const query = Query.select(Filter.type(TestSchema.Person)); expect(() => planner.createPlan(query.ast)).toThrow('Query must be scoped with a from() clause'); }); describe('aggregate', () => { test('a count by index fields selects bare index rows; a property key loads documents', () => { const selectOf = (query: Query.Any) => planner.createPlan(withSpaceIdOptions(query.ast)).steps.find((step) => step._tag === 'SelectStep'); const everything = Query.select(Filter.everything()).aggregate({ type: Aggregate.type(), hour: Aggregate.updated('hour'), count: Aggregate.count(), }); const tasks = Query.select(Filter.type(TestSchema.Task)).aggregate({ count: Aggregate.count() }); const byTitle = Query.select(Filter.type(TestSchema.Task)).aggregate({ title: Aggregate.group('title'), count: Aggregate.count(), }); // A metadata predicate reads keys an index row does not carry (only a stored AST can pair one // with a typename, since `Filter.key` carries no typename). const byMetaKey: QueryAST.Query = { type: 'aggregate', query: { type: 'select', filter: { type: 'object', typename: URI.make('dxn:com.example.type.task:0.1.0'), props: {}, metaKey: 'example.com/id', }, }, aggregates: [{ name: 'count', kind: 'count' }], }; expect(selectOf(everything)).toMatchObject({ bare: true }); expect(selectOf(tasks)).toMatchObject({ bare: true }); expect(selectOf(byTitle)).not.toHaveProperty('bare'); expect(planner.createPlan(withSpaceIdOptions(byMetaKey)).steps[0]).not.toHaveProperty('bare'); }); test('group by single property inserts a natural OrderStep before AggregateStep', () => { const query = Query.select(Filter.type(TestSchema.Task)).aggregate({ title: Aggregate.group('title') }); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); const tags = plan.steps.map((step) => step._tag); expect(tags).toEqual(['SelectStep', 'FilterDeletedStep', 'FilterStep', 'OrderStep', 'AggregateStep']); const orderStep = plan.steps.find((step) => step._tag === 'OrderStep'); expect(orderStep).toMatchObject({ order: [{ kind: 'natural', direction: 'asc' }] }); const aggregateStep = plan.steps.find((step) => step._tag === 'AggregateStep'); expect(aggregateStep).toMatchObject({ aggregates: [{ name: 'title', kind: 'group', properties: ['title'] }] }); }); test('an explicit orderBy before aggregate is preserved (no natural order inserted)', () => { const query = Query.select(Filter.type(TestSchema.Task)) .orderBy(Order.property('title', 'desc')) .aggregate({ title: Aggregate.group('title') }); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); const tags = plan.steps.map((step) => step._tag); expect(tags).toEqual(['SelectStep', 'FilterDeletedStep', 'FilterStep', 'OrderStep', 'AggregateStep']); const orderStep = plan.steps.find((step) => step._tag === 'OrderStep'); expect(orderStep).toMatchObject({ order: [{ kind: 'property', property: 'title', direction: 'desc' }] }); }); test('multi-key aggregate carries all group entries on AggregateStep', () => { const query = Query.select(Filter.type(TestSchema.Task)).aggregate({ title: Aggregate.group('title'), id: Aggregate.group('id'), }); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); const aggregateStep = plan.steps.find((step) => step._tag === 'AggregateStep'); expect(aggregateStep).toMatchObject({ aggregates: [ { name: 'title', kind: 'group', properties: ['title'] }, { name: 'id', kind: 'group', properties: ['id'] }, ], }); }); test('limit before aggregate stays before AggregateStep with no pushdown across it', () => { const query = Query.select(Filter.type(TestSchema.Task)) .orderBy(Order.property('title', 'asc')) .limit(10) .aggregate({ title: Aggregate.group('title') }); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); const tags = plan.steps.map((step) => step._tag); // No optimizeLimits pushdown: LimitStep remains a distinct step (not folded into SelectStep/OrderStep). expect(tags).toEqual([ 'SelectStep', 'FilterDeletedStep', 'FilterStep', 'OrderStep', 'LimitStep', 'AggregateStep', ]); const limitStep = plan.steps.find((step) => step._tag === 'LimitStep'); expect(limitStep).toMatchObject({ limit: 10 }); }); test('limit after aggregate pages over groups (stays after AggregateStep, no pushdown)', () => { const query = Query.select(Filter.type(TestSchema.Task)) .aggregate({ title: Aggregate.group('title') }) .limit(5); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); const tags = plan.steps.map((step) => step._tag); expect(tags).toEqual([ 'SelectStep', 'FilterDeletedStep', 'FilterStep', 'OrderStep', 'AggregateStep', 'LimitStep', ]); // The group-level limit must NOT be pushed into the SelectStep/OrderStep. const selectStep = plan.steps.find((step) => step._tag === 'SelectStep'); expect((selectStep as any).limit).toBeUndefined(); const orderStep = plan.steps.find((step) => step._tag === 'OrderStep'); expect((orderStep as any).limit).toBeUndefined(); const limitStep = plan.steps.find((step) => step._tag === 'LimitStep'); expect(limitStep).toMatchObject({ limit: 5 }); }); test('skip + limit after aggregate pages over groups', () => { const query = Query.select(Filter.type(TestSchema.Task)) .aggregate({ title: Aggregate.group('title') }) .skip(2) .limit(5); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); const tags = plan.steps.map((step) => step._tag); expect(tags).toEqual([ 'SelectStep', 'FilterDeletedStep', 'FilterStep', 'OrderStep', 'AggregateStep', 'SkipStep', 'LimitStep', ]); }); test('AggregateStep carries all declared aggregates', () => { const query = Query.select(Filter.type(TestSchema.Task)).aggregate({ title: Aggregate.group('title'), latest: Aggregate.max('title'), }); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); const aggregateStep = plan.steps.find((step) => step._tag === 'AggregateStep'); expect(aggregateStep).toMatchObject({ aggregates: [ { name: 'title', kind: 'group', properties: ['title'] }, { name: 'latest', kind: 'max', property: 'title' }, ], }); }); test('a post-aggregate orderBy is a group-level OrderStep after AggregateStep', () => { const query = Query.select(Filter.type(TestSchema.Task)) .orderBy(Order.property('title', 'desc')) .aggregate({ title: Aggregate.group('title'), latest: Aggregate.max('title') }) .orderBy(Order.property('latest', 'desc')) .limit(5); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); const tags = plan.steps.map((step) => step._tag); // Within-group OrderStep, then AggregateStep, then the group-level OrderStep (which absorbs the // group-level limit via optimizeLimits — it pages over whole groups, so no separate LimitStep). expect(tags).toEqual([ 'SelectStep', 'FilterDeletedStep', 'FilterStep', 'OrderStep', 'AggregateStep', 'OrderStep', ]); const groupOrderStep = plan.steps[plan.steps.length - 1]; expect(groupOrderStep).toMatchObject({ _tag: 'OrderStep', order: [{ kind: 'property', property: 'latest', direction: 'desc' }], limit: 5, }); }); test('throws when aggregate is nested inside another aggregate', () => { const inner = Query.select(Filter.type(TestSchema.Task)).aggregate({ title: Aggregate.group('title') }); // Raw AST composition: an inner query with its own aggregate, wrapped by an outer aggregate. const query = Query.fromAst({ type: 'aggregate', query: inner.ast, aggregates: [{ name: 'id', kind: 'group', properties: ['id'] }], }); expect(() => planner.createPlan(withSpaceIdOptions(query.ast))).toThrow('Only one aggregate clause is supported'); }); test('throws when an aggregated subquery is used as a from() source', () => { // The planner flattens `.from(subquery)`; an aggregated subquery would merge a second aggregate // into the plan, so it must be rejected even though the outer query has no aggregate of its own. const aggregatedSubquery = Query.select(Filter.type(TestSchema.Person)).aggregate({ email: Aggregate.group('email'), }); const query = Query.select(Filter.type(TestSchema.Task)).from(aggregatedSubquery); expect(() => planner.createPlan(withSpaceIdOptions(query.ast))).toThrow( 'aggregate must be the outermost query clause', ); }); test('throws when both the outer query and a from() subquery are aggregated', () => { const aggregatedSubquery = Query.select(Filter.type(TestSchema.Person)).aggregate({ email: Aggregate.group('email'), }); const query = Query.select(Filter.type(TestSchema.Task)) .from(aggregatedSubquery) .aggregate({ title: Aggregate.group('title') }); expect(() => planner.createPlan(withSpaceIdOptions(query.ast))).toThrow('Only one aggregate clause is supported'); }); test('aggregate under from()/options() is still valid (outermost data clause)', () => { const query = Query.select(Filter.type(TestSchema.Task)) .aggregate({ title: Aggregate.group('title') }) .options({ debugLabel: 'grouped', }); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan.steps.some((step) => step._tag === 'AggregateStep')).toBe(true); }); }); test('from all accessible spaces', () => { const query = Query.select(Filter.type(TestSchema.Person)).from('all-accessible-spaces'); const plan = planner.createPlan(query.ast); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('from all accessible spaces with feeds', () => { const query = Query.select(Filter.type(TestSchema.Person)).from('all-accessible-spaces', { includeFeeds: true, }); const plan = planner.createPlan(query.ast); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('from specific feed via feeds scope', () => { const query = Query.select(Filter.type(TestSchema.Task)).from([{ _tag: 'feed', feedUri: QUEUE_DXN }]); const plan = planner.createPlan(query.ast); expect(plan.steps[0]).toMatchObject({ _tag: 'SelectStep', scope: [{ _tag: 'feed' }], }); }); test('from specific space with feeds', () => { const query = Query.select(Filter.type(TestSchema.Person)).from([ { _tag: 'space', spaceId: SPACE_ID, includeAllFeeds: true }, ]); const plan = planner.createPlan(query.ast); expect(plan.steps[0]).toMatchObject({ _tag: 'SelectStep', scope: [{ _tag: 'space', spaceId: SPACE_ID, includeAllFeeds: true }], }); }); test('from subquery flattens into filter on subquery results', () => { // Query.select(Filter.props({ name: 'Alice' })).from(Query.select(Filter.type(Person))) // Should flatten to: select type Person -> filter props { name: 'Alice' }. const subquery = Query.select(Filter.type(TestSchema.Person)).from([{ _tag: 'space', spaceId: SPACE_ID }]); const query = Query.select(Filter.props({ name: 'Alice' })).from(subquery); const plan = planner.createPlan(query.ast); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": { "name": { "operator": "eq", "type": "compare", "value": "Alice", }, }, "type": "object", "typename": null, }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, ], } `); }); test('from subquery with reference traversal', () => { // Query.select(Filter.type(Task)).reference('assignee').from(subquery) // The reference traversal wraps the outer select; flattening should still work. const subquery = Query.select(Filter.type(TestSchema.Person)).from([{ _tag: 'space' as const, spaceId: SPACE_ID }]); const query = Query.select(Filter.type(TestSchema.Task)).reference('assignee').from(subquery); const plan = planner.createPlan(query.ast); expect(plan).toMatchInlineSnapshot(` { "steps": [ { "_tag": "SelectStep", "scope": [ { "_tag": "space", "spaceId": "B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO", }, ], "selector": { "_tag": "TypeSelector", "inverted": false, "typename": [ "dxn:com.example.type.person:0.1.0", ], }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.person:0.1.0", }, }, { "_tag": "FilterStep", "filter": { "id": undefined, "props": {}, "type": "object", "typename": "dxn:com.example.type.task:0.1.0", }, }, { "_tag": "OrderStep", "order": [ { "direction": "asc", "kind": "natural", }, ], }, { "_tag": "TraverseStep", "traversal": { "_tag": "ReferenceTraversal", "direction": "outgoing", "property": "assignee", }, }, { "_tag": "FilterDeletedStep", "mode": "only-non-deleted", }, ], } `); }); test('from subquery inherits scope from inner query', () => { // The outer query has no scope — the subquery carries the scope. const subquery = Query.select(Filter.type(TestSchema.Task)).from([{ _tag: 'space', spaceId: SPACE_ID }]); const query = Query.select(Filter.props({ title: 'Test' })).from(subquery); const plan = planner.createPlan(query.ast); const selectStep = plan.steps.find((step) => step._tag === 'SelectStep'); expect(selectStep).toMatchObject({ _tag: 'SelectStep', scope: [{ _tag: 'space', spaceId: SPACE_ID }], }); }); test('and(type, compare, timestamp) throws descriptive error about timestamp limitation', () => { const query = Query.select( Filter.and( Filter.type(TestSchema.Person), Filter.type(TestSchema.Person, { name: 'Alice' }), Filter.updated({ after: Date.now() }), ), ); expect(() => planner.createPlan(withSpaceIdOptions(query.ast))).toThrow(/[Tt]imestamp/); }); test('and(type, timestamp) produces valid plan', () => { const query = Query.select(Filter.and(Filter.type(TestSchema.Person), Filter.updated({ after: Date.now() }))); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); const hasTimestampFilter = plan.steps.some( (step) => step._tag === 'FilterStep' && JSON.stringify(step.filter).includes('timestamp'), ); expect(hasTimestampFilter).toBe(true); }); test('childOf with limit: limit must not be propagated into the SelectStep', () => { // Regression: when a child-of FilterStep sits between SelectStep and LimitStep, pushing // the limit into the SelectStep slices candidates before the filter runs and starves the // result set (e.g. wildcard select grabs 10 random objects, then child-of leaves 0). const parentRef = Ref.fromURI(EID.make({ entityId: EntityId.make('01J7XKZ6E3MZRY7H9TGFR3W6CN') })); const query = Query.select(Filter.everything()).select(Filter.childOf(parentRef)).limit(10); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); const selectStep = plan.steps.find((step) => step._tag === 'SelectStep'); expect(selectStep).toBeDefined(); expect((selectStep as any).limit).toBeUndefined(); const hasLimitStep = plan.steps.some((step) => step._tag === 'LimitStep'); const orderWithLimit = plan.steps.some((step) => step._tag === 'OrderStep' && (step as any).limit === 10); expect(hasLimitStep || orderWithLimit).toBe(true); }); describe('Filter.in subquery (semi-join)', () => { const subquery = Query.select(Filter.type(TestSchema.Task, { completed: true })); test('nested in-query rides inside the residual object FilterStep', () => { const query = Query.select(Filter.type(TestSchema.Task, { title: Filter.in(subquery.project('title')) })); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); const tags = plan.steps.map((step) => step._tag); // _ensureOrderStep always appends a trailing natural OrderStep when none is present. expect(tags).toEqual(['SelectStep', 'FilterDeletedStep', 'FilterStep', 'OrderStep']); const filterStep = plan.steps.find((step) => step._tag === 'FilterStep'); expect(filterStep).toMatchObject({ filter: { type: 'object', props: { title: { type: 'in-query', property: 'title', subquery: subquery.ast, }, }, }, }); }); test('root Filter.in(projection) throws query too complex', () => { const query = Query.select(Filter.in(subquery.project('title'))); expect(() => planner.createPlan(withSpaceIdOptions(query.ast))).toThrow('Query too complex'); }); test('Filter.and(type, root in-query) throws query too complex', () => { const query = Query.select(Filter.and(Filter.type(TestSchema.Task), Filter.in(subquery.project('title')))); expect(() => planner.createPlan(withSpaceIdOptions(query.ast))).toThrow('Query too complex'); }); test('filterContainsInQuery detects a nested in-query in object props', () => { const filter = Filter.type(TestSchema.Task, { title: Filter.in(subquery.project('title')) }).ast; expect(filterContainsInQuery(filter)).toBe(true); expect(filterContainsInQuery(Filter.type(TestSchema.Task).ast)).toBe(false); }); test('filterContainsInQuery detects in-query nested inside and/or/not', () => { const inQueryFilter = Filter.type(TestSchema.Task, { title: Filter.in(subquery.project('title')) }).ast; expect(filterContainsInQuery({ type: 'not', filter: inQueryFilter })).toBe(true); expect(filterContainsInQuery({ type: 'and', filters: [Filter.type(TestSchema.Person).ast, inQueryFilter] })).toBe( true, ); expect(filterContainsInQuery({ type: 'or', filters: [Filter.type(TestSchema.Person).ast, inQueryFilter] })).toBe( true, ); }); test('limit-guard: limit is not folded into SelectStep when a nested in-query is present', () => { // Mirrors the 'childOf with limit' regression test above: the guard only prevents pushing // the limit into the SelectStep (which would slice candidates before the semi-join runs and // starve the result set) — it does not forbid a *later*, unblocked OrderStep from absorbing // the limit, since that OrderStep already sees the fully-filtered set. const query = Query.select(Filter.type(TestSchema.Task, { title: Filter.in(subquery.project('title')) })) .orderBy(Order.natural('asc')) .limit(10); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); const selectStep = plan.steps.find((step) => step._tag === 'SelectStep'); expect((selectStep as any).limit).toBeUndefined(); const hasLimitStep = plan.steps.some((step) => step._tag === 'LimitStep'); const orderWithLimit = plan.steps.some((step) => step._tag === 'OrderStep' && (step as any).limit === 10); expect(hasLimitStep || orderWithLimit).toBe(true); }); test('negative control: without in-query, the limit folds into the OrderStep', () => { const query = Query.select(Filter.type(TestSchema.Task)).orderBy(Order.natural('asc')).limit(10); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); expect(plan.steps.some((step) => step._tag === 'LimitStep')).toBe(false); const orderStep = plan.steps.find((step) => step._tag === 'OrderStep'); expect((orderStep as any).limit).toBe(10); }); test('composes below aggregate + limit: the semi-join FilterStep runs before grouping', () => { const query = Query.select(Filter.type(TestSchema.Task, { title: Filter.in(subquery.project('title')) })) .orderBy(Order.property('title', 'asc')) .aggregate({ title: Aggregate.group('title'), count: Aggregate.count() }) .orderBy(Order.property('count', 'desc')) .limit(10); const plan = planner.createPlan(withSpaceIdOptions(query.ast)); const tags = plan.steps.map((step) => step._tag); // Mirrors 'a post-aggregate orderBy is a group-level OrderStep after AggregateStep' above: // the trailing group-level OrderStep absorbs the limit (it already pages over whole groups), // so there is no separate LimitStep. The semi-join FilterStep still runs before OrderStep/AggregateStep. expect(tags).toEqual([ 'SelectStep', 'FilterDeletedStep', 'FilterStep', 'OrderStep', 'AggregateStep', 'OrderStep', ]); const filterStepIndex = tags.indexOf('FilterStep'); const aggregateStepIndex = tags.indexOf('AggregateStep'); expect(filterStepIndex).toBeLessThan(aggregateStepIndex); const groupOrderStep = plan.steps[plan.steps.length - 1]; expect(groupOrderStep).toMatchObject({ _tag: 'OrderStep', limit: 10 }); const filterStep = plan.steps[filterStepIndex]; expect(filterStep).toMatchObject({ filter: { type: 'object', props: { title: { type: 'in-query' } } } }); }); }); // A feed scan orders and caps its own rows, so `limit` reaches storage instead of slicing a // working set the whole feed was decoded into. describe('feed limit pushdown', () => { const feedScope: QueryAST.Scope[] = [{ _tag: 'feed', feedUri: QUEUE_DXN }]; const selectStep = (query: QueryAST.Query): QueryPlan.SelectStep => { const step = planner.createPlan(query).steps.find((step) => step._tag === 'SelectStep'); expect(step).toBeDefined(); return step as QueryPlan.SelectStep; }; test('a bounded feed read scans in the requested direction', () => { for (const [direction, order] of [ ['asc', Order.natural()], ['desc', Order.natural('desc')], ] as const) { const query = Query.select(Filter.everything()).orderBy(order).limit(5).from(feedScope); expect(selectStep(query.ast)).toMatchObject({ limit: 5, feedScan: { direction, deleted: false }, }); } }); test('a skip is folded into the scan, so no SkipStep drops rows after their documents load', () => { const query = Query.select(Filter.everything()).orderBy(Order.natural('desc')).skip(2).limit(3).from(feedScope); const plan = planner.createPlan(query.ast); expect(plan.steps.find((step) => step._tag === 'SelectStep')).toMatchObject({ limit: 3, feedScan: { direction: 'desc', skip: 2 }, }); expect(plan.steps.some((step) => step._tag === 'SkipStep')).toBe(false); }); test('the typename re-check beside a TypeSelector does not block the scan', () => { const query = Query.select(Filter.type(TestSchema.Task)).limit(5).from(feedScope); expect(selectStep(query.ast)).toMatchObject({ limit: 5, feedScan: { direction: 'asc' } }); }); // `Cursor.seedDedupSet` pages a feed this way: by bare typename, so every schema version matches. test('a page of a bare-typename select scans only the page', () => { const query = Query.select(Filter.type(DXN.make('com.example.type.task'))) .orderBy(Order.natural('desc')) .skip(50) .limit(50) .from(feedScope); expect(selectStep(query.ast)).toMatchObject({ limit: 50, feedScan: { direction: 'desc', skip: 50 } }); }); test('a `deleted: only` read scans the deleted rows, so the cap counts them', () => { const query = Query.select(Filter.everything()).options({ deleted: 'only' }).limit(5).from(feedScope); expect(selectStep(query.ast)).toMatchObject({ feedScan: { deleted: true } }); }); // Each of these prunes the page after the scan, so a cap applied at scan time would return // short of the limit. test('a filter the scan cannot reproduce keeps the limit downstream', () => { const query = Query.select(Filter.type(TestSchema.Task, { title: 'a' })) .limit(5) .from(feedScope); expect(selectStep(query.ast).feedScan).toBeUndefined(); }); test('a skip below the order keeps the limit downstream', () => { // `skip(2).orderBy(desc)` drops two rows in scan order and orders what is left; an inflated // scan limit would order first and drop the two NEWEST, which is a different page. const query = Query.select(Filter.everything()).skip(2).orderBy(Order.natural('desc')).limit(3).from(feedScope); expect(selectStep(query.ast).feedScan).toBeUndefined(); }); test('a content-based order keeps the limit downstream', () => { const query = Query.select(Filter.everything()).orderBy(Order.property('title', 'asc')).limit(5).from(feedScope); const step = selectStep(query.ast); expect(step.feedScan).toBeUndefined(); expect(step.limit).toBeUndefined(); }); test('a space scope keeps the limit downstream — only a feed scan is ordered', () => { const query = Query.select(Filter.everything()).orderBy(Order.natural('desc')).limit(5); const step = selectStep(withSpaceIdOptions(query.ast)); expect(step.feedScan).toBeUndefined(); expect(step.limit).toBeUndefined(); }); test('a cursor read stays a cursor read — it is windowed by position, not natural order', () => { const query = Query.select(Filter.feedCursor({ begin: Feed.Cursor.make('3') })) .limit(5) .from(feedScope); const step = selectStep(query.ast); expect(step.feedScan).toBeUndefined(); expect(step).toMatchObject({ limit: 5, feedCursorRange: { begin: '3' } }); }); }); describe('changes', () => { const task = Obj.make(TestSchema.Task, { title: 'Task' }); const plan = (query: Query.Any) => planner.createPlan(withSpaceIdOptions(query.ast)); const selectorOf = (query: Query.Any) => { const [select] = plan(query).steps; invariant(select._tag === 'SelectStep'); return select.selector; }; test('a space-wide count by day reads the activity index', () => { const query = Query.select(Filter.changes()).aggregate({ day: Aggregate.time('time', 'day'), source: Aggregate.group('source'), changes: Aggregate.count(), ops: Aggregate.sum('ops'), }); expect(selectorOf(query)).toEqual({ _tag: 'ChangesSelector', targets: undefined, source: 'index' }); expect(plan(query).steps.map((step) => step._tag)).toEqual(['SelectStep', 'OrderStep', 'AggregateStep']); }); test('targets narrow the index to their documents', () => { const query = Query.select(Filter.changes(task)).aggregate({ hour: Aggregate.time('time', 'hour') }); expect(selectorOf(query)).toMatchObject({ source: 'index', targets: [expect.stringContaining(task.id)] }); }); test('anything the index cannot answer replays the targets', () => { const history = Query.select(Filter.changes(task)).orderBy(Order.property('time', 'desc')).limit(10); const byActor = Query.select(Filter.changes(task)).aggregate({ actor: Aggregate.group('actor'), changes: Aggregate.count(), }); const limitedFirst = Query.select(Filter.changes(task)).limit(5).aggregate({ changes: Aggregate.count() }); expect(selectorOf(history)).toMatchObject({ source: 'replay' }); expect(selectorOf(byActor)).toMatchObject({ source: 'replay' }); expect(selectorOf(limitedFirst)).toMatchObject({ source: 'replay' }); expect(plan(history).steps.at(-1)).toMatchObject({ _tag: 'LimitStep', limit: 10 }); }); test('a space-wide query must be one the index answers', () => { expect(() => plan(Query.select(Filter.changes()))).toThrow('space-wide'); expect(() => plan(Query.select(Filter.changes()).aggregate({ actor: Aggregate.group('actor'), n: Aggregate.count() })), ).toThrow('space-wide'); expect(() => plan(Query.select(Filter.changes()).aggregate({ max: Aggregate.max('ops') }))).toThrow('space-wide'); }); test('changes cannot mix with object filters, member lists or system timestamps', () => { expect(() => plan(Query.select(Filter.and(Filter.changes(task), Filter.type(TestSchema.Task))))).toThrow( 'cannot be combined', ); expect(() => plan(Query.select(Filter.not(Filter.changes(task))))).toThrow('cannot be combined'); expect(() => plan(Query.select(Filter.changes(task)).aggregate({ items: Aggregate.items() }))).toThrow( 'Aggregate.items()', ); expect(() => plan(Query.select(Filter.changes(task)).aggregate({ day: Aggregate.updated('day') }))).toThrow( 'Aggregate.updated()', ); }); }); }); const SPACE_ID = SpaceId.make('B2NJDFNVZIW77OQSXUBNAD7BUMBD3G5PO'); // Stable id for inline snapshots. // Stable queue EID for inline snapshots. const QUEUE_DXN = EID.make({ spaceId: SPACE_ID, entityId: EntityId.make('01JJRA86VK4H1TEB6QQVSWXP0E') }); const withSpaceIdOptions = (query: QueryAST.Query): QueryAST.Query => ({ type: 'from', query, from: { _tag: 'scope', scopes: [{ _tag: 'space', spaceId: SPACE_ID }], }, });