1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/parser/expression/cast_expression.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/parser/parsed_expression.hpp"
12#include "duckdb/common/types.hpp"
13
14namespace duckdb {
15
16//! CastExpression represents a type cast from one SQL type to another SQL type
17class CastExpression : public ParsedExpression {
18public:
19 static constexpr const ExpressionClass TYPE = ExpressionClass::CAST;
20
21public:
22 DUCKDB_API CastExpression(LogicalType target, unique_ptr<ParsedExpression> child, bool try_cast = false);
23
24 //! The child of the cast expression
25 unique_ptr<ParsedExpression> child;
26 //! The type to cast to
27 LogicalType cast_type;
28 //! Whether or not this is a try_cast expression
29 bool try_cast;
30
31public:
32 string ToString() const override;
33
34 static bool Equal(const CastExpression &a, const CastExpression &b);
35
36 unique_ptr<ParsedExpression> Copy() const override;
37
38 void Serialize(FieldWriter &writer) const override;
39 static unique_ptr<ParsedExpression> Deserialize(ExpressionType type, FieldReader &source);
40 void FormatSerialize(FormatSerializer &serializer) const override;
41 static unique_ptr<ParsedExpression> FormatDeserialize(ExpressionType type, FormatDeserializer &deserializer);
42
43public:
44 template <class T, class BASE>
45 static string ToString(const T &entry) {
46 return (entry.try_cast ? "TRY_CAST(" : "CAST(") + entry.child->ToString() + " AS " +
47 entry.cast_type.ToString() + ")";
48 }
49};
50} // namespace duckdb
51