Coverage Report

Created: 2026-05-21 19:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/row_cursor.h
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
#pragma once
19
20
#include <butil/macros.h>
21
#include <stddef.h>
22
#include <stdint.h>
23
24
#include <memory>
25
#include <string>
26
#include <vector>
27
28
#include "common/consts.h"
29
#include "common/status.h"
30
#include "core/data_type/primitive_type.h"
31
#include "core/field.h"
32
#include "storage/olap_tuple.h"
33
#include "storage/schema.h"
34
#include "storage/tablet/tablet_schema.h"
35
36
namespace doris {
37
38
// Delegate the operation of a row of data.
39
// Stores values as core::Field objects instead of raw byte buffers.
40
class RowCursor {
41
public:
42
    RowCursor();
43
    ~RowCursor();
44
    RowCursor(const RowCursor&) = delete;
45
    RowCursor& operator=(const RowCursor&) = delete;
46
    RowCursor(RowCursor&&) noexcept;
47
    RowCursor& operator=(RowCursor&&) noexcept;
48
49
    // Initialize from OlapTuple (which now stores Fields).
50
    // Sets up the schema and copies Fields from the tuple.
51
    Status init(TabletSchemaSPtr schema, const OlapTuple& tuple);
52
    Status init(TabletSchemaSPtr schema, const OlapTuple& tuple,
53
                const std::shared_ptr<Schema>& shared_schema);
54
55
    // Initialize with schema and num_columns, creating null Fields.
56
    // Caller sets individual fields via mutable_field().
57
    Status init(TabletSchemaSPtr schema, size_t num_columns);
58
59
    // Initialize from typed Fields directly.
60
    Status init_scan_key(TabletSchemaSPtr schema, std::vector<Field> fields);
61
62
651k
    const Field& field(uint32_t cid) const { return _fields[cid]; }
63
651k
    Field& mutable_field(uint32_t cid) { return _fields[cid]; }
64
65
1
    size_t field_count() const { return _fields.size(); }
66
67
0
    const TabletColumn* column(uint32_t cid) const { return _schema->column(cid); }
68
651k
    const Schema* schema() const { return _schema.get(); }
69
70
    // Returns a deep copy of this RowCursor with the same schema and field values.
71
    RowCursor clone() const;
72
73
    // Pad all CHAR-type fields in-place to their declared column length using '\0'.
74
    // RowCursor holds CHAR values in compute format (unpadded). Call this before
75
    // comparing against storage-format data (e.g. _seek_block) where CHAR is padded.
76
    void pad_char_fields();
77
78
    // Output row cursor content in string format
79
    std::string to_string() const;
80
81
    // Encode one row into binary according given num_keys.
82
    // Internally converts each core::Field to its storage representation via
83
    // PrimitiveTypeConvertor before passing to KeyCoder.
84
    // CHAR fields are zero-padded to column.length() for encoding.
85
    template <bool is_mow = false>
86
    void encode_key_with_padding(std::string* buf, size_t num_keys, bool padding_minimal) const;
87
88
    // Encode one row into binary according given num_keys.
89
    // Client must ensure that row contains the first num_keys columns.
90
    template <bool full_encode = false>
91
    void encode_key(std::string* buf, size_t num_keys) const;
92
93
    // Encode a single field at column index 'cid' into 'buf'.
94
64.4k
    void encode_single_field(uint32_t cid, std::string* buf, bool full_encode) const {
95
64.4k
        const auto& f = _fields[cid];
96
        DCHECK(!f.is_null());
97
64.4k
        _encode_column_value(_schema->column(cid), f, full_encode, buf);
98
64.4k
    }
99
100
private:
101
    // Copy Fields from an OlapTuple into this cursor.
102
    Status from_tuple(const OlapTuple& tuple);
103
104
    void _init_schema(TabletSchemaSPtr schema, uint32_t column_count);
105
    void _init_schema(const std::shared_ptr<Schema>& shared_schema, uint32_t column_count);
106
107
    // Helper: encode a single non-null field for the given column.
108
    // Converts the core::Field to storage format and calls KeyCoder.
109
    void _encode_column_value(const TabletColumn* column, const Field& value, bool full_encode,
110
                              std::string* buf) const;
111
112
    std::unique_ptr<Schema> _schema;
113
    std::vector<Field> _fields;
114
};
115
} // namespace doris