1#include "duckdb/parser/expression/operator_expression.hpp"
2
3#include "duckdb/common/exception.hpp"
4#include "duckdb/common/serializer.hpp"
5#include "duckdb/common/string_util.hpp"
6
7using namespace duckdb;
8using namespace std;
9
10OperatorExpression::OperatorExpression(ExpressionType type, unique_ptr<ParsedExpression> left,
11 unique_ptr<ParsedExpression> right)
12 : ParsedExpression(type, ExpressionClass::OPERATOR) {
13 if (left) {
14 children.push_back(move(left));
15 }
16 if (right) {
17 children.push_back(move(right));
18 }
19}
20
21string OperatorExpression::ToString() const {
22 auto op = ExpressionTypeToOperator(type);
23 if (!op.empty()) {
24 // use the operator string to represent the operator
25 if (children.size() == 1) {
26 return op + children[0]->ToString();
27 } else if (children.size() == 2) {
28 return children[0]->ToString() + " " + op + " " + children[1]->ToString();
29 }
30 }
31 // if there is no operator we render it as a function
32 auto result = ExpressionTypeToString(type) + "(";
33 result += StringUtil::Join(children, children.size(), ", ",
34 [](const unique_ptr<ParsedExpression> &child) { return child->ToString(); });
35 result += ")";
36 return result;
37}
38
39bool OperatorExpression::Equals(const OperatorExpression *a, const OperatorExpression *b) {
40 if (a->children.size() != b->children.size()) {
41 return false;
42 }
43 for (idx_t i = 0; i < a->children.size(); i++) {
44 if (!a->children[i]->Equals(b->children[i].get())) {
45 return false;
46 }
47 }
48 return true;
49}
50
51unique_ptr<ParsedExpression> OperatorExpression::Copy() const {
52 auto copy = make_unique<OperatorExpression>(type);
53 copy->CopyProperties(*this);
54 for (auto &it : children) {
55 copy->children.push_back(it->Copy());
56 }
57 return move(copy);
58}
59
60void OperatorExpression::Serialize(Serializer &serializer) {
61 ParsedExpression::Serialize(serializer);
62 serializer.WriteList(children);
63}
64
65unique_ptr<ParsedExpression> OperatorExpression::Deserialize(ExpressionType type, Deserializer &source) {
66 auto expression = make_unique<OperatorExpression>(type);
67 source.ReadList<ParsedExpression>(expression->children);
68 return move(expression);
69}
70