Skip to content
Merged
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
10 changes: 10 additions & 0 deletions .changeset/33868-skills-no-value-ordering-measure-filter.md
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 7 additions & 1 deletion scripts/check-skills-token-ratchet.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions skills/objectstack-query/evals/filters-pagination-search.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
}
}
]
}
18 changes: 13 additions & 5 deletions skills/objectstack-query/rules/aggregation.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,23 @@ 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 values 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 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)).

<!-- os:check -->
```typescript
Expand Down
30 changes: 14 additions & 16 deletions skills/objectstack-query/rules/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down
Loading