Repository: duckdb/duckdb
File: src/optimizer/remove_unused_columns.cpp (C++)

Code before:
		return;
	}
	case LogicalOperatorType::LOGICAL_EXCEPT:
	case LogicalOperatorType::LOGICAL_INTERSECT: {
		// for INTERSECT/EXCEPT operations we can't remove anything, just recursively visit the children
		for (auto &child : op.children) {
			RemoveUnusedColumns remove(binder, context, true);
			remove.VisitOperator(*child);
		}
		return;
	}
	case LogicalOperatorType::LOGICAL_PROJECTION: {
		if (!everything_referenced) {
			auto &proj = op.Cast<LogicalProjection>();
			ClearUnusedExpressions(proj.expressions, proj.table_index);

			if (proj.expressions.empty()) {
				// nothing references the projected expressions
				// this happens in the case of e.g. EXISTS(SELECT * FROM ...)
				// in this case we only need to project a single constant
				proj.expressions.push_back(make_uniq<BoundConstantExpression>(Value::INTEGER(42)));
			}
			for (idx_t idx = 0; idx < proj.expressions.size(); idx++) {
				auto &expr = proj.expressions[idx];
				VisitExpression(&expr);
				if (expr->type != ExpressionType::BOUND_COLUMN_REF) {
					continue;
				}
				auto &colref = expr->Cast<BoundColumnRefExpression>();
				auto record = column_references.find(ColumnBinding(proj.table_index, idx));
				if (record != column_references.end() && !record->second.child_columns.empty()) {
					auto my_record = column_references.find(colref.binding);
					D_ASSERT(column_references.find(colref.binding) != column_references.end());
					my_record->second.child_columns = record->second.child_columns;
				}
			}
			VisitOperator(*op.children[0]);
			return;
		}
		// then recurse into the children of this projection
		RemoveUnusedColumns remove(binder, context);
		remove.VisitOperatorExpressions(op);
		remove.VisitOperator(*op.children[0]);
		return;
	}
	case LogicalOperatorType::LOGICAL_INSERT:
	case LogicalOperatorType::LOGICAL_UPDATE:
	case LogicalOperatorType::LOGICAL_DELETE:
	case LogicalOperatorType::LOGICAL_MERGE_INTO: {
		//! 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

Review comment (suggestion):
Why limit this to having the bound column ref at the root, shouldn't this also work if we have a bound column ref in a function (e.g. `upper(s1.c2)`) (or really any other configuration of column references).

Code after:
				if (child->has_estimated_cardinality) {
					new_projection->SetEstimatedCardinality(child->estimated_cardinality);
				}
				new_projection->children.push_back(std::move(child));
				op.children[child_idx] = std::move(new_projection);

				remove.VisitOperator(*op.children[child_idx]);
			}
			return;
		}
		for (auto &child : op.children) {
			RemoveUnusedColumns remove(binder, context, true);
			remove.VisitOperator(*child);
		}
		return;
	}
	case LogicalOperatorType::LOGICAL_EXCEPT:
	case LogicalOperatorType::LOGICAL_INTERSECT: {
		// for INTERSECT/EXCEPT operations we can't remove anything, just recursively visit the children
		for (auto &child : op.children) {
			RemoveUnusedColumns remove(binder, context, true);
			remove.VisitOperator(*child);
		}
		return;
	}
	case LogicalOperatorType::LOGICAL_PROJECTION: {
		if (!everything_referenced) {
			auto &proj = op.Cast<LogicalProjection>();
			ClearUnusedExpressions(proj.expressions, proj.table_index);

			if (proj.expressions.empty()) {
				// nothing references the projected expressions
				// this happens in the case of e.g. EXISTS(SELECT * FROM ...)
				// in this case we only need to project a single constant
				proj.expressions.push_back(make_uniq<BoundConstantExpression>(Value::INTEGER(42)));
			}
			RemoveUnusedColumns remove(binder, context);
			auto tmp_deliver = remove.deliver_child;
			for (idx_t idx = 0; idx < proj.expressions.size(); idx++) {
				auto &expr = proj.expressions[idx];
				auto record = column_references.find(ColumnBinding(proj.table_index, idx));
				if (record != column_references.end() && !record->second.child_columns.empty()) {
					remove.deliver_child = record->second.child_columns;
				}
				remove.VisitExpression(&expr);
				remove.deliver_child = tmp_deliver;
			}
			remove.VisitOperator(*op.children[0]);
			return;
		}
		// then recurse into the children of this projection