1#include "duckdb/common/field_writer.hpp"
2#include "duckdb/planner/operator/logical_limit_percent.hpp"
3#include <cmath>
4
5namespace duckdb {
6
7void LogicalLimitPercent::Serialize(FieldWriter &writer) const {
8 writer.WriteField(element: limit_percent);
9 writer.WriteField(element: offset_val);
10 writer.WriteOptional(element: limit);
11 writer.WriteOptional(element: offset);
12}
13
14unique_ptr<LogicalOperator> LogicalLimitPercent::Deserialize(LogicalDeserializationState &state, FieldReader &reader) {
15 auto limit_percent = reader.ReadRequired<double>();
16 auto offset_val = reader.ReadRequired<int64_t>();
17 auto limit = reader.ReadOptional<Expression>(default_value: nullptr, args&: state.gstate);
18 auto offset = reader.ReadOptional<Expression>(default_value: nullptr, args&: state.gstate);
19 return make_uniq<LogicalLimitPercent>(args&: limit_percent, args&: offset_val, args: std::move(limit), args: std::move(offset));
20}
21
22idx_t LogicalLimitPercent::EstimateCardinality(ClientContext &context) {
23 auto child_cardinality = LogicalOperator::EstimateCardinality(context);
24 if ((limit_percent < 0 || limit_percent > 100) || std::isnan(x: limit_percent)) {
25 return child_cardinality;
26 }
27 return idx_t(child_cardinality * (limit_percent / 100.0));
28}
29
30} // namespace duckdb
31