1#include "duckdb/parser/expression/cast_expression.hpp"
2
3#include "duckdb/common/exception.hpp"
4#include "duckdb/common/field_writer.hpp"
5
6#include "duckdb/common/serializer/format_serializer.hpp"
7#include "duckdb/common/serializer/format_deserializer.hpp"
8
9namespace duckdb {
10
11CastExpression::CastExpression(LogicalType target, unique_ptr<ParsedExpression> child, bool try_cast_p)
12 : ParsedExpression(ExpressionType::OPERATOR_CAST, ExpressionClass::CAST), cast_type(std::move(target)),
13 try_cast(try_cast_p) {
14 D_ASSERT(child);
15 this->child = std::move(child);
16}
17
18string CastExpression::ToString() const {
19 return ToString<CastExpression, ParsedExpression>(entry: *this);
20}
21
22bool CastExpression::Equal(const CastExpression &a, const CastExpression &b) {
23 if (!a.child->Equals(other: *b.child)) {
24 return false;
25 }
26 if (a.cast_type != b.cast_type) {
27 return false;
28 }
29 if (a.try_cast != b.try_cast) {
30 return false;
31 }
32 return true;
33}
34
35unique_ptr<ParsedExpression> CastExpression::Copy() const {
36 auto copy = make_uniq<CastExpression>(args: cast_type, args: child->Copy(), args: try_cast);
37 copy->CopyProperties(other: *this);
38 return std::move(copy);
39}
40
41void CastExpression::Serialize(FieldWriter &writer) const {
42 writer.WriteSerializable(element: *child);
43 writer.WriteSerializable(element: cast_type);
44 writer.WriteField<bool>(element: try_cast);
45}
46
47unique_ptr<ParsedExpression> CastExpression::Deserialize(ExpressionType type, FieldReader &reader) {
48 auto child = reader.ReadRequiredSerializable<ParsedExpression>();
49 auto cast_type = reader.ReadRequiredSerializable<LogicalType, LogicalType>();
50 auto try_cast = reader.ReadRequired<bool>();
51 return make_uniq_base<ParsedExpression, CastExpression>(args&: cast_type, args: std::move(child), args&: try_cast);
52}
53
54void CastExpression::FormatSerialize(FormatSerializer &serializer) const {
55 ParsedExpression::FormatSerialize(serializer);
56 serializer.WriteProperty(tag: "child", value&: *child);
57 serializer.WriteProperty(tag: "cast_type", value: cast_type);
58 serializer.WriteProperty(tag: "try_cast", value: try_cast);
59}
60
61unique_ptr<ParsedExpression> CastExpression::FormatDeserialize(ExpressionType type, FormatDeserializer &deserializer) {
62 auto child = deserializer.ReadProperty<unique_ptr<ParsedExpression>>(tag: "child");
63 auto cast_type = deserializer.ReadProperty<LogicalType>(tag: "cast_type");
64 auto try_cast = deserializer.ReadProperty<bool>(tag: "try_cast");
65 return make_uniq_base<ParsedExpression, CastExpression>(args&: cast_type, args: std::move(child), args&: try_cast);
66}
67
68} // namespace duckdb
69