1 | #include "duckdb/parser/expression/function_expression.hpp" |
2 | |
3 | #include <utility> |
4 | #include "duckdb/common/string_util.hpp" |
5 | #include "duckdb/common/exception.hpp" |
6 | #include "duckdb/common/field_writer.hpp" |
7 | #include "duckdb/common/types/hash.hpp" |
8 | |
9 | #include "duckdb/common/serializer/format_serializer.hpp" |
10 | #include "duckdb/common/serializer/format_deserializer.hpp" |
11 | |
12 | namespace duckdb { |
13 | |
14 | FunctionExpression::FunctionExpression(string catalog, string schema, const string &function_name, |
15 | vector<unique_ptr<ParsedExpression>> children_p, |
16 | unique_ptr<ParsedExpression> filter, unique_ptr<OrderModifier> order_bys_p, |
17 | bool distinct, bool is_operator, bool export_state_p) |
18 | : ParsedExpression(ExpressionType::FUNCTION, ExpressionClass::FUNCTION), catalog(std::move(catalog)), |
19 | schema(std::move(schema)), function_name(StringUtil::Lower(str: function_name)), is_operator(is_operator), |
20 | children(std::move(children_p)), distinct(distinct), filter(std::move(filter)), order_bys(std::move(order_bys_p)), |
21 | export_state(export_state_p) { |
22 | D_ASSERT(!function_name.empty()); |
23 | if (!order_bys) { |
24 | order_bys = make_uniq<OrderModifier>(); |
25 | } |
26 | } |
27 | |
28 | FunctionExpression::FunctionExpression(const string &function_name, vector<unique_ptr<ParsedExpression>> children_p, |
29 | unique_ptr<ParsedExpression> filter, unique_ptr<OrderModifier> order_bys, |
30 | bool distinct, bool is_operator, bool export_state_p) |
31 | : FunctionExpression(INVALID_CATALOG, INVALID_SCHEMA, function_name, std::move(children_p), std::move(filter), |
32 | std::move(order_bys), distinct, is_operator, export_state_p) { |
33 | } |
34 | |
35 | string FunctionExpression::ToString() const { |
36 | return ToString<FunctionExpression, ParsedExpression>(entry: *this, schema, function_name, is_operator, distinct, |
37 | filter: filter.get(), order_bys: order_bys.get(), export_state, add_alias: true); |
38 | } |
39 | |
40 | bool FunctionExpression::Equal(const FunctionExpression &a, const FunctionExpression &b) { |
41 | if (a.catalog != b.catalog || a.schema != b.schema || a.function_name != b.function_name || |
42 | b.distinct != a.distinct) { |
43 | return false; |
44 | } |
45 | if (b.children.size() != a.children.size()) { |
46 | return false; |
47 | } |
48 | for (idx_t i = 0; i < a.children.size(); i++) { |
49 | if (!a.children[i]->Equals(other: *b.children[i])) { |
50 | return false; |
51 | } |
52 | } |
53 | if (!ParsedExpression::Equals(left: a.filter, right: b.filter)) { |
54 | return false; |
55 | } |
56 | if (!OrderModifier::Equals(left: a.order_bys, right: b.order_bys)) { |
57 | return false; |
58 | } |
59 | if (a.export_state != b.export_state) { |
60 | return false; |
61 | } |
62 | return true; |
63 | } |
64 | |
65 | hash_t FunctionExpression::Hash() const { |
66 | hash_t result = ParsedExpression::Hash(); |
67 | result = CombineHash(left: result, right: duckdb::Hash<const char *>(val: schema.c_str())); |
68 | result = CombineHash(left: result, right: duckdb::Hash<const char *>(val: function_name.c_str())); |
69 | result = CombineHash(left: result, right: duckdb::Hash<bool>(value: distinct)); |
70 | result = CombineHash(left: result, right: duckdb::Hash<bool>(value: export_state)); |
71 | return result; |
72 | } |
73 | |
74 | unique_ptr<ParsedExpression> FunctionExpression::Copy() const { |
75 | vector<unique_ptr<ParsedExpression>> copy_children; |
76 | unique_ptr<ParsedExpression> filter_copy; |
77 | copy_children.reserve(n: children.size()); |
78 | for (auto &child : children) { |
79 | copy_children.push_back(x: child->Copy()); |
80 | } |
81 | if (filter) { |
82 | filter_copy = filter->Copy(); |
83 | } |
84 | auto order_copy = order_bys ? unique_ptr_cast<ResultModifier, OrderModifier>(src: order_bys->Copy()) : nullptr; |
85 | auto copy = |
86 | make_uniq<FunctionExpression>(args: catalog, args: schema, args: function_name, args: std::move(copy_children), args: std::move(filter_copy), |
87 | args: std::move(order_copy), args: distinct, args: is_operator, args: export_state); |
88 | copy->CopyProperties(other: *this); |
89 | return std::move(copy); |
90 | } |
91 | |
92 | void FunctionExpression::Serialize(FieldWriter &writer) const { |
93 | writer.WriteString(val: function_name); |
94 | writer.WriteString(val: schema); |
95 | writer.WriteSerializableList(elements: children); |
96 | writer.WriteOptional(element: filter); |
97 | writer.WriteSerializable(element: (ResultModifier &)*order_bys); |
98 | writer.WriteField<bool>(element: distinct); |
99 | writer.WriteField<bool>(element: is_operator); |
100 | writer.WriteField<bool>(element: export_state); |
101 | writer.WriteString(val: catalog); |
102 | } |
103 | |
104 | unique_ptr<ParsedExpression> FunctionExpression::Deserialize(ExpressionType type, FieldReader &reader) { |
105 | auto function_name = reader.ReadRequired<string>(); |
106 | auto schema = reader.ReadRequired<string>(); |
107 | auto children = reader.ReadRequiredSerializableList<ParsedExpression>(); |
108 | auto filter = reader.ReadOptional<ParsedExpression>(default_value: nullptr); |
109 | auto order_bys = unique_ptr_cast<ResultModifier, OrderModifier>(src: reader.ReadRequiredSerializable<ResultModifier>()); |
110 | auto distinct = reader.ReadRequired<bool>(); |
111 | auto is_operator = reader.ReadRequired<bool>(); |
112 | auto export_state = reader.ReadField<bool>(default_value: false); |
113 | auto catalog = reader.ReadField<string>(INVALID_CATALOG); |
114 | |
115 | unique_ptr<FunctionExpression> function; |
116 | function = make_uniq<FunctionExpression>(args&: catalog, args&: schema, args&: function_name, args: std::move(children), args: std::move(filter), |
117 | args: std::move(order_bys), args&: distinct, args&: is_operator, args&: export_state); |
118 | return std::move(function); |
119 | } |
120 | |
121 | void FunctionExpression::Verify() const { |
122 | D_ASSERT(!function_name.empty()); |
123 | } |
124 | |
125 | void FunctionExpression::FormatSerialize(FormatSerializer &serializer) const { |
126 | ParsedExpression::FormatSerialize(serializer); |
127 | serializer.WriteProperty(tag: "function_name" , value: function_name); |
128 | serializer.WriteProperty(tag: "schema" , value: schema); |
129 | serializer.WriteProperty(tag: "children" , value: children); |
130 | serializer.WriteOptionalProperty(tag: "filter" , ptr: filter); |
131 | serializer.WriteProperty(tag: "order_bys" , value&: (ResultModifier &)*order_bys); |
132 | serializer.WriteProperty(tag: "distinct" , value: distinct); |
133 | serializer.WriteProperty(tag: "is_operator" , value: is_operator); |
134 | serializer.WriteProperty(tag: "export_state" , value: export_state); |
135 | serializer.WriteProperty(tag: "catalog" , value: catalog); |
136 | } |
137 | |
138 | unique_ptr<ParsedExpression> FunctionExpression::FormatDeserialize(ExpressionType type, |
139 | FormatDeserializer &deserializer) { |
140 | auto function_name = deserializer.ReadProperty<string>(tag: "function_name" ); |
141 | auto schema = deserializer.ReadProperty<string>(tag: "schema" ); |
142 | auto children = deserializer.ReadProperty<vector<unique_ptr<ParsedExpression>>>(tag: "children" ); |
143 | auto filter = deserializer.ReadOptionalProperty<unique_ptr<ParsedExpression>>(tag: "filter" ); |
144 | auto order_bys = unique_ptr_cast<ResultModifier, OrderModifier>( |
145 | src: deserializer.ReadProperty<unique_ptr<ResultModifier>>(tag: "order_bys" )); |
146 | auto distinct = deserializer.ReadProperty<bool>(tag: "distinct" ); |
147 | auto is_operator = deserializer.ReadProperty<bool>(tag: "is_operator" ); |
148 | auto export_state = deserializer.ReadProperty<bool>(tag: "export_state" ); |
149 | auto catalog = deserializer.ReadProperty<string>(tag: "catalog" ); |
150 | |
151 | unique_ptr<FunctionExpression> function; |
152 | function = make_uniq<FunctionExpression>(args&: catalog, args&: schema, args&: function_name, args: std::move(children), args: std::move(filter), |
153 | args: std::move(order_bys), args&: distinct, args&: is_operator, args&: export_state); |
154 | return std::move(function); |
155 | } |
156 | |
157 | } // namespace duckdb |
158 | |