1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 #include <gtest/gtest.h>
19 #include <math.h>
20 #include <time.h>
21 #include "arrow/memory_pool.h"
22 #include "gandiva/projector.h"
23 #include "gandiva/tests/test_util.h"
24 #include "gandiva/tree_expr_builder.h"
25
26 namespace gandiva {
27
28 using arrow::boolean;
29 using arrow::float64;
30 using arrow::int32;
31 using arrow::int64;
32
33 class TestToString : public ::testing::Test {
34 public:
SetUp()35 void SetUp() { pool_ = arrow::default_memory_pool(); }
36
37 protected:
38 arrow::MemoryPool* pool_;
39 };
40
41 #define CHECK_EXPR_TO_STRING(e, str) EXPECT_STREQ(e->ToString().c_str(), str)
42
TEST_F(TestToString,TestAll)43 TEST_F(TestToString, TestAll) {
44 auto literal_node = TreeExprBuilder::MakeLiteral((uint64_t)100);
45 auto literal_expr =
46 TreeExprBuilder::MakeExpression(literal_node, arrow::field("r", int64()));
47 CHECK_EXPR_TO_STRING(literal_expr, "(const uint64) 100");
48
49 auto f0 = arrow::field("f0", float64());
50 auto f0_node = TreeExprBuilder::MakeField(f0);
51 auto f0_expr = TreeExprBuilder::MakeExpression(f0_node, f0);
52 CHECK_EXPR_TO_STRING(f0_expr, "(double) f0");
53
54 auto f1 = arrow::field("f1", int64());
55 auto f2 = arrow::field("f2", int64());
56 auto f1_node = TreeExprBuilder::MakeField(f1);
57 auto f2_node = TreeExprBuilder::MakeField(f2);
58 auto add_node = TreeExprBuilder::MakeFunction("add", {f1_node, f2_node}, int64());
59 auto add_expr = TreeExprBuilder::MakeExpression(add_node, f1);
60 CHECK_EXPR_TO_STRING(add_expr, "int64 add((int64) f1, (int64) f2)");
61
62 auto cond_node = TreeExprBuilder::MakeFunction(
63 "lesser_than", {f0_node, TreeExprBuilder::MakeLiteral(static_cast<float>(0))},
64 boolean());
65 auto then_node = TreeExprBuilder::MakeField(f1);
66 auto else_node = TreeExprBuilder::MakeField(f2);
67
68 auto if_node = TreeExprBuilder::MakeIf(cond_node, then_node, else_node, int64());
69 auto if_expr = TreeExprBuilder::MakeExpression(if_node, f1);
70
71 CHECK_EXPR_TO_STRING(if_expr,
72 "if (bool lesser_than((double) f0, (const float) 0 raw(0))) { "
73 "(int64) f1 } else { (int64) f2 }");
74
75 auto f1_gt_100 =
76 TreeExprBuilder::MakeFunction("greater_than", {f1_node, literal_node}, boolean());
77 auto f2_equals_100 =
78 TreeExprBuilder::MakeFunction("equals", {f2_node, literal_node}, boolean());
79 auto and_node = TreeExprBuilder::MakeAnd({f1_gt_100, f2_equals_100});
80 auto and_expr =
81 TreeExprBuilder::MakeExpression(and_node, arrow::field("f0", boolean()));
82
83 CHECK_EXPR_TO_STRING(and_expr,
84 "bool greater_than((int64) f1, (const uint64) 100) && bool "
85 "equals((int64) f2, (const uint64) 100)");
86 }
87
88 } // namespace gandiva
89