From 75b76eadf79498fc28563c47d47e8607999d1036 Mon Sep 17 00:00:00 2001 From: Liang-Chi Hsieh Date: Sun, 31 May 2026 10:20:00 -0700 Subject: [PATCH 1/6] bench(reader): add ArrowReader benchmark harness There were no in-repo benchmarks for the ArrowReader, so every performance claim in the perf epic (#2172) had to be validated against external workloads. This adds a criterion harness that writes Parquet files to a local temp dir and reads them back through the normal FileIO path, measuring the per-FileScanTask overhead that dominates scans of tables with many small files. Benchmark groups: - many_small_files: scans 16/64/256 small files, reporting files/sec so per-file overhead is directly visible. - concurrency: a fixed corpus read at concurrency 1/4/16, exercising both the single-concurrency fast path and the buffered/flattened multi-task path. - migrated_table: files without embedded field IDs read via name mapping, isolating the migrated-table schema-resolution cost. - same_file_splits: one multi-row-group file read as 1/8/32 byte-range tasks, surfacing the redundant per-split metadata fetch that metadata caching (item #5) targets. - with_predicate: scans carrying a bound predicate with row-group filtering and row selection enabled, exercising the per-task row-filter setup. This gives a reproducible baseline for evaluating reader optimizations such as operator caching (#2177) and metadata reuse. (cherry picked from commit 562f4c739e66414cec644ed09c8c7b3a330ac780) --- Cargo.lock | 203 ++++++++++++ Cargo.toml | 1 + crates/iceberg/Cargo.toml | 5 + crates/iceberg/benches/arrow_reader.rs | 417 +++++++++++++++++++++++++ 4 files changed, 626 insertions(+) create mode 100644 crates/iceberg/benches/arrow_reader.rs diff --git a/Cargo.lock b/Cargo.lock index bbf366a32f..35d7d8858a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -114,6 +114,12 @@ dependencies = [ "libc", ] +[[package]] +name = "anes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" + [[package]] name = "anstyle" version = "1.0.14" @@ -1299,6 +1305,12 @@ dependencies = [ "either", ] +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + [[package]] name = "cbc" version = "0.1.2" @@ -1382,6 +1394,33 @@ dependencies = [ "phf", ] +[[package]] +name = "ciborium" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" + +[[package]] +name = "ciborium-ll" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" +dependencies = [ + "ciborium-io", + "half", +] + [[package]] name = "cipher" version = "0.4.4" @@ -1403,6 +1442,31 @@ dependencies = [ "inout 0.2.2", ] +[[package]] +name = "clap" +version = "4.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "844c5c0b65be192cb68d570cca1b21a6444431d24e39c084dc0336177015337c" +dependencies = [ + "clap_builder", +] + +[[package]] +name = "clap_builder" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0797fb7aeb1406c84efac526901f7ec3ead2124f946b494e72879d4b54704d" +dependencies = [ + "anstyle", + "clap_lex", +] + +[[package]] +name = "clap_lex" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0921b3876b3a1df8a1c9f912e7f94342458022c2e84288015893d83c4d62a79" + [[package]] name = "cmake" version = "0.1.58" @@ -1583,6 +1647,44 @@ dependencies = [ "cfg-if 1.0.4", ] +[[package]] +name = "criterion" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" +dependencies = [ + "anes", + "cast", + "ciborium", + "clap", + "criterion-plot", + "futures", + "is-terminal", + "itertools 0.10.5", + "num-traits", + "once_cell", + "oorandom", + "plotters", + "rayon", + "regex", + "serde", + "serde_derive", + "serde_json", + "tinytemplate", + "tokio", + "walkdir", +] + +[[package]] +name = "criterion-plot" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" +dependencies = [ + "cast", + "itertools 0.10.5", +] + [[package]] name = "crossbeam-channel" version = "0.5.16" @@ -1592,6 +1694,16 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "crossbeam-deque" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + [[package]] name = "crossbeam-epoch" version = "0.9.20" @@ -2582,6 +2694,12 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hermit-abi" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e17592d60ebacc7d5e169f4663c5f84f9161cc90328abcfe8456f41e4dfcb284" + [[package]] name = "hex" version = "0.4.3" @@ -2855,6 +2973,7 @@ dependencies = [ "bytes", "chrono", "crc32fast", + "criterion", "derive_builder", "expect-test", "fastnum", @@ -3281,6 +3400,26 @@ version = "2.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d98f6fed1fde3f8c21bc40a1abb88dd75e67924f9cffc3ef95607bad8017f8e2" +[[package]] +name = "is-terminal" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + [[package]] name = "itertools" version = "0.13.0" @@ -4089,6 +4228,12 @@ version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "269bca4c2591a28585d6bf10d9ed0332b7d76900a1b02bec41bdc3a2cdcda107" +[[package]] +name = "oorandom" +version = "11.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" + [[package]] name = "opaque-debug" version = "0.3.1" @@ -4657,6 +4802,34 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" +[[package]] +name = "plotters" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" +dependencies = [ + "num-traits", + "plotters-backend", + "plotters-svg", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "plotters-backend" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" + +[[package]] +name = "plotters-svg" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" +dependencies = [ + "plotters-backend", +] + [[package]] name = "polyval" version = "0.6.2" @@ -5093,6 +5266,26 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "rayon" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "redb" version = "3.1.3" @@ -6613,6 +6806,16 @@ dependencies = [ "zerovec", ] +[[package]] +name = "tinytemplate" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "tinyvec" version = "1.11.0" diff --git a/Cargo.toml b/Cargo.toml index 1660c8c521..6066e213c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -85,6 +85,7 @@ bytes = "1.11" cfg-if = "1" chrono = "0.4.41" crc32fast = "1" +criterion = { version = "0.5", features = ["async_tokio"] } derive_builder = "0.20" expect-test = "1" fastnum = { version = "0.7", default-features = false, features = [ diff --git a/crates/iceberg/Cargo.toml b/crates/iceberg/Cargo.toml index 45e0cae412..b326f51a7b 100644 --- a/crates/iceberg/Cargo.toml +++ b/crates/iceberg/Cargo.toml @@ -93,6 +93,7 @@ zeroize = { workspace = true } zstd = { workspace = true } [dev-dependencies] +criterion = { workspace = true } expect-test = { workspace = true } iceberg_test_utils = { path = "../test_utils", features = ["tests"] } minijinja = { workspace = true } @@ -109,5 +110,9 @@ tracing-subscriber = { workspace = true } # These dependencies are added to ensure minimal dependency version ignored = ["tap"] +[[bench]] +harness = false +name = "arrow_reader" + [lints] workspace = true diff --git a/crates/iceberg/benches/arrow_reader.rs b/crates/iceberg/benches/arrow_reader.rs new file mode 100644 index 0000000000..6562dab749 --- /dev/null +++ b/crates/iceberg/benches/arrow_reader.rs @@ -0,0 +1,417 @@ +// 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. + +//! Benchmarks for [`ArrowReader`], focused on the per-`FileScanTask` overhead +//! that dominates scans of tables with many small data files (see epic #2172). +//! +//! The files are written to a local temp directory and read back through the +//! normal `FileIO` (local FS) path, so these measure CPU and per-task work +//! (operator construction, metadata loading, schema resolution, projection / +//! row-filter setup, stream wiring) rather than network latency. They give an +//! in-repo, reproducible baseline against which I/O- and CPU-reuse +//! optimizations can be measured. +//! +//! Run with: `cargo bench -p iceberg --bench arrow_reader` + +use std::sync::Arc; + +use arrow_array::{ArrayRef, Int64Array, RecordBatch, StringArray}; +use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; +use futures::{TryStreamExt, stream}; +use iceberg::Runtime; +use iceberg::arrow::ArrowReaderBuilder; +use iceberg::expr::{Bind, Reference}; +use iceberg::io::FileIO; +use iceberg::scan::{FileScanTask, FileScanTaskStream}; +use iceberg::spec::{ + DataFileFormat, Datum, MappedField, NameMapping, NestedField, PrimitiveType, Schema, SchemaRef, + Type, +}; +use parquet::arrow::{ArrowWriter, PARQUET_FIELD_ID_META_KEY}; +use parquet::basic::Compression; +use parquet::file::properties::WriterProperties; +use tempfile::TempDir; +use tokio::runtime::Runtime as TokioRuntime; + +/// Rows written into each generated Parquet data file. Kept small so the +/// benchmark stresses per-file overhead (the regime epic #2172 targets) rather +/// than column decoding throughput. +const ROWS_PER_FILE: usize = 100; + +/// Build the Iceberg + Arrow schema pair used for every generated file: +/// `(id: long, name: string)` with embedded Parquet field IDs, so the reader +/// takes the "file has field IDs" fast path. +fn schemas() -> (SchemaRef, arrow_schema::SchemaRef) { + let iceberg_schema = Arc::new( + Schema::builder() + .with_schema_id(1) + .with_fields(vec![ + NestedField::required(1, "id", Type::Primitive(PrimitiveType::Long)).into(), + NestedField::optional(2, "name", Type::Primitive(PrimitiveType::String)).into(), + ]) + .build() + .unwrap(), + ); + + let arrow_schema = Arc::new(arrow_schema::Schema::new(vec![ + arrow_schema::Field::new("id", arrow_schema::DataType::Int64, false).with_metadata( + std::collections::HashMap::from([( + PARQUET_FIELD_ID_META_KEY.to_string(), + "1".to_string(), + )]), + ), + arrow_schema::Field::new("name", arrow_schema::DataType::Utf8, true).with_metadata( + std::collections::HashMap::from([( + PARQUET_FIELD_ID_META_KEY.to_string(), + "2".to_string(), + )]), + ), + ])); + + (iceberg_schema, arrow_schema) +} + +/// Write `num_files` small Parquet files into `dir` and return their paths. +fn write_files( + dir: &TempDir, + num_files: usize, + arrow_schema: &arrow_schema::SchemaRef, +) -> Vec { + let ids: ArrayRef = Arc::new(Int64Array::from_iter_values(0..ROWS_PER_FILE as i64)); + let names: ArrayRef = Arc::new(StringArray::from_iter_values( + (0..ROWS_PER_FILE).map(|i| format!("row-{i}")), + )); + let batch = + RecordBatch::try_new(arrow_schema.clone(), vec![ids, names]).expect("build record batch"); + + let props = WriterProperties::builder() + .set_compression(Compression::SNAPPY) + .build(); + + (0..num_files) + .map(|i| { + let path = format!("{}/data_{i}.parquet", dir.path().to_str().unwrap()); + let file = std::fs::File::create(&path).expect("create parquet file"); + let mut writer = ArrowWriter::try_new(file, arrow_schema.clone(), Some(props.clone())) + .expect("create arrow writer"); + writer.write(&batch).expect("write batch"); + writer.close().expect("close writer"); + path + }) + .collect() +} + +/// Arrow schema WITHOUT embedded Parquet field IDs, simulating files migrated +/// from Hive/Spark via `add_files`. Forces the reader's name-mapping branch. +fn arrow_schema_without_field_ids() -> arrow_schema::SchemaRef { + Arc::new(arrow_schema::Schema::new(vec![ + arrow_schema::Field::new("id", arrow_schema::DataType::Int64, false), + arrow_schema::Field::new("name", arrow_schema::DataType::Utf8, true), + ])) +} + +/// Name mapping that assigns the table's field IDs by column name — what the +/// reader applies when a file lacks embedded field IDs. +fn name_mapping() -> Arc { + Arc::new(NameMapping::new(vec![ + MappedField::new(Some(1), vec!["id".to_string()], vec![]), + MappedField::new(Some(2), vec!["name".to_string()], vec![]), + ])) +} + +/// Write a single Parquet file with many small row groups, so that byte-range +/// splits select disjoint row-group sets — the same-file-split scenario that +/// metadata caching (#2100 / item #5) targets. +fn write_multi_row_group_file( + dir: &TempDir, + total_rows: usize, + rows_per_row_group: usize, + arrow_schema: &arrow_schema::SchemaRef, +) -> String { + let ids: ArrayRef = Arc::new(Int64Array::from_iter_values(0..total_rows as i64)); + let names: ArrayRef = Arc::new(StringArray::from_iter_values( + (0..total_rows).map(|i| format!("row-{i}")), + )); + let batch = + RecordBatch::try_new(arrow_schema.clone(), vec![ids, names]).expect("build record batch"); + + let props = WriterProperties::builder() + .set_compression(Compression::SNAPPY) + .set_max_row_group_row_count(Some(rows_per_row_group)) + .build(); + + let path = format!("{}/big.parquet", dir.path().to_str().unwrap()); + let file = std::fs::File::create(&path).expect("create parquet file"); + let mut writer = + ArrowWriter::try_new(file, arrow_schema.clone(), Some(props)).expect("create arrow writer"); + writer.write(&batch).expect("write batch"); + writer.close().expect("close writer"); + path +} + +/// Build a whole-file `FileScanTask` for each path (no predicate, no deletes — +/// the projection-only path, which isolates per-task setup overhead). +fn tasks_for(paths: &[String], schema: &SchemaRef) -> Vec { + paths.iter().map(|p| base_task(p, schema)).collect() +} + +/// A whole-file projection-only task for a single path. +fn base_task(path: &str, schema: &SchemaRef) -> FileScanTask { + let size = std::fs::metadata(path).expect("stat parquet file").len(); + FileScanTask { + file_size_in_bytes: size, + start: 0, + length: size, + record_count: Some(ROWS_PER_FILE as u64), + data_file_path: path.to_string(), + data_file_format: DataFileFormat::Parquet, + schema: schema.clone(), + project_field_ids: vec![1, 2], + predicate: None, + deletes: vec![], + partition: None, + partition_spec: None, + name_mapping: None, + case_sensitive: false, + } +} + +/// Like [`tasks_for`] but attaches a name mapping (migrated-table path). +fn tasks_with_name_mapping(paths: &[String], schema: &SchemaRef) -> Vec { + let mapping = name_mapping(); + paths + .iter() + .map(|p| FileScanTask { + name_mapping: Some(mapping.clone()), + ..base_task(p, schema) + }) + .collect() +} + +/// Build `num_splits` byte-range tasks over the SAME file, partitioning its +/// length into contiguous ranges (mirroring how Iceberg Java splits a large +/// file across partitions). +fn same_file_split_tasks(path: &str, schema: &SchemaRef, num_splits: u64) -> Vec { + let size = std::fs::metadata(path).expect("stat parquet file").len(); + let chunk = size.div_ceil(num_splits); + (0..num_splits) + .map(|i| { + let start = i * chunk; + let length = chunk.min(size.saturating_sub(start)); + FileScanTask { + file_size_in_bytes: size, + start, + length, + record_count: None, + data_file_path: path.to_string(), + data_file_format: DataFileFormat::Parquet, + schema: schema.clone(), + project_field_ids: vec![1, 2], + predicate: None, + deletes: vec![], + partition: None, + partition_spec: None, + name_mapping: None, + case_sensitive: false, + } + }) + .collect() +} + +/// Like [`tasks_for`] but attaches a bound predicate (`id < total/2`), +/// exercising row-filter and row-group-filtering setup per task. +fn tasks_with_predicate(paths: &[String], schema: &SchemaRef) -> Vec { + let predicate = Reference::new("id") + .less_than(Datum::long(ROWS_PER_FILE as i64 / 2)) + .bind(schema.clone(), true) + .expect("bind predicate"); + paths + .iter() + .map(|p| FileScanTask { + predicate: Some(predicate.clone()), + ..base_task(p, schema) + }) + .collect() +} + +/// Read every task to completion, draining all record batches. `filtering` +/// turns on row-group filtering and row selection (only meaningful when the +/// tasks carry a predicate). +async fn read_all(tasks: Vec, concurrency: usize, filtering: bool) { + let file_io = FileIO::new_with_fs(); + let reader = ArrowReaderBuilder::new(file_io, Runtime::current()) + .with_data_file_concurrency_limit(concurrency) + .with_row_group_filtering_enabled(filtering) + .with_row_selection_enabled(filtering) + .build(); + + let task_stream: FileScanTaskStream = Box::pin(stream::iter(tasks.into_iter().map(Ok))); + + let batches: Vec = reader + .read(task_stream) + .expect("build read stream") + .stream() + .try_collect() + .await + .expect("read all batches"); + + criterion::black_box(batches); +} + +/// Scans of many small files at a fixed concurrency. Throughput is reported in +/// files/sec so the per-file overhead is directly visible across file counts. +fn bench_many_small_files(c: &mut Criterion) { + let tokio = TokioRuntime::new().unwrap(); + let (iceberg_schema, arrow_schema) = schemas(); + + let mut group = c.benchmark_group("many_small_files"); + for num_files in [16usize, 64, 256] { + // Files are written once and reused across all samples for this size. + let dir = TempDir::new().unwrap(); + let paths = write_files(&dir, num_files, &arrow_schema); + + group.throughput(Throughput::Elements(num_files as u64)); + group.bench_with_input( + BenchmarkId::from_parameter(num_files), + &num_files, + |b, _| { + b.to_async(&tokio).iter(|| { + let tasks = tasks_for(&paths, &iceberg_schema); + read_all(tasks, num_cpus_limit(), false) + }); + }, + ); + } + group.finish(); +} + +/// How concurrency affects throughput for a fixed corpus — exercises both the +/// single-concurrency fast path and the buffered/flattened multi-task path. +fn bench_concurrency(c: &mut Criterion) { + let tokio = TokioRuntime::new().unwrap(); + let (iceberg_schema, arrow_schema) = schemas(); + + let num_files = 128usize; + let dir = TempDir::new().unwrap(); + let paths = write_files(&dir, num_files, &arrow_schema); + + let mut group = c.benchmark_group("concurrency"); + group.throughput(Throughput::Elements(num_files as u64)); + for concurrency in [1usize, 4, 16] { + group.bench_with_input( + BenchmarkId::from_parameter(concurrency), + &concurrency, + |b, &concurrency| { + b.to_async(&tokio).iter(|| { + let tasks = tasks_for(&paths, &iceberg_schema); + read_all(tasks, concurrency, false) + }); + }, + ); + } + group.finish(); +} + +/// Migrated tables: files lack embedded field IDs, so each task must apply the +/// name mapping and rebuild `ArrowReaderMetadata`. Compared against the +/// field-ID fast path, this isolates the migrated-table schema-resolution cost. +fn bench_migrated_table(c: &mut Criterion) { + let tokio = TokioRuntime::new().unwrap(); + let (iceberg_schema, _) = schemas(); + let arrow_schema = arrow_schema_without_field_ids(); + + let num_files = 128usize; + let dir = TempDir::new().unwrap(); + let paths = write_files(&dir, num_files, &arrow_schema); + + let mut group = c.benchmark_group("migrated_table"); + group.throughput(Throughput::Elements(num_files as u64)); + group.bench_function("name_mapping", |b| { + b.to_async(&tokio).iter(|| { + let tasks = tasks_with_name_mapping(&paths, &iceberg_schema); + read_all(tasks, num_cpus_limit(), false) + }); + }); + group.finish(); +} + +/// Same-file splits: one large multi-row-group file read as N byte-range +/// tasks. Each task currently re-fetches the file's Parquet metadata +/// independently — the scenario metadata caching (#2100 / item #5) targets. +fn bench_same_file_splits(c: &mut Criterion) { + let tokio = TokioRuntime::new().unwrap(); + let (iceberg_schema, arrow_schema) = schemas(); + + let total_rows = 100_000usize; + let rows_per_row_group = 2_000usize; + let dir = TempDir::new().unwrap(); + let path = write_multi_row_group_file(&dir, total_rows, rows_per_row_group, &arrow_schema); + + let mut group = c.benchmark_group("same_file_splits"); + for num_splits in [1u64, 8, 32] { + group.throughput(Throughput::Elements(num_splits)); + group.bench_with_input( + BenchmarkId::from_parameter(num_splits), + &num_splits, + |b, &num_splits| { + b.to_async(&tokio).iter(|| { + let tasks = same_file_split_tasks(&path, &iceberg_schema, num_splits); + read_all(tasks, num_cpus_limit(), false) + }); + }, + ); + } + group.finish(); +} + +/// Scans carrying a predicate, with row-group filtering and row selection +/// enabled — exercises the per-task row-filter / field-id-map / row-group +/// pruning setup that the projection-only benchmarks skip. +fn bench_with_predicate(c: &mut Criterion) { + let tokio = TokioRuntime::new().unwrap(); + let (iceberg_schema, arrow_schema) = schemas(); + + let num_files = 128usize; + let dir = TempDir::new().unwrap(); + let paths = write_files(&dir, num_files, &arrow_schema); + + let mut group = c.benchmark_group("with_predicate"); + group.throughput(Throughput::Elements(num_files as u64)); + group.bench_function("id_lt_half", |b| { + b.to_async(&tokio).iter(|| { + let tasks = tasks_with_predicate(&paths, &iceberg_schema); + read_all(tasks, num_cpus_limit(), true) + }); + }); + group.finish(); +} + +fn num_cpus_limit() -> usize { + std::thread::available_parallelism() + .map(|p| p.get()) + .unwrap_or(4) +} + +criterion_group!( + benches, + bench_many_small_files, + bench_concurrency, + bench_migrated_table, + bench_same_file_splits, + bench_with_predicate, +); +criterion_main!(benches); From c2c8bf5a1bc8433a13ea920ca07542635b83692e Mon Sep 17 00:00:00 2001 From: Liang-Chi Hsieh Date: Sun, 31 May 2026 16:05:14 -0700 Subject: [PATCH 2/6] bench(reader): report bytes_read for same-file splits Wall-clock time on the local FS cannot show the benefit of round-trip-saving optimizations, but it also undersells the same-file-split problem, whose cost is redundant *bytes fetched* rather than latency. Report ScanMetrics::bytes_read as the same_file_splits throughput basis (a deterministic count, not a sample), and print the read amplification per split count. Reading one ~1.2MB file as 32 byte-range splits fetches ~15.7x the file size, because each split re-fetches the Parquet metadata independently. This is the machine-independent evidence for the cost that same-file metadata caching (proposed in #2172, attempted in #2100) targets; a caching implementation would drive the amplification back toward 1x. (cherry picked from commit 736466fd321ea7d504ab906b6c62b8f436a92784) --- crates/iceberg/benches/arrow_reader.rs | 52 +++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/crates/iceberg/benches/arrow_reader.rs b/crates/iceberg/benches/arrow_reader.rs index 6562dab749..1152137010 100644 --- a/crates/iceberg/benches/arrow_reader.rs +++ b/crates/iceberg/benches/arrow_reader.rs @@ -25,6 +25,14 @@ //! in-repo, reproducible baseline against which I/O- and CPU-reuse //! optimizations can be measured. //! +//! The `same_file_splits` group additionally reports `ScanMetrics::bytes_read` +//! as its throughput basis. Because that is a deterministic count (not a +//! wall-clock sample), it surfaces redundant I/O directly: reading one file as +//! N byte-range splits fetches the Parquet metadata N times, so total bytes +//! read grows with the split count even though the file contents are identical. +//! This is the cost that same-file metadata caching (proposed in #2172, +//! attempted in #2100) targets, and is measurable even on the local FS. +//! //! Run with: `cargo bench -p iceberg --bench arrow_reader` use std::sync::Arc; @@ -272,6 +280,30 @@ async fn read_all(tasks: Vec, concurrency: usize, filtering: bool) criterion::black_box(batches); } +/// Read every task to completion and return the total number of bytes fetched +/// from storage (`ScanMetrics::bytes_read`). Unlike wall-clock time this is a +/// deterministic measurement, which makes it the right tool for surfacing +/// redundant I/O — e.g. the per-split metadata re-fetch that same-file metadata +/// caching (proposed in #2172, attempted in #2100) targets. +async fn read_all_bytes(tasks: Vec, concurrency: usize) -> u64 { + let file_io = FileIO::new_with_fs(); + let reader = ArrowReaderBuilder::new(file_io, Runtime::current()) + .with_data_file_concurrency_limit(concurrency) + .build(); + + let task_stream: FileScanTaskStream = Box::pin(stream::iter(tasks.into_iter().map(Ok))); + + let result = reader.read(task_stream).expect("build read stream"); + let metrics = result.metrics().clone(); + let _: Vec = result + .stream() + .try_collect() + .await + .expect("read all batches"); + + metrics.bytes_read() +} + /// Scans of many small files at a fixed concurrency. Throughput is reported in /// files/sec so the per-file overhead is directly visible across file counts. fn bench_many_small_files(c: &mut Criterion) { @@ -351,7 +383,8 @@ fn bench_migrated_table(c: &mut Criterion) { /// Same-file splits: one large multi-row-group file read as N byte-range /// tasks. Each task currently re-fetches the file's Parquet metadata -/// independently — the scenario metadata caching (#2100 / item #5) targets. +/// independently — the scenario same-file metadata caching (proposed in #2172, +/// attempted in #2100) targets. fn bench_same_file_splits(c: &mut Criterion) { let tokio = TokioRuntime::new().unwrap(); let (iceberg_schema, arrow_schema) = schemas(); @@ -360,10 +393,25 @@ fn bench_same_file_splits(c: &mut Criterion) { let rows_per_row_group = 2_000usize; let dir = TempDir::new().unwrap(); let path = write_multi_row_group_file(&dir, total_rows, rows_per_row_group, &arrow_schema); + let file_size = std::fs::metadata(&path).expect("stat parquet file").len(); let mut group = c.benchmark_group("same_file_splits"); for num_splits in [1u64, 8, 32] { - group.throughput(Throughput::Elements(num_splits)); + // Measure bytes_read once (deterministic) and report it as the + // benchmark's throughput basis, so criterions own output shows how + // total bytes fetched grows with the split count even though the file + // contents are identical — i.e. the redundant per-split metadata I/O. + let bytes_read = tokio.block_on(read_all_bytes( + same_file_split_tasks(&path, &iceberg_schema, num_splits), + num_cpus_limit(), + )); + eprintln!( + "same_file_splits/{num_splits}: bytes_read = {bytes_read} \ + ({:.2}x file size of {file_size})", + bytes_read as f64 / file_size as f64 + ); + + group.throughput(Throughput::Bytes(bytes_read)); group.bench_with_input( BenchmarkId::from_parameter(num_splits), &num_splits, From 0bd4cc6b62fe6e1f8abc7a556c1f2dcc08e4c92e Mon Sep 17 00:00:00 2001 From: Liang-Chi Hsieh Date: Thu, 2 Jul 2026 08:27:02 -0700 Subject: [PATCH 3/6] bench(reader): assert row counts; document measurement scope Address review feedback from mbutrovich: - Every group now asserts the expected total row count, so a change that reads fewer or duplicated rows fails loudly instead of reporting a bogus win. The same_file_splits assertion doubles as a live regression guard for the #2614 sub-row-group-split duplication. - The module doc now states the measurement regime explicitly: warm-cache CPU/decode path on local FS; operator caching (#2177) and the latency items (#2173, #2181) are not measurable here and a flat local result must not be read as "no benefit"; only Int64/Utf8 decode is exercised. (cherry picked from commit 44b6354f027ad2b8b0eb88c28e63a280ec0f4c9d) --- crates/iceberg/benches/arrow_reader.rs | 88 ++++++++++++++++++++------ 1 file changed, 70 insertions(+), 18 deletions(-) diff --git a/crates/iceberg/benches/arrow_reader.rs b/crates/iceberg/benches/arrow_reader.rs index 1152137010..64af84a884 100644 --- a/crates/iceberg/benches/arrow_reader.rs +++ b/crates/iceberg/benches/arrow_reader.rs @@ -18,20 +18,39 @@ //! Benchmarks for [`ArrowReader`], focused on the per-`FileScanTask` overhead //! that dominates scans of tables with many small data files (see epic #2172). //! +//! # What this measures +//! //! The files are written to a local temp directory and read back through the -//! normal `FileIO` (local FS) path, so these measure CPU and per-task work -//! (operator construction, metadata loading, schema resolution, projection / -//! row-filter setup, stream wiring) rather than network latency. They give an -//! in-repo, reproducible baseline against which I/O- and CPU-reuse -//! optimizations can be measured. +//! normal `FileIO` (local FS) path, so the time-based groups measure the CPU / +//! decode / per-task setup path: metadata parsing, schema resolution, +//! projection and row-filter setup, and stream wiring. After the first sample +//! the files sit in the OS page cache, so these are *warm-cache* numbers, not +//! cold first-touch reads. +//! +//! Every group asserts the expected total row count, so a change that reads +//! fewer (or duplicated) rows fails loudly instead of reporting a bogus win. +//! +//! # What this cannot measure +//! +//! The local-FS `FileIO` has no credential-provider init, request signing, +//! TLS, or network round-trips — the expensive parts of per-task object-store +//! access. Consequently: +//! +//! - operator caching (#2177) will look near-flat here, and +//! - the latency-hiding items (#2173, #2181) are not measurable at all. +//! +//! A flat local result for those must not be read as "the optimization does +//! not help" — that misreading is part of why #2100 was closed. Likewise the +//! schema is `Int64` + `Utf8` only: timestamp / decimal / nested / INT96 +//! column-decode regressions will not show up in these numbers. //! //! The `same_file_splits` group additionally reports `ScanMetrics::bytes_read` //! as its throughput basis. Because that is a deterministic count (not a -//! wall-clock sample), it surfaces redundant I/O directly: reading one file as -//! N byte-range splits fetches the Parquet metadata N times, so total bytes -//! read grows with the split count even though the file contents are identical. -//! This is the cost that same-file metadata caching (proposed in #2172, -//! attempted in #2100) targets, and is measurable even on the local FS. +//! wall-clock sample), it surfaces redundant I/O directly even on the local +//! FS: reading one file as N byte-range splits fetches the Parquet metadata N +//! times, so total bytes read grows with the split count even though the file +//! contents are identical. This is the cost that same-file metadata caching +//! (proposed in #2172, attempted in #2100) targets. //! //! Run with: `cargo bench -p iceberg --bench arrow_reader` @@ -259,7 +278,16 @@ fn tasks_with_predicate(paths: &[String], schema: &SchemaRef) -> Vec, concurrency: usize, filtering: bool) { +/// +/// Asserts that exactly `expected_rows` rows come back: a configuration that +/// reads too few rows — or duplicates rows, like the sub-row-group-split bug +/// in #2614 — would otherwise time faster and read as a performance win. +async fn read_all( + tasks: Vec, + concurrency: usize, + filtering: bool, + expected_rows: usize, +) { let file_io = FileIO::new_with_fs(); let reader = ArrowReaderBuilder::new(file_io, Runtime::current()) .with_data_file_concurrency_limit(concurrency) @@ -277,6 +305,12 @@ async fn read_all(tasks: Vec, concurrency: usize, filtering: bool) .await .expect("read all batches"); + let total_rows: usize = batches.iter().map(|b| b.num_rows()).sum(); + assert_eq!( + total_rows, expected_rows, + "benchmark read returned the wrong number of rows; timings would be meaningless" + ); + criterion::black_box(batches); } @@ -285,7 +319,10 @@ async fn read_all(tasks: Vec, concurrency: usize, filtering: bool) /// deterministic measurement, which makes it the right tool for surfacing /// redundant I/O — e.g. the per-split metadata re-fetch that same-file metadata /// caching (proposed in #2172, attempted in #2100) targets. -async fn read_all_bytes(tasks: Vec, concurrency: usize) -> u64 { +/// +/// The same `expected_rows` assertion as [`read_all`] applies, so the byte +/// counts always describe a correct read. +async fn read_all_bytes(tasks: Vec, concurrency: usize, expected_rows: usize) -> u64 { let file_io = FileIO::new_with_fs(); let reader = ArrowReaderBuilder::new(file_io, Runtime::current()) .with_data_file_concurrency_limit(concurrency) @@ -295,12 +332,18 @@ async fn read_all_bytes(tasks: Vec, concurrency: usize) -> u64 { let result = reader.read(task_stream).expect("build read stream"); let metrics = result.metrics().clone(); - let _: Vec = result + let batches: Vec = result .stream() .try_collect() .await .expect("read all batches"); + let total_rows: usize = batches.iter().map(|b| b.num_rows()).sum(); + assert_eq!( + total_rows, expected_rows, + "benchmark read returned the wrong number of rows; byte counts would be meaningless" + ); + metrics.bytes_read() } @@ -323,7 +366,7 @@ fn bench_many_small_files(c: &mut Criterion) { |b, _| { b.to_async(&tokio).iter(|| { let tasks = tasks_for(&paths, &iceberg_schema); - read_all(tasks, num_cpus_limit(), false) + read_all(tasks, num_cpus_limit(), false, num_files * ROWS_PER_FILE) }); }, ); @@ -350,7 +393,7 @@ fn bench_concurrency(c: &mut Criterion) { |b, &concurrency| { b.to_async(&tokio).iter(|| { let tasks = tasks_for(&paths, &iceberg_schema); - read_all(tasks, concurrency, false) + read_all(tasks, concurrency, false, num_files * ROWS_PER_FILE) }); }, ); @@ -375,7 +418,7 @@ fn bench_migrated_table(c: &mut Criterion) { group.bench_function("name_mapping", |b| { b.to_async(&tokio).iter(|| { let tasks = tasks_with_name_mapping(&paths, &iceberg_schema); - read_all(tasks, num_cpus_limit(), false) + read_all(tasks, num_cpus_limit(), false, num_files * ROWS_PER_FILE) }); }); group.finish(); @@ -404,6 +447,7 @@ fn bench_same_file_splits(c: &mut Criterion) { let bytes_read = tokio.block_on(read_all_bytes( same_file_split_tasks(&path, &iceberg_schema, num_splits), num_cpus_limit(), + total_rows, )); eprintln!( "same_file_splits/{num_splits}: bytes_read = {bytes_read} \ @@ -418,7 +462,9 @@ fn bench_same_file_splits(c: &mut Criterion) { |b, &num_splits| { b.to_async(&tokio).iter(|| { let tasks = same_file_split_tasks(&path, &iceberg_schema, num_splits); - read_all(tasks, num_cpus_limit(), false) + // `total_rows` doubles as the #2614 duplication guard: N splits + // of one file must reassemble it exactly once. + read_all(tasks, num_cpus_limit(), false, total_rows) }); }, ); @@ -442,7 +488,13 @@ fn bench_with_predicate(c: &mut Criterion) { group.bench_function("id_lt_half", |b| { b.to_async(&tokio).iter(|| { let tasks = tasks_with_predicate(&paths, &iceberg_schema); - read_all(tasks, num_cpus_limit(), true) + // `id < ROWS_PER_FILE / 2` matches exactly half the rows per file. + read_all( + tasks, + num_cpus_limit(), + true, + num_files * (ROWS_PER_FILE / 2), + ) }); }); group.finish(); From 74bafb1693e0d5a7f89fe16b505555dd3a1c880f Mon Sep 17 00:00:00 2001 From: "alex.bianchi" Date: Fri, 20 Nov 2026 15:33:31 -0500 Subject: [PATCH 4/6] bench(reader): measure string IN predicates across file layouts --- crates/iceberg/benches/arrow_reader.rs | 232 +++++++++++++++++-------- 1 file changed, 156 insertions(+), 76 deletions(-) diff --git a/crates/iceberg/benches/arrow_reader.rs b/crates/iceberg/benches/arrow_reader.rs index 64af84a884..0f4de4b4e3 100644 --- a/crates/iceberg/benches/arrow_reader.rs +++ b/crates/iceberg/benches/arrow_reader.rs @@ -54,14 +54,16 @@ //! //! Run with: `cargo bench -p iceberg --bench arrow_reader` -use std::sync::Arc; +use std::sync::{Arc, Once}; +use arrow_array::cast::AsArray; +use arrow_array::types::Int64Type; use arrow_array::{ArrayRef, Int64Array, RecordBatch, StringArray}; -use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; +use criterion::{BatchSize, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; use futures::{TryStreamExt, stream}; use iceberg::Runtime; -use iceberg::arrow::ArrowReaderBuilder; -use iceberg::expr::{Bind, Reference}; +use iceberg::arrow::{ArrowReaderBuilder, ArrowReaderOptions}; +use iceberg::expr::{Bind, BoundPredicate, Reference}; use iceberg::io::FileIO; use iceberg::scan::{FileScanTask, FileScanTaskStream}; use iceberg::spec::{ @@ -125,15 +127,20 @@ fn write_files( let batch = RecordBatch::try_new(arrow_schema.clone(), vec![ids, names]).expect("build record batch"); + write_batches(dir, std::iter::repeat_n(batch, num_files)) +} + +fn write_batches(dir: &TempDir, batches: impl Iterator) -> Vec { let props = WriterProperties::builder() .set_compression(Compression::SNAPPY) .build(); - (0..num_files) - .map(|i| { + batches + .enumerate() + .map(|(i, batch)| { let path = format!("{}/data_{i}.parquet", dir.path().to_str().unwrap()); let file = std::fs::File::create(&path).expect("create parquet file"); - let mut writer = ArrowWriter::try_new(file, arrow_schema.clone(), Some(props.clone())) + let mut writer = ArrowWriter::try_new(file, batch.schema(), Some(props.clone())) .expect("create arrow writer"); writer.write(&batch).expect("write batch"); writer.close().expect("close writer"); @@ -193,38 +200,34 @@ fn write_multi_row_group_file( /// Build a whole-file `FileScanTask` for each path (no predicate, no deletes — /// the projection-only path, which isolates per-task setup overhead). fn tasks_for(paths: &[String], schema: &SchemaRef) -> Vec { - paths.iter().map(|p| base_task(p, schema)).collect() -} - -/// A whole-file projection-only task for a single path. -fn base_task(path: &str, schema: &SchemaRef) -> FileScanTask { - let size = std::fs::metadata(path).expect("stat parquet file").len(); - FileScanTask { - file_size_in_bytes: size, - start: 0, - length: size, - record_count: Some(ROWS_PER_FILE as u64), - data_file_path: path.to_string(), - data_file_format: DataFileFormat::Parquet, - schema: schema.clone(), - project_field_ids: vec![1, 2], - predicate: None, - deletes: vec![], - partition: None, - partition_spec: None, - name_mapping: None, - case_sensitive: false, - } + whole_file_tasks(paths, schema, ROWS_PER_FILE, None, None) } -/// Like [`tasks_for`] but attaches a name mapping (migrated-table path). -fn tasks_with_name_mapping(paths: &[String], schema: &SchemaRef) -> Vec { - let mapping = name_mapping(); +fn whole_file_tasks( + paths: &[String], + schema: &SchemaRef, + rows_per_file: usize, + predicate: Option<&BoundPredicate>, + mapping: Option<&Arc>, +) -> Vec { paths .iter() - .map(|p| FileScanTask { - name_mapping: Some(mapping.clone()), - ..base_task(p, schema) + .map(|path| { + let size = std::fs::metadata(path).expect("stat parquet file").len(); + FileScanTask::builder() + .with_file_size_in_bytes(size) + .with_start(0) + .with_length(size) + .with_record_count(Some(rows_per_file as u64)) + .with_data_file_path(path.clone()) + .with_data_file_format(DataFileFormat::Parquet) + .with_schema(schema.clone()) + .with_project_field_ids(vec![1, 2]) + .with_predicate(predicate.cloned()) + .with_name_mapping(mapping.cloned()) + .with_case_sensitive(false) + .build() + .expect("build whole-file task") }) .collect() } @@ -239,22 +242,17 @@ fn same_file_split_tasks(path: &str, schema: &SchemaRef, num_splits: u64) -> Vec .map(|i| { let start = i * chunk; let length = chunk.min(size.saturating_sub(start)); - FileScanTask { - file_size_in_bytes: size, - start, - length, - record_count: None, - data_file_path: path.to_string(), - data_file_format: DataFileFormat::Parquet, - schema: schema.clone(), - project_field_ids: vec![1, 2], - predicate: None, - deletes: vec![], - partition: None, - partition_spec: None, - name_mapping: None, - case_sensitive: false, - } + FileScanTask::builder() + .with_file_size_in_bytes(size) + .with_start(start) + .with_length(length) + .with_data_file_path(path.to_string()) + .with_data_file_format(DataFileFormat::Parquet) + .with_schema(schema.clone()) + .with_project_field_ids(vec![1, 2]) + .with_case_sensitive(false) + .build() + .expect("build split task") }) .collect() } @@ -266,33 +264,30 @@ fn tasks_with_predicate(paths: &[String], schema: &SchemaRef) -> Vec ArrowReaderOptions { + ArrowReaderOptions::builder() + .with_data_file_concurrency_limit(concurrency) + .with_row_group_filtering_enabled(filtering) + .with_row_selection_enabled(filtering) + .build() } -/// Read every task to completion, draining all record batches. `filtering` -/// turns on row-group filtering and row selection (only meaningful when the -/// tasks carry a predicate). +/// Read every task to completion, draining all record batches. /// /// Asserts that exactly `expected_rows` rows come back: a configuration that /// reads too few rows — or duplicates rows, like the sub-row-group-split bug /// in #2614 — would otherwise time faster and read as a performance win. async fn read_all( tasks: Vec, - concurrency: usize, - filtering: bool, + options: ArrowReaderOptions, expected_rows: usize, -) { +) -> Vec { let file_io = FileIO::new_with_fs(); let reader = ArrowReaderBuilder::new(file_io, Runtime::current()) - .with_data_file_concurrency_limit(concurrency) - .with_row_group_filtering_enabled(filtering) - .with_row_selection_enabled(filtering) + .with_options(options) .build(); let task_stream: FileScanTaskStream = Box::pin(stream::iter(tasks.into_iter().map(Ok))); @@ -311,7 +306,7 @@ async fn read_all( "benchmark read returned the wrong number of rows; timings would be meaningless" ); - criterion::black_box(batches); + batches } /// Read every task to completion and return the total number of bytes fetched @@ -325,7 +320,11 @@ async fn read_all( async fn read_all_bytes(tasks: Vec, concurrency: usize, expected_rows: usize) -> u64 { let file_io = FileIO::new_with_fs(); let reader = ArrowReaderBuilder::new(file_io, Runtime::current()) - .with_data_file_concurrency_limit(concurrency) + .with_options( + ArrowReaderOptions::builder() + .with_data_file_concurrency_limit(concurrency) + .build(), + ) .build(); let task_stream: FileScanTaskStream = Box::pin(stream::iter(tasks.into_iter().map(Ok))); @@ -366,7 +365,11 @@ fn bench_many_small_files(c: &mut Criterion) { |b, _| { b.to_async(&tokio).iter(|| { let tasks = tasks_for(&paths, &iceberg_schema); - read_all(tasks, num_cpus_limit(), false, num_files * ROWS_PER_FILE) + read_all( + tasks, + reader_options(num_cpus_limit(), false), + num_files * ROWS_PER_FILE, + ) }); }, ); @@ -393,7 +396,11 @@ fn bench_concurrency(c: &mut Criterion) { |b, &concurrency| { b.to_async(&tokio).iter(|| { let tasks = tasks_for(&paths, &iceberg_schema); - read_all(tasks, concurrency, false, num_files * ROWS_PER_FILE) + read_all( + tasks, + reader_options(concurrency, false), + num_files * ROWS_PER_FILE, + ) }); }, ); @@ -417,8 +424,18 @@ fn bench_migrated_table(c: &mut Criterion) { group.throughput(Throughput::Elements(num_files as u64)); group.bench_function("name_mapping", |b| { b.to_async(&tokio).iter(|| { - let tasks = tasks_with_name_mapping(&paths, &iceberg_schema); - read_all(tasks, num_cpus_limit(), false, num_files * ROWS_PER_FILE) + let tasks = whole_file_tasks( + &paths, + &iceberg_schema, + ROWS_PER_FILE, + None, + Some(&name_mapping()), + ); + read_all( + tasks, + reader_options(num_cpus_limit(), false), + num_files * ROWS_PER_FILE, + ) }); }); group.finish(); @@ -464,7 +481,7 @@ fn bench_same_file_splits(c: &mut Criterion) { let tasks = same_file_split_tasks(&path, &iceberg_schema, num_splits); // `total_rows` doubles as the #2614 duplication guard: N splits // of one file must reassemble it exactly once. - read_all(tasks, num_cpus_limit(), false, total_rows) + read_all(tasks, reader_options(num_cpus_limit(), false), total_rows) }); }, ); @@ -491,12 +508,75 @@ fn bench_with_predicate(c: &mut Criterion) { // `id < ROWS_PER_FILE / 2` matches exactly half the rows per file. read_all( tasks, - num_cpus_limit(), - true, + reader_options(num_cpus_limit(), true), num_files * (ROWS_PER_FILE / 2), ) }); }); + const ROWS: usize = 262_144; + let options = ArrowReaderOptions::builder() + .with_batch_size(8192) + .with_data_file_concurrency_limit(1) + .with_row_group_filtering_enabled(true) + .with_row_selection_enabled(true) + .build(); + group.throughput(Throughput::Elements(ROWS as u64)); + for keys in [400, 5000] { + let ids = Int64Array::from_iter_values(0..ROWS as i64); + let names = StringArray::from_iter_values((0..ROWS).map(|id| { + if id % 2 == 0 { + format!("user-key-{}", (id / 2) % keys) + } else { + "user-key--1".to_owned() + } + })); + let batch = + RecordBatch::try_new(arrow_schema.clone(), vec![Arc::new(ids), Arc::new(names)]) + .expect("build string IN batch"); + let predicate = + Reference::new("name").is_in((0..keys).map(|i| Datum::string(format!("user-key-{i}")))); + let bind = || { + predicate + .bind(iceberg_schema.clone(), true) + .expect("bind string IN") + }; + + for (files, layout) in [(1, "string_in_1_file"), (64, "string_in_64_files")] { + let dir = TempDir::new().unwrap(); + let rows_per_file = ROWS / files; + let paths = write_batches( + &dir, + (0..files).map(|i| batch.slice(i * rows_per_file, rows_per_file)), + ); + let scan = |bound| { + let tasks = + whole_file_tasks(&paths, &iceberg_schema, rows_per_file, Some(&bound), None); + read_all(tasks, options.clone(), ROWS / 2) + }; + let verify = Once::new(); + group.bench_function(BenchmarkId::new(layout, keys), |b| { + verify.call_once(|| { + let batches = tokio.block_on(scan(bind())); + let mut ids: Vec<_> = batches + .iter() + .flat_map(|batch| { + batch + .column(0) + .as_primitive::() + .values() + .iter() + .copied() + }) + .collect(); + ids.sort_unstable(); + assert!(ids.into_iter().eq((0..ROWS as i64).step_by(2))); + }); + // A fresh bound predicate includes any first-use index construction in the scan time. + b.to_async(&tokio) + .iter_batched(bind, scan, BatchSize::PerIteration); + }); + } + } group.finish(); } From 2d55fc4846e7e3b4d724f2721d99b9fce4adf74b Mon Sep 17 00:00:00 2001 From: "alex.bianchi" Date: Fri, 20 Nov 2026 17:00:57 -0500 Subject: [PATCH 5/6] bench(reader): compare Int64 IN probes across file layouts Review: https://github.com/apache/iceberg-rust/pull/3739#discussion_r4648107743 --- crates/iceberg/benches/arrow_reader.rs | 155 +++++++++++++------------ 1 file changed, 83 insertions(+), 72 deletions(-) diff --git a/crates/iceberg/benches/arrow_reader.rs b/crates/iceberg/benches/arrow_reader.rs index 0f4de4b4e3..19775ed56e 100644 --- a/crates/iceberg/benches/arrow_reader.rs +++ b/crates/iceberg/benches/arrow_reader.rs @@ -58,11 +58,11 @@ use std::sync::{Arc, Once}; use arrow_array::cast::AsArray; use arrow_array::types::Int64Type; -use arrow_array::{ArrayRef, Int64Array, RecordBatch, StringArray}; +use arrow_array::{ArrayRef, Int32Array, Int64Array, RecordBatch, StringArray}; use criterion::{BatchSize, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; use futures::{TryStreamExt, stream}; use iceberg::Runtime; -use iceberg::arrow::{ArrowReaderBuilder, ArrowReaderOptions}; +use iceberg::arrow::{ArrowReaderBuilder, ArrowReaderOptions, schema_to_arrow_schema}; use iceberg::expr::{Bind, BoundPredicate, Reference}; use iceberg::io::FileIO; use iceberg::scan::{FileScanTask, FileScanTaskStream}; @@ -70,7 +70,7 @@ use iceberg::spec::{ DataFileFormat, Datum, MappedField, NameMapping, NestedField, PrimitiveType, Schema, SchemaRef, Type, }; -use parquet::arrow::{ArrowWriter, PARQUET_FIELD_ID_META_KEY}; +use parquet::arrow::ArrowWriter; use parquet::basic::Compression; use parquet::file::properties::WriterProperties; use tempfile::TempDir; @@ -85,32 +85,21 @@ const ROWS_PER_FILE: usize = 100; /// `(id: long, name: string)` with embedded Parquet field IDs, so the reader /// takes the "file has field IDs" fast path. fn schemas() -> (SchemaRef, arrow_schema::SchemaRef) { + schemas_with_key("name", PrimitiveType::String) +} + +fn schemas_with_key(key: &str, ty: PrimitiveType) -> (SchemaRef, arrow_schema::SchemaRef) { let iceberg_schema = Arc::new( Schema::builder() .with_schema_id(1) .with_fields(vec![ NestedField::required(1, "id", Type::Primitive(PrimitiveType::Long)).into(), - NestedField::optional(2, "name", Type::Primitive(PrimitiveType::String)).into(), + NestedField::optional(2, key, Type::Primitive(ty)).into(), ]) .build() .unwrap(), ); - - let arrow_schema = Arc::new(arrow_schema::Schema::new(vec![ - arrow_schema::Field::new("id", arrow_schema::DataType::Int64, false).with_metadata( - std::collections::HashMap::from([( - PARQUET_FIELD_ID_META_KEY.to_string(), - "1".to_string(), - )]), - ), - arrow_schema::Field::new("name", arrow_schema::DataType::Utf8, true).with_metadata( - std::collections::HashMap::from([( - PARQUET_FIELD_ID_META_KEY.to_string(), - "2".to_string(), - )]), - ), - ])); - + let arrow_schema = Arc::new(schema_to_arrow_schema(&iceberg_schema).unwrap()); (iceberg_schema, arrow_schema) } @@ -521,60 +510,82 @@ fn bench_with_predicate(c: &mut Criterion) { .with_row_selection_enabled(true) .build(); group.throughput(Throughput::Elements(ROWS as u64)); - for keys in [400, 5000] { - let ids = Int64Array::from_iter_values(0..ROWS as i64); - let names = StringArray::from_iter_values((0..ROWS).map(|id| { - if id % 2 == 0 { - format!("user-key-{}", (id / 2) % keys) - } else { - "user-key--1".to_owned() - } - })); - let batch = - RecordBatch::try_new(arrow_schema.clone(), vec![Arc::new(ids), Arc::new(names)]) - .expect("build string IN batch"); - let predicate = - Reference::new("name").is_in((0..keys).map(|i| Datum::string(format!("user-key-{i}")))); - let bind = || { - predicate - .bind(iceberg_schema.clone(), true) - .expect("bind string IN") - }; - - for (files, layout) in [(1, "string_in_1_file"), (64, "string_in_64_files")] { - let dir = TempDir::new().unwrap(); - let rows_per_file = ROWS / files; - let paths = write_batches( - &dir, - (0..files).map(|i| batch.slice(i * rows_per_file, rows_per_file)), - ); - let scan = |bound| { - let tasks = - whole_file_tasks(&paths, &iceberg_schema, rows_per_file, Some(&bound), None); - read_all(tasks, options.clone(), ROWS / 2) + for (kind, field, ty) in [ + ("string", "name", PrimitiveType::String), + ("int32", "key", PrimitiveType::Int), + ("int64", "key", PrimitiveType::Long), + ] { + let (iceberg_schema, arrow_schema) = schemas_with_key(field, ty.clone()); + for keys in [400, 5000] { + let ids = Int64Array::from_iter_values(0..ROWS as i64); + let values = (0..ROWS as i32).map(|id| if id % 2 == 0 { (id / 2) % keys } else { -1 }); + let (column, literals): (ArrayRef, Vec) = match ty { + PrimitiveType::Int => ( + Arc::new(Int32Array::from_iter_values(values)), + (0..keys).map(Datum::int).collect(), + ), + PrimitiveType::Long => ( + Arc::new(Int64Array::from_iter_values(values.map(i64::from))), + (0..keys).map(|v| Datum::long(i64::from(v))).collect(), + ), + PrimitiveType::String => ( + Arc::new(StringArray::from_iter_values( + values.map(|v| format!("user-key-{v}")), + )), + (0..keys) + .map(|v| Datum::string(format!("user-key-{v}"))) + .collect(), + ), + _ => unreachable!(), }; - let verify = Once::new(); - group.bench_function(BenchmarkId::new(layout, keys), |b| { - verify.call_once(|| { - let batches = tokio.block_on(scan(bind())); - let mut ids: Vec<_> = batches - .iter() - .flat_map(|batch| { - batch - .column(0) - .as_primitive::() - .values() - .iter() - .copied() - }) - .collect(); - ids.sort_unstable(); - assert!(ids.into_iter().eq((0..ROWS as i64).step_by(2))); + let batch = + RecordBatch::try_new(arrow_schema.clone(), vec![Arc::new(ids), column]).unwrap(); + let predicate = Reference::new(field).is_in(literals); + let bind = || { + predicate + .bind(iceberg_schema.clone(), true) + .expect("bind IN predicate") + }; + for (files, layout) in [(1, "1_file"), (64, "64_files")] { + let dir = TempDir::new().unwrap(); + let rows_per_file = ROWS / files; + let paths = write_batches( + &dir, + (0..files).map(|i| batch.slice(i * rows_per_file, rows_per_file)), + ); + let scan = |bound| { + let tasks = whole_file_tasks( + &paths, + &iceberg_schema, + rows_per_file, + Some(&bound), + None, + ); + read_all(tasks, options.clone(), ROWS / 2) + }; + let verify = Once::new(); + group.bench_function(BenchmarkId::new(format!("{kind}_in_{layout}"), keys), |b| { + verify.call_once(|| { + let batches = tokio.block_on(scan(bind())); + let mut ids: Vec<_> = batches + .iter() + .flat_map(|batch| { + batch + .column(0) + .as_primitive::() + .values() + .iter() + .copied() + }) + .collect(); + ids.sort_unstable(); + assert!(ids.into_iter().eq((0..ROWS as i64).step_by(2))); + }); + // A fresh bound predicate includes any first-use index construction in the scan time. + b.to_async(&tokio) + .iter_batched(bind, scan, BatchSize::PerIteration); }); - // A fresh bound predicate includes any first-use index construction in the scan time. - b.to_async(&tokio) - .iter_batched(bind, scan, BatchSize::PerIteration); - }); + } } } group.finish(); From 89beb2622b2e01b55b45409b7256c9b52c0d6c1a Mon Sep 17 00:00:00 2001 From: "alex.bianchi" Date: Fri, 20 Nov 2026 17:00:58 -0500 Subject: [PATCH 6/6] bench(reader): measure short string and Int64 IN lists Review: https://github.com/apache/iceberg-rust/pull/3739#discussion_r4648117006 --- crates/iceberg/benches/arrow_reader.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/iceberg/benches/arrow_reader.rs b/crates/iceberg/benches/arrow_reader.rs index 19775ed56e..f3b42bec48 100644 --- a/crates/iceberg/benches/arrow_reader.rs +++ b/crates/iceberg/benches/arrow_reader.rs @@ -516,7 +516,7 @@ fn bench_with_predicate(c: &mut Criterion) { ("int64", "key", PrimitiveType::Long), ] { let (iceberg_schema, arrow_schema) = schemas_with_key(field, ty.clone()); - for keys in [400, 5000] { + for keys in [2, 4, 8, 16, 32, 400, 5000] { let ids = Int64Array::from_iter_values(0..ROWS as i64); let values = (0..ROWS as i32).map(|id| if id % 2 == 0 { (id / 2) % keys } else { -1 }); let (column, literals): (ArrayRef, Vec) = match ty {