Conversation
This addresses the well-known "COUNT bug". When ORCA decorrelates a correlated subquery whose scalar GROUP BY () computes count(*) or count(attr), it adds the correlation columns as grouping columns. That transformation is not semantics-preserving on an empty input relation: a scalar aggregate returns exactly one row with count = 0, while the grouped version returns no row at all, so the subquery yields NULL instead of 0 and the query returns wrong results. Bail out of CDecorrelator::FProcessGbAgg when the aggregate has no grouping columns and contains a count() aggregate. This is a temporary fix: it gives less optimal plans in some cases (correlated execution or a correlated apply instead of a decorrelated join), but it never produces wrong answers. With decorrelation disabled, CSubqueryHandler no longer needs to patch up the scalar-subquery result with coalesce(count, 0) for a GROUP BY () count: that path only existed to compensate for the transformation above, and applying it could itself turn a legitimate NULL into 0. Drop the non-quantified branch of FCreateOuterApplyForScalarSubquery, which also subsumes the local FHasCorrelatedSelectAboveGbAgg exception for GROUP BY () HAVING <outer_ref>. Port of open-gpdb/gpdb#412 to the Apache Cloudberry 2.x line.
Alena0704
force-pushed
the
port-412-rel2
branch
from
September 22, 2026 20:45
a5d0871 to
482b341
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Disable decorrelation for count() with no grouping columns.
In SQL standard, GROUP BY clause can represent two distinct operations: normal GROUP BY with grouping columns, and scalar GROUP BY without grouping columns. Their main difference is that the scalar GROUP BY always outputs exactly one row, even when input relation is empty. This especially matters for COUNT(*) and COUNT(attr) aggregates since in empty input their output is 0, not NULL.
During subquery decorrelation in ORCA, when pulling predicates through GpAgg, new grouping columns are added to it. This is fine for normal GROUP BYs, but for scalar GROUP BY (the case when there were no grouping columns originally), it changes behavior on empty input relations, which produces invalid output. This seems to be a well-known bug in existing database literature, known as the "COUNT bug".
This behavior was noticed previously in ORCA, and a "COALESCE fix" was added, converting NULLs back to 0. However, this fix was added in a previous transformation (CSubqueryHandler), so it was unnecessary in some cases, and also didn't cover all of them. Fixing this properly will require a partial rewrite of CDecorrelator to use better decorrelation algorithms that don't miss these edge cases.
As a temporary solution, this patch disables decorrelation for GpAgg with no grouping columns, if COUNT(*) or COUNT(attr) is present, as well as the COALESCE fix. This unfortunately results in less optimal plans in some cases, but distinguishing correct decorrelation from incorrect ones is complicated and requires big rewrites.
Tests affected by this:
Ported from greengage #1658 and open-gpdb #412.
Co-Authored-By: Maxim Michkov m.michkov@arenadata.io
to reproduce the case