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

Code before:
			const auto column_count = child_bindings.size();

			vector<unique_ptr<Expression>> expressions;
			expressions.reserve(column_count);
			for (idx_t i = 0; i < column_count; i++) {
				expressions.push_back(make_uniq<BoundColumnRefExpression>(child_types[i], child_bindings[i]));
			}

			ColumnBindingReplacer replacer;
			for (idx_t col_idx = 0; col_idx < column_count; col_idx++) {
				const auto &old_binding = child_bindings[col_idx];
				replacer.replacement_bindings.emplace_back(old_binding, ColumnBinding(proj_index, col_idx));
			}

			auto new_projection = make_uniq<LogicalProjection>(proj_index, std::move(expressions));
			if (child->has_estimated_cardinality) {
				new_projection->SetEstimatedCardinality(child->estimated_cardinality);
			}

			new_projection->children.emplace_back(std::move(child));
			child = std::move(new_projection);

			if (stop_at_op) {
				replacer.stop_operator = op.get();
			} else {
				replacer.stop_operator = child.get();
			}
			replacer.VisitOperator(root);

			Optimize(child->children[0]);
		}
	}
	PopParents(*op);
}

void ProjectionPullup::Optimize(unique_ptr<LogicalOperator> &op) {
	switch (op->type) {
	// These operators depend on column order.
	// If their immediate child is a projection, keep it and recurse into the projection’s child.
	// If no projection is present, insert one, then recurse into the newly added projection’s child.
	case LogicalOperatorType::LOGICAL_INTERSECT:
	case LogicalOperatorType::LOGICAL_EXCEPT:
	case LogicalOperatorType::LOGICAL_UNION: {
		InsertProjectionBelowOp(op, true);
		return;
	}
	case LogicalOperatorType::LOGICAL_DISTINCT:
	case LogicalOperatorType::LOGICAL_RECURSIVE_CTE:
	case LogicalOperatorType::LOGICAL_MATERIALIZED_CTE:
	case LogicalOperatorType::LOGICAL_CTE_REF:
	case LogicalOperatorType::LOGICAL_COPY_TO_FILE:

Review comment (performance):
Both the `if` and the `else` branch call `Optimize(child->children[0]);`, so that can be taken out of the `if`/`else`.

However, I'm a bit confused about why you are adding the current operator to the stack and calling `Optimize` on the child. Neither `op`, nor the child projection can be pulled through. I think you should create a child optimizer for the 2nd children instead so that you're not adding operators to the stack that cannot be pulled through.

Code after:
			expressions.reserve(column_count);
			for (idx_t i = 0; i < column_count; i++) {
				expressions.push_back(make_uniq<BoundColumnRefExpression>(child_types[i], child_bindings[i]));
			}

			ColumnBindingReplacer replacer;
			for (idx_t col_idx = 0; col_idx < column_count; col_idx++) {
				const auto &old_binding = child_bindings[col_idx];
				replacer.replacement_bindings.emplace_back(old_binding, ColumnBinding(proj_index, col_idx));
			}

			auto new_projection = make_uniq<LogicalProjection>(proj_index, std::move(expressions));
			if (child->has_estimated_cardinality) {
				new_projection->SetEstimatedCardinality(child->estimated_cardinality);
			}

			new_projection->children.emplace_back(std::move(child));
			child = std::move(new_projection);

			if (stop_at_op) {
				replacer.stop_operator = op.get();
			} else {
				replacer.stop_operator = child.get();
			}
			replacer.VisitOperator(root);
		}
		ProjectionPullup next(optimizer, root);
		next.Optimize(child->children[0]);
	}
}

void ProjectionPullup::Optimize(unique_ptr<LogicalOperator> &op) {
	switch (op->type) {
	// These operators depend on column order.
	// If their immediate child is a projection, keep it and recurse into the projection’s child.
	// If no projection is present, insert one, then recurse into the newly added projection’s child.
	case LogicalOperatorType::LOGICAL_INTERSECT:
	case LogicalOperatorType::LOGICAL_EXCEPT:
	case LogicalOperatorType::LOGICAL_UNION: {
		InsertProjectionBelowOp(op, true);
		return;
	}
	case LogicalOperatorType::LOGICAL_DISTINCT:
	case LogicalOperatorType::LOGICAL_RECURSIVE_CTE:
	case LogicalOperatorType::LOGICAL_MATERIALIZED_CTE:
	case LogicalOperatorType::LOGICAL_CTE_REF:
	case LogicalOperatorType::LOGICAL_COPY_TO_FILE:
	case LogicalOperatorType::LOGICAL_PIVOT: {
		InsertProjectionBelowOp(op, false);
		return;
	}