1#include "duckdb/planner/bound_result_modifier.hpp"
2#include "duckdb/common/field_writer.hpp"
3
4namespace duckdb {
5
6BoundResultModifier::BoundResultModifier(ResultModifierType type) : type(type) {
7}
8
9BoundResultModifier::~BoundResultModifier() {
10}
11
12BoundOrderByNode::BoundOrderByNode(OrderType type, OrderByNullType null_order, unique_ptr<Expression> expression)
13 : type(type), null_order(null_order), expression(std::move(expression)) {
14}
15BoundOrderByNode::BoundOrderByNode(OrderType type, OrderByNullType null_order, unique_ptr<Expression> expression,
16 unique_ptr<BaseStatistics> stats)
17 : type(type), null_order(null_order), expression(std::move(expression)), stats(std::move(stats)) {
18}
19void BoundOrderModifier::Serialize(Serializer &serializer) const {
20 FieldWriter writer(serializer);
21 writer.WriteRegularSerializableList(elements: orders);
22 writer.Finalize();
23}
24unique_ptr<BoundOrderModifier> BoundOrderModifier::Deserialize(Deserializer &source, PlanDeserializationState &state) {
25 auto x = make_uniq<BoundOrderModifier>();
26
27 FieldReader reader(source);
28 x->orders = reader.ReadRequiredSerializableList<BoundOrderByNode, BoundOrderByNode>(args&: state);
29 reader.Finalize();
30
31 return x;
32}
33
34BoundOrderByNode BoundOrderByNode::Copy() const {
35 if (stats) {
36 return BoundOrderByNode(type, null_order, expression->Copy(), stats->ToUnique());
37 } else {
38 return BoundOrderByNode(type, null_order, expression->Copy());
39 }
40}
41
42bool BoundOrderByNode::Equals(const BoundOrderByNode &other) const {
43 if (type != other.type || null_order != other.null_order) {
44 return false;
45 }
46 if (!expression->Equals(other: *other.expression)) {
47 return false;
48 }
49
50 return true;
51}
52
53string BoundOrderByNode::ToString() const {
54 auto str = expression->ToString();
55 switch (type) {
56 case OrderType::ASCENDING:
57 str += " ASC";
58 break;
59 case OrderType::DESCENDING:
60 str += " DESC";
61 break;
62 default:
63 break;
64 }
65
66 switch (null_order) {
67 case OrderByNullType::NULLS_FIRST:
68 str += " NULLS FIRST";
69 break;
70 case OrderByNullType::NULLS_LAST:
71 str += " NULLS LAST";
72 break;
73 default:
74 break;
75 }
76 return str;
77}
78
79void BoundOrderByNode::Serialize(Serializer &serializer) const {
80 FieldWriter writer(serializer);
81 writer.WriteField(element: type);
82 writer.WriteField(element: null_order);
83 writer.WriteSerializable(element: *expression);
84 // TODO statistics
85 writer.Finalize();
86}
87
88BoundOrderByNode BoundOrderByNode::Deserialize(Deserializer &source, PlanDeserializationState &state) {
89 FieldReader reader(source);
90 auto type = reader.ReadRequired<OrderType>();
91 auto null_order = reader.ReadRequired<OrderByNullType>();
92 auto expression = reader.ReadRequiredSerializable<Expression>(args&: state);
93 reader.Finalize();
94 return BoundOrderByNode(type, null_order, std::move(expression));
95}
96
97unique_ptr<BoundOrderModifier> BoundOrderModifier::Copy() const {
98 auto result = make_uniq<BoundOrderModifier>();
99 for (auto &order : orders) {
100 result->orders.push_back(x: order.Copy());
101 }
102 return result;
103}
104
105bool BoundOrderModifier::Equals(const BoundOrderModifier &left, const BoundOrderModifier &right) {
106 if (left.orders.size() != right.orders.size()) {
107 return false;
108 }
109 for (idx_t i = 0; i < left.orders.size(); i++) {
110 if (!left.orders[i].Equals(other: right.orders[i])) {
111 return false;
112 }
113 }
114 return true;
115}
116
117bool BoundOrderModifier::Equals(const unique_ptr<BoundOrderModifier> &left,
118 const unique_ptr<BoundOrderModifier> &right) {
119 if (left.get() == right.get()) {
120 return true;
121 }
122 if (!left || !right) {
123 return false;
124 }
125 return BoundOrderModifier::Equals(left: *left, right: *right);
126}
127
128BoundLimitModifier::BoundLimitModifier() : BoundResultModifier(ResultModifierType::LIMIT_MODIFIER) {
129}
130
131BoundOrderModifier::BoundOrderModifier() : BoundResultModifier(ResultModifierType::ORDER_MODIFIER) {
132}
133
134BoundDistinctModifier::BoundDistinctModifier() : BoundResultModifier(ResultModifierType::DISTINCT_MODIFIER) {
135}
136
137BoundLimitPercentModifier::BoundLimitPercentModifier()
138 : BoundResultModifier(ResultModifierType::LIMIT_PERCENT_MODIFIER) {
139}
140
141} // namespace duckdb
142