1 | #include "duckdb/parser/query_node/select_node.hpp" |
---|---|
2 | #include "duckdb/parser/expression_util.hpp" |
3 | |
4 | using namespace duckdb; |
5 | using namespace std; |
6 | |
7 | bool SelectNode::Equals(const QueryNode *other_) const { |
8 | if (!QueryNode::Equals(other_)) { |
9 | return false; |
10 | } |
11 | if (this == other_) { |
12 | return true; |
13 | } |
14 | auto other = (SelectNode *)other_; |
15 | |
16 | // SELECT |
17 | if (!ExpressionUtil::ListEquals(select_list, other->select_list)) { |
18 | return false; |
19 | } |
20 | // FROM |
21 | if (from_table) { |
22 | // we have a FROM clause, compare to the other one |
23 | if (!from_table->Equals(other->from_table.get())) { |
24 | return false; |
25 | } |
26 | } else if (other->from_table) { |
27 | // we don't have a FROM clause, if the other statement has one they are |
28 | // not equal |
29 | return false; |
30 | } |
31 | // WHERE |
32 | if (!BaseExpression::Equals(where_clause.get(), other->where_clause.get())) { |
33 | return false; |
34 | } |
35 | // GROUP BY |
36 | if (!ExpressionUtil::ListEquals(groups, other->groups)) { |
37 | return false; |
38 | } |
39 | |
40 | // HAVING |
41 | if (!BaseExpression::Equals(having.get(), other->having.get())) { |
42 | return false; |
43 | } |
44 | return true; |
45 | } |
46 | |
47 | unique_ptr<QueryNode> SelectNode::Copy() { |
48 | auto result = make_unique<SelectNode>(); |
49 | for (auto &child : select_list) { |
50 | result->select_list.push_back(child->Copy()); |
51 | } |
52 | result->from_table = from_table ? from_table->Copy() : nullptr; |
53 | result->where_clause = where_clause ? where_clause->Copy() : nullptr; |
54 | // groups |
55 | for (auto &group : groups) { |
56 | result->groups.push_back(group->Copy()); |
57 | } |
58 | result->having = having ? having->Copy() : nullptr; |
59 | this->CopyProperties(*result); |
60 | return move(result); |
61 | } |
62 | |
63 | void SelectNode::Serialize(Serializer &serializer) { |
64 | QueryNode::Serialize(serializer); |
65 | // select_list |
66 | serializer.WriteList(select_list); |
67 | // from clause |
68 | serializer.WriteOptional(from_table); |
69 | // where_clause |
70 | serializer.WriteOptional(where_clause); |
71 | // group by / having |
72 | serializer.WriteList(groups); |
73 | serializer.WriteOptional(having); |
74 | } |
75 | |
76 | unique_ptr<QueryNode> SelectNode::Deserialize(Deserializer &source) { |
77 | auto result = make_unique<SelectNode>(); |
78 | // select_list |
79 | source.ReadList<ParsedExpression>(result->select_list); |
80 | // from clause |
81 | result->from_table = source.ReadOptional<TableRef>(); |
82 | // where_clause |
83 | result->where_clause = source.ReadOptional<ParsedExpression>(); |
84 | // group by / having |
85 | source.ReadList<ParsedExpression>(result->groups); |
86 | result->having = source.ReadOptional<ParsedExpression>(); |
87 | return move(result); |
88 | } |
89 |