forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvcase_expr.cpp
More file actions
149 lines (129 loc) · 5.13 KB
/
Copy pathvcase_expr.cpp
File metadata and controls
149 lines (129 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "exprs/vcase_expr.h"
#include <gen_cpp/Exprs_types.h>
#include <gen_cpp/Types_types.h>
#include <ostream>
#include "common/status.h"
#include "core/block/block.h"
#include "core/block/column_with_type_and_name.h"
#include "core/block/columns_with_type_and_name.h"
#include "core/column/column.h"
#include "exec/common/util.hpp"
#include "exprs/aggregate/aggregate_function.h"
#include "exprs/vexpr_context.h"
#include "runtime/runtime_state.h"
namespace doris {
class RowDescriptor;
class RuntimeState;
} // namespace doris
namespace doris {
VCaseExpr::VCaseExpr(const TExprNode& node)
: VExpr(node), _has_else_expr(node.case_expr.has_else_expr) {}
Status VCaseExpr::prepare(RuntimeState* state, const RowDescriptor& desc, VExprContext* context) {
RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context));
ColumnsWithTypeAndName argument_template;
DataTypes arguments;
for (auto child : _children) {
argument_template.emplace_back(nullptr, child->data_type(), child->expr_name());
arguments.emplace_back(child->data_type());
}
VExpr::register_function_context(state, context);
_prepare_finished = true;
return Status::OK();
}
Status VCaseExpr::open(RuntimeState* state, VExprContext* context,
FunctionContext::FunctionStateScope scope) {
DCHECK(_prepare_finished);
for (auto& i : _children) {
RETURN_IF_ERROR(i->open(state, context, scope));
}
if (scope == FunctionContext::FRAGMENT_LOCAL) {
RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
}
_open_finished = true;
return Status::OK();
}
void VCaseExpr::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
DCHECK(_prepare_finished);
VExpr::close(context, scope);
}
Status VCaseExpr::execute_column_impl(VExprContext* context, const Block* block,
const Selector* selector, size_t count,
ColumnPtr& result_column) const {
if (is_const_and_have_executed()) { // const have execute in open function
result_column = get_result_from_const(count);
return Status::OK();
}
DCHECK(_open_finished || block == nullptr);
size_t rows_count = count;
std::vector<ColumnPtr> when_columns;
std::vector<ColumnPtr> then_columns;
if (_has_else_expr) {
ColumnPtr else_column_ptr;
RETURN_IF_ERROR(
_children.back()->execute_column(context, block, selector, count, else_column_ptr));
then_columns.emplace_back(else_column_ptr);
} else {
then_columns.emplace_back(nullptr);
}
for (int i = 0; i < _children.size() - _has_else_expr; i += 2) {
ColumnPtr when_column_ptr;
RETURN_IF_ERROR(
_children[i]->execute_column(context, block, selector, count, when_column_ptr));
if (calculate_false_number(when_column_ptr) == rows_count) {
continue;
}
when_columns.emplace_back(when_column_ptr);
ColumnPtr then_column_ptr;
RETURN_IF_ERROR(
_children[i + 1]->execute_column(context, block, selector, count, then_column_ptr));
then_columns.emplace_back(then_column_ptr);
}
if (then_columns.size() > UINT16_MAX) {
return Status::NotSupported(
"case when do not support more than UINT16_MAX then conditions");
} else if (then_columns.size() > UINT8_MAX) {
result_column = _execute_impl<uint16_t>(when_columns, then_columns, rows_count);
} else {
result_column = _execute_impl<uint8_t>(when_columns, then_columns, rows_count);
}
if (result_column->size() != count) {
return Status::InternalError("case when result column size {} not equal input count {}",
result_column->size(), count);
}
return Status::OK();
}
const std::string& VCaseExpr::expr_name() const {
return EXPR_NAME;
}
std::string VCaseExpr::debug_string() const {
std::stringstream out;
out << "CaseExpr(has_else_expr=" << _has_else_expr << " function=" << FUNCTION_NAME << "){";
bool first = true;
for (const auto& input_expr : children()) {
if (first) {
first = false;
} else {
out << ",";
}
out << input_expr->debug_string();
}
out << "}";
return out.str();
}
} // namespace doris