1#include "duckdb/execution/operator/helper/physical_reservoir_sample.hpp"
2#include "duckdb/execution/operator/helper/physical_streaming_sample.hpp"
3#include "duckdb/execution/physical_plan_generator.hpp"
4#include "duckdb/planner/operator/logical_sample.hpp"
5#include "duckdb/common/enum_util.hpp"
6
7namespace duckdb {
8
9unique_ptr<PhysicalOperator> PhysicalPlanGenerator::CreatePlan(LogicalSample &op) {
10 D_ASSERT(op.children.size() == 1);
11
12 auto plan = CreatePlan(op&: *op.children[0]);
13
14 unique_ptr<PhysicalOperator> sample;
15 switch (op.sample_options->method) {
16 case SampleMethod::RESERVOIR_SAMPLE:
17 sample = make_uniq<PhysicalReservoirSample>(args&: op.types, args: std::move(op.sample_options), args&: op.estimated_cardinality);
18 break;
19 case SampleMethod::SYSTEM_SAMPLE:
20 case SampleMethod::BERNOULLI_SAMPLE:
21 if (!op.sample_options->is_percentage) {
22 throw ParserException("Sample method %s cannot be used with a discrete sample count, either switch to "
23 "reservoir sampling or use a sample_size",
24 EnumUtil::ToString(value: op.sample_options->method));
25 }
26 sample = make_uniq<PhysicalStreamingSample>(args&: op.types, args&: op.sample_options->method,
27 args: op.sample_options->sample_size.GetValue<double>(),
28 args&: op.sample_options->seed, args&: op.estimated_cardinality);
29 break;
30 default:
31 throw InternalException("Unimplemented sample method");
32 }
33 sample->children.push_back(x: std::move(plan));
34 return sample;
35}
36
37} // namespace duckdb
38