| @@ -2356,3 +2356,130 @@ |
| 2356 | 2356 | IMMUTABLE rejection). All suites pass (feel-pg via `cargo pgrx test pg18`). The research |
| 2357 | 2357 | record is `docs/special-char-names-research.md`; the follow-up increment is decision-result |
| 2358 | 2358 | binding in the ingester, which stands on its own feet (hyphen-free DRG chains need it too). |
| 2359 | + |
| 2360 | +## ADR-48: Name-resolution levels, backtick-quoted names, candidate-keyed parse cache, path-member probe |
| 2361 | + |
| 2362 | +- **Status:** *Proposed* (2026-10-14) — planned, not implemented. Builds on ADR-47. |
| 2363 | + |
| 2364 | +- **Context:** ADR-47 (v1) resolves special-character names against the root scope at parse |
| 2365 | + time. Its review and follow-up left four limits: |
| 2366 | + 1. `compile()` is probe-free, so `compile("a-b").evaluate({"a-b": 5})` is null while |
| 2367 | + `evaluate("a-b", {"a-b": 5})` is 5 — the compiled path cannot reach such names at all. |
| 2368 | + 2. feel-pg re-parses *every* row that binds a candidate (no cache), and `feel_eval_immutable` |
| 2369 | + rejects such rows with an ERROR. |
| 2370 | + 3. Only root keys are probed: `applicant.first-name` stays `applicant.first - name` — |
| 2371 | + probably the most common real-world shape (nested jsonb/JSON keys). |
| 2372 | + 4. Space-joined keyword names (`Profit and loss`, §10.3.1.6's own example) stay unsupported: |
| 2373 | + treating keyword adjacency as runs gives nearly every condition candidates. The |
| 2374 | + §10.3.1.4 name-start rule (runs start only at a `Name`) roughly halves them — |
| 2375 | + `if a then b else c`: 15 → 6, `a and b or c`: 10 → 6 (hand-counted) — but they remain |
| 2376 | + present in almost every condition. A name may still *end* with a keyword (`a then`): |
| 2377 | + the spec allows it, so those candidates stay. |
| 2378 | + |
| 2379 | + Cost model (measured with `name_run_candidates`): a run of *n* words yields n·(n−1)/2 |
| 2380 | + candidates; numbers, parentheses and comparison operators end a run. Typical expressions |
| 2381 | + have 0–4 (`applicant.age >= 18`: 1; `price * quantity - discount`: 3); the worst realistic |
| 2382 | + case is a name-only formula — `w1*x1 + … + w10*x10` is one 20-word run, 190 candidates. Per |
| 2383 | + evaluation a probing host pays one scope lookup per candidate; a *re-parse* only when a |
| 2384 | + candidate is bound. |
| 2385 | + |
| 2386 | + Prior art, researched in the feel-scala source (Camunda): names are plain Java identifiers |
| 2387 | + and are **never** resolved against the scope (`a-b` is always subtraction); special names |
| 2388 | + are written in backticks — `` `a-b` ``, `` `first name` ``, `` order.`total price` `` — |
| 2389 | + grammar `` "`" (!"`" AnyChar)+ "`" `` (≥ 1 char, no escape, content verbatim), accepted |
| 2390 | + wherever a name is (references, path members, `for`/quantifier variables, parameters, |
| 2391 | + context keys, call heads). Introduced in feel-scala 1.5.0 (2018, with `'`), switched to |
| 2392 | + backticks in 1.8.0 (2019, PR #63). Camunda documents it as a *Camunda extension*; its own |
| 2393 | + Modeler tooling rejects it (standard FEEL only) and points to `get value(ctx, "key")`. |
| 2394 | + feel-scala also accepts space names unquoted when followed by `(` or `:` (call heads, |
| 2395 | + parameter names, context keys) — a text-only lookahead. |
| 2396 | + |
| 2397 | + Spec (§10.3.1.4): "A name start (grammar rule 26) SHALL NOT be a literal terminal symbol"; |
| 2398 | + "a name part MAY be". Implemented on the ADR-47 branch: runs start only at a `Name` |
| 2399 | + (`x-in` is a name, `if-then` is not). |
| 2400 | + |
| 2401 | +- **Decision (planned):** |
| 2402 | + |
| 2403 | + 1. **Backtick-quoted names — a non-standard extension, always on.** Syntax identical to |
| 2404 | + feel-scala, so Camunda expressions run unchanged. Lexed as a **dedicated token** |
| 2405 | + (`Tok::QuotedName`) that becomes a name only in the grammar: it must never be joined by |
| 2406 | + `join_names` (`` `a b` c `` is not `a b c`) nor fused by the ADR-47 pass |
| 2407 | + (`` `a`-b `` is not `a-b`). Scope-independent — decided by the text alone — so it |
| 2408 | + works in `compile`, in the feel-pg cache and in `feel_eval_immutable` at zero per-row |
| 2409 | + cost, and in every slot the probe cannot reach: keyword names (`` `Profit and loss` ``), |
| 2410 | + inner scopes (`` applicant.`first-name` ``, `` items[`unit-price` > 10] ``), declarations |
| 2411 | + (`` {`a-b`: 1} ``, `` for `x-y` in … ``), `?` names (`` `what if?` ``). The ingester's |
| 2412 | + slice scanner treats a quoted name as one name. Marked as an extension in README and |
| 2413 | + docs. |
| 2414 | + |
| 2415 | + 2. **Name-resolution level as a parse option.** `#[non_exhaustive] enum NameResolution`: |
| 2416 | + - `Quoted` — backticks only; no run fusion, no candidates. Camunda semantics; cheapest. |
| 2417 | + - `Symbols` (**default**) — backticks plus ADR-47 runs over `- + * / .`. |
| 2418 | + - `Full` — additionally keyword-joined names (`Profit and loss`); **deferred** (see |
| 2419 | + below), the variant is reserved so it can land without an API break. |
| 2420 | + |
| 2421 | + API: `ParseOptions { names: NameResolution }` on `compile`/`evaluate` variants in |
| 2422 | + feel-core; a keyword argument in the wrappers (`feel.compile(expr, names="quoted")`); |
| 2423 | + in feel-pg a **function argument with a default** (`name_resolution text DEFAULT |
| 2424 | + 'symbols'`) — **never a GUC**: a session setting would make `feel_eval_immutable`'s |
| 2425 | + result depend on something other than its arguments and silently break expression |
| 2426 | + indexes. The level is stored in `CompiledExpr`, shown in its `Debug`/`repr`, and part |
| 2427 | + of every cache key. "Who wants spec compatibility pays with performance": `Full` costs |
| 2428 | + most, `Quoted` (Camunda compatibility) costs nothing. |
| 2429 | + |
| 2430 | + 3. **Candidate-keyed parse cache; `compile` becomes scope-adaptive.** The parse is a pure |
| 2431 | + function of *(text, level, which candidates are bound)* — fusion queries the probe only |
| 2432 | + for `name_run_candidates`. `CompiledExpr` therefore keeps the source, the level, the |
| 2433 | + candidates and a small bounded cache (≤ 8 entries, LRU, behind a `Mutex` — `Clone` and |
| 2434 | + pyo3 `Send`) of parses keyed by the bound-candidate bitset. `evaluate` computes the |
| 2435 | + bitset (one lookup per candidate; none when there are no candidates) and picks or |
| 2436 | + builds the matching parse. Consequences: |
| 2437 | + - `compile` and `evaluate` agree; the compiled wrapper classes get it for free. This |
| 2438 | + **reverses ADR-47 point 3** (probe-free compile); `has_name_runs()` becomes obsolete. |
| 2439 | + - feel-pg keeps the cache in `fn_extra`: no more per-row re-parse, one parse per |
| 2440 | + distinct bitset. |
| 2441 | + - `feel_eval_immutable` **drops the ERROR**: the result remains a pure function of |
| 2442 | + `(expr, context, level)`, which is all IMMUTABLE requires; a row that binds `a-b` |
| 2443 | + indexes the name reading, as the spec demands for that row. Documented. |
| 2444 | + - `FunctionRegistry::contains(name)` (default `get(name).is_some()`, overridden |
| 2445 | + allocation-free in py/lua/wasm) so `ScopeWithFunctions` no longer allocates an `Arc` |
| 2446 | + — or crosses into Python — per candidate per evaluation. |
| 2447 | + - Guard test: **no built-in name contains a name symbol** (`- + * / .`). Built-ins are |
| 2448 | + in scope, so a future built-in matching a candidate would silently change parses — |
| 2449 | + and stored index values. |
| 2450 | + - Benchmark: the 190-candidate formula (worst case) and `applicant.age >= 18` (the |
| 2451 | + RD-10 fast path) against today's numbers. |
| 2452 | + |
| 2453 | + 4. **Path-member probe (ROADMAP v2, inner scopes).** `ScopeProbe::contains_member(path, |
| 2454 | + name)` with a default of `false` (non-breaking). When a member run follows a *static* |
| 2455 | + path chain from a root name (`applicant.`, `p.q.`), the lexer asks the probe, which |
| 2456 | + navigates the context value; candidates become path-qualified |
| 2457 | + (`(["applicant"], "first-name")`) and take part in the bitset of 3. A list-valued step |
| 2458 | + (`orders.unit-price`) checks **any** element (deterministic; "first element" breaks on |
| 2459 | + empty or heterogeneous lists). Dynamic bases (`f(x).a-b`, `(e).a-b`) stay arithmetic — |
| 2460 | + backticks cover them. Filter-element keys (`items[unit-price > 10]`) stay deferred; |
| 2461 | + backticks cover them too. Cost: one jsonb navigation per path-qualified candidate per |
| 2462 | + row — benchmarked against the plain path case. |
| 2463 | + |
| 2464 | + **Order:** (A) 1 + 2 with `Quoted`/`Symbols` — `Quoted` is nearly free (skip the fusion |
| 2465 | + pass); (B) 3; (C) 4; (D) `Full` only on concrete demand, after measuring whether the TCK |
| 2466 | + uses unquoted keyword names at all. Deciding which keywords may join (`in` must not — |
| 2467 | + `for x in y`) and how `Full` interacts with the fixed joins (`date and time`, ADR-26) is |
| 2468 | + part of D; excluding `in` as a name part (as Drools does) removes loop headers from |
| 2469 | + `Full` candidates entirely. |
| 2470 | + |
| 2471 | +- **Rejected / deferred:** |
| 2472 | + - *GUC for the level* — breaks IMMUTABLE (see 2). |
| 2473 | + - *`Full` now* — cost and ambiguity for a shape with no measured demand; backticks give a |
| 2474 | + standard-adjacent escape meanwhile. |
| 2475 | + - *Escape for a backtick inside a quoted name* — feel-scala has none; staying identical |
| 2476 | + keeps expressions portable. |
| 2477 | + - *Schema-based probe* (DMN typeRefs/ItemDefinitions — the spec-intended way to know member |
| 2478 | + names, and the one that would make `compile` sound without a context) — needs a type |
| 2479 | + model the engine does not have. |
| 2480 | + - *Compile-time name lists* (`compile(expr, names=[…])`) — subsumed by 3. |
| 2481 | + |
| 2482 | +- **Consequence (expected):** one expression text can mean different things per level — |
| 2483 | + intended and made visible. Fixture suite runs at `Symbols`; the special-name tests run at |
| 2484 | + all levels. Camunda-written expressions run unchanged (`Quoted` or `Symbols`); spec-full |
| 2485 | + keyword names remain the one documented gap until D. |