-
Notifications
You must be signed in to change notification settings - Fork 846
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
Add ExtensionType
trait and CanonicalExtensionType
enum
#5822
Open
mbrobbel
wants to merge
20
commits into
apache:main
Choose a base branch
from
mbrobbel:parquet-uuid-schema
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.
+2,342
−12
Open
Changes from 18 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a7244f5
Add `ExtensionType` for `uuid` and map to parquet logical type
mbrobbel 6b2e7aa
Fix docs
mbrobbel bdeab9f
Use an `ExtensionType` trait instead
mbrobbel 315bd92
Merge branch 'master' into parquet-uuid-schema
mbrobbel 8428653
Fix clippy warnings
mbrobbel 7896455
Add type annotation to fix build
mbrobbel 374d017
Merge branch 'master' into parquet-uuid-schema
mbrobbel e35630a
Update `ExtensionType` trait to support more canonical extension types
mbrobbel 0966a0f
Add `Json` support to parquet, schema roundtrip not working yet
mbrobbel f5c06b1
Fix some clippy warnings
mbrobbel b602412
Add explicit lifetime, resolving elided lifetime to static in assoc c…
mbrobbel 5cdfa3f
Merge branch 'main' into parquet-uuid-schema
mbrobbel 81594d9
Replace use of deprecated method, mark roundtrip as todo
mbrobbel bb7c86a
Add more tests and missing impls
mbrobbel 38c7255
Add missing type annotations
mbrobbel 1a21e96
Fix doc warning
mbrobbel 069642f
Add the feature to the `arrow` crate and use underscores
mbrobbel 8519344
Update feature name in `parquet` crate
mbrobbel 5fec56d
Add experimental warning to `extensions` module docs
mbrobbel b78e692
Add a note about the associated metadata type
mbrobbel 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 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 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 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,143 @@ | ||||||||||||||
// 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. | ||||||||||||||
|
||||||||||||||
//! 8-bit Boolean | ||||||||||||||
//! | ||||||||||||||
//! <https://arrow.apache.org/docs/format/CanonicalExtensions.html#bit-boolean> | ||||||||||||||
use crate::{extension::ExtensionType, ArrowError, DataType}; | ||||||||||||||
|
||||||||||||||
/// The extension type for `8-bit Boolean`. | ||||||||||||||
/// | ||||||||||||||
/// Extension name: `arrow.bool8`. | ||||||||||||||
/// | ||||||||||||||
/// The storage type of the extension is `Int8` where: | ||||||||||||||
/// - false is denoted by the value 0. | ||||||||||||||
/// - true can be specified using any non-zero value. Preferably 1. | ||||||||||||||
/// | ||||||||||||||
/// <https://arrow.apache.org/docs/format/CanonicalExtensions.html#bit-boolean> | ||||||||||||||
#[derive(Debug, Default, Clone, Copy, PartialEq)] | ||||||||||||||
pub struct Bool8; | ||||||||||||||
|
||||||||||||||
impl ExtensionType for Bool8 { | ||||||||||||||
const NAME: &'static str = "arrow.bool8"; | ||||||||||||||
|
||||||||||||||
type Metadata = &'static str; | ||||||||||||||
|
||||||||||||||
fn metadata(&self) -> &Self::Metadata { | ||||||||||||||
&"" | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
fn serialize_metadata(&self) -> Option<String> { | ||||||||||||||
Some(String::default()) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
fn deserialize_metadata(metadata: Option<&str>) -> Result<Self::Metadata, ArrowError> { | ||||||||||||||
const ERR: &str = "Bool8 extension type expects an empty string as metadata"; | ||||||||||||||
metadata.map_or_else( | ||||||||||||||
|| Err(ArrowError::InvalidArgumentError(ERR.to_owned())), | ||||||||||||||
|value| match value { | ||||||||||||||
"" => Ok(""), | ||||||||||||||
_ => Err(ArrowError::InvalidArgumentError(ERR.to_owned())), | ||||||||||||||
}, | ||||||||||||||
) | ||||||||||||||
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. slightly shorter:
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
fn supports_data_type(&self, data_type: &DataType) -> Result<(), ArrowError> { | ||||||||||||||
match data_type { | ||||||||||||||
DataType::Int8 => Ok(()), | ||||||||||||||
data_type => Err(ArrowError::InvalidArgumentError(format!( | ||||||||||||||
"Bool8 data type mismatch, expected Int8, found {data_type}" | ||||||||||||||
))), | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
fn try_new(data_type: &DataType, _metadata: Self::Metadata) -> Result<Self, ArrowError> { | ||||||||||||||
Self.supports_data_type(data_type).map(|_| Self) | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[cfg(test)] | ||||||||||||||
mod tests { | ||||||||||||||
#[cfg(feature = "canonical_extension_types")] | ||||||||||||||
use crate::extension::CanonicalExtensionType; | ||||||||||||||
use crate::{ | ||||||||||||||
extension::{EXTENSION_TYPE_METADATA_KEY, EXTENSION_TYPE_NAME_KEY}, | ||||||||||||||
Field, | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
use super::*; | ||||||||||||||
|
||||||||||||||
#[test] | ||||||||||||||
fn valid() -> Result<(), ArrowError> { | ||||||||||||||
let mut field = Field::new("", DataType::Int8, false); | ||||||||||||||
field.try_with_extension_type(Bool8)?; | ||||||||||||||
field.try_extension_type::<Bool8>()?; | ||||||||||||||
#[cfg(feature = "canonical_extension_types")] | ||||||||||||||
assert_eq!( | ||||||||||||||
field.try_canonical_extension_type()?, | ||||||||||||||
CanonicalExtensionType::Bool8(Bool8) | ||||||||||||||
); | ||||||||||||||
|
||||||||||||||
Ok(()) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[test] | ||||||||||||||
#[should_panic(expected = "Field extension type name missing")] | ||||||||||||||
fn missing_name() { | ||||||||||||||
let field = Field::new("", DataType::Int8, false).with_metadata( | ||||||||||||||
[(EXTENSION_TYPE_METADATA_KEY.to_owned(), "".to_owned())] | ||||||||||||||
.into_iter() | ||||||||||||||
.collect(), | ||||||||||||||
); | ||||||||||||||
field.extension_type::<Bool8>(); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[test] | ||||||||||||||
#[should_panic(expected = "expected Int8, found Boolean")] | ||||||||||||||
fn invalid_type() { | ||||||||||||||
Field::new("", DataType::Boolean, false).with_extension_type(Bool8); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[test] | ||||||||||||||
#[should_panic(expected = "Bool8 extension type expects an empty string as metadata")] | ||||||||||||||
fn missing_metadata() { | ||||||||||||||
let field = Field::new("", DataType::Int8, false).with_metadata( | ||||||||||||||
[(EXTENSION_TYPE_NAME_KEY.to_owned(), Bool8::NAME.to_owned())] | ||||||||||||||
.into_iter() | ||||||||||||||
.collect(), | ||||||||||||||
); | ||||||||||||||
field.extension_type::<Bool8>(); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[test] | ||||||||||||||
#[should_panic(expected = "Bool8 extension type expects an empty string as metadata")] | ||||||||||||||
fn invalid_metadata() { | ||||||||||||||
let field = Field::new("", DataType::Int8, false).with_metadata( | ||||||||||||||
[ | ||||||||||||||
(EXTENSION_TYPE_NAME_KEY.to_owned(), Bool8::NAME.to_owned()), | ||||||||||||||
( | ||||||||||||||
EXTENSION_TYPE_METADATA_KEY.to_owned(), | ||||||||||||||
"non-empty".to_owned(), | ||||||||||||||
), | ||||||||||||||
] | ||||||||||||||
.into_iter() | ||||||||||||||
.collect(), | ||||||||||||||
); | ||||||||||||||
field.extension_type::<Bool8>(); | ||||||||||||||
} | ||||||||||||||
} |
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.
Maybe bad idea: use
()
here. This way it's not possible to encode bad metadata value (any string that is not empty)