Skip to content

Commit f1a9233

Browse files
committed
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 <noreply@anthropic.com>
1 parent b8ac0e2 commit f1a9233

4 files changed

Lines changed: 45 additions & 21 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@objectstack/skills': patch
3+
---
4+
5+
docs(skills): the query skill teaches that a column with no value fails an ordering filter, and where a per-measure filter runs
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
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.

‎skills/objectstack-query/evals/filters-pagination-search.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@
7070
"must_contain": ["$empty: true"],
7171
"must_not_contain": []
7272
}
73+
},
74+
{
75+
"id": 8,
76+
"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.",
77+
"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.",
78+
"files": [],
79+
"assertions": {
80+
"must_contain": ["filter", "$or", "$lte: 0", "$null: true"],
81+
"must_not_contain": []
82+
}
7383
}
7484
]
7585
}

‎skills/objectstack-query/rules/aggregation.md‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,21 @@ const rows = await engine.aggregate('order', {
112112
```
113113

114114
`having` filters *groups*; `where` filters the *input rows* before grouping —
115-
use `where` to shrink the scan, `having` to threshold the aggregates.
115+
use `where` to shrink the scan, `having` to threshold the aggregates. `avg` /
116+
`min` / `max` over no rows is `null`, and no value fails every ordering test
117+
([filter rules](./filters.md)): `having: { average: { $lte: 0 } }` drops it.
116118

117119
## Filtered Aggregation (FILTER WHERE)
118120

119121
> ✅ **Enforced.** A per-aggregation `filter` scopes that one measure, so a
120-
> total and a conditional count share ONE call. Any aggregation carrying a
121-
> non-empty `filter` forces the in-memory path — no driver compiles a
122-
> conditional aggregate, and one reached directly refuses `NOT_IMPLEMENTED`;
123-
> unfiltered aggregations keep native push-down.
122+
> total and a conditional count share ONE call. A driver that declares
123+
> `supports.aggregationFilter` (`driver-sql`; `driver-turso` outside remote
124+
> mode) runs it in the database, compiled as that datasource's `where` is: it
125+
> answers what the same condition answers there (text by the database
126+
> collation) and is refused where it is — a `{ $field }` comparison on a column
127+
> the object does not declare, `INVALID_FILTER` / 400. On any other driver the
128+
> engine reads the scoped rows and evaluates the filter itself. A column with
129+
> no value fails its ordering tests either way ([filter rules](./filters.md)).
124130
125131
<!-- os:check -->
126132
```typescript

‎skills/objectstack-query/rules/filters.md‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@ Every block below is a `where` fragment, so `check:skill-examples` skips it.
2626
| Null | `$exists` | `IS NOT NULL` / `IS NULL` | `{ metadata: { $exists: true } }` |
2727
| Null | `$empty` | `IS NULL`, `''` or `[]` by type | `{ notes: { $empty: true } }` |
2828

29-
**Case sensitivity is part of the contract** (`filter.zod.ts`): "`$contains` /
30-
`$notContains` / `$startsWith` / `$endsWith` compare CASE-SENSITIVELY.
31-
`$icontains` is the case-INSENSITIVE twin" — ASCII folding only, so `café` does
32-
not match `CAFÉ`. Use `$icontains` for anything a human typed. `$like`/`$ilike`
33-
match the WHOLE value, so a pattern with no wildcard is an exact comparison, not
29+
Case sensitivity: SKILL.md, **String Operators**. `$like`/`$ilike` match the
30+
WHOLE value, so a pattern with no wildcard is an exact comparison, not
3431
a substring search. Their reach is narrower than the rest of the table: the SQL
3532
family, `driver-memory` and `@objectstack/formula` answer them, while
3633
`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**).
124121
`{ age: { $contains: '25' } }` — the string operators are declared on strings
125122
only; use the comparison operators for numbers and dates.
126123

127-
### ⚠️ Prefer `$null` to a bare `null` comparand
124+
### ⚠️ A column with no value fails every ordering test
128125

129-
`where: { deleted_at: null }` works — a bare null lowers to `IS NULL` on both
130-
paths — but it reads as "equals null" and has no `IS NOT NULL` spelling. Write
131-
`{ deleted_at: { $null: true } }` instead.
126+
`$gt` / `$gte` / `$lt` / `$lte` / `$between` never match a row whose column is
127+
null or absent — in `where`, an aggregation's `filter` and `having` alike. So
128+
`{ amount: { $lte: 0 } }` skips the rows with no amount; to keep them, say so:
129+
130+
```typescript
131+
where: { $or: [{ amount: { $lte: 0 } }, { amount: { $null: true } }] }
132+
```
133+
134+
State absence with `$null`: `null` is not an ordering comparand
135+
(`{ $gte: null }` is refused, `INVALID_FILTER` / 400), and a bare
136+
`{ deleted_at: null }` lowers to `IS NULL` but has no `IS NOT NULL` spelling.
132137

133138
## Date Filtering Patterns
134139

@@ -138,13 +143,6 @@ For **ad-hoc queries in application code**, compute the date yourself:
138143
where: {
139144
created_at: { $gte: new Date('2025-01-01') }
140145
}
141-
142-
// Records within a date range
143-
where: {
144-
created_at: {
145-
$between: [new Date('2025-01-01'), new Date('2025-03-31')]
146-
}
147-
}
148146
```
149147

150148
### Date Macros (declarative filter metadata)

0 commit comments

Comments
 (0)