Coverage Report

Created: 2026-04-13 11:59

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