|
| 1 | +// Copyright (C) 2017-2018 Dremio Corporation |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <string> |
| 16 | +#include <sstream> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +#include "codegen/expr_validator.h" |
| 20 | + |
| 21 | +namespace gandiva { |
| 22 | + |
| 23 | +Status ExprValidator::Validate(const ExpressionPtr &expr) { |
| 24 | + if (expr == nullptr) { |
| 25 | + return Status::ExpressionValidationError("Expression cannot be null."); |
| 26 | + } |
| 27 | + Node &root = *expr->root(); |
| 28 | + Status status = root.Accept(*this); |
| 29 | + if (!status.ok()) { |
| 30 | + return status; |
| 31 | + } |
| 32 | + // validate return type matches |
| 33 | + // no need to check if type is supported |
| 34 | + // since root type has been validated. |
| 35 | + if (!root.return_type()->Equals(*expr->result()->type())) { |
| 36 | + std::stringstream ss; |
| 37 | + ss << "Return type of root node " << root.return_type()->name() |
| 38 | + << " does not match that of expression " << *expr->result()->type(); |
| 39 | + return Status::ExpressionValidationError(ss.str()); |
| 40 | + } |
| 41 | + return Status::OK(); |
| 42 | +} |
| 43 | + |
| 44 | +Status ExprValidator::Visit(const FieldNode &node) { |
| 45 | + auto llvm_type = types_->IRType(node.return_type()->id()); |
| 46 | + if (llvm_type == nullptr) { |
| 47 | + std::stringstream ss; |
| 48 | + ss << "Field "<< node.field()->name() << " has unsupported data type " |
| 49 | + << node.return_type()->name(); |
| 50 | + return Status::ExpressionValidationError(ss.str()); |
| 51 | + } |
| 52 | + |
| 53 | + auto field_in_schema_entry = field_map_.find(node.field()->name()); |
| 54 | + |
| 55 | + // validate that field is in schema. |
| 56 | + if (field_in_schema_entry == field_map_.end()) { |
| 57 | + std::stringstream ss; |
| 58 | + ss << "Field " << node.field()->name() << " not in schema."; |
| 59 | + return Status::ExpressionValidationError(ss.str()); |
| 60 | + } |
| 61 | + |
| 62 | + FieldPtr field_in_schema = field_in_schema_entry->second; |
| 63 | + // validate that field matches the definition in schema. |
| 64 | + if (!field_in_schema->Equals(node.field())) { |
| 65 | + std::stringstream ss; |
| 66 | + ss << "Field definition in schema " << field_in_schema->ToString() |
| 67 | + << " different from field in expression " << node.field()->ToString(); |
| 68 | + return Status::ExpressionValidationError(ss.str()); |
| 69 | + } |
| 70 | + return Status::OK(); |
| 71 | +} |
| 72 | + |
| 73 | +Status ExprValidator::Visit(const FunctionNode &node) { |
| 74 | + auto desc = node.descriptor(); |
| 75 | + FunctionSignature signature(desc->name(), |
| 76 | + desc->params(), |
| 77 | + desc->return_type()); |
| 78 | + const NativeFunction *native_function = registry_.LookupSignature(signature); |
| 79 | + if (native_function == nullptr) { |
| 80 | + std::stringstream ss; |
| 81 | + ss << "Function "<< signature.ToString() << " not supported yet. "; |
| 82 | + return Status::ExpressionValidationError(ss.str()); |
| 83 | + } |
| 84 | + |
| 85 | + for (auto &child : node.children()) { |
| 86 | + Status status = child->Accept(*this); |
| 87 | + GANDIVA_RETURN_NOT_OK(status); |
| 88 | + } |
| 89 | + return Status::OK(); |
| 90 | +} |
| 91 | + |
| 92 | +Status ExprValidator::Visit(const IfNode &node) { |
| 93 | + Status status = node.condition()->Accept(*this); |
| 94 | + GANDIVA_RETURN_NOT_OK(status); |
| 95 | + status = node.then_node()->Accept(*this); |
| 96 | + GANDIVA_RETURN_NOT_OK(status); |
| 97 | + status = node.else_node()->Accept(*this); |
| 98 | + GANDIVA_RETURN_NOT_OK(status); |
| 99 | + |
| 100 | + auto if_node_ret_type = node.return_type(); |
| 101 | + auto then_node_ret_type = node.then_node()->return_type(); |
| 102 | + auto else_node_ret_type = node.else_node()->return_type(); |
| 103 | + |
| 104 | + if (if_node_ret_type != then_node_ret_type) { |
| 105 | + std::stringstream ss; |
| 106 | + ss << "Return type of if "<< *if_node_ret_type << " and then " |
| 107 | + << then_node_ret_type->name() << " not matching."; |
| 108 | + return Status::ExpressionValidationError(ss.str()); |
| 109 | + } |
| 110 | + |
| 111 | + if (if_node_ret_type != else_node_ret_type) { |
| 112 | + std::stringstream ss; |
| 113 | + ss << "Return type of if "<< *if_node_ret_type << " and else " |
| 114 | + << else_node_ret_type->name() << " not matching."; |
| 115 | + return Status::ExpressionValidationError(ss.str()); |
| 116 | + } |
| 117 | + |
| 118 | + return Status::OK(); |
| 119 | +} |
| 120 | + |
| 121 | +Status ExprValidator::Visit(const LiteralNode &node) { |
| 122 | + auto llvm_type = types_->IRType(node.return_type()->id()); |
| 123 | + if (llvm_type == nullptr) { |
| 124 | + std::stringstream ss; |
| 125 | + ss << "Value "<< node.holder() << " has unsupported data type " |
| 126 | + << node.return_type()->name(); |
| 127 | + return Status::ExpressionValidationError(ss.str()); |
| 128 | + } |
| 129 | + return Status::OK(); |
| 130 | +} |
| 131 | + |
| 132 | +} // namespace gandiva |
0 commit comments