/root/doris/be/src/exprs/vcase_expr.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 "exprs/vcase_expr.h" |
19 | | |
20 | | #include <gen_cpp/Exprs_types.h> |
21 | | #include <gen_cpp/Types_types.h> |
22 | | |
23 | | #include <ostream> |
24 | | |
25 | | #include "common/status.h" |
26 | | #include "core/block/block.h" |
27 | | #include "core/block/column_with_type_and_name.h" |
28 | | #include "core/block/columns_with_type_and_name.h" |
29 | | #include "core/column/column.h" |
30 | | #include "exec/common/util.hpp" |
31 | | #include "exprs/aggregate/aggregate_function.h" |
32 | | #include "exprs/vexpr_context.h" |
33 | | #include "runtime/runtime_state.h" |
34 | | |
35 | | namespace doris { |
36 | | class RowDescriptor; |
37 | | class RuntimeState; |
38 | | } // namespace doris |
39 | | |
40 | | namespace doris { |
41 | | #include "common/compile_check_begin.h" |
42 | | |
43 | | VCaseExpr::VCaseExpr(const TExprNode& node) |
44 | 0 | : VExpr(node), _has_else_expr(node.case_expr.has_else_expr) {} |
45 | | |
46 | 0 | Status VCaseExpr::prepare(RuntimeState* state, const RowDescriptor& desc, VExprContext* context) { |
47 | 0 | RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context)); |
48 | |
|
49 | 0 | ColumnsWithTypeAndName argument_template; |
50 | 0 | DataTypes arguments; |
51 | 0 | for (auto child : _children) { |
52 | 0 | argument_template.emplace_back(nullptr, child->data_type(), child->expr_name()); |
53 | 0 | arguments.emplace_back(child->data_type()); |
54 | 0 | } |
55 | |
|
56 | 0 | VExpr::register_function_context(state, context); |
57 | 0 | _prepare_finished = true; |
58 | 0 | return Status::OK(); |
59 | 0 | } |
60 | | |
61 | | Status VCaseExpr::open(RuntimeState* state, VExprContext* context, |
62 | 0 | FunctionContext::FunctionStateScope scope) { |
63 | 0 | DCHECK(_prepare_finished); |
64 | 0 | for (auto& i : _children) { |
65 | 0 | RETURN_IF_ERROR(i->open(state, context, scope)); |
66 | 0 | } |
67 | 0 | if (scope == FunctionContext::FRAGMENT_LOCAL) { |
68 | 0 | RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr)); |
69 | 0 | } |
70 | 0 | _open_finished = true; |
71 | 0 | return Status::OK(); |
72 | 0 | } |
73 | | |
74 | 0 | void VCaseExpr::close(VExprContext* context, FunctionContext::FunctionStateScope scope) { |
75 | 0 | DCHECK(_prepare_finished); |
76 | 0 | VExpr::close(context, scope); |
77 | 0 | } |
78 | | |
79 | | Status VCaseExpr::execute_column(VExprContext* context, const Block* block, Selector* selector, |
80 | 0 | size_t count, ColumnPtr& result_column) const { |
81 | 0 | if (is_const_and_have_executed()) { // const have execute in open function |
82 | 0 | result_column = get_result_from_const(count); |
83 | 0 | return Status::OK(); |
84 | 0 | } |
85 | 0 | DCHECK(_open_finished || block == nullptr); |
86 | |
|
87 | 0 | size_t rows_count = count; |
88 | 0 | std::vector<ColumnPtr> when_columns; |
89 | 0 | std::vector<ColumnPtr> then_columns; |
90 | |
|
91 | 0 | if (_has_else_expr) { |
92 | 0 | ColumnPtr else_column_ptr; |
93 | 0 | RETURN_IF_ERROR( |
94 | 0 | _children.back()->execute_column(context, block, selector, count, else_column_ptr)); |
95 | 0 | then_columns.emplace_back(else_column_ptr); |
96 | 0 | } else { |
97 | 0 | then_columns.emplace_back(nullptr); |
98 | 0 | } |
99 | | |
100 | 0 | for (int i = 0; i < _children.size() - _has_else_expr; i += 2) { |
101 | 0 | ColumnPtr when_column_ptr; |
102 | 0 | RETURN_IF_ERROR( |
103 | 0 | _children[i]->execute_column(context, block, selector, count, when_column_ptr)); |
104 | 0 | if (calculate_false_number(when_column_ptr) == rows_count) { |
105 | 0 | continue; |
106 | 0 | } |
107 | 0 | when_columns.emplace_back(when_column_ptr); |
108 | 0 | ColumnPtr then_column_ptr; |
109 | 0 | RETURN_IF_ERROR( |
110 | 0 | _children[i + 1]->execute_column(context, block, selector, count, then_column_ptr)); |
111 | 0 | then_columns.emplace_back(then_column_ptr); |
112 | 0 | } |
113 | | |
114 | 0 | if (then_columns.size() > UINT16_MAX) { |
115 | 0 | return Status::NotSupported( |
116 | 0 | "case when do not support more than UINT16_MAX then conditions"); |
117 | 0 | } else if (then_columns.size() > UINT8_MAX) { |
118 | 0 | result_column = _execute_impl<uint16_t>(when_columns, then_columns, rows_count); |
119 | 0 | } else { |
120 | 0 | result_column = _execute_impl<uint8_t>(when_columns, then_columns, rows_count); |
121 | 0 | } |
122 | 0 | if (result_column->size() != count) { |
123 | 0 | return Status::InternalError("case when result column size {} not equal input count {}", |
124 | 0 | result_column->size(), count); |
125 | 0 | } |
126 | 0 | return Status::OK(); |
127 | 0 | } |
128 | | |
129 | 0 | const std::string& VCaseExpr::expr_name() const { |
130 | 0 | return EXPR_NAME; |
131 | 0 | } |
132 | | |
133 | 0 | std::string VCaseExpr::debug_string() const { |
134 | 0 | std::stringstream out; |
135 | 0 | out << "CaseExpr(has_else_expr=" << _has_else_expr << " function=" << FUNCTION_NAME << "){"; |
136 | 0 | bool first = true; |
137 | 0 | for (const auto& input_expr : children()) { |
138 | 0 | if (first) { |
139 | 0 | first = false; |
140 | 0 | } else { |
141 | 0 | out << ","; |
142 | 0 | } |
143 | 0 | out << input_expr->debug_string(); |
144 | 0 | } |
145 | 0 | out << "}"; |
146 | 0 | return out.str(); |
147 | 0 | } |
148 | | |
149 | | #include "common/compile_check_end.h" |
150 | | } // namespace doris |