Skip to content

Match a compared decimal constant written with another scale - #370

Merged
archlemon merged 1 commit into
mainfrom
numeric-literal-compare
Nov 21, 2026
Merged

archlemon merged 1 commit into
mainfrom
numeric-literal-compare

Conversation

@archlemon

Copy link
Copy Markdown
Member

Fixes #359

Cause. PostgreSQL keeps a numeric constant's scale as written, so pg_get_constraintdef prints 1.50 for a check declared n > 1.50. The text comparison saw n>1.5 and n>1.50 as different. The server probe couldn't settle it either: it reads the declared n > 1.5 back verbatim as CHECK ((n > 1.5)), and that is not exactly the live text.

Fix. canonicalizeComparedNumbers (new file internal/diff/numeric_literal.go) runs first in canonicalizeExprNoting. 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:

  • <, >, =, <=, >=, <>, !=
  • a between bound
  • an in list element, or an element of the ARRAY[...] in = ANY / <> ALL

It 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:

  • defaults (1.5 vs 1.50)
  • arithmetic (x + 1.50)
  • function arguments (round(1.50))
  • casts to text (1.50::text)
  • exponent forms

An integer constant against a decimal one (n > 1 vs n > 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 applyExprProbesAs now 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 an int column: live ((i)::numeric > 1.50) against declared i > 1.5 has a cast on one side only, so it is probed and read back as ((i)::numeric > 1.5).

pgy pull is 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 with 1.50, -1.50, .50, 9.90, 3.00 in checks on numeric, numeric(5,2), int and float8 columns, a domain check, a generated column, an in list, 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) and docs/feature_support.md.

make test, go vet ./... and golangci-lint run ./... pass, and the integration suites pass on PostgreSQL 15, 18 and 19.

… against a live n > 1.50) in checks, domain checks, generated columns, index predicates, policies and trigger conditions, also through the server probe.
@archlemon
archlemon merged commit 337b62b into main Nov 21, 2026
8 checks passed
@archlemon
archlemon deleted the numeric-literal-compare branch November 21, 2026 03:42
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.

Check constraint with a numeric literal 1.5 against live 1.50 counts as changed

1 participant