1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/parser/query_node.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/common/common.hpp"
12#include "duckdb/common/serializer.hpp"
13#include "duckdb/parser/parsed_expression.hpp"
14#include "duckdb/parser/result_modifier.hpp"
15#include "duckdb/parser/common_table_expression_info.hpp"
16#include "duckdb/common/case_insensitive_map.hpp"
17#include "duckdb/common/exception.hpp"
18
19namespace duckdb {
20
21class FormatDeserializer;
22class FormatSerializer;
23
24enum class QueryNodeType : uint8_t {
25 SELECT_NODE = 1,
26 SET_OPERATION_NODE = 2,
27 BOUND_SUBQUERY_NODE = 3,
28 RECURSIVE_CTE_NODE = 4
29};
30
31struct CommonTableExpressionInfo;
32
33class CommonTableExpressionMap {
34public:
35 CommonTableExpressionMap();
36
37 case_insensitive_map_t<unique_ptr<CommonTableExpressionInfo>> map;
38
39public:
40 string ToString() const;
41 CommonTableExpressionMap Copy() const;
42
43 void FormatSerialize(FormatSerializer &serializer) const;
44 // static void FormatDeserialize(FormatDeserializer &deserializer, CommonTableExpressionMap &ret);
45 static CommonTableExpressionMap FormatDeserialize(FormatDeserializer &deserializer);
46};
47
48class QueryNode {
49public:
50 explicit QueryNode(QueryNodeType type) : type(type) {
51 }
52 virtual ~QueryNode() {
53 }
54
55 //! The type of the query node, either SetOperation or Select
56 QueryNodeType type;
57 //! The set of result modifiers associated with this query node
58 vector<unique_ptr<ResultModifier>> modifiers;
59 //! CTEs (used by SelectNode and SetOperationNode)
60 CommonTableExpressionMap cte_map;
61
62 virtual const vector<unique_ptr<ParsedExpression>> &GetSelectList() const = 0;
63
64public:
65 //! Convert the query node to a string
66 virtual string ToString() const = 0;
67
68 virtual bool Equals(const QueryNode *other) const;
69
70 //! Create a copy of this QueryNode
71 virtual unique_ptr<QueryNode> Copy() const = 0;
72 //! Serializes a QueryNode to a stand-alone binary blob
73 DUCKDB_API void Serialize(Serializer &serializer) const;
74 //! Serializes a QueryNode to a stand-alone binary blob
75 DUCKDB_API virtual void Serialize(FieldWriter &writer) const = 0;
76 //! Deserializes a blob back into a QueryNode
77 DUCKDB_API static unique_ptr<QueryNode> Deserialize(Deserializer &source);
78
79 string ResultModifiersToString() const;
80
81 //! Adds a distinct modifier to the query node
82 void AddDistinct();
83
84 virtual void FormatSerialize(FormatSerializer &serializer) const;
85 static unique_ptr<QueryNode> FormatDeserialize(FormatDeserializer &deserializer);
86
87protected:
88 //! Copy base QueryNode properties from another expression to this one,
89 //! used in Copy method
90 void CopyProperties(QueryNode &other) const;
91
92public:
93 template <class TARGET>
94 TARGET &Cast() {
95 if (type != TARGET::TYPE) {
96 throw InternalException("Failed to cast query node to type - query node type mismatch");
97 }
98 return reinterpret_cast<TARGET &>(*this);
99 }
100
101 template <class TARGET>
102 const TARGET &Cast() const {
103 if (type != TARGET::TYPE) {
104 throw InternalException("Failed to cast query node to type - query node type mismatch");
105 }
106 return reinterpret_cast<const TARGET &>(*this);
107 }
108};
109
110} // namespace duckdb
111