Skip to content
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

Merged
merged 25 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
a7244f5
Add `ExtensionType` for `uuid` and map to parquet logical type
mbrobbel May 31, 2024
6b2e7aa
Fix docs
mbrobbel May 31, 2024
bdeab9f
Use an `ExtensionType` trait instead
mbrobbel Sep 26, 2024
315bd92
Merge branch 'master' into parquet-uuid-schema
mbrobbel Sep 26, 2024
8428653
Fix clippy warnings
mbrobbel Sep 26, 2024
7896455
Add type annotation to fix build
mbrobbel Sep 26, 2024
374d017
Merge branch 'master' into parquet-uuid-schema
mbrobbel Sep 30, 2024
e35630a
Update `ExtensionType` trait to support more canonical extension types
mbrobbel Jan 17, 2025
0966a0f
Add `Json` support to parquet, schema roundtrip not working yet
mbrobbel Jan 17, 2025
f5c06b1
Fix some clippy warnings
mbrobbel Jan 17, 2025
b602412
Add explicit lifetime, resolving elided lifetime to static in assoc c…
mbrobbel Jan 17, 2025
5cdfa3f
Merge branch 'main' into parquet-uuid-schema
mbrobbel Jan 20, 2025
81594d9
Replace use of deprecated method, mark roundtrip as todo
mbrobbel Jan 20, 2025
bb7c86a
Add more tests and missing impls
mbrobbel Jan 20, 2025
38c7255
Add missing type annotations
mbrobbel Jan 20, 2025
1a21e96
Fix doc warning
mbrobbel Jan 20, 2025
069642f
Add the feature to the `arrow` crate and use underscores
mbrobbel Jan 22, 2025
8519344
Update feature name in `parquet` crate
mbrobbel Jan 22, 2025
5fec56d
Add experimental warning to `extensions` module docs
mbrobbel Jan 22, 2025
b78e692
Add a note about the associated metadata type
mbrobbel Jan 22, 2025
29a94cb
Fix `Json` canonical extension type empty string metadata
mbrobbel Jan 24, 2025
757b041
Simplify `Bool8::deserialize_metadata`
mbrobbel Jan 24, 2025
c6f0443
Use `Empty` instead of `serde_json::Map` in `JsonMetadata`
mbrobbel Jan 24, 2025
75f56a4
Use `map_or` instead of `is_some_and` (msrv)
mbrobbel Jan 24, 2025
4c62785
Merge remote-tracking branch 'apache/main' into parquet-uuid-schema
alamb Feb 2, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions arrow-array/src/array/list_view_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,8 @@ mod tests {
.build()
.unwrap(),
);
assert_eq!(string.value_offsets(), &[]);
assert_eq!(string.value_sizes(), &[]);
assert_eq!(string.value_offsets(), &[] as &[i32; 0]);
assert_eq!(string.value_sizes(), &[] as &[i32; 0]);

let string = LargeListViewArray::from(
ArrayData::builder(DataType::LargeListView(f))
Expand All @@ -906,8 +906,8 @@ mod tests {
.unwrap(),
);
assert_eq!(string.len(), 0);
assert_eq!(string.value_offsets(), &[]);
assert_eq!(string.value_sizes(), &[]);
assert_eq!(string.value_offsets(), &[] as &[i64; 0]);
assert_eq!(string.value_sizes(), &[] as &[i64; 0]);
}

#[test]
Expand Down
12 changes: 9 additions & 3 deletions arrow-schema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,27 @@ path = "src/lib.rs"
bench = false

[dependencies]
serde = { version = "1.0", default-features = false, features = ["derive", "std", "rc"], optional = true }
serde = { version = "1.0", default-features = false, features = [
"derive",
"std",
"rc",
], optional = true }
bitflags = { version = "2.0.0", default-features = false, optional = true }
serde_json = { version = "1.0", optional = true }

[features]
canonical_extension_types = ["dep:serde", "dep:serde_json"]
# Enable ffi support
ffi = ["bitflags"]
serde = ["dep:serde"]

[package.metadata.docs.rs]
features = ["ffi"]

[dev-dependencies]
serde_json = "1.0"
bincode = { version = "1.3.3", default-features = false }
criterion = { version = "0.5", default-features = false }

[[bench]]
name = "ffi"
harness = false
harness = false
142 changes: 142 additions & 0 deletions arrow-schema/src/extension/canonical/bool8.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// 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;
Copy link

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the proposed definition of the trait, () would indicate no metadata and results in the metadata key to be in unset in the field custom metadata map. Note that there is no actual storage for the metadata in this extension type. Both serialization and deserialization make sure the metadata is always an empty string.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! Missed that! Thanks!


fn metadata(&self) -> &Self::Metadata {
&""
}

fn serialize_metadata(&self) -> Option<String> {
Some(String::default())
}

fn deserialize_metadata(metadata: Option<&str>) -> Result<Self::Metadata, ArrowError> {
if metadata.map_or(false, str::is_empty) {
Ok("")
} else {
Err(ArrowError::InvalidArgumentError(
"Bool8 extension type expects an empty string as metadata".to_owned(),
))
}
}

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>();
}
}
Loading
Loading