1#include "duckdb/common/types/column/column_data_collection.hpp"
2#include "duckdb/execution/operator/scan/physical_column_data_scan.hpp"
3#include "duckdb/execution/operator/scan/physical_expression_scan.hpp"
4#include "duckdb/execution/physical_plan_generator.hpp"
5#include "duckdb/planner/operator/logical_expression_get.hpp"
6
7namespace duckdb {
8
9unique_ptr<PhysicalOperator> PhysicalPlanGenerator::CreatePlan(LogicalExpressionGet &op) {
10 D_ASSERT(op.children.size() == 1);
11 auto plan = CreatePlan(op&: *op.children[0]);
12
13 auto expr_scan = make_uniq<PhysicalExpressionScan>(args&: op.types, args: std::move(op.expressions), args&: op.estimated_cardinality);
14 expr_scan->children.push_back(x: std::move(plan));
15 if (!expr_scan->IsFoldable()) {
16 return std::move(expr_scan);
17 }
18 auto &allocator = Allocator::Get(context);
19 // simple expression scan (i.e. no subqueries to evaluate and no prepared statement parameters)
20 // we can evaluate all the expressions right now and turn this into a chunk collection scan
21 auto chunk_scan = make_uniq<PhysicalColumnDataScan>(args&: op.types, args: PhysicalOperatorType::COLUMN_DATA_SCAN,
22 args: expr_scan->expressions.size());
23 chunk_scan->owned_collection = make_uniq<ColumnDataCollection>(args&: context, args&: op.types);
24 chunk_scan->collection = chunk_scan->owned_collection.get();
25
26 DataChunk chunk;
27 chunk.Initialize(allocator, types: op.types);
28
29 ColumnDataAppendState append_state;
30 chunk_scan->owned_collection->InitializeAppend(state&: append_state);
31 for (idx_t expression_idx = 0; expression_idx < expr_scan->expressions.size(); expression_idx++) {
32 chunk.Reset();
33 expr_scan->EvaluateExpression(context, expression_idx, child_chunk: nullptr, result&: chunk);
34 chunk_scan->owned_collection->Append(state&: append_state, new_chunk&: chunk);
35 }
36 return std::move(chunk_scan);
37}
38
39} // namespace duckdb
40