1#include "duckdb/parser/result_modifier.hpp"
2#include "duckdb/common/field_writer.hpp"
3#include "duckdb/parser/expression_util.hpp"
4#include "duckdb/common/serializer/format_serializer.hpp"
5#include "duckdb/common/serializer/format_deserializer.hpp"
6
7namespace duckdb {
8
9bool ResultModifier::Equals(const ResultModifier &other) const {
10 return type == other.type;
11}
12
13void ResultModifier::Serialize(Serializer &serializer) const {
14 FieldWriter writer(serializer);
15 writer.WriteField<ResultModifierType>(element: type);
16 Serialize(writer);
17 writer.Finalize();
18}
19
20void ResultModifier::FormatSerialize(FormatSerializer &serializer) const {
21 serializer.WriteProperty(tag: "type", value: type);
22}
23
24unique_ptr<ResultModifier> ResultModifier::FormatDeserialize(FormatDeserializer &deserializer) {
25 auto type = deserializer.ReadProperty<ResultModifierType>(tag: "type");
26
27 unique_ptr<ResultModifier> result;
28 switch (type) {
29 case ResultModifierType::LIMIT_MODIFIER:
30 result = LimitModifier::FormatDeserialize(deserializer);
31 break;
32 case ResultModifierType::ORDER_MODIFIER:
33 result = OrderModifier::FormatDeserialize(deserializer);
34 break;
35 case ResultModifierType::DISTINCT_MODIFIER:
36 result = DistinctModifier::FormatDeserialize(deserializer);
37 break;
38 case ResultModifierType::LIMIT_PERCENT_MODIFIER:
39 result = LimitPercentModifier::FormatDeserialize(deserializer);
40 break;
41 default:
42 throw InternalException("Unrecognized ResultModifierType for Deserialization");
43 }
44 return result;
45}
46
47unique_ptr<ResultModifier> ResultModifier::Deserialize(Deserializer &source) {
48 FieldReader reader(source);
49 auto type = reader.ReadRequired<ResultModifierType>();
50
51 unique_ptr<ResultModifier> result;
52 switch (type) {
53 case ResultModifierType::LIMIT_MODIFIER:
54 result = LimitModifier::Deserialize(reader);
55 break;
56 case ResultModifierType::ORDER_MODIFIER:
57 result = OrderModifier::Deserialize(reader);
58 break;
59 case ResultModifierType::DISTINCT_MODIFIER:
60 result = DistinctModifier::Deserialize(reader);
61 break;
62 case ResultModifierType::LIMIT_PERCENT_MODIFIER:
63 result = LimitPercentModifier::Deserialize(reader);
64 break;
65 default:
66 throw InternalException("Unrecognized ResultModifierType for Deserialization");
67 }
68 reader.Finalize();
69 return result;
70}
71
72bool LimitModifier::Equals(const ResultModifier &other_p) const {
73 if (!ResultModifier::Equals(other: other_p)) {
74 return false;
75 }
76 auto &other = other_p.Cast<LimitModifier>();
77 if (!ParsedExpression::Equals(left: limit, right: other.limit)) {
78 return false;
79 }
80 if (!ParsedExpression::Equals(left: offset, right: other.offset)) {
81 return false;
82 }
83 return true;
84}
85
86unique_ptr<ResultModifier> LimitModifier::Copy() const {
87 auto copy = make_uniq<LimitModifier>();
88 if (limit) {
89 copy->limit = limit->Copy();
90 }
91 if (offset) {
92 copy->offset = offset->Copy();
93 }
94 return std::move(copy);
95}
96
97void LimitModifier::Serialize(FieldWriter &writer) const {
98 writer.WriteOptional(element: limit);
99 writer.WriteOptional(element: offset);
100}
101
102void LimitModifier::FormatSerialize(FormatSerializer &serializer) const {
103 ResultModifier::FormatSerialize(serializer);
104 serializer.WriteOptionalProperty(tag: "limit", ptr: limit);
105 serializer.WriteOptionalProperty(tag: "offset", ptr: offset);
106}
107
108unique_ptr<ResultModifier> LimitModifier::FormatDeserialize(FormatDeserializer &deserializer) {
109 auto mod = make_uniq<LimitModifier>();
110 deserializer.ReadOptionalProperty(tag: "limit", ret&: mod->limit);
111 deserializer.ReadOptionalProperty(tag: "offset", ret&: mod->offset);
112 return std::move(mod);
113}
114
115unique_ptr<ResultModifier> LimitModifier::Deserialize(FieldReader &reader) {
116 auto mod = make_uniq<LimitModifier>();
117 mod->limit = reader.ReadOptional<ParsedExpression>(default_value: nullptr);
118 mod->offset = reader.ReadOptional<ParsedExpression>(default_value: nullptr);
119 return std::move(mod);
120}
121
122bool DistinctModifier::Equals(const ResultModifier &other_p) const {
123 if (!ResultModifier::Equals(other: other_p)) {
124 return false;
125 }
126 auto &other = other_p.Cast<DistinctModifier>();
127 if (!ExpressionUtil::ListEquals(a: distinct_on_targets, b: other.distinct_on_targets)) {
128 return false;
129 }
130 return true;
131}
132
133unique_ptr<ResultModifier> DistinctModifier::Copy() const {
134 auto copy = make_uniq<DistinctModifier>();
135 for (auto &expr : distinct_on_targets) {
136 copy->distinct_on_targets.push_back(x: expr->Copy());
137 }
138 return std::move(copy);
139}
140
141void DistinctModifier::Serialize(FieldWriter &writer) const {
142 writer.WriteSerializableList(elements: distinct_on_targets);
143}
144
145void DistinctModifier::FormatSerialize(duckdb::FormatSerializer &serializer) const {
146 ResultModifier::FormatSerialize(serializer);
147 serializer.WriteProperty(tag: "distinct_on_targets", value: distinct_on_targets);
148}
149
150unique_ptr<ResultModifier> DistinctModifier::FormatDeserialize(FormatDeserializer &deserializer) {
151 auto mod = make_uniq<DistinctModifier>();
152 deserializer.ReadProperty(tag: "distinct_on_targets", ret&: mod->distinct_on_targets);
153 return std::move(mod);
154}
155
156unique_ptr<ResultModifier> DistinctModifier::Deserialize(FieldReader &reader) {
157 auto mod = make_uniq<DistinctModifier>();
158 mod->distinct_on_targets = reader.ReadRequiredSerializableList<ParsedExpression>();
159 return std::move(mod);
160}
161
162bool OrderModifier::Equals(const ResultModifier &other_p) const {
163 if (!ResultModifier::Equals(other: other_p)) {
164 return false;
165 }
166 auto &other = other_p.Cast<OrderModifier>();
167 if (orders.size() != other.orders.size()) {
168 return false;
169 }
170 for (idx_t i = 0; i < orders.size(); i++) {
171 if (orders[i].type != other.orders[i].type) {
172 return false;
173 }
174 if (!BaseExpression::Equals(left: *orders[i].expression, right: *other.orders[i].expression)) {
175 return false;
176 }
177 }
178 return true;
179}
180
181bool OrderModifier::Equals(const unique_ptr<OrderModifier> &left, const unique_ptr<OrderModifier> &right) {
182 if (left.get() == right.get()) {
183 return true;
184 }
185 if (!left || !right) {
186 return false;
187 }
188 return left->Equals(other_p: *right);
189}
190
191unique_ptr<ResultModifier> OrderModifier::Copy() const {
192 auto copy = make_uniq<OrderModifier>();
193 for (auto &order : orders) {
194 copy->orders.emplace_back(args: order.type, args: order.null_order, args: order.expression->Copy());
195 }
196 return std::move(copy);
197}
198
199string OrderByNode::ToString() const {
200 auto str = expression->ToString();
201 switch (type) {
202 case OrderType::ASCENDING:
203 str += " ASC";
204 break;
205 case OrderType::DESCENDING:
206 str += " DESC";
207 break;
208 default:
209 break;
210 }
211
212 switch (null_order) {
213 case OrderByNullType::NULLS_FIRST:
214 str += " NULLS FIRST";
215 break;
216 case OrderByNullType::NULLS_LAST:
217 str += " NULLS LAST";
218 break;
219 default:
220 break;
221 }
222 return str;
223}
224
225void OrderByNode::Serialize(Serializer &serializer) const {
226 FieldWriter writer(serializer);
227 writer.WriteField<OrderType>(element: type);
228 writer.WriteField<OrderByNullType>(element: null_order);
229 writer.WriteSerializable(element: *expression);
230 writer.Finalize();
231}
232
233void OrderByNode::FormatSerialize(FormatSerializer &serializer) const {
234 serializer.WriteProperty(tag: "type", value: type);
235 serializer.WriteProperty(tag: "null_order", value: null_order);
236 serializer.WriteProperty(tag: "expression", value: expression);
237}
238
239OrderByNode OrderByNode::FormatDeserialize(FormatDeserializer &deserializer) {
240 auto type = deserializer.ReadProperty<OrderType>(tag: "type");
241 auto null_order = deserializer.ReadProperty<OrderByNullType>(tag: "null_order");
242 auto expression = deserializer.ReadProperty<unique_ptr<ParsedExpression>>(tag: "expression");
243 return OrderByNode(type, null_order, std::move(expression));
244}
245
246OrderByNode OrderByNode::Deserialize(Deserializer &source) {
247 FieldReader reader(source);
248 auto type = reader.ReadRequired<OrderType>();
249 auto null_order = reader.ReadRequired<OrderByNullType>();
250 auto expression = reader.ReadRequiredSerializable<ParsedExpression>();
251 reader.Finalize();
252 return OrderByNode(type, null_order, std::move(expression));
253}
254
255void OrderModifier::Serialize(FieldWriter &writer) const {
256 writer.WriteRegularSerializableList(elements: orders);
257}
258
259void OrderModifier::FormatSerialize(FormatSerializer &serializer) const {
260 ResultModifier::FormatSerialize(serializer);
261 serializer.WriteProperty(tag: "orders", value: orders);
262}
263unique_ptr<ResultModifier> OrderModifier::FormatDeserialize(FormatDeserializer &deserializer) {
264 auto mod = make_uniq<OrderModifier>();
265 deserializer.ReadProperty(tag: "orders", ret&: mod->orders);
266 return std::move(mod);
267}
268
269unique_ptr<ResultModifier> OrderModifier::Deserialize(FieldReader &reader) {
270 auto mod = make_uniq<OrderModifier>();
271 mod->orders = reader.ReadRequiredSerializableList<OrderByNode, OrderByNode>();
272 return std::move(mod);
273}
274
275bool LimitPercentModifier::Equals(const ResultModifier &other_p) const {
276 if (!ResultModifier::Equals(other: other_p)) {
277 return false;
278 }
279 auto &other = other_p.Cast<LimitPercentModifier>();
280 if (!ParsedExpression::Equals(left: limit, right: other.limit)) {
281 return false;
282 }
283 if (!ParsedExpression::Equals(left: offset, right: other.offset)) {
284 return false;
285 }
286 return true;
287}
288
289unique_ptr<ResultModifier> LimitPercentModifier::Copy() const {
290 auto copy = make_uniq<LimitPercentModifier>();
291 if (limit) {
292 copy->limit = limit->Copy();
293 }
294 if (offset) {
295 copy->offset = offset->Copy();
296 }
297 return std::move(copy);
298}
299
300void LimitPercentModifier::Serialize(FieldWriter &writer) const {
301 writer.WriteOptional(element: limit);
302 writer.WriteOptional(element: offset);
303}
304
305void LimitPercentModifier::FormatSerialize(FormatSerializer &serializer) const {
306 ResultModifier::FormatSerialize(serializer);
307 serializer.WriteOptionalProperty(tag: "limit", ptr: limit);
308 serializer.WriteOptionalProperty(tag: "offset", ptr: offset);
309}
310
311unique_ptr<ResultModifier> LimitPercentModifier::Deserialize(FieldReader &reader) {
312 auto mod = make_uniq<LimitPercentModifier>();
313 mod->limit = reader.ReadOptional<ParsedExpression>(default_value: nullptr);
314 mod->offset = reader.ReadOptional<ParsedExpression>(default_value: nullptr);
315 return std::move(mod);
316}
317
318unique_ptr<ResultModifier> LimitPercentModifier::FormatDeserialize(FormatDeserializer &deserializer) {
319 auto mod = make_uniq<LimitPercentModifier>();
320 deserializer.ReadOptionalProperty(tag: "limit", ret&: mod->limit);
321 deserializer.ReadOptionalProperty(tag: "offset", ret&: mod->offset);
322 return std::move(mod);
323}
324
325} // namespace duckdb
326