Skip to content

perf(plan): eliminate bounded groups on not-null unique keys - #29225

Merged
XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/optimizer-roadmap-27856-27860
Sep 22, 2026
Merged

XuPeng-SH merged 2 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/optimizer-roadmap-27856-27860

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Sep 22, 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:

issue #27856 (first logical-proof phase; does not claim to complete the full roadmap)

Related roadmap issues: #27859, #27860, #27858, #27857, #27753, #27728

What this PR does / why we need it:

This PR adds a planner-local, fail-closed uniqueness proof for direct scans of ordinary tables. When a complete built secondary UNIQUE key is declared NOT NULL and exactly covers the GROUP BY keys, bounded singleton groups can eliminate the aggregate operator. This avoids scanning and aggregating rows that are already proven to be one row per group.

The proof deliberately rejects nullable, partial, prefix, plugin, generated, hidden, partitioned, temporary, malformed, and unbuilt keys. Existing primary-key behavior is preserved. The SQL equality-domain checks are shared with ONLY_FULL_GROUP_BY without changing their behavior.

The BVT covers activation, nullable-UNIQUE and prepared-statement invalidation controls, including DROP UNIQUE, ADD UNIQUE, and table recreation. The metadata oracle is explicitly enabled with -- @metacmp(true).

Validation:

  • focused planner activation and fail-closed tests
  • go test ./pkg/sql/plan ./pkg/sql/plan/rule -count=1
  • go vet ./pkg/sql/plan ./pkg/sql/plan/rule
  • candidate service build
  • strict standalone BVT on the isolated candidate instance: 23/23 twice, with teardown verified between runs and after completion
  • final BVT oracle correction verified at candidate git_version=2e9ef1afabd; production sources are identical to PR head be53fe749e1
  • prepared execution and EXPLAIN checks confirm both rewrite activation and aggregate fallback after uniqueness is removed

Performance evidence on the same host with 1M rows and 21 fresh clients per query:

Query Base median Candidate median Speedup
LIMIT 10 34.452 ms 12.634 ms 2.73x
OFFSET 500000 33.792 ms 13.165 ms 2.57x
selective WHERE 18.852 ms 11.635 ms 1.62x
HAVING 38.101 ms 11.440 ms 3.33x

For LIMIT 10, the base plan scanned 1,000,000 rows and used 108.98 MiB aggregate memory; the candidate read one 8,192-row block, used 256 KiB scan memory, and built no aggregate.

Roadmap disposition:

@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

Copy link
Copy Markdown
Contributor Author

Fixed the BVT decimal oracle in be53fe7: SUM metadata (32,2), AVG (14,6), and six-fractional-digit AVG values. Added explicit SQL-level @metacmp(true). Verified on the isolated candidate at port 16001 (production-equivalent binary 2e9ef1afabd): strict normal comparison passed 23/23 twice, with teardown checked between runs and after completion. Base/candidate JDBC contract probes agree on the corrected metadata. This replaces the earlier BVT evidence obtained against the wrong local server. Final delta review: PASS, no P1/P2 blockers. Production code is unchanged; CI is not claimed green.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Local exact-head fallback review (Astra thread capacity unavailable), substantive result APPROVE. Reviewed head be53fe7 against base 3727c5c. The secondary-UNIQUE proof is fail-closed: ordinary persisted non-partitioned tables, built regular keys without prefixes, visible non-generated columns, complete NOT NULL metadata, and SQL-equality-compatible types are required; complete grouping coverage and prepared invalidation controls are covered. No concrete planner/runtime correctness defect found. The reported 23/23 twice with teardown and prepared DROP/ADD/recreate checks support the change; current CI status remains separate.

@XuPeng-SH
XuPeng-SH merged commit bbbfb70 into matrixorigin:main Sep 22, 2026
28 checks passed
@XuPeng-SH
XuPeng-SH deleted the codex/optimizer-roadmap-27856-27860 branch September 22, 2026 07:46

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/enhancement kind/test-ci size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants