add min aggregate function

add changes for all.test
This commit is contained in:
mazchew
2024-07-09 03:10:22 +08:00
parent 7bc83e5a0f
commit fc0c3d539a
4 changed files with 62 additions and 2 deletions

View File

@@ -486,7 +486,20 @@ fn translate_aggregation(
});
target_register
}
AggFunc::Min => todo!(),
AggFunc::Min => {
if args.len() != 1 {
anyhow::bail!("Parse error: min bad number of arguments");
}
let expr = &args[0];
let expr_reg = program.alloc_register();
let _ = translate_expr(program, cursor_id, table, expr, expr_reg);
program.emit_insn(Insn::AggStep {
acc_reg: target_register,
col: expr_reg,
func: AggFunc::Min,
});
target_register
}
AggFunc::StringAgg => todo!(),
AggFunc::Sum => {
if args.len() != 1 {

View File

@@ -48,6 +48,7 @@ impl Display for OwnedValue {
AggContext::Sum(acc) => write!(f, "{}", acc),
AggContext::Count(count) => write!(f, "{}", count),
AggContext::Max(max) => write!(f, "{}", max),
AggContext::Min(min) => write!(f, "{}", min),
},
OwnedValue::Record(r) => write!(f, "{:?}", r),
}
@@ -60,6 +61,7 @@ pub enum AggContext {
Sum(OwnedValue),
Count(OwnedValue),
Max(OwnedValue),
Min(OwnedValue),
}
impl std::ops::Add<OwnedValue> for OwnedValue {
@@ -166,6 +168,7 @@ pub fn to_value(value: &OwnedValue) -> Value<'_> {
AggContext::Sum(acc) => to_value(acc),
AggContext::Count(count) => to_value(count),
AggContext::Max(max) => to_value(max),
AggContext::Min(min) => to_value(min),
},
OwnedValue::Record(_) => todo!(),
}

View File

@@ -447,6 +447,20 @@ impl Program {
}
}
}
AggFunc::Min => {
let col = state.registers[*col].clone();
match col {
OwnedValue::Integer(_) => {
OwnedValue::Agg(Box::new(AggContext::Min(OwnedValue::Integer(i64::MAX))))
}
OwnedValue::Float(_) => {
OwnedValue::Agg(Box::new(AggContext::Min(OwnedValue::Float(f64::INFINITY))))
}
_ => {
unreachable!();
}
}
}
_ => {
todo!();
}
@@ -511,6 +525,31 @@ impl Program {
}
}
}
AggFunc::Min => {
let col = state.registers[*col].clone();
let OwnedValue::Agg(agg) = state.registers[*acc_reg].borrow_mut() else {
unreachable!();
};
let AggContext::Min(acc) = agg.borrow_mut() else {
unreachable!();
};
match (acc, col) {
(OwnedValue::Integer(ref mut current_min), OwnedValue::Integer(value)) => {
if value < *current_min {
*current_min = value;
}
}
(OwnedValue::Float(ref mut current_min), OwnedValue::Float(value)) => {
if value < *current_min {
*current_min = value;
}
}
_ => {
eprintln!("Unexpected types in min aggregation");
}
}
}
_ => {
todo!();
}
@@ -531,7 +570,8 @@ impl Program {
}
AggFunc::Sum => {}
AggFunc::Count => {}
AggFunc::Max => {}
AggFunc::Max => {}
AggFunc::Min => {}
_ => {
todo!();
}

View File

@@ -51,6 +51,10 @@ do_execsql_test select-max {
SELECT max(age) FROM users;
} {100}
do_execsql_test select-min {
SELECT min(age) FROM users;
} {1}
do_execsql_test select-limit-0 {
SELECT id FROM users LIMIT 0;
} {}