diff --git a/core/translate/group_by.rs b/core/translate/group_by.rs index 0f2e852a6..d47f116b1 100644 --- a/core/translate/group_by.rs +++ b/core/translate/group_by.rs @@ -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); } } diff --git a/testing/groupby.test b/testing/groupby.test index 1012ed658..0ed4c9078 100644 --- a/testing/groupby.test +++ b/testing/groupby.test @@ -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} \ No newline at end of file +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}