be/src/exprs/function/function_assert_true.cpp
Line | Count | Source |
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 <memory> |
19 | | |
20 | | #include "common/exception.h" |
21 | | #include "common/status.h" |
22 | | #include "core/block/block.h" |
23 | | #include "core/block/column_numbers.h" |
24 | | #include "core/block/column_with_type_and_name.h" |
25 | | #include "core/column/column.h" |
26 | | #include "core/column/column_const.h" |
27 | | #include "core/column/column_nullable.h" |
28 | | #include "core/data_type/data_type.h" |
29 | | #include "core/data_type/data_type_number.h" |
30 | | #include "core/types.h" |
31 | | #include "exprs/function/function.h" |
32 | | #include "exprs/function/function_helpers.h" |
33 | | #include "exprs/function/simple_function_factory.h" |
34 | | |
35 | | namespace doris { |
36 | | class FunctionContext; |
37 | | } // namespace doris |
38 | | |
39 | | namespace doris { |
40 | | |
41 | | class FunctionAssertTrue : public IFunction { |
42 | | public: |
43 | | static constexpr auto name = "assert_true"; |
44 | | |
45 | 8 | static FunctionPtr create() { return std::make_shared<FunctionAssertTrue>(); } |
46 | | |
47 | 1 | String get_name() const override { return name; } |
48 | | |
49 | 0 | size_t get_number_of_arguments() const override { return 2; } |
50 | | |
51 | 0 | Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { |
52 | 0 | if (!context->is_col_constant(1)) [[unlikely]] { |
53 | 0 | return Status::InvalidArgument("assert_true only accept constant for 2nd argument"); |
54 | 0 | } |
55 | 0 | return Status::OK(); |
56 | 0 | } |
57 | | |
58 | 0 | DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { |
59 | 0 | return std::make_shared<DataTypeBool>(); |
60 | 0 | } |
61 | | |
62 | 0 | bool use_default_implementation_for_nulls() const final { return false; } |
63 | 0 | ColumnNumbers get_arguments_that_are_always_constant() const final { return {1}; } |
64 | | |
65 | | // column2 is const, so in default logic column1 is no way const. |
66 | | Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, |
67 | 0 | uint32_t result, size_t input_rows_count) const override { |
68 | 0 | std::string errmsg = |
69 | 0 | assert_cast<const ColumnConst&>(*block.get_by_position(arguments[1]).column) |
70 | 0 | .get_data_at(0) |
71 | 0 | .to_string(); |
72 | |
|
73 | 0 | ColumnPtr col_ptr = block.get_by_position(arguments[0]).column; |
74 | 0 | if (const auto* col_nullable = check_and_get_column<ColumnNullable>(col_ptr.get())) { |
75 | 0 | if (col_nullable->has_null()) { |
76 | 0 | throw doris::Exception(Status::InvalidArgument(errmsg)); |
77 | 0 | } |
78 | 0 | col_ptr = col_nullable->get_nested_column_ptr(); |
79 | 0 | } |
80 | 0 | const auto& data = assert_cast<const ColumnBool*>(col_ptr.get())->get_data(); |
81 | |
|
82 | 0 | for (int i = 0; i < input_rows_count; i++) { |
83 | 0 | if (!data[i]) [[unlikely]] { |
84 | 0 | throw doris::Exception(Status::InvalidArgument(errmsg)); |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | 0 | block.replace_by_position(result, ColumnBool::create(input_rows_count, true)); |
89 | 0 | return Status::OK(); |
90 | 0 | } |
91 | | }; |
92 | | |
93 | 7 | void register_function_assert_true(SimpleFunctionFactory& factory) { |
94 | 7 | factory.register_function<FunctionAssertTrue>(); |
95 | 7 | } |
96 | | } // namespace doris |