1#include "duckdb/parser/statement/select_statement.hpp"
2
3#include "duckdb/common/assert.hpp"
4#include "duckdb/common/serializer.hpp"
5
6using namespace duckdb;
7using namespace std;
8
9unique_ptr<SelectStatement> SelectStatement::Copy() {
10 auto result = make_unique<SelectStatement>();
11 for (auto &cte : cte_map) {
12 result->cte_map[cte.first] = cte.second->Copy();
13 }
14 result->node = node->Copy();
15 return result;
16}
17
18void SelectStatement::Serialize(Serializer &serializer) {
19 // with clauses
20 assert(cte_map.size() <= numeric_limits<uint32_t>::max());
21 serializer.Write<uint32_t>((uint32_t)cte_map.size());
22 for (auto &cte : cte_map) {
23 serializer.WriteString(cte.first);
24 cte.second->Serialize(serializer);
25 }
26 node->Serialize(serializer);
27}
28
29unique_ptr<SelectStatement> SelectStatement::Deserialize(Deserializer &source) {
30 auto result = make_unique<SelectStatement>();
31 auto cte_count = source.Read<uint32_t>();
32 for (idx_t i = 0; i < cte_count; i++) {
33 auto name = source.Read<string>();
34 auto statement = QueryNode::Deserialize(source);
35 result->cte_map[name] = move(statement);
36 }
37 result->node = QueryNode::Deserialize(source);
38 return result;
39}
40
41bool SelectStatement::Equals(const SQLStatement *other_) const {
42 if (type != other_->type) {
43 return false;
44 }
45 auto other = (SelectStatement *)other_;
46 // WITH clauses (CTEs)
47 if (cte_map.size() != other->cte_map.size()) {
48 return false;
49 }
50 for (auto &entry : cte_map) {
51 auto other_entry = other->cte_map.find(entry.first);
52 if (other_entry == other->cte_map.end()) {
53 return false;
54 }
55 if (!entry.second->Equals(other_entry->second.get())) {
56 return false;
57 }
58 }
59 return node->Equals(other->node.get());
60}
61