1#include "duckdb/execution/operator/helper/physical_limit.hpp"
2#include "duckdb/execution/operator/helper/physical_streaming_limit.hpp"
3#include "duckdb/execution/physical_plan_generator.hpp"
4#include "duckdb/main/config.hpp"
5#include "duckdb/planner/operator/logical_limit.hpp"
6
7namespace duckdb {
8
9unique_ptr<PhysicalOperator> PhysicalPlanGenerator::CreatePlan(LogicalLimit &op) {
10 D_ASSERT(op.children.size() == 1);
11
12 auto plan = CreatePlan(op&: *op.children[0]);
13
14 unique_ptr<PhysicalOperator> limit;
15 if (!PreserveInsertionOrder(plan&: *plan)) {
16 // use parallel streaming limit if insertion order is not important
17 limit = make_uniq<PhysicalStreamingLimit>(args&: op.types, args: (idx_t)op.limit_val, args&: op.offset_val, args: std::move(op.limit),
18 args: std::move(op.offset), args&: op.estimated_cardinality, args: true);
19 } else {
20 // maintaining insertion order is important
21 if (UseBatchIndex(plan&: *plan)) {
22 // source supports batch index: use parallel batch limit
23 limit = make_uniq<PhysicalLimit>(args&: op.types, args: (idx_t)op.limit_val, args&: op.offset_val, args: std::move(op.limit),
24 args: std::move(op.offset), args&: op.estimated_cardinality);
25 } else {
26 // source does not support batch index: use a non-parallel streaming limit
27 limit = make_uniq<PhysicalStreamingLimit>(args&: op.types, args: (idx_t)op.limit_val, args&: op.offset_val, args: std::move(op.limit),
28 args: std::move(op.offset), args&: op.estimated_cardinality, args: false);
29 }
30 }
31
32 limit->children.push_back(x: std::move(plan));
33 return limit;
34}
35
36} // namespace duckdb
37