-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
parquet.rs
804 lines (726 loc) · 28.6 KB
/
parquet.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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
// 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.
//! Execution plan for reading Parquet files
use std::fmt;
use std::sync::Arc;
use std::{any::Any, convert::TryInto};
use crate::datasource::file_format::parquet::ChunkObjectReader;
use crate::datasource::object_store::ObjectStore;
use crate::datasource::PartitionedFile;
use crate::{
error::{DataFusionError, Result},
logical_plan::{Column, Expr},
physical_optimizer::pruning::{PruningPredicate, PruningStatistics},
physical_plan::{
file_format::PhysicalPlanConfig,
metrics::{self, ExecutionPlanMetricsSet, MetricBuilder, MetricsSet},
stream::RecordBatchReceiverStream,
DisplayFormatType, ExecutionPlan, Partitioning, SendableRecordBatchStream,
Statistics,
},
scalar::ScalarValue,
};
use arrow::{
array::ArrayRef,
datatypes::{Schema, SchemaRef},
error::{ArrowError, Result as ArrowResult},
record_batch::RecordBatch,
};
use log::debug;
use parquet::file::{
metadata::RowGroupMetaData,
reader::{FileReader, SerializedFileReader},
statistics::Statistics as ParquetStatistics,
};
use fmt::Debug;
use parquet::arrow::{ArrowReader, ParquetFileArrowReader};
use tokio::{
sync::mpsc::{channel, Receiver, Sender},
task,
};
use async_trait::async_trait;
use super::PartitionColumnProjector;
/// Execution plan for scanning one or more Parquet partitions
#[derive(Debug, Clone)]
pub struct ParquetExec {
base_config: PhysicalPlanConfig,
projected_statistics: Statistics,
projected_schema: SchemaRef,
/// Execution metrics
metrics: ExecutionPlanMetricsSet,
/// Optional predicate builder
predicate_builder: Option<PruningPredicate>,
}
/// Stores metrics about the parquet execution for a particular parquet file
#[derive(Debug, Clone)]
struct ParquetFileMetrics {
/// Number of times the predicate could not be evaluated
pub predicate_evaluation_errors: metrics::Count,
/// Number of row groups pruned using
pub row_groups_pruned: metrics::Count,
}
impl ParquetExec {
/// Create a new Parquet reader execution plan provided file list and schema.
/// Even if `limit` is set, ParquetExec rounds up the number of records to the next `batch_size`.
pub fn new(base_config: PhysicalPlanConfig, predicate: Option<Expr>) -> Self {
debug!("Creating ParquetExec, files: {:?}, projection {:?}, predicate: {:?}, limit: {:?}",
base_config.file_groups, base_config.projection, predicate, base_config.limit);
let metrics = ExecutionPlanMetricsSet::new();
let predicate_creation_errors =
MetricBuilder::new(&metrics).global_counter("num_predicate_creation_errors");
let predicate_builder = predicate.and_then(|predicate_expr| {
match PruningPredicate::try_new(
&predicate_expr,
base_config.file_schema.clone(),
) {
Ok(predicate_builder) => Some(predicate_builder),
Err(e) => {
debug!(
"Could not create pruning predicate for {:?}: {}",
predicate_expr, e
);
predicate_creation_errors.add(1);
None
}
}
});
let (projected_schema, projected_statistics) = base_config.project();
Self {
base_config,
projected_schema,
projected_statistics,
metrics,
predicate_builder,
}
}
/// Ref to the base configs
pub fn base_config(&self) -> &PhysicalPlanConfig {
&self.base_config
}
}
impl ParquetFileMetrics {
/// Create new metrics
pub fn new(
partition: usize,
filename: &str,
metrics: &ExecutionPlanMetricsSet,
) -> Self {
let predicate_evaluation_errors = MetricBuilder::new(metrics)
.with_new_label("filename", filename.to_string())
.counter("predicate_evaluation_errors", partition);
let row_groups_pruned = MetricBuilder::new(metrics)
.with_new_label("filename", filename.to_string())
.counter("row_groups_pruned", partition);
Self {
predicate_evaluation_errors,
row_groups_pruned,
}
}
}
#[async_trait]
impl ExecutionPlan for ParquetExec {
/// Return a reference to Any that can be used for downcasting
fn as_any(&self) -> &dyn Any {
self
}
fn schema(&self) -> SchemaRef {
Arc::clone(&self.projected_schema)
}
fn children(&self) -> Vec<Arc<dyn ExecutionPlan>> {
// this is a leaf node and has no children
vec![]
}
/// Get the output partitioning of this plan
fn output_partitioning(&self) -> Partitioning {
Partitioning::UnknownPartitioning(self.base_config.file_groups.len())
}
fn with_new_children(
&self,
children: Vec<Arc<dyn ExecutionPlan>>,
) -> Result<Arc<dyn ExecutionPlan>> {
if children.is_empty() {
Ok(Arc::new(self.clone()))
} else {
Err(DataFusionError::Internal(format!(
"Children cannot be replaced in {:?}",
self
)))
}
}
async fn execute(&self, partition_index: usize) -> Result<SendableRecordBatchStream> {
// because the parquet implementation is not thread-safe, it is necessary to execute
// on a thread and communicate with channels
let (response_tx, response_rx): (
Sender<ArrowResult<RecordBatch>>,
Receiver<ArrowResult<RecordBatch>>,
) = channel(2);
let partition = self.base_config.file_groups[partition_index].clone();
let metrics = self.metrics.clone();
let projection = match self.base_config.file_column_projection_indices() {
Some(proj) => proj,
None => (0..self.base_config.file_schema.fields().len()).collect(),
};
let predicate_builder = self.predicate_builder.clone();
let batch_size = self.base_config.batch_size;
let limit = self.base_config.limit;
let object_store = Arc::clone(&self.base_config.object_store);
let partition_col_proj = PartitionColumnProjector::new(
Arc::clone(&self.projected_schema),
&self.base_config.table_partition_cols,
);
let join_handle = task::spawn_blocking(move || {
if let Err(e) = read_partition(
object_store.as_ref(),
partition_index,
partition,
metrics,
&projection,
&predicate_builder,
batch_size,
response_tx,
limit,
partition_col_proj,
) {
println!("Parquet reader thread terminated due to error: {:?}", e);
}
});
Ok(RecordBatchReceiverStream::create(
&self.projected_schema,
response_rx,
join_handle,
))
}
fn fmt_as(
&self,
t: DisplayFormatType,
f: &mut std::fmt::Formatter,
) -> std::fmt::Result {
match t {
DisplayFormatType::Default => {
write!(
f,
"ParquetExec: batch_size={}, limit={:?}, partitions={}",
self.base_config.batch_size,
self.base_config.limit,
super::FileGroupsDisplay(&self.base_config.file_groups)
)
}
}
}
fn metrics(&self) -> Option<MetricsSet> {
Some(self.metrics.clone_inner())
}
fn statistics(&self) -> Statistics {
self.projected_statistics.clone()
}
}
fn send_result(
response_tx: &Sender<ArrowResult<RecordBatch>>,
result: ArrowResult<RecordBatch>,
) -> Result<()> {
// Note this function is running on its own blockng tokio thread so blocking here is ok.
response_tx
.blocking_send(result)
.map_err(|e| DataFusionError::Execution(e.to_string()))?;
Ok(())
}
/// Wraps parquet statistics in a way
/// that implements [`PruningStatistics`]
struct RowGroupPruningStatistics<'a> {
row_group_metadata: &'a [RowGroupMetaData],
parquet_schema: &'a Schema,
}
/// Extract the min/max statistics from a `ParquetStatistics` object
macro_rules! get_statistic {
($column_statistics:expr, $func:ident, $bytes_func:ident) => {{
if !$column_statistics.has_min_max_set() {
return None;
}
match $column_statistics {
ParquetStatistics::Boolean(s) => Some(ScalarValue::Boolean(Some(*s.$func()))),
ParquetStatistics::Int32(s) => Some(ScalarValue::Int32(Some(*s.$func()))),
ParquetStatistics::Int64(s) => Some(ScalarValue::Int64(Some(*s.$func()))),
// 96 bit ints not supported
ParquetStatistics::Int96(_) => None,
ParquetStatistics::Float(s) => Some(ScalarValue::Float32(Some(*s.$func()))),
ParquetStatistics::Double(s) => Some(ScalarValue::Float64(Some(*s.$func()))),
ParquetStatistics::ByteArray(s) => {
let s = std::str::from_utf8(s.$bytes_func())
.map(|s| s.to_string())
.ok();
Some(ScalarValue::Utf8(s))
}
// type not supported yet
ParquetStatistics::FixedLenByteArray(_) => None,
}
}};
}
// Extract the min or max value calling `func` or `bytes_func` on the ParquetStatistics as appropriate
macro_rules! get_min_max_values {
($self:expr, $column:expr, $func:ident, $bytes_func:ident) => {{
let (column_index, field) = if let Some((v, f)) = $self.parquet_schema.column_with_name(&$column.name) {
(v, f)
} else {
// Named column was not present
return None
};
let data_type = field.data_type();
let null_scalar: ScalarValue = if let Ok(v) = data_type.try_into() {
v
} else {
// DataFusion doesn't have support for ScalarValues of the column type
return None
};
let scalar_values : Vec<ScalarValue> = $self.row_group_metadata
.iter()
.flat_map(|meta| {
meta.column(column_index).statistics()
})
.map(|stats| {
get_statistic!(stats, $func, $bytes_func)
})
.map(|maybe_scalar| {
// column either did't have statistics at all or didn't have min/max values
maybe_scalar.unwrap_or_else(|| null_scalar.clone())
})
.collect();
// ignore errors converting to arrays (e.g. different types)
ScalarValue::iter_to_array(scalar_values).ok()
}}
}
impl<'a> PruningStatistics for RowGroupPruningStatistics<'a> {
fn min_values(&self, column: &Column) -> Option<ArrayRef> {
get_min_max_values!(self, column, min, min_bytes)
}
fn max_values(&self, column: &Column) -> Option<ArrayRef> {
get_min_max_values!(self, column, max, max_bytes)
}
fn num_containers(&self) -> usize {
self.row_group_metadata.len()
}
}
fn build_row_group_predicate(
predicate_builder: &PruningPredicate,
metrics: ParquetFileMetrics,
row_group_metadata: &[RowGroupMetaData],
) -> Box<dyn Fn(&RowGroupMetaData, usize) -> bool> {
let parquet_schema = predicate_builder.schema().as_ref();
let pruning_stats = RowGroupPruningStatistics {
row_group_metadata,
parquet_schema,
};
let predicate_values = predicate_builder.prune(&pruning_stats);
match predicate_values {
Ok(values) => {
// NB: false means don't scan row group
let num_pruned = values.iter().filter(|&v| !*v).count();
metrics.row_groups_pruned.add(num_pruned);
Box::new(move |_, i| values[i])
}
// stats filter array could not be built
// return a closure which will not filter out any row groups
Err(e) => {
debug!("Error evaluating row group predicate values {}", e);
metrics.predicate_evaluation_errors.add(1);
Box::new(|_r, _i| true)
}
}
}
#[allow(clippy::too_many_arguments)]
fn read_partition(
object_store: &dyn ObjectStore,
partition_index: usize,
partition: Vec<PartitionedFile>,
metrics: ExecutionPlanMetricsSet,
projection: &[usize],
predicate_builder: &Option<PruningPredicate>,
batch_size: usize,
response_tx: Sender<ArrowResult<RecordBatch>>,
limit: Option<usize>,
mut partition_column_projector: PartitionColumnProjector,
) -> Result<()> {
let mut total_rows = 0;
'outer: for partitioned_file in partition {
let file_metrics = ParquetFileMetrics::new(
partition_index,
&*partitioned_file.file_meta.path(),
&metrics,
);
let object_reader =
object_store.file_reader(partitioned_file.file_meta.sized_file.clone())?;
let mut file_reader =
SerializedFileReader::new(ChunkObjectReader(object_reader))?;
if let Some(predicate_builder) = predicate_builder {
let row_group_predicate = build_row_group_predicate(
predicate_builder,
file_metrics,
file_reader.metadata().row_groups(),
);
file_reader.filter_row_groups(&row_group_predicate);
}
let mut arrow_reader = ParquetFileArrowReader::new(Arc::new(file_reader));
let mut batch_reader = arrow_reader
.get_record_reader_by_columns(projection.to_owned(), batch_size)?;
loop {
match batch_reader.next() {
Some(Ok(batch)) => {
total_rows += batch.num_rows();
let proj_batch = partition_column_projector
.project(batch, &partitioned_file.partition_values);
send_result(&response_tx, proj_batch)?;
if limit.map(|l| total_rows >= l).unwrap_or(false) {
break 'outer;
}
}
None => {
break;
}
Some(Err(e)) => {
let err_msg = format!(
"Error reading batch from {}: {}",
partitioned_file,
e.to_string()
);
// send error to operator
send_result(
&response_tx,
Err(ArrowError::ParquetError(err_msg.clone())),
)?;
// terminate thread with error
return Err(DataFusionError::Execution(err_msg));
}
}
}
}
// finished reading files (dropping response_tx will close
// channel)
Ok(())
}
#[cfg(test)]
mod tests {
use crate::datasource::{
file_format::{parquet::ParquetFormat, FileFormat},
object_store::local::{
local_object_reader_stream, local_unpartitioned_file, LocalFileSystem,
},
};
use super::*;
use arrow::datatypes::{DataType, Field};
use futures::StreamExt;
use parquet::{
basic::Type as PhysicalType,
file::{metadata::RowGroupMetaData, statistics::Statistics as ParquetStatistics},
schema::types::SchemaDescPtr,
};
#[tokio::test]
async fn parquet_exec_with_projection() -> Result<()> {
let testdata = crate::test_util::parquet_test_data();
let filename = format!("{}/alltypes_plain.parquet", testdata);
let parquet_exec = ParquetExec::new(
PhysicalPlanConfig {
object_store: Arc::new(LocalFileSystem {}),
file_groups: vec![vec![local_unpartitioned_file(filename.clone())]],
file_schema: ParquetFormat::default()
.infer_schema(local_object_reader_stream(vec![filename]))
.await?,
statistics: Statistics::default(),
projection: Some(vec![0, 1, 2]),
batch_size: 1024,
limit: None,
table_partition_cols: vec![],
},
None,
);
assert_eq!(parquet_exec.output_partitioning().partition_count(), 1);
let mut results = parquet_exec.execute(0).await?;
let batch = results.next().await.unwrap()?;
assert_eq!(8, batch.num_rows());
assert_eq!(3, batch.num_columns());
let schema = batch.schema();
let field_names: Vec<&str> =
schema.fields().iter().map(|f| f.name().as_str()).collect();
assert_eq!(vec!["id", "bool_col", "tinyint_col"], field_names);
let batch = results.next().await;
assert!(batch.is_none());
let batch = results.next().await;
assert!(batch.is_none());
let batch = results.next().await;
assert!(batch.is_none());
Ok(())
}
#[tokio::test]
async fn parquet_exec_with_partition() -> Result<()> {
let testdata = crate::test_util::parquet_test_data();
let filename = format!("{}/alltypes_plain.parquet", testdata);
let mut partitioned_file = local_unpartitioned_file(filename.clone());
partitioned_file.partition_values = vec![
ScalarValue::Utf8(Some("2021".to_owned())),
ScalarValue::Utf8(Some("10".to_owned())),
ScalarValue::Utf8(Some("26".to_owned())),
];
let parquet_exec = ParquetExec::new(
PhysicalPlanConfig {
object_store: Arc::new(LocalFileSystem {}),
file_groups: vec![vec![partitioned_file]],
file_schema: ParquetFormat::default()
.infer_schema(local_object_reader_stream(vec![filename]))
.await?,
statistics: Statistics::default(),
// file has 10 cols so index 12 should be month
projection: Some(vec![0, 1, 2, 12]),
batch_size: 1024,
limit: None,
table_partition_cols: vec![
"year".to_owned(),
"month".to_owned(),
"day".to_owned(),
],
},
None,
);
assert_eq!(parquet_exec.output_partitioning().partition_count(), 1);
let mut results = parquet_exec.execute(0).await?;
let batch = results.next().await.unwrap()?;
let expected = vec![
"+----+----------+-------------+-------+",
"| id | bool_col | tinyint_col | month |",
"+----+----------+-------------+-------+",
"| 4 | true | 0 | 10 |",
"| 5 | false | 1 | 10 |",
"| 6 | true | 0 | 10 |",
"| 7 | false | 1 | 10 |",
"| 2 | true | 0 | 10 |",
"| 3 | false | 1 | 10 |",
"| 0 | true | 0 | 10 |",
"| 1 | false | 1 | 10 |",
"+----+----------+-------------+-------+",
];
crate::assert_batches_eq!(expected, &[batch]);
let batch = results.next().await;
assert!(batch.is_none());
Ok(())
}
fn parquet_file_metrics() -> ParquetFileMetrics {
let metrics = Arc::new(ExecutionPlanMetricsSet::new());
ParquetFileMetrics::new(0, "file.parquet", &metrics)
}
#[test]
fn row_group_predicate_builder_simple_expr() -> Result<()> {
use crate::logical_plan::{col, lit};
// int > 1 => c1_max > 1
let expr = col("c1").gt(lit(15));
let schema = Schema::new(vec![Field::new("c1", DataType::Int32, false)]);
let predicate_builder = PruningPredicate::try_new(&expr, Arc::new(schema))?;
let schema_descr = get_test_schema_descr(vec![("c1", PhysicalType::INT32)]);
let rgm1 = get_row_group_meta_data(
&schema_descr,
vec![ParquetStatistics::int32(Some(1), Some(10), None, 0, false)],
);
let rgm2 = get_row_group_meta_data(
&schema_descr,
vec![ParquetStatistics::int32(Some(11), Some(20), None, 0, false)],
);
let row_group_metadata = vec![rgm1, rgm2];
let row_group_predicate = build_row_group_predicate(
&predicate_builder,
parquet_file_metrics(),
&row_group_metadata,
);
let row_group_filter = row_group_metadata
.iter()
.enumerate()
.map(|(i, g)| row_group_predicate(g, i))
.collect::<Vec<_>>();
assert_eq!(row_group_filter, vec![false, true]);
Ok(())
}
#[test]
fn row_group_predicate_builder_missing_stats() -> Result<()> {
use crate::logical_plan::{col, lit};
// int > 1 => c1_max > 1
let expr = col("c1").gt(lit(15));
let schema = Schema::new(vec![Field::new("c1", DataType::Int32, false)]);
let predicate_builder = PruningPredicate::try_new(&expr, Arc::new(schema))?;
let schema_descr = get_test_schema_descr(vec![("c1", PhysicalType::INT32)]);
let rgm1 = get_row_group_meta_data(
&schema_descr,
vec![ParquetStatistics::int32(None, None, None, 0, false)],
);
let rgm2 = get_row_group_meta_data(
&schema_descr,
vec![ParquetStatistics::int32(Some(11), Some(20), None, 0, false)],
);
let row_group_metadata = vec![rgm1, rgm2];
let row_group_predicate = build_row_group_predicate(
&predicate_builder,
parquet_file_metrics(),
&row_group_metadata,
);
let row_group_filter = row_group_metadata
.iter()
.enumerate()
.map(|(i, g)| row_group_predicate(g, i))
.collect::<Vec<_>>();
// missing statistics for first row group mean that the result from the predicate expression
// is null / undefined so the first row group can't be filtered out
assert_eq!(row_group_filter, vec![true, true]);
Ok(())
}
#[test]
fn row_group_predicate_builder_partial_expr() -> Result<()> {
use crate::logical_plan::{col, lit};
// test row group predicate with partially supported expression
// int > 1 and int % 2 => c1_max > 1 and true
let expr = col("c1").gt(lit(15)).and(col("c2").modulus(lit(2)));
let schema = Arc::new(Schema::new(vec![
Field::new("c1", DataType::Int32, false),
Field::new("c2", DataType::Int32, false),
]));
let predicate_builder = PruningPredicate::try_new(&expr, schema.clone())?;
let schema_descr = get_test_schema_descr(vec![
("c1", PhysicalType::INT32),
("c2", PhysicalType::INT32),
]);
let rgm1 = get_row_group_meta_data(
&schema_descr,
vec![
ParquetStatistics::int32(Some(1), Some(10), None, 0, false),
ParquetStatistics::int32(Some(1), Some(10), None, 0, false),
],
);
let rgm2 = get_row_group_meta_data(
&schema_descr,
vec![
ParquetStatistics::int32(Some(11), Some(20), None, 0, false),
ParquetStatistics::int32(Some(11), Some(20), None, 0, false),
],
);
let row_group_metadata = vec![rgm1, rgm2];
let row_group_predicate = build_row_group_predicate(
&predicate_builder,
parquet_file_metrics(),
&row_group_metadata,
);
let row_group_filter = row_group_metadata
.iter()
.enumerate()
.map(|(i, g)| row_group_predicate(g, i))
.collect::<Vec<_>>();
// the first row group is still filtered out because the predicate expression can be partially evaluated
// when conditions are joined using AND
assert_eq!(row_group_filter, vec![false, true]);
// if conditions in predicate are joined with OR and an unsupported expression is used
// this bypasses the entire predicate expression and no row groups are filtered out
let expr = col("c1").gt(lit(15)).or(col("c2").modulus(lit(2)));
let predicate_builder = PruningPredicate::try_new(&expr, schema)?;
let row_group_predicate = build_row_group_predicate(
&predicate_builder,
parquet_file_metrics(),
&row_group_metadata,
);
let row_group_filter = row_group_metadata
.iter()
.enumerate()
.map(|(i, g)| row_group_predicate(g, i))
.collect::<Vec<_>>();
assert_eq!(row_group_filter, vec![true, true]);
Ok(())
}
#[test]
fn row_group_predicate_builder_null_expr() -> Result<()> {
use crate::logical_plan::{col, lit};
// test row group predicate with an unknown (Null) expr
//
// int > 1 and bool = NULL => c1_max > 1 and null
let expr = col("c1")
.gt(lit(15))
.and(col("c2").eq(lit(ScalarValue::Boolean(None))));
let schema = Arc::new(Schema::new(vec![
Field::new("c1", DataType::Int32, false),
Field::new("c2", DataType::Boolean, false),
]));
let predicate_builder = PruningPredicate::try_new(&expr, schema)?;
let schema_descr = get_test_schema_descr(vec![
("c1", PhysicalType::INT32),
("c2", PhysicalType::BOOLEAN),
]);
let rgm1 = get_row_group_meta_data(
&schema_descr,
vec![
ParquetStatistics::int32(Some(1), Some(10), None, 0, false),
ParquetStatistics::boolean(Some(false), Some(true), None, 0, false),
],
);
let rgm2 = get_row_group_meta_data(
&schema_descr,
vec![
ParquetStatistics::int32(Some(11), Some(20), None, 0, false),
ParquetStatistics::boolean(Some(false), Some(true), None, 0, false),
],
);
let row_group_metadata = vec![rgm1, rgm2];
let row_group_predicate = build_row_group_predicate(
&predicate_builder,
parquet_file_metrics(),
&row_group_metadata,
);
let row_group_filter = row_group_metadata
.iter()
.enumerate()
.map(|(i, g)| row_group_predicate(g, i))
.collect::<Vec<_>>();
// no row group is filtered out because the predicate expression can't be evaluated
// when a null array is generated for a statistics column,
// because the null values propagate to the end result, making the predicate result undefined
assert_eq!(row_group_filter, vec![true, true]);
Ok(())
}
fn get_row_group_meta_data(
schema_descr: &SchemaDescPtr,
column_statistics: Vec<ParquetStatistics>,
) -> RowGroupMetaData {
use parquet::file::metadata::ColumnChunkMetaData;
let mut columns = vec![];
for (i, s) in column_statistics.iter().enumerate() {
let column = ColumnChunkMetaData::builder(schema_descr.column(i))
.set_statistics(s.clone())
.build()
.unwrap();
columns.push(column);
}
RowGroupMetaData::builder(schema_descr.clone())
.set_num_rows(1000)
.set_total_byte_size(2000)
.set_column_metadata(columns)
.build()
.unwrap()
}
fn get_test_schema_descr(fields: Vec<(&str, PhysicalType)>) -> SchemaDescPtr {
use parquet::schema::types::{SchemaDescriptor, Type as SchemaType};
let mut schema_fields = fields
.iter()
.map(|(n, t)| {
Arc::new(SchemaType::primitive_type_builder(n, *t).build().unwrap())
})
.collect::<Vec<_>>();
let schema = SchemaType::group_type_builder("schema")
.with_fields(&mut schema_fields)
.build()
.unwrap();
Arc::new(SchemaDescriptor::new(Arc::new(schema)))
}
}