/
expr_validator.cc
157 lines (136 loc) · 5.32 KB
/
expr_validator.cc
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <sstream>
#include <string>
#include <vector>
#include "gandiva/expr_validator.h"
namespace gandiva {
Status ExprValidator::Validate(const ExpressionPtr& expr) {
if (expr == nullptr) {
return Status::ExpressionValidationError("Expression cannot be null.");
}
Node& root = *expr->root();
Status status = root.Accept(*this);
if (!status.ok()) {
return status;
}
// validate return type matches
// no need to check if type is supported
// since root type has been validated.
if (!root.return_type()->Equals(*expr->result()->type())) {
std::stringstream ss;
ss << "Return type of root node " << root.return_type()->name()
<< " does not match that of expression " << *expr->result()->type();
return Status::ExpressionValidationError(ss.str());
}
return Status::OK();
}
Status ExprValidator::Visit(const FieldNode& node) {
auto llvm_type = types_->IRType(node.return_type()->id());
if (llvm_type == nullptr) {
std::stringstream ss;
ss << "Field " << node.field()->name() << " has unsupported data type "
<< node.return_type()->name();
return Status::ExpressionValidationError(ss.str());
}
auto field_in_schema_entry = field_map_.find(node.field()->name());
// validate that field is in schema.
if (field_in_schema_entry == field_map_.end()) {
std::stringstream ss;
ss << "Field " << node.field()->name() << " not in schema.";
return Status::ExpressionValidationError(ss.str());
}
FieldPtr field_in_schema = field_in_schema_entry->second;
// validate that field matches the definition in schema.
if (!field_in_schema->Equals(node.field())) {
std::stringstream ss;
ss << "Field definition in schema " << field_in_schema->ToString()
<< " different from field in expression " << node.field()->ToString();
return Status::ExpressionValidationError(ss.str());
}
return Status::OK();
}
Status ExprValidator::Visit(const FunctionNode& node) {
auto desc = node.descriptor();
FunctionSignature signature(desc->name(), desc->params(), desc->return_type());
const NativeFunction* native_function = registry_.LookupSignature(signature);
if (native_function == nullptr) {
std::stringstream ss;
ss << "Function " << signature.ToString() << " not supported yet. ";
return Status::ExpressionValidationError(ss.str());
}
for (auto& child : node.children()) {
Status status = child->Accept(*this);
GANDIVA_RETURN_NOT_OK(status);
}
return Status::OK();
}
Status ExprValidator::Visit(const IfNode& node) {
Status status = node.condition()->Accept(*this);
GANDIVA_RETURN_NOT_OK(status);
status = node.then_node()->Accept(*this);
GANDIVA_RETURN_NOT_OK(status);
status = node.else_node()->Accept(*this);
GANDIVA_RETURN_NOT_OK(status);
auto if_node_ret_type = node.return_type();
auto then_node_ret_type = node.then_node()->return_type();
auto else_node_ret_type = node.else_node()->return_type();
if (!if_node_ret_type->Equals(*then_node_ret_type)) {
std::stringstream ss;
ss << "Return type of if " << *if_node_ret_type << " and then " << *then_node_ret_type
<< " not matching.";
return Status::ExpressionValidationError(ss.str());
}
if (!if_node_ret_type->Equals(*else_node_ret_type)) {
std::stringstream ss;
ss << "Return type of if " << *if_node_ret_type << " and else " << *else_node_ret_type
<< " not matching.";
return Status::ExpressionValidationError(ss.str());
}
return Status::OK();
}
Status ExprValidator::Visit(const LiteralNode& node) {
auto llvm_type = types_->IRType(node.return_type()->id());
if (llvm_type == nullptr) {
std::stringstream ss;
ss << "Value " << node.holder() << " has unsupported data type "
<< node.return_type()->name();
return Status::ExpressionValidationError(ss.str());
}
return Status::OK();
}
Status ExprValidator::Visit(const BooleanNode& node) {
Status status;
if (node.children().size() < 2) {
std::stringstream ss;
ss << "Boolean expression has " << node.children().size()
<< " children, expected atleast two";
return Status::ExpressionValidationError(ss.str());
}
for (auto& child : node.children()) {
if (!child->return_type()->Equals(arrow::boolean())) {
std::stringstream ss;
ss << "Boolean expression has a child with return type "
<< child->return_type()->name() << ", expected return type boolean";
return Status::ExpressionValidationError(ss.str());
}
status = child->Accept(*this);
GANDIVA_RETURN_NOT_OK(status);
}
return Status::OK();
}
} // namespace gandiva