-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
row_group_pruning.rs
518 lines (482 loc) · 12.5 KB
/
row_group_pruning.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
// 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.
//! This file contains an end to end test of parquet pruning. It writes
//! data into a parquet file and then verifies row groups are pruned as
//! expected.
use datafusion::prelude::SessionConfig;
use datafusion_common::ScalarValue;
use crate::parquet::Unit::RowGroup;
use crate::parquet::{ContextWithParquet, Scenario};
use datafusion_expr::{col, lit};
async fn test_prune(
case_data_type: Scenario,
sql: &str,
expected_errors: Option<usize>,
expected_row_group_pruned: Option<usize>,
expected_results: usize,
) {
let output = ContextWithParquet::new(case_data_type, RowGroup)
.await
.query(sql)
.await;
println!("{}", output.description());
assert_eq!(output.predicate_evaluation_errors(), expected_errors);
assert_eq!(output.row_groups_pruned(), expected_row_group_pruned);
assert_eq!(
output.result_rows,
expected_results,
"{}",
output.description()
);
}
#[tokio::test]
async fn prune_timestamps_nanos() {
test_prune(
Scenario::Timestamps,
"SELECT * FROM t where nanos < to_timestamp('2020-01-02 01:01:11Z')",
Some(0),
Some(1),
10,
)
.await;
}
#[tokio::test]
async fn prune_timestamps_micros() {
test_prune(
Scenario::Timestamps,
"SELECT * FROM t where micros < to_timestamp_micros('2020-01-02 01:01:11Z')",
Some(0),
Some(1),
10,
)
.await;
}
#[tokio::test]
async fn prune_timestamps_millis() {
test_prune(
Scenario::Timestamps,
"SELECT * FROM t where millis < to_timestamp_millis('2020-01-02 01:01:11Z')",
Some(0),
Some(1),
10,
)
.await;
}
#[tokio::test]
async fn prune_timestamps_seconds() {
test_prune(
Scenario::Timestamps,
"SELECT * FROM t where seconds < to_timestamp_seconds('2020-01-02 01:01:11Z')",
Some(0),
Some(1),
10,
)
.await;
}
#[tokio::test]
async fn prune_date32() {
test_prune(
Scenario::Dates,
"SELECT * FROM t where date32 < cast('2020-01-02' as date)",
Some(0),
Some(3),
1,
)
.await;
}
#[tokio::test]
async fn prune_date64() {
// work around for not being able to cast Date32 to Date64 automatically
let date = "2020-01-02"
.parse::<chrono::NaiveDate>()
.unwrap()
.and_time(chrono::NaiveTime::from_hms_opt(0, 0, 0).unwrap());
let date = ScalarValue::Date64(Some(date.timestamp_millis()));
let output = ContextWithParquet::new(Scenario::Dates, RowGroup)
.await
.query_with_expr(col("date64").lt(lit(date)))
// .query(
// "SELECT * FROM t where date64 < cast('2020-01-02' as date)",
// query results in Plan("'Date64 < Date32' can't be evaluated because there isn't a common type to coerce the types to")
// )
.await;
println!("{}", output.description());
// This should prune out groups without error
assert_eq!(output.predicate_evaluation_errors(), Some(0));
assert_eq!(output.row_groups_pruned(), Some(3));
assert_eq!(output.result_rows, 1, "{}", output.description());
}
#[tokio::test]
async fn prune_disabled() {
test_prune(
Scenario::Timestamps,
"SELECT * FROM t where nanos < to_timestamp('2020-01-02 01:01:11Z')",
Some(0),
Some(1),
10,
)
.await;
// test without pruning
let query = "SELECT * FROM t where nanos < to_timestamp('2020-01-02 01:01:11Z')";
let expected_rows = 10;
let config = SessionConfig::new().with_parquet_pruning(false);
let output = ContextWithParquet::with_config(Scenario::Timestamps, RowGroup, config)
.await
.query(query)
.await;
println!("{}", output.description());
// This should not prune any
assert_eq!(output.predicate_evaluation_errors(), Some(0));
assert_eq!(output.row_groups_pruned(), Some(0));
assert_eq!(
output.result_rows,
expected_rows,
"{}",
output.description()
);
}
#[tokio::test]
async fn prune_int32_lt() {
test_prune(
Scenario::Int32,
"SELECT * FROM t where i < 1",
Some(0),
Some(1),
11,
)
.await;
// result of sql "SELECT * FROM t where i < 1" is same as
// "SELECT * FROM t where -i > -1"
test_prune(
Scenario::Int32,
"SELECT * FROM t where -i > -1",
Some(0),
Some(1),
11,
)
.await;
}
#[tokio::test]
async fn prune_int32_eq() {
test_prune(
Scenario::Int32,
"SELECT * FROM t where i = 1",
Some(0),
Some(3),
1,
)
.await;
}
#[tokio::test]
async fn prune_int32_scalar_fun_and_eq() {
test_prune(
Scenario::Int32,
"SELECT * FROM t where abs(i) = 1 and i = 1",
Some(0),
Some(3),
1,
)
.await;
}
#[tokio::test]
async fn prune_int32_scalar_fun() {
test_prune(
Scenario::Int32,
"SELECT * FROM t where abs(i) = 1",
Some(0),
Some(0),
3,
)
.await;
}
#[tokio::test]
async fn prune_int32_complex_expr() {
test_prune(
Scenario::Int32,
"SELECT * FROM t where i+1 = 1",
Some(0),
Some(0),
2,
)
.await;
}
#[tokio::test]
async fn prune_int32_complex_expr_subtract() {
test_prune(
Scenario::Int32,
"SELECT * FROM t where 1-i > 1",
Some(0),
Some(0),
9,
)
.await;
}
#[tokio::test]
async fn prune_f64_lt() {
test_prune(
Scenario::Float64,
"SELECT * FROM t where f < 1",
Some(0),
Some(1),
11,
)
.await;
test_prune(
Scenario::Float64,
"SELECT * FROM t where -f > -1",
Some(0),
Some(1),
11,
)
.await;
}
#[tokio::test]
async fn prune_f64_scalar_fun_and_gt() {
// result of sql "SELECT * FROM t where abs(f - 1) <= 0.000001 and f >= 0.1"
// only use "f >= 0" to prune
test_prune(
Scenario::Float64,
"SELECT * FROM t where abs(f - 1) <= 0.000001 and f >= 0.1",
Some(0),
Some(2),
1,
)
.await;
}
#[tokio::test]
async fn prune_f64_scalar_fun() {
// result of sql "SELECT * FROM t where abs(f-1) <= 0.000001" is not supported
test_prune(
Scenario::Float64,
"SELECT * FROM t where abs(f-1) <= 0.000001",
Some(0),
Some(0),
1,
)
.await;
}
#[tokio::test]
async fn prune_f64_complex_expr() {
// result of sql "SELECT * FROM t where f+1 > 1.1"" is not supported
test_prune(
Scenario::Float64,
"SELECT * FROM t where f+1 > 1.1",
Some(0),
Some(0),
9,
)
.await;
}
#[tokio::test]
async fn prune_f64_complex_expr_subtract() {
// result of sql "SELECT * FROM t where 1-f > 1" is not supported
test_prune(
Scenario::Float64,
"SELECT * FROM t where 1-f > 1",
Some(0),
Some(0),
9,
)
.await;
}
#[tokio::test]
async fn prune_int32_eq_in_list() {
// result of sql "SELECT * FROM t where in (1)"
test_prune(
Scenario::Int32,
"SELECT * FROM t where i in (1)",
Some(0),
Some(3),
1,
)
.await;
}
#[tokio::test]
async fn prune_int32_eq_in_list_2() {
// result of sql "SELECT * FROM t where in (1000)", prune all
test_prune(
Scenario::Int32,
"SELECT * FROM t where i in (1000)",
Some(0),
Some(4),
0,
)
.await;
}
#[tokio::test]
async fn prune_int32_eq_in_list_negated() {
// result of sql "SELECT * FROM t where not in (1)" prune nothing
test_prune(
Scenario::Int32,
"SELECT * FROM t where i not in (1)",
Some(0),
Some(0),
19,
)
.await;
}
#[tokio::test]
async fn prune_decimal_lt() {
// The data type of decimal_col is decimal(9,2)
// There are three row groups:
// [1.00, 6.00], [-5.00,6.00], [20.00,60.00]
test_prune(
Scenario::Decimal,
"SELECT * FROM t where decimal_col < 4",
Some(0),
Some(1),
6,
)
.await;
// compare with the casted decimal value
test_prune(
Scenario::Decimal,
"SELECT * FROM t where decimal_col < cast(4.55 as decimal(20,2))",
Some(0),
Some(1),
8,
)
.await;
// The data type of decimal_col is decimal(38,2)
test_prune(
Scenario::DecimalLargePrecision,
"SELECT * FROM t where decimal_col < 4",
Some(0),
Some(1),
6,
)
.await;
// compare with the casted decimal value
test_prune(
Scenario::DecimalLargePrecision,
"SELECT * FROM t where decimal_col < cast(4.55 as decimal(20,2))",
Some(0),
Some(1),
8,
)
.await;
}
#[tokio::test]
async fn prune_decimal_eq() {
// The data type of decimal_col is decimal(9,2)
// There are three row groups:
// [1.00, 6.00], [-5.00,6.00], [20.00,60.00]
test_prune(
Scenario::Decimal,
"SELECT * FROM t where decimal_col = 4",
Some(0),
Some(1),
2,
)
.await;
test_prune(
Scenario::Decimal,
"SELECT * FROM t where decimal_col = 4.00",
Some(0),
Some(1),
2,
)
.await;
// The data type of decimal_col is decimal(38,2)
test_prune(
Scenario::DecimalLargePrecision,
"SELECT * FROM t where decimal_col = 4",
Some(0),
Some(1),
2,
)
.await;
test_prune(
Scenario::DecimalLargePrecision,
"SELECT * FROM t where decimal_col = 4.00",
Some(0),
Some(1),
2,
)
.await;
}
#[tokio::test]
async fn prune_decimal_in_list() {
// The data type of decimal_col is decimal(9,2)
// There are three row groups:
// [1.00, 6.00], [-5.00,6.00], [20.00,60.00]
test_prune(
Scenario::Decimal,
"SELECT * FROM t where decimal_col in (4,3,2,123456789123)",
Some(0),
Some(1),
5,
)
.await;
test_prune(
Scenario::Decimal,
"SELECT * FROM t where decimal_col in (4.00,3.00,11.2345,1)",
Some(0),
Some(1),
6,
)
.await;
// The data type of decimal_col is decimal(38,2)
test_prune(
Scenario::DecimalLargePrecision,
"SELECT * FROM t where decimal_col in (4,3,2,123456789123)",
Some(0),
Some(1),
5,
)
.await;
test_prune(
Scenario::DecimalLargePrecision,
"SELECT * FROM t where decimal_col in (4.00,3.00,11.2345,1)",
Some(0),
Some(1),
6,
)
.await;
}
#[tokio::test]
async fn prune_periods_in_column_names() {
// There are three row groups for "service.name", each with 5 rows = 15 rows total
// name = "HTTP GET / DISPATCH", service.name = ['frontend', 'frontend'],
// name = "HTTP PUT / DISPATCH", service.name = ['backend', 'frontend'],
// name = "HTTP GET / DISPATCH", service.name = ['backend', 'backend' ],
test_prune(
Scenario::PeriodsInColumnNames,
// use double quotes to use column named "service.name"
"SELECT \"name\", \"service.name\" FROM t WHERE \"service.name\" = 'frontend'",
Some(0),
Some(1), // prune out last row group
7,
)
.await;
test_prune(
Scenario::PeriodsInColumnNames,
"SELECT \"name\", \"service.name\" FROM t WHERE \"name\" != 'HTTP GET / DISPATCH'",
Some(0),
Some(2), // prune out first and last row group
5,
)
.await;
test_prune(
Scenario::PeriodsInColumnNames,
"SELECT \"name\", \"service.name\" FROM t WHERE \"service.name\" = 'frontend' AND \"name\" != 'HTTP GET / DISPATCH'",
Some(0),
Some(2), // prune out middle and last row group
2,
)
.await;
}