Public repository: timo / feel

Reader

An interpreter of FEEL, written in Rust, with integrations for PostgreSQL, WASM, Lua, and Python.

docs: record ADR-18 (temporal types) + RD-2; update roadmap & README

public

docs: record ADR-18 (temporal types) + RD-2; update roadmap & README

ADR-18 captures the temporal slice: jiff, five flat Value variants (two
distinct durations), @"…" literals, the arithmetic table, the injected
clock, and the canonical ISO-8601 boundary (PG casts / WASM Date objects),
with the jiff-range deviations. RD-2 records why feel-pg returns ISO
strings rather than native-typed functions (the cast keeps the error path
in SQL, avoiding a longjmp through Rust). Roadmap marks temporal done and
lists the remaining deferrals (unary tests, special-char names); README
gains the built-in and temporal capabilities.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Changeset2ee94987b164c9f837850fda62f99e7553f31192
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parents79b7ecf3abc7
Children
Pushlog
by timo via ssh

modified DECISIONS.md +95 −5

@@ -404,6 +404,67 @@
404404
     built-in phrase (the oracle case). Both are temporal-adjacent and yield null
405405
     today, so the choice is observable only once temporal types exist.
406406
 
407
+## ADR-18: Temporal types (date, time, date and time, the two durations)
408
+
409
+- **Context:** Temporal support was deferred from the start (ADR-8) with the
410
+  PostgreSQL mapping pre-decided (ADR-5) and the multi-word *name* machinery
411
+  shipped (ADR-17, so `date and time(…)` already lexed). This is the largest
412
+  remaining slice: FEEL's five temporal types, their `@"…"` literals, arithmetic,
413
+  comparison, component access, and the temporal built-in functions.
414
+- **Decision:**
415
+  - **Library `jiff` (RD-/portable, ADR-1 holds).** Its calendar-aware `Span` /
416
+    `SignedDuration` map cleanly onto FEEL's two *distinct* duration types, it has
417
+    nanosecond precision, ISO-8601 parse/format, and a bundled IANA tzdb on
418
+    platforms without a system one (so WASM gets named zones). feel-core never
419
+    enables jiff's wasm-bindgen-pulling `js` feature; the wrappers do.
420
+  - **Five flat `Value` variants** (matching the existing flat style): `Date`,
421
+    `Time`, `DateTime` (each civil value + an optional zone preserved verbatim for
422
+    output: `Z` vs `+00:00` vs `@Area/City`), `YearsMonthsDuration(i64 months)`,
423
+    `DaysTimeDuration(SignedDuration)`. All temporal logic — parsing, canonical
424
+    ISO-8601 formatting, comparison/equality, arithmetic, component access — lives
425
+    in one `temporal` module; `eval`/`builtins` delegate.
426
+  - **`@"…"` literal** is a new lexer value token (its type is inferred from the
427
+    lexical form); a malformed literal is an error-null. Date strings are strict
428
+    4-digit-year; `24:00:00` rolls to next-day midnight; a zero offset is `Z`;
429
+    offsets are bounded to ±14:00; a named zone combined with an explicit offset,
430
+    and a leap second, are errors.
431
+  - **Arithmetic** follows the FEEL table: date/time/date-and-time ± duration,
432
+    date ± days-time-duration → date, point − point → days-time-duration,
433
+    duration ± duration, duration × / number, duration / duration → number. Two
434
+    date-times subtract only when both are *instants* (a date is midnight UTC; a
435
+    zoned date-time) or both *floating* (a zoneless date-time); a mix is a type
436
+    error. String `+` concatenates. Component access (`.year`, `.time offset`,
437
+    `.seconds`, …) extends `Expr::Path`.
438
+  - **Built-ins:** constructors `date`/`time`/`date and time`/`duration`/
439
+    `years and months duration` (with their arity overloads), components
440
+    `day of week`/`month of year` (names) and `day of year`/`week of year`
441
+    (numbers), and `abs` on durations (ADR-15 had deferred it).
442
+  - **Injected clock (generalises ADR-16's regex seam):** an `EvalEnv` carries the
443
+    regex provider *and* the instant `now()`/`today()` read. `CompiledExpr` gains
444
+    `evaluate_with_env`. feel-pg supplies `GetCurrentTransactionStartTimestamp()`
445
+    (transaction-stable, matching PostgreSQL's `now()`); feel-wasm supplies
446
+    `Date.now()`; tests inject a fixed instant (so the clock builtins are
447
+    deterministic).
448
+  - **Boundary = canonical ISO-8601** (durations `P1Y2M`, dates `2017-08-03`).
449
+    feel-pg emits ISO jsonb strings, so a plain SQL cast yields native
450
+    date/timestamptz/interval (activating ADR-5's mapping) with the cast's error
451
+    path staying in SQL. feel-wasm returns date/date-and-time as native JS `Date`
452
+    objects (ISO string fallback when `Date` can't represent them; time and
453
+    durations stay ISO strings) and accepts JS `Date` objects anywhere in the
454
+    context input.
455
+- **Consequence:** Reverses ADR-5's deferral; the `temporal-interval-mapping`
456
+  deviation is now live. Ingested 0007, 0095–0098, 1115–1117, 1120–1121 and the
457
+  temporal halves of 0100/0075/0099: **672 → 1709 fixture cases**. The WASM bundle
458
+  grows ~272 KB → ~738 KB (the bundled IANA tzdb — the accepted cost of full
459
+  zone-name support). ADR-1 is intact (`cargo tree -p feel-core` shows no
460
+  pgrx/wasm-bindgen).
461
+- **Deviations (deliberate, documented):** jiff caps the year at ±9999 and its
462
+  `Span` caps the years/months magnitude, so the TCK's extreme-range
463
+  dates/durations (and the odd `duration("PT0.S")` lexical form) cannot round-trip;
464
+  these are listed in `tools/ingest_tck.py`'s `TEMPORAL_RANGE_SKIPS`. Unary tests
465
+  (the non-interval `in` forms) and special-character/hyphen names stay out of
466
+  scope (see Roadmap).
467
+
407468
 ## Revisable design decisions
408469
 
409470
 Softer than the ADRs above: deliberate, **reversible** choices that are not
@@ -441,6 +502,31 @@
441502
   per-target divergence under ADR-16; and decide validation — the TCK fixtures are
442503
   XPath-leaning, so PG-dialect cases would need their own expectations.
443504
 
505
+### RD-2: feel-pg returns temporal values as ISO strings, not native PG types
506
+
507
+- **Weighed:** ADR-18 maps temporal results to canonical ISO-8601 jsonb strings,
508
+  so a caller casts (`… ::date`/`::timestamptz`/`::interval`) to get a native type.
509
+  The alternative is typed-returning SQL functions (`feel_eval_date`,
510
+  `feel_eval_ts`, `feel_eval_interval`) returning a native `pgrx::Date`/`Timestamp`/
511
+  `Interval`. Against that: (1) ISO + cast already yields every native type with no
512
+  extra runtime code; (2) crucially, the cast's *error* path runs in Postgres SQL —
513
+  it never `ereport`/longjmps **through Rust frames** (the RD-1 hazard); a
514
+  typed-returning variant would be safe only with explicit Rust-side range/
515
+  precision guards (Postgres' date range, ns→µs truncation) *before* `IntoDatum`;
516
+  (3) the result type of `feel_eval` is unknown until evaluation, so a single typed
517
+  function cannot serve all expressions — it would be a family of functions the
518
+  caller must pick between, which a cast does for free.
519
+- **Decision (2026-06-16):** feel-pg keeps `feel_eval … RETURNS jsonb` with temporal
520
+  values as ISO-8601 strings; conversion to native types is the documented cast
521
+  recipe. Nothing typed is built.
522
+- **Revisit trigger:** a concrete need to call `feel_eval` directly where a native
523
+  `date`/`timestamptz`/`interval` is required without a cast (e.g. a generated
524
+  column, or indexing the result) and the cast is too awkward.
525
+- **If flipped:** add `feel_eval_date`/`_ts`/`_interval` wrappers that evaluate as
526
+  today, then range/precision-check the temporal `Value` in Rust and build the
527
+  Datum via `IntoDatum` (never calling an `ereport`-ing C function), mapping an
528
+  out-of-range value to SQL NULL rather than risking a longjmp.
529
+
444530
 ## Roadmap: deferred scope (recorded, not yet built)
445531
 
446532
 These were considered and deliberately deferred to keep the slice thin. Listed so
@@ -458,11 +544,15 @@
458544
   `assert non null(x)` tripwire — an in-expression complement to the strict
459545
   boundary policy (ADR-14) for surfacing error-nulls where the author wants them
460546
   loud.
461
-- **Temporal types** — `date`/`time`/`date and time`/`duration` values, their
462
-  constructor functions, and `@"…"` literals. Largest lift; reverses ADR-5's
463
-  deferral and activates the placeholder `temporal-interval-mapping` deviation
464
-  rule. Unlocks 0007-date-time, 1115–1121, and the temporal halves of
465
-  0100-arithmetic / 0075-exponent.
547
+- **Temporal types** — *done (ADR-18).* The five temporal types, `@"…"` literals,
548
+  arithmetic/comparison/component access, the temporal built-ins, and an injected
549
+  clock landed; the boundary is canonical ISO-8601 (native PG casts; WASM `Date`
550
+  objects). Reversed ADR-5's deferral and made the `temporal-interval-mapping`
551
+  deviation live. *Still deferred:* **unary tests** (the non-interval `in` forms
552
+  `x in <10` / `x in =@"…"` / `x in (1,2,3)` / `x in not(3)` — a predicate with an
553
+  implicit operand, needing a grammar+eval increment independent of temporal);
554
+  special-character/hyphen names (`Date-Time`, ADR-17 deviation); and temporal
555
+  values outside jiff's representable range (ADR-18, `TEMPORAL_RANGE_SKIPS`).
466556
 - **inputNode context binding in ingestion** — *done (ADR-11/ADR-12).* Each
467557
   `<testCase>`'s `<inputNode>` values are captured into a per-fixture `context`,
468558
   built into a `Context` in the fixture test and bound during evaluation.

modified README.md +15 −5

@@ -25,11 +25,21 @@
2525
 - **Control flow:** `if … then … else …`, `for v in src return …`,
2626
   `some/every v in src satisfies …` (over list literals and integer ranges
2727
   `a..b`), and variable references.
28
-
29
-Built-in functions and temporal types are still out of scope. See
30
-[DECISIONS.md](DECISIONS.md) for the architecture decisions, the deferred-scope
31
-roadmap, and accepted deviations (notably: numbers are `f64`, not spec
32
-`decimal128`; type errors evaluate to `null`).
28
+- **Built-in functions:** single- and multi-word string / list / numeric /
29
+  context functions (`abs`, `string length`, `distinct values`, `get value`, …;
30
+  ADR-15/ADR-17).
31
+- **Temporal types:** `date`, `time`, `date and time`, and the two durations,
32
+  with `@"…"` literals, arithmetic / comparison / component access (`.year`,
33
+  `.time offset`, …), the temporal constructor and component built-ins
34
+  (`date(…)`, `day of week(…)`, …), and `now()` / `today()` (ADR-18). Values
35
+  cross the boundary as canonical ISO-8601 (native PostgreSQL `date`/`timestamptz`/
36
+  `interval` casts; WASM `Date` objects).
37
+
38
+Still out of scope: unary tests (the non-interval `in` forms), special characters
39
+in names, and user-defined functions. See [DECISIONS.md](DECISIONS.md) for the
40
+architecture decisions, the deferred-scope roadmap, and accepted deviations
41
+(notably: numbers are `f64`, not spec `decimal128`; type errors evaluate to
42
+`null`; temporal values outside jiff's range are skipped).
3343
 
3444
 ## Architecture invariant (Cardinal Rule)
3545