Coverage Report

Created: 2026-05-16 12:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/row_cursor.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 "storage/row_cursor.h"
19
20
#include <glog/logging.h>
21
22
#include <algorithm>
23
#include <numeric>
24
#include <ostream>
25
26
#include "common/cast_set.h"
27
#include "common/consts.h"
28
#include "core/data_type/primitive_type.h"
29
#include "core/field.h"
30
#include "storage/field.h"
31
#include "storage/olap_common.h"
32
#include "storage/olap_define.h"
33
#include "storage/tablet/tablet_schema.h"
34
#include "storage/types.h"
35
#include "util/slice.h"
36
37
namespace doris {
38
using namespace ErrorCode;
39
40
3.98M
RowCursor::RowCursor() = default;
41
3.98M
RowCursor::~RowCursor() = default;
42
0
RowCursor::RowCursor(RowCursor&&) noexcept = default;
43
0
RowCursor& RowCursor::operator=(RowCursor&&) noexcept = default;
44
45
362
void RowCursor::_init_schema(TabletSchemaSPtr schema, uint32_t column_count) {
46
362
    std::vector<uint32_t> columns(column_count);
47
362
    std::iota(columns.begin(), columns.end(), 0);
48
362
    _schema.reset(new Schema(schema->columns(), columns));
49
362
}
50
51
3.20M
void RowCursor::_init_schema(const std::shared_ptr<Schema>& shared_schema, uint32_t column_count) {
52
3.20M
    _schema.reset(new Schema(*shared_schema));
53
3.20M
}
54
55
94
Status RowCursor::init(TabletSchemaSPtr schema, size_t num_columns) {
56
94
    if (num_columns > schema->num_columns()) {
57
0
        return Status::Error<INVALID_ARGUMENT>(
58
0
                "Input param are invalid. Column count is bigger than num_columns of schema. "
59
0
                "column_count={}, schema.num_columns={}",
60
0
                num_columns, schema->num_columns());
61
0
    }
62
94
    _init_schema(schema, cast_set<uint32_t>(num_columns));
63
    // Initialize all fields as null (TYPE_NULL).
64
94
    _fields.resize(num_columns);
65
94
    return Status::OK();
66
94
}
67
68
0
Status RowCursor::init(TabletSchemaSPtr schema, const OlapTuple& tuple) {
69
0
    size_t key_size = tuple.size();
70
0
    if (key_size > schema->num_columns()) {
71
0
        return Status::Error<INVALID_ARGUMENT>(
72
0
                "Input param are invalid. Column count is bigger than num_columns of schema. "
73
0
                "column_count={}, schema.num_columns={}",
74
0
                key_size, schema->num_columns());
75
0
    }
76
0
    _init_schema(schema, cast_set<uint32_t>(key_size));
77
0
    return from_tuple(tuple);
78
0
}
79
80
Status RowCursor::init(TabletSchemaSPtr schema, const OlapTuple& tuple,
81
3.21M
                       const std::shared_ptr<Schema>& shared_schema) {
82
3.21M
    size_t key_size = tuple.size();
83
3.21M
    if (key_size > schema->num_columns()) {
84
0
        return Status::Error<INVALID_ARGUMENT>(
85
0
                "Input param are invalid. Column count is bigger than num_columns of schema. "
86
0
                "column_count={}, schema.num_columns={}",
87
0
                key_size, schema->num_columns());
88
0
    }
89
3.21M
    _init_schema(shared_schema, cast_set<uint32_t>(key_size));
90
3.21M
    return from_tuple(tuple);
91
3.21M
}
92
93
268
Status RowCursor::init_scan_key(TabletSchemaSPtr schema, std::vector<Field> fields) {
94
268
    size_t key_size = fields.size();
95
268
    if (key_size > schema->num_columns()) {
96
0
        return Status::Error<INVALID_ARGUMENT>(
97
0
                "Input param are invalid. Column count is bigger than num_columns of schema. "
98
0
                "column_count={}, schema.num_columns={}",
99
0
                key_size, schema->num_columns());
100
0
    }
101
268
    _init_schema(schema, cast_set<uint32_t>(key_size));
102
268
    _fields = std::move(fields);
103
268
    return Status::OK();
104
268
}
105
106
3.21M
Status RowCursor::from_tuple(const OlapTuple& tuple) {
107
3.21M
    if (tuple.size() != _schema->num_column_ids()) {
108
0
        return Status::Error<INVALID_ARGUMENT>(
109
0
                "column count does not match. tuple_size={}, field_count={}", tuple.size(),
110
0
                _schema->num_column_ids());
111
0
    }
112
3.21M
    _fields.resize(tuple.size());
113
10.6M
    for (size_t i = 0; i < tuple.size(); ++i) {
114
7.44M
        _fields[i] = tuple.get_field(i);
115
7.44M
    }
116
3.21M
    return Status::OK();
117
3.21M
}
118
119
764k
RowCursor RowCursor::clone() const {
120
764k
    RowCursor result;
121
764k
    result._schema = std::make_unique<Schema>(*_schema);
122
764k
    result._fields = _fields;
123
764k
    return result;
124
764k
}
125
126
765k
void RowCursor::pad_char_fields() {
127
1.54M
    for (size_t i = 0; i < _fields.size(); ++i) {
128
776k
        const StorageField* col = _schema->column(cast_set<uint32_t>(i));
129
776k
        if (col->type() == FieldType::OLAP_FIELD_TYPE_CHAR && !_fields[i].is_null()) {
130
724
            String padded = _fields[i].get<TYPE_CHAR>();
131
724
            padded.resize(col->length(), '\0');
132
724
            _fields[i] = Field::create_field<TYPE_CHAR>(std::move(padded));
133
724
        }
134
776k
    }
135
765k
}
136
137
8.14M
std::string RowCursor::to_string() const {
138
8.14M
    std::string result;
139
29.7M
    for (size_t i = 0; i < _fields.size(); ++i) {
140
21.5M
        if (i > 0) {
141
13.4M
            result.append("|");
142
13.4M
        }
143
21.5M
        if (_fields[i].is_null()) {
144
430k
            result.append("1&NULL");
145
21.1M
        } else {
146
21.1M
            result.append("0&");
147
21.1M
            result.append(_fields[i].to_debug_string(
148
21.1M
                    _schema->column(cast_set<uint32_t>(i))->get_scale()));
149
21.1M
        }
150
21.5M
    }
151
8.14M
    return result;
152
8.14M
}
153
154
void RowCursor::_encode_field(const StorageField* storage_field, const Field& f, bool full_encode,
155
11.4M
                              std::string* buf) const {
156
11.4M
    FieldType ft = storage_field->type();
157
158
11.4M
    if (field_is_slice_type(ft)) {
159
        // String types: CHAR, VARCHAR, STRING — all stored as String in Field.
160
10.2M
        const String& str = f.get<TYPE_STRING>();
161
162
10.2M
        if (ft == FieldType::OLAP_FIELD_TYPE_CHAR) {
163
            // CHAR type: must pad with \0 to the declared column length
164
759
            size_t col_len = storage_field->length();
165
759
            String padded(col_len, '\0');
166
759
            memcpy(padded.data(), str.data(), std::min(str.size(), col_len));
167
168
759
            Slice slice(padded.data(), col_len);
169
759
            if (full_encode) {
170
32
                storage_field->full_encode_ascending(&slice, buf);
171
727
            } else {
172
727
                storage_field->encode_ascending(&slice, buf);
173
727
            }
174
10.2M
        } else {
175
            // VARCHAR / STRING: use actual length
176
10.2M
            Slice slice(str.data(), str.size());
177
10.2M
            if (full_encode) {
178
10.2M
                storage_field->full_encode_ascending(&slice, buf);
179
10.2M
            } else {
180
16.8k
                storage_field->encode_ascending(&slice, buf);
181
16.8k
            }
182
10.2M
        }
183
10.2M
        return;
184
10.2M
    }
185
186
    // Non-string scalar keys are fixed-width; their KeyCoder::encode_ascending
187
    // ignores `index_size` and delegates to full_encode_ascending, so the
188
    // `full_encode` flag here is a no-op and we always call the full helper.
189
1.14M
    const KeyCoder* coder = storage_field->key_coder();
190
1.14M
    switch (ft) {
191
0
#define CASE(FT, PT)                                                \
192
1.14M
    case FieldType::FT:                                             \
193
1.14M
        full_encode_field_as_key<PrimitiveType::PT>(f, coder, buf); \
194
1.14M
        break;
195
1.14M
        DORIS_APPLY_FOR_KEY_ENCODABLE_NON_STRING_TYPES(CASE)
196
0
#undef CASE
197
0
    default:
198
0
        LOG(FATAL) << "unsupported field type for encoding: " << int(ft);
199
0
        break;
200
1.14M
    }
201
1.14M
}
202
203
template <bool is_mow>
204
void RowCursor::encode_key_with_padding(std::string* buf, size_t num_keys,
205
3.50M
                                        bool padding_minimal) const {
206
14.7M
    for (uint32_t cid = 0; cid < num_keys; cid++) {
207
14.4M
        auto* storage_field = _schema->column(cid);
208
14.4M
        if (storage_field == nullptr) {
209
3.25M
            if (padding_minimal) {
210
1.45M
                buf->push_back(KeyConsts::KEY_MINIMAL_MARKER);
211
1.79M
            } else {
212
1.79M
                if (is_mow) {
213
1.47M
                    buf->push_back(KeyConsts::KEY_NORMAL_NEXT_MARKER);
214
1.47M
                } else {
215
318k
                    buf->push_back(KeyConsts::KEY_MAXIMAL_MARKER);
216
318k
                }
217
1.79M
            }
218
3.25M
            break;
219
3.25M
        }
220
221
11.2M
        if (cid >= _fields.size() || _fields[cid].is_null()) {
222
111k
            buf->push_back(KeyConsts::KEY_NULL_FIRST_MARKER);
223
111k
            continue;
224
111k
        }
225
226
11.1M
        buf->push_back(KeyConsts::KEY_NORMAL_MARKER);
227
11.1M
        _encode_field(storage_field, _fields[cid], is_mow, buf);
228
11.1M
    }
229
3.50M
}
_ZNK5doris9RowCursor23encode_key_with_paddingILb0EEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEmb
Line
Count
Source
205
765k
                                        bool padding_minimal) const {
206
1.53M
    for (uint32_t cid = 0; cid < num_keys; cid++) {
207
1.34M
        auto* storage_field = _schema->column(cid);
208
1.34M
        if (storage_field == nullptr) {
209
566k
            if (padding_minimal) {
210
247k
                buf->push_back(KeyConsts::KEY_MINIMAL_MARKER);
211
318k
            } else {
212
318k
                if (is_mow) {
213
0
                    buf->push_back(KeyConsts::KEY_NORMAL_NEXT_MARKER);
214
318k
                } else {
215
318k
                    buf->push_back(KeyConsts::KEY_MAXIMAL_MARKER);
216
318k
                }
217
318k
            }
218
566k
            break;
219
566k
        }
220
221
775k
        if (cid >= _fields.size() || _fields[cid].is_null()) {
222
95.5k
            buf->push_back(KeyConsts::KEY_NULL_FIRST_MARKER);
223
95.5k
            continue;
224
95.5k
        }
225
226
678k
        buf->push_back(KeyConsts::KEY_NORMAL_MARKER);
227
678k
        _encode_field(storage_field, _fields[cid], is_mow, buf);
228
678k
    }
229
765k
}
_ZNK5doris9RowCursor23encode_key_with_paddingILb1EEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEmb
Line
Count
Source
205
2.74M
                                        bool padding_minimal) const {
206
13.1M
    for (uint32_t cid = 0; cid < num_keys; cid++) {
207
13.1M
        auto* storage_field = _schema->column(cid);
208
13.1M
        if (storage_field == nullptr) {
209
2.68M
            if (padding_minimal) {
210
1.20M
                buf->push_back(KeyConsts::KEY_MINIMAL_MARKER);
211
1.47M
            } else {
212
1.47M
                if (is_mow) {
213
1.47M
                    buf->push_back(KeyConsts::KEY_NORMAL_NEXT_MARKER);
214
18.4E
                } else {
215
18.4E
                    buf->push_back(KeyConsts::KEY_MAXIMAL_MARKER);
216
18.4E
                }
217
1.47M
            }
218
2.68M
            break;
219
2.68M
        }
220
221
10.4M
        if (cid >= _fields.size() || _fields[cid].is_null()) {
222
16.0k
            buf->push_back(KeyConsts::KEY_NULL_FIRST_MARKER);
223
16.0k
            continue;
224
16.0k
        }
225
226
10.4M
        buf->push_back(KeyConsts::KEY_NORMAL_MARKER);
227
10.4M
        _encode_field(storage_field, _fields[cid], is_mow, buf);
228
10.4M
    }
229
2.74M
}
230
231
// Explicit template instantiations
232
template void RowCursor::encode_key_with_padding<false>(std::string*, size_t, bool) const;
233
template void RowCursor::encode_key_with_padding<true>(std::string*, size_t, bool) const;
234
235
template <bool full_encode>
236
129k
void RowCursor::encode_key(std::string* buf, size_t num_keys) const {
237
387k
    for (uint32_t cid = 0; cid < num_keys; cid++) {
238
258k
        if (cid >= _fields.size() || _fields[cid].is_null()) {
239
6
            buf->push_back(KeyConsts::KEY_NULL_FIRST_MARKER);
240
6
            continue;
241
6
        }
242
258k
        buf->push_back(KeyConsts::KEY_NORMAL_MARKER);
243
258k
        _encode_field(_schema->column(cid), _fields[cid], full_encode, buf);
244
258k
    }
245
129k
}
_ZNK5doris9RowCursor10encode_keyILb0EEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEm
Line
Count
Source
236
33
void RowCursor::encode_key(std::string* buf, size_t num_keys) const {
237
82
    for (uint32_t cid = 0; cid < num_keys; cid++) {
238
49
        if (cid >= _fields.size() || _fields[cid].is_null()) {
239
6
            buf->push_back(KeyConsts::KEY_NULL_FIRST_MARKER);
240
6
            continue;
241
6
        }
242
43
        buf->push_back(KeyConsts::KEY_NORMAL_MARKER);
243
43
        _encode_field(_schema->column(cid), _fields[cid], full_encode, buf);
244
43
    }
245
33
}
_ZNK5doris9RowCursor10encode_keyILb1EEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEm
Line
Count
Source
236
129k
void RowCursor::encode_key(std::string* buf, size_t num_keys) const {
237
387k
    for (uint32_t cid = 0; cid < num_keys; cid++) {
238
258k
        if (cid >= _fields.size() || _fields[cid].is_null()) {
239
0
            buf->push_back(KeyConsts::KEY_NULL_FIRST_MARKER);
240
0
            continue;
241
0
        }
242
258k
        buf->push_back(KeyConsts::KEY_NORMAL_MARKER);
243
258k
        _encode_field(_schema->column(cid), _fields[cid], full_encode, buf);
244
258k
    }
245
129k
}
246
247
template void RowCursor::encode_key<false>(std::string*, size_t) const;
248
template void RowCursor::encode_key<true>(std::string*, size_t) const;
249
250
} // namespace doris