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::CTEInlining Class Reference
Collaboration diagram for duckdb::CTEInlining:

Public Member Functions

 CTEInlining (Optimizer &optimizer)
unique_ptr< LogicalOperator > Optimize (unique_ptr< LogicalOperator > op)

Static Public Member Functions

static bool EndsInAggregateOrDistinct (const LogicalOperator &op)

Private Member Functions

void TryInlining (unique_ptr< LogicalOperator > &op)
bool Inline (unique_ptr< LogicalOperator > &op, LogicalOperator &materialized_cte, bool requires_copy=true)

Private Attributes

Optimizer & optimizer
 The optimizer.
optional_ptr< bound_parameter_map_t > parameter_data

Constructor & Destructor Documentation

◆ CTEInlining()

duckdb::CTEInlining::CTEInlining ( Optimizer & optimizer)
explicit
307102 : optimizer(optimizer_p) {
307103}
Optimizer & optimizer
The optimizer.
Definition duckdb.cpp:304397

Member Function Documentation

◆ Optimize()

unique_ptr< LogicalOperator > duckdb::CTEInlining::Optimize ( unique_ptr< LogicalOperator > op)
307105 {
307106 TryInlining(op);
307107 return op;
307108}

◆ EndsInAggregateOrDistinct()

bool duckdb::CTEInlining::EndsInAggregateOrDistinct ( const LogicalOperator & op)
static
307152 {
307153 switch (op.type) {
307154 case LogicalOperatorType::LOGICAL_AGGREGATE_AND_GROUP_BY:
307155 case LogicalOperatorType::LOGICAL_DISTINCT:
307156 case LogicalOperatorType::LOGICAL_WINDOW:
307157 return true;
307158 default:
307159 break;
307160 }
307161 if (op.children.size() != 1) {
307162 return false;
307163 }
307164 for (auto &child : op.children) {
307165 if (EndsInAggregateOrDistinct(*child)) {
307166 return true;
307167 }
307168 }
307169 return false;
307170}

◆ TryInlining()

void duckdb::CTEInlining::TryInlining ( unique_ptr< LogicalOperator > & op)
private
307172 {
307173 if (op->type == LogicalOperatorType::LOGICAL_PREPARE) {
307174 // we are in a prepare statement, if we have to copy an operator during inlining,
307175 // we have to be careful to use the correct parameter data
307176 auto &prepare = op->Cast<LogicalPrepare>();
307177 parameter_data = prepare.prepared->value_map;
307178 }
307179
307180 // traverse children first, so we can inline the deepest CTEs first
307181 for (auto &child : op->children) {
307182 TryInlining(child);
307183 }
307184
307185 if (op->type == LogicalOperatorType::LOGICAL_MATERIALIZED_CTE) {
307186 auto &cte = op->Cast<LogicalMaterializedCTE>();
307187 auto ref_count = CountCTEReferences(*op, cte.table_index);
307188 if (ref_count == 0) {
307189 // this CTE is not referenced, we can remove it
307190 op = std::move(op->children[1]);
307191 return;
307192 }
307193 if (cte.materialize == CTEMaterialize::CTE_MATERIALIZE_ALWAYS) {
307194 // This CTE is always materialized, we cannot inline it
307195 return;
307196 }
307197 if (ref_count == 1) {
307198 // this CTE is only referenced once, we can inline it directly without copying
307199 bool success = Inline(op->children[1], *op, false);
307200 if (success) {
307201 op = std::move(op->children[1]);
307202 }
307203 return;
307204 }
307205 if (ref_count > 1) {
307206 if (cte.materialize == CTEMaterialize::CTE_MATERIALIZE_NEVER) {
307207 // this CTE is referenced multiple times, but we are not allowed to materialize it
307208 // we have to inline it if possible
307209 bool success = Inline(op->children[1], *op, true);
307210 if (success) {
307211 op = std::move(op->children[1]);
307212 }
307213 return;
307214 }
307215 // check if we can inline this CTE
307216 PreventInlining prevent_inlining;
307217 prevent_inlining.VisitOperator(*op->children[0]);
307218
307219 if (prevent_inlining.prevent_inlining) {
307220 // we cannot inline this CTE, we have to keep it materialized
307221 return;
307222 }
307223
307224 // Prevent inlining if the CTE ends in an aggregate or distinct operator
307225 // This mimics the behavior of the CTE materialization in the binder
307226 if (EndsInAggregateOrDistinct(*op->children[0])) {
307227 return;
307228 }
307229
307230 // Check how many base table references the CTE has
307231 auto base_table_references = CountBaseTableReferences(*op->children[0]);
307232
307233 if (base_table_references > 2 && base_table_references * ref_count > 10) {
307234 return;
307235 }
307236
307237 // CTEs require full materialization before the CTE scans begin,
307238 // LIMIT and TOP_N operators cannot abort the materialization,
307239 // even if only a part of the CTE result is needed.
307240 // Therefore, we check if the CTE Scans are below the LIMIT or TOP_N operator
307241 // and if so, we try to inline the CTE definition.
307242 if (ContainsLimit(*op->children[1]) || op->children[0]->type == LogicalOperatorType::LOGICAL_EMPTY_RESULT) {
307243 // this CTE is referenced multiple times and has a limit, we want to inline it
307244 bool success = Inline(op->children[1], *op, true);
307245 if (success) {
307246 op = std::move(op->children[1]);
307247 }
307248 return;
307249 }
307250 }
307251 }
307252}

◆ Inline()

bool duckdb::CTEInlining::Inline ( unique_ptr< LogicalOperator > & op,
LogicalOperator & materialized_cte,
bool requires_copy = true )
private
307254 {
307255 if (op->type == LogicalOperatorType::LOGICAL_CTE_REF) {
307256 auto &cteref = op->Cast<LogicalCTERef>();
307257 auto &cte = materialized_cte.Cast<LogicalCTE>();
307258 if (cteref.cte_index == cte.table_index) {
307259 unique_ptr<LogicalOperator> &definition = cte.children[0];
307260 unique_ptr<LogicalOperator> copy;
307261 if (requires_copy) {
307262 // there are multiple references to the CTE, we need to copy it
307263 LogicalOperatorDeepCopy deep_copy(optimizer.binder, parameter_data);
307264 try {
307265 copy = deep_copy.DeepCopy(definition);
307266 } catch (NotImplementedException &ex) {
307267 // if we have to copy the lhs of a CTE, but we cannot copy the operator, we have to
307268 // stop inlining and keep the materialized CTE instead
307269 return false;
307270 }
307271 }
307272 vector<unique_ptr<Expression>> proj_expressions;
307273 definition->ResolveOperatorTypes();
307274 vector<LogicalType> types = definition->types;
307275 vector<ColumnBinding> bindings =
307276 requires_copy ? copy->GetColumnBindings() : definition->GetColumnBindings();
307277
307278 idx_t col_idx = 0;
307279 for (auto &col : bindings) {
307280 proj_expressions.push_back(make_uniq<BoundColumnRefExpression>(types[col_idx], col));
307281 col_idx++;
307282 }
307283 auto proj = make_uniq<LogicalProjection>(cteref.table_index, std::move(proj_expressions));
307284
307285 if (requires_copy) {
307286 proj->children.push_back(std::move(copy));
307287 } else {
307288 proj->children.push_back(std::move(definition));
307289 }
307290 op = std::move(proj);
307291 return true;
307292 }
307293 return true;
307294 } else {
307295 bool success = true;
307296 for (auto &child : op->children) {
307297 success &= Inline(child, materialized_cte, requires_copy);
307298 }
307299 return success;
307300 }
307301}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

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