forked from alibaba/AliSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.cpp
More file actions
180 lines (149 loc) · 6.18 KB
/
function.cpp
File metadata and controls
180 lines (149 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "duckdb/function/function.hpp"
#include "duckdb/common/string_util.hpp"
#include "duckdb/common/types/hash.hpp"
#include "duckdb/function/built_in_functions.hpp"
#include "duckdb/function/scalar/string_functions.hpp"
#include "duckdb/function/scalar_function.hpp"
#include "duckdb/parser/parsed_data/pragma_info.hpp"
#include "duckdb/planner/expression/bound_aggregate_expression.hpp"
#include "duckdb/planner/expression/bound_function_expression.hpp"
#include "duckdb/main/extension_entries.hpp"
namespace duckdb {
FunctionData::~FunctionData() {
}
bool FunctionData::Equals(const FunctionData *left, const FunctionData *right) {
if (left == right) {
return true;
}
if (!left || !right) {
return false;
}
return left->Equals(*right);
}
TableFunctionData::~TableFunctionData() {
}
unique_ptr<FunctionData> TableFunctionData::Copy() const {
throw InternalException("Copy not supported for TableFunctionData");
}
bool TableFunctionData::Equals(const FunctionData &other) const {
return false;
}
bool FunctionData::SupportStatementCache() const {
return true;
}
Function::Function(string name_p) : name(std::move(name_p)) {
}
Function::~Function() {
}
SimpleFunction::SimpleFunction(string name_p, vector<LogicalType> arguments_p, LogicalType varargs_p)
: Function(std::move(name_p)), arguments(std::move(arguments_p)), varargs(std::move(varargs_p)) {
}
SimpleFunction::~SimpleFunction() {
}
string SimpleFunction::ToString() const {
return Function::CallToString(catalog_name, schema_name, name, arguments, varargs);
}
bool SimpleFunction::HasVarArgs() const {
return varargs.id() != LogicalTypeId::INVALID;
}
SimpleNamedParameterFunction::SimpleNamedParameterFunction(string name_p, vector<LogicalType> arguments_p,
LogicalType varargs_p)
: SimpleFunction(std::move(name_p), std::move(arguments_p), std::move(varargs_p)) {
}
SimpleNamedParameterFunction::~SimpleNamedParameterFunction() {
}
string SimpleNamedParameterFunction::ToString() const {
return Function::CallToString(catalog_name, schema_name, name, arguments, named_parameters);
}
bool SimpleNamedParameterFunction::HasNamedParameters() const {
return !named_parameters.empty();
}
BaseScalarFunction::BaseScalarFunction(string name_p, vector<LogicalType> arguments_p, LogicalType return_type_p,
FunctionStability stability, LogicalType varargs_p,
FunctionNullHandling null_handling, FunctionErrors errors)
: SimpleFunction(std::move(name_p), std::move(arguments_p), std::move(varargs_p)),
return_type(std::move(return_type_p)), stability(stability), null_handling(null_handling), errors(errors),
collation_handling(FunctionCollationHandling::PROPAGATE_COLLATIONS) {
}
BaseScalarFunction::~BaseScalarFunction() {
}
string BaseScalarFunction::ToString() const {
return Function::CallToString(catalog_name, schema_name, name, arguments, varargs, return_type);
}
// add your initializer for new functions here
void BuiltinFunctions::Initialize() {
RegisterTableScanFunctions();
RegisterSQLiteFunctions();
RegisterReadFunctions();
RegisterTableFunctions();
RegisterArrowFunctions();
RegisterPragmaFunctions();
// initialize collations
AddCollation("nocase", LowerFun::GetFunction(), true);
AddCollation("noaccent", StripAccentsFun::GetFunction(), true);
AddCollation("nfc", NFCNormalizeFun::GetFunction());
RegisterExtensionOverloads();
}
hash_t BaseScalarFunction::Hash() const {
hash_t hash = return_type.Hash();
for (auto &arg : arguments) {
hash = duckdb::CombineHash(hash, arg.Hash());
}
return hash;
}
static bool RequiresCatalogAndSchemaNamePrefix(const string &catalog_name, const string &schema_name) {
return !catalog_name.empty() && catalog_name != SYSTEM_CATALOG && !schema_name.empty() &&
schema_name != DEFAULT_SCHEMA;
}
string Function::CallToString(const string &catalog_name, const string &schema_name, const string &name,
const vector<LogicalType> &arguments, const LogicalType &varargs) {
string result;
if (RequiresCatalogAndSchemaNamePrefix(catalog_name, schema_name)) {
result += catalog_name + "." + schema_name + ".";
}
result += name + "(";
vector<string> string_arguments;
for (auto &arg : arguments) {
string_arguments.push_back(arg.ToString());
}
if (varargs.IsValid()) {
string_arguments.push_back("[" + varargs.ToString() + "...]");
}
result += StringUtil::Join(string_arguments, ", ");
return result + ")";
}
string Function::CallToString(const string &catalog_name, const string &schema_name, const string &name,
const vector<LogicalType> &arguments, const LogicalType &varargs,
const LogicalType &return_type) {
string result = CallToString(catalog_name, schema_name, name, arguments, varargs);
result += " -> " + return_type.ToString();
return result;
}
string Function::CallToString(const string &catalog_name, const string &schema_name, const string &name,
const vector<LogicalType> &arguments,
const named_parameter_type_map_t &named_parameters) {
vector<string> input_arguments;
input_arguments.reserve(arguments.size() + named_parameters.size());
for (auto &arg : arguments) {
input_arguments.push_back(arg.ToString());
}
for (auto &kv : named_parameters) {
input_arguments.push_back(StringUtil::Format("%s : %s", kv.first, kv.second.ToString()));
}
string prefix = "";
if (RequiresCatalogAndSchemaNamePrefix(catalog_name, schema_name)) {
prefix = StringUtil::Format("%s.%s.", catalog_name, schema_name);
}
return StringUtil::Format("%s%s(%s)", prefix, name, StringUtil::Join(input_arguments, ", "));
}
void Function::EraseArgument(SimpleFunction &bound_function, vector<unique_ptr<Expression>> &arguments,
idx_t argument_index) {
if (bound_function.original_arguments.empty()) {
bound_function.original_arguments = bound_function.arguments;
}
D_ASSERT(arguments.size() == bound_function.arguments.size());
D_ASSERT(argument_index < arguments.size());
arguments.erase_at(argument_index);
bound_function.arguments.erase_at(argument_index);
}
} // namespace duckdb