|
| 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 | +#include "paimon/common/data/columnar/columnar_row_ref.h" |
| 18 | + |
| 19 | +#include <cassert> |
| 20 | + |
| 21 | +#include "arrow/array/array_decimal.h" |
| 22 | +#include "arrow/array/array_nested.h" |
| 23 | +#include "arrow/array/array_primitive.h" |
| 24 | +#include "arrow/type_traits.h" |
| 25 | +#include "arrow/util/checked_cast.h" |
| 26 | +#include "arrow/util/decimal.h" |
| 27 | +#include "paimon/common/data/columnar/columnar_array.h" |
| 28 | +#include "paimon/common/data/columnar/columnar_map.h" |
| 29 | +#include "paimon/common/utils/date_time_utils.h" |
| 30 | + |
| 31 | +namespace paimon { |
| 32 | +Decimal ColumnarRowRef::GetDecimal(int32_t pos, int32_t precision, int32_t scale) const { |
| 33 | + using ArrayType = typename arrow::TypeTraits<arrow::Decimal128Type>::ArrayType; |
| 34 | + auto array = arrow::internal::checked_cast<const ArrayType*>(ctx_->array_ptrs[pos]); |
| 35 | + assert(array); |
| 36 | + arrow::Decimal128 decimal(array->GetValue(row_id_)); |
| 37 | + return Decimal(precision, scale, |
| 38 | + static_cast<Decimal::int128_t>(decimal.high_bits()) << 64 | decimal.low_bits()); |
| 39 | +} |
| 40 | + |
| 41 | +Timestamp ColumnarRowRef::GetTimestamp(int32_t pos, int32_t precision) const { |
| 42 | + using ArrayType = typename arrow::TypeTraits<arrow::TimestampType>::ArrayType; |
| 43 | + auto array = arrow::internal::checked_cast<const ArrayType*>(ctx_->array_ptrs[pos]); |
| 44 | + assert(array); |
| 45 | + int64_t data = array->Value(row_id_); |
| 46 | + auto timestamp_type = |
| 47 | + arrow::internal::checked_pointer_cast<arrow::TimestampType>(array->type()); |
| 48 | + // for orc format, data is saved as nano, therefore, Timestamp convert should consider precision |
| 49 | + // in arrow array rather than input precision |
| 50 | + DateTimeUtils::TimeType time_type = DateTimeUtils::GetTimeTypeFromArrowType(timestamp_type); |
| 51 | + auto [milli, nano] = DateTimeUtils::TimestampConverter( |
| 52 | + data, time_type, DateTimeUtils::TimeType::MILLISECOND, DateTimeUtils::TimeType::NANOSECOND); |
| 53 | + return Timestamp(milli, nano); |
| 54 | +} |
| 55 | + |
| 56 | +std::shared_ptr<InternalRow> ColumnarRowRef::GetRow(int32_t pos, int32_t num_fields) const { |
| 57 | + auto struct_array = |
| 58 | + arrow::internal::checked_cast<const arrow::StructArray*>(ctx_->array_ptrs[pos]); |
| 59 | + assert(struct_array); |
| 60 | + auto nested_ctx = |
| 61 | + std::make_shared<ColumnarBatchContext>(nullptr, struct_array->fields(), ctx_->pool); |
| 62 | + return std::make_shared<ColumnarRowRef>(std::move(nested_ctx), row_id_); |
| 63 | +} |
| 64 | + |
| 65 | +std::shared_ptr<InternalArray> ColumnarRowRef::GetArray(int32_t pos) const { |
| 66 | + auto list_array = arrow::internal::checked_cast<const arrow::ListArray*>(ctx_->array_ptrs[pos]); |
| 67 | + assert(list_array); |
| 68 | + int32_t offset = list_array->value_offset(row_id_); |
| 69 | + int32_t length = list_array->value_length(row_id_); |
| 70 | + return std::make_shared<ColumnarArray>(list_array->values(), ctx_->pool, offset, length); |
| 71 | +} |
| 72 | + |
| 73 | +std::shared_ptr<InternalMap> ColumnarRowRef::GetMap(int32_t pos) const { |
| 74 | + auto map_array = arrow::internal::checked_cast<const arrow::MapArray*>(ctx_->array_ptrs[pos]); |
| 75 | + assert(map_array); |
| 76 | + int32_t offset = map_array->value_offset(row_id_); |
| 77 | + int32_t length = map_array->value_length(row_id_); |
| 78 | + return std::make_shared<ColumnarMap>(map_array->keys(), map_array->items(), ctx_->pool, offset, |
| 79 | + length); |
| 80 | +} |
| 81 | + |
| 82 | +} // namespace paimon |
0 commit comments