use crate::schema::expressions::Expression;
use crate::schema::{BinaryOperator, Predicate, Projection, QueryPlan};
fn parse_arithmetic_column(col: &str) -> Option<(String, BinaryOperator, String)> {
let col = col.trim();
let patterns: &[(&str, BinaryOperator)] = &[
(" * ", BinaryOperator::Mul),
(" / ", BinaryOperator::Div),
(" + ", BinaryOperator::Add),
(" - ", BinaryOperator::Sub),
];
for (sep, op) in patterns {
if let Some(pos) = col.find(sep) {
let left = col[..pos].trim().to_owned();
let right = col[pos + sep.len()..].trim().to_owned();
if !left.is_empty() && !right.is_empty() {
return Some((left, op.clone(), right));
}
}
}
None
}
fn parse_sql_function_column(col: &str) -> Option<(String, Vec<String>)> {
let col = col.trim();
let open = col.find('(')?;
let close = col.rfind(')')?;
if open >= close {
return None;
}
let name = col[..open].trim().to_owned();
if name.is_empty() || !name.chars().all(|c| c.is_ascii_uppercase() || c == '_') {
return None;
}
let args_str = &col[open + 1..close];
let mut args: Vec<String> = Vec::new();
for part in args_str.split(',') {
let part = part.trim();
if !part.is_empty() {
if let Some(from_pos) = part.to_uppercase().find(" FROM ") {
let keyword = part[..from_pos].trim().to_lowercase();
let column = part[from_pos + 6..].trim().to_owned();
if !keyword.is_empty() && !column.is_empty() {
args.push(keyword);
args.push(column);
continue;
}
}
args.push(part.to_owned());
}
}
if args.is_empty() {
return None;
}
Some((name, args))
}
fn col_ref(table: Option<String>, column: &str) -> Expression {
Expression::ColumnRef {
table,
column: column.to_owned(),
}
}
fn arg_to_expr(table: Option<&str>, arg: &str) -> Expression {
let arg = arg.trim();
if (arg.starts_with('\'') && arg.ends_with('\''))
|| (arg.starts_with('"') && arg.ends_with('"'))
{
let inner = &arg[1..arg.len() - 1];
return Expression::Literal {
value: serde_json::Value::String(inner.to_owned()),
data_type: crate::schema::DataType::String,
};
}
if arg.chars().all(|c| c.is_ascii_uppercase() || c == '_')
|| SQL_KEYWORDS.contains(&arg.to_lowercase().as_str())
{
return Expression::Literal {
value: serde_json::Value::String(arg.to_lowercase()),
data_type: crate::schema::DataType::String,
};
}
if let Ok(n) = arg.parse::<i64>() {
return Expression::Literal {
value: serde_json::json!(n),
data_type: crate::schema::DataType::Int,
};
}
Expression::ColumnRef {
table: table.map(|t| t.to_owned()),
column: arg.to_owned(),
}
}
const SQL_KEYWORDS: &[&str] = &[
"month", "year", "day", "hour", "minute", "second",
"quarter", "week", "dow", "doy", "epoch", "decade",
"century", "millennium", "millisecond", "microsecond",
"yyyy", "mm", "dd", "hh", "mi", "ss",
];
fn fix_expr(expr: &mut Expression) -> bool {
match expr {
Expression::ColumnRef {
table,
column,
} if !column.is_empty() && parse_arithmetic_column(column).is_some() => {
let (left, op, right) = parse_arithmetic_column(column).unwrap();
let new_expr = Expression::BinaryOp {
left: Box::new(col_ref(table.clone(), &left)),
op,
right: Box::new(col_ref(table.clone(), &right)),
};
*expr = new_expr;
true
}
Expression::ColumnRef {
table,
column,
} if !column.is_empty() && parse_sql_function_column(column).is_some() => {
let table_str = table.as_deref();
let (name, args) = parse_sql_function_column(column).unwrap();
let expr_args: Vec<Expression> = args
.iter()
.map(|a| arg_to_expr(table_str, a))
.collect();
let new_expr = Expression::FunctionCall {
name: name.to_lowercase(),
args: expr_args,
distinct: false,
};
*expr = new_expr;
true
}
Expression::ColumnRef { .. } => false,
Expression::FunctionCall { args, .. } => {
let mut changed = false;
for arg in args.iter_mut() {
changed |= fix_expr(arg);
}
changed
}
Expression::BinaryOp { left, right, .. } => {
fix_expr(left) | fix_expr(right)
}
Expression::Case {
operand,
when_thens,
else_result,
} => {
let mut changed = false;
if let Some(op) = operand {
changed |= fix_expr(op);
}
for wt in when_thens.iter_mut() {
changed |= fix_expr(&mut wt.when);
changed |= fix_expr(&mut wt.then);
}
if let Some(els) = else_result {
changed |= fix_expr(els);
}
changed
}
Expression::SubQuery { query } => fix_plan(query),
Expression::WindowFunction { args, .. } => {
let mut changed = false;
for arg in args.iter_mut() {
changed |= fix_expr(arg);
}
changed
}
Expression::Literal { .. } | Expression::Star => false,
}
}
fn fix_pred(pred: &mut Predicate) -> bool {
match pred {
Predicate::Comparison { left, right, .. } => fix_expr(left) | fix_expr(right),
Predicate::And { left, right } | Predicate::Or { left, right } => {
fix_pred(left) | fix_pred(right)
}
Predicate::Not { child } => fix_pred(child),
Predicate::Between { expr, low, high } => {
fix_expr(expr) | fix_expr(low) | fix_expr(high)
}
Predicate::In { expr, target } => {
let mut changed = fix_expr(expr);
if let crate::schema::InTarget::SubQuery(query) = target {
changed |= fix_plan(query);
}
changed
}
Predicate::Like { expr, .. } => fix_expr(expr),
Predicate::IsNull { expr } => fix_expr(expr),
Predicate::Exists { query } => fix_plan(query),
}
}
fn fix_proj(proj: &mut Projection) -> bool {
match proj {
Projection::Expr { expression, .. } => fix_expr(expression),
Projection::Column {
table,
column,
alias,
} => {
if column.is_empty() {
return false;
}
if let Some((left, op, right)) = parse_arithmetic_column(column) {
let new_expr = Expression::BinaryOp {
left: Box::new(col_ref(table.clone(), &left)),
op,
right: Box::new(col_ref(table.clone(), &right)),
};
let old_alias = alias.take();
*proj = Projection::Expr {
expression: new_expr,
alias: old_alias,
};
return true;
}
if let Some((name, args)) = parse_sql_function_column(column) {
let table_str = table.as_deref();
let expr_args: Vec<Expression> = args
.iter()
.map(|a| arg_to_expr(table_str, a))
.collect();
let new_expr = Expression::FunctionCall {
name: name.to_lowercase(),
args: expr_args,
distinct: false,
};
let old_alias = alias.take();
*proj = Projection::Expr {
expression: new_expr,
alias: old_alias,
};
return true;
}
false
}
Projection::Star { .. } => false,
}
}
fn fix_plan(plan: &mut QueryPlan) -> bool {
let mut changed = false;
for proj in plan.select.iter_mut() {
changed |= fix_proj(proj);
}
if let Some(ref mut pred) = plan.r#where {
changed |= fix_pred(pred);
}
if let Some(ref mut having) = plan.having {
changed |= fix_pred(having);
}
if let Some(ref mut group_by) = plan.group_by {
for expr in group_by.iter_mut() {
changed |= fix_expr(expr);
}
}
if let Some(ref mut order_by) = plan.order_by {
for term in order_by.iter_mut() {
changed |= fix_expr(&mut term.expr);
}
}
if let Some(ref mut joins) = plan.joins {
for join in joins.iter_mut() {
changed |= fix_pred(&mut join.on);
}
}
if let Some(ref mut ctes) = plan.ctes {
for cte in ctes.iter_mut() {
changed |= fix_plan(&mut cte.query);
}
}
if let Some(ref mut set_op) = plan.set_operation {
changed |= fix_plan(&mut set_op.right);
}
changed
}
pub fn fix_arithmetic_column_refs(plan: &mut QueryPlan) -> bool {
fix_plan(plan)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_mul() {
let (left, op, right) = parse_arithmetic_column("unit_price * quantity").unwrap();
assert_eq!(left, "unit_price");
assert_eq!(op, BinaryOperator::Mul);
assert_eq!(right, "quantity");
}
#[test]
fn parses_div() {
let (left, op, right) = parse_arithmetic_column("total / count").unwrap();
assert_eq!(left, "total");
assert_eq!(op, BinaryOperator::Div);
assert_eq!(right, "count");
}
#[test]
fn parses_add() {
let (left, op, right) = parse_arithmetic_column("price + tax").unwrap();
assert_eq!(left, "price");
assert_eq!(op, BinaryOperator::Add);
assert_eq!(right, "tax");
}
#[test]
fn parses_sub() {
let (left, op, right) = parse_arithmetic_column("total - discount").unwrap();
assert_eq!(left, "total");
assert_eq!(op, BinaryOperator::Sub);
assert_eq!(right, "discount");
}
#[test]
fn no_match_without_spaces() {
assert!(parse_arithmetic_column("unit_price*quantity").is_none());
}
#[test]
fn no_match_single_word() {
assert!(parse_arithmetic_column("unit_price").is_none());
}
#[test]
fn no_match_empty() {
assert!(parse_arithmetic_column("").is_none());
}
#[test]
fn fixes_column_ref_in_select() {
let mut plan = QueryPlan {
select: vec![
Projection::Column {
table: Some("products".to_owned()),
column: "name".to_owned(),
alias: Some("product".to_owned()),
},
Projection::Expr {
expression: Expression::FunctionCall {
name: "sum".to_owned(),
args: vec![Expression::ColumnRef {
table: Some("order_items".to_owned()),
column: "unit_price * quantity".to_owned(),
}],
distinct: false,
},
alias: Some("total_sold".to_owned()),
},
],
distinct: false,
distinct_on: None,
from: crate::schema::FromClause {
table: "products".to_owned(),
alias: None,
},
r#where: None,
group_by: Some(vec![Expression::ColumnRef {
table: Some("products".to_owned()),
column: "name".to_owned(),
}]),
having: None,
order_by: None,
limit: None,
offset: None,
joins: None,
ctes: None,
set_operation: None,
};
assert!(fix_arithmetic_column_refs(&mut plan));
if let Projection::Expr { expression, .. } = &plan.select[1] {
if let Expression::FunctionCall { args, .. } = expression {
assert_eq!(args.len(), 1);
match &args[0] {
Expression::BinaryOp { left, op, right } => {
assert_eq!(*op, BinaryOperator::Mul);
if let Expression::ColumnRef {
table: Some(t),
column: c,
} = left.as_ref()
{
assert_eq!(t, "order_items");
assert_eq!(c, "unit_price");
} else {
panic!("left should be ColumnRef");
}
if let Expression::ColumnRef {
table: Some(t),
column: c,
} = right.as_ref()
{
assert_eq!(t, "order_items");
assert_eq!(c, "quantity");
} else {
panic!("right should be ColumnRef");
}
}
_ => panic!("expected BinaryOp"),
}
} else {
panic!("expected FunctionCall");
}
} else {
panic!("expected Expr projection");
}
}
#[test]
fn no_change_when_column_name_valid() {
let mut plan = QueryPlan {
select: vec![Projection::Column {
table: Some("products".to_owned()),
column: "name".to_owned(),
alias: None,
}],
distinct: false,
distinct_on: None,
from: crate::schema::FromClause {
table: "products".to_owned(),
alias: None,
},
r#where: None,
group_by: None,
having: None,
order_by: None,
limit: None,
offset: None,
joins: None,
ctes: None,
set_operation: None,
};
assert!(!fix_arithmetic_column_refs(&mut plan));
}
}