Apache DataFusion SQL Query Engine
Support Avro's Enum type and Fixed type (#7635)
Kousuke Sarutaaddd3681cb15
diff --git a/datafusion/core/src/datasource/avro_to_arrow/arrow_array_reader.rs b/datafusion/core/src/datasource/avro_to_arrow/arrow_array_reader.rsindex eae30b4219..f983e26d48 100644--- a/datafusion/core/src/datasource/avro_to_arrow/arrow_array_reader.rs+++ b/datafusion/core/src/datasource/avro_to_arrow/arrow_array_reader.rs@@ -41,7 +41,7 @@ use apache_avro::{types::Value,AvroResult, Error as AvroError, Reader as AvroReader,};-use arrow::array::{BinaryArray, GenericListArray};+use arrow::array::{BinaryArray, FixedSizeBinaryArray, GenericListArray};use arrow::datatypes::{Fields, SchemaRef};use arrow::error::ArrowError::SchemaError;use arrow::error::Result as ArrowResult;@@ -734,6 +734,15 @@ impl<'a, R: Read> AvroArrowArrayReader<'a, R> {.collect::<BinaryArray>(),)as ArrayRef,+ DataType::FixedSizeBinary(ref size) => {+ Arc::new(FixedSizeBinaryArray::try_from_sparse_iter_with_size(+ rows.iter().map(|row| {+ let maybe_value = self.field_lookup(field.name(), row);+ maybe_value.and_then(|v| resolve_fixed(v, *size as usize))+ }),+ *size,+ )?) as ArrayRef+ }DataType::List(ref list_field) => {match list_field.data_type() {DataType::Dictionary(ref key_ty, _) => {@@ -893,6 +902,7 @@ fn resolve_string(v: &Value) -> ArrowResult<Option<String>> {Value::Bytes(bytes) => String::from_utf8(bytes.to_vec()).map_err(AvroError::ConvertToUtf8).map(Some),+ Value::Enum(_, s) => Ok(Some(s.clone())),Value::Null => Ok(None),other => Err(AvroError::GetString(other.into())),}@@ -935,6 +945,20 @@ fn resolve_bytes(v: &Value) -> Option<Vec<u8>> {})}+fn resolve_fixed(v: &Value, size: usize) -> Option<Vec<u8>> {+ let v = if let Value::Union(_, b) = v { b } else { v };+ match v {+ Value::Fixed(n, bytes) => {+ if *n == size {+ Some(bytes.clone())+ } else {+ None+ }+ }+ _ => None,+ }+}+fn resolve_boolean(value: &Value) -> Option<bool> {let v = if let Value::Union(_, b) = value {bdiff --git a/datafusion/core/src/datasource/avro_to_arrow/schema.rs b/datafusion/core/src/datasource/avro_to_arrow/schema.rsindex 41b1d3c6e4..3a4fab2ee6 100644--- a/datafusion/core/src/datasource/avro_to_arrow/schema.rs+++ b/datafusion/core/src/datasource/avro_to_arrow/schema.rs@@ -24,7 +24,6 @@ use apache_avro::types::Value;use apache_avro::Schema as AvroSchema;use arrow::datatypes::{Field, UnionFields};use std::collections::HashMap;-use std::convert::TryFrom;use std::sync::Arc;/// Converts an avro schema to an arrow schema@@ -137,15 +136,7 @@ fn schema_to_field_with_props(.collect();DataType::Struct(fields?)}- AvroSchema::Enum(EnumSchema { symbols, name, .. }) => {- return Ok(Field::new_dict(- name.fullname(None),- index_type(symbols.len()),- false,- 0,- false,- ))- }+ AvroSchema::Enum(EnumSchema { .. }) => DataType::Utf8,AvroSchema::Fixed(FixedSchema { size, .. }) => {DataType::FixedSizeBinary(*size as i32)}@@ -235,18 +226,6 @@ fn default_field_name(dt: &DataType) -> &str {}}-fn index_type(len: usize) -> DataType {- if len <= usize::from(u8::MAX) {- DataType::Int8- } else if len <= usize::from(u16::MAX) {- DataType::Int16- } else if usize::try_from(u32::MAX).map(|i| len < i).unwrap_or(false) {- DataType::Int32- } else {- DataType::Int64- }-}-fn external_props(schema: &AvroSchema) -> HashMap<String, String> {let mut props = HashMap::new();match &schema {diff --git a/datafusion/sqllogictest/test_files/avro.slt b/datafusion/sqllogictest/test_files/avro.sltindex 0037c7e2f4..ede11406e1 100644--- a/datafusion/sqllogictest/test_files/avro.slt+++ b/datafusion/sqllogictest/test_files/avro.slt@@ -48,6 +48,18 @@ STORED AS AVROWITH HEADER ROWLOCATION '../../testing/data/avro/nested_records.avro'+statement ok+CREATE EXTERNAL TABLE simple_enum+STORED AS AVRO+WITH HEADER ROW+LOCATION '../../testing/data/avro/simple_enum.avro'++statement ok+CREATE EXTERNAL TABLE simple_fixed+STORED AS AVRO+WITH HEADER ROW+LOCATION '../../testing/data/avro/simple_fixed.avro'+# test avro queryquery ITSELECT id, CAST(string_col AS varchar) FROM alltypes_plain@@ -95,6 +107,22 @@ SELECT f1, f2 FROM nested_records{ns2.record2.f1_1: aaa, ns2.record2.f1_2: 10, ns2.record2.f1_3: {ns3.record3.f1_3_1: 3.14}} [{ns4.record4.f2_1: true, ns4.record4.f2_2: 1.2}, {ns4.record4.f2_1: true, ns4.record4.f2_2: 2.2}]{ns2.record2.f1_1: bbb, ns2.record2.f1_2: 20, ns2.record2.f1_3: {ns3.record3.f1_3_1: 3.14}} [{ns4.record4.f2_1: false, ns4.record4.f2_2: 10.2}]+# test avro enum+query TTT+SELECT f1, f2, f3 FROM simple_enum+----+a g j+b h k+c e NULL+d f i++# test avro fixed+query ???+SELECT f1, f2, f3 FROM simple_fixed+----+6162636465 666768696a6b6c6d6e6f 414243444546+3132333435 31323334353637383930 NULL+# test avro explainquery TTEXPLAIN SELECT count(*) from alltypes_plaindiff --git a/parquet-testing b/parquet-testingindex d79a0101d9..e45cd23f78 160000--- a/parquet-testing+++ b/parquet-testing@@ -1 +1 @@-Subproject commit d79a0101d90dfa3bbb10337626f57a3e8c4b5363+Subproject commit e45cd23f784aab3d6bf0701f8f4e621469ed3be7diff --git a/testing b/testingindex 2c84953c8c..37f29510ce 160000--- a/testing+++ b/testing@@ -1 +1 @@-Subproject commit 2c84953c8c2779a0dc86ef9ebe8a6cd978125bfe+Subproject commit 37f29510ce97cd491b8e6ed75866c6533a5ea2a1