//===----------------------------------------------------------------------===// // DuckDB // // duckdb/parser/query_node.hpp // // //===----------------------------------------------------------------------===// #pragma once #include "duckdb/common/common.hpp" #include "duckdb/common/serializer.hpp" #include "duckdb/parser/parsed_expression.hpp" #include "duckdb/parser/result_modifier.hpp" #include "duckdb/parser/common_table_expression_info.hpp" #include "duckdb/common/case_insensitive_map.hpp" #include "duckdb/common/exception.hpp" namespace duckdb { class FormatDeserializer; class FormatSerializer; enum class QueryNodeType : uint8_t { SELECT_NODE = 1, SET_OPERATION_NODE = 2, BOUND_SUBQUERY_NODE = 3, RECURSIVE_CTE_NODE = 4 }; struct CommonTableExpressionInfo; class CommonTableExpressionMap { public: CommonTableExpressionMap(); case_insensitive_map_t> map; public: string ToString() const; CommonTableExpressionMap Copy() const; void FormatSerialize(FormatSerializer &serializer) const; // static void FormatDeserialize(FormatDeserializer &deserializer, CommonTableExpressionMap &ret); static CommonTableExpressionMap FormatDeserialize(FormatDeserializer &deserializer); }; class QueryNode { public: explicit QueryNode(QueryNodeType type) : type(type) { } virtual ~QueryNode() { } //! The type of the query node, either SetOperation or Select QueryNodeType type; //! The set of result modifiers associated with this query node vector> modifiers; //! CTEs (used by SelectNode and SetOperationNode) CommonTableExpressionMap cte_map; virtual const vector> &GetSelectList() const = 0; public: //! Convert the query node to a string virtual string ToString() const = 0; virtual bool Equals(const QueryNode *other) const; //! Create a copy of this QueryNode virtual unique_ptr Copy() const = 0; //! Serializes a QueryNode to a stand-alone binary blob DUCKDB_API void Serialize(Serializer &serializer) const; //! Serializes a QueryNode to a stand-alone binary blob DUCKDB_API virtual void Serialize(FieldWriter &writer) const = 0; //! Deserializes a blob back into a QueryNode DUCKDB_API static unique_ptr Deserialize(Deserializer &source); string ResultModifiersToString() const; //! Adds a distinct modifier to the query node void AddDistinct(); virtual void FormatSerialize(FormatSerializer &serializer) const; static unique_ptr FormatDeserialize(FormatDeserializer &deserializer); protected: //! Copy base QueryNode properties from another expression to this one, //! used in Copy method void CopyProperties(QueryNode &other) const; public: template TARGET &Cast() { if (type != TARGET::TYPE) { throw InternalException("Failed to cast query node to type - query node type mismatch"); } return reinterpret_cast(*this); } template const TARGET &Cast() const { if (type != TARGET::TYPE) { throw InternalException("Failed to cast query node to type - query node type mismatch"); } return reinterpret_cast(*this); } }; } // namespace duckdb