Coverage Report

Created: 2026-10-29 21:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
2
        : 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_impl(VExprContext* context, const Block* block,
80
                                      const Selector* selector, size_t count,
81
0
                                      ColumnPtr& result_column) const {
82
0
    if (is_const_and_have_executed()) { // const have execute in open function
83
0
        result_column = get_result_from_const(count);
84
0
        return Status::OK();
85
0
    }
86
0
    DCHECK(_open_finished || block == nullptr);
87
88
0
    size_t rows_count = count;
89
0
    std::vector<ColumnPtr> when_columns;
90
0
    std::vector<ColumnPtr> then_columns;
91
92
0
    if (_has_else_expr) {
93
0
        ColumnPtr else_column_ptr;
94
0
        RETURN_IF_ERROR(
95
0
                _children.back()->execute_column(context, block, selector, count, else_column_ptr));
96
0
        then_columns.emplace_back(else_column_ptr);
97
0
    } else {
98
0
        then_columns.emplace_back(nullptr);
99
0
    }
100
101
0
    for (int i = 0; i < _children.size() - _has_else_expr; i += 2) {
102
0
        ColumnPtr when_column_ptr;
103
0
        RETURN_IF_ERROR(
104
0
                _children[i]->execute_column(context, block, selector, count, when_column_ptr));
105
0
        if (calculate_false_number(when_column_ptr) == rows_count) {
106
0
            continue;
107
0
        }
108
0
        when_columns.emplace_back(when_column_ptr);
109
0
        ColumnPtr then_column_ptr;
110
0
        RETURN_IF_ERROR(
111
0
                _children[i + 1]->execute_column(context, block, selector, count, then_column_ptr));
112
0
        then_columns.emplace_back(then_column_ptr);
113
0
    }
114
115
0
    if (then_columns.size() > UINT16_MAX) {
116
0
        return Status::NotSupported(
117
0
                "case when do not support more than UINT16_MAX then conditions");
118
0
    } else if (then_columns.size() > UINT8_MAX) {
119
0
        result_column = _execute_impl<uint16_t>(when_columns, then_columns, rows_count);
120
0
    } else {
121
0
        result_column = _execute_impl<uint8_t>(when_columns, then_columns, rows_count);
122
0
    }
123
0
    DCHECK_EQ(result_column->size(), count);
124
0
    return Status::OK();
125
0
}
126
127
2
const std::string& VCaseExpr::expr_name() const {
128
2
    return EXPR_NAME;
129
2
}
130
131
0
std::string VCaseExpr::debug_string() const {
132
0
    std::stringstream out;
133
0
    out << "CaseExpr(has_else_expr=" << _has_else_expr << " function=" << FUNCTION_NAME << "){";
134
0
    bool first = true;
135
0
    for (const auto& input_expr : children()) {
136
0
        if (first) {
137
0
            first = false;
138
0
        } else {
139
0
            out << ",";
140
0
        }
141
0
        out << input_expr->debug_string();
142
0
    }
143
0
    out << "}";
144
0
    return out.str();
145
0
}
146
147
#include "common/compile_check_end.h"
148
} // namespace doris