Skip to content

perf: reduce redundant group-by hash keys - #26727

Merged
XuPeng-SH merged 1 commit into
matrixorigin:mainfrom
aptend:feature/groupby-minimal-hash
Aug 5, 2026
Merged

XuPeng-SH merged 1 commit into
matrixorigin:mainfrom
aptend:feature/groupby-minimal-hash

Conversation

@aptend

@aptend aptend commented Aug 5, 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 #13680

What this PR does / why we need it:

This PR keeps the logical GROUP BY expressions unchanged while allowing the executor to hash only an optimizer-proven physical equality key. When a complete base-table primary key is present, non-key columns from the same table no longer need to participate in hash-table equality.

The change:

  • derives a physical group-by hash key from complete primary-key metadata, with conservative fallbacks for grouping sets, derived expressions, fake keys, and unavailable composite-key components;
  • propagates the key through local Group, MergeGroup, spill/reload, operator duplication, and remote pipeline encoding;
  • keeps the protobuf change backward-compatible: an empty or missing field uses all logical group-by expressions;
  • updates aggregate cardinality estimation, shuffle selection, text/JSON explain output, and DISTINCT aggregate rewrites;
  • validates physical-key indices at execution boundaries and preserves the full logical group output.

Benchmark with 8,192 rows, 4,096 groups, one integer primary key, and four wide VARCHAR columns:

  • full logical key: 2.43-2.78 ms/op, 141-144 KiB/op;
  • integer primary key: 1.34-1.39 ms/op, 16.5-16.7 KiB/op.

Validation:

  • make pb
  • go build and go vet for pkg/sql/plan, pkg/sql/colexec/group, pkg/sql/compile, and pkg/sql/plan/explain
  • full tests for the same packages
  • race test for pkg/sql/colexec/group
  • planner, DISTINCT, shuffle, Group/MergeGroup, spill/reload, reset/reuse, explain, and remote-codec regression tests

@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.

Reviewed exact head 66df311. The functional-dependency optimization is conservative: it removes only direct same-table non-PK keys when a complete real PK is present, while retaining the logical GroupBy contract. Planner rewrites and local/merge/spill/shuffle/codec lifecycle paths were checked. Full controlled CGo group/plan/compile/explain tests and the full group race suite passed. APPROVE.

@XuPeng-SH
XuPeng-SH force-pushed the feature/groupby-minimal-hash branch from 66df311 to c2bf4ed Compare August 5, 2026 16:19
@XuPeng-SH
XuPeng-SH merged commit ab28d1b into matrixorigin:main Aug 5, 2026
26 checks passed
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#13680

## What this PR does / why we need it:

This PR keeps the logical `GROUP BY` expressions unchanged while
allowing the executor to hash only an optimizer-proven physical equality
key. When a complete base-table primary key is present, non-key columns
from the same table no longer need to participate in hash-table
equality.

The change:

- derives a physical group-by hash key from complete primary-key
metadata, with conservative fallbacks for grouping sets, derived
expressions, fake keys, and unavailable composite-key components;
- propagates the key through local Group, MergeGroup, spill/reload,
operator duplication, and remote pipeline encoding;
- keeps the protobuf change backward-compatible: an empty or missing
field uses all logical group-by expressions;
- updates aggregate cardinality estimation, shuffle selection, text/JSON
explain output, and DISTINCT aggregate rewrites;
- validates physical-key indices at execution boundaries and preserves
the full logical group output.

Benchmark with 8,192 rows, 4,096 groups, one integer primary key, and
four wide VARCHAR columns:

- full logical key: 2.43-2.78 ms/op, 141-144 KiB/op;
- integer primary key: 1.34-1.39 ms/op, 16.5-16.7 KiB/op.

Validation:

- `make pb`
- `go build` and `go vet` for `pkg/sql/plan`, `pkg/sql/colexec/group`,
`pkg/sql/compile`, and `pkg/sql/plan/explain`
- full tests for the same packages
- race test for `pkg/sql/colexec/group`
- planner, DISTINCT, shuffle, Group/MergeGroup, spill/reload,
reset/reuse, explain, and remote-codec regression tests
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#13680

## What this PR does / why we need it:

This PR keeps the logical `GROUP BY` expressions unchanged while
allowing the executor to hash only an optimizer-proven physical equality
key. When a complete base-table primary key is present, non-key columns
from the same table no longer need to participate in hash-table
equality.

The change:

- derives a physical group-by hash key from complete primary-key
metadata, with conservative fallbacks for grouping sets, derived
expressions, fake keys, and unavailable composite-key components;
- propagates the key through local Group, MergeGroup, spill/reload,
operator duplication, and remote pipeline encoding;
- keeps the protobuf change backward-compatible: an empty or missing
field uses all logical group-by expressions;
- updates aggregate cardinality estimation, shuffle selection, text/JSON
explain output, and DISTINCT aggregate rewrites;
- validates physical-key indices at execution boundaries and preserves
the full logical group output.

Benchmark with 8,192 rows, 4,096 groups, one integer primary key, and
four wide VARCHAR columns:

- full logical key: 2.43-2.78 ms/op, 141-144 KiB/op;
- integer primary key: 1.34-1.39 ms/op, 16.5-16.7 KiB/op.

Validation:

- `make pb`
- `go build` and `go vet` for `pkg/sql/plan`, `pkg/sql/colexec/group`,
`pkg/sql/compile`, and `pkg/sql/plan/explain`
- full tests for the same packages
- race test for `pkg/sql/colexec/group`
- planner, DISTINCT, shuffle, Group/MergeGroup, spill/reload,
reset/reuse, explain, and remote-codec regression tests
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [ ] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#13680

## What this PR does / why we need it:

This PR keeps the logical `GROUP BY` expressions unchanged while
allowing the executor to hash only an optimizer-proven physical equality
key. When a complete base-table primary key is present, non-key columns
from the same table no longer need to participate in hash-table
equality.

The change:

- derives a physical group-by hash key from complete primary-key
metadata, with conservative fallbacks for grouping sets, derived
expressions, fake keys, and unavailable composite-key components;
- propagates the key through local Group, MergeGroup, spill/reload,
operator duplication, and remote pipeline encoding;
- keeps the protobuf change backward-compatible: an empty or missing
field uses all logical group-by expressions;
- updates aggregate cardinality estimation, shuffle selection, text/JSON
explain output, and DISTINCT aggregate rewrites;
- validates physical-key indices at execution boundaries and preserves
the full logical group output.

Benchmark with 8,192 rows, 4,096 groups, one integer primary key, and
four wide VARCHAR columns:

- full logical key: 2.43-2.78 ms/op, 141-144 KiB/op;
- integer primary key: 1.34-1.39 ms/op, 16.5-16.7 KiB/op.

Validation:

- `make pb`
- `go build` and `go vet` for `pkg/sql/plan`, `pkg/sql/colexec/group`,
`pkg/sql/compile`, and `pkg/sql/plan/explain`
- full tests for the same packages
- race test for `pkg/sql/colexec/group`
- planner, DISTINCT, shuffle, Group/MergeGroup, spill/reload,
reset/reuse, explain, and remote-codec regression tests

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 size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants