use super::*;
pub(super) fn plan_skips_sort(
select: &BoundSelect,
sources: &[PlannedSource],
aggregation: AggregationMode,
) -> bool {
aggregation == AggregationMode::None
&& sources
.first()
.is_some_and(|outer| every_term_is_constant(select, outer.id))
}
pub(super) fn every_term_is_constant(select: &BoundSelect, id: usize) -> bool {
!select.order_by.is_empty()
&& select.order_by.iter().all(|term| {
let mut expr = &term.expr;
while let BoundExpr::Collate { operand, .. } = expr {
expr = operand;
}
equated_to_outside_value(select, id, expr, term.collation)
})
}
pub(super) fn equated_to_outside_value(
select: &BoundSelect,
id: usize,
expr: &BoundExpr,
collation: Collation,
) -> bool {
let BoundExpr::Column { source, column, .. } = expr else {
return false;
};
if *source != id {
return false;
}
let Some(filter) = &select.filter else {
return false;
};
conjunction(filter).iter().any(|term| {
let BoundExpr::Compare {
op: BinaryOp::Equal,
left,
right,
collation: used,
..
} = term
else {
return false;
};
if *used != collation {
return false;
}
let other = match (left.as_ref(), right.as_ref()) {
(
BoundExpr::Column {
source,
column: held,
..
},
other,
)
| (
other,
BoundExpr::Column {
source,
column: held,
..
},
) if *source == id && held == column => other,
_ => return false,
};
let mut used = Vec::new();
other.sources_used(&mut used);
!used.contains(&id)
})
}