Skip to content
This repository was archived by the owner on May 9, 2024. It is now read-only.

Commit b0bc9ca

Browse files
committed
Fix input descs ordering to get determined UNION ALL results.
Signed-off-by: ienkovich <[email protected]>
1 parent 661ca1a commit b0bc9ca

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

omniscidb/QueryEngine/RelAlgExecutor.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,14 @@ ExecutionResult RelAlgExecutor::executeStep(const hdk::ir::Node* step_root,
14941494
auto sort = step_root->as<hdk::ir::Sort>();
14951495
ExecutionOptions eo_with_limit =
14961496
eo.with_just_validate(eo.just_validate || (sort && sort->isEmptyResult()));
1497+
// Use additional result fragments sort for UNION ALL case.
1498+
// Detect it via a check for two outer tables in the input
1499+
// descriptors vector.
1500+
if (work_unit.exe_unit.input_descs.size() >= 2 &&
1501+
work_unit.exe_unit.input_descs[0].getNestLevel() ==
1502+
work_unit.exe_unit.input_descs[1].getNestLevel()) {
1503+
eo_with_limit = eo_with_limit.with_preserve_order(true);
1504+
}
14971505

14981506
bool cpu_only = false;
14991507
if (auto project = step_root->as<hdk::ir::Project>()) {

omniscidb/QueryEngine/WorkUnitBuilder.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,13 @@ int WorkUnitBuilder::assignNestLevels(const ir::Node* node, int start_idx) {
494494

495495
if (node->is<ir::LogicalUnion>()) {
496496
for (size_t i = 0; i < node->inputCount(); ++i) {
497-
assignNestLevels(node->getInput(i), start_idx);
497+
auto input_node = node->getInput(i);
498+
assignNestLevels(input_node, start_idx);
499+
if (auto scan = input_node->as<ir::Scan>()) {
500+
union_order_[{scan->getDatabaseId(), scan->getTableId()}] = i;
501+
} else {
502+
union_order_[{-1, -(int)input_node->getId()}] = i;
503+
}
498504
}
499505
++start_idx;
500506
} else {
@@ -547,8 +553,13 @@ void WorkUnitBuilder::computeInputDescs() {
547553

548554
std::sort(input_descs_.begin(),
549555
input_descs_.end(),
550-
[](const InputDescriptor& lhs, const InputDescriptor& rhs) {
551-
return lhs.getNestLevel() < rhs.getNestLevel();
556+
[this](const InputDescriptor& lhs, const InputDescriptor& rhs) {
557+
if (lhs.getNestLevel() != rhs.getNestLevel()) {
558+
return lhs.getNestLevel() < rhs.getNestLevel();
559+
}
560+
TableRef lhs_ref{lhs.getDatabaseId(), lhs.getTableId()};
561+
TableRef rhs_ref{rhs.getDatabaseId(), rhs.getTableId()};
562+
return union_order_.at(lhs_ref) < union_order_.at(rhs_ref);
552563
});
553564
}
554565

omniscidb/QueryEngine/WorkUnitBuilder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ class WorkUnitBuilder {
120120
std::unordered_map<const ir::Node*, int> input_nest_levels_;
121121
// Stores nest level for each node to process.
122122
std::unordered_map<const ir::Node*, int> all_nest_levels_;
123+
// Stores order for each UNION ALL input node for proper
124+
// result ordering.
125+
std::unordered_map<TableRef, int> union_order_;
123126
std::vector<JoinType> join_types_;
124127
bool is_agg_ = false;
125128

0 commit comments

Comments
 (0)