Skip to content

Apache DataFusion SQL Query Engine

support byte array for decimal in parquet page and row group filters (#4742)

Kun Liuf91f623eecc1
diff --git a/datafusion/core/src/physical_plan/file_format/parquet.rs b/datafusion/core/src/physical_plan/file_format/parquet.rs
index 2bdddcddc1..c3e74ad229 100644
--- a/datafusion/core/src/physical_plan/file_format/parquet.rs
+++ b/datafusion/core/src/physical_plan/file_format/parquet.rs
@@ -674,21 +674,26 @@ pub async fn plan_to_parquet(
}
}
-// TODO: consolidate code with arrow-rs
+// Copy from the arrow-rs
+// https://github.com/apache/arrow-rs/blob/733b7e7fd1e8c43a404c3ce40ecf741d493c21b4/parquet/src/arrow/buffer/bit_util.rs#L55
+// Convert the byte slice to fixed length byte array with the length of 16
+fn sign_extend_be(b: &[u8]) -> [u8; 16] {
+ assert!(b.len() <= 16, "Array too large, expected less than 16");
+ let is_negative = (b[0] & 128u8) == 128u8;
+ let mut result = if is_negative { [255u8; 16] } else { [0u8; 16] };
+ for (d, s) in result.iter_mut().skip(16 - b.len()).zip(b) {
+ *d = *s;
+ }
+ result
+}
+
// Convert the bytes array to i128.
// The endian of the input bytes array must be big-endian.
-// Copy from the arrow-rs
pub(crate) fn from_bytes_to_i128(b: &[u8]) -> i128 {
- assert!(b.len() <= 16, "Decimal128Array supports only up to size 16");
- let first_bit = b[0] & 128u8 == 128u8;
- let mut result = if first_bit { [255u8; 16] } else { [0u8; 16] };
- for (i, v) in b.iter().enumerate() {
- result[i + (16 - b.len())] = *v;
- }
// The bytes array are from parquet file and must be the big-endian.
// The endian is defined by parquet format, and the reference document
// https://github.com/apache/parquet-format/blob/54e53e5d7794d383529dd30746378f19a12afd58/src/main/thrift/parquet.thrift#L66
- i128::from_be_bytes(result)
+ i128::from_be_bytes(sign_extend_be(b))
}
// Convert parquet column schema to arrow data type, and just consider the
diff --git a/datafusion/core/src/physical_plan/file_format/parquet/page_filter.rs b/datafusion/core/src/physical_plan/file_format/parquet/page_filter.rs
index 57115fbf98..9d7f11462f 100644
--- a/datafusion/core/src/physical_plan/file_format/parquet/page_filter.rs
+++ b/datafusion/core/src/physical_plan/file_format/parquet/page_filter.rs
@@ -380,15 +380,28 @@ macro_rules! get_min_max_values_for_page_index {
vec.iter().map(|x| x.$func().cloned()),
)))
}
- Index::BYTE_ARRAY(index) => {
- let vec = &index.indexes;
- let array: StringArray = vec
- .iter()
- .map(|x| x.$func())
- .map(|x| x.and_then(|x| std::str::from_utf8(x).ok()))
- .collect();
- Some(Arc::new(array))
- }
+ Index::BYTE_ARRAY(index) => match $self.target_type {
+ Some(DataType::Decimal128(precision, scale)) => {
+ let vec = &index.indexes;
+ Decimal128Array::from(
+ vec.iter()
+ .map(|x| x.$func().and_then(|x| Some(from_bytes_to_i128(x))))
+ .collect::<Vec<Option<i128>>>(),
+ )
+ .with_precision_and_scale(*precision, *scale)
+ .ok()
+ .map(|arr| Arc::new(arr) as ArrayRef)
+ }
+ _ => {
+ let vec = &index.indexes;
+ let array: StringArray = vec
+ .iter()
+ .map(|x| x.$func())
+ .map(|x| x.and_then(|x| std::str::from_utf8(x).ok()))
+ .collect();
+ Some(Arc::new(array))
+ }
+ },
Index::INT96(_) => {
//Todo support these type
None
diff --git a/datafusion/core/src/physical_plan/file_format/parquet/row_groups.rs b/datafusion/core/src/physical_plan/file_format/parquet/row_groups.rs
index 0f0b37807e..716c47cf9b 100644
--- a/datafusion/core/src/physical_plan/file_format/parquet/row_groups.rs
+++ b/datafusion/core/src/physical_plan/file_format/parquet/row_groups.rs
@@ -125,11 +125,22 @@ macro_rules! get_statistic {
ParquetStatistics::Float(s) => Some(ScalarValue::Float32(Some(*s.$func()))),
ParquetStatistics::Double(s) => Some(ScalarValue::Float64(Some(*s.$func()))),
ParquetStatistics::ByteArray(s) => {
- // TODO support decimal type for byte array type
- let s = std::str::from_utf8(s.$bytes_func())
- .map(|s| s.to_string())
- .ok();
- Some(ScalarValue::Utf8(s))
+ match $target_arrow_type {
+ // decimal data type
+ Some(DataType::Decimal128(precision, scale)) => {
+ Some(ScalarValue::Decimal128(
+ Some(from_bytes_to_i128(s.$bytes_func())),
+ precision,
+ scale,
+ ))
+ }
+ _ => {
+ let s = std::str::from_utf8(s.$bytes_func())
+ .map(|s| s.to_string())
+ .ok();
+ Some(ScalarValue::Utf8(s))
+ }
+ }
}
// type not supported yet
ParquetStatistics::FixedLenByteArray(s) => {
@@ -627,6 +638,56 @@ mod tests {
);
// TODO: BYTE_ARRAY support read decimal from parquet, after the 20.0.0 arrow-rs release
+ // BYTE_ARRAY: c1 = decimal128(100000, 28, 3), the c1 is decimal(18,2)
+ // the type of parquet is decimal(18,2)
+ let schema =
+ Schema::new(vec![Field::new("c1", DataType::Decimal128(18, 2), false)]);
+ // cast the type of c1 to decimal(28,3)
+ let left = cast(col("c1"), DataType::Decimal128(28, 3));
+ let expr = left.eq(lit(ScalarValue::Decimal128(Some(100000), 28, 3)));
+ let schema_descr = get_test_schema_descr(vec![(
+ "c1",
+ PhysicalType::BYTE_ARRAY,
+ Some(LogicalType::Decimal {
+ scale: 2,
+ precision: 18,
+ }),
+ Some(18),
+ Some(2),
+ Some(16),
+ )]);
+ let pruning_predicate =
+ PruningPredicate::try_new(expr, Arc::new(schema)).unwrap();
+ // we must use the big-endian when encode the i128 to bytes or vec[u8].
+ let rgm1 = get_row_group_meta_data(
+ &schema_descr,
+ vec![ParquetStatistics::byte_array(
+ // 5.00
+ Some(ByteArray::from(500i128.to_be_bytes().to_vec())),
+ // 80.00
+ Some(ByteArray::from(8000i128.to_be_bytes().to_vec())),
+ None,
+ 0,
+ false,
+ )],
+ );
+ let rgm2 = get_row_group_meta_data(
+ &schema_descr,
+ vec![ParquetStatistics::byte_array(
+ // 5.00
+ Some(ByteArray::from(500i128.to_be_bytes().to_vec())),
+ // 200.00
+ Some(ByteArray::from(20000i128.to_be_bytes().to_vec())),
+ None,
+ 0,
+ false,
+ )],
+ );
+ let metrics = parquet_file_metrics();
+ assert_eq!(
+ prune_row_groups(&[rgm1, rgm2], None, Some(&pruning_predicate), &metrics),
+ vec![1]
+ );
}
fn get_row_group_meta_data(