Skip to content

Add heap memory estimation for statistics - #19599

Closed
mkleen wants to merge 2 commits into
apache:mainfrom
mkleen:stats-limit
Closed

Add heap memory estimation for statistics#19599
mkleen wants to merge 2 commits into
apache:mainfrom
mkleen:stats-limit

Conversation

@mkleen

@mkleenmkleen commented Jan 2, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Relates to #19052 (comment)

Rationale for this change

This adds heap memory estimation to statistics.

What changes are included in this PR?

NA.

Are these changes tested?

Yes

Are there any user-facing changes?

Adds a new HeapSize trait and implementations for all relevant types used in memory estimation. The trait is taken from arrow-rs, where it is currently private, and is intended as a temporary solution until arrow-rs is updated.

@github-actionsgithub-actionsBot added common Related to common crate execution Related to the execution crate labels Jan 2, 2026
Comment threaddatafusion/common/src/stats.rs Outdated
pub fn heap_size(&self) -> usize {
// column_statistics + num_rows + total_byte_size
self.column_statistics.capacity() * size_of::<ColumnStatistics>()
+ size_of::<Precision<usize>>() * 2

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here Precision<usize> is an enum and does not have a heap allocated fields, so it is allocated in the stack.

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 think these things are usually Arc'ed - so everything should be moved to the heap, right?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

So size_of::<Precision<usize>>() * 2 should be removed?

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 think so, if we want to follow the trait in arrow, which I think according to #19599 (comment) was the conclusion of the next step? Do you plan on push a commit to do so?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

yes i am on it. pr coming up soon.

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.

We need to be able to get the heap size of arrays to implement it for Statistics? What's the chain of fields that takes us there?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

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.

Bummer. Isn't there ways to get the size of an array in memory? E.g. Array::get_array_memory_size?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

This may actually work. Thanks, I will try that.

Comment threaddatafusion/common/src/stats.rs Outdated
This adds a heap_size method returning the amount of memory a statistics
struct allocates on the heap.
@github-actionsgithub-actionsBot removed the execution Related to the execution crate label Jan 10, 2026
@mkleen
mkleenforce-pushed the stats-limit branch 3 times, most recently from 17a4cd1 to 153d1adCompareJanuary 10, 2026 06:12
fn heap_size(&self) -> usize {
self.num_rows.heap_size()
+ self.total_byte_size.heap_size()
+ self

@mkleenmkleenJan 10, 2026

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

num_rows and total_byte_size will result in 0, so this is included for consistency, but could also be omitted.

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 think as defined in arrow-rs we're supposed to not include these and the caller should include them using size_off::<Statistics>(). IMO it would be nice if HeapSize had a memory_size() method that did self.heap_size() + size_of::<Self> or something like that.

@mkleenmkleen changed the title Add heap_size to statisticsAdd support for HeapSize to statisticsJan 10, 2026
@mkleenmkleen changed the title Add support for HeapSize to statisticsAdd heap memory estimation to statisticsJan 10, 2026
@mkleenmkleen changed the title Add heap memory estimation to statisticsAdd heap memory estimation for statisticsJan 10, 2026
@github-actionsgithub-actionsBot added the execution Related to the execution crate label Jan 11, 2026
@adriangb

Copy link
Copy Markdown
Contributor

@mkleen correct me if I'm missing something but the arrow-rs trait is currently under the parquet crate right? I opened apache/arrow-rs#9138 to move it into it's own crate so that datafusion-common doesn't end up depending on parquet and also added derive macros and a bunch of other things intended to improve QOL of having this as an important ecosystem trait.

That shouldn't block this PR though. I plan on reviewing it again this week (although it's a busy customer site visit week for me).

I do think we'll want to get input from @alamb on this since it's turned into a bit bigger of a piece than I think any of us originally thought it would be (mostly in a good way though 😄).

@mkleen

mkleen commented Jan 12, 2026

Copy link
Copy Markdown
ContributorAuthor

@mkleen correct me if I'm missing something but the arrow-rs trait is currently under the parquet crate right? I opened apache/arrow-rs#9138 to move it into it's own crate so that datafusion-common doesn't end up depending on parquet and also added derive macros and a bunch of other things intended to improve QOL of having this as an important ecosystem trait.

Yes, this is correct and this all makes sense to me.

That shouldn't block this PR though. I plan on reviewing it again this week (although it's a busy customer site visit week for me).

No worries, thanks a lot!

I do think we'll want to get input from @alamb on this since it's turned into a bit bigger of a piece than I think any of us originally thought it would be (mostly in a good way though 😄).

Perfect!

@alamb

Copy link
Copy Markdown
Contributor

Here is another suggestion:

fn heap_size(&self) -> usize;
}

impl HeapSize for Statistics {

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.

Well, it does makes sense for the primitives of the arrow to have the implementation here, I think we can move the HeapSize for Statistics to the cache folder? Wdyt @mkleen

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Yes, this makes sense. This now became part of #20047

@adriangbadriangb 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.

@mkleen I think let's proceed with this as is and we can work on the arrow side in the future

@mkleen

Copy link
Copy Markdown
ContributorAuthor

@adriangb Thanks for the feedback. What are your thoughts on @alchemist51’s suggestion to move this into the cache folder, since it will only be used there?

@adriangb

Copy link
Copy Markdown
Contributor

@adriangb Thanks for the feedback. What are your thoughts on @alchemist51’s suggestion to move this into the cache folder, since it will only be used there?

If that keeps it out of the public API at least for now I think that’s an excellent suggestion.

@mkleen

Copy link
Copy Markdown
ContributorAuthor

I will close this one, because it became part of #20047

@mkleenmkleen closed this Feb 10, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commonRelated to common crateexecutionRelated to the execution crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@mkleen@adriangb@alamb@martin-g@alchemist51