Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/backend/gporca/libgpopt/src/xforms/CDecorrelator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,14 @@ CDecorrelator::FProcessGbAgg(CMemoryPool *mp, CExpression *pexpr,
return false;
}

// TODO: 12/20/2012 - ; check for strictness of agg function
if (0 == popAggOriginal->Pdrgpcr()->Size())
{
CColRef *pcrCount = nullptr;
if (CUtils::FHasCountAgg((*pexpr)[1], &pcrCount))
Copy link
Contributor

Choose a reason for hiding this comment

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

It's OK to process count star and count any here. But it's a general problem, namely, any AggFunc with not nulll initval we should process here.

{
return false;
}
}

// decorrelate relational child, allow only equality predicates, see below for the reason
CExpression *pexprRelational = nullptr;
Expand Down
35 changes: 12 additions & 23 deletions src/backend/gporca/libgpopt/src/xforms/CSubqueryHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,7 @@ CSubqueryHandler::FCreateOuterApplyForScalarSubquery(
*ppexprNewOuter = pexprPrj;

BOOL fGeneratedByQuantified = popSubquery->FGeneratedByQuantified();
if (fGeneratedByQuantified ||
(fHasCountAggMatchingColumn && 0 == pgbAgg->Pdrgpcr()->Size()))
if (fGeneratedByQuantified)
Copy link
Contributor

Choose a reason for hiding this comment

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

And please add a test case, thanks!

{
CMDAccessor *md_accessor = COptCtxt::PoctxtFromTLS()->Pmda();
const IMDTypeInt8 *pmdtypeint8 = md_accessor->PtMDType<IMDTypeInt8>();
Expand All @@ -765,27 +764,17 @@ CSubqueryHandler::FCreateOuterApplyForScalarSubquery(
CUtils::PexprScalarIdent(mp, pcrComputed),
CUtils::PexprScalarConstInt8(mp, 0 /*val*/));

if (fGeneratedByQuantified)
{
// we produce Null if count(*) value is -1,
// this case can only occur when transforming quantified subquery to
// count(*) subquery using CXformSimplifySubquery
pmdidInt8->AddRef();
*ppexprResidualScalar = GPOS_NEW(mp) CExpression(
mp, GPOS_NEW(mp) CScalarIf(mp, pmdidInt8),
CUtils::PexprScalarEqCmp(
mp, pcrComputed,
CUtils::PexprScalarConstInt8(mp, -1 /*value*/)),
CUtils::PexprScalarConstInt8(mp, 0 /*value*/, true /*is_null*/),
pexprCoalesce);
}
else
{
// count(*) value can either be NULL (if produced by a lower outer join), or some value >= 0,
// we return coalesce(count(*), 0) in this case

*ppexprResidualScalar = pexprCoalesce;
}
// we produce Null if count(*) value is -1,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why we change logic here?

// this case can only occur when transforming quantified subquery to
// count(*) subquery using CXformSimplifySubquery
pmdidInt8->AddRef();
*ppexprResidualScalar = GPOS_NEW(mp) CExpression(
mp, GPOS_NEW(mp) CScalarIf(mp, pmdidInt8),
CUtils::PexprScalarEqCmp(
mp, pcrComputed,
CUtils::PexprScalarConstInt8(mp, -1 /*value*/)),
CUtils::PexprScalarConstInt8(mp, 0 /*value*/, true /*is_null*/),
pexprCoalesce);

return fSuccess;
}
Expand Down
Loading