Skip to content

fix: use JSON value identity in window partitions and correlated LIMIT - #31681

Merged
XuPeng-SH merged 2 commits into
mainfrom
fix/astra-meow-31636
Oct 29, 2026
Merged

XuPeng-SH merged 2 commits into
mainfrom
fix/astra-meow-31636

Conversation

@aptend

@aptend aptend commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #31636

What this PR does / why we need it:

Two SQL-visible symptoms came from comparing JSON partition keys by raw bytes:

  • PARTITION BY a JSON column split SQL-equal values: 1 and 1.0 landed in different window partitions and returned wrong ROW_NUMBER, COUNT and SUM.
  • The correlated ORDER BY ... LIMIT 1 subquery, rewritten as ROW_NUMBER over the correlation keys and joined back by JSON equality, returned multiple rows per outer row.

Both wrong results persisted through INSERT SELECT. Root cause: the non-ORDER comparator pkg/compare.New() fell back to a raw-byte strCompare for T_json (the colexec partition operator uses it to split window feeds), and pkg/partition.Partition() routed T_json to bytesPartition for the window operator's internal identity detector.

Fix: both paths now reuse the JSON value-identity detector that ORDER BY already applies (jsonCompare / jsonOrderPartition), matching the SQL = operator and GROUP BY / DISTINCT semantics. NULL / const-null / desc / cross-vector / ordinary-type contracts stay on the shared nullsCompare machinery and are locked by tests; varchar keeps byte identity.

Adds the distributed BVT case json_value_identity_window_31636 (+ golden) covering window ROW_NUMBER/COUNT/SUM, correlated LIMIT 1 and LIMIT 1 OFFSET 1, real SELECT plus INSERT SELECT persistence for both families, number spellings (1, 1.0, 1e0), objects, arrays and NULL, non-JSON decimal/varchar controls, duplicate-PK failure atomicity and same-connection reuse after the error.

Validation

  • mo-tester CI mode (run.sh -m run -n -g -o -i json_value_identity_window_31636 -r 100) against a fresh isolated instance: TOTAL: 33, SUCCESS: 33, SUCCESS RATE: 100%, exit code 0; the golden file is byte-stable before and after the run; the in-case Duplicate entry error occurs on the same JDBC connection and the following statements still pass (connection reuse).
  • Unfixed-main vs fixed runs of the same case with true per-statement exit codes: unfixed main returns the wrong window values (S1 yields 11 rows, and the correlated INSERT hits ERROR 1062 because the multi-row rewrite violates the primary key); fixed main returns SQL-correct values everywhere and the INSERT persists exactly 6 rows.
  • Unit tests on main 81082d34: 937 tests green across the four owning packages (pkg/compare, pkg/partition, pkg/sql/colexec/partition, pkg/sql/colexec/window) via the CGo test wrapper, exit code 0.
  • Scoped statics: go vet, molint check, golangci-lint run over ./pkg/compare/... ./pkg/partition/... all exit 0. Focused benchmark: JSON value-identity comparison costs 97.62 / 305.0 ns/op (0 allocs) versus the 7.69 / 7.66 ns/op raw-byte baseline.
  • Isolation: single foreground lifecycles on a private TCP/UDP range (18400-18499), fail-closed audits before any SQL (config targets, full-host enumeration, every listener owned by the exact service PID, SQL port exclusively ours, HAKeeper target pinned to 127.0.0.1:18491), precise PID cleanup after each run.

Notes

  • End-to-end runs used binaries built at main 7597f979 (RED 00373844…, FIX 95a41b9a…, identified by SHA and vcs stamps); 7597f979 -> 81082d34 adds only a test-only commit with a 0-line delta in the affected packages, so that evidence carries over, and the unit/static results above were rerun on the rebased source.
  • The historical local make exit codes for those binary builds were not independently preserved; this PR does not claim a verified raw make pass code for them.
  • mo-tester was run against the fixed tree only (the golden is generated from it); the unfixed-main evidence comes from the same case via direct SQL runs with per-statement exit codes.

Window PARTITION BY feeds compare their keys through pkg/compare.New,
which fell back to a raw-byte strCompare for T_json, and the window
operator's internal detector reused bytesPartition for the same type.
JSON 1 and 1.0 therefore split window partitions (wrong ROW_NUMBER,
COUNT and SUM), and the correlated pagination rewrite, which rejoins a
ROW_NUMBER over the correlation keys by JSON equality, returned multiple
rows per outer row. Both wrong results persisted through INSERT SELECT.

Both identity paths now reuse the JSON value-identity detector that
ORDER BY already applies: New() returns jsonCompare for T_json (matching
NewOrder and the SQL = operator), and partition.Partition routes T_json
to jsonOrderPartition. NULL/const-null/desc/cross-vector/ordinary-type
contracts stay on the shared nullsCompare machinery and are locked by
tests.

Adds the distributed case json_value_identity_window_31636 with golden,
covering window ROW_NUMBER/COUNT/SUM, correlated LIMIT 1 and OFFSET 1,
INSERT SELECT persistence of both families, number spellings, objects,
arrays and NULL, non-JSON controls, duplicate-PK failure atomicity and
same-connection reuse after the error.

Fixes #31636
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH

XuPeng-SH commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: d25dd030f018ee11b8bf0472058a1892723ec9e8

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

mo-bot review · mo-bot-local · review

Commit: d25dd030f018ee11b8bf0472058a1892723ec9e8

Assessment: approve

The change consistently applies JSON value identity across the affected partition paths. No actionable findings remain.

Model: gpt-6-astra / xhigh

Session: 01a13dcf-1517-7f90-b3f9-c5f9f9e7031b · slot 0 · generation 1

Validation:

  • All four owning/consumer packages passed: 937 named test/subtest results, with no failures or skips.

  • Independent boundary probe passed for NULL handling, selector order, accumulated boundaries and hash/comparator agreement.

  • Comparison benchmark completed with zero allocations for both JSON cases.

  • Preflight, native build and source/receipt/log audit passed. validation-audit.json retains the evidence mapping; source remained unchanged.

Limitations:

  • Public SQL validation relies on supplied BVT reports and successful frozen-head CI; raw BVT logs were unavailable and BVT was not rerun locally.

  • The comparison benchmark does not establish end-to-end query performance.

@mergify mergify Bot added the queued label Oct 29, 2026
@mergify

mergify Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

🚫 Left the queue

1 h 5 min in the queue, with no time running CI.

So far

  • ✅ Entered the queue Thu 29 Oct, 06:01 · rule main · triggered by rule Automatic queue on approval for main
  • ⏹️ Testing stopped · in place
  • 🚫 Left the queue 07:06 · 30c1051

Reason

Pull request #31681 has been merged manually at d950fdc

Hint

You were too fast!

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

Times in UTC · dashboard · how the merge queue works

@XuPeng-SH
XuPeng-SH merged commit d950fdc into main Oct 29, 2026
29 checks passed
@XuPeng-SH
XuPeng-SH deleted the fix/astra-meow-31636 branch October 29, 2026 07:05
@mergify mergify Bot removed the queued label Oct 29, 2026

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: JSON window partitions split equal numeric values and make correlated LIMIT 1 return multiple rows

3 participants