Fix incorrect handling of OR clauses in HAVING

This commit is contained in:
Jussi Saurio
2025-06-10 18:01:21 +03:00
parent a4237dbe6e
commit 18dd87eff1
2 changed files with 19 additions and 2 deletions

View File

@@ -885,6 +885,7 @@ pub fn group_by_emit_row_phase<'a>(
if let Some(having) = &group_by.having {
for expr in having.iter() {
let if_true_target = program.allocate_label();
translate_condition_expr(
program,
&plan.table_references,
@@ -892,10 +893,11 @@ pub fn group_by_emit_row_phase<'a>(
ConditionMetadata {
jump_if_condition_is_true: false,
jump_target_when_false: labels.label_group_by_end_without_emitting_row,
jump_target_when_true: BranchOffset::Placeholder, // not used. FIXME: this is a bug. HAVING can have e.g. HAVING a OR b.
jump_target_when_true: if_true_target,
},
&t_ctx.resolver,
)?;
program.preassign_label_to_next_insn(if_true_target);
}
}

View File

@@ -206,4 +206,19 @@ do_execsql_test distinct_agg_functions {
limit 3;
} {Aaron|1769|33|53.6060606060606
Abigail|833|15|55.5333333333333
Adam|1517|30|50.5666666666667}
Adam|1517|30|50.5666666666667}
do_execsql_test_on_specific_db {:memory:} having_or {
CREATE TABLE users (first_name TEXT, age INTEGER);
INSERT INTO users VALUES
('Michael', 25), ('Michael', 50),
('David', 50),
('Sarah', 65);
select first_name, count(*) as cnt, avg(age) as avg_age
from users
group by first_name
having cnt = 2 or avg_age = 65
order by cnt desc
} {Michael|2|37.5
Sarah|1|65.0}