327655 {
327656 if (op->type == LogicalOperatorType::LOGICAL_WINDOW) {
327657 auto &window = op->Cast<LogicalWindow>();
327658
327659
327660 window.children[0] = OptimizeInternal(std::move(window.children[0]), replacer);
327661
327662 if (!WindowSelfJoinTableRebinder::CanRebind(*window.children[0])) {
327663 return op;
327664 }
327665
327666 auto &w_expr0 = window.expressions[0]->Cast<BoundWindowExpression>();
327667 for (auto &expr : window.expressions) {
327668 auto &w_expr = expr->Cast<BoundWindowExpression>();
327669 if (!CanOptimize(w_expr, w_expr0)) {
327670 return op;
327671 }
327672 }
327673 auto &partitions = w_expr0.partitions;
327674
327675
327676
327677 unique_ptr<LogicalOperator> copy_child;
327678 try {
327679 copy_child = window.children[0]->Copy(optimizer.context);
327680 } catch (...) {
327681
327682 return op;
327683 }
327684 auto original_child = std::move(window.children[0]);
327685
327686
327687 WindowSelfJoinTableRebinder rebinder(optimizer);
327688 rebinder.VisitOperator(*copy_child);
327689
327690 auto aggregate_index = optimizer.binder.GenerateTableIndex();
327691 auto group_index = optimizer.binder.GenerateTableIndex();
327692
327693 vector<unique_ptr<Expression>> groups;
327694 vector<unique_ptr<Expression>> aggregates;
327695
327696
327697 for (auto &part : partitions) {
327698 auto part_copy = part->Copy();
327699 rebinder.VisitExpression(&part_copy);
327700 groups.push_back(std::move(part_copy));
327701 }
327702
327703 for (auto &expr : window.expressions) {
327704 auto &w_expr = expr->Cast<BoundWindowExpression>();
327705 aggregates.emplace_back(rebinder.TranslateAggregate(w_expr));
327706 }
327707
327708
327709 auto agg_op = make_uniq<LogicalAggregate>(group_index, aggregate_index, std::move(aggregates));
327710
327711 agg_op->groups = std::move(groups);
327712 agg_op->children.push_back(std::move(copy_child));
327713 agg_op->ResolveOperatorTypes();
327714
327715 if (agg_op->types.size() <= agg_op->groups.size()) {
327716 throw InternalException("LogicalAggregate types size mismatch");
327717 }
327718
327719
327720 auto join = make_uniq<LogicalComparisonJoin>(JoinType::INNER);
327721
327722 for (size_t i = 0; i < partitions.size(); ++i) {
327723 JoinCondition cond;
327724 cond.comparison = ExpressionType::COMPARE_NOT_DISTINCT_FROM;
327725 cond.left = partitions[i]->Copy();
327726 cond.right = make_uniq<BoundColumnRefExpression>(partitions[i]->return_type, ColumnBinding(group_index, i));
327727 join->conditions.push_back(std::move(cond));
327728 }
327729
327730 join->children.push_back(std::move(original_child));
327731 join->children.push_back(std::move(agg_op));
327732 join->ResolveOperatorTypes();
327733
327734
327735
327736
327737 for (
idx_t column_index = 0; column_index < window.expressions.size(); ++column_index) {
327738 ColumnBinding old_binding(window.window_index, column_index);
327739 ColumnBinding new_binding(aggregate_index, column_index);
327740 replacer.replacement_bindings.emplace_back(old_binding, new_binding);
327741 }
327742
327743 return std::move(join);
327744 } else if (!op->children.empty()) {
327745 for (auto &child : op->children) {
327746 child = OptimizeInternal(std::move(child), replacer);
327747 }
327748 }
327749 return op;
327750}
cv::String join(const cv::String &base, const cv::String &path)
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237