|
| 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 "exprs/function/functions_logical.h" |
| 19 | + |
| 20 | +#include <gen_cpp/Exprs_types.h> |
| 21 | +#include <gtest/gtest.h> |
| 22 | + |
| 23 | +#include <string> |
| 24 | +#include <utility> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +#include "core/block/block.h" |
| 28 | +#include "core/column/column_const.h" |
| 29 | +#include "core/column/column_nullable.h" |
| 30 | +#include "core/data_type/data_type_nullable.h" |
| 31 | +#include "core/data_type/data_type_number.h" |
| 32 | +#include "core/data_type/primitive_type.h" |
| 33 | +#include "exprs/vcompound_pred.h" |
| 34 | +#include "exprs/vexpr_context.h" |
| 35 | +#include "testutil/column_helper.h" |
| 36 | + |
| 37 | +namespace doris { |
| 38 | + |
| 39 | +namespace { |
| 40 | + |
| 41 | +ColumnWithTypeAndName nullable_boolean_column(std::vector<UInt8> data, std::vector<UInt8> null_map, |
| 42 | + std::string name) { |
| 43 | + return {ColumnHelper::create_nullable_column<DataTypeUInt8>(data, null_map), |
| 44 | + make_nullable(std::make_shared<DataTypeUInt8>()), std::move(name)}; |
| 45 | +} |
| 46 | + |
| 47 | +ColumnPtr execute_or(ColumnWithTypeAndName left, ColumnWithTypeAndName right, size_t rows) { |
| 48 | + auto result_type = make_nullable(std::make_shared<DataTypeUInt8>()); |
| 49 | + Block block({std::move(left), std::move(right), {nullptr, result_type, "result"}}); |
| 50 | + auto status = FunctionOr::create()->execute_impl(nullptr, block, {0, 1}, 2, rows); |
| 51 | + EXPECT_TRUE(status.ok()) << status.to_string(); |
| 52 | + return block.get_by_position(2).column; |
| 53 | +} |
| 54 | + |
| 55 | +class ColumnExpr final : public VExpr { |
| 56 | +public: |
| 57 | + ColumnExpr(ColumnPtr column, DataTypePtr type) |
| 58 | + : VExpr(std::move(type), false), _column(std::move(column)) {} |
| 59 | + |
| 60 | + bool is_constant() const override { return false; } |
| 61 | + |
| 62 | + const std::string& expr_name() const override { |
| 63 | + static const std::string name = "ColumnExpr"; |
| 64 | + return name; |
| 65 | + } |
| 66 | + |
| 67 | + Status execute_column_impl(VExprContext*, const Block*, const Selector*, size_t, |
| 68 | + ColumnPtr& result_column) const override { |
| 69 | + result_column = _column; |
| 70 | + return Status::OK(); |
| 71 | + } |
| 72 | + |
| 73 | +private: |
| 74 | + ColumnPtr _column; |
| 75 | +}; |
| 76 | + |
| 77 | +ColumnPtr execute_compound_or(ColumnWithTypeAndName left, ColumnWithTypeAndName right, |
| 78 | + size_t rows) { |
| 79 | + TExprNode node; |
| 80 | + node.__set_type(create_type_desc(TYPE_BOOLEAN)); |
| 81 | + node.__set_node_type(TExprNodeType::COMPOUND_PRED); |
| 82 | + node.__set_opcode(TExprOpcode::COMPOUND_OR); |
| 83 | + node.__set_num_children(2); |
| 84 | + node.__set_is_nullable(true); |
| 85 | + |
| 86 | + auto compound = VCompoundPred::create_shared(node); |
| 87 | + compound->add_child(std::make_shared<ColumnExpr>(std::move(left.column), left.type)); |
| 88 | + compound->add_child(std::make_shared<ColumnExpr>(std::move(right.column), right.type)); |
| 89 | + VExprContext context(compound); |
| 90 | + ColumnPtr result; |
| 91 | + auto status = compound->execute_column(&context, nullptr, nullptr, rows, result); |
| 92 | + EXPECT_TRUE(status.ok()) << status.to_string(); |
| 93 | + return result; |
| 94 | +} |
| 95 | + |
| 96 | +void expect_boolean(const ColumnNullable& result, size_t row, UInt8 value) { |
| 97 | + EXPECT_FALSE(result.is_null_at(row)); |
| 98 | + EXPECT_EQ(assert_cast<const ColumnUInt8&>(result.get_nested_column()).get_data()[row], value); |
| 99 | +} |
| 100 | + |
| 101 | +} // namespace |
| 102 | + |
| 103 | +TEST(FunctionsLogicalTest, NullableOrIgnoresNullPayload) { |
| 104 | + auto result = |
| 105 | + execute_or(nullable_boolean_column({65, 0, 65, 1, 65}, {1, 0, 1, 0, 1}, "left"), |
| 106 | + nullable_boolean_column({1, 0, 0, 65, 127}, {0, 0, 0, 1, 1}, "right"), 5); |
| 107 | + |
| 108 | + const auto& nullable_result = assert_cast<const ColumnNullable&>(*result); |
| 109 | + expect_boolean(nullable_result, 0, 1); |
| 110 | + expect_boolean(nullable_result, 1, 0); |
| 111 | + EXPECT_TRUE(nullable_result.is_null_at(2)); |
| 112 | + expect_boolean(nullable_result, 3, 1); |
| 113 | + EXPECT_TRUE(nullable_result.is_null_at(4)); |
| 114 | +} |
| 115 | + |
| 116 | +TEST(FunctionsLogicalTest, NullableOrWithTrueConstantProducesCanonicalTrue) { |
| 117 | + constexpr size_t rows = 4; |
| 118 | + auto left = nullable_boolean_column({3, 65, 0, 255}, {1, 1, 1, 1}, "left"); |
| 119 | + ColumnWithTypeAndName right { |
| 120 | + ColumnConst::create(ColumnHelper::create_column<DataTypeUInt8>({1}), rows), |
| 121 | + std::make_shared<DataTypeUInt8>(), "right"}; |
| 122 | + |
| 123 | + auto result = execute_or(std::move(left), std::move(right), rows); |
| 124 | + const auto& nullable_result = assert_cast<const ColumnNullable&>(*result); |
| 125 | + for (size_t row = 0; row < rows; ++row) { |
| 126 | + expect_boolean(nullable_result, row, 1); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +TEST(FunctionsLogicalTest, CompoundNullableOrIgnoresNullPayload) { |
| 131 | + auto result = execute_compound_or( |
| 132 | + nullable_boolean_column({65, 0, 65, 1, 65}, {1, 0, 1, 0, 1}, "left"), |
| 133 | + nullable_boolean_column({1, 0, 0, 65, 127}, {0, 0, 0, 1, 1}, "right"), 5); |
| 134 | + |
| 135 | + const auto& nullable_result = assert_cast<const ColumnNullable&>(*result); |
| 136 | + expect_boolean(nullable_result, 0, 1); |
| 137 | + expect_boolean(nullable_result, 1, 0); |
| 138 | + EXPECT_TRUE(nullable_result.is_null_at(2)); |
| 139 | + expect_boolean(nullable_result, 3, 1); |
| 140 | + EXPECT_TRUE(nullable_result.is_null_at(4)); |
| 141 | +} |
| 142 | + |
| 143 | +} // namespace doris |
0 commit comments