Skip to content

Estimate partitioned-root NDV by segments from value frequencies - #2427

Draft
Alena0704 wants to merge 1 commit into
apache:mainfrom
Alena0704:fix/ndv-by-segments-frequency
Draft

Alena0704 wants to merge 1 commit into
apache:mainfrom
Alena0704:fix/ndv-by-segments-frequency

Conversation

@Alena0704

Copy link
Copy Markdown
Collaborator

The average number of segments holding a value can differ between a leaf partition and the whole table. Applying the leaf ratio to the root NDV underestimates NDV_BY_SEGMENTS when a value occurs on different segments across partitions, which can make ORCA choose unhelpful partial aggregation.

Estimate root segment occupancy from MCV frequencies and a uniform frequency for the remaining non-null values. Use log1p/expm1 for numerical stability, retain colocation for a sole hash distribution key, and bound the result by the leaf sum, root NDV times the segment count, and non-null row count. Remove the now-unused leaf ndistinct sum.

The idea for the frequency-based segment occupancy estimator was proposed by Jianghua Yang yjhjstz@gmail.com.

Add incremental_analyze coverage for sparse values spanning segments across partitions with a multi-column distribution key. The existing low-cardinality and sole-distribution-key cases remain covered.

Fixes #2407

Type of Change

  • Bug fix (non-breaking change)

Breaking Changes

Test Plan

  • Integration tests added/updated

Impact

Performance:

  • For the ANALYZE underestimates ndistinct-by-segments of partitioned tables since 19b4ae07f8e; ORCA picks a useless two-stage aggregate #2407 reproduction, median execution time decreased from 7.33 s to 4.74 s across three runs (approximately 35% faster) by avoiding an unhelpful partial aggregation.
  • Full SF1 suites retained the same plan structures, with similar total execution times: TPC-H 19.99 → 20.10 s, TPC-DS 56.39 → 56.05 s. These were single-pass measurements, so small differences are not conclusive.
  • The additional calculation runs during root statistics merging in ANALYZE, with work proportional to the MCV list size. Performance effects depend on data distribution and the resulting plan.

Checklist

The average number of segments holding a value can differ between a leaf
partition and the whole table. Applying the leaf ratio to the root NDV
underestimates NDV_BY_SEGMENTS when a value occurs on different segments
across partitions, which can make ORCA choose unhelpful partial aggregation.

Estimate root segment occupancy from MCV frequencies and a uniform
frequency for the remaining non-null values. Use log1p/expm1 for numerical
stability, retain colocation for a sole hash distribution key, and bound
the result by the leaf sum, root NDV times the segment count, and non-null
row count. Remove the now-unused leaf ndistinct sum.

The idea for the frequency-based segment occupancy estimator was proposed
by Jianghua Yang <yjhjstz@gmail.com>.

Add incremental_analyze coverage for sparse values spanning segments
across partitions with a multi-column distribution key. The existing
low-cardinality and sole-distribution-key cases remain covered.

Fixes apache#2407
@Alena0704
Alena0704 requested a review from yjhjstz November 25, 2026 13:47
Comment on lines +5055 to +5060
if (numsegments == 1 ||
(GpPolicyIsHashPartitioned(policy) && policy->nattrs == 1 &&
policy->attrs[0] == stats->attr->attnum))
estimate = root_ndistinct;
else
{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only the sole hash distribution key is treated as colocated; every other column is assumed to be placed independently. Columns that are functionally dependent on the distribution key (denormalized dimension attributes, derived ids, a copy of the key, ...) are colocated too, and the leaf ratio used to capture that. The leaf-sum upper bound does not help once a value appears in >= numsegments partitions.

Repro (3 segments): create table h1 (a int, pk int, b int) distributed by (a) partition by range (pk) (start (0) end (6) every (1)); insert into h1 select v, p, v from generate_series(0,5) p, generate_series(1,100) v, generate_series(1,10) k; analyze h1; -> NDV_BY_SEGMENTS for b is 300 on this PR, actual count(distinct (gp_segment_id, b)) is 100, and main gives 100. In the worst case this overestimates by numsegments, which skews ORCA's local/partial aggregate costing.

Suggestion: keep the leaf information as a colocation correction, e.g. compute the independent-placement expectation E_leaf per leaf with the same formula, derive a factor clamp((observed_leaf - ndv_leaf) / (E_leaf - ndv_leaf), 0, 1), and use it to interpolate between root_ndistinct and the root's independent estimate. Please also add a regression case for a colocated non-distribution-key column. (verified: SQL above on PR vs base)

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.

ANALYZE underestimates ndistinct-by-segments of partitioned tables since 19b4ae07f8e; ORCA picks a useless two-stage aggregate

2 participants