You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "petern48 (via GitHub)" <gi...@apache.org> on 2025/11/10 00:20:44 UTC

[PR] feat(parquet): Implement scan_efficiency_ratio metric for parquet reading stats [datafusion]

petern48 opened a new pull request, #18577:
URL: https://github.com/apache/datafusion/pull/18577

   ## Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   - Part of #18195
   
   ## Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   ## What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   ## Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   Added test
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   Yes, new metric to view.
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat(parquet): Implement `scan_efficiency_ratio` metric for parquet reading [datafusion]

Posted by "petern48 (via GitHub)" <gi...@apache.org>.
petern48 commented on code in PR #18577:
URL: https://github.com/apache/datafusion/pull/18577#discussion_r2515663472


##########
datafusion/physical-plan/src/metrics/builder.rs:
##########
@@ -283,7 +275,17 @@ impl<'a> MetricBuilder<'a> {
         name: impl Into<Cow<'static, str>>,
         partition: usize,
     ) -> RatioMetrics {
-        let ratio_metrics = RatioMetrics::new();
+        self.ratio_metrics_with_strategy(name, partition, RatioMergeStrategy::default())
+    }
+
+    /// Consumes self and creates a new [`RatioMetrics`] with a specific merge strategy
+    pub fn ratio_metrics_with_strategy(
+        self,
+        name: impl Into<Cow<'static, str>>,
+        partition: usize,
+        merge_strategy: RatioMergeStrategy,
+    ) -> RatioMetrics {
+        let ratio_metrics = RatioMetrics::new().with_merge_strategy(merge_strategy);
         self.with_partition(partition).build(MetricValue::Ratio {
             name: name.into(),
             ratio_metrics: ratio_metrics.clone(),

Review Comment:
   Originally, I tried `builder.ratio_metrics().with_merge_strategy()` in metrics.rs,
   
   but found the merge strategy wasn't working since the `ratio_metrics.clone()` call here was registering the ratio_metrics before I added the merge strategy to it. Hence the need for this new `ratio_metrics_with_strategy()`



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat(parquet): Implement `scan_efficiency_ratio` metric for parquet reading [datafusion]

Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 commented on code in PR #18577:
URL: https://github.com/apache/datafusion/pull/18577#discussion_r2525647959


##########
datafusion/datasource-parquet/src/reader.rs:
##########
@@ -129,6 +130,17 @@ impl AsyncFileReader for ParquetFileReader {
     }
 }
 
+impl Drop for ParquetFileReader {
+    fn drop(&mut self) {
+        self.file_metrics
+            .scan_efficiency_ratio
+            .add_part(self.file_metrics.bytes_scanned.value());
+        self.file_metrics
+            .scan_efficiency_ratio
+            .add_total(self.partitioned_file.object_meta.size as usize);

Review Comment:
   Perhaps in https://github.com/apache/parquet-testing/tree/107b36603e051aee26bd93e04b871034f6c756c0, we can find a existing parquet test file, with multiple row groups, and apply a filter that can prune out some row groups.
   
   However, we can proceed as is. Since it's just a metric, the impact is minimal even if it misbehaves.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat(parquet): Implement `scan_efficiency_ratio` metric for parquet reading [datafusion]

Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 merged PR #18577:
URL: https://github.com/apache/datafusion/pull/18577


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat(parquet): Implement `scan_efficiency_ratio` metric for parquet reading [datafusion]

Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 commented on code in PR #18577:
URL: https://github.com/apache/datafusion/pull/18577#discussion_r2509906685


##########
datafusion/datasource-parquet/src/reader.rs:
##########
@@ -129,6 +130,17 @@ impl AsyncFileReader for ParquetFileReader {
     }
 }
 
+impl Drop for ParquetFileReader {
+    fn drop(&mut self) {
+        self.file_metrics
+            .scan_efficiency_ratio
+            .add_part(self.file_metrics.bytes_scanned.value());
+        self.file_metrics
+            .scan_efficiency_ratio
+            .add_total(self.partitioned_file.object_meta.size as usize);

Review Comment:
   I vaguely remember each parquet reader is possible to read only a sub-range of a file, if that's the case here we might double count the total size.
   
   I verified with the following queries -- The `l_orderkey` is globally ordered, so if we set predicate `l_orderkey=42` then only the first row group will be scanned. if we set partition to 2, then there are two parquet reader instance, each reading half of the file range, and the resulting `scan_efficiency_ratio` should be same even if they're executed under different partitioning, now they're different.
   
   Data is generated with https://github.com/clflushopt/tpchgen-rs/tree/main/tpchgen-cli, in parquet, sf1
   ```
   -- If target_partitions=1, the ratio is 0.86%
   -- If target_partitions=2, the ratio is 0.43%
   > set datafusion.execution.target_partitions = 2;
   0 row(s) fetched.
   Elapsed 0.000 seconds.
   
   > explain analyze select * from '/Users/yongting/data/tpch_sf1/lineitem.parquet' where l_orderkey = 42;
   +-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 ---+
   | plan_type         | plan                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
    |
   +-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 ---+
   | Plan with Metrics | CoalesceBatchesExec: target_batch_size=8192, metrics=[output_rows=0, elapsed_compute=376ns, output_bytes=0.0 B]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
    |
   |                   |   FilterExec: l_orderkey@0 = 42, metrics=[output_rows=0, elapsed_compute=25.835µs, output_bytes=0.0 B, selectivity=0% (0/20096)]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
     |
   |                   |     DataSourceExec: file_groups={2 groups: [[Users/yongting/data/tpch_sf1/lineitem.parquet:0..115375407], [Users/yongting/data/tpch_sf1/lineitem.parquet:115375407..230750813]]}, projection=[l_orderkey, l_partkey, l_suppkey, l_linenumber, l_quantity, l_extendedprice, l_discount, l_tax, l_returnflag, l_linestatus, l_shipdate, l_commitdate, l_receiptdate, l_shipinstruct, l_shipmode, l_comment], file_type=parquet, predicate=l_orderkey@0 = 42, pruning_predicate=l_orderkey_null_count@2 != row_count@3 AND l_orderkey_min@0 <= 42 AND 42 <= l_orderkey_max@1, required_guarantees=[l_orderkey in (42)], metrics=[output_rows=20096, elapsed_compute=2ns, output_bytes=6.6 MB, files_ranges_pruned_statistics=2 total → 2 matched, row_groups_pruned_statistics=49 total → 1 matched, row_groups_pruned_bloom_filter=1 total → 1 matched, page_index_rows_pruned=123010 total → 20096 matched, bytes_scanned=1994659, metadata_load_time=123.46µs, scan_efficiency_ratio=0.43% (1994659/
 461501626)] |
   |                   |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
    |
   +-------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 ---+
   1 row(s) fetched.
   Elapsed 0.010 seconds.
   ```



##########
datafusion/datasource-parquet/src/reader.rs:
##########
@@ -97,6 +97,7 @@ impl DefaultParquetFileReaderFactory {
 pub struct ParquetFileReader {
     pub file_metrics: ParquetFileMetrics,
     pub inner: ParquetObjectReader,
+    pub partitioned_file: PartitionedFile,

Review Comment:
   I think so, but here I think a breaking change is acceptable



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat(parquet): Implement `scan_efficiency_ratio` metric for parquet reading [datafusion]

Posted by "petern48 (via GitHub)" <gi...@apache.org>.
petern48 commented on code in PR #18577:
URL: https://github.com/apache/datafusion/pull/18577#discussion_r2508442522


##########
datafusion/datasource-parquet/src/reader.rs:
##########
@@ -97,6 +97,7 @@ impl DefaultParquetFileReaderFactory {
 pub struct ParquetFileReader {
     pub file_metrics: ParquetFileMetrics,
     pub inner: ParquetObjectReader,
+    pub partitioned_file: PartitionedFile,

Review Comment:
   I wasn't sure, would this technically be considered a breaking change? This struct is only constructed manually throughout the crate (no constructor).
   
   `ParquetFileMetrics` is in this `ParquetFileReader` struct, but `ParquetFileReader` didn't have a way to access the total file size, since the `file_size` field in `ParquetObjectReader` was private (and inside of `arrow-rs`. This is the change that made the most sense to me, since `CachedParquetFileReader` already has this as a field.
   
   https://github.com/apache/datafusion/blob/f162fd325565e14be8e4cace17d8a3a8b2764cc8/datafusion/datasource-parquet/src/reader.rs#L225-L229
   
   Alternatively, maybe I could add a `file_size()` getter to [ParquetObjectReader](https://github.com/apache/arrow-rs/blob/43c7637c634a96998cc135490ea0a8b73972feb8/parquet/src/arrow/async_reader/store.rs#L55) in `arrow-rs`?



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat(parquet): Implement `scan_efficiency_ratio` metric for parquet reading [datafusion]

Posted by "petern48 (via GitHub)" <gi...@apache.org>.
petern48 commented on code in PR #18577:
URL: https://github.com/apache/datafusion/pull/18577#discussion_r2515746103


##########
datafusion/datasource-parquet/src/reader.rs:
##########
@@ -129,6 +130,17 @@ impl AsyncFileReader for ParquetFileReader {
     }
 }
 
+impl Drop for ParquetFileReader {
+    fn drop(&mut self) {
+        self.file_metrics
+            .scan_efficiency_ratio
+            .add_part(self.file_metrics.bytes_scanned.value());
+        self.file_metrics
+            .scan_efficiency_ratio
+            .add_total(self.partitioned_file.object_meta.size as usize);

Review Comment:
   Pushed a fix for this. I also manually verified that the results are the same.
   
   ```
   tpchgen-cli -s 1 --format=parquet --output-dir ...
   
   set datafusion.execution.target_partitions = 2;
   explain analyze select * from '<path>/lineitem.parquet' where l_orderkey = 42;
   -- scan_efficiency_ratio=0.83% (1919614/231669547)
   
   set datafusion.execution.target_partitions = 1;
   explain analyze select * from '<path>/lineitem.parquet' where l_orderkey = 42;
   -- scan_efficiency_ratio=0.83% (1919614/231669547)
   ```
   
   I was hoping to then write a test that would fail on the previous implementation but pass, now. I found it difficult to write a test that does so with the existing files in `datafusion/core/tests/data` (which are all fairly small). Then I tried to writing a test that mimics this exactly, but found the file was quite large (200+ MB), compared to the rest of the tests in the directory.
   
   <img width="962" height="115" alt="image" src="https://github.com/user-attachments/assets/8258382d-a93b-4a42-90f0-df2861e94d1d" />
   
   <details>
       <summary>The test I wrote (not pushed)</summary>
   
   ```rust
   #[tokio::test]
   #[cfg_attr(tarpaulin, ignore)]
   async fn parquet_scan_efficiency_ratio() {
       let table_name = "tpch_lineitem_sf1";
       let parquet_path = "tests/data/tpch_lineitem_sf1.parquet";
   
       for num_partitions in [1, 2, 5] {
           let config = SessionConfig::new().with_target_partitions(num_partitions);
           let ctx = SessionContext::new_with_config(config);
           ctx.register_parquet(table_name, parquet_path, ParquetReadOptions::default())
               .await
               .expect("register parquet table for explain analyze test");
   
           let sql =
               "EXPLAIN ANALYZE select * from tpch_lineitem_sf1 where l_orderkey = 42;";
           let actual = execute_to_batches(&ctx, sql).await;
           let formatted = arrow::util::pretty::pretty_format_batches(&actual)
               .unwrap()
               .to_string();
   
           assert_contains!(
               &formatted,
               "scan_efficiency_ratio=0.83% (1919614/231669547)"
           );
       }
   ```
   </details>
   
   How would you like to proceed? Do you still want a new test, or is the PR good as it is? The test that existed before this changed passes before and after.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat(parquet): Implement `scan_efficiency_ratio` metric for parquet reading [datafusion]

Posted by "petern48 (via GitHub)" <gi...@apache.org>.
petern48 commented on PR #18577:
URL: https://github.com/apache/datafusion/pull/18577#issuecomment-3509062860

   cc @2010YOUY01


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] feat(parquet): Implement `scan_efficiency_ratio` metric for parquet reading [datafusion]

Posted by "petern48 (via GitHub)" <gi...@apache.org>.
petern48 commented on code in PR #18577:
URL: https://github.com/apache/datafusion/pull/18577#discussion_r2508442522


##########
datafusion/datasource-parquet/src/reader.rs:
##########
@@ -97,6 +97,7 @@ impl DefaultParquetFileReaderFactory {
 pub struct ParquetFileReader {
     pub file_metrics: ParquetFileMetrics,
     pub inner: ParquetObjectReader,
+    pub partitioned_file: PartitionedFile,

Review Comment:
   I wasn't sure, would this be considered a breaking change?
   
   `ParquetFileMetrics` is in this `ParquetFileReader` struct, but `ParquetFileReader` didn't have a way to access the total file size, since the `file_size` field in `ParquetObjectReader` was private (and inside of `arrow-rs`. This is the change that made the most sense to me, since `CachedParquetFileReader` already has this as a field.
   
   https://github.com/apache/datafusion/blob/f162fd325565e14be8e4cace17d8a3a8b2764cc8/datafusion/datasource-parquet/src/reader.rs#L225-L229
   
   Alternatively, maybe I could add a `file_size()` getter to [ParquetObjectReader](https://github.com/apache/arrow-rs/blob/43c7637c634a96998cc135490ea0a8b73972feb8/parquet/src/arrow/async_reader/store.rs#L55) in `arrow-rs`?



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org