Ensure GROUP BY clauses with multi same-aggregate functions work correctly

Closes #1731
This commit is contained in:
Tobie Morgan Hitchcock 2023-03-26 10:04:18 +01:00
parent fb2356b17a
commit cad596cdf3
3 changed files with 106 additions and 5 deletions

View file

@ -241,10 +241,7 @@ impl Iterator {
if let Field::Alias(v, i) = field {
match v {
Value::Function(f) if f.is_aggregate() => {
let x = vals
.all()
.get(ctx, opt, txn, v.to_idiom().as_ref())
.await?;
let x = vals.all().get(ctx, opt, txn, i).await?;
let x = f.aggregate(x).compute(ctx, opt, txn, None).await?;
obj.set(ctx, opt, txn, i, x).await?;
}

View file

@ -151,7 +151,7 @@ impl Fields {
};
// Check if this is a single VALUE field expression
match self.single().is_some() {
false => out.set(ctx, opt, txn, v.to_idiom().as_ref(), x).await?,
false => out.set(ctx, opt, txn, i, x).await?,
true => out = x,
}
}

View file

@ -233,3 +233,107 @@ async fn select_limit_fetch() -> Result<(), Error> {
//
Ok(())
}
#[tokio::test]
async fn select_multi_aggregate() -> Result<(), Error> {
let sql = "
CREATE test:1 SET group = 1, one = 1.7, two = 2.4;
CREATE test:2 SET group = 1, one = 4.7, two = 3.9;
CREATE test:3 SET group = 2, one = 3.2, two = 9.7;
CREATE test:4 SET group = 2, one = 4.4, two = 3.0;
SELECT group, math::sum(one) AS one, math::sum(two) AS two FROM test GROUP BY group;
SELECT group, math::sum(two) AS two, math::sum(one) AS one FROM test GROUP BY group;
";
let dbs = Datastore::new("memory").await?;
let ses = Session::for_kv().with_ns("test").with_db("test");
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
assert_eq!(res.len(), 6);
//
let tmp = res.remove(0).result?;
let val = Value::parse(
"[
{
id: test:1,
group: 1,
one: 1.7,
two: 2.4,
}
]",
);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse(
"[
{
id: test:2,
group: 1,
one: 4.7,
two: 3.9,
}
]",
);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse(
"[
{
id: test:3,
group: 2,
one: 3.2,
two: 9.7,
}
]",
);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse(
"[
{
id: test:4,
group: 2,
one: 4.4,
two: 3.0,
}
]",
);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse(
"[
{
group: 1,
one: 6.4,
two: 6.3,
},
{
group: 2,
one: 7.6,
two: 12.7,
}
]",
);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse(
"[
{
group: 1,
one: 6.4,
two: 6.3,
},
{
group: 2,
one: 7.6,
two: 12.7,
}
]",
);
assert_eq!(tmp, val);
//
Ok(())
}