You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "kumarUjjawal (via GitHub)" <gi...@apache.org> on 2026/03/25 15:47:55 UTC

[PR] fix(datasource): keep stats absent when collect_stats is false [datafusion]

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

   ## 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.
   -->
   
   - Closes #2976
   - Part of #8227
   
   ## Rationale for this change
   
     `get_statistics_with_limit` was copying first-file row/byte stats even when`collect_stats=false`. This could return `Exact(0)` instead of `Absent` (unknown stats).
   
   <!--
    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?
   
    Summary stats are only initialized and merged when `collect_stats=true`. Also fixes first-file column `byte_size` initialization in the `collect_stats=true` path, and adds regression tests for both paths and limit behavior.
   
   <!--
   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?
   
   Yes
   
   <!--
   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)?
   -->
   
   ## Are there any user-facing changes?
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   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] fix(datasource): keep stats absent when collect_stats is false [datafusion]

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


##########
datafusion/datasource/src/statistics.rs:
##########
@@ -320,16 +320,19 @@ pub async fn get_statistics_with_limit(
         file.statistics = Some(Arc::clone(&file_stats));
         result_files.push(file);
 
-        // First file, we set them directly from the file statistics.
-        num_rows = file_stats.num_rows;
-        total_byte_size = file_stats.total_byte_size;
-        for (index, file_column) in
-            file_stats.column_statistics.clone().into_iter().enumerate()
-        {
-            col_stats_set[index].null_count = file_column.null_count;
-            col_stats_set[index].max_value = file_column.max_value;
-            col_stats_set[index].min_value = file_column.min_value;
-            col_stats_set[index].sum_value = file_column.sum_value.cast_to_sum_type();
+        if collect_stats {

Review Comment:
   Thanks for fixing the returned summary here. I think this introduces a separate issue in the `limit` path though.
   
   `get_statistics_with_limit` still relies on `num_rows` to decide whether it can stop early. With `collect_stats=false`, `num_rows` now stays `Absent`, so `conservative_num_rows` falls back to `usize::MIN` and we keep scanning the stream instead of stopping early.
   
   Before this change, a known first-file row count could still let us stop after the first file. With this patch, even something like `limit=0` can continue reading later files.
   
   Could we preserve a separate row-count accumulator, or at least keep the first-file row count around for the limit check, while still returning a bare `Statistics` value when `collect_stats` is false? I think we should also add a regression test for `collect_stats=false` with a limit so this behavior is locked in.



##########
datafusion/datasource/src/statistics.rs:
##########
@@ -320,16 +320,19 @@ pub async fn get_statistics_with_limit(
         file.statistics = Some(Arc::clone(&file_stats));
         result_files.push(file);
 
-        // First file, we set them directly from the file statistics.
-        num_rows = file_stats.num_rows;
-        total_byte_size = file_stats.total_byte_size;
-        for (index, file_column) in
-            file_stats.column_statistics.clone().into_iter().enumerate()
-        {
-            col_stats_set[index].null_count = file_column.null_count;
-            col_stats_set[index].max_value = file_column.max_value;
-            col_stats_set[index].min_value = file_column.min_value;
-            col_stats_set[index].sum_value = file_column.sum_value.cast_to_sum_type();
+        if collect_stats {

Review Comment:
   Small refactor suggestion. It may be worth extracting the "seed stats from first file" logic into a helper that can optionally populate the summary accumulator.
   
   This block and the later merge loop now both need to stay in sync on fields like `byte_size` and the `sum_value` casting. Pulling that into one place would make future stats-field additions a bit less likely to drift.



-- 
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] fix(datasource): keep stats absent when collect_stats is false [datafusion]

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


-- 
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] fix(datasource): keep stats absent when collect_stats is false [datafusion]

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

   Thanks @kosiew I have made the changes as requested, please take a look when you get the time.


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