@aeye/query 0.6.3 → 0.6.4

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
@@ -353,6 +353,15 @@ Everything composes around that one call:
353
353
  is used and is bound at run time via `options.params`. Introspect what a built
354
354
  query expects with `query.params(engine)` → `ParamDef[]` (name + inferred
355
355
  type).
356
+
357
+ It reports every param the statement binds **at any depth**, so what it
358
+ declares is always enough to bind the emitted SQL. That includes a
359
+ `limit` / `offset` bound, which lives outside the walked expr tree: on the
360
+ statement itself, on a **`cte`'s `final`** (where `autoPaginate` puts it), on a
361
+ set-operation **arm** or its set-level bound, and inside a FROM / `in` /
362
+ `exists` subquery or an `insert … select` source. An under-reported bound is
363
+ not a cosmetic gap — the SQL still emits `LIMIT ?` and a caller that binds the
364
+ declared signature leaves it NULL, which Postgres reads as *no limit*.
356
365
  - **Filters.** `options.filters` is a `Record<source, ExprDef | Expr | null>` —
357
366
  a single **boolean Expr** per source (or `null` / absent for none). The
358
367
  `filters` EXPR in the query is only a placeholder (`{ source, fields? }`); the
package/aeye-query.md CHANGED
@@ -94,7 +94,7 @@ Everything is referenced by its **Type name** — there is no alias to invent or
94
94
  - **CTEs** — `{ kind: 'cte', ctes, final }`. Each entry is either non-recursive (`{ name, query }`) or **recursive** (`{ name, base, recursive }` — a seed UNION-ed with an arm reading the CTE's own accumulating rows to a fixpoint, iteration-capped). Recursion is its own structural shape (no `recursive?` flag).
95
95
  - **DML** — `insert` (row tuples or a `select`, optional `returning`, optional `onConflict`), `update` (`set` assignments, optional `joins`/`where`/`returning`), `delete` (optional `joins`/`where`/`returning`). `onConflict` = conflict-target `fields` plus `doNothing` or an `update` assignment list (which may reference the `excluded` proposed row).
96
96
  - **Single-expression query** — `{ kind: 'expr', expr }` for a scalar computation.
97
- - **Params** — `{ kind: 'param', name }` infers its type from use; bound at run/emit via `options.params`. Introspect with `query.params(engine)` → `ParamDef[]`.
97
+ - **Params** — `{ kind: 'param', name }` infers its type from use; bound at run/emit via `options.params`. Introspect with `query.params(engine)` → `ParamDef[]`. It reports **every param the statement binds, at any DEPTH** — including a `limit`/`offset` bound, which lives outside the expr tree, wherever it sits: on a `cte`'s `final` (where `autoPaginate` puts it), on a set-operation arm or its set-level bound, or inside a FROM / `in` / `exists` subquery or an `insert … select` source. So what `params()` declares is always enough to BIND the emitted SQL — an under-reported bound emits `LIMIT ?` with nothing to fill it, and `LIMIT NULL` is *no limit* (the whole table), not an error.
98
98
  - **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.
99
99
  - **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`).
100
100
 
package/dist/index.d.ts CHANGED
@@ -6444,14 +6444,34 @@ declare abstract class Query {
6444
6444
  * Params that are referenced but never observed against any type (so no type
6445
6445
  * can be inferred) are OMITTED here — they carry no `type` to report;
6446
6446
  * `validateQuery` surfaces them separately as `param.untyped`.
6447
+ *
6448
+ * Out-of-tree bounds (`limit` / `offset`) are picked up by the same walk —
6449
+ * each kind that OWNS a row bound observes it from its own `validateWalk`
6450
+ * (see {@link observeRowBounds}), so a bound at ANY nesting depth lands here.
6447
6451
  */
6448
6452
  params(engine: QueryEngine, scope?: QueryScope): ParamDef[];
6449
6453
  /**
6450
- * Observe any bind params that live OUTSIDE this query's walked expr tree
6451
- * (e.g. a SELECT's `limit` / `offset`), so `params()` reports them with the
6452
- * right inferred type. Base: none. Overridden by `SelectQuery`.
6454
+ * Observe a `limit` / `offset` ROW BOUND that is bound to a `param` (see
6455
+ * `autoPaginate`) against a number field type — a bound is always a row count
6456
+ * — so `params()` reports it with an inferred type. A bound lives OUTSIDE the
6457
+ * walked expr tree, so nothing else in the walk ever reaches it.
6458
+ *
6459
+ * INVARIANT: the kind that EMITS a row bound is the kind that observes it, and
6460
+ * it does so from its own `validateWalk`. That is what makes the report correct
6461
+ * at every NESTING DEPTH — `validateWalk` already recurses into a CTE body and
6462
+ * its `final`, a set-operation arm, and a FROM / IN / EXISTS subquery, and every
6463
+ * scope in that recursion shares ONE `ParamSet`. Observing only at the ROOT
6464
+ * (what `params()` used to do, via a `SelectQuery`-only hook) reported a paged
6465
+ * `cte` as taking NO params at all while `toSQL` still emitted
6466
+ * `LIMIT $1 OFFSET $2` — an unbindable statement whose declared signature was
6467
+ * empty (bug A17).
6468
+ *
6469
+ * The observation is recorded at the walk's CURRENT path (`p.here`, the
6470
+ * accessor that exists for exactly this), so a `param.conflict` naming a bound
6471
+ * points at the statement that OWNS it (`final.limit`) rather than at a
6472
+ * root-relative `limit` that does not exist.
6453
6473
  */
6454
- protected observeBoundParams(_params: ParamSet): void;
6474
+ protected observeRowBounds(scope: QueryScope, p: Problems, limit: number | ParamExprDef | undefined, offset: number | ParamExprDef | undefined): void;
6455
6475
  /**
6456
6476
  * The source names this query exposes for execution-time `filters` — i.e. the
6457
6477
  * sources a caller may target with `engine.run(query, { filters: [{ kind:
@@ -8689,13 +8709,6 @@ declare class SelectQuery extends Query {
8689
8709
  * `isRoot` marks the entry query being run/emitted (threaded on the context).
8690
8710
  */
8691
8711
  private effectiveDefaultOrder;
8692
- /**
8693
- * `limit` / `offset` may be bound to a `param` (see `autoPaginate`), but they
8694
- * live outside the walked expr tree, so `params()` never observes them. They
8695
- * are always integer row counts, so observe each against a number field type
8696
- * — making them surface in `params()` with the right inferred type.
8697
- */
8698
- protected observeBoundParams(params: ParamSet): void;
8699
8712
  /**
8700
8713
  * Estimate result `{ rows, bytes }` the way a SQL engine PROCESSES the query.
8701
8714
  *
@@ -8866,7 +8879,7 @@ declare class InsertQuery extends Query {
8866
8879
  private conflictScope;
8867
8880
  /** Resolve the output fields from the RETURNING projection against the target scope. */
8868
8881
  outputFields(engine: QueryEngine, scope: QueryScope): QueryField[];
8869
- /** Validate the target type, fields, row homogeneity, RETURNING, and ON CONFLICT assignments. */
8882
+ /** Validate the target type, the source SELECT, fields, row homogeneity, RETURNING, and ON CONFLICT assignments. */
8870
8883
  validateWalk(engine: QueryEngine, scope: QueryScope, p: Problems, _ctx: ValidateContext): void;
8871
8884
  /** The target type plus any types read by an INSERT … SELECT source. */
8872
8885
  referencedTypes(): readonly string[];
@@ -9168,8 +9181,6 @@ declare class SetOperationQuery extends Query {
9168
9181
  validateWalk(engine: QueryEngine, scope: QueryScope, p: Problems, ctx: ValidateContext): void;
9169
9182
  /** The union of both arms' referenced Type names. */
9170
9183
  referencedTypes(): readonly string[];
9171
- /** Set-level `limit` / `offset` params live outside the walked expr tree. */
9172
- protected observeBoundParams(params: ParamSet): void;
9173
9184
  /** Estimate the WORK `{ rows, bytes }`: combine both arms' cost per operation, then cap by LIMIT. */
9174
9185
  cost(ctx: CostContext, scope: QueryScope): Cost;
9175
9186
  /** Estimate the RESULT SIZE: combine both arms' `outputCost` per operation, then cap by LIMIT. */
package/dist/index.js CHANGED
@@ -9223,21 +9223,44 @@ var Query = class {
9223
9223
  * Params that are referenced but never observed against any type (so no type
9224
9224
  * can be inferred) are OMITTED here — they carry no `type` to report;
9225
9225
  * `validateQuery` surfaces them separately as `param.untyped`.
9226
+ *
9227
+ * Out-of-tree bounds (`limit` / `offset`) are picked up by the same walk —
9228
+ * each kind that OWNS a row bound observes it from its own `validateWalk`
9229
+ * (see {@link observeRowBounds}), so a bound at ANY nesting depth lands here.
9226
9230
  */
9227
9231
  params(engine, scope) {
9228
9232
  engine.registry.finalize();
9229
9233
  const s = scope ?? engine.globalScope();
9230
9234
  const p = new Problems();
9231
9235
  this.validateWalk(engine, s, p, ROOT_VALIDATE_CONTEXT);
9232
- this.observeBoundParams(s.params);
9233
9236
  return s.params.toJSON();
9234
9237
  }
9235
9238
  /**
9236
- * Observe any bind params that live OUTSIDE this query's walked expr tree
9237
- * (e.g. a SELECT's `limit` / `offset`), so `params()` reports them with the
9238
- * right inferred type. Base: none. Overridden by `SelectQuery`.
9239
+ * Observe a `limit` / `offset` ROW BOUND that is bound to a `param` (see
9240
+ * `autoPaginate`) against a number field type — a bound is always a row count
9241
+ * — so `params()` reports it with an inferred type. A bound lives OUTSIDE the
9242
+ * walked expr tree, so nothing else in the walk ever reaches it.
9243
+ *
9244
+ * INVARIANT: the kind that EMITS a row bound is the kind that observes it, and
9245
+ * it does so from its own `validateWalk`. That is what makes the report correct
9246
+ * at every NESTING DEPTH — `validateWalk` already recurses into a CTE body and
9247
+ * its `final`, a set-operation arm, and a FROM / IN / EXISTS subquery, and every
9248
+ * scope in that recursion shares ONE `ParamSet`. Observing only at the ROOT
9249
+ * (what `params()` used to do, via a `SelectQuery`-only hook) reported a paged
9250
+ * `cte` as taking NO params at all while `toSQL` still emitted
9251
+ * `LIMIT $1 OFFSET $2` — an unbindable statement whose declared signature was
9252
+ * empty (bug A17).
9253
+ *
9254
+ * The observation is recorded at the walk's CURRENT path (`p.here`, the
9255
+ * accessor that exists for exactly this), so a `param.conflict` naming a bound
9256
+ * points at the statement that OWNS it (`final.limit`) rather than at a
9257
+ * root-relative `limit` that does not exist.
9239
9258
  */
9240
- observeBoundParams(_params) {
9259
+ observeRowBounds(scope, p, limit, offset) {
9260
+ const numeric3 = new NumberFieldType();
9261
+ const at = p.here;
9262
+ if (limit !== void 0 && typeof limit !== "number") scope.params.observe(limit.name, numeric3, [...at, "limit"]);
9263
+ if (offset !== void 0 && typeof offset !== "number") scope.params.observe(offset.name, numeric3, [...at, "offset"]);
9241
9264
  }
9242
9265
  /**
9243
9266
  * The source names this query exposes for execution-time `filters` — i.e. the
@@ -11087,6 +11110,7 @@ var SelectQuery = class _SelectQuery extends Query {
11087
11110
  (o, i) => o instanceof SorterExpr ? p.at(i, () => o.sorts.forEach((e2, name) => p.at(["sorts", name], () => this.checkGrouped(e2, groupCodes, p)))) : p.at([i, "expr"], () => this.checkGrouped(o.expr, groupCodes, p))
11088
11111
  ));
11089
11112
  }
11113
+ this.observeRowBounds(scope, p, this.limit, this.offset);
11090
11114
  }
11091
11115
  /**
11092
11116
  * The GROUP BY keys as `toCode()` strings, with an `output` key EXPANDED to the
@@ -11242,21 +11266,6 @@ var SelectQuery = class _SelectQuery extends Query {
11242
11266
  const inScope = scope === "all" || (scope === "paginated" ? paginated : isRoot || paginated);
11243
11267
  return inScope ? order : void 0;
11244
11268
  }
11245
- /**
11246
- * `limit` / `offset` may be bound to a `param` (see `autoPaginate`), but they
11247
- * live outside the walked expr tree, so `params()` never observes them. They
11248
- * are always integer row counts, so observe each against a number field type
11249
- * — making them surface in `params()` with the right inferred type.
11250
- */
11251
- observeBoundParams(params) {
11252
- const numeric3 = new NumberFieldType();
11253
- if (this.limit !== void 0 && typeof this.limit !== "number") {
11254
- params.observe(this.limit.name, numeric3, ["limit"]);
11255
- }
11256
- if (this.offset !== void 0 && typeof this.offset !== "number") {
11257
- params.observe(this.offset.name, numeric3, ["offset"]);
11258
- }
11259
- }
11260
11269
  // ─── Cost estimation ─────────────────────────────────────────────────────
11261
11270
  /**
11262
11271
  * Estimate result `{ rows, bytes }` the way a SQL engine PROCESSES the query.
@@ -12068,7 +12077,7 @@ var InsertQuery = class _InsertQuery extends Query {
12068
12077
  const inner = this.targetScope(engine, scope);
12069
12078
  return this.returning.map((c, i) => makeField(fieldNameOf(c.expr, c.as, i), c.expr.resolve(engine, inner)));
12070
12079
  }
12071
- /** Validate the target type, fields, row homogeneity, RETURNING, and ON CONFLICT assignments. */
12080
+ /** Validate the target type, the source SELECT, fields, row homogeneity, RETURNING, and ON CONFLICT assignments. */
12072
12081
  validateWalk(engine, scope, p, _ctx) {
12073
12082
  const type = engine.type(this.into);
12074
12083
  if (!type) {
@@ -12128,6 +12137,8 @@ var InsertQuery = class _InsertQuery extends Query {
12128
12137
  }
12129
12138
  const inner = this.targetScope(engine, scope);
12130
12139
  const ctx = { inAggregate: false, inWindow: false, allowAggregate: true, groupKeys: [], inGroupBy: false };
12140
+ const source = this.select;
12141
+ if (source) p.at("select", () => source.validateWalk(engine, scope, p, ctx));
12131
12142
  p.at("returning", () => {
12132
12143
  this.returning.forEach(
12133
12144
  (c, i) => p.at([i, "expr"], () => c.expr.validateWalk(engine, inner, p, identityValueCtx(c.expr, ctx)))
@@ -13083,17 +13094,12 @@ var SetOperationQuery = class _SetOperationQuery extends Query {
13083
13094
  this.order.forEach((o, i) => p.at([i, "expr"], () => o.expr.validateWalk(engine, inner, p, colCtx)));
13084
13095
  });
13085
13096
  }
13097
+ this.observeRowBounds(scope, p, this.limit, this.offset);
13086
13098
  }
13087
13099
  /** The union of both arms' referenced Type names. */
13088
13100
  referencedTypes() {
13089
13101
  return [.../* @__PURE__ */ new Set([...this.left.referencedTypes(), ...this.right.referencedTypes()])];
13090
13102
  }
13091
- /** Set-level `limit` / `offset` params live outside the walked expr tree. */
13092
- observeBoundParams(params) {
13093
- const numeric3 = new NumberFieldType();
13094
- if (this.limit !== void 0 && typeof this.limit !== "number") params.observe(this.limit.name, numeric3, ["limit"]);
13095
- if (this.offset !== void 0 && typeof this.offset !== "number") params.observe(this.offset.name, numeric3, ["offset"]);
13096
- }
13097
13103
  /** Estimate the WORK `{ rows, bytes }`: combine both arms' cost per operation, then cap by LIMIT. */
13098
13104
  cost(ctx, scope) {
13099
13105
  return this.capByLimit(this.combineBranches(this.left.cost(ctx, scope), this.right.cost(ctx, scope)), ctx);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeye/query",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
4
4
  "description": "Query - An LLM-friendly relational query language, runtime & SQL converter",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",