| 1 | #include "duckdb/execution/operator/join/physical_hash_join.hpp" |
| 2 | #include "duckdb/execution/operator/set/physical_union.hpp" |
| 3 | #include "duckdb/execution/physical_plan_generator.hpp" |
| 4 | #include "duckdb/planner/expression/bound_reference_expression.hpp" |
| 5 | #include "duckdb/planner/operator/logical_set_operation.hpp" |
| 6 | |
| 7 | namespace duckdb { |
| 8 | |
| 9 | unique_ptr<PhysicalOperator> PhysicalPlanGenerator::CreatePlan(LogicalSetOperation &op) { |
| 10 | D_ASSERT(op.children.size() == 2); |
| 11 | |
| 12 | auto left = CreatePlan(op&: *op.children[0]); |
| 13 | auto right = CreatePlan(op&: *op.children[1]); |
| 14 | |
| 15 | if (left->GetTypes() != right->GetTypes()) { |
| 16 | throw InvalidInputException("Type mismatch for SET OPERATION" ); |
| 17 | } |
| 18 | |
| 19 | switch (op.type) { |
| 20 | case LogicalOperatorType::LOGICAL_UNION: |
| 21 | // UNION |
| 22 | return make_uniq<PhysicalUnion>(args&: op.types, args: std::move(left), args: std::move(right), args&: op.estimated_cardinality); |
| 23 | default: { |
| 24 | // EXCEPT/INTERSECT |
| 25 | D_ASSERT(op.type == LogicalOperatorType::LOGICAL_EXCEPT || op.type == LogicalOperatorType::LOGICAL_INTERSECT); |
| 26 | auto &types = left->GetTypes(); |
| 27 | vector<JoinCondition> conditions; |
| 28 | // create equality condition for all columns |
| 29 | for (idx_t i = 0; i < types.size(); i++) { |
| 30 | JoinCondition cond; |
| 31 | cond.left = make_uniq<BoundReferenceExpression>(args: types[i], args&: i); |
| 32 | cond.right = make_uniq<BoundReferenceExpression>(args: types[i], args&: i); |
| 33 | cond.comparison = ExpressionType::COMPARE_NOT_DISTINCT_FROM; |
| 34 | conditions.push_back(x: std::move(cond)); |
| 35 | } |
| 36 | // EXCEPT is ANTI join |
| 37 | // INTERSECT is SEMI join |
| 38 | PerfectHashJoinStats join_stats; // used in inner joins only |
| 39 | JoinType join_type = op.type == LogicalOperatorType::LOGICAL_EXCEPT ? JoinType::ANTI : JoinType::SEMI; |
| 40 | return make_uniq<PhysicalHashJoin>(args&: op, args: std::move(left), args: std::move(right), args: std::move(conditions), args&: join_type, |
| 41 | args&: op.estimated_cardinality, args&: join_stats); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | } // namespace duckdb |
| 47 | |