Repository navigation
Description
Is there an existing issue for performance?
- I have checked the existing issues.
Environment
- MatrixOne baseline:
main, commit6f773c556d4f3177f5373eb193cd576e080b662f(2026-10-24). - Hardware/runtime: Apple M4, macOS arm64, 16 GiB host memory, 4 query workers.
- Controlled Q36-shaped input: 50 million rows, 24,997,260 distinct
ClientIPvalues,ClientIP INTEGER NOT NULL. This is synthetic data; it is not the official ClickBenchhitsdistribution. - MatrixOne used a 4 GiB statement budget and did not spill in these measurements. DuckDB used a 4 GiB engine limit, so cross-engine timings are indicative rather than a strict equal-budget comparison.
Details of Performance
ClickBench Q36 groups by an integer key and three expressions derived from that same key:
SELECT ClientIP, ClientIP - 1, ClientIP - 2, ClientIP - 3, COUNT(*) AS c
FROM hits
GROUP BY ClientIP, ClientIP - 1, ClientIP - 2, ClientIP - 3
ORDER BY c DESC
LIMIT 10;For this input, equal ClientIP values necessarily produce equal subtraction results. The three derived keys therefore add no grouping distinctions. MatrixOne currently sends all four values through the aggregate grouping path. Its balanced warm median was 1.953 s. The semantically equivalent control grouping only by ClientIP returned the same rows and took 0.962 s (about 2.0x faster). The two plans completed without spill.
On this same input, the local DuckDB 2.0.0 plan reduced the grouping key to ClientIP automatically and ran the original SQL in 0.361 s. This comparison motivates investigation; it does not establish an apples-to-apples resource-budget result or attribute the remaining gap to one kernel mechanism.
The desired optimization is general: prove when a grouping expression is determined by other grouping inputs, avoid carrying redundant keys through aggregation where safe, and reconstruct required output expressions above the aggregate. Reuse the existing planner/executor grouping-key ownership where possible; do not add a Q36-specific rule or duplicate SQL semantic checks.
The transformation must fail closed when equality domains do not match or expression evaluation cannot safely move across aggregation. Preserve visible columns, result types, NULL/collation behavior, warnings/errors and evaluation semantics. Keep grouping sets and other shapes on the existing path unless their semantics are explicitly proven.
Additional information
Related tracking: #27665 (ClickBench performance gap). This issue is specifically about redundant derived grouping expressions; #27753 tracks reuse of canonical hashes across Shuffle and aggregation, and #27856 tracks aggregate elimination from uniqueness proofs.
Acceptance:
- A planner-level proof removes redundant derived grouping work for Q36-shaped integer expressions without requiring a primary key.
- Negative controls retain all keys when dependency or SQL equality compatibility is not proven; expressions with observable evaluation effects do not move across the aggregate.
- Public SQL results, output types and errors match the unoptimized query for NULLs, integer boundaries, duplicate/skewed keys and unsupported type/equality domains.
- Multi-worker and forced-spill executions preserve results, memory accounting and cleanup; no separate spill implementation or relaxed memory admission is introduced.
- Benchmarks compare original and optimized plans on the same binary/data, include dependent and non-dependent keys plus low/high cardinality, and report latency, aggregate memory/output bytes and spill bytes. The Q36-shaped case should approach the single-key control without material regressions on the negative controls.
Activity
perf(sql): prune total derived grouping keys (#31040)