Skip to content

Conversation

@pyramation
Copy link
Collaborator

feat(deparser): add type-name quoting policy for minimal quoting in type positions

Summary

This PR fixes over-quoting of user-defined schema-qualified type names that contain PostgreSQL keywords. Previously, types like myschema.json, custom.int, and myapp.boolean were being emitted as myschema."json", custom."int", myapp."boolean" because the deparser applied strict identifier quoting to all type name parts.

The fix introduces a new "type-name quoting policy" that is less strict than standalone identifier quoting but still quotes RESERVED_KEYWORD:

  • Before: SELECT '{"d":4}'::myschema."json"
  • After: SELECT '{"d":4}'::myschema.json

Changes:

  • Add quoteIdentifierTypeName() function that only quotes RESERVED_KEYWORD (allows COL_NAME_KEYWORD and TYPE_FUNC_NAME_KEYWORD unquoted)
  • Add quoteTypeDottedName() for schema-qualified type names
  • Update TypeName handler to use new quoting for non-pg_catalog types
  • Add test fixtures for type casts and user-defined schema-qualified types
  • Update QUOTING-RULES.md with documentation

Review & Testing Checklist for Human

  • Verify PostgreSQL grammar correctness: Confirm that PostgreSQL actually allows COL_NAME_KEYWORD (json, int, boolean) and TYPE_FUNC_NAME_KEYWORD (interval) unquoted in type positions. Test by running the generated SQL against a real PostgreSQL instance.
  • Check edge cases with RESERVED_KEYWORD: Verify that types like myschema.select still get quoted correctly as myschema."select" (the snapshots suggest this works, but manual verification recommended)
  • Review the single-line change in deparser.ts: Line 1975-1977 is the critical change - verify quoteTypeDottedName() is the right choice for ALL non-pg_catalog types

Recommended test plan:

  1. Run pnpm test in packages/deparser - quoting tests should pass
  2. Parse and deparse SQL with user-defined schema-qualified types containing keyword names
  3. Execute the generated SQL against PostgreSQL to confirm it parses correctly

Notes

  • Pre-existing test failures (7 tests) related to case sensitivity (AlertLevel vs alertlevel) are unrelated to this change
  • This is part of a larger effort to implement minimal quoting rules across the deparser

Link to Devin run: https://app.devin.ai/sessions/713c834a5761453297e17345e1c1fd47
Requested by: Dan Lynch (@pyramation)

…ype positions

- Add quoteIdentifierTypeName() function that only quotes RESERVED_KEYWORD
- Add quoteTypeDottedName() function for schema-qualified type names
- Update TypeName handler to use quoteTypeDottedName() for non-pg_catalog types
- Add test fixtures for user-defined schema-qualified types with keyword names
- Update QUOTING-RULES.md with type-name quoting policy documentation

This fixes over-quoting of type names like myschema.json, custom.int, myapp.boolean
which were previously emitted as myschema."json", custom."int", myapp."boolean".
@devin-ai-integration
Copy link
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

devin-ai-integration bot pushed a commit that referenced this pull request Dec 25, 2025
This PR demonstrates the bug where user-defined schema-qualified types with keyword names
are over-quoted. The snapshots show:

- myschema."json" (should be myschema.json)
- custom."int" (should be custom.int)
- myapp."boolean" (should be myapp.boolean)

See PR #248 for the fix.
@pyramation pyramation merged commit 8efc2f8 into main Dec 25, 2025
14 checks passed
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.

2 participants