-
Notifications
You must be signed in to change notification settings - Fork 1k
Add Map support to arrow-avro #7451
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
|
|
||
| use crate::schema::{Attributes, ComplexType, PrimitiveType, Record, Schema, TypeName}; | ||
| use arrow_schema::{ | ||
| ArrowError, DataType, Field, FieldRef, IntervalUnit, SchemaBuilder, SchemaRef, TimeUnit, | ||
| ArrowError, DataType, Field, FieldRef, Fields, IntervalUnit, SchemaBuilder, SchemaRef, TimeUnit, | ||
| }; | ||
| use std::borrow::Cow; | ||
| use std::collections::HashMap; | ||
|
|
@@ -45,6 +45,19 @@ pub struct AvroDataType { | |
| } | ||
|
|
||
| impl AvroDataType { | ||
| /// Create a new [`AvroDataType`] with the given parts. | ||
| pub fn new( | ||
| codec: Codec, | ||
| metadata: HashMap<String, String>, | ||
| nullability: Option<Nullability>, | ||
| ) -> Self { | ||
| AvroDataType { | ||
| codec, | ||
| metadata, | ||
| nullability, | ||
| } | ||
| } | ||
|
|
||
| /// Returns an arrow [`Field`] with the given name | ||
| pub fn field_with_name(&self, name: &str) -> Field { | ||
| let d = self.codec.data_type(); | ||
|
|
@@ -162,6 +175,8 @@ pub enum Codec { | |
| List(Arc<AvroDataType>), | ||
| /// Represents Avro record type, maps to Arrow's Struct data type | ||
| Struct(Arc<[AvroField]>), | ||
| /// Represents Avro map type, maps to Arrow's Map data type | ||
| Map(Arc<AvroDataType>), | ||
| /// Represents Avro duration logical type, maps to Arrow's Interval(IntervalUnit::MonthDayNano) data type | ||
| Interval, | ||
| } | ||
|
|
@@ -192,6 +207,22 @@ impl Codec { | |
| DataType::List(Arc::new(f.field_with_name(Field::LIST_FIELD_DEFAULT_NAME))) | ||
| } | ||
| Self::Struct(f) => DataType::Struct(f.iter().map(|x| x.field()).collect()), | ||
| Self::Map(value_type) => { | ||
| let val_dt = value_type.codec.data_type(); | ||
| let val_field = Field::new("value", val_dt, value_type.nullability.is_some()) | ||
| .with_metadata(value_type.metadata.clone()); | ||
| DataType::Map( | ||
| Arc::new(Field::new( | ||
| "entries", | ||
| DataType::Struct(Fields::from(vec![ | ||
| Field::new("key", DataType::Utf8, false), | ||
| val_field, | ||
| ])), | ||
| false, | ||
| )), | ||
| false, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -321,9 +352,14 @@ fn make_data_type<'a>( | |
| ComplexType::Enum(e) => Err(ArrowError::NotYetImplemented(format!( | ||
| "Enum of {e:?} not currently supported" | ||
| ))), | ||
| ComplexType::Map(m) => Err(ArrowError::NotYetImplemented(format!( | ||
| "Map of {m:?} not currently supported" | ||
| ))), | ||
| ComplexType::Map(m) => { | ||
| let val = make_data_type(&m.values, namespace, resolver)?; | ||
| Ok(AvroDataType { | ||
| nullability: None, | ||
|
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. Do we need to set the 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. I was attempting to follow the same pattern used in the other types such as It seemed from reading the code that nullability would only be set in the |
||
| metadata: m.attributes.field_metadata(), | ||
| codec: Codec::Map(Arc::new(val)), | ||
| }) | ||
| } | ||
| }, | ||
| Schema::Type(t) => { | ||
| let mut field = | ||
|
|
||
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.
Since this is a
pubenum and it is not marked asnon exhaustiveI think this is technically a breaking API change and will need to wait for the next major release:56.0.0(July 2025) #7395