Kontinuation commented on code in PR #515:
URL: https://github.com/apache/sedona-db/pull/515#discussion_r2694761640


##########
rust/sedona-spatial-join/src/utils/arrow_utils.rs:
##########
@@ -0,0 +1,166 @@
+// 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 arrow::array::{Array, ArrayData, RecordBatch};
+use arrow_array::ArrayRef;
+use arrow_schema::{ArrowError, DataType};
+use datafusion_common::Result;
+
+/// Estimate the in-memory size of a given RecordBatch. This function 
estimates the
+/// size as if the underlying buffers were copied to somewhere else and not 
shared.
+pub(crate) fn get_record_batch_memory_size(batch: &RecordBatch) -> 
Result<usize> {
+    let mut total_size = 0;
+
+    for array in batch.columns() {
+        let array_data = array.to_data();
+        total_size += count_array_data_memory_size(&array_data)?;
+    }
+
+    Ok(total_size)
+}
+
+/// Estimate the in-memory size of a given Arrow array. This function 
estimates the
+/// size as if the underlying buffers were copied to somewhere else and not 
shared.
+pub(crate) fn get_array_memory_size(array: &ArrayRef) -> Result<usize> {
+    let array_data = array.to_data();
+    let size = count_array_data_memory_size(&array_data)?;
+    Ok(size)
+}
+
+/// The maximum number of bytes that can be stored inline in a byte view.
+///
+/// See [`ByteView`] and [`GenericByteViewArray`] for more information on the
+/// layout of the views.
+///
+/// [`GenericByteViewArray`]: 
https://docs.rs/arrow/latest/arrow/array/struct.GenericByteViewArray.html
+pub const MAX_INLINE_VIEW_LEN: u32 = 12;
+
+/// Count the memory usage of `array_data` and its children recursively.
+fn count_array_data_memory_size(array_data: &ArrayData) -> 
core::result::Result<usize, ArrowError> {
+    Ok(get_binary_view_value_size(array_data)? + 
array_data.get_slice_memory_size()?)
+}
+
+fn get_binary_view_value_size(array_data: &ArrayData) -> Result<usize, 
ArrowError> {
+    let mut result: usize = 0;
+    let array_data_type = array_data.data_type();
+
+    if matches!(array_data_type, DataType::BinaryView | DataType::Utf8View) {

Review Comment:
   Comment was added to `get_array_data_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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to