Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b3fc78c
Keep a filter-only sibling from turning an AND group's index-backed e…
kriszyp Oct 28, 2026
ed00df9
Tighten the filter-only sibling tests and note the remaining estimato…
kriszyp Oct 28, 2026
6a5bfc2
Shorten the estimator DESIGN note and drop a duplicate test require
kriszyp Oct 28, 2026
ddfecd8
Use Number.isFinite in the AND estimate and await every seed write in…
kriszyp Oct 28, 2026
4f1feac
Rank conditions that can drive an index scan ahead of ones that full-…
kriszyp Oct 28, 2026
c0a9dcf
Treat a relationship joined from the primary key as able to drive an …
kriszyp Oct 28, 2026
3dd471b
Keep the index-order sort condition's lead rule unchanged
kriszyp Oct 28, 2026
e503443
Move only the narrowest index-driving condition to the lead
kriszyp Oct 28, 2026
c043b91
Count a full-text condition as driving only when its index is declare…
kriszyp Oct 29, 2026
31ebc1d
Size a singleton primary-key path as one record and keep valueless pr…
kriszyp Oct 29, 2026
bc4a570
Rename a test whose estimate premise no longer holds
kriszyp Oct 29, 2026
65c1353
Trim the conditionDrivesIndex comment to its contract
kriszyp Oct 29, 2026
339f06f
Keep a condition inside an OR from selecting or joining related records
kriszyp Oct 30, 2026
ea4ec7e
Keep a relationship path out of the lead promotion and read an omitte…
kriszyp Oct 30, 2026
91a8d22
Leave a relationship condition that leads by estimate in place and ke…
kriszyp Oct 30, 2026
9eede1a
Follow an AND group's executing lead when keeping a relationship lead…
kriszyp Oct 30, 2026
68a7e03
Let an indexed outer condition lead past an OR group, and read a sing…
kriszyp Oct 30, 2026
75ad339
Keep a relationship lead only when its own scan can run, and classify…
kriszyp Oct 30, 2026
3fa579c
Check every hop of a relationship lead before keeping it, and trim re…
kriszyp Oct 30, 2026
bcd8a24
Assert the rows of the relationship-lead regression, not only its plan
kriszyp Oct 30, 2026
d19cc27
Guard the estimate test's teardown and keep the estimator note within…
kriszyp Oct 30, 2026
ed2d0b0
Match AND-joined conditions on one relationship against the same rela…
kriszyp Oct 30, 2026
6234313
Keep same-record matching independent of select bookkeeping, and let …
kriszyp Oct 30, 2026
fb8ae77
Keep relationship restrictions under symbol keys so a relationship ma…
kriszyp Oct 30, 2026
d1fe9dd
Cover a collection DELETE over HTTP with split-child brands, and drop…
kriszyp Oct 30, 2026
c4b9ada
Trim restating comments and note that an unindexed foreign key never …
kriszyp Oct 30, 2026
818b829
Share only the inner scope of a to-one relationship, so its condition…
kriszyp Oct 30, 2026
00a178e
Reject a parent by the related key alone when sibling conditions shar…
kriszyp Oct 31, 2026
6d954d1
Match a nested select's conditions on one relationship against the sa…
kriszyp Oct 31, 2026
f7d1ca3
Keep the adaptive index switch for every sibling condition that share…
kriszyp Oct 31, 2026
ac43c5a
Read a join's restriction through relationshipRestriction in the join…
kriszyp Oct 31, 2026
90ebd4e
Register the nested-selection fixture's restriction through restrictR…
kriszyp Oct 31, 2026
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
20 changes: 19 additions & 1 deletion integrationTests/database/sql-null-omitted-conjunctions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ const CASES: { wheres: string[]; expected: string[] }[] = [
{ wheres: [`c = 'alpha' AND b IS NULL`, `b IS NULL AND c = 'alpha'`], expected: ['r_a1_babsent', 'r_a1_bnull'] },
{ wheres: [`c = 'alpha' AND (b IS NULL OR b = 5)`], expected: ['r_a1_b5', 'r_a1_babsent', 'r_a1_bnull'] },
{ wheres: [`c = 'alpha' AND (b IS NULL OR b IS NOT NULL)`], expected: ALPHA_IDS },
{ wheres: [`a IS NULL AND b != 5`, `b != 5 AND a IS NULL`], expected: ['r_aabsent_b1', 'r_anull_b1'] },
{ wheres: [`a IS NULL AND b NOT IN (5, 7)`], expected: ['r_aabsent_b1', 'r_anull_b1'] },
{ wheres: [`c LIKE '%lph%' AND b != 5`, `b != 5 AND c LIKE '%lph%'`], expected: ['r1'] },
{
wheres: [`c LIKE '%a' AND b != 5`, `c LIKE '%a' AND b NOT IN (5, 7)`],
expected: ['r1', 'r_a5_b1', 'r_aabsent_b1', 'r_anull_b1'],
},
];

type Client = ReturnType<typeof createApiClient>;
Expand Down Expand Up @@ -98,7 +105,7 @@ for (const sqlEngine of ['auto', 'new']) {

before(async () => {
await setupHarperWithFixture(ctx, FIXTURE_PATH, {
config: { threads: { count: 1 }, logging: { console: true, level: 'error' } },
config: { threads: { count: 1 }, logging: { console: true, level: 'error' }, sql: { allowFullScan: false } },
env: { HARPER_SQL_ENGINE: sqlEngine },
});
client = createApiClient(ctx.harper);
Expand Down Expand Up @@ -166,6 +173,17 @@ for (const sqlEngine of ['auto', 'new']) {
deepStrictEqual(sortedIds(response.body), expected);
});
}

test(`REST GET /${TABLE}/ reports an estimated total next to a filter-only condition`, async () => {
await reseed(client);
const response = await client
.reqRest(`/${TABLE}/?c=alpha&c=ct=lph&limit(2)`)
.set('Prefer', 'count=estimated')
.timeout(60_000);
strictEqual(response.status, 200);
const total = Number(/^items 0-1\/(\d+)$/.exec(response.headers['content-range'])?.[1]);
strictEqual(total >= 2 && total <= SEED.length, true, response.headers['content-range']);
});
}
);
}
45 changes: 45 additions & 0 deletions integrationTests/resources/nested-relationship-docs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,51 @@ suite('documented nested relationship requests and responses', { timeout: 120_00
[[], [], []]
);
});
const productIds = (rows: { id: number; products: string[] | null }[]) =>
rows.map((row) => [row.id, [...(row.products ?? [])].sort()]).sort((a, b) => (a[0] as number) - (b[0] as number));
test('a products condition inside an OR picks brands but not which products they select', async () => {
for (const query of ['(products.name=Cable|id=44)', '(id=44|products.name=Cable)'])
assert.deepStrictEqual(productIds(await get(`/Brand/?${query}&select(id,products[select(id)])`)), [
[42, ['p1', 'p2', 'p3']],
[44, ['p5', 'p6']],
]);
});
test('a collection DELETE on two products conditions spares a brand whose products meet them separately', async () => {
for (const offset of [0, 10]) {
const split = 901 + offset;
const whole = 902 + offset;
const conditions = [`products.name=Split`, `products.id=ge=s${whole}`];
for (const query of [conditions.join('&'), conditions.reverse().join('&')]) {
await insert('Brand', [
{ id: split, name: 'Collection delete' },
{ id: whole, name: 'Collection delete' },
]);
await insert('Product', [
{ id: `s${split}`, brandId: split, name: 'Split', price: 1 },
{ id: `z${split}`, brandId: split, name: 'Other', price: 2 },
{ id: `s${whole}`, brandId: whole, name: 'Split', price: 2 },
]);
const response = await fetch(`${ctx.harper.httpURL}/Brand/?name==Collection%20delete&${query}`, {
method: 'DELETE',
headers: { Authorization: auth },
signal: AbortSignal.timeout(10_000),
});
assert.strictEqual(response.status, 200, await response.text());
assert.deepStrictEqual(
(await get('/Brand/?name==Collection%20delete&select(id,)')).map((brand: { id: number }) => brand.id),
[split],
query
);
await sendOperation(ctx.harper, { operation: 'delete', schema: 'data', table: 'Brand', ids: [split] });
await sendOperation(ctx.harper, {
operation: 'delete',
schema: 'data',
table: 'Product',
ids: [`s${split}`, `z${split}`, `s${whole}`],
});
}
}
});
test('both exact programmatic snippets produce their documented responses', async () => {
assert.deepStrictEqual(await get('/DocumentedQueries/'), {
single: examples.single,
Expand Down
4 changes: 2 additions & 2 deletions resources/DESIGN.md

Large diffs are not rendered by default.

44 changes: 34 additions & 10 deletions resources/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ import {
releaseRelatedLeases,
findAttribute,
estimateCondition,
conditionDrivesIndex,
estimatedEntryCount,
flattenKey,
COERCIBLE_OPERATORS,
Expand Down Expand Up @@ -7534,8 +7535,23 @@ export function makeTable(options): TableResourceClass {
// Sort the query by narrowest to broadest, so we can use the fastest index as possible with minimal filtering.
// Note, that we do allow users to disable condition re-ordering, in case they have knowledge of a preferred
// order for their query.
if (conditions.length > 1 && operator !== 'or') return sortBy(conditions, estimateCondition(TableResource));
else return conditions;
if (conditions.length > 1 && operator !== 'or') {
const ordered = sortBy(conditions, estimateCondition(TableResource));
// a leading sort streams a sorted page, so it keeps the lead (resources/DESIGN.md, estimator and lead rules)
const streamsSort =
ordered[0] === syntheticOrderCondition ||
(ordered[0] === orderAlignedCondition && target.allowFullScan !== false);
if (!streamsSort) {
let lead = 0;
while (lead < ordered.length && !conditionDrivesIndex(TableResource, ordered[lead])) lead++;
if (lead > 0 && lead < ordered.length) {
const driving = ordered[lead];
for (let i = lead; i > 0; i--) ordered[i] = ordered[i - 1];
ordered[0] = driving;
}
}
return ordered;
} else return conditions;
}
const operator = target.operator;
const prepared = TableResource._prepareQueryConditions(conditions, operator, target, context);
Expand Down Expand Up @@ -7906,19 +7922,28 @@ export function makeTable(options): TableResourceClass {
).conditions;
for (let sort = typeof attribute.sort === 'object' && attribute.sort; sort; sort = sort.next)
targetTable._assertNestedReadAccess(sort.attribute, context, attribute);
const compile = (conditions, operator) => {
const predicates = conditions.map((condition) => {
if (condition.conditions) return compile(condition.conditions, condition.operator);
// conditions of one AND on a relationship meet the same related record, as in a top-level query
const compile = (conditions, operator, scope: any = operator === 'or' ? null : Object.create(null)) => {
const predicates = [];
for (const condition of conditions) {
if (condition.conditions) {
const union = condition.operator === 'or';
predicates.push(
compile(condition.conditions, condition.operator, union ? null : (scope ?? Object.create(null)))
);
continue;
}
const predicate = filterByType(
condition,
targetTable,
context,
null,
scope,
(condition.attribute ?? condition[0]) === targetTable.primaryKey
);
if (!predicate) throw new ClientError('This predicate is not supported in a relationship selection');
return predicate;
});
if (predicate) predicates.push(predicate);
else if (!scope || !Array.isArray(condition.attribute ?? condition[0]))
throw new ClientError('This predicate is not supported in a relationship selection');
}
const any = operator === 'or';
const combined: any = any
? (record, entry) => predicates.some((predicate) => predicate(record, entry))
Expand All @@ -7941,7 +7966,6 @@ export function makeTable(options): TableResourceClass {
let selectionStopped = false;
const relationshipSelection = (record, entry?, isSync = false) => {
if (selectionStopped) return undefined;
// per call: an OR-scope condition registers its restriction while the first row is filtered
const filterMap: any = relationshipRestriction(filtered, attribute.name);
const readsKeys =
!isSync && (filterMap ? !filterMap.hasMappings && resolver.toMany : Boolean(resolver.resolveKeys));
Expand Down
Loading
Loading