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

ARROW-11824: [Rust] [Parquet] Use logical types in Arrow schema conversion #9612

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
254 changes: 191 additions & 63 deletions rust/parquet/src/arrow/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ use crate::errors::{ParquetError::ArrowError, Result};
use crate::file::{metadata::KeyValue, properties::WriterProperties};
use crate::schema::types::{ColumnDescriptor, SchemaDescriptor, Type, TypePtr};
use crate::{
basic::{ConvertedType, Repetition, Type as PhysicalType},
basic::{
ConvertedType, DecimalType, IntType, LogicalType, Repetition, TimeType,
TimeUnit as ParquetTimeUnit, TimestampType, Type as PhysicalType,
},
errors::ParquetError,
};

Expand Down Expand Up @@ -321,18 +324,24 @@ fn arrow_to_parquet_type(field: &Field) -> Result<Type> {
// create type from field
match field.data_type() {
DataType::Null => Type::primitive_type_builder(name, PhysicalType::INT32)
.with_converted_type(ConvertedType::NONE)
.with_logical_type(Some(LogicalType::UNKNOWN(Default::default())))
.with_repetition(repetition)
.build(),
DataType::Boolean => Type::primitive_type_builder(name, PhysicalType::BOOLEAN)
.with_repetition(repetition)
.build(),
DataType::Int8 => Type::primitive_type_builder(name, PhysicalType::INT32)
.with_converted_type(ConvertedType::INT_8)
.with_logical_type(Some(LogicalType::INTEGER(IntType {
bit_width: 8,
is_signed: true,
})))
.with_repetition(repetition)
.build(),
DataType::Int16 => Type::primitive_type_builder(name, PhysicalType::INT32)
.with_converted_type(ConvertedType::INT_16)
.with_logical_type(Some(LogicalType::INTEGER(IntType {
bit_width: 16,
is_signed: true,
})))
.with_repetition(repetition)
.build(),
DataType::Int32 => Type::primitive_type_builder(name, PhysicalType::INT32)
Expand All @@ -342,19 +351,31 @@ fn arrow_to_parquet_type(field: &Field) -> Result<Type> {
.with_repetition(repetition)
.build(),
DataType::UInt8 => Type::primitive_type_builder(name, PhysicalType::INT32)
.with_converted_type(ConvertedType::UINT_8)
.with_logical_type(Some(LogicalType::INTEGER(IntType {
bit_width: 8,
is_signed: false,
})))
.with_repetition(repetition)
.build(),
DataType::UInt16 => Type::primitive_type_builder(name, PhysicalType::INT32)
.with_converted_type(ConvertedType::UINT_16)
.with_logical_type(Some(LogicalType::INTEGER(IntType {
bit_width: 16,
is_signed: false,
})))
.with_repetition(repetition)
.build(),
DataType::UInt32 => Type::primitive_type_builder(name, PhysicalType::INT32)
.with_converted_type(ConvertedType::UINT_32)
.with_logical_type(Some(LogicalType::INTEGER(IntType {
bit_width: 32,
is_signed: false,
})))
.with_repetition(repetition)
.build(),
DataType::UInt64 => Type::primitive_type_builder(name, PhysicalType::INT64)
.with_converted_type(ConvertedType::UINT_64)
.with_logical_type(Some(LogicalType::INTEGER(IntType {
bit_width: 64,
is_signed: false,
})))
.with_repetition(repetition)
.build(),
DataType::Float16 => Err(ArrowError("Float16 arrays not supported".to_string())),
Expand All @@ -364,32 +385,46 @@ fn arrow_to_parquet_type(field: &Field) -> Result<Type> {
DataType::Float64 => Type::primitive_type_builder(name, PhysicalType::DOUBLE)
.with_repetition(repetition)
.build(),
DataType::Timestamp(time_unit, _) => {
Type::primitive_type_builder(name, PhysicalType::INT64)
.with_converted_type(match time_unit {
TimeUnit::Second => ConvertedType::TIMESTAMP_MILLIS,
TimeUnit::Millisecond => ConvertedType::TIMESTAMP_MILLIS,
TimeUnit::Microsecond => ConvertedType::TIMESTAMP_MICROS,
TimeUnit::Nanosecond => ConvertedType::TIMESTAMP_MICROS,
})
.with_repetition(repetition)
.build()
}
DataType::Timestamp(time_unit, zone) => Type::primitive_type_builder(
name,
PhysicalType::INT64,
)
.with_logical_type(Some(LogicalType::TIMESTAMP(TimestampType {
is_adjusted_to_u_t_c: matches!(zone, Some(z) if !z.as_str().is_empty()),
unit: match time_unit {
TimeUnit::Second => ParquetTimeUnit::MILLIS(Default::default()),
TimeUnit::Millisecond => ParquetTimeUnit::MILLIS(Default::default()),
TimeUnit::Microsecond => ParquetTimeUnit::MICROS(Default::default()),
TimeUnit::Nanosecond => ParquetTimeUnit::NANOS(Default::default()),
},
})))
.with_repetition(repetition)
.build(),
DataType::Date32 => Type::primitive_type_builder(name, PhysicalType::INT32)
.with_converted_type(ConvertedType::DATE)
.with_logical_type(Some(LogicalType::DATE(Default::default())))
.with_repetition(repetition)
.build(),
// date64 is cast to date32
DataType::Date64 => Type::primitive_type_builder(name, PhysicalType::INT32)
.with_converted_type(ConvertedType::DATE)
.with_logical_type(Some(LogicalType::DATE(Default::default())))
.with_repetition(repetition)
.build(),
DataType::Time32(_) => Type::primitive_type_builder(name, PhysicalType::INT32)
.with_converted_type(ConvertedType::TIME_MILLIS)
.with_logical_type(Some(LogicalType::TIME(TimeType {
is_adjusted_to_u_t_c: false,
unit: ParquetTimeUnit::MILLIS(Default::default()),
})))
.with_repetition(repetition)
.build(),
DataType::Time64(_) => Type::primitive_type_builder(name, PhysicalType::INT64)
.with_converted_type(ConvertedType::TIME_MICROS)
DataType::Time64(unit) => Type::primitive_type_builder(name, PhysicalType::INT64)
.with_logical_type(Some(LogicalType::TIME(TimeType {
is_adjusted_to_u_t_c: false,
unit: match unit {
TimeUnit::Microsecond => ParquetTimeUnit::MICROS(Default::default()),
TimeUnit::Nanosecond => ParquetTimeUnit::NANOS(Default::default()),
u => unreachable!("Invalid unit for Time64: {:?}", u),
},
})))
.with_repetition(repetition)
.build(),
DataType::Duration(_) => Err(ArrowError(
Expand All @@ -414,17 +449,33 @@ fn arrow_to_parquet_type(field: &Field) -> Result<Type> {
.build()
}
DataType::Decimal(precision, scale) => {
// Decimal precision determines the Parquet physical type to use.
// TODO(ARROW-12018): Enable the below after ARROW-10818 Decimal support
//
// let (physical_type, length) = if *precision > 1 && *precision <= 9 {
// (PhysicalType::INT32, -1)
// } else if *precision <= 18 {
// (PhysicalType::INT64, -1)
// } else {
// (
// PhysicalType::FIXED_LEN_BYTE_ARRAY,
// decimal_length_from_precision(*precision) as i32,
// )
// };
Type::primitive_type_builder(name, PhysicalType::FIXED_LEN_BYTE_ARRAY)
.with_repetition(repetition)
.with_length(decimal_length_from_precision(*precision) as i32)
.with_converted_type(ConvertedType::DECIMAL)
.with_logical_type(Some(LogicalType::DECIMAL(DecimalType {
scale: *scale as i32,
precision: *precision as i32,
})))
.with_precision(*precision as i32)
.with_scale(*scale as i32)
.build()
}
DataType::Utf8 | DataType::LargeUtf8 => {
Type::primitive_type_builder(name, PhysicalType::BYTE_ARRAY)
.with_converted_type(ConvertedType::UTF8)
.with_logical_type(Some(LogicalType::STRING(Default::default())))
.with_repetition(repetition)
.build()
}
Expand All @@ -436,7 +487,7 @@ fn arrow_to_parquet_type(field: &Field) -> Result<Type> {
.with_repetition(Repetition::REPEATED)
.build()?,
)])
.with_converted_type(ConvertedType::LIST)
.with_logical_type(Some(LogicalType::LIST(Default::default())))
.with_repetition(repetition)
.build()
}
Expand Down Expand Up @@ -583,48 +634,109 @@ impl ParquetTypeConverter<'_> {
}

fn from_int32(&self) -> Result<DataType> {
match self.schema.get_basic_info().converted_type() {
ConvertedType::NONE => Ok(DataType::Int32),
ConvertedType::UINT_8 => Ok(DataType::UInt8),
ConvertedType::UINT_16 => Ok(DataType::UInt16),
ConvertedType::UINT_32 => Ok(DataType::UInt32),
ConvertedType::INT_8 => Ok(DataType::Int8),
ConvertedType::INT_16 => Ok(DataType::Int16),
ConvertedType::INT_32 => Ok(DataType::Int32),
ConvertedType::DATE => Ok(DataType::Date32),
ConvertedType::TIME_MILLIS => Ok(DataType::Time32(TimeUnit::Millisecond)),
ConvertedType::DECIMAL => Ok(self.to_decimal()),
other => Err(ArrowError(format!(
"Unable to convert parquet INT32 logical type {}",
other
match (
self.schema.get_basic_info().logical_type(),
Copy link
Member

Choose a reason for hiding this comment

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

nit: perhaps we can first "merge" the logical and converted type into a logical type and then do the conversion, to avoid some of the code duplications. In the case when logical type is not present, we can always convert the converted type into a logical type while losing some information.

We can do this as a follow-up though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I can do it as a follow-up when I've completed the overall 2.6.0 type support

self.schema.get_basic_info().converted_type(),
) {
(None, ConvertedType::NONE) => Ok(DataType::Int32),
(Some(LogicalType::INTEGER(t)), _) => match (t.bit_width, t.is_signed) {
(8, true) => Ok(DataType::Int8),
(16, true) => Ok(DataType::Int16),
(32, true) => Ok(DataType::Int32),
(8, false) => Ok(DataType::UInt8),
(16, false) => Ok(DataType::UInt16),
(32, false) => Ok(DataType::UInt32),
_ => Err(ArrowError(format!(
"Cannot create INT32 physical type from {:?}",
t
))),
},
(Some(LogicalType::DECIMAL(_)), _) => Ok(self.to_decimal()),
(Some(LogicalType::DATE(_)), _) => Ok(DataType::Date32),
(Some(LogicalType::TIME(t)), _) => match t.unit {
ParquetTimeUnit::MILLIS(_) => Ok(DataType::Time32(TimeUnit::Millisecond)),
_ => Err(ArrowError(format!(
"Cannot create INT32 physical type from {:?}",
t.unit
))),
},
(None, ConvertedType::UINT_8) => Ok(DataType::UInt8),
(None, ConvertedType::UINT_16) => Ok(DataType::UInt16),
(None, ConvertedType::UINT_32) => Ok(DataType::UInt32),
(None, ConvertedType::INT_8) => Ok(DataType::Int8),
(None, ConvertedType::INT_16) => Ok(DataType::Int16),
(None, ConvertedType::INT_32) => Ok(DataType::Int32),
(None, ConvertedType::DATE) => Ok(DataType::Date32),
(None, ConvertedType::TIME_MILLIS) => {
Ok(DataType::Time32(TimeUnit::Millisecond))
}
(None, ConvertedType::DECIMAL) => Ok(self.to_decimal()),
(logical, converted) => Err(ArrowError(format!(
"Unable to convert parquet INT32 logical type {:?} or converted type {}",
logical, converted
))),
}
}

fn from_int64(&self) -> Result<DataType> {
match self.schema.get_basic_info().converted_type() {
ConvertedType::NONE => Ok(DataType::Int64),
ConvertedType::INT_64 => Ok(DataType::Int64),
ConvertedType::UINT_64 => Ok(DataType::UInt64),
ConvertedType::TIME_MICROS => Ok(DataType::Time64(TimeUnit::Microsecond)),
ConvertedType::TIMESTAMP_MILLIS => {
match (
self.schema.get_basic_info().logical_type(),
self.schema.get_basic_info().converted_type(),
) {
(None, ConvertedType::NONE) => Ok(DataType::Int64),
(Some(LogicalType::INTEGER(t)), _) if t.bit_width == 64 => {
match t.is_signed {
true => Ok(DataType::Int64),
false => Ok(DataType::UInt64),
}
}
(Some(LogicalType::TIME(t)), _) => match t.unit {
ParquetTimeUnit::MILLIS(_) => Err(ArrowError(
"Cannot create INT64 from MILLIS time unit".to_string(),
)),
ParquetTimeUnit::MICROS(_) => Ok(DataType::Time64(TimeUnit::Microsecond)),
ParquetTimeUnit::NANOS(_) => Ok(DataType::Time64(TimeUnit::Nanosecond)),
},
(Some(LogicalType::TIMESTAMP(t)), _) => Ok(DataType::Timestamp(
match t.unit {
ParquetTimeUnit::MILLIS(_) => TimeUnit::Millisecond,
ParquetTimeUnit::MICROS(_) => TimeUnit::Microsecond,
ParquetTimeUnit::NANOS(_) => TimeUnit::Nanosecond,
},
if t.is_adjusted_to_u_t_c {
Some("UTC".to_string())
} else {
None
},
)),
(None, ConvertedType::INT_64) => Ok(DataType::Int64),
(None, ConvertedType::UINT_64) => Ok(DataType::UInt64),
(None, ConvertedType::TIME_MICROS) => {
Ok(DataType::Time64(TimeUnit::Microsecond))
}
(None, ConvertedType::TIMESTAMP_MILLIS) => {
Ok(DataType::Timestamp(TimeUnit::Millisecond, None))
}
ConvertedType::TIMESTAMP_MICROS => {
(None, ConvertedType::TIMESTAMP_MICROS) => {
Ok(DataType::Timestamp(TimeUnit::Microsecond, None))
}
ConvertedType::DECIMAL => Ok(self.to_decimal()),
other => Err(ArrowError(format!(
"Unable to convert parquet INT64 logical type {}",
other
(Some(LogicalType::DECIMAL(_)), _) => Ok(self.to_decimal()),
(None, ConvertedType::DECIMAL) => Ok(self.to_decimal()),
(logical, converted) => Err(ArrowError(format!(
"Unable to convert parquet INT64 logical type {:?} or converted type {}",
logical, converted
))),
}
}

fn from_fixed_len_byte_array(&self) -> Result<DataType> {
match self.schema.get_basic_info().converted_type() {
ConvertedType::DECIMAL => Ok(self.to_decimal()),
ConvertedType::INTERVAL => {
match (
self.schema.get_basic_info().logical_type(),
self.schema.get_basic_info().converted_type(),
) {
(Some(LogicalType::DECIMAL(_)), _) => Ok(self.to_decimal()),
(None, ConvertedType::DECIMAL) => Ok(self.to_decimal()),
(None, ConvertedType::INTERVAL) => {
// There is currently no reliable way of determining which IntervalUnit
// to return. Thus without the original Arrow schema, the results
// would be incorrect if all 12 bytes of the interval are populated
Expand Down Expand Up @@ -656,12 +768,19 @@ impl ParquetTypeConverter<'_> {
}

fn from_byte_array(&self) -> Result<DataType> {
match self.schema.get_basic_info().converted_type() {
ConvertedType::NONE => Ok(DataType::Binary),
ConvertedType::UTF8 => Ok(DataType::Utf8),
other => Err(ArrowError(format!(
"Unable to convert parquet BYTE_ARRAY logical type {}",
other
match (self.schema.get_basic_info().logical_type(), self.schema.get_basic_info().converted_type()) {
(Some(LogicalType::STRING(_)), _) => Ok(DataType::Utf8),
(Some(LogicalType::JSON(_)), _) => Ok(DataType::Binary),
(Some(LogicalType::BSON(_)), _) => Ok(DataType::Binary),
(Some(LogicalType::ENUM(_)), _) => Ok(DataType::Binary),
(None, ConvertedType::NONE) => Ok(DataType::Binary),
(None, ConvertedType::JSON) => Ok(DataType::Binary),
(None, ConvertedType::BSON) => Ok(DataType::Binary),
(None, ConvertedType::ENUM) => Ok(DataType::Binary),
(None, ConvertedType::UTF8) => Ok(DataType::Utf8),
(logical, converted) => Err(ArrowError(format!(
"Unable to convert parquet BYTE_ARRAY logical type {:?} or converted type {}",
logical, converted
))),
}
}
Expand All @@ -683,8 +802,12 @@ impl ParquetTypeConverter<'_> {
})
})
} else {
match self.schema.get_basic_info().converted_type() {
ConvertedType::LIST => self.to_list(),
match (
self.schema.get_basic_info().logical_type(),
self.schema.get_basic_info().converted_type(),
) {
(Some(LogicalType::LIST(_)), _) => self.to_list(),
(None, ConvertedType::LIST) => self.to_list(),
_ => self.to_struct(),
}
}
Expand Down Expand Up @@ -1441,6 +1564,7 @@ mod tests {
}

#[test]
#[ignore = "To be addressed as part of ARROW-11365"]
fn test_field_to_column_desc() {
let message_type = "
message arrow_schema {
Expand Down Expand Up @@ -1550,6 +1674,7 @@ mod tests {
.iter()
.zip(converted_arrow_schema.columns())
.for_each(|(a, b)| {
// TODO: ARROW-11365: If parsing v1 format, there should be no logical type
assert_eq!(a, b);
});
}
Expand Down Expand Up @@ -1688,6 +1813,9 @@ mod tests {
// true,
// ),
Field::new("c35", DataType::Null, true),
Field::new("c36", DataType::Decimal(2, 1), false),
Field::new("c37", DataType::Decimal(50, 20), false),
Field::new("c38", DataType::Decimal(18, 12), true),
],
metadata,
);
Expand Down
Loading