Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::LateMaterialization Class Reference

Transform. More...

Inheritance diagram for duckdb::LateMaterialization:
Collaboration diagram for duckdb::LateMaterialization:

Public Member Functions

 LateMaterialization (Optimizer &optimizer)
unique_ptr< LogicalOperator > Optimize (unique_ptr< LogicalOperator > op)
Public Member Functions inherited from duckdb::LogicalOperatorVisitor
virtual void VisitOperator (LogicalOperator &op)

Private Member Functions

bool TryLateMaterialization (unique_ptr< LogicalOperator > &op)
unique_ptr< LogicalGet > ConstructLHS (LogicalGet &get)
vector< ColumnBinding > ConstructRHS (unique_ptr< LogicalOperator > &op)
vector< idx_t > GetOrInsertRowIds (LogicalGet &get)
void ReplaceTopLevelTableIndex (LogicalOperator &op, idx_t new_index)
void ReplaceTableReferences (unique_ptr< Expression > &expr, idx_t new_table_index)
unique_ptr< Expression > GetExpression (LogicalOperator &op, idx_t column_index)
void ReplaceExpressionReferences (LogicalOperator &next_op, unique_ptr< Expression > &expr)
bool OptimizeLargeLimit (LogicalLimit &limit, idx_t limit_val, bool has_offset)

Private Attributes

Optimizer & optimizer
idx_t max_row_count
 The max row count for which we will consider late materialization.
vector< column_t > row_id_column_ids
 The row-id column ids.
vector< TableColumn > row_id_columns
 The row-id columns.

Additional Inherited Members

Static Public Member Functions inherited from duckdb::LogicalOperatorVisitor
static void EnumerateExpressions (LogicalOperator &op, const std::function< void(unique_ptr< Expression > *child)> &callback)
Protected Member Functions inherited from duckdb::BaseColumnPruner
void VisitExpression (unique_ptr< Expression > *expression) override
unique_ptr< Expression > VisitReplace (BoundColumnRefExpression &expr, unique_ptr< Expression > *expr_ptr) override
unique_ptr< Expression > VisitReplace (BoundReferenceExpression &expr, unique_ptr< Expression > *expr_ptr) override
void AddBinding (BoundColumnRefExpression &col)
 Add a reference to the column in its entirey.
void AddBinding (BoundColumnRefExpression &col, ColumnIndex child_column)
 Add a reference to a sub-section of the column.
void AddBinding (BoundColumnRefExpression &col, ColumnIndex child_column, const vector< ReferencedExtractComponent > &parent)
 Add a reference to a sub-section of the column used in a struct extract, with the parent expression.
idx_t ReplaceBinding (ColumnBinding current_binding, ColumnBinding new_binding)
bool HandleExtractExpression (unique_ptr< Expression > *expression, optional_ptr< unique_ptr< Expression > > cast_expression=nullptr)
bool HandleStructExtract (unique_ptr< Expression > &expr, optional_ptr< BoundColumnRefExpression > &colref, reference< ColumnIndex > &path_ref, vector< ReferencedExtractComponent > &expressions)
bool HandleVariantExtract (unique_ptr< Expression > &expr, optional_ptr< BoundColumnRefExpression > &colref, reference< ColumnIndex > &path_ref, vector< ReferencedExtractComponent > &expressions)
bool HandleExtractRecursive (unique_ptr< Expression > &expr, optional_ptr< BoundColumnRefExpression > &colref, reference< ColumnIndex > &path_ref, vector< ReferencedExtractComponent > &expressions)
void SetMode (BaseColumnPrunerMode mode)
BaseColumnPrunerMode GetMode () const
Protected Member Functions inherited from duckdb::LogicalOperatorVisitor
void VisitOperatorChildren (LogicalOperator &op)
void VisitOperatorExpressions (LogicalOperator &op)
void VisitOperatorWithProjectionMapChildren (LogicalOperator &op)
 Alternatives for VisitOperatorChildren for operators that have a projection map.
void VisitChildOfOperatorWithProjectionMap (LogicalOperator &child, vector< idx_t > &projection_map)
virtual void VisitExpressionChildren (Expression &expression)
virtual unique_ptr< Expression > VisitReplace (BoundAggregateExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundBetweenExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundCaseExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundCastExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundComparisonExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundConjunctionExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundConstantExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundDefaultExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundFunctionExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundOperatorExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundSubqueryExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundParameterExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundWindowExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundUnnestExpression &expr, unique_ptr< Expression > *expr_ptr)
Protected Attributes inherited from duckdb::BaseColumnPruner
column_binding_map_t< ReferencedColumn > column_references
 The map of column references.

Detailed Description

Transform.

Constructor & Destructor Documentation

◆ LateMaterialization()

duckdb::LateMaterialization::LateMaterialization ( Optimizer & optimizer)
explicit
314558 : optimizer(optimizer) {
314559 max_row_count = Settings::Get<LateMaterializationMaxRowsSetting>(optimizer.context);
314560}
idx_t max_row_count
The max row count for which we will consider late materialization.
Definition duckdb.cpp:314506

Member Function Documentation

◆ Optimize()

unique_ptr< LogicalOperator > duckdb::LateMaterialization::Optimize ( unique_ptr< LogicalOperator > op)
314937 {
314938 switch (op->type) {
314939 case LogicalOperatorType::LOGICAL_LIMIT: {
314940 auto &limit = op->Cast<LogicalLimit>();
314941 if (limit.limit_val.Type() != LimitNodeType::CONSTANT_VALUE) {
314942 break;
314943 }
314944 auto limit_val = limit.limit_val.GetConstantValue();
314945 bool has_offset = limit.offset_val.Type() != LimitNodeType::UNSET;
314946 if (limit_val > max_row_count) {
314947 // for large limits - we may still want to do this optimization if the limit is consecutive
314948 // this is the case if there are only projections/get below the limit
314949 // if the row-ids are not consecutive doing the join can worsen performance
314950 if (!OptimizeLargeLimit(limit, limit_val, has_offset)) {
314951 break;
314952 }
314953 } else {
314954 // optimizing small limits really only makes sense if we have an offset
314955 if (!has_offset) {
314956 break;
314957 }
314958 }
314959 if (TryLateMaterialization(op)) {
314960 return op;
314961 }
314962 break;
314963 }
314964 case LogicalOperatorType::LOGICAL_TOP_N: {
314965 auto &top_n = op->Cast<LogicalTopN>();
314966 if (top_n.limit > max_row_count) {
314967 break;
314968 }
314969 // for the top-n we need to visit the order elements
314970 if (TryLateMaterialization(op)) {
314971 return op;
314972 }
314973 break;
314974 }
314975 case LogicalOperatorType::LOGICAL_SAMPLE: {
314976 auto &sample = op->Cast<LogicalSample>();
314977 if (sample.sample_options->is_percentage) {
314978 break;
314979 }
314980 if (sample.sample_options->sample_size.GetValue<uint64_t>() > max_row_count) {
314981 break;
314982 }
314983 if (TryLateMaterialization(op)) {
314984 return op;
314985 }
314986 break;
314987 }
314988 default:
314989 break;
314990 }
314991 for (auto &child : op->children) {
314992 child = Optimize(std::move(child));
314993 }
314994 return op;
314995}

◆ TryLateMaterialization()

bool duckdb::LateMaterialization::TryLateMaterialization ( unique_ptr< LogicalOperator > & op)
private
314688 {
314689 // check if we can benefit from late materialization
314690 // we need to see how many columns we require in the pipeline versus how many columns we emit in the scan
314691 // for example, in a query like SELECT * FROM tbl ORDER BY ts LIMIT 5, the top-n only needs the "ts" column
314692 // the other columns can be fetched later on using late materialization
314693 // we can only push late materialization through a subset of operators
314694 // and we can only do it for scans that support the row-id pushdown (currently only DuckDB table scans)
314695
314696 // visit the expressions for each operator in the chain
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 // recurse into the child node - but ONLY visit expressions that are referenced
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 // we cannot do this optimization if any of the columns are volatile
314711 return false;
314712 }
314713 }
314714
314715 // figure out which projection expressions we are currently referencing
314716 set<idx_t> referenced_columns;
314717 for (auto &entry : column_references) {
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 // clear the list of referenced expressions and visit those columns
314724 column_references.clear();
314725 for (auto &col_idx : referenced_columns) {
314726 VisitExpression(&proj.expressions[col_idx]);
314727 }
314728 // continue into child
314729 child = *child.get().children[0];
314730 break;
314731 }
314732 case LogicalOperatorType::LOGICAL_FILTER: {
314733 // visit filter expressions - we need these columns
314734 VisitOperatorExpressions(child.get());
314735 // continue into child
314736 child = *child.get().children[0];
314737 break;
314738 }
314739 default:
314740 // unsupported operator for late materialization
314741 return false;
314742 }
314743 }
314744 auto &get = child.get().Cast<LogicalGet>();
314745 if (column_references.size() >= get.GetColumnIds().size()) {
314746 // we do not benefit from late materialization
314747 // we need all of the columns to compute the root node anyway (Top-N/Limit/etc)
314748 return false;
314749 }
314750 if (!get.function.late_materialization) {
314751 // this function does not support late materialization
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 }
314757 row_id_column_ids = get.function.get_row_id_columns(optimizer.context, get.bind_data.get());
314758 if (row_id_column_ids.empty()) {
314759 throw InternalException("Row Id Columns must not be empty");
314760 }
314761 row_id_columns.clear();
314762 for (auto &col_id : row_id_column_ids) {
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 }
314767 row_id_columns.push_back(entry->second);
314768 }
314769 // we benefit from late materialization
314770 // we need to transform this plan into a semi-join with the row-id
314771 // we need to ensure the operator returns exactly the same column bindings as before
314772
314773 // construct the LHS from the LogicalGet
314774 auto lhs = LateMaterializationHelper::CreateLHSGet(get, optimizer.binder);
314775 // insert the row-id column on the left hand side
314776 auto &lhs_get = *lhs;
314777 auto lhs_index = lhs_get.table_index;
314778 auto lhs_columns = lhs_get.GetColumnIds().size();
314779 auto lhs_row_indexes = LateMaterializationHelper::GetOrInsertRowIds(lhs_get, row_id_column_ids, row_id_columns);
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 // after constructing the LHS but before constructing the RHS we construct the final projections/orders
314786 // - we do this before constructing the RHS because that alter the original plan
314787 vector<unique_ptr<Expression>> final_proj_list;
314788 // construct the final projection list from either (1) the root projection, or (2) the logical get
314789 if (!source_operators.empty()) {
314790 // construct the columns from the root projection
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 // now we need to "flatten" the projection list by traversing the set of projections and inlining them
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 // if we have no projection directly construct the columns from the root get
314804 for (idx_t i = 0; i < lhs_columns; i++) {
314805 final_proj_list.push_back(GetExpression(lhs_get, i));
314806 }
314807 }
314808
314809 // we need to re-order again at the end
314810 vector<BoundOrderByNode> final_orders;
314811 auto root_type = op->type;
314812 if (root_type == LogicalOperatorType::LOGICAL_TOP_N) {
314813 // for top-n we need to re-order by the top-n conditions
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 // for limit/sample we order by row-id
314821 for (idx_t r_idx = 0; r_idx < row_id_columns.size(); r_idx++) {
314822 auto &row_id_col = row_id_columns[r_idx];
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 // construct the RHS for the join
314830 // this is essentially the old pipeline, but with the `rowid` column added
314831 // note that the purpose of this optimization is to remove columns from the RHS
314832 // we don't do that here yet though - we do this in a later step using the RemoveUnusedColumns optimizer
314833 auto rhs_bindings = ConstructRHS(op);
314834
314835 // the final table index emitted must be the table index of the original operator
314836 // this ensures any upstream operators that refer to the original get will keep on referring to the correct columns
314837 auto final_index = rhs_bindings[0].table_index;
314838
314839 // we need to replace any references to "rhs_binding.table_index" in the rhs to a new table index
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 // construct a semi join between the lhs and rhs
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
314854 for (idx_t r_idx = 0; r_idx < row_id_columns.size(); r_idx++) {
314855 auto &row_id_col = row_id_columns[r_idx];
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 // push a projection that removes the row id again from the lhs
314864 // this is the final projection - so it should have the final table index
314865 auto proj_index = final_index;
314866 if (root_type == LogicalOperatorType::LOGICAL_TOP_N) {
314867 // for top-n we need to order on expressions, so we need to order AFTER the final projection
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 // for limit/sample we order on row-id, so we need to order BEFORE the final projection
314886 // because the final projection removes row-ids
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 // run the RemoveUnusedColumns optimizer to prune the (now) unused columns the plan
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

◆ ConstructRHS()

vector< ColumnBinding > duckdb::LateMaterialization::ConstructRHS ( unique_ptr< LogicalOperator > & op)
private
314562 {
314563 // traverse down until we reach the LogicalGet
314564 vector<reference<LogicalOperator>> stack;
314565 reference<LogicalOperator> child = *op->children[0];
314566 while (child.get().type != LogicalOperatorType::LOGICAL_GET) {
314567 stack.push_back(child);
314568 D_ASSERT(child.get().children.size() == 1);
314569 child = *child.get().children[0];
314570 }
314571 // we have reached the logical get - now we need to push the row-id column (if it is not yet projected out)
314572 auto &get = child.get().Cast<LogicalGet>();
314573 auto row_id_indexes = LateMaterializationHelper::GetOrInsertRowIds(get, row_id_column_ids, row_id_columns);
314574 idx_t column_count = get.projection_ids.empty() ? get.GetColumnIds().size() : get.projection_ids.size();
314575 D_ASSERT(column_count == get.GetColumnBindings().size());
314576
314577 // the row id has been projected - now project it up the stack
314578 vector<ColumnBinding> row_id_bindings;
314579 for (auto &row_id_index : row_id_indexes) {
314580 row_id_bindings.emplace_back(get.table_index, row_id_index);
314581 }
314582 for (idx_t i = stack.size(); i > 0; i--) {
314583 auto &op = stack[i - 1].get();
314584 switch (op.type) {
314585 case LogicalOperatorType::LOGICAL_PROJECTION: {
314586 auto &proj = op.Cast<LogicalProjection>();
314587 // push projection of the row-id columns
314588 for (idx_t r_idx = 0; r_idx < row_id_columns.size(); r_idx++) {
314589 auto &r_col = row_id_columns[r_idx];
314590 proj.expressions.push_back(
314591 make_uniq<BoundColumnRefExpression>(r_col.name, r_col.type, row_id_bindings[r_idx]));
314592 // modify the row-id-binding to the new projection
314593 row_id_bindings[r_idx] = ColumnBinding(proj.table_index, proj.expressions.size() - 1);
314594 }
314595 column_count = proj.expressions.size();
314596 break;
314597 }
314598 case LogicalOperatorType::LOGICAL_FILTER: {
314599 auto &filter = op.Cast<LogicalFilter>();
314600 // column bindings pass-through this operator as-is UNLESS the filter has a projection map
314601 if (filter.HasProjectionMap()) {
314602 // if the filter has a projection map, we need to project the new column
314603 filter.projection_map.push_back(column_count - 1);
314604 }
314605 break;
314606 }
314607 default:
314608 throw InternalException("Unsupported logical operator in LateMaterialization::ConstructRHS");
314609 }
314610 }
314611 return row_id_bindings;
314612}
void filter(InputArray image, InputArray kernel, OutputArray output)

◆ ReplaceTopLevelTableIndex()

void duckdb::LateMaterialization::ReplaceTopLevelTableIndex ( LogicalOperator & op,
idx_t new_index )
private
314614 {
314615 reference<LogicalOperator> current_op = root;
314616 while (true) {
314617 auto &op = current_op.get();
314618 switch (op.type) {
314619 case LogicalOperatorType::LOGICAL_PROJECTION: {
314620 // reached a projection - modify the table index and return
314621 auto &proj = op.Cast<LogicalProjection>();
314622 proj.table_index = new_index;
314623 return;
314624 }
314625 case LogicalOperatorType::LOGICAL_GET: {
314626 // reached the root get - modify the table index and return
314627 auto &get = op.Cast<LogicalGet>();
314628 get.table_index = new_index;
314629 return;
314630 }
314631 case LogicalOperatorType::LOGICAL_TOP_N: {
314632 // visit the expressions of the operator and continue into the child node
314633 auto &top_n = op.Cast<LogicalTopN>();
314634 for (auto &order : top_n.orders) {
314635 ReplaceTableReferences(order.expression, new_index);
314636 }
314637 current_op = *op.children[0];
314638 break;
314639 }
314640 case LogicalOperatorType::LOGICAL_FILTER:
314641 case LogicalOperatorType::LOGICAL_SAMPLE:
314642 case LogicalOperatorType::LOGICAL_LIMIT: {
314643 // visit the expressions of the operator and continue into the child node
314644 for (auto &expr : op.expressions) {
314645 ReplaceTableReferences(expr, new_index);
314646 }
314647 current_op = *op.children[0];
314648 break;
314649 }
314650 default:
314651 throw InternalException("Unsupported operator type in LateMaterialization::ReplaceTopLevelTableIndex");
314652 }
314653 }
314654}

◆ ReplaceTableReferences()

void duckdb::LateMaterialization::ReplaceTableReferences ( unique_ptr< Expression > & expr,
idx_t new_table_index )
private
314656 {
314657 ExpressionIterator::VisitExpressionMutable<BoundColumnRefExpression>(
314658 root_expr, [&](BoundColumnRefExpression &bound_column_ref, unique_ptr<Expression> &expr) {
314659 bound_column_ref.binding.table_index = new_table_index;
314660 });
314661}

◆ GetExpression()

unique_ptr< Expression > duckdb::LateMaterialization::GetExpression ( LogicalOperator & op,
idx_t column_index )
private
314663 {
314664 switch (op.type) {
314665 case LogicalOperatorType::LOGICAL_GET: {
314666 auto &get = op.Cast<LogicalGet>();
314667 auto &column_id = get.GetColumnIds()[column_index];
314668 auto column_name = get.GetColumnName(column_id);
314669 auto &column_type = get.GetColumnType(column_id);
314670 auto expr =
314671 make_uniq<BoundColumnRefExpression>(column_name, column_type, ColumnBinding(get.table_index, column_index));
314672 return std::move(expr);
314673 }
314674 case LogicalOperatorType::LOGICAL_PROJECTION:
314675 return op.expressions[column_index]->Copy();
314676 default:
314677 throw InternalException("Unsupported operator type for LateMaterialization::GetExpression");
314678 }
314679}

◆ ReplaceExpressionReferences()

void duckdb::LateMaterialization::ReplaceExpressionReferences ( LogicalOperator & next_op,
unique_ptr< Expression > & expr )
private
314681 {
314682 ExpressionIterator::VisitExpressionMutable<BoundColumnRefExpression>(
314683 root_expr, [&](BoundColumnRefExpression &bound_column_ref, unique_ptr<Expression> &expr) {
314684 expr = GetExpression(next_op, bound_column_ref.binding.column_index);
314685 });
314686}

◆ OptimizeLargeLimit()

bool duckdb::LateMaterialization::OptimizeLargeLimit ( LogicalLimit & limit,
idx_t limit_val,
bool has_offset )
private
314908 {
314909 if (!has_offset && !Settings::Get<PreserveInsertionOrderSetting>(optimizer.context)) {
314910 // we avoid optimizing large limits if preserve insertion order is false
314911 // since the limit is executed in parallel anyway
314912 return false;
314913 }
314914 // we only perform this optimization until a certain amount of maximum values to reduce memory constraints
314915 // since we still materialize the set of row-ids in the hash table this optimization can increase memory pressure
314916 // FIXME: make this configurable as well
314917 static constexpr const idx_t LIMIT_MAX_VAL = 1000000;
314918 if (limit_val > LIMIT_MAX_VAL) {
314919 return false;
314920 }
314921 // we only support large limits if they are directly below the source
314922 reference<LogicalOperator> current_op = *limit.children[0];
314923 while (current_op.get().type != LogicalOperatorType::LOGICAL_GET) {
314924 if (current_op.get().type != LogicalOperatorType::LOGICAL_PROJECTION) {
314925 return false;
314926 }
314927 current_op = *current_op.get().children[0];
314928 }
314929 // if there are any filters we shouldn't do large limit optimization
314930 auto &get = current_op.get().Cast<LogicalGet>();
314931 if (!get.table_filters.filters.empty()) {
314932 return false;
314933 }
314934 return true;
314935}

The documentation for this class was generated from the following file: