@@ -768,15 +768,26 @@ ExprCP ToGraph::translateLambda(const lp::LambdaExpr* lambda) {
768768}
769769
770770namespace {
771+
772+ constexpr uint64_t kAllAllowedInDt = ~uint64_t {0 };
773+
771774// Returns a mask that allows 'op' in the same derived table.
772- uint64_t allow (PlanType op) {
773- return 1UL << static_cast <uint32_t >(op);
775+ uint64_t allow (lp::NodeKind op) {
776+ return uint64_t { 1 } << static_cast <uint64_t >(op);
774777}
775778
776779// True if 'op' is in 'mask.
777- bool contains (uint64_t mask, PlanType op) {
778- return 0 != (mask & (1UL << static_cast <uint32_t >(op)));
780+ bool contains (uint64_t mask, lp::NodeKind op) {
781+ return mask & allow (op);
782+ }
783+
784+ // Removes 'op' from the set of operators allowed in the current derived
785+ // table. makeQueryGraph() starts a new derived table if it finds an operator
786+ // that does not belong to the mask.
787+ uint64_t makeDtIf (uint64_t mask, lp::NodeKind op) {
788+ return mask & ~allow (op);
779789}
790+
780791} // namespace
781792
782793std::optional<ExprCP> ToGraph::translateSubfieldFunction (
@@ -1126,14 +1137,12 @@ AggregationPlanCP ToGraph::translateAggregation(const lp::AggregateNode& agg) {
11261137 std::move (intermediateColumns));
11271138}
11281139
1129- PlanObjectP ToGraph::addOrderBy (const lp::SortNode& order) {
1140+ void ToGraph::addOrderBy (const lp::SortNode& order) {
11301141 auto [deduppedOrderKeys, deduppedOrderTypes] =
11311142 dedupOrdering (order.ordering ());
11321143
11331144 currentDt_->orderKeys = std::move (deduppedOrderKeys);
11341145 currentDt_->orderTypes = std::move (deduppedOrderTypes);
1135-
1136- return currentDt_;
11371146}
11381147
11391148namespace {
@@ -1194,7 +1203,7 @@ void ToGraph::translateJoin(const lp::JoinNode& join) {
11941203
11951204 // TODO Allow mixing Unnest with Join in a single DT.
11961205 // https://github.com/facebookexperimental/verax/issues/286
1197- const auto allowedInDt = allow (PlanType:: kJoinNode );
1206+ const auto allowedInDt = allow (lp::NodeKind:: kJoin );
11981207 makeQueryGraph (*joinLeft, allowedInDt);
11991208
12001209 // For an inner join a join tree on the right can be flattened, for all other
@@ -1261,15 +1270,13 @@ DerivedTableP ToGraph::newDt() {
12611270 return dt;
12621271}
12631272
1264- PlanObjectP ToGraph::wrapInDt (const lp::LogicalPlanNode& node) {
1273+ void ToGraph::wrapInDt (const lp::LogicalPlanNode& node) {
12651274 DerivedTableP previousDt = currentDt_;
12661275
12671276 currentDt_ = newDt ();
12681277 makeQueryGraph (node, kAllAllowedInDt );
12691278
12701279 finalizeDt (node, previousDt);
1271-
1272- return currentDt_;
12731280}
12741281
12751282void ToGraph::finalizeDt (
@@ -1284,7 +1291,7 @@ void ToGraph::finalizeDt(
12841291 dt->makeInitialPlan ();
12851292}
12861293
1287- PlanObjectP ToGraph::makeBaseTable (const lp::TableScanNode& tableScan) {
1294+ void ToGraph::makeBaseTable (const lp::TableScanNode& tableScan) {
12881295 const auto * schemaTable =
12891296 schema_.findTable (tableScan.connectorId (), tableScan.tableName ());
12901297 VELOX_CHECK_NOT_NULL (
@@ -1359,11 +1366,9 @@ PlanObjectP ToGraph::makeBaseTable(const lp::TableScanNode& tableScan) {
13591366
13601367 optimization->setLeafSelectivity (*baseTable, scanType);
13611368 currentDt_->addTable (baseTable);
1362-
1363- return baseTable;
13641369}
13651370
1366- PlanObjectP ToGraph::makeValuesTable (const lp::ValuesNode& values) {
1371+ void ToGraph::makeValuesTable (const lp::ValuesNode& values) {
13671372 auto * valuesTable = make<ValuesTable>(values);
13681373 valuesTable->cname = newCName (" vt" );
13691374 planLeaves_[&values] = valuesTable;
@@ -1385,8 +1390,6 @@ PlanObjectP ToGraph::makeValuesTable(const lp::ValuesNode& values) {
13851390 }
13861391
13871392 currentDt_->addTable (valuesTable);
1388-
1389- return valuesTable;
13901393}
13911394
13921395namespace {
@@ -1435,15 +1438,15 @@ void ToGraph::makeSubfieldColumns(
14351438 allColumnSubfields_[column] = std::move (projections);
14361439}
14371440
1438- PlanObjectP ToGraph::addProjection (const lp::ProjectNode* project) {
1439- exprSource_ = project-> onlyInput ().get ();
1440- const auto & names = project-> names ();
1441- const auto & exprs = project-> expressions ();
1442- auto channels = usedChannels (* project);
1441+ void ToGraph::addProjection (const lp::ProjectNode& project) {
1442+ exprSource_ = project. onlyInput ().get ();
1443+ const auto & names = project. names ();
1444+ const auto & exprs = project. expressions ();
1445+ auto channels = usedChannels (project);
14431446 trace (OptimizerOptions::kPreprocess , [&]() {
14441447 for (auto i = 0 ; i < exprs.size (); ++i) {
14451448 if (std::ranges::find (channels, i) == channels.end ()) {
1446- std::cout << " P=" << project-> id ()
1449+ std::cout << " P=" << project. id ()
14471450 << " dropped projection name=" << names[i] << " = "
14481451 << lp::ExprPrinter::toText (*exprs[i]) << std::endl;
14491452 }
@@ -1464,15 +1467,13 @@ PlanObjectP ToGraph::addProjection(const lp::ProjectNode* project) {
14641467 auto expr = translateExpr (exprs.at (i));
14651468 renames_[names[i]] = expr;
14661469 }
1467-
1468- return currentDt_;
14691470}
14701471
1471- PlanObjectP ToGraph::addFilter (const lp::FilterNode* filter) {
1472- exprSource_ = filter-> onlyInput ().get ();
1472+ void ToGraph::addFilter (const lp::FilterNode& filter) {
1473+ exprSource_ = filter. onlyInput ().get ();
14731474
14741475 ExprVector flat;
1475- translateConjuncts (filter-> predicate (), flat);
1476+ translateConjuncts (filter. predicate (), flat);
14761477
14771478 if (currentDt_->hasAggregation ()) {
14781479 currentDt_->having .insert (
@@ -1481,34 +1482,25 @@ PlanObjectP ToGraph::addFilter(const lp::FilterNode* filter) {
14811482 currentDt_->conjuncts .insert (
14821483 currentDt_->conjuncts .end (), flat.begin (), flat.end ());
14831484 }
1484-
1485- return currentDt_;
1486- }
1487-
1488- PlanObjectP ToGraph::addAggregation (const lp::AggregateNode& aggNode) {
1489- currentDt_->aggregation = translateAggregation (aggNode);
1490- return currentDt_;
14911485}
14921486
1493- PlanObjectP ToGraph::addLimit (const lp::LimitNode& limitNode ) {
1487+ void ToGraph::addLimit (const lp::LimitNode& limit ) {
14941488 if (currentDt_->hasLimit ()) {
1495- currentDt_->offset += limitNode .offset ();
1489+ currentDt_->offset += limit .offset ();
14961490
1497- if (currentDt_->limit <= limitNode .offset ()) {
1491+ if (currentDt_->limit <= limit .offset ()) {
14981492 currentDt_->limit = 0 ;
14991493 } else {
15001494 currentDt_->limit =
1501- std::min (limitNode .count (), currentDt_->limit - limitNode .offset ());
1495+ std::min (limit .count (), currentDt_->limit - limit .offset ());
15021496 }
15031497 } else {
1504- currentDt_->limit = limitNode .count ();
1505- currentDt_->offset = limitNode .offset ();
1498+ currentDt_->limit = limit .count ();
1499+ currentDt_->offset = limit .offset ();
15061500 }
1507-
1508- return currentDt_;
15091501}
15101502
1511- PlanObjectP ToGraph::addWrite (const lp::TableWriteNode& tableWrite) {
1503+ void ToGraph::addWrite (const lp::TableWriteNode& tableWrite) {
15121504 const auto writeKind =
15131505 static_cast <connector::WriteKind>(tableWrite.writeKind ());
15141506 if (writeKind != connector::WriteKind::kInsert &&
@@ -1566,8 +1558,6 @@ PlanObjectP ToGraph::addWrite(const lp::TableWriteNode& tableWrite) {
15661558
15671559 currentDt_->write =
15681560 make<WritePlan>(*connectorTable, writeKind, std::move (columnExprs));
1569-
1570- return currentDt_;
15711561}
15721562
15731563namespace {
@@ -1585,9 +1575,7 @@ bool hasNondeterministic(const lp::ExprPtr& expr) {
15851575
15861576} // namespace
15871577
1588- DerivedTableP ToGraph::translateSetJoin (
1589- const lp::SetNode& set,
1590- DerivedTableP setDt) {
1578+ void ToGraph::translateSetJoin (const lp::SetNode& set, DerivedTableP setDt) {
15911579 auto previousDt = currentDt_;
15921580 currentDt_ = setDt;
15931581 for (auto & input : set.inputs ()) {
@@ -1632,7 +1620,6 @@ DerivedTableP ToGraph::translateSetJoin(
16321620 setDt->columns = columns;
16331621 setDt->makeInitialPlan ();
16341622 currentDt_ = previousDt;
1635- return setDt;
16361623}
16371624
16381625void ToGraph::makeUnionDistributionAndStats (
@@ -1758,34 +1745,26 @@ DerivedTableP ToGraph::makeQueryGraph(const lp::LogicalPlanNode& logicalPlan) {
17581745 return currentDt_;
17591746}
17601747
1761- namespace {
1762- // Removes 'op' from the set of operators allowed in the current derived
1763- // table. makeQueryGraph() starts a new derived table if it finds an operator
1764- // that does not belong to the mask.
1765- uint64_t makeDtIf (uint64_t mask, PlanType op) {
1766- return mask & ~(1UL << static_cast <uint32_t >(op));
1767- }
1768- } // namespace
1769-
1770- PlanObjectP ToGraph::makeQueryGraph (
1748+ void ToGraph::makeQueryGraph (
17711749 const lp::LogicalPlanNode& node,
17721750 uint64_t allowedInDt) {
17731751 ToGraphContext ctx{&node};
17741752 velox::ExceptionContextSetter exceptionContext{makeExceptionContext (&ctx)};
17751753 switch (node.kind ()) {
1776- case lp::NodeKind::kValues :
1777- return makeValuesTable (*node.asUnchecked <lp::ValuesNode>());
1778-
1779- case lp::NodeKind::kTableScan :
1780- return makeBaseTable (*node.asUnchecked <lp::TableScanNode>());
1781-
1754+ case lp::NodeKind::kValues : {
1755+ makeValuesTable (*node.asUnchecked <lp::ValuesNode>());
1756+ return ;
1757+ }
1758+ case lp::NodeKind::kTableScan : {
1759+ makeBaseTable (*node.asUnchecked <lp::TableScanNode>());
1760+ return ;
1761+ }
17821762 case lp::NodeKind::kFilter : {
17831763 // Multiple filters are allowed before a limit. If DT has a groupBy, then
17841764 // filter is added to 'having', otherwise, to 'conjuncts'.
1785- const auto * filter = node.asUnchecked <lp::FilterNode>();
1765+ const auto & filter = * node.asUnchecked <lp::FilterNode>();
17861766
1787- if (!isNondeterministicWrap_ &&
1788- hasNondeterministic (filter->predicate ())) {
1767+ if (!isNondeterministicWrap_ && hasNondeterministic (filter.predicate ())) {
17891768 // Force wrap the filter and its input inside a dt so the filter
17901769 // does not get mixed with parent nodes.
17911770 makeQueryGraph (*node.onlyInput (), allowedInDt);
@@ -1798,7 +1777,7 @@ PlanObjectP ToGraph::makeQueryGraph(
17981777 finalizeDt (node);
17991778
18001779 isNondeterministicWrap_ = true ;
1801- return currentDt_ ;
1780+ return ;
18021781 }
18031782
18041783 isNondeterministicWrap_ = false ;
@@ -1807,17 +1786,19 @@ PlanObjectP ToGraph::makeQueryGraph(
18071786 if (currentDt_->hasLimit ()) {
18081787 finalizeDt (*node.onlyInput ());
18091788 }
1810- return addFilter (filter);
1789+ addFilter (filter);
1790+ return ;
18111791 }
1812-
1813- case lp::NodeKind::kProject :
1792+ case lp::NodeKind::kProject : {
18141793 // A project is always allowed in a DT. Multiple projects are combined.
18151794 makeQueryGraph (*node.onlyInput (), allowedInDt);
1816- return addProjection (node.asUnchecked <lp::ProjectNode>());
1817-
1818- case lp::NodeKind::kAggregate :
1819- if (!contains (allowedInDt, PlanType::kAggregationNode )) {
1820- return wrapInDt (node);
1795+ addProjection (*node.asUnchecked <lp::ProjectNode>());
1796+ return ;
1797+ }
1798+ case lp::NodeKind::kAggregate : {
1799+ if (!contains (allowedInDt, lp::NodeKind::kAggregate )) {
1800+ wrapInDt (node);
1801+ return ;
18211802 }
18221803
18231804 // A single groupBy is allowed before a limit. If arrives after orderBy,
@@ -1832,19 +1813,21 @@ PlanObjectP ToGraph::makeQueryGraph(
18321813 currentDt_->orderTypes .clear ();
18331814 }
18341815
1835- addAggregation (*node.asUnchecked <lp::AggregateNode>());
1836-
1837- return currentDt_;
1816+ currentDt_->aggregation =
1817+ translateAggregation (*node.asUnchecked <lp::AggregateNode>());
18381818
1839- case lp::NodeKind::kJoin :
1840- if (!contains (allowedInDt, PlanType::kJoinNode )) {
1841- return wrapInDt (node);
1819+ return ;
1820+ }
1821+ case lp::NodeKind::kJoin : {
1822+ if (!contains (allowedInDt, lp::NodeKind::kJoin )) {
1823+ wrapInDt (node);
1824+ return ;
18421825 }
18431826
18441827 translateJoin (*node.asUnchecked <lp::JoinNode>());
1845- return currentDt_ ;
1846-
1847- case lp::NodeKind::kSort :
1828+ return ;
1829+ }
1830+ case lp::NodeKind::kSort : {
18481831 // Multiple orderBys are allowed before a limit. Last one wins. Previous
18491832 // are dropped. If arrives after limit, then starts a new DT.
18501833
@@ -1854,15 +1837,16 @@ PlanObjectP ToGraph::makeQueryGraph(
18541837 finalizeDt (*node.onlyInput ());
18551838 }
18561839
1857- return addOrderBy (*node.asUnchecked <lp::SortNode>());
1858-
1840+ addOrderBy (*node.asUnchecked <lp::SortNode>());
1841+ return ;
1842+ }
18591843 case lp::NodeKind::kLimit : {
18601844 // Multiple limits are allowed. If already present, then it is combined
18611845 // with the new limit.
18621846 makeQueryGraph (*node.onlyInput (), allowedInDt);
1863- return addLimit (*node.asUnchecked <lp::LimitNode>());
1847+ addLimit (*node.asUnchecked <lp::LimitNode>());
1848+ return ;
18641849 }
1865-
18661850 case lp::NodeKind::kSet : {
18671851 auto * setDt = newDt ();
18681852
@@ -1875,12 +1859,12 @@ PlanObjectP ToGraph::makeQueryGraph(
18751859 translateSetJoin (*set, setDt);
18761860 }
18771861 currentDt_->addTable (setDt);
1878- return currentDt_ ;
1862+ return ;
18791863 }
1880-
18811864 case lp::NodeKind::kUnnest : {
1882- if (!contains (allowedInDt, PlanType::kUnnestTableNode )) {
1883- return wrapInDt (node);
1865+ if (!contains (allowedInDt, lp::NodeKind::kUnnest )) {
1866+ wrapInDt (node);
1867+ return ;
18841868 }
18851869
18861870 // Multiple unnest is allowed in a DT.
@@ -1894,13 +1878,13 @@ PlanObjectP ToGraph::makeQueryGraph(
18941878 finalizeDt (input);
18951879 }
18961880 translateUnnest (*node.asUnchecked <lp::UnnestNode>(), isNewDt);
1897- return currentDt_ ;
1881+ return ;
18981882 }
1899-
1900- case lp::NodeKind::kTableWrite :
1883+ case lp::NodeKind::kTableWrite : {
19011884 wrapInDt (*node.onlyInput ());
1902- return addWrite (*node.asUnchecked <lp::TableWriteNode>());
1903-
1885+ addWrite (*node.asUnchecked <lp::TableWriteNode>());
1886+ return ;
1887+ }
19041888 default :
19051889 VELOX_NYI (
19061890 " Unsupported PlanNode {}" , lp::NodeKindName::toName (node.kind ()));
0 commit comments