parquet/file/metadata/
memory.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// 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.

//! Memory calculations for [`ParquetMetadata::memory_size`]
//!
//! [`ParquetMetadata::memory_size`]: crate::file::metadata::ParquetMetaData::memory_size
use crate::basic::{ColumnOrder, Compression, Encoding, PageType};
use crate::data_type::private::ParquetValueType;
use crate::file::metadata::{ColumnChunkMetaData, FileMetaData, KeyValue, RowGroupMetaData};
use crate::file::page_encoding_stats::PageEncodingStats;
use crate::file::page_index::index::{Index, NativeIndex, PageIndex};
use crate::file::page_index::offset_index::OffsetIndexMetaData;
use crate::file::statistics::{Statistics, ValueStatistics};
use crate::format::{BoundaryOrder, PageLocation, SortingColumn};
use std::sync::Arc;

/// 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<T: HeapSize> HeapSize for Vec<T> {
    fn heap_size(&self) -> usize {
        let item_size = std::mem::size_of::<T>();
        // account for the contents of the Vec
        (self.capacity() * item_size) +
        // add any heap allocations by contents
        self.iter().map(|t| t.heap_size()).sum::<usize>()
    }
}

impl<T: HeapSize> HeapSize for Arc<T> {
    fn heap_size(&self) -> usize {
        self.as_ref().heap_size()
    }
}

impl<T: HeapSize> HeapSize for Option<T> {
    fn heap_size(&self) -> usize {
        self.as_ref().map(|inner| inner.heap_size()).unwrap_or(0)
    }
}

impl HeapSize for String {
    fn heap_size(&self) -> usize {
        self.capacity()
    }
}

impl HeapSize for FileMetaData {
    fn heap_size(&self) -> usize {
        self.created_by.heap_size()
            + self.key_value_metadata.heap_size()
            + self.schema_descr.heap_size()
            + self.column_orders.heap_size()
    }
}

impl HeapSize for KeyValue {
    fn heap_size(&self) -> usize {
        self.key.heap_size() + self.value.heap_size()
    }
}

impl HeapSize for RowGroupMetaData {
    fn heap_size(&self) -> usize {
        // don't count schema_descr here because it is already
        // counted in FileMetaData
        self.columns.heap_size() + self.sorting_columns.heap_size()
    }
}

impl HeapSize for ColumnChunkMetaData {
    fn heap_size(&self) -> usize {
        // don't count column_descr here because it is already counted in
        // FileMetaData
        self.encodings.heap_size()
            + self.file_path.heap_size()
            + self.compression.heap_size()
            + self.statistics.heap_size()
            + self.encoding_stats.heap_size()
            + self.unencoded_byte_array_data_bytes.heap_size()
            + self.repetition_level_histogram.heap_size()
            + self.definition_level_histogram.heap_size()
    }
}

impl HeapSize for Encoding {
    fn heap_size(&self) -> usize {
        0 // no heap allocations
    }
}

impl HeapSize for PageEncodingStats {
    fn heap_size(&self) -> usize {
        self.page_type.heap_size() + self.encoding.heap_size()
    }
}

impl HeapSize for SortingColumn {
    fn heap_size(&self) -> usize {
        0 // no heap allocations
    }
}
impl HeapSize for Compression {
    fn heap_size(&self) -> usize {
        0 // no heap allocations
    }
}

impl HeapSize for PageType {
    fn heap_size(&self) -> usize {
        0 // no heap allocations
    }
}
impl HeapSize for Statistics {
    fn heap_size(&self) -> usize {
        match self {
            Statistics::Boolean(value_statistics) => value_statistics.heap_size(),
            Statistics::Int32(value_statistics) => value_statistics.heap_size(),
            Statistics::Int64(value_statistics) => value_statistics.heap_size(),
            Statistics::Int96(value_statistics) => value_statistics.heap_size(),
            Statistics::Float(value_statistics) => value_statistics.heap_size(),
            Statistics::Double(value_statistics) => value_statistics.heap_size(),
            Statistics::ByteArray(value_statistics) => value_statistics.heap_size(),
            Statistics::FixedLenByteArray(value_statistics) => value_statistics.heap_size(),
        }
    }
}

impl HeapSize for OffsetIndexMetaData {
    fn heap_size(&self) -> usize {
        self.page_locations.heap_size() + self.unencoded_byte_array_data_bytes.heap_size()
    }
}

impl HeapSize for Index {
    fn heap_size(&self) -> usize {
        match self {
            Index::NONE => 0,
            Index::BOOLEAN(native_index) => native_index.heap_size(),
            Index::INT32(native_index) => native_index.heap_size(),
            Index::INT64(native_index) => native_index.heap_size(),
            Index::INT96(native_index) => native_index.heap_size(),
            Index::FLOAT(native_index) => native_index.heap_size(),
            Index::DOUBLE(native_index) => native_index.heap_size(),
            Index::BYTE_ARRAY(native_index) => native_index.heap_size(),
            Index::FIXED_LEN_BYTE_ARRAY(native_index) => native_index.heap_size(),
        }
    }
}

impl<T: ParquetValueType> HeapSize for NativeIndex<T> {
    fn heap_size(&self) -> usize {
        self.indexes.heap_size() + self.boundary_order.heap_size()
    }
}

impl<T: ParquetValueType> HeapSize for PageIndex<T> {
    fn heap_size(&self) -> usize {
        self.min.heap_size() + self.max.heap_size() + self.null_count.heap_size()
    }
}

impl<T: ParquetValueType> HeapSize for ValueStatistics<T> {
    fn heap_size(&self) -> usize {
        self.min_opt().map(T::heap_size).unwrap_or(0)
            + self.max_opt().map(T::heap_size).unwrap_or(0)
    }
}
impl HeapSize for bool {
    fn heap_size(&self) -> usize {
        0 // no heap allocations
    }
}
impl HeapSize for i32 {
    fn heap_size(&self) -> usize {
        0 // no heap allocations
    }
}
impl HeapSize for i64 {
    fn heap_size(&self) -> usize {
        0 // no heap allocations
    }
}

impl HeapSize for f32 {
    fn heap_size(&self) -> usize {
        0 // no heap allocations
    }
}
impl HeapSize for f64 {
    fn heap_size(&self) -> usize {
        0 // no heap allocations
    }
}

impl HeapSize for usize {
    fn heap_size(&self) -> usize {
        0 // no heap allocations
    }
}

impl HeapSize for BoundaryOrder {
    fn heap_size(&self) -> usize {
        0 // no heap allocations
    }
}

impl HeapSize for PageLocation {
    fn heap_size(&self) -> usize {
        0 // no heap allocations
    }
}

impl HeapSize for ColumnOrder {
    fn heap_size(&self) -> usize {
        0 // no heap allocations in ColumnOrder
    }
}