Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/optimizer/robust_optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,29 @@ ColumnBinding RobustOptimizerContextState::ResolveColumnBinding(const ColumnBind
return current;
}

static idx_t TableUFFind(unordered_map<idx_t, idx_t> &parent, idx_t x) {
if (parent.find(x) == parent.end()) {
parent[x] = x;
}
while (parent[x] != x) {
parent[x] = parent[parent[x]];
x = parent[x];
}
return x;
}

static void TableUFUnion(unordered_map<idx_t, idx_t> &parent, idx_t a, idx_t b) {
a = TableUFFind(parent, a);
b = TableUFFind(parent, b);
if (a != b) {
parent[a] = b;
}
}

vector<JoinEdge> RobustOptimizerContextState::CreateJoinEdges(vector<LogicalOperator *> &join_ops) {
vector<JoinEdge> edges;
unordered_map<idx_t, idx_t> table_parent;
set<pair<idx_t, idx_t>> seen_pairs;
for (auto &op : join_ops) {
auto &join = op->Cast<LogicalComparisonJoin>();

Expand Down Expand Up @@ -215,6 +236,26 @@ vector<JoinEdge> RobustOptimizerContextState::CreateJoinEdges(vector<LogicalOper
} else {
D_PRINTF("WARNING: Resolved table indices (%llu, %llu) not found in table_lookup",
(unsigned long long)left_table_idx, (unsigned long long)right_table_idx);
continue;
}
for (idx_t i = 0; i < resolved_left_columns.size(); i++) {
auto u = resolved_left_columns[i].table_index;
auto v = resolved_right_columns[i].table_index;
if (u > v) {
std::swap(u, v);
}
if (!seen_pairs.count({u, v})) {
if (TableUFFind(table_parent, u) == TableUFFind(table_parent, v)) {
exist_cycle = true;
break;
} else {
seen_pairs.insert({u, v});
TableUFUnion(table_parent, u, v);
}
}
}
if (exist_cycle) {
break;
}
}
}
Expand Down Expand Up @@ -1662,6 +1703,11 @@ unique_ptr<LogicalOperator> RobustOptimizerContextState::Optimize(unique_ptr<Log
return plan;
}

if (exist_cycle) {
D_PRINTF("Cycle Detected");
return plan;
}

// display physical plan DAG if enabled (before we modify the plan)
PrintPhysicalPlanDAG(plan.get());

Expand Down
2 changes: 2 additions & 0 deletions src/optimizer/robust_optimizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class RobustOptimizerContextState : public ClientContextState {

unordered_map<ColumnBinding, ColumnBinding, ColumnBindingHashFunction> rename_col_bindings;

bool exist_cycle = false;

public:
// extract all the join edges from the plan
// vector<JoinEdge> ExtractOperators(LogicalOperator &plan, vector<LogicalOperator*> &join_ops);
Expand Down
21 changes: 1 addition & 20 deletions test/sql/plan_positive.test
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,7 @@ WHERE t1.k12 = t2.k12
----
physical_plan <REGEX>:.*PROBE_FILTER.*

# Correctness: triangle join
query II
EXPLAIN SELECT count(*)
FROM t2, t3, t4
WHERE t2.k23 = t3.k23
AND t3.k34 = t4.k34
AND t2.k24 = t4.k24;
----
physical_plan <REGEX>:.*CREATE_FILTER.*

query II
EXPLAIN SELECT count(*)
FROM t2, t3, t4
WHERE t2.k23 = t3.k23
AND t3.k34 = t4.k34
AND t2.k24 = t4.k24;
----
physical_plan <REGEX>:.*PROBE_FILTER.*

# Negative: four-table chain join
# Positive: four-table chain join
query II
EXPLAIN SELECT count(*)
FROM t1, t2, t3, t4
Expand Down
Loading