Skip to content

Commit 65aacf2

Browse files
committed
Make input_sorted_by_group_key use input_sortedness_by_group_key
1 parent e4653e0 commit 65aacf2

File tree

1 file changed

+10
-59
lines changed

1 file changed

+10
-59
lines changed

datafusion/src/physical_plan/planner.rs

Lines changed: 10 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,67 +1683,18 @@ fn input_sorted_by_group_key(
16831683
group_key: &[(Arc<dyn PhysicalExpr>, String)],
16841684
sort_order: &mut Vec<usize>,
16851685
) -> bool {
1686-
assert!(!group_key.is_empty());
1686+
let sortedness = input_sortedness_by_group_key(input, group_key);
16871687
sort_order.clear();
1688-
1689-
let hints = input.output_hints();
1690-
// We check the group key is a prefix of the sort key.
1691-
let sort_key = hints.sort_order;
1692-
if sort_key.is_none() {
1693-
return false;
1694-
}
1695-
let sort_key = sort_key.unwrap();
1696-
// Tracks which elements of sort key are used in the group key or have a single value.
1697-
let mut sort_key_hit = vec![false; sort_key.len()];
1698-
let mut sort_to_group = vec![usize::MAX; sort_key.len()];
1699-
for (group_i, (g, _)) in group_key.iter().enumerate() {
1700-
let col = g.as_any().downcast_ref::<Column>();
1701-
if col.is_none() {
1702-
return false;
1703-
}
1704-
let input_col = input.schema().index_of(col.unwrap().name());
1705-
if input_col.is_err() {
1706-
return false;
1707-
}
1708-
let input_col = input_col.unwrap();
1709-
let sort_key_pos = match sort_key.iter().find_position(|i| **i == input_col) {
1710-
None => return false,
1711-
Some((p, _)) => p,
1712-
};
1713-
sort_key_hit[sort_key_pos] = true;
1714-
if sort_to_group[sort_key_pos] != usize::MAX {
1715-
return false; // Bail out to simplify code a bit. This should not happen in practice.
1716-
}
1717-
sort_to_group[sort_key_pos] = group_i;
1718-
}
1719-
1720-
// At this point all elements of the group key mapped into some column of the sort key. This
1721-
// checks the group key is mapped into a prefix of the sort key, except that it's okay if it
1722-
// skips over single value columns.
1723-
let mut pref_len: usize = 0;
1724-
for (i, hit) in sort_key_hit.iter().enumerate() {
1725-
if !hit && !hints.single_value_columns.contains(&sort_key[i]) {
1726-
break;
1727-
}
1728-
pref_len += 1;
1729-
}
1730-
1731-
if sort_key_hit[pref_len..].iter().any(|present| *present) {
1732-
// The group key did not hit a contiguous prefix of the sort key (ignoring single value
1733-
// columns); return false.
1734-
return false;
1688+
if sortedness.is_sorted_by_group_key() {
1689+
sort_order.extend(
1690+
sortedness.sort_order[0]
1691+
.iter()
1692+
.map(|&(_sort_key, group_key)| group_key),
1693+
);
1694+
true
1695+
} else {
1696+
false
17351697
}
1736-
1737-
assert!(sort_order.is_empty()); // Cleared at the beginning of the function.
1738-
1739-
// Note that single-value columns might not have a mapping to the grouping key.
1740-
sort_order.extend(
1741-
sort_to_group
1742-
.iter()
1743-
.take(pref_len)
1744-
.filter(|i| **i != usize::MAX),
1745-
);
1746-
true
17471698
}
17481699

17491700
#[derive(Debug, Clone)]

0 commit comments

Comments
 (0)