When RETURNING is used, a PROJECTION is the top level operator for INSERTS, UPDATES, and DELETES We still need to project all values from these operators so the projection on top of them can select from only the table values being inserted. TODO: Push down the projections from the returning statement TODO: Be careful because you might be adding expressions when a user returns *
318419 {
318420 switch (op.type) {
318421 case LogicalOperatorType::LOGICAL_AGGREGATE_AND_GROUP_BY: {
318422
318423 auto &aggr = op.Cast<LogicalAggregate>();
318425
318427 if (aggr.expressions.empty() && aggr.groups.empty()) {
318428
318429 auto count_star_fun = CountStarFun::GetFunction();
318430 FunctionBinder function_binder(context);
318431 aggr.expressions.push_back(
318432 function_binder.BindAggregateFunction(count_star_fun, {}, nullptr, AggregateType::NON_DISTINCT));
318433 }
318434 }
318435
318436
318437
318438
318439
318441 remove.VisitOperatorExpressions(op);
318442 remove.VisitOperator(*op.children[0]);
318443 return;
318444 }
318445 case LogicalOperatorType::LOGICAL_ASOF_JOIN:
318446 case LogicalOperatorType::LOGICAL_DELIM_JOIN:
318447 case LogicalOperatorType::LOGICAL_COMPARISON_JOIN: {
318449 break;
318450 }
318451 auto &comp_join = op.Cast<LogicalComparisonJoin>();
318452
318453 if (comp_join.join_type != JoinType::INNER) {
318454 break;
318455 }
318456
318457
318458
318459
318460 for (auto &cond : comp_join.conditions) {
318461 if (cond.comparison != ExpressionType::COMPARE_EQUAL) {
318462 continue;
318463 }
318464 if (cond.left->GetExpressionClass() != ExpressionClass::BOUND_COLUMN_REF) {
318465 continue;
318466 }
318467 if (cond.right->GetExpressionClass() != ExpressionClass::BOUND_COLUMN_REF) {
318468 continue;
318469 }
318470 if (cond.left->Cast<BoundColumnRefExpression>().return_type.IsFloating()) {
318471 continue;
318472 }
318473 if (cond.right->Cast<BoundColumnRefExpression>().return_type.IsFloating()) {
318474 continue;
318475 }
318476
318477
318478 auto &lhs_col = cond.left->Cast<BoundColumnRefExpression>();
318479 auto &rhs_col = cond.right->Cast<BoundColumnRefExpression>();
318480
318483 continue;
318484 }
318485 for (auto &entry : colrefs->second.bindings) {
318486 auto &colref = entry.get();
318487 colref.binding = lhs_col.binding;
318489 }
318491 }
318492 break;
318493 }
318494 case LogicalOperatorType::LOGICAL_ANY_JOIN:
318495 break;
318496 case LogicalOperatorType::LOGICAL_UNION: {
318497 auto &setop = op.Cast<LogicalSetOperation>();
318499
318500
318501
318502 vector<idx_t> entries;
318503 for (
idx_t i = 0; i < setop.column_count; i++) {
318504 entries.push_back(i);
318505 }
318507 if (entries.size() >= setop.column_count) {
318508
318509 for (auto &child : op.children) {
318510 RemoveUnusedColumns remove(*this, true);
318511 remove.VisitOperator(*child);
318512 }
318513
318514 return;
318515 }
318516 if (entries.empty()) {
318517
318518
318519 entries.push_back(0);
318520 }
318521
318522 setop.column_count = entries.size();
318523
318524 for (
idx_t child_idx = 0; child_idx < op.children.size(); child_idx++) {
318525 RemoveUnusedColumns remove(*this, true);
318526 auto &child = op.children[child_idx];
318527
318528
318529 child->ResolveOperatorTypes();
318530 auto bindings = child->GetColumnBindings();
318531 vector<unique_ptr<Expression>> expressions;
318532 expressions.reserve(entries.size());
318533 for (auto &column_idx : entries) {
318534 expressions.push_back(
318535 make_uniq<BoundColumnRefExpression>(child->types[column_idx], bindings[column_idx]));
318536 }
318537
318538 auto new_projection = make_uniq<LogicalProjection>(binder.GenerateTableIndex(), std::move(expressions));
318539 if (child->has_estimated_cardinality) {
318540 new_projection->SetEstimatedCardinality(child->estimated_cardinality);
318541 }
318542 new_projection->children.push_back(std::move(child));
318543 op.children[child_idx] = std::move(new_projection);
318544
318545 remove.VisitOperator(*op.children[child_idx]);
318546 }
318547 return;
318548 }
318549 for (auto &child : op.children) {
318550 RemoveUnusedColumns remove(*this, true);
318551 remove.VisitOperator(*child);
318552 }
318553 return;
318554 }
318555 case LogicalOperatorType::LOGICAL_EXCEPT:
318556 case LogicalOperatorType::LOGICAL_INTERSECT: {
318557
318558 for (auto &child : op.children) {
318559 RemoveUnusedColumns remove(*this, true);
318560 remove.VisitOperator(*child);
318561 }
318562 return;
318563 }
318564 case LogicalOperatorType::LOGICAL_PROJECTION: {
318566 auto &proj = op.Cast<LogicalProjection>();
318568 auto old_expression_count = proj.expressions.size();
318571
318572 if (proj.expressions.empty()) {
318573
318574
318575
318576 proj.expressions.push_back(make_uniq<BoundConstantExpression>(
Value::INTEGER(42)));
318577 }
318578 }
318579
318580 RemoveUnusedColumns remove(*this, false);
318581 remove.VisitOperatorExpressions(op);
318582 remove.VisitOperator(*op.children[0]);
318583 return;
318584 }
318585 case LogicalOperatorType::LOGICAL_INSERT:
318586 case LogicalOperatorType::LOGICAL_UPDATE:
318587 case LogicalOperatorType::LOGICAL_DELETE:
318588 case LogicalOperatorType::LOGICAL_MERGE_INTO: {
318594 RemoveUnusedColumns remove(*this, true);
318595 remove.VisitOperatorExpressions(op);
318596 remove.VisitOperator(*op.children[0]);
318597 return;
318598 }
318599 case LogicalOperatorType::LOGICAL_GET: {
318601 auto &
get = op.Cast<LogicalGet>();
318603 if (!op.children.empty()) {
318604
318605
318606 RemoveUnusedColumns remove(*this, true);
318607 remove.VisitOperator(*op.children[0]);
318608 }
318609 return;
318610 }
318611 case LogicalOperatorType::LOGICAL_DISTINCT: {
318612 auto &distinct = op.Cast<LogicalDistinct>();
318613 if (distinct.distinct_type == DistinctType::DISTINCT_ON) {
318614
318615
318616 break;
318617 }
318618
318619
318620
318622 break;
318623 }
318624 case LogicalOperatorType::LOGICAL_RECURSIVE_CTE: {
318625
318626
318627
318628 auto &rec = op.Cast<LogicalCTE>();
318629 auto &cte_info_map = GetCTEMap();
318630 cte_info_map.insert({rec.table_index, MaterializedCTEInfo()});
318632 break;
318633 }
318634 case LogicalOperatorType::LOGICAL_MATERIALIZED_CTE: {
318635 auto &cte_map_ref = GetCTEMap();
318636 auto &cte = op.Cast<LogicalCTE>();
318637 auto &cte_map_entry = cte_map_ref[cte.table_index];
318638
318639
318640 GatherCTEScans(cte.table_index, *cte.children[1], cte_map_entry.expected_readers);
318641 cte_map_entry.everything_referenced = false;
318642 RemoveUnusedColumns rhs_child_optimizer(*this, true);
318643 rhs_child_optimizer.VisitOperator(*cte.children[1]);
318644
318645 unordered_set<idx_t> referenced_columns_in_rhs;
318646 for (auto &entry : cte_map_entry.column_references) {
318647 referenced_columns_in_rhs.insert(entry.first.column_index);
318648 }
318649
318650
318651
318652
318653 auto have_seen_all_readers = cte_map_entry.expected_readers == cte_map_entry.seen_readers;
318654
318655 if (!have_seen_all_readers) {
318656
318657 throw InternalException("CTE pruning did not traverse the entire plan. This is a bug in the optimizer.");
318658 }
318659
318660 if (!cte_map_entry.everything_referenced && have_seen_all_readers) {
318661 RemoveUnusedColumns lhs_child_optimizer(*this, true);
318662
318663
318664 cte.children[0]->ResolveOperatorTypes();
318665 auto bindings = cte.children[0]->GetColumnBindings();
318666 vector<unique_ptr<Expression>> expressions;
318667 if (referenced_columns_in_rhs.empty()) {
318668
318669 referenced_columns_in_rhs.insert(0);
318670 }
318671 for (
idx_t i = 0; i < bindings.size(); i++) {
318672 if (referenced_columns_in_rhs.find(i) != referenced_columns_in_rhs.end()) {
318673 expressions.push_back(make_uniq<BoundColumnRefExpression>(cte.children[0]->types[i], bindings[i]));
318674 }
318675 }
318676
318677 auto projection = make_uniq<LogicalProjection>(binder.GenerateTableIndex(), std::move(expressions));
318678 projection->children.push_back(std::move(cte.children[0]));
318679 cte.children[0] = std::move(projection);
318680
318681 lhs_child_optimizer.VisitOperator(*cte.children[0]);
318682
318683
318684
318685 CTERefPruner cte_ref_pruner(cte.table_index, referenced_columns_in_rhs);
318686 cte_ref_pruner.VisitOperator(*cte.children[1]);
318687
318688
318689
318690
318691 ColumnBindingReplacer column_binding_replacer;
318692 column_binding_replacer.replacement_bindings = cte_ref_pruner.binding_replacements;
318693 column_binding_replacer.VisitOperator(*cte.children[1]);
318694 return;
318695 }
318697
318698 RemoveUnusedColumns remove(*this, true);
318699 remove.VisitOperator(*cte.children[0]);
318700 return;
318701 }
318702 case LogicalOperatorType::LOGICAL_CTE_REF: {
318703 auto &cte_ref = op.Cast<LogicalCTERef>();
318704
318705 auto cte_info_map = TryGetCTEMap();
318706 if (!cte_info_map) {
318708 break;
318709 }
318710 auto &cte_map_ref = *cte_info_map;
318711 auto it = cte_map_ref.find(cte_ref.cte_index);
318712 if (it == cte_map_ref.end()) {
318713 throw InternalException("Could not find CTE definition for CTE reference");
318714 }
318715 auto &cte_entry = it->second;
318716 auto &referenced_columns = cte_entry.column_references;
318717
318718
318719 it->second.seen_readers.insert(cte_ref.table_index);
318720
318722 if (entry.first.table_index == cte_ref.table_index) {
318723 referenced_columns.insert(entry);
318724 }
318725 }
318726
318727 cte_map_ref[cte_ref.cte_index].everything_referenced |=
318728 cte_ref.chunk_types.size() == cte_map_ref[cte_ref.cte_index].column_references.size();
318729
318730 break;
318731 }
318732 case LogicalOperatorType::LOGICAL_COPY_TO_FILE:
318733 case LogicalOperatorType::LOGICAL_PIVOT: {
318735 break;
318736 }
318737 default:
318738 break;
318739 }
318742
318743 if (op.type == LogicalOperatorType::LOGICAL_ASOF_JOIN || op.type == LogicalOperatorType::LOGICAL_DELIM_JOIN ||
318744 op.type == LogicalOperatorType::LOGICAL_COMPARISON_JOIN) {
318745 auto &comp_join = op.Cast<LogicalComparisonJoin>();
318746
318747 vector<JoinCondition> unique_conditions;
318748 for (auto &cond : comp_join.conditions) {
318749 bool found = false;
318750 for (auto &unique_cond : unique_conditions) {
318751 if (cond.comparison == unique_cond.comparison && cond.left->Equals(*unique_cond.left) &&
318752 cond.right->Equals(*unique_cond.right)) {
318753 found = true;
318754 break;
318755 }
318756 }
318757 if (!found) {
318758 unique_conditions.push_back(std::move(cond));
318759 }
318760 }
318761 comp_join.conditions = std::move(unique_conditions);
318762 }
318763}
void AddBinding(BoundColumnRefExpression &col)
Add a reference to the column in its entirey.
Definition duckdb.cpp:319428
column_binding_map_t< ReferencedColumn > column_references
The map of column references.
Definition duckdb.cpp:60560
void VisitOperatorExpressions(LogicalOperator &op)
Definition duckdb.cpp:381188
void VisitOperatorChildren(LogicalOperator &op)
Definition duckdb.cpp:380983
void CheckPushdownExtract(LogicalOperator &op)
Definition duckdb.cpp:318932
void RewriteExpressions(LogicalProjection &proj, idx_t expression_count)
Definition duckdb.cpp:318883
void ClearUnusedExpressions(vector< T > &list, idx_t table_idx, bool replace=true)
Definition duckdb.cpp:318384
void RemoveColumnsFromLogicalGet(LogicalGet &get)
Definition duckdb.cpp:319005
static DUCKDB_API Value INTEGER(int32_t value)
Create an integer Value from a specified value.
Definition duckdb.cpp:132139
__host__ cudaTextureObject_t get() const noexcept
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237