Skip to content

PHOENIX-8120 PDecimal.coerceBytes ignores the sort order when it rescales - #2752

Open
apurtell wants to merge 1 commit into
apache:masterfrom
apurtell:PHOENIX-8120
Open

apurtell wants to merge 1 commit into
apache:masterfrom
apurtell:PHOENIX-8120

Conversation

@apurtell

@apurtell apurtell commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

LongViewIndexEnabledBaseRowKeyMatcherIT fails at random with an array index error when a random view constant for a descending DECIMAL column has more scale than the column and rounds to zero.

When a DECIMAL value has more scale than its column, the coerce rescales it. The rescale decoded and encoded the value in ascending order, whatever the actual and expected sort orders were. A descending value therefore became a wrong value, and a value that rounded to zero became an ascending zero that a later descending read could not parse.

The rescale now decodes in the actual sort order and encodes in the expected sort order. The path without a rescale does not change.

🤖 Generated with Claude Code

…ales

When a DECIMAL value has more scale than its column, coerceBytes
rescales the value. The rescale path decoded the bytes in ascending
order and encoded the result in ascending order. It ignored both the
actual and the expected sort order. For a DESC column the result was a
wrong value. When the decoded value rounded to zero, the result was
the one-byte ascending zero. A later read of that byte in descending
order went past the end of the array.

The rescale path now decodes in the actual sort order and encodes in
the expected sort order. The path without a rescale does not change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@apurtell
apurtell requested a balanced review from Copilot December 4, 2026 20:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@apurtell

apurtell commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Self-review (Claude Code, pr-review-toolkit) — Copilot could not review this PR because of a review quota limit, so this is an automated self-review in its place.

Suggestions

  • Add a deterministic regression test. phoenix-core-client/src/main/java/org/apache/phoenix/schema/types/PDecimal.java:380-383: only a randomized IT covers the rescale branch, and it fails only when a random draw happens to round to zero. Before this fix the branch also gave silently wrong values for ordinary DESC inputs. A small table-driven case in the existing PDataTypeTest would pin the contract cheaply. It should cover positive, negative and round-to-zero values, all four sort-order pairs, and both the value-object and bytes-only inputs.
  • Decode with the actual type. PDecimal.java:380: the bytes-only path decodes the source bytes as DECIMAL even when the source type is a different numeric type. The base class decodes with the actual type. This is pre-existing, but this line already changes, so passing the actual type as well would close the gap at no extra cost. A separate follow-up is also fine.
  • The no-rescale early return still ignores the sort order. PDecimal.java:371-373: when no rescale is needed, the bytes come back unchanged even if the actual and expected sort orders differ. The rescale branch and the base class now convert in that case. The description says this path stays unchanged on purpose, which suits a lean fix. Still, the two branches now follow different contracts, so a follow-up issue would be useful.

Strengths

  • The fix is small and symmetric. It decodes in the actual sort order and encodes in the expected one through existing overloads, which matches the base type's contract.
  • The same change also fixes the bytes path of isSizeCompatible, the UPSERT paths that encode a rescaled value for a DESC column, and the DESC rounded-to-zero case.
  • The description states the root cause, the failure mode and the scope clearly. The new comment is short, design-level prose.

Recommendation: merge after nits

🤖 Generated with Claude Code

@apurtell

apurtell commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Response to the self-review (Claude Code):

  • Deterministic regression test. The fix follows the lean policy for sweep fixes, so it uses the existing tests. Before the PR was opened, a scratch unit test checked the rescale branch. It used all four pairs of actual and expected sort order, positive and negative values, the value that rounds to zero from the CI failure, and 200,000 random values. All cases pass with the fix. Without the fix the zero case throws, and an ordinary DESC value decodes to a wrong number. That test can be added to PDataTypeTest if reviewers want it in the patch.
  • Decode with the actual type. Agreed that this gap exists. It is older than this patch and no failure in the sweep points to it, so it stays out of this fix as a follow-up.
  • No-rescale path ignores the sort order. Agreed. The Scope paragraph in the description gives the reason to leave it unchanged: a change there changes the bytes for callers that pass two different sort orders, so it needs its own review. It is a follow-up too.

No code change is needed for this review.

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