-
Notifications
You must be signed in to change notification settings - Fork 113
feat(parquet): apply column default values when reading missing fields (2/4) #792
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
Open
huan233usc
wants to merge
1
commit into
apache:main
Choose a base branch
from
huan233usc:feat/default-values-read-parquet
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 <cstring> | ||
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include <arrow/array.h> | ||
| #include <arrow/array/builder_base.h> | ||
| #include <arrow/array/util.h> | ||
| #include <arrow/buffer.h> | ||
| #include <arrow/compute/api.h> | ||
| #include <arrow/extension_type.h> | ||
| #include <arrow/scalar.h> | ||
| #include <arrow/type.h> | ||
|
|
||
| #include "iceberg/arrow/arrow_status_internal.h" | ||
| #include "iceberg/arrow/literal_util_internal.h" | ||
| #include "iceberg/type.h" | ||
| #include "iceberg/util/checked_cast.h" | ||
| #include "iceberg/util/formatter.h" // IWYU pragma: keep | ||
| #include "iceberg/util/macros.h" | ||
|
|
||
| namespace iceberg::arrow { | ||
|
|
||
| namespace { | ||
|
|
||
| Result<std::shared_ptr<::arrow::DataType>> ToArrowType(const PrimitiveType& type) { | ||
| switch (type.type_id()) { | ||
| case TypeId::kBoolean: | ||
| return ::arrow::boolean(); | ||
| case TypeId::kInt: | ||
| return ::arrow::int32(); | ||
| case TypeId::kLong: | ||
| return ::arrow::int64(); | ||
| case TypeId::kFloat: | ||
| return ::arrow::float32(); | ||
| case TypeId::kDouble: | ||
| return ::arrow::float64(); | ||
| case TypeId::kDecimal: { | ||
| const auto& decimal_type = internal::checked_cast<const DecimalType&>(type); | ||
| return ::arrow::decimal128(decimal_type.precision(), decimal_type.scale()); | ||
| } | ||
| case TypeId::kDate: | ||
| return ::arrow::date32(); | ||
| case TypeId::kTime: | ||
| return ::arrow::time64(::arrow::TimeUnit::MICRO); | ||
| case TypeId::kTimestamp: | ||
| return ::arrow::timestamp(::arrow::TimeUnit::MICRO); | ||
| case TypeId::kTimestampTz: | ||
| return ::arrow::timestamp(::arrow::TimeUnit::MICRO, "UTC"); | ||
| case TypeId::kTimestampNs: | ||
| return ::arrow::timestamp(::arrow::TimeUnit::NANO); | ||
| case TypeId::kTimestampTzNs: | ||
| return ::arrow::timestamp(::arrow::TimeUnit::NANO, "UTC"); | ||
| case TypeId::kString: | ||
| return ::arrow::utf8(); | ||
| case TypeId::kBinary: | ||
| return ::arrow::binary(); | ||
| case TypeId::kFixed: { | ||
| const auto& fixed_type = internal::checked_cast<const FixedType&>(type); | ||
| return ::arrow::fixed_size_binary(static_cast<int32_t>(fixed_type.length())); | ||
| } | ||
| case TypeId::kUuid: | ||
| return ::arrow::fixed_size_binary(16); | ||
| default: | ||
| return NotSupported("Cannot convert {} to an Arrow type", type); | ||
| } | ||
| } | ||
|
|
||
| Result<std::shared_ptr<::arrow::Buffer>> ToArrowBuffer( | ||
| const std::vector<uint8_t>& bytes) { | ||
| ICEBERG_ARROW_ASSIGN_OR_RETURN(std::unique_ptr<::arrow::Buffer> buffer, | ||
| ::arrow::AllocateBuffer(bytes.size())); | ||
| std::memcpy(buffer->mutable_data(), bytes.data(), bytes.size()); | ||
| return std::shared_ptr<::arrow::Buffer>(std::move(buffer)); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| Result<std::shared_ptr<::arrow::Scalar>> ToArrowScalar(const Literal& literal) { | ||
| if (literal.type() == nullptr) { | ||
| return InvalidArgument("Cannot convert a literal without type to an Arrow scalar"); | ||
| } | ||
|
|
||
| if (literal.IsAboveMax() || literal.IsBelowMin()) { | ||
| return NotSupported("Cannot convert {} to an Arrow scalar", literal); | ||
| } | ||
|
|
||
| ICEBERG_ASSIGN_OR_RAISE(std::shared_ptr<::arrow::DataType> arrow_type, | ||
| ToArrowType(*literal.type())); | ||
| if (literal.IsNull()) { | ||
| return ::arrow::MakeNullScalar(std::move(arrow_type)); | ||
| } | ||
|
|
||
| const Literal::Value& value = literal.value(); | ||
| switch (literal.type()->type_id()) { | ||
| case TypeId::kBoolean: | ||
| return std::make_shared<::arrow::BooleanScalar>(std::get<bool>(value)); | ||
| case TypeId::kInt: | ||
| return std::make_shared<::arrow::Int32Scalar>(std::get<int32_t>(value)); | ||
| case TypeId::kLong: | ||
| return std::make_shared<::arrow::Int64Scalar>(std::get<int64_t>(value)); | ||
| case TypeId::kFloat: | ||
| return std::make_shared<::arrow::FloatScalar>(std::get<float>(value)); | ||
| case TypeId::kDouble: | ||
| return std::make_shared<::arrow::DoubleScalar>(std::get<double>(value)); | ||
| case TypeId::kDecimal: { | ||
| const auto& decimal = std::get<Decimal>(value); | ||
| ::arrow::Decimal128 arrow_decimal( | ||
| static_cast<int64_t>(decimal.value() >> 64), | ||
| static_cast<uint64_t>(decimal.value() & ~uint64_t{0})); | ||
| return std::make_shared<::arrow::Decimal128Scalar>(arrow_decimal, | ||
| std::move(arrow_type)); | ||
| } | ||
| case TypeId::kDate: | ||
| return std::make_shared<::arrow::Date32Scalar>(std::get<int32_t>(value)); | ||
| case TypeId::kTime: | ||
| return std::make_shared<::arrow::Time64Scalar>(std::get<int64_t>(value), | ||
| std::move(arrow_type)); | ||
| case TypeId::kTimestamp: | ||
| case TypeId::kTimestampTz: | ||
| case TypeId::kTimestampNs: | ||
| case TypeId::kTimestampTzNs: | ||
| return std::make_shared<::arrow::TimestampScalar>(std::get<int64_t>(value), | ||
| std::move(arrow_type)); | ||
| case TypeId::kString: | ||
| return std::make_shared<::arrow::StringScalar>(std::get<std::string>(value)); | ||
| case TypeId::kBinary: { | ||
| ICEBERG_ASSIGN_OR_RAISE(std::shared_ptr<::arrow::Buffer> buffer, | ||
| ToArrowBuffer(std::get<std::vector<uint8_t>>(value))); | ||
| return std::make_shared<::arrow::BinaryScalar>(std::move(buffer)); | ||
| } | ||
| case TypeId::kFixed: { | ||
| ICEBERG_ASSIGN_OR_RAISE(std::shared_ptr<::arrow::Buffer> buffer, | ||
| ToArrowBuffer(std::get<std::vector<uint8_t>>(value))); | ||
| return std::make_shared<::arrow::FixedSizeBinaryScalar>(std::move(buffer), | ||
| std::move(arrow_type)); | ||
| } | ||
| case TypeId::kUuid: { | ||
| const Uuid& uuid = std::get<Uuid>(value); | ||
| ICEBERG_ASSIGN_OR_RAISE( | ||
| std::shared_ptr<::arrow::Buffer> buffer, | ||
| ToArrowBuffer(std::vector<uint8_t>(uuid.bytes().begin(), uuid.bytes().end()))); | ||
| return std::make_shared<::arrow::FixedSizeBinaryScalar>(std::move(buffer), | ||
| std::move(arrow_type)); | ||
| } | ||
| default: | ||
| return NotSupported("Cannot convert {} literal to an Arrow scalar", | ||
| *literal.type()); | ||
| } | ||
| } | ||
|
|
||
| Result<std::shared_ptr<::arrow::Array>> MakeDefaultArray( | ||
| const Literal& literal, const std::shared_ptr<::arrow::DataType>& type, | ||
| int64_t num_rows, ::arrow::MemoryPool* pool) { | ||
| // An extension type (e.g. `arrow.uuid` for an Iceberg UUID) is backed by a storage | ||
| // type, and compute::Cast has no kernel that casts a storage array into an extension | ||
| // type. Materialize the array as the storage type and wrap it in the extension type. | ||
| if (type->id() == ::arrow::Type::EXTENSION) { | ||
| const auto& extension_type = | ||
| internal::checked_cast<const ::arrow::ExtensionType&>(*type); | ||
| ICEBERG_ASSIGN_OR_RAISE( | ||
| std::shared_ptr<::arrow::Array> storage, | ||
| MakeDefaultArray(literal, extension_type.storage_type(), num_rows, pool)); | ||
| return ::arrow::ExtensionType::WrapArray(type, storage); | ||
| } | ||
|
|
||
| ICEBERG_ASSIGN_OR_RAISE(std::shared_ptr<::arrow::Scalar> scalar, | ||
| ToArrowScalar(literal)); | ||
| ICEBERG_ARROW_ASSIGN_OR_RETURN(std::shared_ptr<::arrow::Array> array, | ||
| ::arrow::MakeArrayFromScalar(*scalar, num_rows, pool)); | ||
| if (!array->type()->Equals(*type)) { | ||
| ICEBERG_ARROW_ASSIGN_OR_RETURN(::arrow::Datum cast_result, | ||
| ::arrow::compute::Cast(array, type)); | ||
| return cast_result.make_array(); | ||
| } | ||
| return array; | ||
| } | ||
|
|
||
| Status AppendDefaultToBuilder(const Literal& literal, ::arrow::ArrayBuilder* builder) { | ||
| ICEBERG_ASSIGN_OR_RAISE(std::shared_ptr<::arrow::Scalar> scalar, | ||
| ToArrowScalar(literal)); | ||
| if (!scalar->type->Equals(*builder->type())) { | ||
|
Member
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. Could we mirror |
||
| ICEBERG_ARROW_ASSIGN_OR_RETURN(scalar, scalar->CastTo(builder->type())); | ||
| } | ||
| ICEBERG_ARROW_RETURN_NOT_OK(builder->AppendScalar(*scalar)); | ||
| return {}; | ||
| } | ||
|
|
||
| } // namespace iceberg::arrow | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 <arrow/type_fwd.h> | ||
|
|
||
| #include "iceberg/expression/literal.h" | ||
| #include "iceberg/result.h" | ||
|
|
||
| namespace iceberg::arrow { | ||
|
|
||
| /// \brief Convert a primitive literal to an Arrow scalar of its canonical Arrow type. | ||
| /// | ||
| /// A null literal converts to a null scalar of the corresponding Arrow type. | ||
| Result<std::shared_ptr<::arrow::Scalar>> ToArrowScalar(const Literal& literal); | ||
|
|
||
| /// \brief Create an Arrow array of `num_rows` rows where every row holds the literal | ||
| /// value, e.g. to materialize a missing column with a default value. | ||
| /// | ||
| /// The array is cast to `type` when the literal's canonical Arrow type differs. | ||
| Result<std::shared_ptr<::arrow::Array>> MakeDefaultArray( | ||
| const Literal& literal, const std::shared_ptr<::arrow::DataType>& type, | ||
| int64_t num_rows, ::arrow::MemoryPool* pool); | ||
|
|
||
| /// \brief Append the literal value once to `builder`, e.g. to materialize a missing | ||
| /// field with a default value while building rows. | ||
| Status AppendDefaultToBuilder(const Literal& literal, ::arrow::ArrayBuilder* builder); | ||
|
|
||
| } // namespace iceberg::arrow |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
add
arrow::MemoryPool*as an input parameter.