Skip to content

Commit 2231183

Browse files
authored
feat: add optimizer config param to avoid grouping partitions prefer_existing_union (#10259)
* feat: add a config param to avoid converting union to interleave * chore: update config for the tests * chore: update configs.md
1 parent 3d90931 commit 2231183

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

datafusion/common/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,9 @@ config_namespace! {
571571
/// when an exact selectivity cannot be determined. Valid values are
572572
/// between 0 (no selectivity) and 100 (all rows are selected).
573573
pub default_filter_selectivity: u8, default = 20
574+
575+
/// When set to true, the optimizer will not attempt to convert Union to Interleave
576+
pub prefer_existing_union: bool, default = false
574577
}
575578
}
576579

datafusion/core/src/physical_optimizer/enforce_distribution.rs

Lines changed: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,11 @@ fn ensure_distribution(
11921192
.collect::<Result<Vec<_>>>()?;
11931193

11941194
let children_plans = children.iter().map(|c| c.plan.clone()).collect::<Vec<_>>();
1195-
plan = if plan.as_any().is::<UnionExec>() && can_interleave(children_plans.iter()) {
1195+
1196+
plan = if plan.as_any().is::<UnionExec>()
1197+
&& !config.optimizer.prefer_existing_union
1198+
&& can_interleave(children_plans.iter())
1199+
{
11961200
// Add a special case for [`UnionExec`] since we want to "bubble up"
11971201
// hash-partitioned data. So instead of
11981202
//
@@ -1721,23 +1725,33 @@ pub(crate) mod tests {
17211725
/// * `TARGET_PARTITIONS` (optional) - number of partitions to repartition to
17221726
/// * `REPARTITION_FILE_SCANS` (optional) - if true, will repartition file scans
17231727
/// * `REPARTITION_FILE_MIN_SIZE` (optional) - minimum file size to repartition
1728+
/// * `PREFER_EXISTING_UNION` (optional) - if true, will not attempt to convert Union to Interleave
17241729
macro_rules! assert_optimized {
17251730
($EXPECTED_LINES: expr, $PLAN: expr, $FIRST_ENFORCE_DIST: expr) => {
1726-
assert_optimized!($EXPECTED_LINES, $PLAN, $FIRST_ENFORCE_DIST, false, 10, false, 1024);
1731+
assert_optimized!($EXPECTED_LINES, $PLAN, $FIRST_ENFORCE_DIST, false, 10, false, 1024, false);
17271732
};
17281733

17291734
($EXPECTED_LINES: expr, $PLAN: expr, $FIRST_ENFORCE_DIST: expr, $PREFER_EXISTING_SORT: expr) => {
1730-
assert_optimized!($EXPECTED_LINES, $PLAN, $FIRST_ENFORCE_DIST, $PREFER_EXISTING_SORT, 10, false, 1024);
1735+
assert_optimized!($EXPECTED_LINES, $PLAN, $FIRST_ENFORCE_DIST, $PREFER_EXISTING_SORT, 10, false, 1024, false);
1736+
};
1737+
1738+
($EXPECTED_LINES: expr, $PLAN: expr, $FIRST_ENFORCE_DIST: expr, $PREFER_EXISTING_SORT: expr, $PREFER_EXISTING_UNION: expr) => {
1739+
assert_optimized!($EXPECTED_LINES, $PLAN, $FIRST_ENFORCE_DIST, $PREFER_EXISTING_SORT, 10, false, 1024, $PREFER_EXISTING_UNION);
17311740
};
17321741

17331742
($EXPECTED_LINES: expr, $PLAN: expr, $FIRST_ENFORCE_DIST: expr, $PREFER_EXISTING_SORT: expr, $TARGET_PARTITIONS: expr, $REPARTITION_FILE_SCANS: expr, $REPARTITION_FILE_MIN_SIZE: expr) => {
1743+
assert_optimized!($EXPECTED_LINES, $PLAN, $FIRST_ENFORCE_DIST, $PREFER_EXISTING_SORT, $TARGET_PARTITIONS, $REPARTITION_FILE_SCANS, $REPARTITION_FILE_MIN_SIZE, false);
1744+
};
1745+
1746+
($EXPECTED_LINES: expr, $PLAN: expr, $FIRST_ENFORCE_DIST: expr, $PREFER_EXISTING_SORT: expr, $TARGET_PARTITIONS: expr, $REPARTITION_FILE_SCANS: expr, $REPARTITION_FILE_MIN_SIZE: expr, $PREFER_EXISTING_UNION: expr) => {
17341747
let expected_lines: Vec<&str> = $EXPECTED_LINES.iter().map(|s| *s).collect();
17351748

17361749
let mut config = ConfigOptions::new();
17371750
config.execution.target_partitions = $TARGET_PARTITIONS;
17381751
config.optimizer.repartition_file_scans = $REPARTITION_FILE_SCANS;
17391752
config.optimizer.repartition_file_min_size = $REPARTITION_FILE_MIN_SIZE;
17401753
config.optimizer.prefer_existing_sort = $PREFER_EXISTING_SORT;
1754+
config.optimizer.prefer_existing_union = $PREFER_EXISTING_UNION;
17411755

17421756
// NOTE: These tests verify the joint `EnforceDistribution` + `EnforceSorting` cascade
17431757
// because they were written prior to the separation of `BasicEnforcement` into
@@ -3097,7 +3111,67 @@ pub(crate) mod tests {
30973111
"ParquetExec: file_groups={1 group: [[x]]}, projection=[a, b, c, d, e]",
30983112
];
30993113
assert_optimized!(expected, plan.clone(), true);
3100-
assert_optimized!(expected, plan, false);
3114+
assert_optimized!(expected, plan.clone(), false);
3115+
3116+
Ok(())
3117+
}
3118+
3119+
#[test]
3120+
fn union_not_to_interleave() -> Result<()> {
3121+
// group by (a as a1)
3122+
let left = aggregate_exec_with_alias(
3123+
parquet_exec(),
3124+
vec![("a".to_string(), "a1".to_string())],
3125+
);
3126+
// group by (a as a2)
3127+
let right = aggregate_exec_with_alias(
3128+
parquet_exec(),
3129+
vec![("a".to_string(), "a1".to_string())],
3130+
);
3131+
3132+
// Union
3133+
let plan = Arc::new(UnionExec::new(vec![left, right]));
3134+
3135+
// final agg
3136+
let plan =
3137+
aggregate_exec_with_alias(plan, vec![("a1".to_string(), "a2".to_string())]);
3138+
3139+
// Only two RepartitionExecs added, no final RepartitionExec required
3140+
let expected = &[
3141+
"AggregateExec: mode=FinalPartitioned, gby=[a2@0 as a2], aggr=[]",
3142+
"RepartitionExec: partitioning=Hash([a2@0], 10), input_partitions=20",
3143+
"AggregateExec: mode=Partial, gby=[a1@0 as a2], aggr=[]",
3144+
"UnionExec",
3145+
"AggregateExec: mode=FinalPartitioned, gby=[a1@0 as a1], aggr=[]",
3146+
"RepartitionExec: partitioning=Hash([a1@0], 10), input_partitions=10",
3147+
"AggregateExec: mode=Partial, gby=[a@0 as a1], aggr=[]",
3148+
"RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=1",
3149+
"ParquetExec: file_groups={1 group: [[x]]}, projection=[a, b, c, d, e]",
3150+
"AggregateExec: mode=FinalPartitioned, gby=[a1@0 as a1], aggr=[]",
3151+
"RepartitionExec: partitioning=Hash([a1@0], 10), input_partitions=10",
3152+
"AggregateExec: mode=Partial, gby=[a@0 as a1], aggr=[]",
3153+
"RepartitionExec: partitioning=RoundRobinBatch(10), input_partitions=1",
3154+
"ParquetExec: file_groups={1 group: [[x]]}, projection=[a, b, c, d, e]",
3155+
];
3156+
// no sort in the plan but since we need it as a parameter, make it default false
3157+
let prefer_existing_sort = false;
3158+
let first_enforce_distribution = true;
3159+
let prefer_existing_union = true;
3160+
3161+
assert_optimized!(
3162+
expected,
3163+
plan.clone(),
3164+
first_enforce_distribution,
3165+
prefer_existing_sort,
3166+
prefer_existing_union
3167+
);
3168+
assert_optimized!(
3169+
expected,
3170+
plan,
3171+
!first_enforce_distribution,
3172+
prefer_existing_sort,
3173+
prefer_existing_union
3174+
);
31013175

31023176
Ok(())
31033177
}
@@ -3651,7 +3725,8 @@ pub(crate) mod tests {
36513725
true,
36523726
target_partitions,
36533727
true,
3654-
repartition_size
3728+
repartition_size,
3729+
false
36553730
);
36563731

36573732
let expected = [
@@ -3668,7 +3743,8 @@ pub(crate) mod tests {
36683743
true,
36693744
target_partitions,
36703745
true,
3671-
repartition_size
3746+
repartition_size,
3747+
false
36723748
);
36733749

36743750
Ok(())
@@ -3731,7 +3807,7 @@ pub(crate) mod tests {
37313807
)),
37323808
vec![("a".to_string(), "a".to_string())],
37333809
);
3734-
assert_optimized!(expected, plan, true, false, 2, true, 10);
3810+
assert_optimized!(expected, plan, true, false, 2, true, 10, false);
37353811
}
37363812
Ok(())
37373813
}

datafusion/sqllogictest/test_files/information_schema.slt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ datafusion.optimizer.hash_join_single_partition_threshold 1048576
216216
datafusion.optimizer.hash_join_single_partition_threshold_rows 131072
217217
datafusion.optimizer.max_passes 3
218218
datafusion.optimizer.prefer_existing_sort false
219+
datafusion.optimizer.prefer_existing_union false
219220
datafusion.optimizer.prefer_hash_join true
220221
datafusion.optimizer.repartition_aggregations true
221222
datafusion.optimizer.repartition_file_min_size 10485760
@@ -294,6 +295,7 @@ datafusion.optimizer.hash_join_single_partition_threshold 1048576 The maximum es
294295
datafusion.optimizer.hash_join_single_partition_threshold_rows 131072 The maximum estimated size in rows for one input side of a HashJoin will be collected into a single partition
295296
datafusion.optimizer.max_passes 3 Number of times that the optimizer will attempt to optimize the plan
296297
datafusion.optimizer.prefer_existing_sort false When true, DataFusion will opportunistically remove sorts when the data is already sorted, (i.e. setting `preserve_order` to true on `RepartitionExec` and using `SortPreservingMergeExec`) When false, DataFusion will maximize plan parallelism using `RepartitionExec` even if this requires subsequently resorting data using a `SortExec`.
298+
datafusion.optimizer.prefer_existing_union false When set to true, the optimizer will not attempt to convert Union to Interleave
297299
datafusion.optimizer.prefer_hash_join true When set to true, the physical plan optimizer will prefer HashJoin over SortMergeJoin. HashJoin can work more efficiently than SortMergeJoin but consumes more memory
298300
datafusion.optimizer.repartition_aggregations true Should DataFusion repartition data using the aggregate keys to execute aggregates in parallel using the provided `target_partitions` level
299301
datafusion.optimizer.repartition_file_min_size 10485760 Minimum total files size in bytes to perform file scan repartitioning.

docs/source/user-guide/configs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Environment variables are read during `SessionConfig` initialisation so they mus
103103
| datafusion.optimizer.hash_join_single_partition_threshold | 1048576 | The maximum estimated size in bytes for one input side of a HashJoin will be collected into a single partition |
104104
| datafusion.optimizer.hash_join_single_partition_threshold_rows | 131072 | The maximum estimated size in rows for one input side of a HashJoin will be collected into a single partition |
105105
| datafusion.optimizer.default_filter_selectivity | 20 | The default filter selectivity used by Filter Statistics when an exact selectivity cannot be determined. Valid values are between 0 (no selectivity) and 100 (all rows are selected). |
106+
| datafusion.optimizer.prefer_existing_union | false | When set to true, the optimizer will not attempt to convert Union to Interleave |
106107
| datafusion.explain.logical_plan_only | false | When set to true, the explain statement will only print logical plans |
107108
| datafusion.explain.physical_plan_only | false | When set to true, the explain statement will only print physical plans |
108109
| datafusion.explain.show_statistics | false | When set to true, the explain statement will print operator statistics for physical plans |

0 commit comments

Comments
 (0)