Merge pull request #100 from vivek378521/add-agg-fn-count

This commit is contained in:
Pekka Enberg
2024-07-08 11:53:30 +03:00
committed by GitHub
4 changed files with 38 additions and 1 deletions

View File

@@ -454,7 +454,23 @@ fn translate_aggregation(
});
target_register
}
AggFunc::Count => todo!(),
AggFunc::Count => {
let expr_reg = if args.is_empty() {
program.alloc_register()
} else {
let expr = &args[0];
let expr_reg = program.alloc_register();
let _ = translate_expr(program, cursor_id, table, expr, expr_reg);
expr_reg
};
program.emit_insn(Insn::AggStep {
acc_reg: target_register,
col: expr_reg,
func: AggFunc::Count,
});
target_register
}
AggFunc::GroupConcat => todo!(),
AggFunc::Max => todo!(),
AggFunc::Min => todo!(),

View File

@@ -46,6 +46,7 @@ impl Display for OwnedValue {
OwnedValue::Agg(a) => match a.as_ref() {
AggContext::Avg(acc, _count) => write!(f, "{}", acc),
AggContext::Sum(acc) => write!(f, "{}", acc),
AggContext::Count(count) => write!(f, "{}", count),
},
OwnedValue::Record(r) => write!(f, "{:?}", r),
}
@@ -56,6 +57,7 @@ impl Display for OwnedValue {
pub enum AggContext {
Avg(OwnedValue, OwnedValue), // acc and count
Sum(OwnedValue),
Count(OwnedValue),
}
impl std::ops::Add<OwnedValue> for OwnedValue {
@@ -160,6 +162,7 @@ pub fn to_value(value: &OwnedValue) -> Value<'_> {
OwnedValue::Agg(a) => match a.as_ref() {
AggContext::Avg(acc, _count) => to_value(acc), // we assume aggfinal was called
AggContext::Sum(acc) => to_value(acc),
AggContext::Count(count) => to_value(count),
},
OwnedValue::Record(_) => todo!(),
}

View File

@@ -430,6 +430,9 @@ impl Program {
AggFunc::Sum => {
OwnedValue::Agg(Box::new(AggContext::Sum(OwnedValue::Float(0.0))))
}
AggFunc::Count => {
OwnedValue::Agg(Box::new(AggContext::Count(OwnedValue::Integer(0))))
}
_ => {
todo!();
}
@@ -459,6 +462,16 @@ impl Program {
};
*acc += col;
}
AggFunc::Count => {
let OwnedValue::Agg(agg) = state.registers[*acc_reg].borrow_mut()
else {
unreachable!();
};
let AggContext::Count(count) = agg.borrow_mut() else {
unreachable!();
};
*count += 1;
}
_ => {
todo!();
}
@@ -478,6 +491,7 @@ impl Program {
*acc /= count.clone();
}
AggFunc::Sum => {}
AggFunc::Count => {}
_ => {
todo!();
}

View File

@@ -43,6 +43,10 @@ do_execsql_test select-limit {
SELECT id FROM users LIMIT 1;
} {1}
do_execsql_test select-count {
SELECT count(id) FROM users;
} {10000}
do_execsql_test select-limit-0 {
SELECT id FROM users LIMIT 0;
} {}