Repository navigation
Conversation
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
| if (numsegments == 1 || | ||
| (GpPolicyIsHashPartitioned(policy) && policy->nattrs == 1 && | ||
| policy->attrs[0] == stats->attr->attnum)) | ||
| estimate = root_ndistinct; | ||
| else | ||
| { |
There was a problem hiding this comment.
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)
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
Breaking Changes
Test Plan
Impact
Performance:
Checklist