Skip to content

feat: report exact row counts for eager scans with no predicates or deletes - #137

Open
Richard-Durkee wants to merge 1 commit into
apache:mainfrom
Richard-Durkee:exact-eager-row-counts
Open

Richard-Durkee wants to merge 1 commit into
apache:mainfrom
Richard-Durkee:exact-eager-row-counts

Conversation

@Richard-Durkee

Copy link
Copy Markdown

EagerFileScanTaskProvider::partition_statistics sums the record_count of its tasks and reports the result as Inexact whenever it's above zero. DataFusion only answers COUNT(*) from statistics when the row count is exact, so an unfiltered count still opens every data file. Follow-up to #125.

A task returns exactly its record_count when it reads the whole file and has no row predicate and no delete files. When that holds for every task in the requested partition, we now report the sum as Exact. A task with a predicate or deletes keeps the sum Inexact, and a missing record count still gives Absent. Lazy scans are unchanged, because they don't know their files at planning time.

The taxi test table has seven files of 25,000 rows and no deletes. With iceberg.execution.eager_file_planning on:

SELECT count(*) FROM taxi;

Before this PR:

ProjectionExec: expr=[count(Int64(1))@0 as count(*)]
  AggregateExec: mode=Final, gby=[], aggr=[count(Int64(1))]
    CoalescePartitionsExec
      AggregateExec: mode=Partial, gby=[], aggr=[count(Int64(1))]
        DataSourceExec: format=iceberg, projection=[], provider=eager, planned_files=7

With this PR:

ProjectionExec: expr=[175000 as count(*)]
  PlaceholderRowExec

The exact row count also turns the byte estimate into Exact, because IcebergDataSource::partition_statistics derives a missing byte size from the rows. The eager_file_statistics_apply_fetch_per_partition snapshot shows it. I left that alone, but I can keep the bytes Inexact if you'd prefer.

The new test in statistics.rs counts reads with ReadCountingStorageFactory. It checks that the unfiltered count reads no data file, and that a count with a pushed-down predicate still does.

@alexanderbianchi alexanderbianchi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, would like a full harness test for the inexact path, thanks for following up.

// Taxi has seven daily files of 25,000 rows and no deletes.
let (plan, _) = harness.query("SELECT count(*) FROM taxi").await?;
insta::assert_snapshot!(plan, @"
ProjectionExec: expr=[175000 as count(*)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a test for the inexact case that shows the full plan as well?


/// Whether `task` reads a whole file with no row predicate and no deletes, so it
/// returns exactly its record count.
fn returns_every_record(task: &FileScanTask) -> bool {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked if we could potentially use delete metadata do cover more cases, but it'd go beyond what other engines do. Makes sense to stick with this.

  • Spark + Iceberg Java: For an eligible COUNT(*), Spark evaluates the aggregate from Iceberg file metrics and returns a local result. If any planned task has an attached delete, it declines the pushdown for the whole aggregate. That includes both v2 position deletes and v3 deletion vectors. Spark scan builder · Java delete index
  • Trino: It keeps the aggregate in the plan. For a whole-file split with no residual filter or deletes, COUNT(*) needs no data columns, so Trino can synthesize pages from the manifest’s row count. If a split has v2 position deletes or a v3 DV, it takes the regular reader path and applies the deletes. That decision is per split, so eligible clean files can still take the shortcut in the same query. Trino page source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants