|
| 1 | +/* |
| 2 | + * Copyright 2026-present Alibaba Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#include <cstdint> |
| 20 | +#include <memory> |
| 21 | +#include <string> |
| 22 | +#include <string_view> |
| 23 | + |
| 24 | +#include "fmt/format.h" |
| 25 | +#include "paimon/common/data/binary_string.h" |
| 26 | +#include "paimon/common/data/columnar/columnar_batch_context.h" |
| 27 | +#include "paimon/common/data/columnar/columnar_utils.h" |
| 28 | +#include "paimon/common/data/internal_array.h" |
| 29 | +#include "paimon/common/data/internal_map.h" |
| 30 | +#include "paimon/common/data/internal_row.h" |
| 31 | +#include "paimon/common/types/row_kind.h" |
| 32 | +#include "paimon/data/decimal.h" |
| 33 | +#include "paimon/data/timestamp.h" |
| 34 | +#include "paimon/result.h" |
| 35 | + |
| 36 | +namespace paimon { |
| 37 | +class Bytes; |
| 38 | + |
| 39 | +/// Columnar row view which shares batch-level context to reduce per-row overhead. |
| 40 | +class ColumnarRowRef : public InternalRow { |
| 41 | + public: |
| 42 | + ColumnarRowRef(std::shared_ptr<ColumnarBatchContext> ctx, int64_t row_id) |
| 43 | + : ctx_(std::move(ctx)), row_id_(row_id) {} |
| 44 | + |
| 45 | + Result<const RowKind*> GetRowKind() const override { |
| 46 | + return row_kind_; |
| 47 | + } |
| 48 | + |
| 49 | + void SetRowKind(const RowKind* kind) override { |
| 50 | + row_kind_ = kind; |
| 51 | + } |
| 52 | + |
| 53 | + int32_t GetFieldCount() const override { |
| 54 | + return static_cast<int32_t>(ctx_->array_ptrs.size()); |
| 55 | + } |
| 56 | + |
| 57 | + bool IsNullAt(int32_t pos) const override { |
| 58 | + return ctx_->array_ptrs[pos]->IsNull(row_id_); |
| 59 | + } |
| 60 | + |
| 61 | + bool GetBoolean(int32_t pos) const override { |
| 62 | + return ColumnarUtils::GetGenericValue<arrow::BooleanType, bool>(ctx_->array_ptrs[pos], |
| 63 | + row_id_); |
| 64 | + } |
| 65 | + |
| 66 | + char GetByte(int32_t pos) const override { |
| 67 | + return ColumnarUtils::GetGenericValue<arrow::Int8Type, char>(ctx_->array_ptrs[pos], |
| 68 | + row_id_); |
| 69 | + } |
| 70 | + |
| 71 | + int16_t GetShort(int32_t pos) const override { |
| 72 | + return ColumnarUtils::GetGenericValue<arrow::Int16Type, int16_t>(ctx_->array_ptrs[pos], |
| 73 | + row_id_); |
| 74 | + } |
| 75 | + |
| 76 | + int32_t GetInt(int32_t pos) const override { |
| 77 | + return ColumnarUtils::GetGenericValue<arrow::Int32Type, int32_t>(ctx_->array_ptrs[pos], |
| 78 | + row_id_); |
| 79 | + } |
| 80 | + |
| 81 | + int32_t GetDate(int32_t pos) const override { |
| 82 | + return ColumnarUtils::GetGenericValue<arrow::Date32Type, int32_t>(ctx_->array_ptrs[pos], |
| 83 | + row_id_); |
| 84 | + } |
| 85 | + |
| 86 | + int64_t GetLong(int32_t pos) const override { |
| 87 | + return ColumnarUtils::GetGenericValue<arrow::Int64Type, int64_t>(ctx_->array_ptrs[pos], |
| 88 | + row_id_); |
| 89 | + } |
| 90 | + |
| 91 | + float GetFloat(int32_t pos) const override { |
| 92 | + return ColumnarUtils::GetGenericValue<arrow::FloatType, float>(ctx_->array_ptrs[pos], |
| 93 | + row_id_); |
| 94 | + } |
| 95 | + |
| 96 | + double GetDouble(int32_t pos) const override { |
| 97 | + return ColumnarUtils::GetGenericValue<arrow::DoubleType, double>(ctx_->array_ptrs[pos], |
| 98 | + row_id_); |
| 99 | + } |
| 100 | + |
| 101 | + BinaryString GetString(int32_t pos) const override { |
| 102 | + auto bytes = ColumnarUtils::GetBytes<arrow::StringType>(ctx_->array_ptrs[pos], row_id_, |
| 103 | + ctx_->pool.get()); |
| 104 | + return BinaryString::FromBytes(bytes); |
| 105 | + } |
| 106 | + |
| 107 | + std::string_view GetStringView(int32_t pos) const override { |
| 108 | + return ColumnarUtils::GetView(ctx_->array_ptrs[pos], row_id_); |
| 109 | + } |
| 110 | + |
| 111 | + Decimal GetDecimal(int32_t pos, int32_t precision, int32_t scale) const override; |
| 112 | + |
| 113 | + Timestamp GetTimestamp(int32_t pos, int32_t precision) const override; |
| 114 | + |
| 115 | + std::shared_ptr<Bytes> GetBinary(int32_t pos) const override { |
| 116 | + return ColumnarUtils::GetBytes<arrow::BinaryType>(ctx_->array_ptrs[pos], row_id_, |
| 117 | + ctx_->pool.get()); |
| 118 | + } |
| 119 | + |
| 120 | + std::shared_ptr<InternalRow> GetRow(int32_t pos, int32_t num_fields) const override; |
| 121 | + |
| 122 | + std::shared_ptr<InternalArray> GetArray(int32_t pos) const override; |
| 123 | + |
| 124 | + std::shared_ptr<InternalMap> GetMap(int32_t pos) const override; |
| 125 | + |
| 126 | + std::string ToString() const override { |
| 127 | + return fmt::format("ColumnarRowRef, row_id {}", row_id_); |
| 128 | + } |
| 129 | + |
| 130 | + private: |
| 131 | + std::shared_ptr<ColumnarBatchContext> ctx_; |
| 132 | + const RowKind* row_kind_ = RowKind::Insert(); |
| 133 | + int64_t row_id_; |
| 134 | +}; |
| 135 | +} // namespace paimon |
0 commit comments