314688 {
314689
314690
314691
314692
314693
314694
314695
314696
314697 vector<reference<LogicalOperator>> source_operators;
314698
314700 reference<LogicalOperator> child = *op->children[0];
314701 while (child.get().type != LogicalOperatorType::LOGICAL_GET) {
314702 switch (child.get().type) {
314703 case LogicalOperatorType::LOGICAL_PROJECTION: {
314704
314705 auto &proj = child.get().Cast<LogicalProjection>();
314706 source_operators.push_back(child);
314707
314708 for (auto &expr : proj.expressions) {
314709 if (expr->IsVolatile()) {
314710
314711 return false;
314712 }
314713 }
314714
314715
314716 set<idx_t> referenced_columns;
314718 auto &column_binding = entry.first;
314719 if (column_binding.table_index == proj.table_index) {
314720 referenced_columns.insert(column_binding.column_index);
314721 }
314722 }
314723
314725 for (auto &col_idx : referenced_columns) {
314727 }
314728
314729 child = *child.get().children[0];
314730 break;
314731 }
314732 case LogicalOperatorType::LOGICAL_FILTER: {
314733
314735
314736 child = *child.get().children[0];
314737 break;
314738 }
314739 default:
314740
314741 return false;
314742 }
314743 }
314744 auto &
get = child.get().Cast<LogicalGet>();
314746
314747
314748 return false;
314749 }
314750 if (!
get.function.late_materialization) {
314751
314752 return false;
314753 }
314754 if (!
get.function.get_row_id_columns) {
314755 throw InternalException("Function supports late materialization but not get_row_id_columns");
314756 }
314759 throw InternalException("Row Id Columns must not be empty");
314760 }
314763 auto entry =
get.virtual_columns.find(col_id);
314764 if (entry ==
get.virtual_columns.end()) {
314765 throw InternalException("Row id column id not found in virtual column list");
314766 }
314768 }
314769
314770
314771
314772
314773
314774 auto lhs = LateMaterializationHelper::CreateLHSGet(
get, optimizer.binder);
314775
314776 auto &lhs_get = *lhs;
314777 auto lhs_index = lhs_get.table_index;
314778 auto lhs_columns = lhs_get.GetColumnIds().size();
314780 vector<ColumnBinding> lhs_bindings;
314781 for (auto &lhs_row_index : lhs_row_indexes) {
314782 lhs_bindings.emplace_back(lhs_index, lhs_row_index);
314783 }
314784
314785
314786
314787 vector<unique_ptr<Expression>> final_proj_list;
314788
314789 if (!source_operators.empty()) {
314790
314791 auto &root_proj = source_operators[0].get();
314792 for (auto &expr : root_proj.expressions) {
314793 final_proj_list.push_back(expr->Copy());
314794 }
314795
314796 for (
idx_t i = 0; i < source_operators.size(); i++) {
314797 auto &next_operator = i + 1 < source_operators.size() ? source_operators[i + 1].get() : lhs_get;
314798 for (auto &expr : final_proj_list) {
314799 ReplaceExpressionReferences(next_operator, expr);
314800 }
314801 }
314802 } else {
314803
314804 for (
idx_t i = 0; i < lhs_columns; i++) {
314805 final_proj_list.push_back(GetExpression(lhs_get, i));
314806 }
314807 }
314808
314809
314810 vector<BoundOrderByNode> final_orders;
314811 auto root_type = op->type;
314812 if (root_type == LogicalOperatorType::LOGICAL_TOP_N) {
314813
314814 auto &top_n = op->Cast<LogicalTopN>();
314815 for (auto &order : top_n.orders) {
314816 auto expr = order.expression->Copy();
314817 final_orders.emplace_back(order.type, order.null_order, std::move(expr));
314818 }
314819 } else {
314820
314823 auto row_id_expr =
314824 make_uniq<BoundColumnRefExpression>(row_id_col.name, row_id_col.type, lhs_bindings[r_idx]);
314825 final_orders.emplace_back(OrderType::ASCENDING, OrderByNullType::NULLS_LAST, std::move(row_id_expr));
314826 }
314827 }
314828
314829
314830
314831
314832
314833 auto rhs_bindings = ConstructRHS(op);
314834
314835
314836
314837 auto final_index = rhs_bindings[0].table_index;
314838
314839
314840 auto rhs_table_index = optimizer.binder.GenerateTableIndex();
314841 for (auto &rhs_binding : rhs_bindings) {
314842 rhs_binding.table_index = rhs_table_index;
314843 }
314844 ReplaceTopLevelTableIndex(*op, rhs_table_index);
314845 if (rhs_bindings.size() != lhs_bindings.size()) {
314846 throw InternalException("Mismatch in late materialization binding sizes");
314847 }
314848
314849
314850 auto join = make_uniq<LogicalComparisonJoin>(JoinType::SEMI);
314851 join->children.push_back(std::move(lhs));
314852 join->children.push_back(std::move(op));
314853
314856 JoinCondition condition;
314857 condition.comparison = ExpressionType::COMPARE_EQUAL;
314858 condition.left = make_uniq<BoundColumnRefExpression>(row_id_col.name, row_id_col.type, lhs_bindings[r_idx]);
314859 condition.right = make_uniq<BoundColumnRefExpression>(row_id_col.name, row_id_col.type, rhs_bindings[r_idx]);
314860 join->conditions.push_back(std::move(condition));
314861 }
314862
314863
314864
314865 auto proj_index = final_index;
314866 if (root_type == LogicalOperatorType::LOGICAL_TOP_N) {
314867
314868 auto proj = make_uniq<LogicalProjection>(proj_index, std::move(final_proj_list));
314869 if (
join->has_estimated_cardinality) {
314870 proj->SetEstimatedCardinality(
join->estimated_cardinality);
314871 }
314872 proj->children.push_back(std::move(join));
314873
314874 for (auto &order : final_orders) {
314875 ReplaceTableReferences(order.expression, proj_index);
314876 }
314877 auto order = make_uniq<LogicalOrder>(std::move(final_orders));
314878 if (proj->has_estimated_cardinality) {
314879 order->SetEstimatedCardinality(proj->estimated_cardinality);
314880 }
314881 order->children.push_back(std::move(proj));
314882
314883 op = std::move(order);
314884 } else {
314885
314886
314887 auto order = make_uniq<LogicalOrder>(std::move(final_orders));
314888 if (
join->has_estimated_cardinality) {
314889 order->SetEstimatedCardinality(
join->estimated_cardinality);
314890 }
314891 order->children.push_back(std::move(join));
314892
314893 auto proj = make_uniq<LogicalProjection>(proj_index, std::move(final_proj_list));
314894 if (order->has_estimated_cardinality) {
314895 proj->SetEstimatedCardinality(order->estimated_cardinality);
314896 }
314897 proj->children.push_back(std::move(order));
314898
314899 op = std::move(proj);
314900 }
314901
314902
314903 RemoveUnusedColumns unused_optimizer(optimizer);
314904 unused_optimizer.VisitOperator(*op);
314905 return true;
314906}
void VisitExpression(unique_ptr< Expression > *expression) override
Definition duckdb.cpp:319455
column_binding_map_t< ReferencedColumn > column_references
The map of column references.
Definition duckdb.cpp:60560
vector< column_t > row_id_column_ids
The row-id column ids.
Definition duckdb.cpp:314508
vector< TableColumn > row_id_columns
The row-id columns.
Definition duckdb.cpp:314510
void VisitOperatorExpressions(LogicalOperator &op)
Definition duckdb.cpp:381188
__host__ cudaTextureObject_t get() const noexcept
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