1#include "duckdb/parser/query_node/recursive_cte_node.hpp"
2#include "duckdb/common/field_writer.hpp"
3#include "duckdb/common/serializer/format_serializer.hpp"
4#include "duckdb/common/serializer/format_deserializer.hpp"
5
6namespace duckdb {
7
8string RecursiveCTENode::ToString() const {
9 string result;
10 result += "(" + left->ToString() + ")";
11 result += " UNION ";
12 if (union_all) {
13 result += " ALL ";
14 }
15 result += "(" + right->ToString() + ")";
16 return result;
17}
18
19bool RecursiveCTENode::Equals(const QueryNode *other_p) const {
20 if (!QueryNode::Equals(other: other_p)) {
21 return false;
22 }
23 if (this == other_p) {
24 return true;
25 }
26 auto &other = other_p->Cast<RecursiveCTENode>();
27
28 if (other.union_all != union_all) {
29 return false;
30 }
31 if (!left->Equals(other: other.left.get())) {
32 return false;
33 }
34 if (!right->Equals(other: other.right.get())) {
35 return false;
36 }
37 return true;
38}
39
40unique_ptr<QueryNode> RecursiveCTENode::Copy() const {
41 auto result = make_uniq<RecursiveCTENode>();
42 result->ctename = ctename;
43 result->union_all = union_all;
44 result->left = left->Copy();
45 result->right = right->Copy();
46 result->aliases = aliases;
47 this->CopyProperties(other&: *result);
48 return std::move(result);
49}
50
51void RecursiveCTENode::Serialize(FieldWriter &writer) const {
52 writer.WriteString(val: ctename);
53 writer.WriteField<bool>(element: union_all);
54 writer.WriteSerializable(element: *left);
55 writer.WriteSerializable(element: *right);
56 writer.WriteList<string>(elements: aliases);
57}
58
59unique_ptr<QueryNode> RecursiveCTENode::Deserialize(FieldReader &reader) {
60 auto result = make_uniq<RecursiveCTENode>();
61 result->ctename = reader.ReadRequired<string>();
62 result->union_all = reader.ReadRequired<bool>();
63 result->left = reader.ReadRequiredSerializable<QueryNode>();
64 result->right = reader.ReadRequiredSerializable<QueryNode>();
65 result->aliases = reader.ReadRequiredList<string>();
66 return std::move(result);
67}
68
69void RecursiveCTENode::FormatSerialize(FormatSerializer &serializer) const {
70 QueryNode::FormatSerialize(serializer);
71 serializer.WriteProperty(tag: "cte_name", value: ctename);
72 serializer.WriteProperty(tag: "union_all", value: union_all);
73 serializer.WriteProperty(tag: "left", value&: *left);
74 serializer.WriteProperty(tag: "right", value&: *right);
75 serializer.WriteProperty(tag: "aliases", value: aliases);
76}
77
78unique_ptr<QueryNode> RecursiveCTENode::FormatDeserialize(FormatDeserializer &deserializer) {
79 auto result = make_uniq<RecursiveCTENode>();
80 deserializer.ReadProperty(tag: "cte_name", ret&: result->ctename);
81 deserializer.ReadProperty(tag: "union_all", ret&: result->union_all);
82 deserializer.ReadProperty(tag: "left", ret&: result->left);
83 deserializer.ReadProperty(tag: "right", ret&: result->right);
84 deserializer.ReadProperty(tag: "aliases", ret&: result->aliases);
85 return std::move(result);
86}
87
88} // namespace duckdb
89