1#include "duckdb/planner/operator/logical_top_n.hpp"
2#include "duckdb/common/field_writer.hpp"
3
4namespace duckdb {
5
6void LogicalTopN::Serialize(FieldWriter &writer) const {
7 writer.WriteRegularSerializableList(elements: orders);
8 writer.WriteField(element: offset);
9 writer.WriteField(element: limit);
10}
11
12unique_ptr<LogicalOperator> LogicalTopN::Deserialize(LogicalDeserializationState &state, FieldReader &reader) {
13 auto orders = reader.ReadRequiredSerializableList<BoundOrderByNode, BoundOrderByNode>(args&: state.gstate);
14 auto offset = reader.ReadRequired<idx_t>();
15 auto limit = reader.ReadRequired<idx_t>();
16 return make_uniq<LogicalTopN>(args: std::move(orders), args&: limit, args&: offset);
17}
18
19idx_t LogicalTopN::EstimateCardinality(ClientContext &context) {
20 auto child_cardinality = LogicalOperator::EstimateCardinality(context);
21 if (limit >= 0 && child_cardinality < idx_t(limit)) {
22 return limit;
23 }
24 return child_cardinality;
25}
26
27} // namespace duckdb
28