From f1a9233fbb88a48c847753f17ccb314fb9fc02c1 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 15:59:46 +0000 Subject: [PATCH 1/2] docs(skills): teach that a column with no value fails an ordering filter, and where a per-measure filter runs objectstack-query rules/filters.md: `$gt` / `$gte` / `$lt` / `$lte` / `$between` never match a row whose column is null or absent, in `where`, an aggregation's `filter` and `having` alike; the `$or` + `$null: true` spelling keeps those rows; `null` is not an ordering comparand. Paid for in the same file by two restatements: the case-sensitivity paragraph SKILL.md's String Operators section carries, and a second `$between` example. 9925 -> 9892 bytes, 2482 -> 2473 tokens; the ceiling row stays. rules/aggregation.md: a per-measure `filter` no longer "forces the in-memory path". A driver declaring `supports.aggregationFilter` runs it in the database, answering and refusing as that datasource's `where` does; any other driver has the engine evaluate it over the scoped rows. `having` on an `avg` / `min` / `max` over no rows fails an ordering test. 8381 -> 8883 bytes, 2096 -> 2221 tokens, inside the 2357 row. One eval case (id 8) where the no-value rule decides a measure filter. Claude-Session: https://claude.ai/code/session_01ADHkmQokeaWJKGm2S7ZrDA Co-authored-by: Claude --- ...skills-no-value-ordering-measure-filter.md | 10 +++++++ .../evals/filters-pagination-search.json | 10 +++++++ skills/objectstack-query/rules/aggregation.md | 16 ++++++---- skills/objectstack-query/rules/filters.md | 30 +++++++++---------- 4 files changed, 45 insertions(+), 21 deletions(-) create mode 100644 .changeset/33868-skills-no-value-ordering-measure-filter.md diff --git a/.changeset/33868-skills-no-value-ordering-measure-filter.md b/.changeset/33868-skills-no-value-ordering-measure-filter.md new file mode 100644 index 00000000000..208cc4b14b4 --- /dev/null +++ b/.changeset/33868-skills-no-value-ordering-measure-filter.md @@ -0,0 +1,10 @@ +--- +'@objectstack/skills': patch +--- + +docs(skills): the query skill teaches that a column with no value fails an ordering filter, and where a per-measure filter runs + +Clause-②: no +Metadata-protocol: none + +objectstack-query's `rules/filters.md` gains the rule that `$gt` / `$gte` / `$lt` / `$lte` / `$between` never match a row whose column is null or absent, in `where`, an aggregation's `filter` and `having` alike, with the `$or` + `$null: true` spelling that keeps those rows; the same section says `null` is not an ordering comparand. `rules/aggregation.md` no longer says a per-measure `filter` always forces the in-memory path: a driver that declares `supports.aggregationFilter` (`driver-sql`; `driver-turso` outside remote mode) runs it in the database, where it answers and is refused as the same condition is in that datasource's `where`, text collation included; on any other driver the engine evaluates it over the scoped rows. Its `having` section says `avg` / `min` / `max` over no rows is `null` and so fails an ordering test. One eval case covers the rule in a measure filter. Two restatements in `filters.md` were removed to pay for the new text: the case-sensitivity paragraph SKILL.md § String Operators carries, and a second `$between` example. diff --git a/skills/objectstack-query/evals/filters-pagination-search.json b/skills/objectstack-query/evals/filters-pagination-search.json index 4dc22835528..4939954eb0c 100644 --- a/skills/objectstack-query/evals/filters-pagination-search.json +++ b/skills/objectstack-query/evals/filters-pagination-search.json @@ -70,6 +70,16 @@ "must_contain": ["$empty: true"], "must_not_contain": [] } + }, + { + "id": 8, + "prompt": "In one aggregate call on `deal`, return the total number of deals and the number whose `amount` is zero or less. `amount` is an optional number field, and a deal with no amount recorded must count as zero or less.", + "expected_output": "`engine.aggregate('deal', { aggregations: [{ function: 'count', alias: 'total' }, { function: 'count', alias: 'non_positive', filter: { $or: [{ amount: { $lte: 0 } }, { amount: { $null: true } }] } }] })` — a column with no value fails every ordering test (`$gt` / `$gte` / `$lt` / `$lte` / `$between`), in a measure's `filter` as in `where` and `having`, so `{ amount: { $lte: 0 } }` alone skips the deals with no amount; the `$null: true` branch adds them. One call: the per-measure `filter` scopes only that count.", + "files": [], + "assertions": { + "must_contain": ["filter", "$or", "$lte: 0", "$null: true"], + "must_not_contain": [] + } } ] } diff --git a/skills/objectstack-query/rules/aggregation.md b/skills/objectstack-query/rules/aggregation.md index e69216ee836..0a80968b43a 100644 --- a/skills/objectstack-query/rules/aggregation.md +++ b/skills/objectstack-query/rules/aggregation.md @@ -112,15 +112,21 @@ const rows = await engine.aggregate('order', { ``` `having` filters *groups*; `where` filters the *input rows* before grouping — -use `where` to shrink the scan, `having` to threshold the aggregates. +use `where` to shrink the scan, `having` to threshold the aggregates. `avg` / +`min` / `max` over no rows is `null`, and no value fails every ordering test +([filter rules](./filters.md)): `having: { average: { $lte: 0 } }` drops it. ## Filtered Aggregation (FILTER WHERE) > ✅ **Enforced.** A per-aggregation `filter` scopes that one measure, so a -> total and a conditional count share ONE call. Any aggregation carrying a -> non-empty `filter` forces the in-memory path — no driver compiles a -> conditional aggregate, and one reached directly refuses `NOT_IMPLEMENTED`; -> unfiltered aggregations keep native push-down. +> total and a conditional count share ONE call. A driver that declares +> `supports.aggregationFilter` (`driver-sql`; `driver-turso` outside remote +> mode) runs it in the database, compiled as that datasource's `where` is: it +> answers what the same condition answers there (text by the database +> collation) and is refused where it is — a `{ $field }` comparison on a column +> the object does not declare, `INVALID_FILTER` / 400. On any other driver the +> engine reads the scoped rows and evaluates the filter itself. A column with +> no value fails its ordering tests either way ([filter rules](./filters.md)). ```typescript diff --git a/skills/objectstack-query/rules/filters.md b/skills/objectstack-query/rules/filters.md index b2c80821bc7..a55ddd2b310 100644 --- a/skills/objectstack-query/rules/filters.md +++ b/skills/objectstack-query/rules/filters.md @@ -26,11 +26,8 @@ Every block below is a `where` fragment, so `check:skill-examples` skips it. | Null | `$exists` | `IS NOT NULL` / `IS NULL` | `{ metadata: { $exists: true } }` | | Null | `$empty` | `IS NULL`, `''` or `[]` by type | `{ notes: { $empty: true } }` | -**Case sensitivity is part of the contract** (`filter.zod.ts`): "`$contains` / -`$notContains` / `$startsWith` / `$endsWith` compare CASE-SENSITIVELY. -`$icontains` is the case-INSENSITIVE twin" — ASCII folding only, so `café` does -not match `CAFÉ`. Use `$icontains` for anything a human typed. `$like`/`$ilike` -match the WHOLE value, so a pattern with no wildcard is an exact comparison, not +Case sensitivity: SKILL.md, **String Operators**. `$like`/`$ilike` match the +WHOLE value, so a pattern with no wildcard is an exact comparison, not a substring search. Their reach is narrower than the rest of the table: the SQL family, `driver-memory` and `@objectstack/formula` answer them, while `driver-mongodb`, objectql `having` and service-analytics each keep an operator @@ -124,11 +121,19 @@ are. For OR, wrap them in a `$or` array (SKILL.md, **Logical Operators**). `{ age: { $contains: '25' } }` — the string operators are declared on strings only; use the comparison operators for numbers and dates. -### ⚠️ Prefer `$null` to a bare `null` comparand +### ⚠️ A column with no value fails every ordering test -`where: { deleted_at: null }` works — a bare null lowers to `IS NULL` on both -paths — but it reads as "equals null" and has no `IS NOT NULL` spelling. Write -`{ deleted_at: { $null: true } }` instead. +`$gt` / `$gte` / `$lt` / `$lte` / `$between` never match a row whose column is +null or absent — in `where`, an aggregation's `filter` and `having` alike. So +`{ amount: { $lte: 0 } }` skips the rows with no amount; to keep them, say so: + +```typescript +where: { $or: [{ amount: { $lte: 0 } }, { amount: { $null: true } }] } +``` + +State absence with `$null`: `null` is not an ordering comparand +(`{ $gte: null }` is refused, `INVALID_FILTER` / 400), and a bare +`{ deleted_at: null }` lowers to `IS NULL` but has no `IS NOT NULL` spelling. ## Date Filtering Patterns @@ -138,13 +143,6 @@ For **ad-hoc queries in application code**, compute the date yourself: where: { created_at: { $gte: new Date('2025-01-01') } } - -// Records within a date range -where: { - created_at: { - $between: [new Date('2025-01-01'), new Date('2025-03-31')] - } -} ``` ### Date Macros (declarative filter metadata) From b87277b668a3cf9a88eca20bea9cddb54dfcc858 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 16:25:34 +0000 Subject: [PATCH 2/2] docs(skills): a pushed-down measure filter needs the aggregate itself pushed down; re-lock the filters.md ceiling rules/aggregation.md, Filtered Aggregation: a driver declaring `supports.aggregationFilter` runs a per-measure `filter` in the database only when the aggregate itself is pushed down. A date bucket under a non-UTC `timezone`, or one the driver does not bucket natively, keeps the whole call in memory, measure filters included. HAVING: `avg` / `min` / `max` are `null` over no values, not only over no rows. Token ratchet: the rules/filters.md row is re-locked from 2482 to its measured 2473 (9892 bytes), zero headroom. Claude-Session: https://claude.ai/code/session_01ADHkmQokeaWJKGm2S7ZrDA Co-authored-by: Claude --- scripts/check-skills-token-ratchet.mjs | 8 +++++++- skills/objectstack-query/rules/aggregation.md | 12 +++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/scripts/check-skills-token-ratchet.mjs b/scripts/check-skills-token-ratchet.mjs index 488a0387be6..98364f51283 100644 --- a/scripts/check-skills-token-ratchet.mjs +++ b/scripts/check-skills-token-ratchet.mjs @@ -1069,7 +1069,13 @@ export const CEILINGS = new Map([ // file restates another). Maintainer ruling 2026-10-27 on the decision card // 「skills 修复卡的教学和 eval 都撞 token 天花板,怎么付?」, tap A, verbatim and // untranslated: 「A:先削重复,余额按实测提高」. - ['skills/objectstack-query/rules/filters.md', 2482], + // 2482 -> 2473: re-locked AT the measurement after the no-value ordering + // rule (a null or absent column fails `$gt` / `$gte` / `$lt` / `$lte` / + // `$between`) was paid in-file by two restatements: the case-sensitivity + // paragraph (home: SKILL.md, String Operators) and a second `$between` Date + // example (home: the operator table's `$between` row). Arithmetic in bytes: + // 9925 -> 9892 (-33 net) -> ceil(9892 / 4) = 2473; headroom 0 tokens. + ['skills/objectstack-query/rules/filters.md', 2473], ['skills/objectstack-query/rules/pagination.md', 1382], // objectstack-ui `rules/` — the five files the #14296 item 1 split created. diff --git a/skills/objectstack-query/rules/aggregation.md b/skills/objectstack-query/rules/aggregation.md index 0a80968b43a..94b13f35f8d 100644 --- a/skills/objectstack-query/rules/aggregation.md +++ b/skills/objectstack-query/rules/aggregation.md @@ -113,7 +113,7 @@ const rows = await engine.aggregate('order', { `having` filters *groups*; `where` filters the *input rows* before grouping — use `where` to shrink the scan, `having` to threshold the aggregates. `avg` / -`min` / `max` over no rows is `null`, and no value fails every ordering test +`min` / `max` over no values is `null`, and no value fails every ordering test ([filter rules](./filters.md)): `having: { average: { $lte: 0 } }` drops it. ## Filtered Aggregation (FILTER WHERE) @@ -121,10 +121,12 @@ use `where` to shrink the scan, `having` to threshold the aggregates. `avg` / > ✅ **Enforced.** A per-aggregation `filter` scopes that one measure, so a > total and a conditional count share ONE call. A driver that declares > `supports.aggregationFilter` (`driver-sql`; `driver-turso` outside remote -> mode) runs it in the database, compiled as that datasource's `where` is: it -> answers what the same condition answers there (text by the database -> collation) and is refused where it is — a `{ $field }` comparison on a column -> the object does not declare, `INVALID_FILTER` / 400. On any other driver the +> mode) runs it in the database whenever the aggregate itself is pushed down +> (a date bucket under a non-UTC `timezone`, or one the driver does not bucket +> natively, keeps the whole call in memory), compiled as that datasource's +> `where` is: it answers what the same condition answers there (text by the +> database collation) and is refused where it is — a `{ $field }` comparison on +> a column the object does not declare, `INVALID_FILTER` / 400. On any other driver the > engine reads the scoped rows and evaluates the filter itself. A column with > no value fails its ordering tests either way ([filter rules](./filters.md)).