alamb commented on code in PR #7401:
URL: https://github.com/apache/arrow-rs/pull/7401#discussion_r2039656300


##########
parquet/benches/arrow_reader_row_filter.rs:
##########
@@ -0,0 +1,325 @@
+// 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.
+
+//! Benchmark for evaluating row filters and projections on a Parquet file.
+//!
+//! This benchmark creates a Parquet file in memory with 100K rows and four 
columns:
+//!  - int64: sequential integers
+//!  - float64: floating-point values (derived from the integers)
+//!  - utf8View: string values where about half are non-empty,
+//!    and a few rows (every 10Kth row) are the constant "const"
+//!  - ts: timestamp values (using, e.g., a millisecond epoch)
+//!
+//! It then applies several filter functions and projections, benchmarking the 
read-back speed.
+//!
+//! Filters tested:
+//!  - A string filter: `utf8View <> ''` (non-empty)
+//!  - A string filter: `utf8View = 'const'` (selective)
+//!  - An integer non-selective filter (e.g. even numbers)
+//!  - An integer selective filter (e.g. `int64 = 0`)
+//!  - A timestamp filter (e.g. `ts > threshold`)
+//!
+//! Projections tested:
+//!  - All 4 columns.
+//!  - All columns except the one used for the filter.
+//!
+//! To run the benchmark, use `cargo bench --bench bench_filter_projection`.
+
+use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
+use std::sync::Arc;
+use tempfile::NamedTempFile;
+
+use arrow::array::{
+    ArrayRef, BooleanArray, BooleanBuilder, Float64Array, Int64Array, 
TimestampMillisecondArray,
+};
+use arrow::datatypes::{DataType, Field, Schema, TimeUnit};
+use arrow::record_batch::RecordBatch;
+use arrow_array::builder::StringViewBuilder;
+use arrow_array::{Array, StringViewArray};
+use criterion::async_executor::FuturesExecutor;
+use futures::TryStreamExt;
+use parquet::arrow::arrow_reader::{ArrowPredicateFn, ArrowReaderOptions, 
RowFilter};
+use parquet::arrow::{ArrowWriter, ParquetRecordBatchStreamBuilder, 
ProjectionMask};
+use parquet::file::properties::WriterProperties;
+use tokio::fs::File;
+use tokio::runtime::Runtime;
+
+/// Create a RecordBatch with 100K rows and four columns.
+fn make_record_batch() -> RecordBatch {
+    let num_rows = 100_000;
+
+    // int64 column: sequential numbers 0..num_rows
+    let int_values: Vec<i64> = (0..num_rows as i64).collect();
+    let int_array = Arc::new(Int64Array::from(int_values)) as ArrayRef;
+
+    // float64 column: derived from int64 (e.g., multiplied by 0.1)
+    let float_values: Vec<f64> = (0..num_rows).map(|i| i as f64 * 
0.1).collect();
+    let float_array = Arc::new(Float64Array::from(float_values)) as ArrayRef;
+
+    // utf8View column: even rows get non-empty strings; odd rows get an empty 
string;
+    // every 10Kth even row is "const" to be selective.
+    let mut string_view_builder = StringViewBuilder::with_capacity(100_000);
+    for i in 0..num_rows {
+        if i % 2 == 0 {
+            if i % 10_000 == 0 {
+                string_view_builder.append_value("const");
+            } else {
+                string_view_builder.append_value("nonempty");
+            }
+        } else {
+            string_view_builder.append_value("");
+        }
+    }
+    let utf8_view_array = Arc::new(string_view_builder.finish()) as ArrayRef;
+
+    // Timestamp column: using milliseconds from an epoch (simply using the 
row index)
+    let ts_values: Vec<i64> = (0..num_rows as i64).collect();
+    let ts_array = Arc::new(TimestampMillisecondArray::from(ts_values)) as 
ArrayRef;
+
+    let schema = Arc::new(Schema::new(vec![
+        Field::new("int64", DataType::Int64, false),
+        Field::new("float64", DataType::Float64, false),
+        Field::new("utf8View", DataType::Utf8View, false),
+        Field::new(
+            "ts",
+            DataType::Timestamp(TimeUnit::Millisecond, None),
+            false,
+        ),
+    ]));
+
+    RecordBatch::try_new(
+        schema,
+        vec![int_array, float_array, utf8_view_array, ts_array],
+    )
+    .unwrap()
+}
+
+/// Writes the record batch to a temporary Parquet file.
+fn write_parquet_file() -> NamedTempFile {
+    let batch = make_record_batch();
+    let schema = batch.schema();
+    let props = WriterProperties::builder().build();
+
+    let file = tempfile::Builder::new()
+        .suffix(".parquet")
+        .tempfile()
+        .unwrap();
+    {
+        let file_reopen = file.reopen().unwrap();
+        let mut writer = ArrowWriter::try_new(file_reopen, schema.clone(), 
Some(props)).unwrap();
+        // Write the entire batch as a single row group.
+        writer.write(&batch).unwrap();
+        writer.close().unwrap();
+    }
+    file
+}
+
+/// Filter function: returns a BooleanArray with true when utf8View <> "".
+fn filter_utf8_view_nonempty(batch: &RecordBatch) -> BooleanArray {

Review Comment:
   Since these functions are specific to the `FilterType` you could potentially 
add them as methods,  like
   
   ```rust
   impl FilterType {
     fn filter_batch(&self, batch: &RecordBatch) -> BooleanArray {
       match self {
         Utf8ViewNonEmpty => {
            // iimplement filter here
         }
   ...
       }
     }
   }



-- 
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]

Reply via email to