Apache DataFusion SQL Query Engine
Migrate from avro_rs (0.13) to apache_avro (0.14) (#2784)
Martin Grigorov07b7314d8769
diff --git a/datafusion/common/Cargo.toml b/datafusion/common/Cargo.tomlindex d6e69c0331..e7873de4d0 100644--- a/datafusion/common/Cargo.toml+++ b/datafusion/common/Cargo.toml@@ -33,13 +33,13 @@ name = "datafusion_common"path = "src/lib.rs"[features]-avro = ["avro-rs"]+avro = ["apache-avro"]jit = ["cranelift-module"]pyarrow = ["pyo3"][dependencies]+apache-avro = { version = "0.14", features = ["snappy"], optional = true }arrow = { version = "19.0.0", features = ["prettyprint"] }-avro-rs = { version = "0.13", features = ["snappy"], optional = true }cranelift-module = { version = "0.86.1", optional = true }object_store = { version = "0.3", optional = true }ordered-float = "3.0"diff --git a/datafusion/common/src/error.rs b/datafusion/common/src/error.rsindex de5bbe8e00..47a63b3204 100644--- a/datafusion/common/src/error.rs+++ b/datafusion/common/src/error.rs@@ -23,9 +23,9 @@ use std::io;use std::result;use crate::DFSchema;-use arrow::error::ArrowError;#[cfg(feature = "avro")]-use avro_rs::Error as AvroError;+use apache_avro::Error as AvroError;+use arrow::error::ArrowError;#[cfg(feature = "jit")]use cranelift_module::ModuleError;#[cfg(feature = "parquet")]diff --git a/datafusion/core/Cargo.toml b/datafusion/core/Cargo.tomlindex b7b63091e9..073b951ffb 100644--- a/datafusion/core/Cargo.toml+++ b/datafusion/core/Cargo.toml@@ -39,7 +39,7 @@ path = "src/lib.rs"[features]# Used to enable the avro format-avro = ["avro-rs", "num-traits", "datafusion-common/avro"]+avro = ["apache-avro", "num-traits", "datafusion-common/avro"]crypto_expressions = ["datafusion-physical-expr/crypto_expressions"]default = ["crypto_expressions", "regex_expressions", "unicode_expressions"]# Used for testing ONLY: causes all values to hash to the same value (test for collisions)@@ -55,9 +55,9 @@ unicode_expressions = ["datafusion-physical-expr/regex_expressions", "datafusion[dependencies]ahash = { version = "0.7", default-features = false }+apache-avro = { version = "0.14", optional = true }arrow = { version = "19.0.0", features = ["prettyprint"] }async-trait = "0.1.41"-avro-rs = { version = "0.13", features = ["snappy"], optional = true }bytes = "1.1"chrono = { version = "0.4", default-features = false }datafusion-common = { path = "../common", version = "10.0.0", features = ["parquet", "object_store"] }diff --git a/datafusion/core/src/avro_to_arrow/arrow_array_reader.rs b/datafusion/core/src/avro_to_arrow/arrow_array_reader.rsindex 358a6d0485..864a675ed4 100644--- a/datafusion/core/src/avro_to_arrow/arrow_array_reader.rs+++ b/datafusion/core/src/avro_to_arrow/arrow_array_reader.rs@@ -36,17 +36,17 @@ use crate::arrow::error::ArrowError;use crate::arrow::record_batch::RecordBatch;use crate::arrow::util::bit_util;use crate::error::{DataFusionError, Result};-use arrow::array::{BinaryArray, GenericListArray};-use arrow::datatypes::SchemaRef;-use arrow::error::ArrowError::SchemaError;-use arrow::error::Result as ArrowResult;-use avro_rs::{+use apache_avro::{schema::{Schema as AvroSchema, SchemaKind},types::Value,AvroResult, Error as AvroError, Reader as AvroReader,};+use arrow::array::{BinaryArray, GenericListArray};+use arrow::datatypes::SchemaRef;+use arrow::error::ArrowError::SchemaError;+use arrow::error::Result as ArrowResult;use num_traits::NumCast;-use std::collections::HashMap;+use std::collections::BTreeMap;use std::io::Read;use std::sync::Arc;@@ -56,7 +56,7 @@ pub struct AvroArrowArrayReader<'a, R: Read> {reader: AvroReader<'a, R>,schema: SchemaRef,projection: Option<Vec<String>>,- schema_lookup: HashMap<String, usize>,+ schema_lookup: BTreeMap<String, usize>,}impl<'a, R: Read> AvroArrowArrayReader<'a, R> {@@ -76,7 +76,7 @@ impl<'a, R: Read> AvroArrowArrayReader<'a, R> {})}- pub fn schema_lookup(schema: AvroSchema) -> Result<HashMap<String, usize>> {+ pub fn schema_lookup(schema: AvroSchema) -> Result<BTreeMap<String, usize>> {match schema {AvroSchema::Record {lookup: ref schema_lookup,@@ -874,7 +874,7 @@ fn flatten_string_values(values: &[&Value]) -> Vec<Option<String>> {/// This is useful if the expected datatype is a string, in which case we preserve/// all the values regardless of they type.fn resolve_string(v: &Value) -> ArrowResult<String> {- let v = if let Value::Union(b) = v { b } else { v };+ let v = if let Value::Union(_, b) = v { b } else { v };match v {Value::String(s) => Ok(s.clone()),Value::Bytes(bytes) => {@@ -882,7 +882,7 @@ fn resolve_string(v: &Value) -> ArrowResult<String> {}other => Err(AvroError::GetString(other.into())),}- .map_err(|e| SchemaError(format!("expected resolvable string : {}", e)))+ .map_err(|e| SchemaError(format!("expected resolvable string : {:?}", e)))}fn resolve_u8(v: &Value) -> AvroResult<u8> {@@ -901,7 +901,7 @@ fn resolve_u8(v: &Value) -> AvroResult<u8> {}fn resolve_bytes(v: &Value) -> Option<Vec<u8>> {- let v = if let Value::Union(b) = v { b } else { v };+ let v = if let Value::Union(_, b) = v { b } else { v };match v {Value::Bytes(_) => Ok(v.clone()),Value::String(s) => Ok(Value::Bytes(s.clone().into_bytes())),@@ -922,7 +922,7 @@ fn resolve_bytes(v: &Value) -> Option<Vec<u8>> {}fn resolve_boolean(value: &Value) -> Option<bool> {- let v = if let Value::Union(b) = value {+ let v = if let Value::Union(_, b) = value {b} else {value@@ -945,7 +945,7 @@ fn maybe_resolve_union(value: &Value) -> &Value {if SchemaKind::from(value) == SchemaKind::Union {// Pull out the Union, and attempt to resolve against it.match value {- Value::Union(b) => b,+ Value::Union(_, b) => b,_ => unreachable!(),}} else {diff --git a/datafusion/core/src/avro_to_arrow/mod.rs b/datafusion/core/src/avro_to_arrow/mod.rsindex f30fbdcc0c..8ca7f22ef3 100644--- a/datafusion/core/src/avro_to_arrow/mod.rs+++ b/datafusion/core/src/avro_to_arrow/mod.rs@@ -33,7 +33,7 @@ use std::io::Read;#[cfg(feature = "avro")]/// Read Avro schema given a readerpub fn read_avro_schema_from_reader<R: Read>(reader: &mut R) -> Result<Schema> {- let avro_reader = avro_rs::Reader::new(reader)?;+ let avro_reader = apache_avro::Reader::new(reader)?;let schema = avro_reader.writer_schema();schema::to_arrow_schema(schema)}diff --git a/datafusion/core/src/avro_to_arrow/reader.rs b/datafusion/core/src/avro_to_arrow/reader.rsindex 8baad14746..d3fc8de968 100644--- a/datafusion/core/src/avro_to_arrow/reader.rs+++ b/datafusion/core/src/avro_to_arrow/reader.rs@@ -56,7 +56,7 @@ impl ReaderBuilder {/// # Example////// ```- /// extern crate avro_rs;+ /// extern crate apache_avro;////// use std::fs::File;///diff --git a/datafusion/core/src/avro_to_arrow/schema.rs b/datafusion/core/src/avro_to_arrow/schema.rsindex c5b4febbb9..5e601504d1 100644--- a/datafusion/core/src/avro_to_arrow/schema.rs+++ b/datafusion/core/src/avro_to_arrow/schema.rs@@ -17,15 +17,15 @@use crate::arrow::datatypes::{DataType, IntervalUnit, Schema, TimeUnit, UnionMode};use crate::error::{DataFusionError, Result};+use apache_avro::schema::{Alias, Name};+use apache_avro::types::Value;+use apache_avro::Schema as AvroSchema;use arrow::datatypes::Field;-use avro_rs::schema::Name;-use avro_rs::types::Value;-use avro_rs::Schema as AvroSchema;use std::collections::BTreeMap;use std::convert::TryFrom;/// Converts an avro schema to an arrow schema-pub fn to_arrow_schema(avro_schema: &avro_rs::Schema) -> Result<Schema> {+pub fn to_arrow_schema(avro_schema: &apache_avro::Schema) -> Result<Schema> {let mut schema_fields = vec![];match avro_schema {AvroSchema::Record { fields, .. } => {@@ -46,7 +46,7 @@ pub fn to_arrow_schema(avro_schema: &avro_rs::Schema) -> Result<Schema> {}fn schema_to_field(- schema: &avro_rs::Schema,+ schema: &apache_avro::Schema,name: Option<&str>,nullable: bool,) -> Result<Field> {@@ -61,6 +61,7 @@ fn schema_to_field_with_props() -> Result<Field> {let mut nullable = nullable;let field_type: DataType = match schema {+ AvroSchema::Ref { .. } => todo!("Add support for AvroSchema::Ref"),AvroSchema::Null => DataType::Null,AvroSchema::Boolean => DataType::Boolean,AvroSchema::Int => DataType::Int32,@@ -95,7 +96,7 @@ fn schema_to_field_with_props(.clone()} else {return Err(DataFusionError::AvroError(- avro_rs::Error::GetUnionDuplicate,+ apache_avro::Error::GetUnionDuplicate,));}} else {@@ -241,6 +242,9 @@ fn external_props(schema: &AvroSchema) -> BTreeMap<String, String> {}| AvroSchema::Enum {doc: Some(ref doc), ..+ }+ | AvroSchema::Fixed {+ doc: Some(ref doc), ..} => {props.insert("avro::doc".to_string(), doc.clone());}@@ -248,30 +252,18 @@ fn external_props(schema: &AvroSchema) -> BTreeMap<String, String> {}match &schema {AvroSchema::Record {- name:- Name {- aliases: Some(aliases),- namespace,- ..- },+ name: Name { namespace, .. },+ aliases: Some(aliases),..}| AvroSchema::Enum {- name:- Name {- aliases: Some(aliases),- namespace,- ..- },+ name: Name { namespace, .. },+ aliases: Some(aliases),..}| AvroSchema::Fixed {- name:- Name {- aliases: Some(aliases),- namespace,- ..- },+ name: Name { namespace, .. },+ aliases: Some(aliases),..} => {let aliases: Vec<String> = aliases@@ -300,18 +292,18 @@ fn get_metadata(/// Returns the fully qualified name for a fieldpub fn aliased(- name: &str,+ alias: &Alias,namespace: Option<&str>,default_namespace: Option<&str>,) -> String {- if name.contains('.') {- name.to_string()+ if alias.namespace().is_some() {+ alias.fullname(None)} else {let namespace = namespace.as_ref().copied().or(default_namespace);match namespace {- Some(ref namespace) => format!("{}.{}", namespace, name),- None => name.to_string(),+ Some(ref namespace) => format!("{}.{}", namespace, alias.name()),+ None => alias.fullname(None),}}}@@ -322,16 +314,20 @@ mod test {use crate::arrow::datatypes::DataType::{Binary, Float32, Float64, Timestamp, Utf8};use crate::arrow::datatypes::TimeUnit::Microsecond;use crate::arrow::datatypes::{Field, Schema};+ use apache_avro::schema::{Alias, Name};+ use apache_avro::Schema as AvroSchema;use arrow::datatypes::DataType::{Boolean, Int32, Int64};- use avro_rs::schema::Name;- use avro_rs::Schema as AvroSchema;++ fn alias(name: &str) -> Alias {+ Alias::new(name).unwrap()+ }#[test]fn test_alias() {- assert_eq!(aliased("foo.bar", None, None), "foo.bar");- assert_eq!(aliased("bar", Some("foo"), None), "foo.bar");- assert_eq!(aliased("bar", Some("foo"), Some("cat")), "foo.bar");- assert_eq!(aliased("bar", None, Some("cat")), "cat.bar");+ assert_eq!(aliased(&alias("foo.bar"), None, None), "foo.bar");+ assert_eq!(aliased(&alias("bar"), Some("foo"), None), "foo.bar");+ assert_eq!(aliased(&alias("bar"), Some("foo"), Some("cat")), "foo.bar");+ assert_eq!(aliased(&alias("bar"), None, Some("cat")), "cat.bar");}#[test]@@ -340,8 +336,8 @@ mod test {name: Name {name: "record".to_string(),namespace: None,- aliases: Some(vec!["fooalias".to_string(), "baralias".to_string()]),},+ aliases: Some(vec![alias("fooalias"), alias("baralias")]),doc: Some("record documentation".to_string()),fields: vec![],lookup: Default::default(),@@ -359,8 +355,8 @@ mod test {name: Name {name: "enum".to_string(),namespace: None,- aliases: Some(vec!["fooenum".to_string(), "barenum".to_string()]),},+ aliases: Some(vec![alias("fooenum"), alias("barenum")]),doc: Some("enum documentation".to_string()),symbols: vec![],};@@ -377,9 +373,10 @@ mod test {name: Name {name: "fixed".to_string(),namespace: None,- aliases: Some(vec!["foofixed".to_string(), "barfixed".to_string()]),},+ aliases: Some(vec![alias("foofixed"), alias("barfixed")]),size: 1,+ doc: None,};let props = external_props(&fixed_schema);assert_eq!(