Skip to content

Spark: Use actual file sizes instead of schema-based estimates for table statistics#15693

Open
majian1998 wants to merge 2 commits intoapache:mainfrom
majian1998:fix/estimate-size-15684
Open

Spark: Use actual file sizes instead of schema-based estimates for table statistics#15693
majian1998 wants to merge 2 commits intoapache:mainfrom
majian1998:fix/estimate-size-15684

Conversation

@majian1998
Copy link
Copy Markdown
Contributor

What

SparkScan.estimateStatistics() currently estimates table size by multiplying StructType.defaultSize() (hardcoded per-type constants, e.g. STRING=54 bytes) by the total row count. This can be wildly inaccurate compared to actual data, causing Spark to pick suboptimal join strategies (e.g. missing BroadcastHashJoin or broadcasting a table that's too large).

Changes

Replace the type-based estimation with real file size data that Iceberg already tracks:
Partitioned tables (no filters): read total-files-size from snapshot summary
All other paths: sum ScanTaskGroup.sizeBytes() which reflects actual fileSizeInBytes from manifests
Applies to both SparkScan and SparkChangelogScan
This makes Iceberg-based table statistics consistent with how Spark's native Parquet source reports size (using actual file sizes on disk), so the same data produces the same join strategy regardless of the source.

Related issue: #15684

@github-actions github-actions bot added the spark label Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant