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 2 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
76 changes: 76 additions & 0 deletions arrow-schema/src/datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,82 @@ impl DataType {
}
}

/// Canonical extension types.
///
/// The Arrow columnar format allows defining extension types so as to extend
/// standard Arrow data types with custom semantics. Often these semantics will
/// be specific to a system or application. However, it is beneficial to share
/// the definitions of well-known extension types so as to improve
/// interoperability between different systems integrating Arrow columnar data.
///
/// <https://arrow.apache.org/docs/format/CanonicalExtensions.html>
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum ExtensionType {
/// Extension name: `arrow.uuid`.
///
/// The storage type of the extension is `FixedSizeBinary` with a length of
/// 16 bytes.
///
/// Note:
/// A specific UUID version is not required or guaranteed. This extension
/// represents UUIDs as FixedSizeBinary(16) with big-endian notation and
/// does not interpret the bytes in any way.
Uuid,
}

impl fmt::Display for ExtensionType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())
}
}

impl ExtensionType {
/// The metadata key for the string name identifying the custom data type.
pub const NAME_KEY: &'static str = "ARROW:extension:name";

/// The metadata key for a serialized representation of the ExtensionType
/// necessary to reconstruct the custom type.
pub const METADATA_KEY: &'static str = "ARROW:extension:metadata";

/// Returns the name of this extension type.
pub fn name(&self) -> &'static str {
match self {
ExtensionType::Uuid => "arrow.uuid",
}
}

/// Returns the metadata of this extension type.
pub fn metadata(&self) -> Option<String> {
match self {
ExtensionType::Uuid => None,
}
}

/// Returns `true` iff the given [`DataType`] can be used as storage type
/// for this extension type.
pub(crate) fn supports_storage_type(&self, data_type: &DataType) -> bool {
match self {
ExtensionType::Uuid => matches!(data_type, DataType::FixedSizeBinary(16)),
}
}

/// Extract an [`ExtensionType`] from the given [`Field`].
///
/// This function returns `None` if the extension type is not supported or
/// recognized.
pub(crate) fn try_from_field(field: &Field) -> Option<Self> {
let metadata = field.metadata().get(ExtensionType::METADATA_KEY);
field
.metadata()
.get(ExtensionType::NAME_KEY)
.and_then(|name| match name.as_str() {
"arrow.uuid" if metadata.is_none() => Some(ExtensionType::Uuid),
_ => None,
})
}
}

/// The maximum precision for [DataType::Decimal128] values
pub const DECIMAL128_MAX_PRECISION: u8 = 38;

Expand Down
33 changes: 32 additions & 1 deletion arrow-schema/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::sync::Arc;

use crate::datatype::DataType;
use crate::schema::SchemaBuilder;
use crate::{Fields, UnionFields, UnionMode};
use crate::{ExtensionType, Fields, UnionFields, UnionMode};

/// A reference counted [`Field`]
pub type FieldRef = Arc<Field>;
Expand Down Expand Up @@ -337,6 +337,37 @@ impl Field {
self
}

/// Returns the canonical [`ExtensionType`] of this [`Field`], if set.
pub fn extension_type(&self) -> Option<ExtensionType> {
ExtensionType::try_from_field(self)
}

/// Updates the metadata of this [`Field`] with the [`ExtensionType::name`]
/// and [`ExtensionType::metadata`] of the given [`ExtensionType`].
///
/// # Panics
///
/// This function panics when the datatype of this field is not a valid
/// storage type for the given extension type.
pub fn with_extension_type(mut self, extension_type: ExtensionType) -> Self {
if extension_type.supports_storage_type(&self.data_type) {
self.metadata.insert(
ExtensionType::NAME_KEY.to_owned(),
extension_type.name().to_owned(),
);
if let Some(metadata) = extension_type.metadata() {
self.metadata
.insert(ExtensionType::METADATA_KEY.to_owned(), metadata);
}
self
} else {
panic!(
"{extension_type} does not support {} as storage type",
self.data_type
);
}
}

/// Indicates whether this [`Field`] supports null values.
#[inline]
pub const fn is_nullable(&self) -> bool {
Expand Down
6 changes: 5 additions & 1 deletion parquet/src/arrow/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use std::collections::HashMap;
use std::sync::Arc;

use arrow_ipc::writer;
use arrow_schema::{DataType, Field, Fields, Schema, TimeUnit};
use arrow_schema::{DataType, ExtensionType, Field, Fields, Schema, TimeUnit};

use crate::basic::{
ConvertedType, LogicalType, Repetition, TimeUnit as ParquetTimeUnit, Type as PhysicalType,
Expand Down Expand Up @@ -468,6 +468,10 @@ fn arrow_to_parquet_type(field: &Field) -> Result<Type> {
.with_repetition(repetition)
.with_id(id)
.with_length(*length)
.with_logical_type(match field.extension_type() {
Some(ExtensionType::Uuid) => Some(LogicalType::Uuid),
_ => None,
})
.build()
}
DataType::BinaryView => Type::primitive_type_builder(name, PhysicalType::BYTE_ARRAY)
Expand Down
Loading