Skip to content

fix(sql): evaluate sequence-backed DEFAULT expressions on typed engines - #303

Open
EnRaiha wants to merge 1 commit into
NodeDB-Lab:mainfrom
EnRaiha:fix/issue294-seq-defaults
Open

fix(sql): evaluate sequence-backed DEFAULT expressions on typed engines#303
EnRaiha wants to merge 1 commit into
NodeDB-Lab:mainfrom
EnRaiha:fix/issue294-seq-defaults

Conversation

@EnRaiha

@EnRaiha EnRaiha commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

fix(sql): evaluate sequence-backed DEFAULT expressions on typed engines

Partially addresses #294.

Problem

DEFAULT nextval('name') passed DDL validation and then silently produced NULL on every insert: the pure default evaluator does not recognize sequence accessors, so the expression evaluated to "no value" and the column was omitted — including primary keys. The sequence machinery itself exists on the control plane (SequenceRegistry, catalog persistence, range allocation); the SQL DEFAULT path was never wired to it.

Fix

  • SequenceRegistry threaded from SharedState through QueryContext into ConvertContext (for_state / for_state_with_lease and both planning construction sites); sub-planner contexts keep None.
  • New evaluate_sequence_default: recognizes DEFAULT nextval('name'), advances the CP-side registry, and raises a loud PlanError naming the sequence when it does not exist — a DDL-accepted DEFAULT never silently vanishes into NULL again.
  • The INSERT/UPSERT expanded_rows loop prefers the sequence-aware evaluator over the pure stateless one.

Verified

Case Result
document_strict DEFAULT nextval('seq'), 3 inserts ids 1, 2, 3
DEFAULT nextval('no_such_seq') insert loud error naming the sequence
uuid_v7() default on strict unchanged, fills
wire sequence_default_typed 3 passed
nodedb-sql lib / clippy -D warnings / fmt 864 passed / clean / clean

Out of scope (tracked separately)

  • SELECT nextval/currval/setval evaluation — still 42883 (unchanged). The accessor registry registration ships together with the SELECT-side fold so the gate never passes a call that folds to NULL.
  • kv and columnar-family sequence defaults (their default fill paths are pure-evaluator based).
  • Schemaless document column defaults are dropped at the catalog adapter (type_convert.rs hardcodes default: None) — pre-existing, affects UUID defaults too; separate fix.
  • currval/setval session semantics.

How to test

cargo test -p nodedb-sql --lib
cargo nextest run -p nodedb --test wire -E 'test(sequence_default_typed)'

`DEFAULT nextval('name')` passed DDL and then silently produced NULL on
every insert: the pure default evaluator does not recognize sequence
accessors, so the expression evaluated to "no value" and the column was
omitted — including primary keys (NodeDB-Lab#294). The sequence machinery itself
exists on the control plane (SequenceRegistry with catalog persistence and
range allocation); this wires the SQL DEFAULT path to it.

- SequenceRegistry is threaded from SharedState through QueryContext into
  ConvertContext (for_state / for_state_with_lease and both planning
  sites); sub-planner contexts keep None.
- evaluate_sequence_default recognizes DEFAULT nextval('name'), advances
  the CP-side registry, and raises a loud PlanError for unknown sequences —
  a DDL-accepted DEFAULT never silently vanishes into NULL again.
- The INSERT/UPSERT expanded_rows loop prefers the sequence-aware evaluator
  over the pure stateless one.

Verified: document_strict DEFAULT nextval fills distinct ids (1,2,3) across
inserts; unknown sequence raises naming the sequence; uuid defaults
unchanged. Out of scope here (tracked): SELECT nextval/currval evaluation,
kv/columnar-family defaults, schemaless document column defaults (dropped
at catalog adapter — pre-existing), currval/setval session semantics.
Copilot AI lite review requested due to automatic review settings September 7, 2026 01:05
@EnRaiha EnRaiha added sev:2-high Major functionality broken; no acceptable workaround priority:P1 Fix in the current milestone status:needs-triage Awaiting maintainer triage (severity + priority) area:sql Parser, planner, SQL semantics labels Sep 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:sql Parser, planner, SQL semantics priority:P1 Fix in the current milestone sev:2-high Major functionality broken; no acceptable workaround status:needs-triage Awaiting maintainer triage (severity + priority)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants