Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ORCA: Fix eliminate self comparison #722

Merged
merged 2 commits into from
Dec 5, 2024
Merged
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
395 changes: 395 additions & 0 deletions src/backend/gporca/data/dxl/minidump/LOJ-With-Single-Pred-On-Outer.mdp

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ class CExpressionPreprocessor

// eliminate self comparisons
static CExpression *PexprEliminateSelfComparison(CMemoryPool *mp,
CExpression *pexpr);
CExpression *pexpr,
CColRefSet *pcrsNotNull);

// remove CTE Anchor nodes
static CExpression *PexprRemoveCTEAnchors(CMemoryPool *mp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,13 @@ class CPredicateUtils
static BOOL FPlainEquality(CExpression *pexpr);

// is the given expression a self comparison on some column
static BOOL FSelfComparison(CExpression *pexpr, IMDType::ECmpType *pecmpt);
static BOOL FSelfComparison(CExpression *pexpr, IMDType::ECmpType *pecmpt,
CColRefSet *pcrsNotNull);

// eliminate self comparison if possible
static CExpression *PexprEliminateSelfComparison(CMemoryPool *mp,
CExpression *pexpr);
CExpression *pexpr,
CColRefSet *pcrsNotNull);

// is the given expression in the form (col1 Is NOT DISTINCT FROM col2)
static BOOL FINDFScalarIdents(CExpression *pexpr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,24 @@ using namespace gpopt;
// eliminate self comparisons in the given expression
CExpression *
CExpressionPreprocessor::PexprEliminateSelfComparison(CMemoryPool *mp,
CExpression *pexpr)
CExpression *pexpr,
CColRefSet *pcrsNotNull)
{
// protect against stack overflow during recursion
GPOS_CHECK_STACK_SIZE;
GPOS_ASSERT(nullptr != mp);
GPOS_ASSERT(nullptr != pexpr);
COperator *pop = pexpr->Pop();

if (CUtils::FScalarCmp(pexpr))
{
return CPredicateUtils::PexprEliminateSelfComparison(mp, pexpr);
return CPredicateUtils::PexprEliminateSelfComparison(mp, pexpr,
pcrsNotNull);
}
// Use current expr rather then the root to get not null columns
else if (pop->FLogical())
{
pcrsNotNull = pexpr->DeriveNotNullColumns();
}

// recursively process children
Expand All @@ -88,11 +96,10 @@ CExpressionPreprocessor::PexprEliminateSelfComparison(CMemoryPool *mp,
for (ULONG ul = 0; ul < arity; ul++)
{
CExpression *pexprChild =
PexprEliminateSelfComparison(mp, (*pexpr)[ul]);
PexprEliminateSelfComparison(mp, (*pexpr)[ul], pcrsNotNull);
pdrgpexprChildren->Append(pexprChild);
}

COperator *pop = pexpr->Pop();
pop->AddRef();

return GPOS_NEW(mp) CExpression(mp, pop, pdrgpexprChildren);
Expand Down Expand Up @@ -3077,8 +3084,8 @@ CExpressionPreprocessor::PexprPreprocess(
pexprConvert2In->Release();

// (11) eliminate self comparisons
CExpression *pexprSelfCompEliminated =
PexprEliminateSelfComparison(mp, pexprInferredPreds);
CExpression *pexprSelfCompEliminated = PexprEliminateSelfComparison(
mp, pexprInferredPreds, pexprInferredPreds->DeriveNotNullColumns());
GPOS_CHECK_ABORT;
pexprInferredPreds->Release();

Expand Down
12 changes: 7 additions & 5 deletions src/backend/gporca/libgpopt/src/operators/CPredicateUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,8 @@ CPredicateUtils::FPlainEquality(CExpression *pexpr)

// is an expression a self comparison on some column
BOOL
CPredicateUtils::FSelfComparison(CExpression *pexpr, IMDType::ECmpType *pecmpt)
CPredicateUtils::FSelfComparison(CExpression *pexpr, IMDType::ECmpType *pecmpt,
CColRefSet *pcrsNotNull)
{
GPOS_ASSERT(nullptr != pexpr);
GPOS_ASSERT(nullptr != pecmpt);
Expand All @@ -882,8 +883,8 @@ CPredicateUtils::FSelfComparison(CExpression *pexpr, IMDType::ECmpType *pecmpt)
CColRef *colref =
const_cast<CColRef *>(CScalarIdent::PopConvert(popLeft)->Pcr());

return CColRef::EcrtTable == colref->Ecrt() &&
!CColRefTable::PcrConvert(colref)->IsNullable();
// return true if column is a member of NotNull columns(pcrsNotNull) of parent expression
return pcrsNotNull->FMember(colref);
}

return false;
Expand All @@ -892,14 +893,15 @@ CPredicateUtils::FSelfComparison(CExpression *pexpr, IMDType::ECmpType *pecmpt)
// eliminate self comparison and replace it with True or False if possible
CExpression *
CPredicateUtils::PexprEliminateSelfComparison(CMemoryPool *mp,
CExpression *pexpr)
CExpression *pexpr,
CColRefSet *pcrsNotNull)
{
GPOS_ASSERT(pexpr->Pop()->FScalar());

pexpr->AddRef();
CExpression *pexprNew = pexpr;
IMDType::ECmpType cmp_type = IMDType::EcmptOther;
if (FSelfComparison(pexpr, &cmp_type))
if (FSelfComparison(pexpr, &cmp_type, pcrsNotNull))
{
switch (cmp_type)
{
Expand Down
6 changes: 0 additions & 6 deletions src/backend/gporca/libgpopt/src/xforms/CXformUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,6 @@ CXformUtils::ExfpExpandJoinOrder(CExpressionHandle &exprhdl,
return CXform::ExfpNone;
}

//#ifdef GPOS_DEBUG
CAutoMemoryPool amp;
GPOS_ASSERT_FIXME(!FJoinPredOnSingleChild(amp.Pmp(), exprhdl) &&
"join predicates are not pushed down");
//#endif // GPOS_DEBUG

if (nullptr != exprhdl.Pgexpr())
{
// if handle is attached to a group expression, transformation is applied
Expand Down
3 changes: 2 additions & 1 deletion src/backend/gporca/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ DPv2GreedyOnly DPv2MinCardOnly DPv2QueryOnly LOJ-PushDown LeftJoinDPv2JoinOrder;
COuterJoin2Test:
LOJ-IsNullPred Select-Proj-OuterJoin OuterJoin-With-OuterRefs Join-Disj-Subqs
EffectOfLocalPredOnJoin EffectOfLocalPredOnJoin2 EffectOfLocalPredOnJoin3
LeftJoin-UnsupportedFilter-Cardinality LeftOuter2InnerUnionAllAntiSemiJoin;
LeftJoin-UnsupportedFilter-Cardinality LeftOuter2InnerUnionAllAntiSemiJoin
LOJ-With-Single-Pred-On-Outer LOJ_NULLTEST-On-SelfCheck-Pred;

COuterJoin3Test:
LOJ_IDF_no_convert_outer_ref_predicate_with_NULL
Expand Down
Loading
Loading