#include "common/system_config.h" #include "planner/operator/logical_union.h" #include "processor/operator/table_scan/union_all_scan.h" #include "processor/plan_mapper.h" using namespace lbug::common; using namespace lbug::planner; namespace lbug { namespace processor { std::unique_ptr PlanMapper::mapUnionAll(const LogicalOperator* logicalOperator) { auto& logicalUnionAll = logicalOperator->constCast(); auto outSchema = logicalUnionAll.getSchema(); // append result collectors to each child std::vector> prevOperators; std::vector> tables; for (auto i = 0u; i < logicalOperator->getNumChildren(); ++i) { auto child = logicalOperator->getChild(i); auto childSchema = logicalUnionAll.getSchemaBeforeUnion(i); auto prevOperator = mapOperator(child.get()); // Use the child's non-deduplicated projection list so the factorized table has // the correct number of columns (matching expressionsToUnion.size()), even when // the child projects the same expression more than once (e.g. RETURN b.age, b.age). auto resultCollector = createResultCollector(AccumulateType::REGULAR, logicalUnionAll.getChildProjections()[i], childSchema, std::move(prevOperator)); tables.push_back(resultCollector->getResultFTable()); prevOperators.push_back(std::move(resultCollector)); } // append union all std::vector outputPositions; std::vector columnIndices; auto expressionsToUnion = logicalUnionAll.getExpressionsToUnion(); for (auto i = 0u; i < expressionsToUnion.size(); ++i) { auto expression = expressionsToUnion[i]; outputPositions.emplace_back(outSchema->getExpressionPos(*expression)); columnIndices.push_back(i); } auto info = UnionAllScanInfo(std::move(outputPositions), std::move(columnIndices)); auto maxMorselSize = tables[0]->hasUnflatCol() ? 1 : DEFAULT_VECTOR_CAPACITY; auto unionSharedState = make_shared(std::move(tables), maxMorselSize); auto printInfo = std::make_unique(expressionsToUnion); auto scan = make_unique(std::move(info), unionSharedState, getOperatorID(), std::move(printInfo)); for (auto& child : prevOperators) { scan->addChild(std::move(child)); } return scan; } } // namespace processor } // namespace lbug