1 | #include "duckdb/parser/expression/conjunction_expression.hpp" |
2 | #include "duckdb/common/exception.hpp" |
3 | #include "duckdb/common/field_writer.hpp" |
4 | #include "duckdb/parser/expression_util.hpp" |
5 | |
6 | #include "duckdb/common/serializer/format_serializer.hpp" |
7 | #include "duckdb/common/serializer/format_deserializer.hpp" |
8 | |
9 | namespace duckdb { |
10 | |
11 | ConjunctionExpression::ConjunctionExpression(ExpressionType type) |
12 | : ParsedExpression(type, ExpressionClass::CONJUNCTION) { |
13 | } |
14 | |
15 | ConjunctionExpression::ConjunctionExpression(ExpressionType type, vector<unique_ptr<ParsedExpression>> children) |
16 | : ParsedExpression(type, ExpressionClass::CONJUNCTION) { |
17 | for (auto &child : children) { |
18 | AddExpression(expr: std::move(child)); |
19 | } |
20 | } |
21 | |
22 | ConjunctionExpression::ConjunctionExpression(ExpressionType type, unique_ptr<ParsedExpression> left, |
23 | unique_ptr<ParsedExpression> right) |
24 | : ParsedExpression(type, ExpressionClass::CONJUNCTION) { |
25 | AddExpression(expr: std::move(left)); |
26 | AddExpression(expr: std::move(right)); |
27 | } |
28 | |
29 | void ConjunctionExpression::AddExpression(unique_ptr<ParsedExpression> expr) { |
30 | if (expr->type == type) { |
31 | // expr is a conjunction of the same type: merge the expression lists together |
32 | auto &other = expr->Cast<ConjunctionExpression>(); |
33 | for (auto &child : other.children) { |
34 | children.push_back(x: std::move(child)); |
35 | } |
36 | } else { |
37 | children.push_back(x: std::move(expr)); |
38 | } |
39 | } |
40 | |
41 | string ConjunctionExpression::ToString() const { |
42 | return ToString<ConjunctionExpression, ParsedExpression>(entry: *this); |
43 | } |
44 | |
45 | bool ConjunctionExpression::Equal(const ConjunctionExpression &a, const ConjunctionExpression &b) { |
46 | return ExpressionUtil::SetEquals(a: a.children, b: b.children); |
47 | } |
48 | |
49 | unique_ptr<ParsedExpression> ConjunctionExpression::Copy() const { |
50 | vector<unique_ptr<ParsedExpression>> copy_children; |
51 | copy_children.reserve(n: children.size()); |
52 | for (auto &expr : children) { |
53 | copy_children.push_back(x: expr->Copy()); |
54 | } |
55 | |
56 | auto copy = make_uniq<ConjunctionExpression>(args: type, args: std::move(copy_children)); |
57 | copy->CopyProperties(other: *this); |
58 | return std::move(copy); |
59 | } |
60 | |
61 | void ConjunctionExpression::Serialize(FieldWriter &writer) const { |
62 | writer.WriteSerializableList(elements: children); |
63 | } |
64 | |
65 | unique_ptr<ParsedExpression> ConjunctionExpression::Deserialize(ExpressionType type, FieldReader &reader) { |
66 | auto result = make_uniq<ConjunctionExpression>(args&: type); |
67 | result->children = reader.ReadRequiredSerializableList<ParsedExpression>(); |
68 | return std::move(result); |
69 | } |
70 | |
71 | void ConjunctionExpression::FormatSerialize(FormatSerializer &serializer) const { |
72 | ParsedExpression::FormatSerialize(serializer); |
73 | serializer.WriteProperty(tag: "children" , value: children); |
74 | } |
75 | |
76 | unique_ptr<ParsedExpression> ConjunctionExpression::FormatDeserialize(ExpressionType type, |
77 | FormatDeserializer &deserializer) { |
78 | auto result = make_uniq<ConjunctionExpression>(args&: type); |
79 | result->children = deserializer.ReadProperty<vector<unique_ptr<ParsedExpression>>>(tag: "children" ); |
80 | return std::move(result); |
81 | } |
82 | |
83 | } // namespace duckdb |
84 | |