This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-12180-dcf287027030d6c98d8cc6ff0dc68489c2ff8b35 in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
commit 94bb4838e55dc56ad1f556da46e60165ac0ee53f Author: 龙骑士k <[email protected]> AuthorDate: Mon Nov 16 02:19:12 2026 +0000 fix(parquet): count only shared predicate cache hits in cache-read metrics (#12180) # Which issue does this PR close? - Closes #8307 # Rationale for this change Callers use `records_read_from_cache` to see how many rows the predicate cache served, including when the cache is disabled. The metric used to count every hit of a reader's temporary local buffer. `fetch_batch` always stores the decoded batch there, so later reads of the rest of that batch were reported as cache reads even when the shared cache was disabled. # What changes are included in this PR? `CachedArrayReader::read_records` now increments `records_read_from_cache` only for batches loaded from the shared (global) predicate cache. Later local hits of those batches still count. | Row source | Metric updated | | --- | --- | | Shared cache, including later local hits of those batches | `records_read_from_cache` | | `fetch_batch` decode into the local buffer | `records_read_from_inner` only | # Are these changes tested? `test_local_cache_hits_do_not_count_as_cache_reads` checks that a disabled shared cache reports zero cache reads for later local hits. `test_shared_cache_hits_count_across_partial_reads` checks that shared-cache rows still count when later served from `local_cache`. # Are there any significant user-facing changes? No public API changes; only the cache-read metric values are corrected. ## AI Disclosure Human-led contribution. Grok Bot, working for dragonknightk, assisted with this change. All submitted content was reviewed by a human before submission. Grok Bot assisted with drafting the metric-accounting change, the regression tests, and this description. (๑•̀ㅂ•́)و✧ --- .../src/arrow/array_reader/cached_array_reader.rs | 98 ++++++++++++++++++++-- parquet/src/arrow/arrow_reader/metrics.rs | 4 + 2 files changed, 97 insertions(+), 5 deletions(-) diff --git a/parquet/src/arrow/array_reader/cached_array_reader.rs b/parquet/src/arrow/array_reader/cached_array_reader.rs index 5cd722ba48..3afe191143 100644 --- a/parquet/src/arrow/array_reader/cached_array_reader.rs +++ b/parquet/src/arrow/array_reader/cached_array_reader.rs @@ -12,7 +12,7 @@ use arrow_array::{ArrayRef, BooleanArray, new_empty_array}; use arrow_buffer::BooleanBufferBuilder; use arrow_schema::DataType as ArrowType; use std::any::Any; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::sync::{Arc, RwLock}; /// Role of the cached array reader @@ -72,6 +72,9 @@ pub struct CachedArrayReader { /// Local cache to store batches between read_records and consume_batch calls /// This ensures data is available even if the shared cache evicts items local_cache: HashMap<BatchID, ArrayRef>, + /// Local-cache batch IDs loaded from the shared predicate cache. + /// Cache-read metrics count only those hits. + local_from_shared_cache: HashSet<BatchID>, /// Statistics to report on the Cache behavior metrics: ArrowReaderMetrics, /// Exclusive upper bound of the batch ids already removed from the shared @@ -100,6 +103,7 @@ impl CachedArrayReader { selections: BooleanBufferBuilder::new(0), role, local_cache: HashMap::new(), + local_from_shared_cache: HashSet::new(), metrics, cleaned_up_to: 0, } @@ -200,8 +204,11 @@ impl ArrayReader for CachedArrayReader { let batch_id = self.get_batch_id_from_position(self.outer_position); // Check local cache first - let cached = if let Some(array) = self.local_cache.get(&batch_id) { - Some(Arc::clone(array)) + let (cached, from_shared_cache) = if let Some(array) = self.local_cache.get(&batch_id) { + ( + Some(Arc::clone(array)), + self.local_from_shared_cache.contains(&batch_id), + ) } else { // If not in local cache, i.e., we are consumer, check shared cache let cache_content = self @@ -212,8 +219,9 @@ impl ArrayReader for CachedArrayReader { if let Some(array) = cache_content.as_ref() { // Store in local cache for later use in consume_batch self.local_cache.insert(batch_id, Arc::clone(array)); + self.local_from_shared_cache.insert(batch_id); } - cache_content + (cache_content, true) }; match cached { @@ -224,7 +232,11 @@ impl ArrayReader for CachedArrayReader { let v = array_len + batch_id.val * self.batch_size - self.outer_position; let select_cnt = std::cmp::min(num_records - read, v); read += select_cnt; - self.metrics.increment_cache_reads(select_cnt); + // Shared-cache hits only; later local hits of those + // batches still count. + if from_shared_cache { + self.metrics.increment_cache_reads(select_cnt); + } self.outer_position += select_cnt; self.selections.append_n(select_cnt, true); } else { @@ -320,6 +332,8 @@ impl ArrayReader for CachedArrayReader { let current_batch_id = self.get_batch_id_from_position(self.outer_position); self.local_cache .retain(|batch_id, _| batch_id.val >= current_batch_id.val); + self.local_from_shared_cache + .retain(|batch_id| batch_id.val >= current_batch_id.val); // For consumers, cleanup batches that have been completely consumed // This reduces the memory usage of the shared cache @@ -843,4 +857,78 @@ mod tests { let int32_array = array.as_any().downcast_ref::<Int32Array>().unwrap(); assert_eq!(int32_array.values(), &[1, 2, 3, 4]); } + + /// Regression test for <https://github.com/apache/arrow-rs/issues/8307>. + /// With the shared cache disabled, later hits of a decoded batch in + /// `local_cache` must not increment `records_read_from_cache`. + #[test] + fn test_local_cache_hits_do_not_count_as_cache_reads() { + let metrics = ArrowReaderMetrics::enabled(); + let mock_reader = MockArrayReader::new(vec![1, 2, 3, 4, 5, 6]); + let cache = Arc::new(RwLock::new(RowGroupCache::new(3, 0))); // Batch size 3, cache disabled + let mut reader = CachedArrayReader::new( + Box::new(mock_reader), + cache, + 0, + CacheRole::Consumer, + metrics.clone(), + ); + + assert_eq!(reader.read_records(1).unwrap(), 1); + assert_eq!(reader.consume_batch().unwrap().len(), 1); + assert_eq!(metrics.records_read_from_inner(), Some(3)); + assert_eq!(metrics.records_read_from_cache(), Some(0)); + + assert_eq!(reader.read_records(2).unwrap(), 2); + let array = reader.consume_batch().unwrap(); + assert_eq!(array.len(), 2); + let int32_array = array.as_any().downcast_ref::<Int32Array>().unwrap(); + assert_eq!(int32_array.values(), &[2, 3]); + assert_eq!(metrics.records_read_from_inner(), Some(3)); + assert_eq!( + metrics.records_read_from_cache(), + Some(0), + "local-only hits must not increment cache-read metrics" + ); + } + + /// Regression test for <https://github.com/apache/arrow-rs/issues/8307>. + /// Rows loaded from the shared cache must increment + /// `records_read_from_cache`, including later local hits of the same batch. + #[test] + fn test_shared_cache_hits_count_across_partial_reads() { + let producer_metrics = ArrowReaderMetrics::enabled(); + let cache = Arc::new(RwLock::new(RowGroupCache::new(3, usize::MAX))); // Batch size 3 + let mut producer = CachedArrayReader::new( + Box::new(MockArrayReader::new(vec![1, 2, 3, 4, 5, 6])), + cache.clone(), + 0, + CacheRole::Producer, + producer_metrics.clone(), + ); + assert_eq!(producer.read_records(3).unwrap(), 3); + producer.consume_batch().unwrap(); + assert_eq!(producer_metrics.records_read_from_inner(), Some(3)); + assert_eq!(producer_metrics.records_read_from_cache(), Some(0)); + + let consumer_metrics = ArrowReaderMetrics::enabled(); + let mut consumer = CachedArrayReader::new( + Box::new(MockArrayReader::new(vec![1, 2, 3, 4, 5, 6])), + cache, + 0, + CacheRole::Consumer, + consumer_metrics.clone(), + ); + assert_eq!(consumer.read_records(1).unwrap(), 1); + assert_eq!(consumer.consume_batch().unwrap().len(), 1); + assert_eq!(consumer_metrics.records_read_from_cache(), Some(1)); + assert_eq!(consumer_metrics.records_read_from_inner(), Some(0)); + + assert_eq!(consumer.read_records(2).unwrap(), 2); + let array = consumer.consume_batch().unwrap(); + let int32_array = array.as_any().downcast_ref::<Int32Array>().unwrap(); + assert_eq!(int32_array.values(), &[2, 3]); + assert_eq!(consumer_metrics.records_read_from_cache(), Some(3)); + assert_eq!(consumer_metrics.records_read_from_inner(), Some(0)); + } } diff --git a/parquet/src/arrow/arrow_reader/metrics.rs b/parquet/src/arrow/arrow_reader/metrics.rs index 1cd55cf2f6..fa538c2944 100644 --- a/parquet/src/arrow/arrow_reader/metrics.rs +++ b/parquet/src/arrow/arrow_reader/metrics.rs @@ -65,6 +65,10 @@ impl ArrowReaderMetrics { /// It returns the number of records read across all columns, so if you read /// 2 columns each with 100 records from the cache, this will return 200. /// + /// Only records served from the shared (global) predicate cache are + /// counted. A reader's temporary local buffer of batches it has already + /// decoded is left out of this metric. + /// /// Returns None if metrics are disabled. pub fn records_read_from_cache(&self) -> Option<usize> { match self {
