aisle 0.2.1

Metadata-driven Parquet pruning for Rust: Skip irrelevant data before reading
Documentation
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};

use arrow_schema::Schema;
use parquet::{
    arrow::arrow_reader::{RowSelection, RowSelector},
    bloom_filter::Sbbf,
    file::metadata::ParquetMetaData,
};

use super::{
    context::{RowGroupContext, build_column_lookup},
    eval,
    options::PruneOptions,
    provider::{AsyncBloomFilterProvider, DictionaryHintEvidence, DictionaryHintValue},
    result::PruneResult,
};
use crate::{
    AisleResult,
    expr::rewrite::{self, MetadataHintConfig},
    expr::{Expr, TriState},
    selection::row_selection_to_roaring,
};

pub(crate) fn prune_compiled(
    metadata: &ParquetMetaData,
    schema: &Schema,
    compile: AisleResult,
    options: &PruneOptions,
    output_projection: Option<Vec<String>>,
    predicate_columns: Option<Vec<String>>,
) -> PruneResult {
    let evaluator = PruneEvaluator::new(metadata, schema);
    let hints = MetadataHintConfig {
        bloom: options.enable_bloom_filter(),
        dictionary: options.enable_dictionary_hints(),
    };
    let predicates = if hints.bloom || hints.dictionary {
        Cow::Owned(rewrite::inject_metadata_hints_all(
            compile.prunable(),
            hints,
        ))
    } else {
        Cow::Borrowed(compile.prunable())
    };
    let mut row_groups = Vec::new();
    let mut selections = Vec::new();
    let mut any_selection = false;

    for row_group_idx in 0..metadata.num_row_groups() {
        let row_count = metadata.row_group(row_group_idx).num_rows() as usize;
        let tri = evaluator.eval_row_group_conjunction(
            predicates.as_ref(),
            row_group_idx,
            None,
            None,
            options,
        );
        if tri == TriState::False {
            continue;
        }

        let mut selection = if options.enable_page_index() {
            evaluator.eval_pages_for_predicates(predicates.as_ref(), row_group_idx, options)
        } else {
            None
        };

        if let Some(sel) = selection.as_ref() {
            if !sel.selects_any() {
                continue;
            }
            any_selection = true;
        }

        row_groups.push(row_group_idx);
        selections.push((row_count, selection.take()));
    }

    let row_selection = if any_selection {
        Some(concat_selections(&selections))
    } else {
        None
    };

    let roaring = row_selection.as_ref().and_then(|selection| {
        if options.emit_roaring() {
            let total_rows: u64 = selections.iter().map(|(rows, _)| *rows as u64).sum();

            if total_rows > u32::MAX as u64 {
                // Dataset too large for RoaringBitmap
                // This is expected for very large datasets (>4.2B rows)
                // Users should use RowSelection directly in this case
                eprintln!(
                    "Note: Dataset has {} rows (exceeds u32::MAX limit of {}). RoaringBitmap \
                     output skipped. Use RowSelection for large datasets.",
                    total_rows,
                    u32::MAX
                );
                None
            } else {
                row_selection_to_roaring(selection, total_rows)
            }
        } else {
            None
        }
    });

    PruneResult::new(
        row_groups,
        row_selection,
        roaring,
        compile,
        output_projection,
        predicate_columns,
    )
}

pub(crate) async fn prune_compiled_with_bloom_provider<P: AsyncBloomFilterProvider>(
    metadata: &ParquetMetaData,
    schema: &Schema,
    compile: AisleResult,
    options: &PruneOptions,
    provider: &mut P,
    output_projection: Option<Vec<String>>,
    predicate_columns: Option<Vec<String>>,
) -> PruneResult {
    let evaluator = PruneEvaluator::new(metadata, schema);
    let hints = MetadataHintConfig {
        bloom: options.enable_bloom_filter(),
        dictionary: options.enable_dictionary_hints(),
    };
    let predicates = if hints.bloom || hints.dictionary {
        Cow::Owned(rewrite::inject_metadata_hints_all(
            compile.prunable(),
            hints,
        ))
    } else {
        Cow::Borrowed(compile.prunable())
    };
    let mut row_groups = Vec::new();
    let mut selections = Vec::new();
    let mut any_selection = false;

    let bloom_columns = if options.enable_bloom_filter() {
        collect_bloom_columns(predicates.as_ref())
    } else {
        HashSet::new()
    };
    let dictionary_columns = if options.enable_dictionary_hints() {
        collect_dictionary_columns(predicates.as_ref())
    } else {
        HashSet::new()
    };
    let bloom_column_indices = resolve_column_indices(evaluator.column_lookup(), &bloom_columns);
    let dictionary_column_indices =
        resolve_column_indices(evaluator.column_lookup(), &dictionary_columns);

    for row_group_idx in 0..metadata.num_row_groups() {
        let row_count = metadata.row_group(row_group_idx).num_rows() as usize;
        let bloom_filters = if !bloom_column_indices.is_empty() {
            load_bloom_filters_async(provider, row_group_idx, &bloom_column_indices).await
        } else {
            None
        };
        let dictionary_hints = if !dictionary_column_indices.is_empty() {
            load_dictionary_hints_async(provider, row_group_idx, &dictionary_column_indices).await
        } else {
            None
        };

        let tri = evaluator.eval_row_group_conjunction(
            predicates.as_ref(),
            row_group_idx,
            bloom_filters,
            dictionary_hints,
            options,
        );
        if tri == TriState::False {
            continue;
        }

        let mut selection = if options.enable_page_index() {
            evaluator.eval_pages_for_predicates(predicates.as_ref(), row_group_idx, options)
        } else {
            None
        };

        if let Some(sel) = selection.as_ref() {
            if !sel.selects_any() {
                continue;
            }
            any_selection = true;
        }

        row_groups.push(row_group_idx);
        selections.push((row_count, selection.take()));
    }

    let row_selection = if any_selection {
        Some(concat_selections(&selections))
    } else {
        None
    };

    let roaring = row_selection.as_ref().and_then(|selection| {
        if options.emit_roaring() {
            let total_rows: u64 = selections.iter().map(|(rows, _)| *rows as u64).sum();

            if total_rows > u32::MAX as u64 {
                // Dataset too large for RoaringBitmap
                // This is expected for very large datasets (>4.2B rows)
                // Users should use RowSelection directly in this case
                eprintln!(
                    "Note: Dataset has {} rows (exceeds u32::MAX limit of {}). RoaringBitmap \
                     output skipped. Use RowSelection for large datasets.",
                    total_rows,
                    u32::MAX
                );
                None
            } else {
                row_selection_to_roaring(selection, total_rows)
            }
        } else {
            None
        }
    });

    PruneResult::new(
        row_groups,
        row_selection,
        roaring,
        compile,
        output_projection,
        predicate_columns,
    )
}

fn concat_selections(selections: &[(usize, Option<RowSelection>)]) -> RowSelection {
    let mut combined = Vec::new();
    for (row_count, selection) in selections {
        let selection = selection
            .clone()
            .unwrap_or_else(|| RowSelection::from(vec![RowSelector::select(*row_count)]));
        let mut selectors: Vec<RowSelector> = selection.into();
        combined.append(&mut selectors);
    }
    RowSelection::from(combined)
}

fn collect_bloom_columns(predicates: &[Expr]) -> HashSet<String> {
    let mut columns = HashSet::new();
    for predicate in predicates {
        collect_bloom_columns_for_expr(predicate, &mut columns);
    }
    columns
}

fn collect_bloom_columns_for_expr(expr: &Expr, columns: &mut HashSet<String>) {
    match expr {
        Expr::BloomFilterEq { column, .. } | Expr::BloomFilterInList { column, .. } => {
            columns.insert(column.clone());
        }
        Expr::And(parts) | Expr::Or(parts) => {
            for part in parts {
                collect_bloom_columns_for_expr(part, columns);
            }
        }
        Expr::Not(inner) => collect_bloom_columns_for_expr(inner, columns),
        Expr::Cmp { .. }
        | Expr::Between { .. }
        | Expr::InList { .. }
        | Expr::DictionaryHintEq { .. }
        | Expr::DictionaryHintInList { .. }
        | Expr::StartsWith { .. }
        | Expr::IsNull { .. }
        | Expr::True
        | Expr::False => {}
    }
}

fn collect_dictionary_columns(predicates: &[Expr]) -> HashSet<String> {
    let mut columns = HashSet::new();
    for predicate in predicates {
        collect_dictionary_columns_for_expr(predicate, &mut columns);
    }
    columns
}

fn collect_dictionary_columns_for_expr(expr: &Expr, columns: &mut HashSet<String>) {
    match expr {
        Expr::DictionaryHintEq { column, .. } | Expr::DictionaryHintInList { column, .. } => {
            columns.insert(column.clone());
        }
        Expr::And(parts) | Expr::Or(parts) => {
            for part in parts {
                collect_dictionary_columns_for_expr(part, columns);
            }
        }
        Expr::Not(inner) => collect_dictionary_columns_for_expr(inner, columns),
        Expr::Cmp { .. }
        | Expr::Between { .. }
        | Expr::InList { .. }
        | Expr::BloomFilterEq { .. }
        | Expr::BloomFilterInList { .. }
        | Expr::StartsWith { .. }
        | Expr::IsNull { .. }
        | Expr::True
        | Expr::False => {}
    }
}

fn resolve_column_indices(
    column_lookup: &HashMap<String, usize>,
    columns: &HashSet<String>,
) -> Vec<usize> {
    let mut indices = Vec::with_capacity(columns.len());
    for column in columns {
        let Some(col_idx) = column_lookup.get(column) else {
            continue;
        };
        indices.push(*col_idx);
    }
    indices.sort_unstable();
    indices.dedup();
    indices
}

async fn load_bloom_filters_async<P: AsyncBloomFilterProvider>(
    provider: &mut P,
    row_group_idx: usize,
    bloom_column_indices: &[usize],
) -> Option<HashMap<usize, Sbbf>> {
    let mut requests = Vec::with_capacity(bloom_column_indices.len());
    for &column_idx in bloom_column_indices {
        requests.push((row_group_idx, column_idx));
    }
    if requests.is_empty() {
        return None;
    }

    let batch = provider.bloom_filters_batch(&requests).await;
    let mut filters = HashMap::new();
    for ((rg, col), filter) in batch {
        if rg == row_group_idx {
            filters.insert(col, filter);
        }
    }

    if filters.is_empty() {
        None
    } else {
        Some(filters)
    }
}

async fn load_dictionary_hints_async<P: AsyncBloomFilterProvider>(
    provider: &mut P,
    row_group_idx: usize,
    dictionary_column_indices: &[usize],
) -> Option<HashMap<usize, HashSet<DictionaryHintValue>>> {
    let mut requests = Vec::with_capacity(dictionary_column_indices.len());
    for &column_idx in dictionary_column_indices {
        requests.push((row_group_idx, column_idx));
    }
    if requests.is_empty() {
        return None;
    }

    let batch = provider.dictionary_hints_batch(&requests).await;
    let mut hints = HashMap::new();
    for ((rg, col), evidence) in batch {
        if rg != row_group_idx {
            continue;
        }
        if let DictionaryHintEvidence::Exact(values) = evidence {
            hints.insert(col, values);
        }
    }

    if hints.is_empty() { None } else { Some(hints) }
}

struct PruneEvaluator<'a> {
    metadata: &'a ParquetMetaData,
    schema: &'a Schema,
    column_lookup: HashMap<String, usize>,
}

impl<'a> PruneEvaluator<'a> {
    fn new(metadata: &'a ParquetMetaData, schema: &'a Schema) -> Self {
        let column_lookup = build_column_lookup(metadata.file_metadata().schema_descr());
        Self {
            metadata,
            schema,
            column_lookup,
        }
    }

    fn column_lookup(&self) -> &HashMap<String, usize> {
        &self.column_lookup
    }

    fn row_group_context(
        &self,
        row_group_idx: usize,
        bloom_filters: Option<HashMap<usize, Sbbf>>,
        dictionary_hints: Option<HashMap<usize, HashSet<DictionaryHintValue>>>,
        options: &'a PruneOptions,
    ) -> RowGroupContext<'_> {
        RowGroupContext {
            metadata: self.metadata,
            schema: self.schema,
            column_lookup: &self.column_lookup,
            row_group_idx,
            bloom_filters,
            dictionary_hints,
            options,
        }
    }

    fn eval_row_group_conjunction(
        &self,
        predicates: &[Expr],
        row_group_idx: usize,
        bloom_filters: Option<HashMap<usize, Sbbf>>,
        dictionary_hints: Option<HashMap<usize, HashSet<DictionaryHintValue>>>,
        options: &PruneOptions,
    ) -> TriState {
        let ctx = self.row_group_context(row_group_idx, bloom_filters, dictionary_hints, options);
        eval::eval_conjunction(predicates, &ctx)
    }

    fn eval_pages_for_predicates(
        &self,
        predicates: &[Expr],
        row_group_idx: usize,
        options: &PruneOptions,
    ) -> Option<RowSelection> {
        let ctx = self.row_group_context(row_group_idx, None, None, options);
        eval::page_selection_for_predicates(predicates, &ctx)
    }
}