wip: feat(postgres): handle postgres DEFAULT in schema changes - #4635
Draft
dtunikov wants to merge 1 commit into
Draft
wip: feat(postgres): handle postgres DEFAULT in schema changes#4635dtunikov wants to merge 1 commit into
dtunikov wants to merge 1 commit into
Conversation
Relation messages carry neither nullability nor defaults, so read both from pg_catalog for added columns in one lookup, replacing the nullable-only query. Constant defaults are translated to a dialect neutral SQL literal per the column's QValueKind; anything else is declined. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #4632, which added
FieldDescription.default_exprand the ClickHouse-side replay. This fills the field in for the Postgres source, so rows that predate anADD COLUMN ... DEFAULTread back the same value on both sides instead of ClickHouse's type zero.How
Relation messages carry neither nullability nor defaults, so both now come from one
pg_cataloglookup over the added columns, replacing the nullable-only query (and its replica-identity heuristic —attnotnullis authoritative for every column). Generated columns are excluded, since their expression also lives inpg_attrdef.pg_get_exproutput is not a bare literal: Postgres constant folds and type labels defaults, sosmallint DEFAULT -32768arrives as'-32768'::integer,numeric(10,2) DEFAULT 1.50as1.50,char(3)as'abc'::bpchar. The translator peels one cast label or paren wrapper, then validates the constant against the column'sQValueKind:'NaN'/'Infinity'declinedBoolean—true/falseTimestampTZ— the+00offset Postgres renders undertimezone=UTCis stripped, since the destination column carries no zoneAnything that is not a constant (
now(),nextval(...),(2 + 3), generated expressions) is declined, leaving the column without a default. Defaults are only translated for the Q type system; PG-type-system destinations splice the source type in verbatim.Tests
TestDefaultExprFromPostgresDefault— table-driven over whatpg_get_exprrenders, including hostile input ('x'::text || 'y'::text, chained casts, unterminated literals)TestAddedColumnCatalogInfo— same shape but against a live Postgres, so the rendering quirks above are pinned down rather than assumedTest_PG_AlterTableAddColumnDefault/..._Untranslated— pg→ClickHouse e2e; a row lands before theALTERso it reads through the ClickHouse default, and the untranslated case also coversTIMESTAMP DEFAULT 'infinity', which is translated but rejected by ClickHouse, exercising the retry-without-default fallbackAll four pass locally against the Tilt environment.
🤖 Generated with Claude Code