//===----------------------------------------------------------------------===// // DuckDB // // duckdb/planner/logical_operator.hpp // // //===----------------------------------------------------------------------===// #pragma once #include "duckdb/catalog/catalog.hpp" #include "duckdb/common/common.hpp" #include "duckdb/common/enums/logical_operator_type.hpp" #include "duckdb/optimizer/join_order/estimated_properties.hpp" #include "duckdb/planner/column_binding.hpp" #include "duckdb/planner/expression.hpp" #include "duckdb/planner/logical_operator_visitor.hpp" #include "duckdb/planner/plan_serialization.hpp" #include #include namespace duckdb { class FieldWriter; class FieldReader; //! The current version of the plan serialization format. Exposed via by @Serializer & @Deserializer //! to be used by various Operator to know what format to read and write. extern const uint64_t PLAN_SERIALIZATION_VERSION; //! LogicalOperator is the base class of the logical operators present in the //! logical query tree class LogicalOperator { public: explicit LogicalOperator(LogicalOperatorType type); LogicalOperator(LogicalOperatorType type, vector> expressions); virtual ~LogicalOperator(); //! The type of the logical operator LogicalOperatorType type; //! The set of children of the operator vector> children; //! The set of expressions contained within the operator, if any vector> expressions; //! The types returned by this logical operator. Set by calling LogicalOperator::ResolveTypes. vector types; //! Estimated Cardinality idx_t estimated_cardinality; bool has_estimated_cardinality; unique_ptr estimated_props; public: virtual vector GetColumnBindings(); static vector GenerateColumnBindings(idx_t table_idx, idx_t column_count); static vector MapTypes(const vector &types, const vector &projection_map); static vector MapBindings(const vector &types, const vector &projection_map); //! Resolve the types of the logical operator and its children void ResolveOperatorTypes(); virtual string GetName() const; virtual string ParamsToString() const; virtual string ToString() const; DUCKDB_API void Print(); //! Debug method: verify that the integrity of expressions & child nodes are maintained virtual void Verify(ClientContext &context); void AddChild(unique_ptr child); virtual idx_t EstimateCardinality(ClientContext &context); //! Serializes a LogicalOperator to a stand-alone binary blob void Serialize(Serializer &serializer) const; //! Serializes an LogicalOperator to a stand-alone binary blob virtual void Serialize(FieldWriter &writer) const = 0; static unique_ptr Deserialize(Deserializer &deserializer, PlanDeserializationState &state); virtual unique_ptr Copy(ClientContext &context) const; virtual bool RequireOptimizer() const { return true; } //! Allows LogicalOperators to opt out of serialization virtual bool SupportSerialization() const { return true; }; //! Returns the set of table indexes of this operator virtual vector GetTableIndex() const; protected: //! Resolve types for this specific operator virtual void ResolveTypes() = 0; public: template TARGET &Cast() { if (TARGET::TYPE != LogicalOperatorType::LOGICAL_INVALID && type != TARGET::TYPE) { throw InternalException("Failed to cast logical operator to type - logical operator type mismatch"); } return reinterpret_cast(*this); } template const TARGET &Cast() const { if (TARGET::TYPE != LogicalOperatorType::LOGICAL_INVALID && type != TARGET::TYPE) { throw InternalException("Failed to cast logical operator to type - logical operator type mismatch"); } return reinterpret_cast(*this); } }; } // namespace duckdb