forked from datafusion-contrib/datafusion-distributed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistributed_aggregation.rs
More file actions
152 lines (133 loc) · 8.42 KB
/
distributed_aggregation.rs
File metadata and controls
152 lines (133 loc) · 8.42 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
#[cfg(all(feature = "integration", test))]
mod tests {
use datafusion::arrow::util::pretty::pretty_format_batches;
use datafusion::physical_plan::{displayable, execute_stream};
use datafusion_distributed::test_utils::localhost::start_localhost_context;
use datafusion_distributed::test_utils::parquet::register_parquet_tables;
use datafusion_distributed::{
DefaultSessionBuilder, DistributedConfig, apply_network_boundaries, assert_snapshot,
display_plan_ascii, distribute_plan,
};
use futures::TryStreamExt;
use std::error::Error;
#[tokio::test]
async fn distributed_aggregation() -> Result<(), Box<dyn Error>> {
let (ctx, _guard) = start_localhost_context(3, DefaultSessionBuilder).await;
register_parquet_tables(&ctx).await?;
let df = ctx
.sql(r#"SELECT count(*), "RainToday" FROM weather GROUP BY "RainToday" ORDER BY count(*)"#)
.await?;
let physical = df.create_physical_plan().await?;
let physical_str = displayable(physical.as_ref()).indent(true).to_string();
let cfg = DistributedConfig::default().with_network_shuffle_tasks(2);
let physical_distributed = apply_network_boundaries(physical.clone(), &cfg)?;
let physical_distributed = distribute_plan(physical_distributed)?;
let physical_distributed_str = display_plan_ascii(physical_distributed.as_ref(), false);
assert_snapshot!(physical_str,
@r"
ProjectionExec: expr=[count(*)@0 as count(*), RainToday@1 as RainToday]
SortPreservingMergeExec: [count(Int64(1))@2 ASC NULLS LAST]
SortExec: expr=[count(*)@0 ASC NULLS LAST], preserve_partitioning=[true]
ProjectionExec: expr=[count(Int64(1))@1 as count(*), RainToday@0 as RainToday, count(Int64(1))@1 as count(Int64(1))]
AggregateExec: mode=FinalPartitioned, gby=[RainToday@0 as RainToday], aggr=[count(Int64(1))]
CoalesceBatchesExec: target_batch_size=8192
RepartitionExec: partitioning=Hash([RainToday@0], 3), input_partitions=3
AggregateExec: mode=Partial, gby=[RainToday@0 as RainToday], aggr=[count(Int64(1))]
DataSourceExec: file_groups={3 groups: [[/testdata/weather/result-000000.parquet], [/testdata/weather/result-000001.parquet], [/testdata/weather/result-000002.parquet]]}, projection=[RainToday], file_type=parquet
",
);
assert_snapshot!(physical_distributed_str,
@r"
┌───── DistributedExec ── Tasks: t0:[p0]
│ ProjectionExec: expr=[count(*)@0 as count(*), RainToday@1 as RainToday]
│ SortPreservingMergeExec: [count(Int64(1))@2 ASC NULLS LAST]
│ SortExec: expr=[count(*)@0 ASC NULLS LAST], preserve_partitioning=[true]
│ ProjectionExec: expr=[count(Int64(1))@1 as count(*), RainToday@0 as RainToday, count(Int64(1))@1 as count(Int64(1))]
│ AggregateExec: mode=FinalPartitioned, gby=[RainToday@0 as RainToday], aggr=[count(Int64(1))]
│ CoalesceBatchesExec: target_batch_size=8192
│ [Stage 1] => NetworkShuffleExec: output_partitions=3, input_tasks=2
└──────────────────────────────────────────────────
┌───── Stage 1 ── Tasks: t0:[p0..p2] t1:[p0..p2]
│ RepartitionExec: partitioning=Hash([RainToday@0], 3), input_partitions=2
│ AggregateExec: mode=Partial, gby=[RainToday@0 as RainToday], aggr=[count(Int64(1))]
│ PartitionIsolatorExec: t0:[p0,p1,__] t1:[__,__,p0]
│ DataSourceExec: file_groups={3 groups: [[/testdata/weather/result-000000.parquet], [/testdata/weather/result-000001.parquet], [/testdata/weather/result-000002.parquet]]}, projection=[RainToday], file_type=parquet
└──────────────────────────────────────────────────
",
);
let batches = pretty_format_batches(
&execute_stream(physical, ctx.task_ctx())?
.try_collect::<Vec<_>>()
.await?,
)?;
assert_snapshot!(batches, @r"
+----------+-----------+
| count(*) | RainToday |
+----------+-----------+
| 66 | Yes |
| 300 | No |
+----------+-----------+
");
let batches_distributed = pretty_format_batches(
&execute_stream(physical_distributed, ctx.task_ctx())?
.try_collect::<Vec<_>>()
.await?,
)?;
assert_snapshot!(batches_distributed, @r"
+----------+-----------+
| count(*) | RainToday |
+----------+-----------+
| 66 | Yes |
| 300 | No |
+----------+-----------+
");
Ok(())
}
#[tokio::test]
async fn distributed_aggregation_head_node_partitioned() -> Result<(), Box<dyn Error>> {
let (ctx, _guard) = start_localhost_context(6, DefaultSessionBuilder).await;
register_parquet_tables(&ctx).await?;
let df = ctx
.sql(r#"SELECT count(*), "RainToday" FROM weather GROUP BY "RainToday""#)
.await?;
let physical = df.create_physical_plan().await?;
let physical_str = displayable(physical.as_ref()).indent(true).to_string();
let cfg = DistributedConfig::default()
.with_network_shuffle_tasks(6)
.with_network_coalesce_tasks(6);
let physical_distributed = apply_network_boundaries(physical.clone(), &cfg)?;
let physical_distributed = distribute_plan(physical_distributed)?;
let physical_distributed_str = display_plan_ascii(physical_distributed.as_ref(), false);
assert_snapshot!(physical_str,
@r"
ProjectionExec: expr=[count(Int64(1))@1 as count(*), RainToday@0 as RainToday]
AggregateExec: mode=FinalPartitioned, gby=[RainToday@0 as RainToday], aggr=[count(Int64(1))]
CoalesceBatchesExec: target_batch_size=8192
RepartitionExec: partitioning=Hash([RainToday@0], 3), input_partitions=3
AggregateExec: mode=Partial, gby=[RainToday@0 as RainToday], aggr=[count(Int64(1))]
DataSourceExec: file_groups={3 groups: [[/testdata/weather/result-000000.parquet], [/testdata/weather/result-000001.parquet], [/testdata/weather/result-000002.parquet]]}, projection=[RainToday], file_type=parquet
",
);
assert_snapshot!(physical_distributed_str,
@r"
┌───── DistributedExec ── Tasks: t0:[p0]
│ CoalescePartitionsExec
│ [Stage 2] => NetworkCoalesceExec: output_partitions=18, input_tasks=6
└──────────────────────────────────────────────────
┌───── Stage 2 ── Tasks: t0:[p0..p2] t1:[p0..p2] t2:[p0..p2] t3:[p0..p2] t4:[p0..p2] t5:[p0..p2]
│ ProjectionExec: expr=[count(Int64(1))@1 as count(*), RainToday@0 as RainToday]
│ AggregateExec: mode=FinalPartitioned, gby=[RainToday@0 as RainToday], aggr=[count(Int64(1))]
│ CoalesceBatchesExec: target_batch_size=8192
│ [Stage 1] => NetworkShuffleExec: output_partitions=3, input_tasks=3
└──────────────────────────────────────────────────
┌───── Stage 1 ── Tasks: t0:[p0..p17] t1:[p0..p17] t2:[p0..p17]
│ RepartitionExec: partitioning=Hash([RainToday@0], 18), input_partitions=1
│ AggregateExec: mode=Partial, gby=[RainToday@0 as RainToday], aggr=[count(Int64(1))]
│ PartitionIsolatorExec: t0:[p0,__,__] t1:[__,p0,__] t2:[__,__,p0]
│ DataSourceExec: file_groups={3 groups: [[/testdata/weather/result-000000.parquet], [/testdata/weather/result-000001.parquet], [/testdata/weather/result-000002.parquet]]}, projection=[RainToday], file_type=parquet
└──────────────────────────────────────────────────
",
);
Ok(())
}
}