Skip to content

feat(pg-codegen): emit per-column field decoders for projections - #1731

Merged
pyramation merged 1 commit into
mainfrom
feat/pg-codegen-field-decoders
Aug 14, 2026
Merged

feat(pg-codegen): emit per-column field decoders for projections#1731
pyramation merged 1 commit into
mainfrom
feat/pg-codegen-field-decoders

Conversation

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Summary

The record decoders only accept a whole table row — every NOT NULL column present, or CoerceError. Most real queries don't return one: they project a subset, alias columns, and join. Auditing constructive-db's 280 unchecked row.x as string casts, the single biggest site (157 casts, compute/lib/module-loader/src/loaders.ts) is entirely of that shape:

SELECT am.scope, am.thread_table_name, ps.schema_name AS public_schema
FROM metaschema_modules_public.agent_module am
LEFT JOIN metaschema_public.schema ps ON ps.id = am.schema_id

so there was nothing generated it could use. Each record module now also exports <TABLE>_FIELDS, one decoder per column:

export const AGENT_RUNS_FIELDS = {
  /** `id` (uuid) */ id: (value: unknown, label = 'agent_runs.id'): string => requireUuid(value, label),
  /** `status` (run_status) */ status: (value: unknown, label = 'agent_runs.status'): RunStatus => requireRunStatus(value, label),
  /** `finished_at` (timestamptz) */ finishedAt: (value: unknown): string | null => asIsoString(value)
} as const;

A NOT NULL column's decoder is strict and takes an overridable label so a projection names its own alias (FIELDS.name(row.thread_table_name, 'agent_module.thread_table')); a nullable column's is lenient and has no label, because it cannot fail. decodeExpression was generalized to take value+label expressions instead of a key, so the whole-record decoder (record.foo, `${label}.foo`) and the field decoder (value, label) are the same definition of a column's runtime check — a test asserts they agree column-by-column over a whole row.

Also replaces the README, which still documented v1's removed class X implements X output and a generateCodeTree/pg-codegen/introspect API that no longer exists.

Link to Devin session: https://app.devin.ai/sessions/b1fb8d90ec0a4cd1bd95e3c77c6f6680
Requested by: @pyramation

A joined or partial SELECT is not a whole table row, so the record decoders
reject it. Each record module now also exports <TABLE>_FIELDS: one decoder per
column, sharing the record decoders' definition of the column's runtime check.

Also replaces the README, which still described the removed v1 class output.
@pyramation pyramation self-assigned this Aug 14, 2026
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@pyramation
pyramation merged commit 975373f into main Aug 14, 2026
5 checks passed
@pyramation
pyramation deleted the feat/pg-codegen-field-decoders branch August 14, 2026 23:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant