1#include "duckdb/parser/expression/conjunction_expression.hpp"
2#include "duckdb/common/exception.hpp"
3#include "duckdb/common/serializer.hpp"
4#include "duckdb/parser/expression_util.hpp"
5
6using namespace duckdb;
7using namespace std;
8
9ConjunctionExpression::ConjunctionExpression(ExpressionType type)
10 : ParsedExpression(type, ExpressionClass::CONJUNCTION) {
11}
12
13ConjunctionExpression::ConjunctionExpression(ExpressionType type, vector<unique_ptr<ParsedExpression>> children)
14 : ParsedExpression(type, ExpressionClass::CONJUNCTION) {
15 for (auto &child : children) {
16 AddExpression(move(child));
17 }
18}
19
20ConjunctionExpression::ConjunctionExpression(ExpressionType type, unique_ptr<ParsedExpression> left,
21 unique_ptr<ParsedExpression> right)
22 : ParsedExpression(type, ExpressionClass::CONJUNCTION) {
23 AddExpression(move(left));
24 AddExpression(move(right));
25}
26
27void ConjunctionExpression::AddExpression(unique_ptr<ParsedExpression> expr) {
28 if (expr->type == type) {
29 // expr is a conjunction of the same type: merge the expression lists together
30 auto &other = (ConjunctionExpression &)*expr;
31 for (auto &child : other.children) {
32 children.push_back(move(child));
33 }
34 } else {
35 children.push_back(move(expr));
36 }
37}
38
39string ConjunctionExpression::ToString() const {
40 string result = children[0]->ToString();
41 for (idx_t i = 1; i < children.size(); i++) {
42 result += " " + ExpressionTypeToOperator(type) + " " + children[i]->ToString();
43 }
44 return result;
45}
46
47bool ConjunctionExpression::Equals(const ConjunctionExpression *a, const ConjunctionExpression *b) {
48 return ExpressionUtil::SetEquals(a->children, b->children);
49}
50
51unique_ptr<ParsedExpression> ConjunctionExpression::Copy() const {
52 auto copy = make_unique<ConjunctionExpression>(type);
53 for (auto &expr : children) {
54 copy->children.push_back(expr->Copy());
55 }
56 copy->CopyProperties(*this);
57 return move(copy);
58}
59
60void ConjunctionExpression::Serialize(Serializer &serializer) {
61 ParsedExpression::Serialize(serializer);
62 serializer.WriteList<ParsedExpression>(children);
63}
64
65unique_ptr<ParsedExpression> ConjunctionExpression::Deserialize(ExpressionType type, Deserializer &source) {
66 auto result = make_unique<ConjunctionExpression>(type);
67 source.ReadList<ParsedExpression>(result->children);
68 return move(result);
69}
70