my-ship-it commented on code in PR #1126:
URL: https://github.com/apache/cloudberry/pull/1126#discussion_r2113573909


##########
src/backend/gporca/libgpopt/src/operators/CDedupSupersetPreprocessor.cpp:
##########
@@ -0,0 +1,551 @@
+/*-------------------------------------------------------------------------
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ * CDedupSupersetPreprocessor.cpp
+ *
+ * IDENTIFICATION
+ *       
src/backend/gporca/libgpopt/src/operators/CDedupSupersetPreprocessor.cpp
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "gpopt/operators/CDedupSupersetPreprocessor.h"
+
+#include <limits>
+
+#include "gpopt/operators/CExpression.h"
+#include "gpopt/operators/CLogicalCTEAnchor.h"
+#include "gpopt/operators/CLogicalCTEConsumer.h"
+#include "gpopt/operators/CLogicalCTEProducer.h"
+#include "gpopt/operators/CLogicalDynamicGet.h"
+#include "gpopt/operators/CLogicalGet.h"
+#include "gpopt/operators/CLogicalInnerJoin.h"
+#include "gpopt/operators/CLogicalNAryJoin.h"
+#include "gpopt/operators/CScalarCmp.h"
+#include "gpopt/operators/CScalarIdent.h"
+#include "gpopt/operators/CScalarSubqueryAny.h"
+#include "gpopt/optimizer/COptimizerConfig.h"
+
+using namespace gpopt;
+
+#define CTEID_TO_OID_START 60000
+
+TableDescIdent::TableDescIdent(IMDId *mdid, ULONG sid, ULONG output_colid)
+       : pmdid(mdid), subid(sid), ocolid(output_colid)
+{
+       GPOS_ASSERT(nullptr != mdid);
+}
+
+TableDescIdent::~TableDescIdent()
+{
+       pmdid->Release();
+}
+
+ULONG
+TableDescIdent::HashFunc(const TableDescIdent *ptdi)
+{
+       GPOS_ASSERT(nullptr != ptdi);
+
+       return gpos::CombineHashes(
+               ptdi->pmdid->HashValue(),
+               gpos::CombineHashes(gpos::HashValue(&ptdi->subid),
+                                                       
gpos::HashValue(&ptdi->ocolid)));
+}
+
+// equality function
+BOOL
+TableDescIdent::EqualFunc(const TableDescIdent *pltdi,
+                                                 const TableDescIdent *prtdi)
+{
+       GPOS_ASSERT(nullptr != pltdi && nullptr != prtdi);
+       return pltdi->pmdid->Equals(prtdi->pmdid) && pltdi->subid == 
prtdi->subid &&
+                  pltdi->ocolid == prtdi->ocolid;
+}
+
+
+// expr is CLogicalGet,CLogicalDynamicGet or CLogicalCTEConsumer ?
+BOOL
+CDedupSupersetPreprocessor::PexprIsLogicalOutput(CExpression *pexpr)
+{
+       GPOS_ASSERT(pexpr);
+       COperator *pop = pexpr->Pop();
+       return pop->Eopid() == COperator::EopLogicalGet ||
+                  pop->Eopid() == COperator::EopLogicalDynamicGet ||
+                  pop->Eopid() == COperator::EopLogicalCTEConsumer;
+}
+
+// Get the meta id from expression
+// The expression must be CLogicalGet/CLogicalDynamicGet/CLogicalCTEConsumer
+// for the CLogicalGet/CLogicalDynamicGet, we can direct get the meta id by 
table desc.
+// But for the CLogicalCTEConsumer, we will create a meta id from CTEid
+IMDId *
+CDedupSupersetPreprocessor::PexprGetIMDid(CMemoryPool *mp, CExpression *pexpr)
+{
+       GPOS_ASSERT(pexpr);
+       COperator *poper = pexpr->Pop();
+       IMDId *pmdid = nullptr;
+       switch (poper->Eopid())
+       {
+               case COperator::EopLogicalGet:
+               {
+                       CLogicalGet *pop_get = CLogicalGet::PopConvert(poper);
+                       if (pop_get->Ptabdesc())
+                       {
+                               pmdid = pop_get->Ptabdesc()->MDId()->Copy(mp);
+                       }
+                       break;
+               }
+               case COperator::EopLogicalDynamicGet:
+               {
+                       CLogicalDynamicGet *pop_dynamicget =
+                               CLogicalDynamicGet::PopConvert(poper);
+                       if (pop_dynamicget->Ptabdesc())
+                       {
+                               pmdid = 
pop_dynamicget->Ptabdesc()->MDId()->Copy(mp);
+                       }
+                       break;
+               }
+               case COperator::EopLogicalCTEConsumer:
+               {
+                       CLogicalCTEConsumer *pop_cc =
+                               CLogicalCTEConsumer::PopConvert(poper);
+                       pmdid = GPOS_NEW(mp) CMDIdGPDB(
+                               IMDId::EmdidGeneral, pop_cc->UlCTEId() + 
CTEID_TO_OID_START);
+                       break;
+               }
+               default:
+                       break;
+       }
+
+       return pmdid;
+}
+
+// full the maps with superset expression
+void
+CDedupSupersetPreprocessor::ChildExprFullSuperset(CMemoryPool *mp,
+                                                                               
                  CExpression *pexpr,
+                                                                               
                  TDIToUlongPtrMap *eqcrmaps,
+                                                                               
                  BOOL *dedupulmasks, ULONG ul)
+{
+       CScalarSubqueryAny *pop_subany =
+               CScalarSubqueryAny::PopConvert(pexpr->Pop());
+       CExpression *pexpr_child0 = (*pexpr)[0];
+       IMDId *tdimid = PexprGetIMDid(mp, pexpr_child0);
+       CScalarIdent *pop_ident = CScalarIdent::PopConvert((*pexpr)[1]->Pop());
+       ULONG ulidx_subany;
+
+       if (nullptr == tdimid)
+       {
+               // skip
+               return;
+       }
+       CColRefSet *colrefsets = pexpr_child0->DeriveOutputColumns();
+
+       // The colref(in CScalarSubqueryAny) must in the output
+       (void) colrefsets->ExtractIndex(pop_subany->Pcr(), &ulidx_subany);
+       GPOS_ASSERT(ulidx_subany != gpos::ulong_max);
+
+       TableDescIdent tdikey =
+               TableDescIdent(tdimid, ulidx_subany, pop_ident->Pcr()->Id());
+       ULONG *pul = eqcrmaps->Find(&tdikey);
+       if (!pul)
+       {
+               // new find superset, register it
+               eqcrmaps->Insert(GPOS_NEW(mp) TableDescIdent(tdimid, 
ulidx_subany,
+                                                                               
                         pop_ident->Pcr()->Id()),
+                                                GPOS_NEW(mp) ULONG(ul));
+       }
+       else
+       {
+               // already register the same superset expr, direct remove it
+               //
+               // ex.
+               //   select * from t1,t2 where
+               //   t1.v1 in (select v3 from t2)  <- colref(v3)
+               //   and
+               //   t1.v1 in (select v3 from t2); <- same colref(v3) but id is 
different
+               dedupulmasks[ul] = false;
+       }
+}
+
+// full the dedups array with subset(simply logicalget)
+void
+CDedupSupersetPreprocessor::ChildExprFullSimplySubset(
+       CMemoryPool *mp, CExpression *pexpr, TDIToUlongPtrMap *eqcrmaps,
+       BOOL *dedupulmasks)

Review Comment:
   The function is similar to ChildExprFullSuperset above. Have we already 
finished dedup in ChildExprFullSuperset?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to