Repository navigation
Conversation
2ddc24d to
3518a1c
Compare
|
Thanks for working on this. I built the branch locally ( The correctness goal holds up well: the empty-input values for correlated Two issues I think should be addressed before merge. 1.
|
Thank you! Agree with this. I need some time to figure out how to address these issues |
e9721d4 to
dede2af
Compare
dede2af to
c47d048
Compare
Hi, I've reworked the implementation since your review of The approach builds on ORCA's existing scalar-subquery handling, which already restores COUNT's zero result on empty input. The original COUNT/IN bug exposed a gap in applying the same semantics to IN/ANY. I extended the scalar-subquery handling and reused it for supported single-row aggregate IN/ANY queries. I removed the additional metadata describing aggregate behavior on empty input. COUNT and built-in MIN/MAX use known empty-input values; other aggregates are evaluated on empty input only when no matching group exists. These results are preserved before evaluating expressions, HAVING, and surrounding predicates. Nested subqueries in projections or HAVING retain correlated execution, avoiding the shared-column-reference assertion reported in the review. IN/ANY forms that can return no rows retain quantified semantics. The PR now contains three commits:
While testing nested aggregate subqueries, I also found a separate cardinality-estimation bug: fractional row estimates could produce join scale factors below one, making selectivity exceed one or triggering an assertion. The first commit fixes the affected scale factors and their bound. Examples, results, and query plans before and after the fix are included in the PR description. |
c23a5e7 to
5abe617
Compare
|
Since my previous update, I narrowed the fix to avoid changing queries that do not need empty-input handling. Orca unlike Postgres optimizer can evaluate a volatile expression for rows that do not match the correlated subquery, causing an unexpected error. Bug reproduction: Only (1, 1) should match, so the query should return 1. After the fix, ORCA keeps a correlated SubPlan, checks i.k = o.k first, and returns 1. |
26406a6 to
940f089
Compare
Keep fallback join scale factors and their NDV-based cap at least one. This prevents fractional correlated-row estimates from producing selectivity above one or violating scale-factor assertions.
Reject decorrelation when an inner expression has volatile function properties. Keep this restriction separate from scalar aggregate empty-input repair and aggregate final-function metadata. Update the main and PAX regression expectations for volatile subqueries, including random(), row-security predicates, and table-valued functions. Normalize the PL/Python SPI exception prefix on the expected distributed SQL-access error in the CTE tests.
Restore missing scalar aggregate rows before evaluating projections, HAVING and surrounding predicates. Preserve COUNT's zero and evaluate other empty-input values on a guarded empty input when necessary, including user-defined aggregate results and required errors. Restrict the repair to correlated aggregate inputs. Retain existing paths for bare built-in SUM/AVG/MIN/MAX and aggregates synthesized from quantified subqueries. Preserve the protection for COUNT with correlated HAVING, keep unsupported nested expressions correlated, and identify built-ins by catalog identity rather than names. Keep outer rows for null-accepting scalar predicates and supported inputs eligible for join plans. Expose aggregate FINALFUNC volatility in metadata and function properties so volatile-subquery safeguards also apply to aggregate final functions. Preserve the flag in DXL while accepting existing metadata without it; leave nonvolatile final functions unchanged. Add scalar aggregate, nested HAVING, same-named user aggregate and volatile final-function regression coverage, including ANY and ALL subqueries, a metadata round-trip fixture, and update the affected subselect plans. Fixes the GPORCA wrong-result examples in apache#2046.
Route correlated IN/ANY over a supported single-row aggregate through scalar-subquery handling. Restore the aggregate's empty-input value before comparing it, including zero for COUNT and user-defined aggregate results. Keep the quantified form when HAVING, grouping or LIMIT can remove the row. Prevent the quantified-to-aggregate rewrite from hiding this distinction, and retain NULL comparisons in correlated ANY subplans. Add IN/ANY regression coverage for empty and all-NULL inputs, projections, HAVING, grouping, LIMIT/OFFSET, nested subqueries and decorrelated plans.
940f089 to
f2f883c
Compare
What does this PR do?
Fix incorrect results when ORCA decorrelates aggregate subqueries or removes outer rows before a scalar-subquery filter can evaluate.
The following examples use the same data:
1. COUNT inside IN
Before the fix: no rows. After the fix:
(0, 1).The inner input is empty, but
count(*)returns zero. The condition is therefore0 IN (0), which is true.Plans before and after
Before:
After:
The left join preserves the outer row, and
COALESCErestores zero before the comparison.2. Empty-input values of other aggregates
Before the fix:
(0, NULL). After the fix:(0, 0).Like
count,regr_countreturns zero on empty input. The fix also preserves the actual empty-input results of user-defined aggregates.Plans before and after
Before:
After:
For this dataset, ORCA chooses a correlated aggregate SubPlan that preserves the empty-input result. Supported queries remain eligible for join plans.
3. Filters that accept NULL
Before the fix: no rows. After the fix:
(0, 1).The scalar subquery returns no rows, so its value is NULL and
IS NULLis true. The outer row must survive until the filter is evaluated.Plans before and after
Before:
After:
The fixed plan evaluates
IS NULLon the scalar subquery result, preserving the outer row when the subquery is empty.The implementation preserves aggregate values before evaluating expressions and
HAVING, and reuses scalar-subquery handling for supported aggregateIN/ANYqueries.When
HAVING, grouping, orLIMITcan remove the aggregate row, quantified semantics are retained: an empty scalar subquery yields NULL, whileINover an empty result is false.Fixes the GPORCA cases reported in #2046.
There was found an additional issue during development. Testing nested aggregate subqueries exposed a separate statistics issue: fractional row estimates could produce join selectivity above one or trigger an assertion. A separate commit fixes this by keeping the affected join scale factors and their bound at least one.
Type of Change
Test Plan
make installcheck.make -C src/test installcheck-cbdb-parallel.Additional Context
The COUNT/IN regression scenarios were adapted from Greengage commit 2ef1914. This PR uses a different implementation and preserves decorrelation for supported cases.
Checklist