Skip to content

fix: strip quotes and uppercase EXTRACT field name in deparser#289

Open
pyramation wants to merge 4 commits intomainfrom
devin/1775200098-fix-extract-epoch-syntax
Open

fix: strip quotes and uppercase EXTRACT field name in deparser#289
pyramation wants to merge 4 commits intomainfrom
devin/1775200098-fix-extract-epoch-syntax

Conversation

@pyramation
Copy link
Copy Markdown
Collaborator

@pyramation pyramation commented Apr 3, 2026

Summary

The SQL deparser's FuncCall handler for EXTRACT was producing EXTRACT('epoch' FROM x) instead of the correct EXTRACT(EPOCH FROM x). The root cause: the first argument is an A_Const with an sval field, and A_Const deparsing wraps string values in single quotes via QuoteUtils.formatEString(). The EXTRACT branch then passed this quoted string through verbatim.

The fix strips surrounding single quotes and uppercases the field name before formatting the EXTRACT expression. This matches the behavior fixed in constructive-io/constructive-db#748 for the PL/pgSQL-native deparser.

Adds a kitchen-sink fixture (__fixtures__/kitchen-sink/misc/extract.sql) with 18 EXTRACT keyword variants (EPOCH, YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, CENTURY, MILLENNIUM, DOW, DOY, QUARTER, WEEK, TIMEZONE, TIMEZONE_HOUR, TIMEZONE_MINUTE) using the project's standard fixture-based testing pipeline.

Updates since last revision

  • Added AGENTS.md at the repo root with project overview, package listing, setup/build/test instructions, and code conventions.
  • Added .agents/skills/testing-fixtures/SKILL.md documenting the full fixture-based testing pipeline (how SQL fixtures → generated.json → kitchen-sink test files, how to add new fixtures, pretty-print test pattern, package scripts reference).
  • New: Added .agents/skills/code-generation/SKILL.md documenting the protobuf-based code generation pipeline (build:proto across 4 packages), type inference & narrowed type generation (pgsql-types), keyword list generation (@pgsql/quotes), and version-specific deparser generation.
  • New: Expanded the testing-fixtures skill to cover PL/pgSQL fixtures (plpgsql-deparser) and transform package kitchen-sink/test:ast workflows, with per-package script reference tables.
  • New: Expanded AGENTS.md with a Skills directory, comprehensive root and per-package script reference tables (organized by category: fixtures, code generation, version management, CLI), and a note about not hand-editing generated files.

Review & Testing Checklist for Human

  • Quote-stripping robustness: The fix checks for startsWith("'") && endsWith("'"). Verify this won't misfire on E-prefixed strings (E'...') — shouldn't happen since EXTRACT field names are simple keywords, but worth confirming formatEString never adds the E prefix for plain alpha strings.
  • Other COERCE_SQL_SYNTAX functions with same issue: Check whether date_part or any other pg_catalog.* function handled in this block has a similar quoting problem with its string arguments (e.g., TRIM, SUBSTRING, OVERLAY all use different arg patterns so likely fine, but worth a glance).
  • Verify round-trip: Run cd packages/deparser && npx jest __tests__/kitchen-sink/misc-extract.test.ts to confirm all 18 EXTRACT variants pass the parse→deparse→reparse AST comparison.
  • Review skill and AGENTS.md accuracy: Skim AGENTS.md, .agents/skills/testing-fixtures/SKILL.md, and .agents/skills/code-generation/SKILL.md to confirm that documented commands, directory paths, and workflow descriptions match reality. All three were written from code reading — spot-check a few commands (e.g., npm run build:proto in packages/utils, npm run infer in packages/pgsql-types, npm run keywords in packages/quotes) to verify they work as described.

Notes

  • All 291 test suites (711 tests, 424 snapshots) pass with this change
  • Tests use the standard fixture pipeline (npm run kitchen-sink) rather than ad-hoc test files — generated.json and the kitchen-sink test file were regenerated from the new fixture SQL
  • The code-generation skill documents workflows across utils, traverse, transform, pg-ast, pgsql-types, proto-parser, and quotes packages — all based on reading the actual scripts, not existing documentation (there was none)
  • Upstream ref: constructive-io/constructive-db#748

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

The EXTRACT SQL syntax handler was passing the A_Const sval value
directly, which gets single-quoted by QuoteUtils.formatEString().
This produced EXTRACT('epoch' FROM x) instead of the correct
EXTRACT(EPOCH FROM x).

Strip surrounding quotes and uppercase the field name to match
PostgreSQL's expected EXTRACT syntax.

Ref: constructive-io/constructive-db#748
@devin-ai-integration
Copy link
Copy Markdown
Contributor

🤖 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 and CI monitoring

Replace standalone extract.test.ts with fixture-based testing that
follows the project's standard pattern:
- Add __fixtures__/kitchen-sink/misc/extract.sql with EPOCH, YEAR,
  MONTH, DAY, HOUR, MINUTE, SECOND, CENTURY, MILLENNIUM, DOW, DOY,
  QUARTER, WEEK, TIMEZONE, TIMEZONE_HOUR, TIMEZONE_MINUTE
- Regenerate generated.json and kitchen-sink test file
- Remove ad-hoc misc/extract.test.ts
Add AGENTS.md with repo overview, package listing, setup instructions,
and code conventions.

Add .agents/skills/testing-fixtures/SKILL.md documenting the full
fixture-based testing pipeline: how SQL fixtures get split into
generated.json, how kitchen-sink tests are generated, and how to
add new fixtures.
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