Coverage Report

Created: 2026-05-19 12:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
42
VCaseExpr::VCaseExpr(const TExprNode& node)
43
18
        : VExpr(node), _has_else_expr(node.case_expr.has_else_expr) {}
44
45
18
Status VCaseExpr::prepare(RuntimeState* state, const RowDescriptor& desc, VExprContext* context) {
46
18
    RETURN_IF_ERROR_OR_PREPARED(VExpr::prepare(state, desc, context));
47
48
18
    ColumnsWithTypeAndName argument_template;
49
18
    DataTypes arguments;
50
170
    for (auto child : _children) {
51
170
        argument_template.emplace_back(nullptr, child->data_type(), child->expr_name());
52
170
        arguments.emplace_back(child->data_type());
53
170
    }
54
55
18
    VExpr::register_function_context(state, context);
56
18
    _prepare_finished = true;
57
18
    return Status::OK();
58
18
}
59
60
Status VCaseExpr::open(RuntimeState* state, VExprContext* context,
61
82
                       FunctionContext::FunctionStateScope scope) {
62
82
    DCHECK(_prepare_finished);
63
706
    for (auto& i : _children) {
64
706
        RETURN_IF_ERROR(i->open(state, context, scope));
65
706
    }
66
82
    if (scope == FunctionContext::FRAGMENT_LOCAL) {
67
18
        RETURN_IF_ERROR(VExpr::get_const_col(context, nullptr));
68
18
    }
69
82
    _open_finished = true;
70
82
    return Status::OK();
71
82
}
72
73
82
void VCaseExpr::close(VExprContext* context, FunctionContext::FunctionStateScope scope) {
74
82
    DCHECK(_prepare_finished);
75
82
    VExpr::close(context, scope);
76
82
}
77
78
Status VCaseExpr::execute_column_impl(VExprContext* context, const Block* block,
79
                                      const Selector* selector, size_t count,
80
32
                                      ColumnPtr& result_column) const {
81
32
    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
32
    DCHECK(_open_finished || block == nullptr);
86
87
32
    size_t rows_count = count;
88
32
    std::vector<ColumnPtr> when_columns;
89
32
    std::vector<ColumnPtr> then_columns;
90
91
32
    if (_has_else_expr) {
92
32
        ColumnPtr else_column_ptr;
93
32
        RETURN_IF_ERROR(
94
32
                _children.back()->execute_column(context, block, selector, count, else_column_ptr));
95
32
        then_columns.emplace_back(else_column_ptr);
96
32
    } else {
97
0
        then_columns.emplace_back(nullptr);
98
0
    }
99
100
150
    for (int i = 0; i < _children.size() - _has_else_expr; i += 2) {
101
118
        ColumnPtr when_column_ptr;
102
118
        RETURN_IF_ERROR(
103
118
                _children[i]->execute_column(context, block, selector, count, when_column_ptr));
104
118
        if (calculate_false_number(when_column_ptr) == rows_count) {
105
58
            continue;
106
58
        }
107
60
        when_columns.emplace_back(when_column_ptr);
108
60
        ColumnPtr then_column_ptr;
109
60
        RETURN_IF_ERROR(
110
60
                _children[i + 1]->execute_column(context, block, selector, count, then_column_ptr));
111
60
        then_columns.emplace_back(then_column_ptr);
112
60
    }
113
114
32
    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
32
    } else if (then_columns.size() > UINT8_MAX) {
118
0
        result_column = _execute_impl<uint16_t>(when_columns, then_columns, rows_count);
119
32
    } else {
120
32
        result_column = _execute_impl<uint8_t>(when_columns, then_columns, rows_count);
121
32
    }
122
32
    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
32
    return Status::OK();
127
32
}
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
} // namespace doris