//===----------------------------------------------------------------------===// // DuckDB // // duckdb/planner/expression/bound_cast_expression.hpp // // //===----------------------------------------------------------------------===// #pragma once #include "duckdb/planner/expression.hpp" #include "duckdb/function/cast/default_casts.hpp" namespace duckdb { class BoundCastExpression : public Expression { public: static constexpr const ExpressionClass TYPE = ExpressionClass::BOUND_CAST; public: BoundCastExpression(unique_ptr child, LogicalType target_type, BoundCastInfo bound_cast, bool try_cast = false); //! The child type unique_ptr child; //! Whether to use try_cast or not. try_cast converts cast failures into NULLs instead of throwing an error. bool try_cast; //! The bound cast info BoundCastInfo bound_cast; public: LogicalType source_type() { D_ASSERT(child->return_type.IsValid()); return child->return_type; } //! Cast an expression to the specified SQL type, using only the built-in SQL casts static unique_ptr AddDefaultCastToType(unique_ptr expr, const LogicalType &target_type, bool try_cast = false); //! Cast an expression to the specified SQL type if required DUCKDB_API static unique_ptr AddCastToType(ClientContext &context, unique_ptr expr, const LogicalType &target_type, bool try_cast = false); //! Returns true if a cast is invertible (i.e. CAST(s -> t -> s) = s for all values of s). This is not true for e.g. //! boolean casts, because that can be e.g. -1 -> TRUE -> 1. This is necessary to prevent some optimizer bugs. static bool CastIsInvertible(const LogicalType &source_type, const LogicalType &target_type); string ToString() const override; bool Equals(const BaseExpression &other) const override; unique_ptr Copy() override; void Serialize(FieldWriter &writer) const override; static unique_ptr Deserialize(ExpressionDeserializationState &state, FieldReader &reader); }; } // namespace duckdb