You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "mkleen (via GitHub)" <gi...@apache.org> on 2026/01/02 01:11:43 UTC

[PR] Add heap_size to statistics [datafusion]

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

   ## Which issue does this PR close?
   
   Relates to https://github.com/apache/datafusion/issues/19052#issuecomment-3603796097
   
   ## Rationale for this change
   
   This adds a heap_size method returning the amount of memory a statistics struct allocates on the heap.
   
   ## What changes are included in this PR?
   
   NA.
   
   ## Are these changes tested?
   
   Yes
   
   ## Are there any user-facing changes?
   
   One new method on `Statistics`.
   
   <!--
   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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   We also have https://github.com/apache/arrow-rs/blob/b93fa52e47a83dff130709a6e80a4be5017dbf09/parquet/src/file/metadata/memory.rs#L35 which goes into the same direction.



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/heap_size.rs:
##########
@@ -0,0 +1,454 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::stats::Precision;
+use crate::{ColumnStatistics, ScalarValue, Statistics};
+use arrow::array::{
+    Array, FixedSizeListArray, LargeListArray, ListArray, MapArray, StructArray,
+};
+use arrow::datatypes::{
+    DataType, Field, Fields, IntervalDayTime, IntervalMonthDayNano, IntervalUnit,
+    TimeUnit, UnionFields, UnionMode, i256,
+};
+use half::f16;
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::sync::Arc;
+/// Since HeapSize is private in arrow-rs this code is taken over
+/// from <https://github.com/apache/arrow-rs/blob/main/parquet/src/file/metadata/memory.rs>
+/// and extended to provide heap size estimation for Statistics. This needs to be cleaned
+/// up and replaced once the HeapSize is publicly available from arrow-rs.
+///
+///
+/// Trait for calculating the size of various containers
+pub trait HeapSize {
+    /// Return the size of any bytes allocated on the heap by this object,
+    /// including heap memory in those structures
+    ///
+    /// Note that the size of the type itself is not included in the result --
+    /// instead, that size is added by the caller (e.g. container).
+    fn heap_size(&self) -> usize;
+}
+
+impl HeapSize for Statistics {
+    fn heap_size(&self) -> usize {
+        self.num_rows.heap_size()
+            + self.total_byte_size.heap_size()
+            + self

Review Comment:
   `num_rows` and `total_byte_size` will result in 0, so this is only included for consistency.
   
   



-- 
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] Add heap_size to statistics [datafusion]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #19599:
URL: https://github.com/apache/datafusion/pull/19599#discussion_r2658389431


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   deepsize seems abandoned:
   * https://github.com/Aeledfyr/deepsize/issues/38
   * https://github.com/Aeledfyr/deepsize/issues/43
   
   I haven't used any of the following but they look better maintained:
   * https://github.com/nhtyy/deepsize2
   * https://github.com/bircni/get-size2



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   What would be the next step then? File a new issue? 



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

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



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/heap_size.rs:
##########
@@ -0,0 +1,454 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::stats::Precision;
+use crate::{ColumnStatistics, ScalarValue, Statistics};
+use arrow::array::{
+    Array, FixedSizeListArray, LargeListArray, ListArray, MapArray, StructArray,
+};
+use arrow::datatypes::{
+    DataType, Field, Fields, IntervalDayTime, IntervalMonthDayNano, IntervalUnit,
+    TimeUnit, UnionFields, UnionMode, i256,
+};
+use half::f16;
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::sync::Arc;
+/// Since HeapSize is private in arrow-rs this code is taken over
+/// from https://github.com/apache/arrow-rs/blob/main/parquet/src/file/metadata/memory.rs
+/// and extended to provide heap size estimation for Statistics. This needs to be cleaned
+/// up and replaced once the HeapSize is publicly available from arrow-rs.
+///
+///
+/// Trait for calculating the size of various containers
+pub trait HeapSize {
+    /// Return the size of any bytes allocated on the heap by this object,
+    /// including heap memory in those structures
+    ///
+    /// Note that the size of the type itself is not included in the result --
+    /// instead, that size is added by the caller (e.g. container).
+    fn heap_size(&self) -> usize;
+}
+
+impl HeapSize for Statistics {
+    fn heap_size(&self) -> usize {
+        self.num_rows.heap_size()
+            + self.total_byte_size.heap_size()

Review Comment:
   `num_rows` and `total_byte_size` will result in `0`, so this is only there for being consistent.



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

Review Comment:
   Ah crap I thought it was public already in `arrow-rs`! But thanks for handling that. I guess we have to wait for an `arrow-rs` release and update before we can merge this, but maybe you can unblock by copying the trait over and writing all of the code here then we can rebase and point to the arrow-rs version?



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

Review Comment:
   I think these things are usually `Arc`'ed - so everything should be moved to the heap, right?



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

Review Comment:
   The `HeapSize` Trait in arrow-rs is private. I made it public and implemented all arrow-rs-related types in arrow-rs. This makes sense, right? This would require an arrow-rs release to have the code available here.



-- 
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] Add heap memory estimation for statistics [datafusion]

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

   Here is another suggestion: 
   - https://github.com/apache/arrow-rs/pull/9138#issuecomment-3739741636


-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

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



-- 
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] Add heap memory estimation for statistics [datafusion]

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

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


-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   I think so. It looks like it has no required deps. Can you check if it is by chance already in our dep tree?



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   @martin-g any thoughts on using deepsize?



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

Review Comment:
   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?



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

Review Comment:
   And these are part of ColumnStatistics https://github.com/apache/datafusion/blob/main/datafusion/common/src/scalar/mod.rs#L376



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   I think if we want to accurately calculate the sizes we need to propagate that method all the way down. I'm not sure at what point we should just be using https://crates.io/crates/deepsize or similar.



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   Interesting. I guess the advantage of some of the 3rd party crates is the derive macros. We could always `impl<T: HeapSize> DeepSizeOf for T {` and visa versa.



-- 
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] Add heap memory estimation for statistics [datafusion]

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


##########
datafusion/common/src/heap_size.rs:
##########
@@ -0,0 +1,454 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::stats::Precision;
+use crate::{ColumnStatistics, ScalarValue, Statistics};
+use arrow::array::{
+    Array, FixedSizeListArray, LargeListArray, ListArray, MapArray, StructArray,
+};
+use arrow::datatypes::{
+    DataType, Field, Fields, IntervalDayTime, IntervalMonthDayNano, IntervalUnit,
+    TimeUnit, UnionFields, UnionMode, i256,
+};
+use half::f16;
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::sync::Arc;
+/// Since HeapSize is private in arrow-rs this code is taken over
+/// from <https://github.com/apache/arrow-rs/blob/main/parquet/src/file/metadata/memory.rs>
+/// and extended to provide heap size estimation for Statistics. This needs to be cleaned
+/// up and replaced once the HeapSize is publicly available from arrow-rs.
+///
+///
+/// Trait for calculating the size of various containers
+pub trait HeapSize {
+    /// Return the size of any bytes allocated on the heap by this object,
+    /// including heap memory in those structures
+    ///
+    /// Note that the size of the type itself is not included in the result --
+    /// instead, that size is added by the caller (e.g. container).
+    fn heap_size(&self) -> usize;
+}
+
+impl HeapSize for Statistics {
+    fn heap_size(&self) -> usize {
+        self.num_rows.heap_size()
+            + self.total_byte_size.heap_size()
+            + self

Review Comment:
   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.



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

Review Comment:
   The only workaround would be to use an estimate on the heap size for the statistics rather than being accurate and use this for https://github.com/apache/datafusion/issues/19052 to have a limit.



-- 
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] Add heap memory estimation for statistics [datafusion]

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

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


-- 
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] Add heap memory estimation for statistics [datafusion]

Posted by "mkleen (via GitHub)" <gi...@apache.org>.
mkleen closed pull request #19599: Add heap memory estimation for statistics
URL: https://github.com/apache/datafusion/pull/19599


-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/heap_size.rs:
##########
@@ -0,0 +1,454 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::stats::Precision;
+use crate::{ColumnStatistics, ScalarValue, Statistics};
+use arrow::array::{
+    Array, FixedSizeListArray, LargeListArray, ListArray, MapArray, StructArray,
+};
+use arrow::datatypes::{
+    DataType, Field, Fields, IntervalDayTime, IntervalMonthDayNano, IntervalUnit,
+    TimeUnit, UnionFields, UnionMode, i256,
+};
+use half::f16;
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::sync::Arc;
+/// Since HeapSize is private in arrow-rs this code is taken over
+/// from <https://github.com/apache/arrow-rs/blob/main/parquet/src/file/metadata/memory.rs>
+/// and extended to provide heap size estimation for Statistics. This needs to be cleaned
+/// up and replaced once the HeapSize is publicly available from arrow-rs.
+///
+///
+/// Trait for calculating the size of various containers
+pub trait HeapSize {
+    /// Return the size of any bytes allocated on the heap by this object,
+    /// including heap memory in those structures
+    ///
+    /// Note that the size of the type itself is not included in the result --
+    /// instead, that size is added by the caller (e.g. container).
+    fn heap_size(&self) -> usize;
+}
+
+impl HeapSize for Statistics {
+    fn heap_size(&self) -> usize {
+        self.num_rows.heap_size()
+            + self.total_byte_size.heap_size()
+            + self

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



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

Review Comment:
   This may actually work. Thanks, I will try that.



-- 
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] Add heap_size to statistics [datafusion]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #19599:
URL: https://github.com/apache/datafusion/pull/19599#discussion_r2657777540


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   IMO you need to iterate over the column_statistics and add their heap allocations
   https://github.com/mkleen/datafusion/blob/41b875d19d9c3671e141cec11814afd91a06a0f1/datafusion/common/src/stats.rs#L732-L736 - there are `Precision<ScalarValue>` fields and the ScalarValue enum has variants which use `String`, `Vec` and `Box`



##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

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



-- 
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] Add heap_size to statistics [datafusion]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #19599:
URL: https://github.com/apache/datafusion/pull/19599#discussion_r2657789266


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   I think so!



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   Thanks for the feedback. So would it make sense to add `heap_size()` to `column_statistics` ?



-- 
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] Add heap memory estimation for statistics [datafusion]

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


##########
datafusion/common/src/heap_size.rs:
##########
@@ -0,0 +1,454 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::stats::Precision;
+use crate::{ColumnStatistics, ScalarValue, Statistics};
+use arrow::array::{
+    Array, FixedSizeListArray, LargeListArray, ListArray, MapArray, StructArray,
+};
+use arrow::datatypes::{
+    DataType, Field, Fields, IntervalDayTime, IntervalMonthDayNano, IntervalUnit,
+    TimeUnit, UnionFields, UnionMode, i256,
+};
+use half::f16;
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::sync::Arc;
+/// Since HeapSize is private in arrow-rs this code is taken over
+/// from <https://github.com/apache/arrow-rs/blob/main/parquet/src/file/metadata/memory.rs>
+/// and extended to provide heap size estimation for Statistics. This needs to be cleaned
+/// up and replaced once the HeapSize is publicly available from arrow-rs.
+///
+///
+/// Trait for calculating the size of various containers
+pub trait HeapSize {
+    /// Return the size of any bytes allocated on the heap by this object,
+    /// including heap memory in those structures
+    ///
+    /// Note that the size of the type itself is not included in the result --
+    /// instead, that size is added by the caller (e.g. container).
+    fn heap_size(&self) -> usize;
+}
+
+impl HeapSize for Statistics {

Review Comment:
   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 



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

Review Comment:
   https://github.com/apache/datafusion/blob/main/datafusion/common/src/scalar/mod.rs#L376 as part of `ScalarValue`.



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

Review Comment:
   I tried that, but certain fields of datatypes are private, which are necessary for scalar values. There is no way around it other than to first add this to arrow-rs.
    
   e.g.:
   
   ```rust
   impl HeapSize for FixedSizeListArray {
       fn heap_size(&self) -> usize {
           self.data_type().heap_size() + // <- private
               self.values().heap_size() +
               self.nulls().unwrap().heap_size() + // <- private
               self.value_length().heap_size() +
               self.len().heap_size() // <- private
       }
   }
   ```



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

Review Comment:
   Then the next step is to implement all datafusion related types like `ScalarValue` here.



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/heap_size.rs:
##########
@@ -0,0 +1,454 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::stats::Precision;
+use crate::{ColumnStatistics, ScalarValue, Statistics};
+use arrow::array::{
+    Array, FixedSizeListArray, LargeListArray, ListArray, MapArray, StructArray,
+};
+use arrow::datatypes::{
+    DataType, Field, Fields, IntervalDayTime, IntervalMonthDayNano, IntervalUnit,
+    TimeUnit, UnionFields, UnionMode, i256,
+};
+use half::f16;
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::sync::Arc;
+/// Since HeapSize is private in arrow-rs this code is taken over
+/// from https://github.com/apache/arrow-rs/blob/main/parquet/src/file/metadata/memory.rs
+/// and extended to provide heap size estimation for Statistics. This needs to be cleaned
+/// up and replaced once the HeapSize is publicly available from arrow-rs.
+///
+///
+/// Trait for calculating the size of various containers
+pub trait HeapSize {
+    /// Return the size of any bytes allocated on the heap by this object,
+    /// including heap memory in those structures
+    ///
+    /// Note that the size of the type itself is not included in the result --
+    /// instead, that size is added by the caller (e.g. container).
+    fn heap_size(&self) -> usize;
+}
+
+impl HeapSize for Statistics {
+    fn heap_size(&self) -> usize {
+        self.num_rows.heap_size()
+            + self.total_byte_size.heap_size()

Review Comment:
   `num_rows` and `total_byte_size` will result in `0`, so this is only included for consistency.



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   Good point. Should we add `heap_size()` to `ScalarValue` as well?



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   Sadly none looks all that popular (going off of stars) or battle tested. Hard choice.



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   I opened #19615. I think for this PR I would recommend adopting arrow-rs's `HeapSize` with manual implementations. If we add a macro there later we can update to use that.



-- 
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] Add heap_size to statistics [datafusion]

Posted by "martin-g (via GitHub)" <gi...@apache.org>.
martin-g commented on code in PR #19599:
URL: https://github.com/apache/datafusion/pull/19599#discussion_r2658396945


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   deepsize2 is a fork of deepsize + applying all opened PRs and updating the (optional) dependencies



-- 
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] Add heap memory estimation for statistics [datafusion]

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

   I will close this one, because it became part of https://github.com/apache/datafusion/pull/20047


-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/heap_size.rs:
##########
@@ -0,0 +1,454 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::stats::Precision;
+use crate::{ColumnStatistics, ScalarValue, Statistics};
+use arrow::array::{
+    Array, FixedSizeListArray, LargeListArray, ListArray, MapArray, StructArray,
+};
+use arrow::datatypes::{
+    DataType, Field, Fields, IntervalDayTime, IntervalMonthDayNano, IntervalUnit,
+    TimeUnit, UnionFields, UnionMode, i256,
+};
+use half::f16;
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::sync::Arc;
+/// Since HeapSize is private in arrow-rs this code is taken over
+/// from https://github.com/apache/arrow-rs/blob/main/parquet/src/file/metadata/memory.rs
+/// and extended to provide heap size estimation for Statistics. This needs to be cleaned
+/// up and replaced once the HeapSize is publicly available from arrow-rs.
+///
+///
+/// Trait for calculating the size of various containers
+pub trait HeapSize {
+    /// Return the size of any bytes allocated on the heap by this object,
+    /// including heap memory in those structures
+    ///
+    /// Note that the size of the type itself is not included in the result --
+    /// instead, that size is added by the caller (e.g. container).
+    fn heap_size(&self) -> usize;
+}
+
+impl HeapSize for Statistics {
+    fn heap_size(&self) -> usize {
+        self.num_rows.heap_size()
+            + self.total_byte_size.heap_size()

Review Comment:
   `num_rows` and `total_byte_size` will result in `0`, so this is only included for consistency.



-- 
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] Add heap memory estimation for statistics [datafusion]

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

   @mkleen correct me if I'm missing something but the arrow-rs trait is currently under the `parquet` crate right? I opened https://github.com/apache/arrow-rs/pull/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 😄).


-- 
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] Add heap memory estimation for statistics [datafusion]

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

   
   > @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](https://github.com/apache/arrow-rs/pull/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. Thank you!
   
   > 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! 
   
   


-- 
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] Add heap memory estimation for statistics [datafusion]

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


##########
datafusion/common/src/heap_size.rs:
##########
@@ -0,0 +1,454 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::stats::Precision;
+use crate::{ColumnStatistics, ScalarValue, Statistics};
+use arrow::array::{
+    Array, FixedSizeListArray, LargeListArray, ListArray, MapArray, StructArray,
+};
+use arrow::datatypes::{
+    DataType, Field, Fields, IntervalDayTime, IntervalMonthDayNano, IntervalUnit,
+    TimeUnit, UnionFields, UnionMode, i256,
+};
+use half::f16;
+use std::collections::HashMap;
+use std::fmt::Debug;
+use std::sync::Arc;
+/// Since HeapSize is private in arrow-rs this code is taken over
+/// from <https://github.com/apache/arrow-rs/blob/main/parquet/src/file/metadata/memory.rs>
+/// and extended to provide heap size estimation for Statistics. This needs to be cleaned
+/// up and replaced once the HeapSize is publicly available from arrow-rs.
+///
+///
+/// Trait for calculating the size of various containers
+pub trait HeapSize {
+    /// Return the size of any bytes allocated on the heap by this object,
+    /// including heap memory in those structures
+    ///
+    /// Note that the size of the type itself is not included in the result --
+    /// instead, that size is added by the caller (e.g. container).
+    fn heap_size(&self) -> usize;
+}
+
+impl HeapSize for Statistics {

Review Comment:
   Yes, this makes sense. This now became part of https://github.com/apache/datafusion/pull/20047 



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

Review Comment:
   I think so, if we want to follow the trait in arrow, which I think according to https://github.com/apache/datafusion/pull/19599#discussion_r2658477799 was the conclusion of the next step? Do you plan on push a commit to do so?



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   So let's go with https://crates.io/crates/deepsize then?



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   I am just reading through the docs.
   
   https://crates.io/crates/get-size2
   
   ```
   Note that the helpers are currently only available for regular structs, that is they do neither support tuple structs nor enums.
   ```
   
   We need enum support in this case, so I don't think this will work out of the box.



##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   I am just reading through the docs.
   
   https://crates.io/crates/get-size2
   
   ```
   Note that the helpers are currently only available for regular structs, that is they do neither support tuple structs nor enums.
   ```
   
   We need enum support in this case, so I don't think this will work out of the box.



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    pub fn heap_size(&self) -> usize {
+        // column_statistics + num_rows + total_byte_size
+        self.column_statistics.capacity() * size_of::<ColumnStatistics>()

Review Comment:
   Ah makes sense then as the choice if we’re going to use an external crate. It seems like the most ergonomic. This does seems like something that should be more of a community wide decision than isolated in this PR though



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

Review Comment:
   yes i am on it. pr coming up soon.



-- 
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] Add heap_size to statistics [datafusion]

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


##########
datafusion/common/src/stats.rs:
##########
@@ -321,6 +321,13 @@ impl Statistics {
         }
     }
 
+    /// Returns the memory size in bytes.
+    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

Review Comment:
   This sounds like a good idea. Let's do this! Thanks for the input!



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