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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
//! Input distribution requirements for physical execution plans.
use std::sync::Arc;
use datafusion_common::{Result, internal_err};
use datafusion_physical_expr::{Distribution, Partitioning, PartitioningSatisfaction};
use crate::execution_plan::{ExecutionPlan, ExecutionPlanProperties, InvariantLevel};
/// Distribution requirements for an [`ExecutionPlan`]'s inputs.
///
/// [`InputDistributionRequirements`] describes what distribution an operator
/// requires from each child.
///
/// - [`Self::new`] describes independent per-child requirements.
/// - [`Self::co_partitioned`] additionally requires child partitions with the
/// same index to cover compatible key ranges.
///
/// For a single-input aggregate:
///
/// ```text
/// AggregateExec
/// child 0 requirement: KeyPartitioned(group_exprs)
/// ```
///
/// each input partition can aggregate its own key domain independently.
///
/// For a partitioned join:
///
/// ```text
/// HashJoinExec
/// child 0 requirement: KeyPartitioned(left_keys)
/// child 1 requirement: KeyPartitioned(right_keys)
///
/// partition 0: join(left partition 0, right partition 0)
/// partition 1: join(left partition 1, right partition 1)
/// partition 2: join(left partition 2, right partition 2)
/// ```
///
/// each child must satisfy its own key requirement. In addition, matching
/// partition indexes must be safe to process together.
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct InputDistributionRequirements {
/// Per-child distribution requirements, indexed by child position.
children: Vec<ChildDistributionRequirement>,
/// Child indexes that must also have compatible partition layouts.
co_partitioned: Option<Vec<usize>>,
}
/// Options for checking child distribution satisfaction.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ChildSatisfactionOptions {
allow_subset: bool,
}
impl ChildSatisfactionOptions {
/// Create default satisfaction options.
pub fn new() -> Self {
Self::default()
}
/// Allow a child partitioning whose key expressions are a subset of the
/// required key expressions to satisfy the requirement.
pub fn with_allow_subset(mut self, allow_subset: bool) -> Self {
self.allow_subset = allow_subset;
self
}
/// Whether subset satisfaction is enabled.
pub fn allow_subset(&self) -> bool {
self.allow_subset
}
}
impl InputDistributionRequirements {
/// Create independent per-child requirements.
pub fn new(per_child: Vec<Distribution>) -> Self {
let children = per_child
.into_iter()
.map(|distribution| ChildDistributionRequirement { distribution })
.collect();
Self {
children,
co_partitioned: None,
}
}
/// Create a requirement that all children are co-partitioned.
///
/// Each child must satisfy its own [`Distribution`]. Matching partition
/// indexes are processed together:
///
/// ```text
/// left: Range(left.a ASC, split_points=[10, 20])
/// right: Range(right.x ASC, split_points=[10, 20])
///
/// partition 0 from both sides contains keys before 10
/// partition 1 from both sides contains keys in [10, 20)
/// partition 2 from both sides contains keys at/after 20
/// ```
///
/// If the split points differ, partition `i` from one side no longer covers
/// the same key range as partition `i` from the other side.
pub fn co_partitioned(per_child: Vec<Distribution>) -> Self {
debug_assert!(
per_child.len() >= 2,
"co-partitioned distribution requirements need at least two children"
);
let co_partitioned = (0..per_child.len()).collect();
let mut result = Self::new(per_child);
result.co_partitioned = Some(co_partitioned);
result
}
/// Return the per-child distribution requirements.
pub fn per_child_distributions(
&self,
) -> impl ExactSizeIterator<Item = &Distribution> + '_ {
self.children.iter().map(|child| &child.distribution)
}
/// Whether these requirements include a co-partitioning relationship between children.
pub fn is_co_partitioned(&self) -> bool {
self.co_partitioned.is_some()
}
/// Return the distribution requirement for a child.
pub fn child_distribution(&self, child_idx: usize) -> Option<&Distribution> {
self.children
.get(child_idx)
.map(|child| &child.distribution)
}
/// Return the per-child distribution requirements.
///
/// WARNING: This intentionally drops any grouped relationship.
pub fn into_per_child(self) -> Vec<Distribution> {
self.children
.into_iter()
.map(|child| child.distribution)
.collect()
}
/// Returns how a child satisfies its distribution requirement.
///
/// This preserves the requirement set's satisfaction policy.
pub fn child_satisfaction(
&self,
child_idx: usize,
child: &dyn ExecutionPlan,
options: ChildSatisfactionOptions,
) -> Result<PartitioningSatisfaction> {
let Some(requirement) = self.children.get(child_idx) else {
return internal_err!(
"missing distribution requirement for child {child_idx}"
);
};
Ok(child.output_partitioning().satisfaction(
&requirement.distribution,
child.equivalence_properties(),
options.allow_subset(),
))
}
/// Return child indexes whose co-partitioning requirements are
/// unsatisfied by the provided candidate children.
///
/// Independent per-child requirements are intentionally ignored here, use
/// [`Self::child_satisfaction`] for those checks. An empty result means all
/// co-partitioning requirements are satisfied.
///
/// # Public Only for Internal Use:
///
/// This is not a public API and is for internal use only; see [API policy] for details.
///
/// [API policy]: https://datafusion.apache.org/contributor-guide/api-health.html#internal-public-apis
#[doc(hidden)]
pub fn unsatisfied_co_partitioned_children(
&self,
plan_name: &str,
children: &[&dyn ExecutionPlan],
) -> Result<Vec<usize>> {
self.validate_shape(plan_name, children.len())?;
let Some(co_partitioned) = &self.co_partitioned else {
return Ok(vec![]);
};
if self.co_partitioning_satisfied(co_partitioned, children) {
return Ok(vec![]);
}
Ok(co_partitioned.clone())
}
/// Validate the requirements against a plan's children.
#[inline]
pub(crate) fn check_invariants(
&self,
plan_name: &str,
children: Vec<&Arc<dyn ExecutionPlan>>,
check: InvariantLevel,
) -> Result<()> {
let children_len = children.len();
if self.children.len() != children_len {
return self.validate_shape(plan_name, children_len);
}
if let Some(co_partitioned) = &self.co_partitioned {
self.validate_shape(plan_name, children_len)?;
if matches!(check, InvariantLevel::Executable) {
return self.check_co_partitioning_invariant(
plan_name,
co_partitioned,
children,
);
}
}
Ok(())
}
fn check_co_partitioning_invariant(
&self,
plan_name: &str,
co_partitioned: &[usize],
children: Vec<&Arc<dyn ExecutionPlan>>,
) -> Result<()> {
let children = children
.into_iter()
.map(|child| child.as_ref())
.collect::<Vec<_>>();
if !self.co_partitioning_satisfied(co_partitioned, &children) {
return internal_err!(
"{} requires children {:?} to be co-partitioned",
plan_name,
co_partitioned
);
}
Ok(())
}
fn validate_shape(&self, plan_name: &str, children_len: usize) -> Result<()> {
if self.children.len() != children_len {
return internal_err!(
"{plan_name}::input_distribution_requirements returned incorrect child count: {} != {}",
self.children.len(),
children_len
);
}
if let Some(co_partitioned) = &self.co_partitioned {
if co_partitioned.len() < 2 {
return internal_err!(
"{plan_name} has invalid co-partitioning requirement: at least two children are required"
);
}
let mut seen = vec![false; self.children.len()];
for &child in co_partitioned {
validate_child_index(plan_name, child, self.children.len(), &mut seen)?;
if matches!(
self.children[child].distribution,
Distribution::UnspecifiedDistribution
) {
return internal_err!(
"{plan_name} has invalid co-partitioning requirement: child {child} has unspecified distribution"
);
}
}
}
Ok(())
}
fn co_partitioning_satisfied(
&self,
co_partitioned: &[usize],
children: &[&dyn ExecutionPlan],
) -> bool {
let first_idx = co_partitioned[0];
let first_requirement = &self.children[first_idx];
let first = children[first_idx];
let first_partitioning = first.output_partitioning();
if !first_partitioning
.satisfaction(
&first_requirement.distribution,
first.equivalence_properties(),
false,
)
.is_satisfied()
{
return false;
}
for &child_idx in co_partitioned.iter().skip(1) {
let requirement = &self.children[child_idx];
let child = children[child_idx];
if !child
.output_partitioning()
.satisfaction(
&requirement.distribution,
child.equivalence_properties(),
false,
)
.is_satisfied()
|| !compatible_co_partitioning_layout(
first_partitioning,
child.output_partitioning(),
)
{
return false;
}
}
true
}
}
/// A distribution requirement for a single child.
#[derive(Debug, Clone)]
struct ChildDistributionRequirement {
distribution: Distribution,
}
fn validate_child_index(
plan_name: &str,
child_idx: usize,
child_count: usize,
seen: &mut [bool],
) -> Result<()> {
if child_idx >= child_count {
return internal_err!(
"{plan_name} has invalid distribution requirement: child index {child_idx} out of bounds"
);
}
if seen[child_idx] {
return internal_err!(
"{plan_name} has invalid distribution requirement: child {child_idx} appears more than once"
);
}
seen[child_idx] = true;
Ok(())
}
fn compatible_co_partitioning_layout(
first_partitioning: &Partitioning,
other_partitioning: &Partitioning,
) -> bool {
if first_partitioning.partition_count() == 1
&& other_partitioning.partition_count() == 1
{
return true;
}
if first_partitioning.partition_count() != other_partitioning.partition_count() {
return false;
}
match (first_partitioning, other_partitioning) {
(Partitioning::Hash(_, _), Partitioning::Hash(_, _)) => true,
(Partitioning::Range(left), Partitioning::Range(right)) => {
left.split_points() == right.split_points()
&& left.ordering().len() == right.ordering().len()
&& left
.ordering()
.iter()
.zip(right.ordering())
.all(|(left, right)| left.options == right.options)
}
_ => false,
}
}