Repository navigation
Match a compared decimal constant written with another scale - #370
Merged
Merged
Conversation
… against a live n > 1.50) in checks, domain checks, generated columns, index predicates, policies and trigger conditions, also through the server probe.
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.
Fixes #359
Cause. PostgreSQL keeps a numeric constant's scale as written, so
pg_get_constraintdefprints1.50for a check declaredn > 1.50. The text comparison sawn>1.5andn>1.50as different. The server probe couldn't settle it either: it reads the declaredn > 1.5back verbatim asCHECK ((n > 1.5)), and that is not exactly the live text.Fix.
canonicalizeComparedNumbers(new fileinternal/diff/numeric_literal.go) runs first incanonicalizeExprNoting. It rewrites a decimal constant to its shortest spelling (1.50→1.5,.50→0.5,2.00→2.0) only where the constant is a whole operand of a comparison:<,>,=,<=,>=,<>,!=betweenboundinlist element, or an element of theARRAY[...]in= ANY/<> ALLIt looks past grouping parentheses, a unary sign, and casts to numeric types. A quoted constant such as
'-1.50'::numeric(how PostgreSQL prints a negative one) is rewritten only when it is cast to a numeric type.Scale can't change what a comparison returns, so this is safe. Everywhere else the spelling stays as written, because the scale shows in the result. That covers:
1.5vs1.50)x + 1.50)round(1.50))1.50::text)An integer constant against a decimal one (
n > 1vsn > 1.0) also stays a change. Their types differ, and on an integer column that changes the operator, which matters for partial-index predicate proofs.The probe verdict in
applyExprProbesAsnow accepts a read-back that differs from the live text only in compared constants' scale (sameComparedNumbers). This covers definitions the probe sees for other reasons. One example is anintcolumn: live((i)::numeric > 1.50)against declaredi > 1.5has a cast on one side only, so it is probed and read back as((i)::numeric > 1.5).pgy pullis unaffected. It writes the server's text, so pulled YAML already matched (checked: a pulled schema still diffs clean). The issue only concerned hand-written YAML.Tests
internal/diff/numeric_literal_test.go: equal and not-equal pairs, defaults keeping their scale, the probe read-back comparison,shortestDecimal.internal/integration/numeric_literal_test.go: a live schema written with1.50,-1.50,.50,9.90,3.00in checks onnumeric,numeric(5,2),intandfloat8columns, a domain check, a generated column, aninlist,between, a partial index predicate and a policy, against hand-written YAML using the other scale. Nothing is planned, with or without--unsafe. Changing the values (1.5→1.6) and a default's scale (1.50→1.5) are still planned and converge, and the scale-only definitions are left alone. Both tests fail without the fix.Docs:
docs/schema.md(How expressions are compared, Constraint alterations) anddocs/feature_support.md.make test,go vet ./...andgolangci-lint run ./...pass, and the integration suites pass on PostgreSQL 15, 18 and 19.