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
// 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.
//! Support for reading [`ColumnIndexMetaData`] and [`OffsetIndexMetaData`] from parquet metadata.
use crate;
use crateInt96;
use crate;
use crate;
use crateOffsetIndexMetaData;
use crate;
use cratethrift_struct;
use Write;
use Range;
/// Computes the covering range of two optional ranges
///
/// For example `acc_range(Some(7..9), Some(1..3)) = Some(1..9)`
pub
/// Decode a Thrift [`OffsetIndex`] from the provided bytes.
///
/// The passed in bytes contain a serialized Thrift `OffsetIndex` struct as
/// read from a Parquet file.
///
/// Returns an [`OffsetIndexMetaData`] containing page location information.
///
/// # Example
///
/// ```
/// # use parquet::file::reader::{FileReader, SerializedFileReader};
/// # use parquet::file::page_index::index_reader::decode_offset_index;
/// # use std::fs::File;
/// # use std::io::{Read, Seek};
/// # use parquet::errors::Result;
/// #
/// # fn read_offset_index() -> Result<()> {
/// // Open the Parquet file
/// let mut file = File::open("data.parquet")?;
/// let reader = SerializedFileReader::new(file.try_clone()?)?;
/// let metadata = reader.metadata();
///
/// // Select a row group and column to read
/// let row_group_idx = 0;
/// let column_idx = 0;
///
/// // Get the column chunk metadata
/// let row_group = metadata.row_group(row_group_idx);
/// let column_chunk = row_group.column(column_idx);
///
/// // Get the offset index byte range from the column metadata
/// if let Some(range) = column_chunk.offset_index_range() {
/// // Read the offset index bytes from the file
/// let mut buffer = vec![0u8; (range.end - range.start) as usize];
/// file.seek(std::io::SeekFrom::Start(range.start))?;
/// file.read_exact(&mut buffer)?;
///
/// // Decode the offset index
/// let offset_index = decode_offset_index(&buffer)?;
///
/// // Access page location information
/// for (i, page_location) in offset_index.page_locations().iter().enumerate() {
/// println!("Page {}: offset={}, size={}, first_row={}",
/// i,
/// page_location.offset,
/// page_location.compressed_page_size,
/// page_location.first_row_index
/// );
/// }
/// }
/// # Ok(())
/// # }
/// ```
///
/// [`OffsetIndex`]: https://github.com/apache/parquet-format/blob/e94a5d090b324a0c0ee1adbb8ea6b099852dc3cc/src/main/thrift/parquet.thrift#L1253-L1273
// private struct only used for decoding then discarded
thrift_struct!;
/// Decode a Thrift [`ColumnIndex`] from the provided bytes.
///
/// The passed in bytes contain a serialized Thrift `OffsetIndex` struct as
/// read from a Parquet file. The `column_type` can be obtained via
/// [`ColumnChunkMetaData::column_type`].
///
/// Returns a [`ColumnIndexMetaData`] containing per-page statistics.
///
/// # Example
///
/// ```
/// # use parquet::file::reader::{FileReader, SerializedFileReader};
/// # use parquet::file::page_index::index_reader::decode_column_index;
/// # use std::fs::File;
/// # use std::io::{Read, Seek};
/// # use parquet::errors::Result;
/// #
/// # fn read_column_index() -> Result<()> {
/// // Open the Parquet file
/// let mut file = File::open("data.parquet")?;
/// let reader = SerializedFileReader::new(file.try_clone()?)?;
/// let metadata = reader.metadata();
///
/// // Select a row group and column to read
/// let row_group_idx = 0;
/// let column_idx = 0;
///
/// // Get the column chunk metadata
/// let row_group = metadata.row_group(row_group_idx);
/// let column_chunk = row_group.column(column_idx);
///
/// // Get the column index byte range from the column metadata
/// if let Some(range) = column_chunk.column_index_range() {
/// // Get the column type for proper deserialization
/// let column_type = column_chunk.column_type();
///
/// // Read the column index bytes from the file
/// let mut buffer = vec![0u8; (range.end - range.start) as usize];
/// file.seek(std::io::SeekFrom::Start(range.start))?;
/// file.read_exact(&mut buffer)?;
///
/// // Decode the column index
/// let column_index = decode_column_index(&buffer, column_type)?;
///
/// // Access per-page statistics (example for INT32 column)
/// use parquet::file::page_index::column_index::ColumnIndexMetaData;
/// match column_index {
/// ColumnIndexMetaData::INT32(index) => {
/// for (i, (min, max)) in index.min_values().iter()
/// .zip(index.max_values().iter())
/// .enumerate() {
/// println!("Page {}: min={}, max={}", i, min, max);
/// }
/// }
/// _ => println!("Column is not INT32 type"),
/// }
/// }
/// # Ok(())
/// # }
/// ```
///
/// [`ColumnChunkMetaData::column_type`]: crate::file::metadata::ColumnChunkMetaData::column_type
/// [`ColumnIndex`]: https://github.com/apache/parquet-format/blob/e94a5d090b324a0c0ee1adbb8ea6b099852dc3cc/src/main/thrift/parquet.thrift#L1275-1373