diff --git a/dbcon/execplan/calpontselectexecutionplan.h b/dbcon/execplan/calpontselectexecutionplan.h index c9b3c9fea..36b48403d 100644 --- a/dbcon/execplan/calpontselectexecutionplan.h +++ b/dbcon/execplan/calpontselectexecutionplan.h @@ -945,6 +945,7 @@ class CalpontSelectExecutionPlan : public CalpontExecutionPlan bool fHasOrderBy = false; // for Select clause subquery + // Populated from derived found in projection list. SelectList fSelectSubList; // @bug3321, for string scan blocks @@ -961,6 +962,7 @@ class CalpontSelectExecutionPlan : public CalpontExecutionPlan uint32_t fOrderByThreads = 1; // Derived table involved in the query. For derived table optimization + // Populated from derived found in filters. std::vector fSubSelectList; boost::uuids::uuid fUuid{}; diff --git a/dbcon/mysql/ha_mcs_execplan.cpp b/dbcon/mysql/ha_mcs_execplan.cpp index e143a38ec..0984073d6 100644 --- a/dbcon/mysql/ha_mcs_execplan.cpp +++ b/dbcon/mysql/ha_mcs_execplan.cpp @@ -7610,6 +7610,14 @@ int cs_get_select_plan(ha_columnstore_select_handler* handler, THD* thd, SCSEP& } } + derivedTableOptimization(&gwi, csep); + if (csep->traceOn()) + { + cerr << "---------------- cs_get_select_plan 2nd derived table optimization pass EXECUTION PLAN ----------------" << endl; + cerr << *csep << endl; + cerr << "-------------- EXECUTION PLAN END --------------\n" << endl; + } + return 0; } diff --git a/dbcon/mysql/rbo_apply_parallel_ces.cpp b/dbcon/mysql/rbo_apply_parallel_ces.cpp index f326251d0..53b04aa87 100644 --- a/dbcon/mysql/rbo_apply_parallel_ces.cpp +++ b/dbcon/mysql/rbo_apply_parallel_ces.cpp @@ -208,6 +208,9 @@ template std::optional> populateRangeBounds(Histogram_json_hb* columnStatistics) { FilterRangeBounds bounds; + // bounds.push_back({0, 6200000}); + + // return bounds; // TODO configurable parallel factor via session variable // NB now histogram size is the way to control parallel factor with 16 being the maximum @@ -395,9 +398,9 @@ bool applyParallelCES(execplan::CalpontSelectExecutionPlan& csep, optimizer::RBO // add new SC auto& [newTableAlias, colPosition] = tableAliasIt->second; auto newSC = boost::make_shared(*rc, rc->sessionID()); - newSC->tableName(""); newSC->schemaName(""); newSC->tableAlias(newTableAlias); + newSC->tableAlias(newTableAlias); newSC->alias(rc->alias()); auto* sc = dynamic_cast(rc.get()); @@ -408,14 +411,16 @@ bool applyParallelCES(execplan::CalpontSelectExecutionPlan& csep, optimizer::RBO if (it == SCAliasToPosCounterMap.end()) { SCAliasToPosCounterMap.insert({sc->columnName(), currentColPosition++}); + colPosition++; std::cout << " first case new column in the map colPosition " << SCAliasToPosCounterMap[sc->columnName()] << std::endl; } else { std::cout << " first case reusing column from the map colPosition " << SCAliasToPosCounterMap[sc->columnName()] << std::endl; } - assert(SCAliasToPosCounterMap[sc->columnName()] == colPosition); + assert(SCAliasToPosCounterMap[sc->columnName()] == colPosition-1); newSC->colPosition(SCAliasToPosCounterMap[sc->columnName()]); + sc->derivedTable(newTableAlias); } else { @@ -445,9 +450,10 @@ bool applyParallelCES(execplan::CalpontSelectExecutionPlan& csep, optimizer::RBO if (tableAliasIt != tableAliasMap.end()) { auto& [newTableAlias, colPosition] = tableAliasIt->second; - sc->tableName(""); sc->schemaName(""); + sc->tableName(newTableAlias); sc->tableAlias(newTableAlias); + sc->derivedTable(newTableAlias); auto& [unused, SCAliasToPosCounterMap, currentColPosition] = tableAliasToSCPositionsIt->second; auto it = SCAliasToPosCounterMap.find(sc->columnName()); @@ -463,6 +469,7 @@ bool applyParallelCES(execplan::CalpontSelectExecutionPlan& csep, optimizer::RBO assert(SCAliasToPosCounterMap[sc->columnName()] == colPosition); sc->colPosition(SCAliasToPosCounterMap[sc->columnName()]); // sc->colPosition(colPosition++); + colPosition++; } // do nothing with this SC } @@ -472,7 +479,6 @@ bool applyParallelCES(execplan::CalpontSelectExecutionPlan& csep, optimizer::RBO // Remove the filters that are not necessary as they were pushed down to union units. // But this is inappropriate for some EXISTS filter and join conditions - // WIP hardcoded query with lhs,rhs being simple columns auto filters = csep.filters(); if (filters) @@ -498,42 +504,14 @@ bool applyParallelCES(execplan::CalpontSelectExecutionPlan& csep, optimizer::RBO } sc->colPosition(SCAliasToPosCounterMap[sc->columnName()]); - sc->tableName(""); sc->schemaName(""); + sc->tableName(newTableAlias); + sc->derivedTable(newTableAlias); sc->tableAlias(newTableAlias); } } } - // if (csep.filters() && csep.filters()->data()) - // { - // auto* left = dynamic_cast(csep.filters()->data()); - // if (left) - // { - // auto* lhs = left->lhs()->clone(); - // if (lhs) - // { - // auto* lhsSC = dynamic_cast(lhs); - // if (lhsSC) - // { - // auto newTableAlias = - // tableAliasMap.find({lhsSC->schemaName(), lhsSC->tableName(), lhsSC->tableAlias(), "", false}); - // // WIP Leak loosing previous lhs - // if (newTableAlias != tableAliasMap.end()) - // { - // lhsSC->tableName(""); - // lhsSC->schemaName(""); - // lhsSC->tableAlias(newTableAlias->second.first); - // lhsSC->colPosition(0); - // left->lhs(lhs); - // } - // } - // } - // } - // } - - - csep.derivedTableList(newDerivedTableList); // Replace table list with new table list populated with union units csep.tableList(newTableList); diff --git a/mysql-test/columnstore/future/MCOL-6072-qa.test b/mysql-test/columnstore/future/MCOL-6072-qa.test new file mode 100644 index 000000000..9e7f75123 --- /dev/null +++ b/mysql-test/columnstore/future/MCOL-6072-qa.test @@ -0,0 +1,25 @@ +# For manual usage only! + +select l_orderkey,l_suppkey from lineitem limit 10; +select l_suppkey,l_orderkey from (select l_suppkey,l_orderkey from lineitem limit 10) s1; + +select s1.* from (select l_suppkey+1 as ho,l_orderkey+1 as mo from lineitem limit 10) s1; +select mo,ho from (select l_suppkey+1 as ho,l_orderkey+1 as mo from lineitem limit 10) s1; + +select nl.l_suppkey,lineitem.l_suppkey from lineitem,lineitem_10rows nl WHERE lineitem.l_suppkey=nl.l_suppkey limit 10; + +select nl.l_orderkey,l.l_suppkey,nl.l_suppkey,l.l_suppkey from lineitem l,lineitem_10rows nl WHERE l.l_suppkey=nl.l_suppkey limit 10; +# test set with join +select nl.l_orderkey,l.l_suppkey,nl.l_suppkey,l.l_orderkey from lineitem l,lineitem_10rows nl WHERE l.l_suppkey=nl.l_suppkey limit 10; + +select nl.l_orderkey,l.l_suppkey,nl.l_suppkey,l.l_orderkey from (select l_orderkey,l_suppkey from lineitem) l,lineitem_10rows nl WHERE l.l_suppkey=nl.l_suppkey limit 10; +select * from (select l_orderkey,l_suppkey+1 from lineitem limit 10) sa where sa.l_orderkey = 1999905; + + +elect nl.l_orderkey,l.l_suppkey,nl.l_suppkey,l.l_orderkey from (select l_orderkey,l_suppkey from lineitem) l,(select l_orderkey,l_suppkey from lineitem_10rows) nl WHERE l.l_suppkey=nl.l_suppkey limit 10; + + +select nl.l_orderkey,l.l_suppkey+1 from lineitem l,lineitem_10rows nl WHERE l.l_suppkey=nl.l_suppkey limit 10 + +select nl.l_suppkey,lineitem.l_suppkey from lineitem,lineitem_10rows nl WHERE lineitem.l_suppkey=nl.l_suppkey AND lineitem.l_suppkey = 25 limit 10; +