use crate::schema::expressions::Expression;
use crate::schema::{Predicate, Projection, QueryPlan};
const AGGREGATES: &[&str] = &["sum", "count", "avg", "min", "max"];
fn is_aggregate(name: &str) -> bool {
AGGREGATES.iter().any(|a| a.eq_ignore_ascii_case(name))
}
fn dedup_expr(expr: &mut Expression) -> bool {
match expr {
Expression::FunctionCall { name, args, .. } if is_aggregate(name) => {
let mut changed = false;
for arg in args.iter_mut() {
changed |= dedup_expr(arg);
}
if args.len() == 1 {
if let Expression::FunctionCall {
name: inner_name,
args: inner_args,
..
} = &args[0]
{
if inner_name.eq_ignore_ascii_case(name) {
if let Some(inner_arg) = inner_args.first() {
if let Some(unwrapped) = take_single_arg(inner_arg) {
*expr = unwrapped;
return true;
}
}
}
}
}
changed
}
Expression::FunctionCall { args, .. } => {
let mut changed = false;
for arg in args.iter_mut() {
changed |= dedup_expr(arg);
}
changed
}
Expression::BinaryOp { left, right, .. } => {
dedup_expr(left) | dedup_expr(right)
}
Expression::Case {
operand,
when_thens,
else_result,
} => {
let mut changed = false;
if let Some(op) = operand {
changed |= dedup_expr(op);
}
for wt in when_thens.iter_mut() {
changed |= dedup_expr(&mut wt.when);
changed |= dedup_expr(&mut wt.then);
}
if let Some(els) = else_result {
changed |= dedup_expr(els);
}
changed
}
Expression::SubQuery { query } => dedup_plan(query),
Expression::WindowFunction { args, .. } => {
let mut changed = false;
for arg in args.iter_mut() {
changed |= dedup_expr(arg);
}
changed
}
Expression::Literal { .. } | Expression::ColumnRef { .. } | Expression::Star => false,
}
}
fn dedup_pred(pred: &mut Predicate) -> bool {
match pred {
Predicate::Comparison { left, right, .. } => dedup_expr(left) | dedup_expr(right),
Predicate::And { left, right } | Predicate::Or { left, right } => {
dedup_pred(left) | dedup_pred(right)
}
Predicate::Not { child } => dedup_pred(child),
Predicate::Between { expr, low, high } => {
dedup_expr(expr) | dedup_expr(low) | dedup_expr(high)
}
Predicate::In { expr, target } => {
let mut changed = dedup_expr(expr);
if let crate::schema::InTarget::SubQuery(query) = target {
changed |= dedup_plan(query);
}
changed
}
Predicate::Like { expr, .. } => dedup_expr(expr),
Predicate::IsNull { expr } => dedup_expr(expr),
Predicate::Exists { query } => dedup_plan(query),
}
}
fn dedup_proj(proj: &mut Projection) -> bool {
match proj {
Projection::Expr { expression, .. } => dedup_expr(expression),
Projection::Column { .. } | Projection::Star { .. } => false,
}
}
fn dedup_plan(plan: &mut QueryPlan) -> bool {
let mut changed = false;
for proj in plan.select.iter_mut() {
changed |= dedup_proj(proj);
}
if let Some(ref mut pred) = plan.r#where {
changed |= dedup_pred(pred);
}
if let Some(ref mut having) = plan.having {
changed |= dedup_pred(having);
}
if let Some(ref mut group_by) = plan.group_by {
for expr in group_by.iter_mut() {
changed |= dedup_expr(expr);
}
}
if let Some(ref mut order_by) = plan.order_by {
for term in order_by.iter_mut() {
changed |= dedup_expr(&mut term.expr);
}
}
if let Some(ref mut ctes) = plan.ctes {
for cte in ctes.iter_mut() {
changed |= dedup_plan(&mut cte.query);
}
}
if let Some(ref mut set_op) = plan.set_operation {
changed |= dedup_plan(&mut set_op.right);
}
changed
}
fn take_single_arg(expr: &Expression) -> Option<Expression> {
Some(expr.clone())
}
pub fn deduplicate_nested_aggregates(plan: &mut QueryPlan) -> bool {
dedup_plan(plan)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dedup_sum_sum() {
let mut expr = Expression::FunctionCall {
name: "sum".to_owned(),
args: vec![Expression::FunctionCall {
name: "sum".to_owned(),
args: vec![Expression::ColumnRef {
table: Some("orders".to_owned()),
column: "total".to_owned(),
}],
distinct: false,
}],
distinct: false,
};
assert!(dedup_expr(&mut expr));
match expr {
Expression::ColumnRef {
ref table,
ref column,
} => {
assert_eq!(table.as_deref(), Some("orders"));
assert_eq!(column, "total");
}
_ => panic!("Expected ColumnRef after dedup"),
}
}
#[test]
fn dedup_count_count() {
let mut expr = Expression::FunctionCall {
name: "count".to_owned(),
args: vec![Expression::FunctionCall {
name: "count".to_owned(),
args: vec![Expression::Star],
distinct: false,
}],
distinct: false,
};
assert!(dedup_expr(&mut expr));
assert!(matches!(expr, Expression::Star));
}
#[test]
fn no_change_different_functions() {
let mut expr = Expression::FunctionCall {
name: "sum".to_owned(),
args: vec![Expression::FunctionCall {
name: "count".to_owned(),
args: vec![Expression::ColumnRef {
table: None,
column: "id".to_owned(),
}],
distinct: false,
}],
distinct: false,
};
assert!(!dedup_expr(&mut expr));
}
#[test]
fn no_change_single_aggregate() {
let mut expr = Expression::FunctionCall {
name: "sum".to_owned(),
args: vec![Expression::ColumnRef {
table: Some("orders".to_owned()),
column: "total".to_owned(),
}],
distinct: false,
};
assert!(!dedup_expr(&mut expr));
}
#[test]
fn dedup_in_having() {
let mut having = Predicate::Comparison {
left: Expression::FunctionCall {
name: "sum".to_owned(),
args: vec![Expression::FunctionCall {
name: "sum".to_owned(),
args: vec![Expression::ColumnRef {
table: Some("orders".to_owned()),
column: "total".to_owned(),
}],
distinct: false,
}],
distinct: false,
},
op: crate::schema::ComparisonOperator::Gt,
right: Expression::Literal {
value: serde_json::json!(150),
data_type: crate::schema::DataType::Float,
},
};
assert!(dedup_pred(&mut having));
if let Predicate::Comparison { ref left, .. } = having {
match left {
Expression::ColumnRef {
table: Some(t),
column: c,
} => {
assert_eq!(t, "orders");
assert_eq!(c, "total");
}
_ => panic!("Expected ColumnRef after dedup"),
}
}
}
}