-
Notifications
You must be signed in to change notification settings - Fork 29
feat(core): introduce ColumnarRowRef with shared batch context #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Copyright 2026-present Alibaba Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| #include "arrow/array/array_base.h" | ||
|
|
||
| namespace arrow { | ||
| class StructArray; | ||
| } // namespace arrow | ||
|
|
||
| namespace paimon { | ||
| class MemoryPool; | ||
|
|
||
| struct ColumnarBatchContext { | ||
| ColumnarBatchContext(const std::shared_ptr<arrow::StructArray>& struct_array_in, | ||
| const arrow::ArrayVector& field_arrays_in, | ||
| const std::shared_ptr<MemoryPool>& pool_in) | ||
| : struct_array(struct_array_in), pool(pool_in), field_arrays(field_arrays_in) { | ||
| array_ptrs.reserve(field_arrays.size()); | ||
| for (const auto& array : field_arrays) { | ||
| array_ptrs.push_back(array.get()); | ||
| } | ||
| } | ||
|
|
||
| std::shared_ptr<arrow::StructArray> struct_array; | ||
| std::shared_ptr<MemoryPool> pool; | ||
| arrow::ArrayVector field_arrays; | ||
| std::vector<const arrow::Array*> array_ptrs; | ||
| }; | ||
| } // namespace paimon | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Copyright 2026-present Alibaba Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "paimon/common/data/columnar/columnar_row_ref.h" | ||
|
|
||
| #include <cassert> | ||
|
|
||
| #include "arrow/array/array_decimal.h" | ||
| #include "arrow/array/array_nested.h" | ||
| #include "arrow/array/array_primitive.h" | ||
| #include "arrow/type_traits.h" | ||
| #include "arrow/util/checked_cast.h" | ||
| #include "arrow/util/decimal.h" | ||
| #include "paimon/common/data/columnar/columnar_array.h" | ||
| #include "paimon/common/data/columnar/columnar_map.h" | ||
| #include "paimon/common/utils/date_time_utils.h" | ||
|
|
||
| namespace paimon { | ||
| Decimal ColumnarRowRef::GetDecimal(int32_t pos, int32_t precision, int32_t scale) const { | ||
| using ArrayType = typename arrow::TypeTraits<arrow::Decimal128Type>::ArrayType; | ||
| auto array = arrow::internal::checked_cast<const ArrayType*>(ctx_->array_ptrs[pos]); | ||
| assert(array); | ||
| arrow::Decimal128 decimal(array->GetValue(row_id_)); | ||
| return Decimal(precision, scale, | ||
| static_cast<Decimal::int128_t>(decimal.high_bits()) << 64 | decimal.low_bits()); | ||
| } | ||
|
|
||
| Timestamp ColumnarRowRef::GetTimestamp(int32_t pos, int32_t precision) const { | ||
| using ArrayType = typename arrow::TypeTraits<arrow::TimestampType>::ArrayType; | ||
| auto array = arrow::internal::checked_cast<const ArrayType*>(ctx_->array_ptrs[pos]); | ||
| assert(array); | ||
| int64_t data = array->Value(row_id_); | ||
| auto timestamp_type = | ||
| arrow::internal::checked_pointer_cast<arrow::TimestampType>(array->type()); | ||
| // for orc format, data is saved as nano, therefore, Timestamp convert should consider precision | ||
| // in arrow array rather than input precision | ||
| DateTimeUtils::TimeType time_type = DateTimeUtils::GetTimeTypeFromArrowType(timestamp_type); | ||
| auto [milli, nano] = DateTimeUtils::TimestampConverter( | ||
| data, time_type, DateTimeUtils::TimeType::MILLISECOND, DateTimeUtils::TimeType::NANOSECOND); | ||
| return Timestamp(milli, nano); | ||
| } | ||
|
|
||
| std::shared_ptr<InternalRow> ColumnarRowRef::GetRow(int32_t pos, int32_t num_fields) const { | ||
| auto struct_array = | ||
| arrow::internal::checked_pointer_cast<arrow::StructArray>(ctx_->field_arrays[pos]); | ||
| assert(struct_array); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May change |
||
| auto nested_ctx = | ||
| std::make_shared<ColumnarBatchContext>(struct_array, struct_array->fields(), ctx_->pool); | ||
| return std::make_shared<ColumnarRowRef>(std::move(nested_ctx), row_id_); | ||
| } | ||
|
|
||
| std::shared_ptr<InternalArray> ColumnarRowRef::GetArray(int32_t pos) const { | ||
| auto list_array = arrow::internal::checked_cast<const arrow::ListArray*>(ctx_->array_ptrs[pos]); | ||
| assert(list_array); | ||
| int32_t offset = list_array->value_offset(row_id_); | ||
| int32_t length = list_array->value_length(row_id_); | ||
| return std::make_shared<ColumnarArray>(list_array->values(), ctx_->pool, offset, length); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a |
||
| } | ||
|
|
||
| std::shared_ptr<InternalMap> ColumnarRowRef::GetMap(int32_t pos) const { | ||
| auto map_array = arrow::internal::checked_cast<const arrow::MapArray*>(ctx_->array_ptrs[pos]); | ||
| assert(map_array); | ||
| int32_t offset = map_array->value_offset(row_id_); | ||
| int32_t length = map_array->value_length(row_id_); | ||
| return std::make_shared<ColumnarMap>(map_array->keys(), map_array->items(), ctx_->pool, offset, | ||
| length); | ||
| } | ||
|
|
||
| } // namespace paimon | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| * Copyright 2026-present Alibaba Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| #include "fmt/format.h" | ||
| #include "paimon/common/data/binary_string.h" | ||
| #include "paimon/common/data/columnar/columnar_batch_context.h" | ||
| #include "paimon/common/data/columnar/columnar_utils.h" | ||
| #include "paimon/common/data/internal_array.h" | ||
| #include "paimon/common/data/internal_map.h" | ||
| #include "paimon/common/data/internal_row.h" | ||
| #include "paimon/common/types/row_kind.h" | ||
| #include "paimon/data/decimal.h" | ||
| #include "paimon/data/timestamp.h" | ||
| #include "paimon/result.h" | ||
|
|
||
| namespace paimon { | ||
| class Bytes; | ||
|
|
||
| /// Columnar row view which shares batch-level context to reduce per-row overhead. | ||
| class ColumnarRowRef : public InternalRow { | ||
| public: | ||
| ColumnarRowRef(std::shared_ptr<ColumnarBatchContext> ctx, int64_t row_id) | ||
| : ctx_(std::move(ctx)), row_id_(row_id) {} | ||
|
|
||
| Result<const RowKind*> GetRowKind() const override { | ||
| return row_kind_; | ||
| } | ||
|
|
||
| void SetRowKind(const RowKind* kind) override { | ||
| row_kind_ = kind; | ||
| } | ||
|
|
||
| int32_t GetFieldCount() const override { | ||
| return static_cast<int32_t>(ctx_->array_ptrs.size()); | ||
| } | ||
|
|
||
| bool IsNullAt(int32_t pos) const override { | ||
| return ctx_->array_ptrs[pos]->IsNull(row_id_); | ||
| } | ||
|
|
||
| bool GetBoolean(int32_t pos) const override { | ||
| return ColumnarUtils::GetGenericValue<arrow::BooleanType, bool>(ctx_->array_ptrs[pos], | ||
| row_id_); | ||
| } | ||
|
|
||
| char GetByte(int32_t pos) const override { | ||
| return ColumnarUtils::GetGenericValue<arrow::Int8Type, char>(ctx_->array_ptrs[pos], | ||
| row_id_); | ||
| } | ||
|
|
||
| int16_t GetShort(int32_t pos) const override { | ||
| return ColumnarUtils::GetGenericValue<arrow::Int16Type, int16_t>(ctx_->array_ptrs[pos], | ||
| row_id_); | ||
| } | ||
|
|
||
| int32_t GetInt(int32_t pos) const override { | ||
| return ColumnarUtils::GetGenericValue<arrow::Int32Type, int32_t>(ctx_->array_ptrs[pos], | ||
| row_id_); | ||
| } | ||
|
|
||
| int32_t GetDate(int32_t pos) const override { | ||
| return ColumnarUtils::GetGenericValue<arrow::Date32Type, int32_t>(ctx_->array_ptrs[pos], | ||
| row_id_); | ||
| } | ||
|
|
||
| int64_t GetLong(int32_t pos) const override { | ||
| return ColumnarUtils::GetGenericValue<arrow::Int64Type, int64_t>(ctx_->array_ptrs[pos], | ||
| row_id_); | ||
| } | ||
|
|
||
| float GetFloat(int32_t pos) const override { | ||
| return ColumnarUtils::GetGenericValue<arrow::FloatType, float>(ctx_->array_ptrs[pos], | ||
| row_id_); | ||
| } | ||
|
|
||
| double GetDouble(int32_t pos) const override { | ||
| return ColumnarUtils::GetGenericValue<arrow::DoubleType, double>(ctx_->array_ptrs[pos], | ||
| row_id_); | ||
| } | ||
|
|
||
| BinaryString GetString(int32_t pos) const override { | ||
| auto bytes = ColumnarUtils::GetBytes<arrow::StringType>(ctx_->array_ptrs[pos], row_id_, | ||
| ctx_->pool.get()); | ||
| return BinaryString::FromBytes(bytes); | ||
| } | ||
|
|
||
| std::string_view GetStringView(int32_t pos) const override { | ||
| return ColumnarUtils::GetView(ctx_->array_ptrs[pos], row_id_); | ||
| } | ||
|
|
||
| Decimal GetDecimal(int32_t pos, int32_t precision, int32_t scale) const override; | ||
|
|
||
| Timestamp GetTimestamp(int32_t pos, int32_t precision) const override; | ||
|
|
||
| std::shared_ptr<Bytes> GetBinary(int32_t pos) const override { | ||
| return ColumnarUtils::GetBytes<arrow::BinaryType>(ctx_->array_ptrs[pos], row_id_, | ||
| ctx_->pool.get()); | ||
| } | ||
|
|
||
| std::shared_ptr<InternalRow> GetRow(int32_t pos, int32_t num_fields) const override; | ||
|
|
||
| std::shared_ptr<InternalArray> GetArray(int32_t pos) const override; | ||
|
|
||
| std::shared_ptr<InternalMap> GetMap(int32_t pos) const override; | ||
|
|
||
| std::string ToString() const override { | ||
| return fmt::format("ColumnarRowRef, row_id {}", row_id_); | ||
| } | ||
|
|
||
| private: | ||
| std::shared_ptr<ColumnarBatchContext> ctx_; | ||
| const RowKind* row_kind_ = RowKind::Insert(); | ||
| int64_t row_id_; | ||
| }; | ||
| } // namespace paimon |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously, the
struct_array_field inColumnarRowwas used solely as a data holder and could not be directly utilized, since it might contain fields beyond those present inarray_vec_.In the new
ColumnarBatchContext, however,array_vec_holderappears to serve as the primary source of columnar data. Given that a file may contain thousands of fields, maintaining separate holders likearray_vec_holdercould lead to unnecessaryshared_ptrincrements.Suggestion: Consider using
struct_arrayas the canonical holder and removingarray_vec_holderto simplify the design and eliminate redundancy.