@aeye/query 0.3.9 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -45,6 +45,17 @@ automatically gain the matching has-many field pointing back. The join key on
45
45
  either side resolves through each Type's *identity field* (the field of its
46
46
  first unique single-field index, else the field named `id`).
47
47
 
48
+ A relation field can be **compared directly to a value** in `= <> in notIn`,
49
+ where the value is an object keyed by the target's **primary key** (single or
50
+ composite) — `{ kind: 'comparison', op: '=', left: relRef, right: { kind:
51
+ 'param', name: 'u' } }` with `:u = { id: 5 }` (a single-key relation also
52
+ accepts a bare scalar). A **belongs-to** matches its FK columns; a **has-many**
53
+ matches by **membership** — `= value` is a correlated `EXISTS` testing that the
54
+ value's key is in the related set (`<>` / `notIn` → `NOT EXISTS`). Two relations
55
+ of the same target compare by their FK key (`order.customer = invoice.customer`);
56
+ a has-many may not be compared to another relation. Ordering / LIKE on a relation
57
+ is rejected — a relation compares by identity, not order.
58
+
48
59
  Indexes are **composite**: an ordered list of parts, each with a prefix
49
60
  distinct-row `count` (non-increasing); the index is unique iff its last part's
50
61
  `count === 1`. Text matching is **case-insensitive by default** — set a text
@@ -839,17 +850,32 @@ identifier; suffixed `_2`, `_3`, … on collision with a param the query already
839
850
  uses). Failure cases (`drill.no-aggregation` / `non-invertible` /
840
851
  `having-aggregate` / `window-unsupported`) return LLM-friendly `Problems`.
841
852
 
842
- ## Cost
853
+ ## Cost & estimation
843
854
 
844
- Every query has a bottom-up `{ rows, bytes }` estimate driven by Type counts +
845
- indexes. Constraints reject over-budget queries during validation.
855
+ Bottom-up estimates driven by each Type's cardinality (`count` rows, `bytes`
856
+ per row), its indexes + fixed **selectivity** for predicates (an indexed
857
+ equality narrows toward one row; a non-indexed one applies `EQ_SELECTIVITY`),
858
+ and per-Type / per-field `changes` rates. Five estimators hang off the engine:
846
859
 
847
860
  ```ts
848
- const cost = engine.cost(select); // { rows, bytes }
849
- const problems = engine.validateQuery(select, undefined, { maxRows: 100 });
850
- // → cost.rows-exceeded when the estimate blows past the cap
861
+ const cost = engine.cost(select); // { rows, bytes } — WORK to produce the result (scanned rows)
862
+ const output = engine.outputCost(select); // { rows, bytes } — SIZE of the result (delivered rows × projection width)
863
+ const affected = engine.affected(update); // { rows, types: [{ type, rows }] } — rows an INSERT/UPDATE/DELETE (or CTE) mutates
864
+ const refs = engine.references(select); // { types, fields, functions } — exactly what the query READS
865
+ const ttl = engine.changeInterval(select); // ms until the data behind it could change (0 = always, -1 = never, else fastest rate)
866
+
867
+ // Constraints reject over-budget queries during validation:
868
+ const problems = engine.validateQuery(select, undefined, { maxRows: 100, maxBytes: 1_000_000 });
869
+ // → cost.rows-exceeded / cost.bytes-exceeded when the estimate blows past a cap
851
870
  ```
852
871
 
872
+ `cost` estimates work (OR-aware, index-probe driven); `outputCost` sizes the
873
+ delivered result (post-WHERE/GROUP/DISTINCT, capped by LIMIT/OFFSET);
874
+ `affected` counts mutated rows per Type; `references` powers `changeInterval`,
875
+ which folds the read Types' / fields' / functions' `changes` rates into a single
876
+ freshness / cache-TTL signal. All accept the same execution-time
877
+ `options.params` / `filters` / `sort` so the estimate reflects what actually runs.
878
+
853
879
  ## Functions
854
880
 
855
881
  All four function shapes are uniform: declare a `FunctionDef` (name, shape,
package/aeye-query.md CHANGED
@@ -47,11 +47,11 @@ Every kind is one branch of the `ExprDef` union. Availability in the LLM schema
47
47
  | `relation-path` | Walks relation fields from a source (optionally ending at a scalar); planner synthesizes the joins. |
48
48
  | `binary` | Arithmetic `left <op> right` (`+ - * / %`). |
49
49
  | `unary` | `<op> operand` (`-` / `+`). |
50
- | `comparison` | `left <op> right` → boolean (`= <> < <= > >=`, `like`, `notLike`, `ilike`). |
50
+ | `comparison` | `left <op> right` → boolean (`= <> < <= > >=`, `like`, `notLike`, `ilike`). A RELATION operand compares by identity in `=`/`<>` only: RHS a same-target relation (FK key) or a `{ pk }` value param; a has-many compares by membership (correlated `EXISTS`). |
51
51
  | `logical` | Boolean connective `and` / `or` / `not` over operands. |
52
52
  | `is-null` | `value IS [NOT] NULL`. |
53
53
  | `between` | `value BETWEEN lower AND upper` (negatable). |
54
- | `in` | `value IN (list \| subquery)` (negatable). |
54
+ | `in` | `value IN (list \| subquery)` (negatable). A RELATION `value` matches list `{ pk }` values by FK key (belongs-to) or membership (has-many). |
55
55
  | `case` | `CASE WHEN … THEN … [ELSE …] END`. |
56
56
  | `function-call` | A scalar function call by name with named args. |
57
57
  | `aggregate` | An aggregate function over a group (`count(*)` = empty args); optional `distinct`. |
@@ -89,7 +89,7 @@ Everything is referenced by its **Type name** — there is no alias to invent or
89
89
  - **Single-expression query** — `{ kind: 'expr', expr }` for a scalar computation.
90
90
  - **Params** — `{ kind: 'param', name }` infers its type from use; bound at run/emit via `options.params`. Introspect with `query.params(engine)` → `ParamDef[]`.
91
91
  - **Filters** — a `filters` placeholder (`{ source, fields? }`) is authored by the LLM but the **predicate is supplied at execution time** by the developer: `options.filters` is a `Record<source, boolean Expr | ExprDef | null>`. The placeholder evaluates/emits it (vacuously `TRUE` when none). Introspect exposed sources + fields with `query.filters(engine)` → `Record<source, { fields: QueryField[] }>` (name + resolved type + nullability + kind, restricted to any `fields` allowlist); `query.filterSources()` lists targetable sources.
92
- - **Cost** — every query has a bottom-up `{ rows, bytes }` estimate (`engine.cost(query)`); `validateQuery(query, _, { maxRows })` rejects over-budget queries (`cost.rows-exceeded`).
92
+ - **Cost & estimation** — bottom-up estimates driven by Type `count`/`bytes`, indexes + selectivity, and per-Type/field `changes` rates. `engine.cost(query)` → `{ rows, bytes }` (the WORK to produce the result); `engine.outputCost(query)` → `{ rows, bytes }` (the SIZE of the result — delivered rows × projection width); `engine.affected(query)` → `{ rows, types[] }` (rows an INSERT/UPDATE/DELETE or CTE mutates, OR-aware via indexes); `engine.references(query)` → `{ types, fields, functions }` (exactly what the query reads, incl. execution-time `filters`/`sort` and called functions); `engine.changeInterval(query)` → ms until the data behind it could change (freshness/cache-TTL: `0` = always, `-1` = never, else the fastest read rate). `validateQuery(query, _, { maxRows, maxBytes })` rejects over-budget queries (`cost.rows-exceeded` / `cost.bytes-exceeded`).
93
93
 
94
94
  ## Write model & permissions
95
95