307172 {
307173 if (op->type == LogicalOperatorType::LOGICAL_PREPARE) {
307174
307175
307176 auto &prepare = op->Cast<LogicalPrepare>();
307177 parameter_data = prepare.prepared->value_map;
307178 }
307179
307180
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
307190 op = std::move(op->children[1]);
307191 return;
307192 }
307193 if (cte.materialize == CTEMaterialize::CTE_MATERIALIZE_ALWAYS) {
307194
307195 return;
307196 }
307197 if (ref_count == 1) {
307198
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
307208
307209 bool success = Inline(op->children[1], *op, true);
307210 if (success) {
307211 op = std::move(op->children[1]);
307212 }
307213 return;
307214 }
307215
307216 PreventInlining prevent_inlining;
307217 prevent_inlining.VisitOperator(*op->children[0]);
307218
307219 if (prevent_inlining.prevent_inlining) {
307220
307221 return;
307222 }
307223
307224
307225
307226 if (EndsInAggregateOrDistinct(*op->children[0])) {
307227 return;
307228 }
307229
307230
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
307238
307239
307240
307241
307242 if (ContainsLimit(*op->children[1]) || op->children[0]->type == LogicalOperatorType::LOGICAL_EMPTY_RESULT) {
307243
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}