etseidl commented on code in PR #11549: URL: https://github.com/apache/arrow-rs/pull/11549#discussion_r4445833512
########## parquet/src/arrow/bloom_filter.rs: ########## @@ -0,0 +1,1237 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Check Arrow arrays against Parquet bloom filters + +use arrow_array::builder::BooleanBuilder; +use arrow_array::cast::AsArray; +use arrow_array::types::{Float32Type, Float64Type, RunEndIndexType}; +use arrow_array::{Array, BooleanArray, RunArray, downcast_run_array}; +use arrow_buffer::{ArrowNativeType, BooleanBuffer}; +use arrow_schema::{DataType, Field}; + +use crate::arrow::arrow_writer::{ + FixedLenByteArrayVisitor, PrimitiveVisitor, visit_fixed_len_byte_array, visit_i32, visit_i64, +}; +use crate::arrow::schema::{arrow_to_parquet_type, parquet_to_arrow_field}; +use crate::basic::{ConvertedType, LogicalType, Type as PhysicalType}; +use crate::bloom_filter::Sbbf; +use crate::data_type::AsBytes; +use crate::errors::{ParquetError, Result}; +use crate::schema::types::{ColumnDescriptor, Type}; + +/// A Parquet bloom filter ([`Sbbf`]) that can be checked with Arrow arrays +/// +/// [`ArrowWriter`] may store an Arrow value as a different Parquet value, and +/// the bloom filter holds the stored value, so [`Sbbf::check`] with the Arrow +/// value can report a value that is present as absent. [`ArrowSbbf::check`] +/// converts Arrow values in the same way as [`ArrowWriter`] before checking the +/// filter. +/// +/// ``` +/// # use std::sync::Arc; +/// # use bytes::Bytes; +/// # use arrow_array::{ArrayRef, Int8Array, RecordBatch}; +/// # use parquet::arrow::ArrowWriter; +/// # use parquet::arrow::bloom_filter::ArrowSbbf; +/// # use parquet::file::properties::{ReaderProperties, WriterProperties}; +/// # use parquet::file::reader::{FileReader, SerializedFileReader}; +/// # use parquet::file::serialized_reader::ReadOptionsBuilder; +/// let array: ArrayRef = Arc::new(Int8Array::from(vec![1, 2, 3])); +/// let batch = RecordBatch::try_from_iter([("c", array)]).unwrap(); +/// +/// let props = WriterProperties::builder() +/// .set_bloom_filter_enabled(true) +/// .build(); +/// let mut buffer = Vec::new(); +/// let mut writer = ArrowWriter::try_new(&mut buffer, batch.schema(), Some(props)).unwrap(); +/// writer.write(&batch).unwrap(); +/// writer.close().unwrap(); +/// +/// let options = ReadOptionsBuilder::new() +/// .with_reader_properties( +/// ReaderProperties::builder() +/// .set_read_bloom_filter(true) +/// .build(), +/// ) +/// .build(); +/// let reader = SerializedFileReader::new_with_options(Bytes::from(buffer), options).unwrap(); +/// let row_group = reader.get_row_group(0).unwrap(); +/// let sbbf = row_group.get_column_bloom_filter(0).unwrap(); +/// let column = reader.metadata().row_group(0).column(0).column_descr(); +/// +/// // The Int8 values were written as INT32, so checking an i8 misses them +/// assert!(!sbbf.check(&1_i8)); +/// +/// // Use the column's Arrow type, here Int8, from the file's Arrow schema +/// let arrow_type = batch.schema().field(0).data_type().clone(); +/// let sbbf = ArrowSbbf::try_new(sbbf, column, Some(arrow_type)).unwrap(); +/// let found = sbbf.check(&Int8Array::from(vec![1, 3, 4])).unwrap(); +/// assert_eq!(found, vec![Some(true), Some(true), Some(false)].into()); +/// ``` +/// +/// [`ArrowWriter`]: crate::arrow::ArrowWriter +#[derive(Debug, Clone)] +pub struct ArrowSbbf<'a> { + sbbf: &'a Sbbf, + /// The Arrow type of the arrays to check + arrow_type: DataType, + physical_type: PhysicalType, + /// The length of the column's values if it is a `FIXED_LEN_BYTE_ARRAY` + /// column, otherwise 0 + /// + /// Decimals are converted to this length, as other writers can store + /// decimals in more bytes than their precision needs. + type_length: usize, +} + +impl<'a> ArrowSbbf<'a> { + /// Create a new [`ArrowSbbf`] for the bloom filter `sbbf` of `column` + /// + /// `arrow_type` is the Arrow type of the arrays that will be checked, for + /// example the type of the column in the file's Arrow schema. If `None`, the + /// type is derived from `column` in the same way as when reading a file + /// without an Arrow schema. + /// + /// Returns an error if values of `arrow_type` cannot be stored in `column`, + /// or if `arrow_type` is a decimal and `column` is not a decimal of the same + /// scale, or the other way around. Review Comment: Suggest adding a note about INTERVAL handling requiring a type hint. `ArrowSbbf::try_new(..., None)` derives its type through `parquet_to_arrow_field` (`parquet/src/arrow/bloom_filter.rs:120–124`). Parquet INTERVAL inference chooses DayTime, explicitly acknowledging ambiguity in `parquet/src/arrow/schema/primitive.rs:324–333`. The reader's DayTime conversion drops the stored months component (`parquet/src/arrow/array_reader/fixed_len_byte_array.rs:264–271`). ########## parquet/src/arrow/bloom_filter.rs: ########## @@ -0,0 +1,1237 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Check Arrow arrays against Parquet bloom filters + +use arrow_array::builder::BooleanBuilder; +use arrow_array::cast::AsArray; +use arrow_array::types::{Float32Type, Float64Type, RunEndIndexType}; +use arrow_array::{Array, BooleanArray, RunArray, downcast_run_array}; +use arrow_buffer::{ArrowNativeType, BooleanBuffer}; +use arrow_schema::{DataType, Field}; + +use crate::arrow::arrow_writer::{ + FixedLenByteArrayVisitor, PrimitiveVisitor, visit_fixed_len_byte_array, visit_i32, visit_i64, +}; +use crate::arrow::schema::{arrow_to_parquet_type, parquet_to_arrow_field}; +use crate::basic::{ConvertedType, LogicalType, Type as PhysicalType}; +use crate::bloom_filter::Sbbf; +use crate::data_type::AsBytes; +use crate::errors::{ParquetError, Result}; +use crate::schema::types::{ColumnDescriptor, Type}; + +/// A Parquet bloom filter ([`Sbbf`]) that can be checked with Arrow arrays +/// +/// [`ArrowWriter`] may store an Arrow value as a different Parquet value, and +/// the bloom filter holds the stored value, so [`Sbbf::check`] with the Arrow +/// value can report a value that is present as absent. [`ArrowSbbf::check`] +/// converts Arrow values in the same way as [`ArrowWriter`] before checking the +/// filter. +/// +/// ``` +/// # use std::sync::Arc; +/// # use bytes::Bytes; +/// # use arrow_array::{ArrayRef, Int8Array, RecordBatch}; +/// # use parquet::arrow::ArrowWriter; +/// # use parquet::arrow::bloom_filter::ArrowSbbf; +/// # use parquet::file::properties::{ReaderProperties, WriterProperties}; +/// # use parquet::file::reader::{FileReader, SerializedFileReader}; +/// # use parquet::file::serialized_reader::ReadOptionsBuilder; +/// let array: ArrayRef = Arc::new(Int8Array::from(vec![1, 2, 3])); +/// let batch = RecordBatch::try_from_iter([("c", array)]).unwrap(); +/// +/// let props = WriterProperties::builder() +/// .set_bloom_filter_enabled(true) +/// .build(); +/// let mut buffer = Vec::new(); +/// let mut writer = ArrowWriter::try_new(&mut buffer, batch.schema(), Some(props)).unwrap(); +/// writer.write(&batch).unwrap(); +/// writer.close().unwrap(); +/// +/// let options = ReadOptionsBuilder::new() +/// .with_reader_properties( +/// ReaderProperties::builder() +/// .set_read_bloom_filter(true) +/// .build(), +/// ) +/// .build(); +/// let reader = SerializedFileReader::new_with_options(Bytes::from(buffer), options).unwrap(); +/// let row_group = reader.get_row_group(0).unwrap(); +/// let sbbf = row_group.get_column_bloom_filter(0).unwrap(); +/// let column = reader.metadata().row_group(0).column(0).column_descr(); +/// +/// // The Int8 values were written as INT32, so checking an i8 misses them +/// assert!(!sbbf.check(&1_i8)); +/// +/// // Use the column's Arrow type, here Int8, from the file's Arrow schema +/// let arrow_type = batch.schema().field(0).data_type().clone(); +/// let sbbf = ArrowSbbf::try_new(sbbf, column, Some(arrow_type)).unwrap(); +/// let found = sbbf.check(&Int8Array::from(vec![1, 3, 4])).unwrap(); +/// assert_eq!(found, vec![Some(true), Some(true), Some(false)].into()); +/// ``` +/// +/// [`ArrowWriter`]: crate::arrow::ArrowWriter +#[derive(Debug, Clone)] +pub struct ArrowSbbf<'a> { + sbbf: &'a Sbbf, + /// The Arrow type of the arrays to check + arrow_type: DataType, + physical_type: PhysicalType, + /// The length of the column's values if it is a `FIXED_LEN_BYTE_ARRAY` + /// column, otherwise 0 + /// + /// Decimals are converted to this length, as other writers can store + /// decimals in more bytes than their precision needs. + type_length: usize, +} + +impl<'a> ArrowSbbf<'a> { + /// Create a new [`ArrowSbbf`] for the bloom filter `sbbf` of `column` + /// + /// `arrow_type` is the Arrow type of the arrays that will be checked, for + /// example the type of the column in the file's Arrow schema. If `None`, the + /// type is derived from `column` in the same way as when reading a file + /// without an Arrow schema. + /// + /// Returns an error if values of `arrow_type` cannot be stored in `column`, + /// or if `arrow_type` is a decimal and `column` is not a decimal of the same + /// scale, or the other way around. + pub fn try_new( Review Comment: For an actual INTERVAL FLBA(12) column, `try_new(..., Some(Interval(MonthDayNano)))` succeeds because schema conversion accepts that layout. A matching array subsequently returns NYI in the writer visitor. Recommend rejecting this unsupported type in construction, including dictionary/REE wrappers. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
