Coverage Report

Created: 2026-06-01 10:04

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/key_coder.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.26M
RowCursor::RowCursor() = default;
41
3.26M
RowCursor::~RowCursor() = default;
42
0
RowCursor::RowCursor(RowCursor&&) noexcept = default;
43
0
RowCursor& RowCursor::operator=(RowCursor&&) noexcept = default;
44
45
321
void RowCursor::_init_schema(TabletSchemaSPtr schema, uint32_t column_count) {
46
321
    std::vector<uint32_t> columns(column_count);
47
321
    std::iota(columns.begin(), columns.end(), 0);
48
321
    _schema.reset(new Schema(schema->columns(), columns));
49
321
}
50
51
3.25M
void RowCursor::_init_schema(const std::shared_ptr<Schema>& shared_schema, uint32_t column_count) {
52
3.25M
    _schema.reset(new Schema(*shared_schema));
53
3.25M
}
54
55
95
Status RowCursor::init(TabletSchemaSPtr schema, size_t num_columns) {
56
95
    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
95
    _init_schema(schema, cast_set<uint32_t>(num_columns));
63
    // Initialize all fields as null (TYPE_NULL).
64
95
    _fields.resize(num_columns);
65
95
    return Status::OK();
66
95
}
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.26M
                       const std::shared_ptr<Schema>& shared_schema) {
82
3.26M
    size_t key_size = tuple.size();
83
3.26M
    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.26M
    _init_schema(shared_schema, cast_set<uint32_t>(key_size));
90
3.26M
    return from_tuple(tuple);
91
3.26M
}
92
93
226
Status RowCursor::init_scan_key(TabletSchemaSPtr schema, std::vector<Field> fields) {
94
226
    size_t key_size = fields.size();
95
226
    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
226
    _init_schema(schema, cast_set<uint32_t>(key_size));
102
226
    _fields = std::move(fields);
103
226
    return Status::OK();
104
226
}
105
106
3.26M
Status RowCursor::from_tuple(const OlapTuple& tuple) {
107
3.26M
    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.26M
    _fields.resize(tuple.size());
113
10.8M
    for (size_t i = 0; i < tuple.size(); ++i) {
114
7.54M
        _fields[i] = tuple.get_field(i);
115
7.54M
    }
116
3.26M
    return Status::OK();
117
3.26M
}
118
119
0
RowCursor RowCursor::clone() const {
120
0
    RowCursor result;
121
0
    result._schema = std::make_unique<Schema>(*_schema);
122
0
    result._fields = _fields;
123
0
    return result;
124
0
}
125
126
8.98M
std::string RowCursor::to_string() const {
127
8.98M
    std::string result;
128
33.5M
    for (size_t i = 0; i < _fields.size(); ++i) {
129
24.5M
        if (i > 0) {
130
15.6M
            result.append("|");
131
15.6M
        }
132
24.5M
        if (_fields[i].is_null()) {
133
403k
            result.append("1&NULL");
134
24.1M
        } else {
135
24.1M
            result.append("0&");
136
24.1M
            result.append(
137
24.1M
                    _fields[i].to_debug_string(_schema->column(cast_set<uint32_t>(i))->frac()));
138
24.1M
        }
139
24.5M
    }
140
8.98M
    return result;
141
8.98M
}
142
143
void RowCursor::_encode_column_value(const TabletColumn* column, const Field& value,
144
13.2M
                                     bool full_encode, std::string* buf) const {
145
13.2M
    FieldType ft = column->type();
146
13.2M
    const KeyCoder* coder = get_key_coder(ft);
147
148
13.2M
    if (field_is_slice_type(ft)) {
149
        // String types: CHAR, VARCHAR, STRING — all stored as String in Field.
150
11.9M
        const String& str = value.get<TYPE_STRING>();
151
152
11.9M
        if (ft == FieldType::OLAP_FIELD_TYPE_CHAR) {
153
            // CHAR type: must pad with \0 to the declared column length
154
753
            size_t col_len = column->length();
155
753
            String padded(col_len, '\0');
156
753
            memcpy(padded.data(), str.data(), std::min(str.size(), col_len));
157
158
753
            Slice slice(padded.data(), col_len);
159
753
            if (full_encode) {
160
32
                coder->full_encode_ascending(&slice, buf);
161
721
            } else {
162
721
                coder->encode_ascending(&slice, column->index_length(), buf);
163
721
            }
164
11.9M
        } else {
165
            // VARCHAR / STRING: use actual length
166
11.9M
            Slice slice(str.data(), str.size());
167
11.9M
            if (full_encode) {
168
11.9M
                coder->full_encode_ascending(&slice, buf);
169
11.9M
            } else {
170
40.2k
                coder->encode_ascending(&slice, column->index_length(), buf);
171
40.2k
            }
172
11.9M
        }
173
11.9M
        return;
174
11.9M
    }
175
176
    // Non-string scalar keys are fixed-width; their KeyCoder::encode_ascending
177
    // ignores `index_size` and delegates to full_encode_ascending, so the
178
    // `full_encode` flag here is a no-op and we always call the full helper.
179
1.20M
    switch (ft) {
180
0
#define CASE(FT, PT)                                                    \
181
1.20M
    case FieldType::FT:                                                 \
182
1.20M
        full_encode_field_as_key<PrimitiveType::PT>(value, coder, buf); \
183
1.20M
        break;
184
1.20M
        DORIS_APPLY_FOR_KEY_ENCODABLE_NON_STRING_TYPES(CASE)
185
0
#undef CASE
186
0
    default:
187
0
        LOG(FATAL) << "unsupported field type for encoding: " << int(ft);
188
0
        break;
189
1.20M
    }
190
1.20M
}
191
192
// Encodes the first `num_keys` key columns as a memcomparable byte string.
193
// Each slot is [marker][value bytes]. The marker sits at a position that
194
// real entries fill with KEY_NORMAL_MARKER (0x02), so any byte > 0x02 there
195
// sorts strictly after every real entry — independent of the value bytes.
196
//
197
// Examples — PK (a STRING, b STRING), stored entry (foo, bar) encodes as
198
// `02 foo | 02 bar`. Calls with num_keys=2 and only partial key "foo":
199
//
200
//   padding_minimal=true                  -> 02 foo | 00          (MINIMAL)
201
//   padding_minimal=false, is_mow=false   -> 02 foo | FF          (MAXIMAL)
202
//   padding_minimal=false, is_mow=true    -> 02 foo | 03      (NORMAL_NEXT)
203
template <bool is_mow>
204
void RowCursor::encode_key_with_padding(std::string* buf, size_t num_keys,
205
3.98M
                                        bool padding_minimal) const {
206
16.9M
    for (uint32_t cid = 0; cid < num_keys; cid++) {
207
16.6M
        auto* column = _schema->column(cid);
208
16.6M
        if (column == nullptr) {
209
3.69M
            if (padding_minimal) {
210
1.69M
                buf->push_back(KeyConsts::KEY_MINIMAL_MARKER);
211
1.99M
            } else {
212
1.99M
                if (is_mow) {
213
1.68M
                    buf->push_back(KeyConsts::KEY_NORMAL_NEXT_MARKER);
214
1.68M
                } else {
215
313k
                    buf->push_back(KeyConsts::KEY_MAXIMAL_MARKER);
216
313k
                }
217
1.99M
            }
218
3.69M
            break;
219
3.69M
        }
220
221
12.9M
        if (cid >= _fields.size() || _fields[cid].is_null()) {
222
116k
            buf->push_back(KeyConsts::KEY_NULL_FIRST_MARKER);
223
116k
            continue;
224
116k
        }
225
226
12.8M
        buf->push_back(KeyConsts::KEY_NORMAL_MARKER);
227
12.8M
        _encode_column_value(column, _fields[cid], is_mow, buf);
228
12.8M
    }
229
3.98M
}
_ZNK5doris9RowCursor23encode_key_with_paddingILb0EEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEmb
Line
Count
Source
205
777k
                                        bool padding_minimal) const {
206
1.58M
    for (uint32_t cid = 0; cid < num_keys; cid++) {
207
1.36M
        auto* column = _schema->column(cid);
208
1.36M
        if (column == nullptr) {
209
557k
            if (padding_minimal) {
210
242k
                buf->push_back(KeyConsts::KEY_MINIMAL_MARKER);
211
314k
            } else {
212
314k
                if (is_mow) {
213
0
                    buf->push_back(KeyConsts::KEY_NORMAL_NEXT_MARKER);
214
314k
                } else {
215
314k
                    buf->push_back(KeyConsts::KEY_MAXIMAL_MARKER);
216
314k
                }
217
314k
            }
218
557k
            break;
219
557k
        }
220
221
806k
        if (cid >= _fields.size() || _fields[cid].is_null()) {
222
100k
            buf->push_back(KeyConsts::KEY_NULL_FIRST_MARKER);
223
100k
            continue;
224
100k
        }
225
226
703k
        buf->push_back(KeyConsts::KEY_NORMAL_MARKER);
227
703k
        _encode_column_value(column, _fields[cid], is_mow, buf);
228
703k
    }
229
777k
}
_ZNK5doris9RowCursor23encode_key_with_paddingILb1EEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEmb
Line
Count
Source
205
3.20M
                                        bool padding_minimal) const {
206
15.3M
    for (uint32_t cid = 0; cid < num_keys; cid++) {
207
15.3M
        auto* column = _schema->column(cid);
208
15.3M
        if (column == nullptr) {
209
3.13M
            if (padding_minimal) {
210
1.45M
                buf->push_back(KeyConsts::KEY_MINIMAL_MARKER);
211
1.68M
            } else {
212
1.68M
                if (is_mow) {
213
1.68M
                    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.68M
            }
218
3.13M
            break;
219
3.13M
        }
220
221
12.1M
        if (cid >= _fields.size() || _fields[cid].is_null()) {
222
16.3k
            buf->push_back(KeyConsts::KEY_NULL_FIRST_MARKER);
223
16.3k
            continue;
224
16.3k
        }
225
226
12.1M
        buf->push_back(KeyConsts::KEY_NORMAL_MARKER);
227
12.1M
        _encode_column_value(column, _fields[cid], is_mow, buf);
228
12.1M
    }
229
3.20M
}
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_column_value(_schema->column(cid), _fields[cid], full_encode, buf);
244
258k
    }
245
129k
}
_ZNK5doris9RowCursor10encode_keyILb0EEEvPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEm
Line
Count
Source
236
34
void RowCursor::encode_key(std::string* buf, size_t num_keys) const {
237
83
    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_column_value(_schema->column(cid), _fields[cid], full_encode, buf);
244
43
    }
245
34
}
_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_column_value(_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