1 | #include "duckdb/parser/expression/case_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 | |
9 | namespace duckdb { |
10 | |
11 | void CaseCheck::FormatSerialize(FormatSerializer &serializer) const { |
12 | serializer.WriteProperty(tag: "when_expr" , value: when_expr); |
13 | serializer.WriteProperty(tag: "then_expr" , value: then_expr); |
14 | } |
15 | |
16 | CaseCheck CaseCheck::FormatDeserialize(FormatDeserializer &deserializer) { |
17 | CaseCheck check; |
18 | deserializer.ReadProperty(tag: "when_expr" , ret&: check.when_expr); |
19 | deserializer.ReadProperty(tag: "then_expr" , ret&: check.then_expr); |
20 | return check; |
21 | } |
22 | |
23 | CaseExpression::CaseExpression() : ParsedExpression(ExpressionType::CASE_EXPR, ExpressionClass::CASE) { |
24 | } |
25 | |
26 | string CaseExpression::ToString() const { |
27 | return ToString<CaseExpression, ParsedExpression>(entry: *this); |
28 | } |
29 | |
30 | bool CaseExpression::Equal(const CaseExpression &a, const CaseExpression &b) { |
31 | if (a.case_checks.size() != b.case_checks.size()) { |
32 | return false; |
33 | } |
34 | for (idx_t i = 0; i < a.case_checks.size(); i++) { |
35 | if (!a.case_checks[i].when_expr->Equals(other: *b.case_checks[i].when_expr)) { |
36 | return false; |
37 | } |
38 | if (!a.case_checks[i].then_expr->Equals(other: *b.case_checks[i].then_expr)) { |
39 | return false; |
40 | } |
41 | } |
42 | if (!a.else_expr->Equals(other: *b.else_expr)) { |
43 | return false; |
44 | } |
45 | return true; |
46 | } |
47 | |
48 | unique_ptr<ParsedExpression> CaseExpression::Copy() const { |
49 | auto copy = make_uniq<CaseExpression>(); |
50 | copy->CopyProperties(other: *this); |
51 | for (auto &check : case_checks) { |
52 | CaseCheck new_check; |
53 | new_check.when_expr = check.when_expr->Copy(); |
54 | new_check.then_expr = check.then_expr->Copy(); |
55 | copy->case_checks.push_back(x: std::move(new_check)); |
56 | } |
57 | copy->else_expr = else_expr->Copy(); |
58 | return std::move(copy); |
59 | } |
60 | |
61 | void CaseExpression::Serialize(FieldWriter &writer) const { |
62 | auto &serializer = writer.GetSerializer(); |
63 | // we write a list of multiple expressions here |
64 | // in order to write this as a single field we directly use the field writers' internal serializer |
65 | writer.WriteField<uint32_t>(element: case_checks.size()); |
66 | for (auto &check : case_checks) { |
67 | check.when_expr->Serialize(serializer); |
68 | check.then_expr->Serialize(serializer); |
69 | } |
70 | writer.WriteSerializable<ParsedExpression>(element: *else_expr); |
71 | } |
72 | |
73 | unique_ptr<ParsedExpression> CaseExpression::Deserialize(ExpressionType type, FieldReader &reader) { |
74 | auto result = make_uniq<CaseExpression>(); |
75 | auto &source = reader.GetSource(); |
76 | auto count = reader.ReadRequired<uint32_t>(); |
77 | for (idx_t i = 0; i < count; i++) { |
78 | CaseCheck new_check; |
79 | new_check.when_expr = ParsedExpression::Deserialize(source); |
80 | new_check.then_expr = ParsedExpression::Deserialize(source); |
81 | result->case_checks.push_back(x: std::move(new_check)); |
82 | } |
83 | result->else_expr = reader.ReadRequiredSerializable<ParsedExpression>(); |
84 | return std::move(result); |
85 | } |
86 | |
87 | void CaseExpression::FormatSerialize(FormatSerializer &serializer) const { |
88 | ParsedExpression::FormatSerialize(serializer); |
89 | serializer.WriteProperty(tag: "case_checks" , value: case_checks); |
90 | serializer.WriteProperty(tag: "else_expr" , value&: *else_expr); |
91 | } |
92 | |
93 | unique_ptr<ParsedExpression> CaseExpression::FormatDeserialize(ExpressionType type, FormatDeserializer &deserializer) { |
94 | auto result = make_uniq<CaseExpression>(); |
95 | deserializer.ReadProperty(tag: "case_checks" , ret&: result->case_checks); |
96 | deserializer.ReadProperty(tag: "else_expr" , ret&: result->else_expr); |
97 | return std::move(result); |
98 | } |
99 | |
100 | } // namespace duckdb |
101 | |