|
| 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 <gtest/gtest.h> |
| 16 | +#include <math.h> |
| 17 | +#include <time.h> |
| 18 | +#include "arrow/memory_pool.h" |
| 19 | +#include "gandiva/projector.h" |
| 20 | +#include "gandiva/tree_expr_builder.h" |
| 21 | +#include "integ/test_util.h" |
| 22 | + |
| 23 | +namespace gandiva { |
| 24 | + |
| 25 | +using arrow::boolean; |
| 26 | +using arrow::float64; |
| 27 | +using arrow::int32; |
| 28 | +using arrow::int64; |
| 29 | + |
| 30 | +class TestToString : public ::testing::Test { |
| 31 | + public: |
| 32 | + void SetUp() { pool_ = arrow::default_memory_pool(); } |
| 33 | + |
| 34 | + protected: |
| 35 | + arrow::MemoryPool* pool_; |
| 36 | +}; |
| 37 | + |
| 38 | +#define CHECK_EXPR_TO_STRING(e, str) EXPECT_STREQ(e->ToString().c_str(), str) |
| 39 | + |
| 40 | +TEST_F(TestToString, TestAll) { |
| 41 | + auto literal_node = TreeExprBuilder::MakeLiteral((uint64_t)100); |
| 42 | + auto literal_expr = |
| 43 | + TreeExprBuilder::MakeExpression(literal_node, arrow::field("r", int64())); |
| 44 | + CHECK_EXPR_TO_STRING(literal_expr, "100"); |
| 45 | + |
| 46 | + auto f0 = arrow::field("f0", float64()); |
| 47 | + auto f0_node = TreeExprBuilder::MakeField(f0); |
| 48 | + auto f0_expr = TreeExprBuilder::MakeExpression(f0_node, f0); |
| 49 | + CHECK_EXPR_TO_STRING(f0_expr, "double"); |
| 50 | + |
| 51 | + auto f1 = arrow::field("f1", int64()); |
| 52 | + auto f2 = arrow::field("f2", int64()); |
| 53 | + auto f1_node = TreeExprBuilder::MakeField(f1); |
| 54 | + auto f2_node = TreeExprBuilder::MakeField(f2); |
| 55 | + auto add_node = TreeExprBuilder::MakeFunction("add", {f1_node, f2_node}, int64()); |
| 56 | + auto add_expr = TreeExprBuilder::MakeExpression(add_node, f1); |
| 57 | + CHECK_EXPR_TO_STRING(add_expr, "int64 add(int64, int64)"); |
| 58 | + |
| 59 | + auto cond_node = TreeExprBuilder::MakeFunction( |
| 60 | + "lesser_than", {f0_node, TreeExprBuilder::MakeLiteral((float)0)}, boolean()); |
| 61 | + auto then_node = TreeExprBuilder::MakeField(f1); |
| 62 | + auto else_node = TreeExprBuilder::MakeField(f2); |
| 63 | + |
| 64 | + auto if_node = TreeExprBuilder::MakeIf(cond_node, then_node, else_node, int64()); |
| 65 | + auto if_expr = TreeExprBuilder::MakeExpression(if_node, f1); |
| 66 | + CHECK_EXPR_TO_STRING(if_expr, |
| 67 | + "if (bool lesser_than(double, 0)) { int64 } else { int64 }"); |
| 68 | + |
| 69 | + auto f1_gt_100 = |
| 70 | + TreeExprBuilder::MakeFunction("greater_than", {f1_node, literal_node}, boolean()); |
| 71 | + auto f2_equals_100 = |
| 72 | + TreeExprBuilder::MakeFunction("equals", {f2_node, literal_node}, boolean()); |
| 73 | + auto and_node = TreeExprBuilder::MakeAnd({f1_gt_100, f2_equals_100}); |
| 74 | + auto and_expr = |
| 75 | + TreeExprBuilder::MakeExpression(and_node, arrow::field("f0", boolean())); |
| 76 | + CHECK_EXPR_TO_STRING(and_expr, |
| 77 | + "bool greater_than(int64, 100) && bool equals(int64, 100)"); |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace gandiva |
0 commit comments