-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathexplain_analyze.rs
More file actions
1272 lines (1149 loc) · 47 KB
/
explain_analyze.rs
File metadata and controls
1272 lines (1149 loc) · 47 KB
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
use super::*;
use insta::assert_snapshot;
use rstest::rstest;
use datafusion::config::ConfigOptions;
use datafusion::physical_plan::display::DisplayableExecutionPlan;
use datafusion::physical_plan::metrics::Timestamp;
use datafusion_common::format::{ExplainAnalyzeCategories, MetricCategory, MetricType};
use object_store::path::Path;
#[tokio::test]
async fn explain_analyze_baseline_metrics() {
// This test uses the execute function to run an actual plan under EXPLAIN ANALYZE
// and then validate the presence of baseline metrics for supported operators
let config = SessionConfig::new()
.with_target_partitions(3)
.with_batch_size(4096);
let ctx = SessionContext::new_with_config(config);
register_aggregate_csv_by_sql(&ctx).await;
// a query with as many operators as we have metrics for
let sql = "EXPLAIN ANALYZE \
SELECT count(*) as cnt FROM \
(SELECT count(*), c1 \
FROM aggregate_test_100 \
WHERE c13 != 'C2GT5KVyOPZpgKVl110TyZO0NcJ434' \
GROUP BY c1 \
ORDER BY c1 ) AS a \
UNION ALL \
SELECT 1 as cnt \
UNION ALL \
SELECT lead(c1, 1) OVER () as cnt FROM (select 1 as c1) AS b \
LIMIT 3";
println!("running query: {sql}");
let dataframe = ctx.sql(sql).await.unwrap();
let physical_plan = dataframe.create_physical_plan().await.unwrap();
let task_ctx = ctx.task_ctx();
let results = collect(physical_plan.clone(), task_ctx).await.unwrap();
let formatted = arrow::util::pretty::pretty_format_batches(&results)
.unwrap()
.to_string();
println!("Query Output:\n\n{formatted}");
assert_metrics!(
&formatted,
"AggregateExec: mode=Partial, gby=[]",
"metrics=[output_rows=3, elapsed_compute=",
"output_bytes=",
"output_batches=3"
);
assert_metrics!(
&formatted,
"AggregateExec: mode=Partial, gby=[c1@0 as c1]",
"reduction_factor=5.05% (5/99)"
);
{
let expected_batch_count_after_repartition =
if cfg!(not(feature = "force_hash_collisions")) {
"output_batches=3"
} else {
"output_batches=1"
};
assert_metrics!(
&formatted,
"AggregateExec: mode=FinalPartitioned, gby=[c1@0 as c1]",
"metrics=[output_rows=5, elapsed_compute=",
"output_bytes=",
expected_batch_count_after_repartition
);
assert_metrics!(
&formatted,
"RepartitionExec: partitioning=Hash([c1@0], 3), input_partitions=3",
"metrics=[output_rows=5, elapsed_compute=",
"output_bytes=",
expected_batch_count_after_repartition
);
assert_metrics!(
&formatted,
"ProjectionExec: expr=[]",
"metrics=[output_rows=5, elapsed_compute=",
"output_bytes=",
expected_batch_count_after_repartition
);
}
assert_metrics!(
&formatted,
"FilterExec: c13@1 != C2GT5KVyOPZpgKVl110TyZO0NcJ434",
"metrics=[output_rows=99, elapsed_compute=",
"output_bytes=",
"output_batches=1"
);
assert_metrics!(
&formatted,
"FilterExec: c13@1 != C2GT5KVyOPZpgKVl110TyZO0NcJ434",
"selectivity=99% (99/100)"
);
assert_metrics!(
&formatted,
"UnionExec",
"metrics=[output_rows=3, elapsed_compute=",
"output_bytes=",
"output_batches=3"
);
assert_metrics!(
&formatted,
"WindowAggExec",
"metrics=[output_rows=1, elapsed_compute=",
"output_bytes=",
"output_batches=1"
);
fn expected_to_have_metrics(plan: &dyn ExecutionPlan) -> bool {
use datafusion::physical_plan;
use datafusion::physical_plan::sorts;
plan.is::<sorts::sort::SortExec>()
|| plan.is::<physical_plan::aggregates::AggregateExec>()
|| plan.is::<physical_plan::filter::FilterExec>()
|| plan.is::<physical_plan::limit::LocalLimitExec>()
|| plan.is::<physical_plan::projection::ProjectionExec>()
|| plan.is::<physical_plan::coalesce_partitions::CoalescePartitionsExec>()
|| plan.is::<physical_plan::union::UnionExec>()
|| plan.is::<physical_plan::windows::WindowAggExec>()
}
// Validate that the recorded elapsed compute time was more than
// zero for all operators as well as the start/end timestamp are set
struct TimeValidator {}
impl ExecutionPlanVisitor for TimeValidator {
type Error = std::convert::Infallible;
fn pre_visit(&mut self, plan: &dyn ExecutionPlan) -> Result<bool, Self::Error> {
if !expected_to_have_metrics(plan) {
return Ok(true);
}
let metrics = plan.metrics().unwrap().aggregate_by_name();
assert!(
metrics.output_rows().unwrap() > 0,
"plan: {}",
DisplayableExecutionPlan::with_metrics(plan).one_line()
);
assert!(
metrics.elapsed_compute().unwrap() > 0,
"plan: {}",
DisplayableExecutionPlan::with_metrics(plan).one_line()
);
let mut saw_start = false;
let mut saw_end = false;
metrics.iter().for_each(|m| match m.value() {
MetricValue::StartTimestamp(ts) => {
saw_start = true;
assert!(nanos_from_timestamp(ts) > 0);
}
MetricValue::EndTimestamp(ts) => {
saw_end = true;
assert!(nanos_from_timestamp(ts) > 0);
}
_ => {}
});
assert!(saw_start);
assert!(saw_end);
Ok(true)
}
}
datafusion::physical_plan::accept(physical_plan.as_ref(), &mut TimeValidator {})
.unwrap();
}
fn nanos_from_timestamp(ts: &Timestamp) -> i64 {
ts.value().unwrap().timestamp_nanos_opt().unwrap()
}
// Test different detail level for config `datafusion.explain.analyze_level`
async fn collect_plan_with_context(
sql_str: &str,
ctx: &SessionContext,
level: MetricType,
) -> String {
{
let state = ctx.state_ref();
let mut state = state.write();
state.config_mut().options_mut().explain.analyze_level = level;
}
let dataframe = ctx.sql(sql_str).await.unwrap();
let batches = dataframe.collect().await.unwrap();
arrow::util::pretty::pretty_format_batches(&batches)
.unwrap()
.to_string()
}
async fn collect_plan_with_categories(
sql_str: &str,
categories: ExplainAnalyzeCategories,
) -> String {
let ctx = SessionContext::new();
{
let state = ctx.state_ref();
let mut state = state.write();
state.config_mut().options_mut().explain.analyze_categories = categories;
}
let dataframe = ctx.sql(sql_str).await.unwrap();
let batches = dataframe.collect().await.unwrap();
arrow::util::pretty::pretty_format_batches(&batches)
.unwrap()
.to_string()
}
async fn collect_plan(sql_str: &str, level: MetricType) -> String {
let ctx = SessionContext::new();
collect_plan_with_context(sql_str, &ctx, level).await
}
#[tokio::test]
async fn explain_analyze_level() {
let sql = "EXPLAIN ANALYZE \
SELECT * \
FROM generate_series(10) as t1(v1) \
ORDER BY v1 DESC";
for (level, needle, should_contain) in [
(MetricType::Summary, "spill_count", false),
(MetricType::Summary, "output_batches", false),
(MetricType::Summary, "output_rows", true),
(MetricType::Summary, "output_bytes", true),
(MetricType::Dev, "spill_count", true),
(MetricType::Dev, "output_rows", true),
(MetricType::Dev, "output_bytes", true),
(MetricType::Dev, "output_batches", true),
] {
let plan = collect_plan(sql, level).await;
assert_eq!(
plan.contains(needle),
should_contain,
"plan for level {level:?} unexpected content: {plan}"
);
}
}
#[tokio::test]
async fn explain_analyze_level_datasource_parquet() {
let table_name = "tpch_lineitem_small";
let parquet_path = "tests/data/tpch_lineitem_small.parquet";
let sql = format!("EXPLAIN ANALYZE SELECT * FROM {table_name}");
// Register test parquet file into context
let ctx = SessionContext::new();
ctx.register_parquet(table_name, parquet_path, ParquetReadOptions::default())
.await
.expect("register parquet table for explain analyze test");
for (level, needle, should_contain) in [
(MetricType::Summary, "metadata_load_time", true),
(MetricType::Summary, "page_index_eval_time", false),
(MetricType::Dev, "metadata_load_time", true),
(MetricType::Dev, "page_index_eval_time", true),
] {
let plan = collect_plan_with_context(&sql, &ctx, level).await;
assert_eq!(
plan.contains(needle),
should_contain,
"plan for level {level:?} unexpected content: {plan}"
);
}
}
#[tokio::test]
async fn explain_analyze_parquet_pruning_metrics() {
let table_name = "tpch_lineitem_small";
let parquet_path = "tests/data/tpch_lineitem_small.parquet";
let ctx = SessionContext::new();
ctx.register_parquet(table_name, parquet_path, ParquetReadOptions::default())
.await
.expect("register parquet table for explain analyze test");
// Test scenario:
// This table's l_orderkey has range [1, 7]
// So the following query can't prune the file:
// select * from tpch_lineitem_small where l_orderkey = 5;
// If change filter to `l_orderkey=10`, the whole file can be pruned using stat.
for (l_orderkey, expected_pruning_metrics) in
[(5, "1 total → 1 matched"), (10, "1 total → 0 matched")]
{
let sql = format!(
"explain analyze select * from {table_name} where l_orderkey = {l_orderkey};"
);
let plan = collect_plan_with_context(&sql, &ctx, MetricType::Summary).await;
let expected_metrics =
format!("files_ranges_pruned_statistics={expected_pruning_metrics}");
assert_metrics!(&plan, "DataSourceExec", &expected_metrics);
}
}
#[tokio::test]
async fn csv_explain_plans() {
// This test verify the look of each plan in its full cycle plan creation
let ctx = SessionContext::new();
register_aggregate_csv_by_sql(&ctx).await;
let sql = "EXPLAIN SELECT c1 FROM aggregate_test_100 where c2 > 10";
// Logical plan
// Create plan
let msg = format!("Creating logical plan for '{sql}'");
let dataframe = ctx.sql(sql).await.expect(&msg);
let logical_schema = dataframe.schema();
let plan = dataframe.logical_plan();
//
println!("SQL: {sql}");
//
// Verify schema
let formatted = plan.display_indent_schema().to_string();
let actual = formatted.trim();
assert_snapshot!(
actual,
@r"
Explain [plan_type:Utf8, plan:Utf8]
Projection: aggregate_test_100.c1 [c1:Utf8View]
Filter: aggregate_test_100.c2 > Int64(10) [c1:Utf8View, c2:Int8, c3:Int16, c4:Int16, c5:Int32, c6:Int64, c7:Int16, c8:Int32, c9:UInt32, c10:UInt64, c11:Float32, c12:Float64, c13:Utf8View]
TableScan: aggregate_test_100 [c1:Utf8View, c2:Int8, c3:Int16, c4:Int16, c5:Int32, c6:Int64, c7:Int16, c8:Int32, c9:UInt32, c10:UInt64, c11:Float32, c12:Float64, c13:Utf8View]
"
);
//
// Verify the text format of the plan
let formatted = plan.display_indent().to_string();
let actual = formatted.trim();
assert_snapshot!(
actual,
@r"
Explain
Projection: aggregate_test_100.c1
Filter: aggregate_test_100.c2 > Int64(10)
TableScan: aggregate_test_100
"
);
//
// verify the grahviz format of the plan
let formatted = plan.display_graphviz().to_string();
let actual = formatted.trim();
assert_snapshot!(
actual,
@r#"
// Begin DataFusion GraphViz Plan,
// display it online here: https://dreampuf.github.io/GraphvizOnline
digraph {
subgraph cluster_1
{
graph[label="LogicalPlan"]
2[shape=box label="Explain"]
3[shape=box label="Projection: aggregate_test_100.c1"]
2 -> 3 [arrowhead=none, arrowtail=normal, dir=back]
4[shape=box label="Filter: aggregate_test_100.c2 > Int64(10)"]
3 -> 4 [arrowhead=none, arrowtail=normal, dir=back]
5[shape=box label="TableScan: aggregate_test_100"]
4 -> 5 [arrowhead=none, arrowtail=normal, dir=back]
}
subgraph cluster_6
{
graph[label="Detailed LogicalPlan"]
7[shape=box label="Explain\nSchema: [plan_type:Utf8, plan:Utf8]"]
8[shape=box label="Projection: aggregate_test_100.c1\nSchema: [c1:Utf8View]"]
7 -> 8 [arrowhead=none, arrowtail=normal, dir=back]
9[shape=box label="Filter: aggregate_test_100.c2 > Int64(10)\nSchema: [c1:Utf8View, c2:Int8, c3:Int16, c4:Int16, c5:Int32, c6:Int64, c7:Int16, c8:Int32, c9:UInt32, c10:UInt64, c11:Float32, c12:Float64, c13:Utf8View]"]
8 -> 9 [arrowhead=none, arrowtail=normal, dir=back]
10[shape=box label="TableScan: aggregate_test_100\nSchema: [c1:Utf8View, c2:Int8, c3:Int16, c4:Int16, c5:Int32, c6:Int64, c7:Int16, c8:Int32, c9:UInt32, c10:UInt64, c11:Float32, c12:Float64, c13:Utf8View]"]
9 -> 10 [arrowhead=none, arrowtail=normal, dir=back]
}
}
// End DataFusion GraphViz Plan
"#
);
// Optimized logical plan
let state = ctx.state();
let msg = format!("Optimizing logical plan for '{sql}': {plan}");
let plan = state.optimize(plan).expect(&msg);
let optimized_logical_schema = plan.schema();
// Both schema has to be the same
assert_eq!(logical_schema, optimized_logical_schema.as_ref());
//
// Verify schema
let formatted = plan.display_indent_schema().to_string();
let actual = formatted.trim();
assert_snapshot!(
actual,
@r"
Explain [plan_type:Utf8, plan:Utf8]
Projection: aggregate_test_100.c1 [c1:Utf8View]
Filter: aggregate_test_100.c2 > Int8(10) [c1:Utf8View, c2:Int8]
TableScan: aggregate_test_100 projection=[c1, c2], partial_filters=[aggregate_test_100.c2 > Int8(10)] [c1:Utf8View, c2:Int8]
"
);
//
// Verify the text format of the plan
let formatted = plan.display_indent().to_string();
let actual = formatted.trim();
assert_snapshot!(
actual,
@r"
Explain
Projection: aggregate_test_100.c1
Filter: aggregate_test_100.c2 > Int8(10)
TableScan: aggregate_test_100 projection=[c1, c2], partial_filters=[aggregate_test_100.c2 > Int8(10)]
"
);
//
// verify the grahviz format of the plan
let formatted = plan.display_graphviz().to_string();
let actual = formatted.trim();
assert_snapshot!(
actual,
@r#"
// Begin DataFusion GraphViz Plan,
// display it online here: https://dreampuf.github.io/GraphvizOnline
digraph {
subgraph cluster_1
{
graph[label="LogicalPlan"]
2[shape=box label="Explain"]
3[shape=box label="Projection: aggregate_test_100.c1"]
2 -> 3 [arrowhead=none, arrowtail=normal, dir=back]
4[shape=box label="Filter: aggregate_test_100.c2 > Int8(10)"]
3 -> 4 [arrowhead=none, arrowtail=normal, dir=back]
5[shape=box label="TableScan: aggregate_test_100 projection=[c1, c2], partial_filters=[aggregate_test_100.c2 > Int8(10)]"]
4 -> 5 [arrowhead=none, arrowtail=normal, dir=back]
}
subgraph cluster_6
{
graph[label="Detailed LogicalPlan"]
7[shape=box label="Explain\nSchema: [plan_type:Utf8, plan:Utf8]"]
8[shape=box label="Projection: aggregate_test_100.c1\nSchema: [c1:Utf8View]"]
7 -> 8 [arrowhead=none, arrowtail=normal, dir=back]
9[shape=box label="Filter: aggregate_test_100.c2 > Int8(10)\nSchema: [c1:Utf8View, c2:Int8]"]
8 -> 9 [arrowhead=none, arrowtail=normal, dir=back]
10[shape=box label="TableScan: aggregate_test_100 projection=[c1, c2], partial_filters=[aggregate_test_100.c2 > Int8(10)]\nSchema: [c1:Utf8View, c2:Int8]"]
9 -> 10 [arrowhead=none, arrowtail=normal, dir=back]
}
}
// End DataFusion GraphViz Plan
"#
);
// Physical plan
// Create plan
let msg = format!("Creating physical plan for '{sql}': {plan}");
let plan = state.create_physical_plan(&plan).await.expect(&msg);
//
// Execute plan
let msg = format!("Executing physical plan for '{sql}': {plan:?}");
let results = collect(plan, state.task_ctx()).await.expect(&msg);
let actual = result_vec(&results);
// flatten to a single string
let actual = actual.into_iter().map(|r| r.join("\t")).collect::<String>();
// Since the plan contains path that are environmentally dependant (e.g. full path of the test file), only verify important content
assert_contains!(&actual, "logical_plan");
assert_contains!(&actual, "Projection: aggregate_test_100.c1");
assert_contains!(actual, "Filter: aggregate_test_100.c2 > Int8(10)");
}
#[tokio::test]
async fn csv_explain_verbose() {
let ctx = SessionContext::new();
register_aggregate_csv_by_sql(&ctx).await;
let sql = "EXPLAIN VERBOSE SELECT c1 FROM aggregate_test_100 where c2 > 10";
let actual = execute(&ctx, sql).await;
// flatten to a single string
let actual = actual.into_iter().map(|r| r.join("\t")).collect::<String>();
// Don't actually test the contents of the debugging output (as
// that may change and keeping this test updated will be a
// pain). Instead just check for a few key pieces.
assert_contains!(&actual, "logical_plan");
assert_contains!(&actual, "physical_plan");
assert_contains!(&actual, "aggregate_test_100.c2 > Int64(10)");
// ensure the "same text as above" optimization is working
assert_contains!(actual, "SAME TEXT AS ABOVE");
}
#[tokio::test]
async fn csv_explain_inlist_verbose() {
let ctx = SessionContext::new();
register_aggregate_csv_by_sql(&ctx).await;
// Inlist len <=3 case will be transformed to OR List so we test with len=4
let sql = "EXPLAIN VERBOSE SELECT c1 FROM aggregate_test_100 where c2 in (1,2,4,5)";
let actual = execute(&ctx, sql).await;
// Optimized by PreCastLitInComparisonExpressions rule
// the data type of c2 is INT8, the type of `1,2,4` is INT64.
// the value of `1,2,4` will be casted to INT8 and pre-calculated
// flatten to a single string
let actual = actual.into_iter().map(|r| r.join("\t")).collect::<String>();
// before optimization (Int64 literals)
assert_contains!(
&actual,
"aggregate_test_100.c2 IN ([Int64(1), Int64(2), Int64(4), Int64(5)])"
);
// after optimization (casted to Int8)
assert_contains!(
&actual,
"aggregate_test_100.c2 IN ([Int8(1), Int8(2), Int8(4), Int8(5)])"
);
}
#[tokio::test]
async fn csv_explain_verbose_plans() {
// This test verify the look of each plan in its full cycle plan creation
let ctx = SessionContext::new();
register_aggregate_csv_by_sql(&ctx).await;
let sql = "EXPLAIN VERBOSE SELECT c1 FROM aggregate_test_100 where c2 > 10";
// Logical plan
// Create plan
let msg = format!("Creating logical plan for '{sql}'");
let dataframe = ctx.sql(sql).await.expect(&msg);
let logical_schema = dataframe.schema().clone();
//
println!("SQL: {sql}");
//
// Verify schema
let formatted = dataframe.logical_plan().display_indent_schema().to_string();
let actual = formatted.trim();
assert_snapshot!(
actual,
@r"
Explain [plan_type:Utf8, plan:Utf8]
Projection: aggregate_test_100.c1 [c1:Utf8View]
Filter: aggregate_test_100.c2 > Int64(10) [c1:Utf8View, c2:Int8, c3:Int16, c4:Int16, c5:Int32, c6:Int64, c7:Int16, c8:Int32, c9:UInt32, c10:UInt64, c11:Float32, c12:Float64, c13:Utf8View]
TableScan: aggregate_test_100 [c1:Utf8View, c2:Int8, c3:Int16, c4:Int16, c5:Int32, c6:Int64, c7:Int16, c8:Int32, c9:UInt32, c10:UInt64, c11:Float32, c12:Float64, c13:Utf8View]
"
);
//
// Verify the text format of the plan
let formatted = dataframe.logical_plan().display_indent().to_string();
let actual = formatted.trim();
assert_snapshot!(
actual,
@r"
Explain
Projection: aggregate_test_100.c1
Filter: aggregate_test_100.c2 > Int64(10)
TableScan: aggregate_test_100
"
);
//
// verify the grahviz format of the plan
let formatted = dataframe.logical_plan().display_graphviz().to_string();
let actual = formatted.trim();
assert_snapshot!(
actual,
@r#"
// Begin DataFusion GraphViz Plan,
// display it online here: https://dreampuf.github.io/GraphvizOnline
digraph {
subgraph cluster_1
{
graph[label="LogicalPlan"]
2[shape=box label="Explain"]
3[shape=box label="Projection: aggregate_test_100.c1"]
2 -> 3 [arrowhead=none, arrowtail=normal, dir=back]
4[shape=box label="Filter: aggregate_test_100.c2 > Int64(10)"]
3 -> 4 [arrowhead=none, arrowtail=normal, dir=back]
5[shape=box label="TableScan: aggregate_test_100"]
4 -> 5 [arrowhead=none, arrowtail=normal, dir=back]
}
subgraph cluster_6
{
graph[label="Detailed LogicalPlan"]
7[shape=box label="Explain\nSchema: [plan_type:Utf8, plan:Utf8]"]
8[shape=box label="Projection: aggregate_test_100.c1\nSchema: [c1:Utf8View]"]
7 -> 8 [arrowhead=none, arrowtail=normal, dir=back]
9[shape=box label="Filter: aggregate_test_100.c2 > Int64(10)\nSchema: [c1:Utf8View, c2:Int8, c3:Int16, c4:Int16, c5:Int32, c6:Int64, c7:Int16, c8:Int32, c9:UInt32, c10:UInt64, c11:Float32, c12:Float64, c13:Utf8View]"]
8 -> 9 [arrowhead=none, arrowtail=normal, dir=back]
10[shape=box label="TableScan: aggregate_test_100\nSchema: [c1:Utf8View, c2:Int8, c3:Int16, c4:Int16, c5:Int32, c6:Int64, c7:Int16, c8:Int32, c9:UInt32, c10:UInt64, c11:Float32, c12:Float64, c13:Utf8View]"]
9 -> 10 [arrowhead=none, arrowtail=normal, dir=back]
}
}
// End DataFusion GraphViz Plan
"#
);
// Optimized logical plan
let msg = format!("Optimizing logical plan for '{sql}': {dataframe:?}");
let (state, plan) = dataframe.into_parts();
let plan = state.optimize(&plan).expect(&msg);
let optimized_logical_schema = plan.schema();
// Both schema has to be the same
assert_eq!(&logical_schema, optimized_logical_schema.as_ref());
//
// Verify schema
let formatted = plan.display_indent_schema().to_string();
let actual = formatted.trim();
assert_snapshot!(
actual,
@r"
Explain [plan_type:Utf8, plan:Utf8]
Projection: aggregate_test_100.c1 [c1:Utf8View]
Filter: aggregate_test_100.c2 > Int8(10) [c1:Utf8View, c2:Int8]
TableScan: aggregate_test_100 projection=[c1, c2], partial_filters=[aggregate_test_100.c2 > Int8(10)] [c1:Utf8View, c2:Int8]
"
);
//
// Verify the text format of the plan
let formatted = plan.display_indent().to_string();
let actual = formatted.trim();
assert_snapshot!(
actual,
@r"
Explain
Projection: aggregate_test_100.c1
Filter: aggregate_test_100.c2 > Int8(10)
TableScan: aggregate_test_100 projection=[c1, c2], partial_filters=[aggregate_test_100.c2 > Int8(10)]
"
);
//
// verify the grahviz format of the plan
let formatted = plan.display_graphviz().to_string();
let actual = formatted.trim();
assert_snapshot!(
actual,
@r#"
// Begin DataFusion GraphViz Plan,
// display it online here: https://dreampuf.github.io/GraphvizOnline
digraph {
subgraph cluster_1
{
graph[label="LogicalPlan"]
2[shape=box label="Explain"]
3[shape=box label="Projection: aggregate_test_100.c1"]
2 -> 3 [arrowhead=none, arrowtail=normal, dir=back]
4[shape=box label="Filter: aggregate_test_100.c2 > Int8(10)"]
3 -> 4 [arrowhead=none, arrowtail=normal, dir=back]
5[shape=box label="TableScan: aggregate_test_100 projection=[c1, c2], partial_filters=[aggregate_test_100.c2 > Int8(10)]"]
4 -> 5 [arrowhead=none, arrowtail=normal, dir=back]
}
subgraph cluster_6
{
graph[label="Detailed LogicalPlan"]
7[shape=box label="Explain\nSchema: [plan_type:Utf8, plan:Utf8]"]
8[shape=box label="Projection: aggregate_test_100.c1\nSchema: [c1:Utf8View]"]
7 -> 8 [arrowhead=none, arrowtail=normal, dir=back]
9[shape=box label="Filter: aggregate_test_100.c2 > Int8(10)\nSchema: [c1:Utf8View, c2:Int8]"]
8 -> 9 [arrowhead=none, arrowtail=normal, dir=back]
10[shape=box label="TableScan: aggregate_test_100 projection=[c1, c2], partial_filters=[aggregate_test_100.c2 > Int8(10)]\nSchema: [c1:Utf8View, c2:Int8]"]
9 -> 10 [arrowhead=none, arrowtail=normal, dir=back]
}
}
// End DataFusion GraphViz Plan
"#
);
// Physical plan
// Create plan
let msg = format!("Creating physical plan for '{sql}': {plan}");
let plan = state.create_physical_plan(&plan).await.expect(&msg);
//
// Execute plan
let msg = format!("Executing physical plan for '{sql}': {plan:?}");
let task_ctx = ctx.task_ctx();
let results = collect(plan, task_ctx).await.expect(&msg);
let actual = result_vec(&results);
// flatten to a single string
let actual = actual.into_iter().map(|r| r.join("\t")).collect::<String>();
// Since the plan contains path that are environmentally
// dependant(e.g. full path of the test file), only verify
// important content
assert_contains!(&actual, "logical_plan after optimize_projections");
assert_contains!(&actual, "physical_plan");
assert_contains!(&actual, "FilterExec: c2@1 > 10");
assert_contains!(actual, "ProjectionExec: expr=[c1@0 as c1]");
}
#[rstest]
#[tokio::test]
async fn explain_analyze_runs_optimizers(#[values("*", "1")] count_expr: &str) {
// repro for https://github.com/apache/datafusion/issues/917
// where EXPLAIN ANALYZE was not correctly running optimizer
let ctx = SessionContext::new_with_config(
SessionConfig::new().with_collect_statistics(true),
);
register_alltypes_parquet(&ctx).await;
// This happens as an optimization pass where count(*)/count(1) can be
// answered using statistics only.
let expected = "PlaceholderRowExec";
let sql = format!("EXPLAIN SELECT count({count_expr}) from alltypes_plain");
let actual = execute_to_batches(&ctx, &sql).await;
let actual = arrow::util::pretty::pretty_format_batches(&actual)
.unwrap()
.to_string();
assert_contains!(actual, expected);
// EXPLAIN ANALYZE should work the same
let sql = format!("EXPLAIN ANALYZE SELECT count({count_expr}) from alltypes_plain");
let actual = execute_to_batches(&ctx, &sql).await;
let actual = arrow::util::pretty::pretty_format_batches(&actual)
.unwrap()
.to_string();
assert_contains!(actual, expected);
}
#[tokio::test]
async fn test_physical_plan_display_indent() {
// Hard code target_partitions as it appears in the RepartitionExec output
let config = SessionConfig::new()
.with_target_partitions(9000)
.with_batch_size(4096);
let ctx = SessionContext::new_with_config(config);
register_aggregate_csv(&ctx).await.unwrap();
let sql = "SELECT c1, MAX(c12), MIN(c12) as the_min \
FROM aggregate_test_100 \
WHERE c12 < 10 \
GROUP BY c1 \
ORDER BY the_min DESC \
LIMIT 10";
let dataframe = ctx.sql(sql).await.unwrap();
let physical_plan = dataframe.create_physical_plan().await.unwrap();
let normalizer = ExplainNormalizer::new();
let actual = format!("{}", displayable(physical_plan.as_ref()).indent(true))
.trim()
.lines()
// normalize paths
.map(|s| normalizer.normalize(s))
.collect::<Vec<_>>()
.join("\n");
assert_snapshot!(
actual,
@r"
SortPreservingMergeExec: [the_min@2 DESC], fetch=10
SortExec: TopK(fetch=10), expr=[the_min@2 DESC], preserve_partitioning=[true]
ProjectionExec: expr=[c1@0 as c1, max(aggregate_test_100.c12)@1 as max(aggregate_test_100.c12), min(aggregate_test_100.c12)@2 as the_min]
AggregateExec: mode=FinalPartitioned, gby=[c1@0 as c1], aggr=[max(aggregate_test_100.c12), min(aggregate_test_100.c12)]
RepartitionExec: partitioning=Hash([c1@0], 9000), input_partitions=9000
AggregateExec: mode=Partial, gby=[c1@0 as c1], aggr=[max(aggregate_test_100.c12), min(aggregate_test_100.c12)]
FilterExec: c12@1 < 10
RepartitionExec: partitioning=RoundRobinBatch(9000), input_partitions=1
DataSourceExec: file_groups={1 group: [[ARROW_TEST_DATA/csv/aggregate_test_100.csv]]}, projection=[c1, c12], file_type=csv, has_header=true
"
);
}
#[tokio::test]
async fn test_physical_plan_display_indent_multi_children() {
// Hard code target_partitions as it appears in the RepartitionExec output
let config = SessionConfig::new()
.with_target_partitions(9000)
.with_batch_size(4096);
let ctx = SessionContext::new_with_config(config);
// ensure indenting works for nodes with multiple children
register_aggregate_csv(&ctx).await.unwrap();
let sql = "SELECT c1 \
FROM (select c1 from aggregate_test_100) AS a \
JOIN\
(select c1 as c2 from aggregate_test_100) AS b \
ON c1=c2\
";
let dataframe = ctx.sql(sql).await.unwrap();
let physical_plan = dataframe.create_physical_plan().await.unwrap();
let normalizer = ExplainNormalizer::new();
let actual = format!("{}", displayable(physical_plan.as_ref()).indent(true))
.trim()
.lines()
// normalize paths
.map(|s| normalizer.normalize(s))
.collect::<Vec<_>>()
.join("\n");
assert_snapshot!(
actual,
@r"
HashJoinExec: mode=Partitioned, join_type=Inner, on=[(c1@0, c2@0)], projection=[c1@0]
RepartitionExec: partitioning=Hash([c1@0], 9000), input_partitions=1
DataSourceExec: file_groups={1 group: [[ARROW_TEST_DATA/csv/aggregate_test_100.csv]]}, projection=[c1], file_type=csv, has_header=true
RepartitionExec: partitioning=Hash([c2@0], 9000), input_partitions=1
DataSourceExec: file_groups={1 group: [[ARROW_TEST_DATA/csv/aggregate_test_100.csv]]}, projection=[c1@0 as c2], file_type=csv, has_header=true
"
);
}
#[tokio::test]
#[cfg_attr(tarpaulin, ignore)]
async fn csv_explain_analyze() {
// This test uses the execute function to run an actual plan under EXPLAIN ANALYZE
let ctx = SessionContext::new();
register_aggregate_csv_by_sql(&ctx).await;
let sql = "EXPLAIN ANALYZE SELECT count(*), c1 FROM aggregate_test_100 group by c1";
let actual = execute_to_batches(&ctx, sql).await;
let formatted = arrow::util::pretty::pretty_format_batches(&actual)
.unwrap()
.to_string();
// Only test basic plumbing and try to avoid having to change too
// many things. explain_analyze_baseline_metrics covers the values
// in greater depth
let needle = "ProjectionExec: expr=[count(Int64(1))@1 as count(*), c1@0 as c1], metrics=[output_rows=5";
assert_contains!(&formatted, needle);
let verbose_needle = "Output Rows";
assert_not_contains!(formatted, verbose_needle);
}
#[tokio::test]
#[cfg_attr(tarpaulin, ignore)]
async fn csv_explain_analyze_order_by() {
let ctx = SessionContext::new();
register_aggregate_csv_by_sql(&ctx).await;
let sql = "EXPLAIN ANALYZE SELECT c1 FROM aggregate_test_100 order by c1";
let actual = execute_to_batches(&ctx, sql).await;
let formatted = arrow::util::pretty::pretty_format_batches(&actual)
.unwrap()
.to_string();
// Ensure that the ordering is not optimized away from the plan
// https://github.com/apache/datafusion/issues/6379
let needle = "SortExec: expr=[c1@0 ASC NULLS LAST], preserve_partitioning=[false], metrics=[output_rows=100, elapsed_compute";
assert_contains!(&formatted, needle);
}
#[tokio::test]
#[cfg_attr(tarpaulin, ignore)]
async fn parquet_explain_analyze() {
let ctx = SessionContext::new();
register_alltypes_parquet(&ctx).await;
let sql = "EXPLAIN ANALYZE select id, float_col, timestamp_col from alltypes_plain where timestamp_col > to_timestamp('2009-02-01T00:00:00'); ";
let actual = execute_to_batches(&ctx, sql).await;
let formatted = arrow::util::pretty::pretty_format_batches(&actual)
.unwrap()
.to_string();
// should contain aggregated stats. The scan now applies the predicate
// post-scan (the `FilterExec` above it is removed), so `output_rows` is
// the matching row count (5) rather than the decoded row count (8).
// The 3 rejected rows show up as `post_scan_rows_pruned=3`.
assert_contains!(&formatted, "output_rows=5");
assert_contains!(&formatted, "post_scan_rows_pruned=3");
assert_contains!(
&formatted,
"row_groups_pruned_bloom_filter=1 total \u{2192} 1 matched"
);
assert_contains!(
&formatted,
"row_groups_pruned_statistics=1 total \u{2192} 1 matched"
);
assert_contains!(&formatted, "output_rows_skew=0%");
assert_contains!(&formatted, "scan_efficiency_ratio=13.99%");
// The order of metrics is expected to be the same as the actual pruning order
// (file-> row-group -> page)
let i_file = formatted.find("files_ranges_pruned_statistics").unwrap();
let i_rowgroup_stat = formatted.find("row_groups_pruned_statistics").unwrap();
let i_rowgroup_bloomfilter =
formatted.find("row_groups_pruned_bloom_filter").unwrap();
let i_page_rows = formatted.find("page_index_rows_pruned").unwrap();
let i_page_pages = formatted.find("page_index_pages_pruned").unwrap();
assert!(
(i_file < i_rowgroup_stat)
&& (i_rowgroup_stat < i_rowgroup_bloomfilter)
&& (i_rowgroup_bloomfilter < i_page_pages && i_page_pages < i_page_rows),
"The parquet pruning metrics should be displayed in an order of: file range -> row group statistics -> row group bloom filter -> page index."
);
}
// This test reproduces the behavior described in
// https://github.com/apache/datafusion/issues/16684 where projection
// pushdown with recursive CTEs could fail to remove unused columns
// (e.g. nested/recursive expansion causing full schema to be scanned).
// Keeping this test ensures we don't regress that behavior.
#[tokio::test]
#[cfg_attr(tarpaulin, ignore)]
async fn parquet_recursive_projection_pushdown() -> Result<()> {
use parquet::arrow::arrow_writer::ArrowWriter;
use parquet::file::properties::WriterProperties;
let temp_dir = TempDir::new().unwrap();
let parquet_path = temp_dir.path().join("hierarchy.parquet");
let ids = Int64Array::from(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
let parent_ids = Int64Array::from(vec![0, 1, 1, 2, 2, 3, 4, 5, 6, 7]);
let values = Int64Array::from(vec![10, 20, 30, 40, 50, 60, 70, 80, 90, 100]);
let schema = Arc::new(Schema::new(vec![
Field::new("id", DataType::Int64, false),
Field::new("parent_id", DataType::Int64, true),
Field::new("value", DataType::Int64, false),
]));
let batch = RecordBatch::try_new(
schema.clone(),
vec![Arc::new(ids), Arc::new(parent_ids), Arc::new(values)],
)
.unwrap();
let file = File::create(&parquet_path).unwrap();
let props = WriterProperties::builder().build();
let mut writer = ArrowWriter::try_new(file, schema, Some(props)).unwrap();
writer.write(&batch).unwrap();
writer.close().unwrap();
let ctx = SessionContext::new();
ctx.register_parquet(
"hierarchy",
parquet_path.to_str().unwrap(),
ParquetReadOptions::default(),
)
.await?;
let sql = r#"
WITH RECURSIVE number_series AS (
SELECT id, 1 as level
FROM hierarchy
WHERE id = 1
UNION ALL
SELECT ns.id + 1, ns.level + 1
FROM number_series ns
WHERE ns.id < 10
)
SELECT * FROM number_series ORDER BY id
"#;
let dataframe = ctx.sql(sql).await?;
let physical_plan = dataframe.create_physical_plan().await?;
let normalizer = ExplainNormalizer::new();
let mut actual = format!("{}", displayable(physical_plan.as_ref()).indent(true))
.trim()
.lines()
.map(|line| normalizer.normalize(line))
.collect::<Vec<_>>()
.join("\n");
fn replace_path_variants(actual: &mut String, path: &str) {
let mut candidates = vec![path.to_string()];
let trimmed = path.trim_start_matches(std::path::MAIN_SEPARATOR);
if trimmed != path {
candidates.push(trimmed.to_string());
}
let forward_slash = path.replace('\\', "/");
if forward_slash != path {
candidates.push(forward_slash.clone());
let trimmed_forward = forward_slash.trim_start_matches('/');
if trimmed_forward != forward_slash {
candidates.push(trimmed_forward.to_string());
}
}