forked from substrait-io/substrait-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunction.cpp
More file actions
93 lines (82 loc) · 2.63 KB
/
Function.cpp
File metadata and controls
93 lines (82 loc) · 2.63 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
/* SPDX-License-Identifier: Apache-2.0 */
#include "substrait/function/Function.h"
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#include "substrait/function/FunctionSignature.h"
namespace io::substrait {
bool FunctionImplementation::tryMatch(const FunctionSignature& signature) {
const auto& actualTypes = signature.arguments;
if (variadic.has_value()) {
// return false if actual types length less than min of variadic
const auto max = variadic->max;
if ((actualTypes.size() < variadic->min) ||
(max.has_value() && actualTypes.size() > max.value())) {
return false;
}
const auto& variadicArgument = arguments[0];
// actual type must same as the variadicArgument
if (const auto& variadicValueArgument =
std::dynamic_pointer_cast<const ValueArgument>(variadicArgument)) {
for (const auto& actualType : actualTypes) {
if (!variadicValueArgument->type->isMatch(actualType)) {
return false;
}
}
}
} else {
std::vector<std::shared_ptr<const ValueArgument>> valueArguments;
for (const auto& argument : arguments) {
if (const auto& variadicValueArgument =
std::dynamic_pointer_cast<const ValueArgument>(argument)) {
valueArguments.emplace_back(variadicValueArgument);
}
}
// return false if size of actual types not equal to size of value
// arguments.
if (valueArguments.size() != actualTypes.size()) {
return false;
}
for (auto i = 0; i < actualTypes.size(); i++) {
const auto& valueArgument = valueArguments[i];
if (!valueArgument->type->isMatch(actualTypes[i])) {
return false;
}
}
}
const auto& sigReturnType = signature.returnType;
if (this->returnType && sigReturnType) {
return returnType->isMatch(sigReturnType);
} else {
return true;
}
}
std::string FunctionImplementation::signature() const {
std::stringstream ss;
ss << name;
if (!arguments.empty()) {
ss << ":";
for (auto it = arguments.begin(); it != arguments.end(); ++it) {
const auto& typeSign = (*it)->toTypeString();
if (it == arguments.end() - 1) {
ss << typeSign;
} else {
ss << typeSign << "_";
}
}
}
return ss.str();
}
bool AggregateFunctionImplementation::tryMatch(
const FunctionSignature& signature) {
bool matched = FunctionImplementation::tryMatch(signature);
if (!matched && intermediate) {
const auto& actualTypes = signature.arguments;
if (actualTypes.size() == 1) {
return intermediate->isMatch(actualTypes[0]);
}
}
return matched;
}
} // namespace io::substrait