forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.rs
More file actions
159 lines (143 loc) · 5.95 KB
/
utils.rs
File metadata and controls
159 lines (143 loc) · 5.95 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
// 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 std::sync::Arc;
use datafusion_common::Result;
use datafusion_physical_expr::{Distribution, LexOrdering, LexRequirement};
use datafusion_physical_plan::coalesce_partitions::CoalescePartitionsExec;
use datafusion_physical_plan::limit::{GlobalLimitExec, LocalLimitExec};
use datafusion_physical_plan::repartition::RepartitionExec;
use datafusion_physical_plan::sorts::sort::SortExec;
use datafusion_physical_plan::sorts::sort_preserving_merge::SortPreservingMergeExec;
use datafusion_physical_plan::tree_node::PlanContext;
use datafusion_physical_plan::union::UnionExec;
use datafusion_physical_plan::windows::{BoundedWindowAggExec, WindowAggExec};
use datafusion_physical_plan::{ExecutionPlan, ExecutionPlanProperties};
/// This utility function adds a `SortExec` above an operator according to the
/// given ordering requirements while preserving the original partitioning.
///
/// Note that this updates the plan in both the `PlanContext.children` and
/// the `PlanContext.plan`'s children. Therefore its not required to sync
/// the child plans with [`PlanContext::update_plan_from_children`].
pub fn add_sort_above<T: Clone + Default>(
node: PlanContext<T>,
sort_requirements: LexRequirement,
fetch: Option<usize>,
) -> PlanContext<T> {
add_sort_above_impl(node, sort_requirements, fetch, false)
}
/// This utility function adds a `SortExec` above an operator according to the
/// given ordering requirements. If the parent distribution requires a single
/// input partition, it adds a `SortPreservingMergeExec` above the
/// partition-preserving sort.
pub fn add_sort_above_with_distribution<T: Clone + Default>(
node: PlanContext<T>,
sort_requirements: LexRequirement,
fetch: Option<usize>,
required_distribution: &Distribution,
) -> PlanContext<T> {
add_sort_above_impl(
node,
sort_requirements,
fetch,
matches!(required_distribution, Distribution::SinglePartition),
)
}
fn add_sort_above_impl<T: Clone + Default>(
node: PlanContext<T>,
sort_requirements: LexRequirement,
fetch: Option<usize>,
requires_single_partition: bool,
) -> PlanContext<T> {
let mut sort_reqs: Vec<_> = sort_requirements.into();
sort_reqs.retain(|sort_expr| {
node.plan
.equivalence_properties()
.is_expr_constant(&sort_expr.expr)
.is_none()
});
let sort_exprs = sort_reqs.into_iter().map(Into::into).collect::<Vec<_>>();
let Some(ordering) = LexOrdering::new(sort_exprs) else {
return node;
};
let input_has_multiple_partitions =
node.plan.output_partitioning().partition_count() > 1;
let mut new_sort =
SortExec::new(ordering.clone(), Arc::clone(&node.plan)).with_fetch(fetch);
if input_has_multiple_partitions {
new_sort = new_sort.with_preserve_partitioning(true);
}
let sort_node = PlanContext::new(Arc::new(new_sort), T::default(), vec![node]);
if !(requires_single_partition && input_has_multiple_partitions) {
return sort_node;
}
PlanContext::new(
Arc::new(
SortPreservingMergeExec::new(ordering, Arc::clone(&sort_node.plan))
.with_fetch(fetch),
),
T::default(),
vec![sort_node],
)
}
/// This utility function adds a `SortExec` above an operator according to the
/// given ordering requirements while preserving the original partitioning. If
/// requirement is already satisfied no `SortExec` is added.
pub fn add_sort_above_with_check<T: Clone + Default>(
node: PlanContext<T>,
sort_requirements: LexRequirement,
fetch: Option<usize>,
) -> Result<PlanContext<T>> {
if !node
.plan
.equivalence_properties()
.ordering_satisfy_requirement(sort_requirements.clone())?
{
Ok(add_sort_above(node, sort_requirements, fetch))
} else {
Ok(node)
}
}
/// Checks whether the given operator is a [`SortExec`].
pub fn is_sort(plan: &Arc<dyn ExecutionPlan>) -> bool {
plan.as_any().is::<SortExec>()
}
/// Checks whether the given operator is a window;
/// i.e. either a [`WindowAggExec`] or a [`BoundedWindowAggExec`].
pub fn is_window(plan: &Arc<dyn ExecutionPlan>) -> bool {
plan.as_any().is::<WindowAggExec>() || plan.as_any().is::<BoundedWindowAggExec>()
}
/// Checks whether the given operator is a [`UnionExec`].
pub fn is_union(plan: &Arc<dyn ExecutionPlan>) -> bool {
plan.as_any().is::<UnionExec>()
}
/// Checks whether the given operator is a [`SortPreservingMergeExec`].
pub fn is_sort_preserving_merge(plan: &Arc<dyn ExecutionPlan>) -> bool {
plan.as_any().is::<SortPreservingMergeExec>()
}
/// Checks whether the given operator is a [`CoalescePartitionsExec`].
pub fn is_coalesce_partitions(plan: &Arc<dyn ExecutionPlan>) -> bool {
plan.as_any().is::<CoalescePartitionsExec>()
}
/// Checks whether the given operator is a [`RepartitionExec`].
pub fn is_repartition(plan: &Arc<dyn ExecutionPlan>) -> bool {
plan.as_any().is::<RepartitionExec>()
}
/// Checks whether the given operator is a limit;
/// i.e. either a [`LocalLimitExec`] or a [`GlobalLimitExec`].
pub fn is_limit(plan: &Arc<dyn ExecutionPlan>) -> bool {
plan.as_any().is::<GlobalLimitExec>() || plan.as_any().is::<LocalLimitExec>()
}