Skip to content

MDEV-40779 Evaluate HAVING on completed groups before DISTINCT - #5776

Open
DerZc wants to merge 1 commit into
MariaDB:11.4from
DerZc:fix-mdev-40779
Open

DerZc wants to merge 1 commit into
MariaDB:11.4from
DerZc:fix-mdev-40779

Conversation

@DerZc

@DerZc DerZc commented Sep 28, 2026 •

Copy link
Copy Markdown

HAVING can read stale aggregate and field values when grouped rows are
copied into a second temporary table for DISTINCT. Rechecking the condition
during duplicate removal can discard qualifying rows.

Bind HAVING to the stored values of the completed groups. Evaluate it while
writing the DISTINCT table, and avoid attaching or evaluating it again
against that table.

Bug report: https://jira.mariadb.org/browse/MDEV-40779

Regression coverage

Reduce the addition to main.func_group from 67 lines to five: an issue
header, table creation, three input rows, one query and cleanup. Two groups
satisfy HAVING and produce the same output; a third group fails HAVING. The
expected DISTINCT result is one row, (2, 2).

Remove the extra InnoDB include, session settings, separate database,
sorting directives, equivalent query variants and manual prepared statements.
The original source fix and existing test contents are preserved.

Validation

On current 11.4 at f49e838d367f2154b3edb43effa99ff9eb0d8602:

  • The unmodified server builds, then fails main.func_group only because the
    new query returns no rows instead of (2, 2).
  • The patched Release build succeeds.
  • Normal MTR passes: main.func_group, main.group_by, main.having,
    main.select, main.distinct and main.view.
  • main.func_group passes with the view protocol.
  • The exact five-line regression passes as a temporary standalone MTR test
    with the prepared-statement protocol, including its automatic repeated
    SELECT execution. The existing full main.func_group test disables that
    protocol at its start.
  • The full server regression suite was not run.
@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Sep 28, 2026
@gkodinov gkodinov self-assigned this Sep 28, 2026

@gkodinov gkodinov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! This is a preliminary review.

Very close, but I'd like to see a clearer test case before I approve it.

Comment thread mysql-test/main/func_group.test Outdated
HAVING can read stale aggregate and field values when grouped rows are
copied into a second temporary table for DISTINCT. Rechecking the same
condition during duplicate removal can then discard qualifying rows.

Bind HAVING references to the stored values of the completed groups.
Evaluate the condition while writing the DISTINCT table, and avoid
attaching or evaluating it again against that table.

Add one grouped query with two qualifying groups that produce the same
output and one rejected group. Check that DISTINCT returns the single
qualifying row. Use the default test environment without extra session
settings, a separate database or equivalent query variants.

Bug report: https://jira.mariadb.org/browse/MDEV-40779
@DerZc DerZc changed the title MDEV-40779 DISTINCT + char_length(SPACE(AVG(…))) + HAVING returns empty for one insertion order of the same rows Sep 29, 2026

@gkodinov gkodinov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks. Please stand by for the final review.

@gkodinov
gkodinov requested a review from spetrunia September 29, 2026 10:04
@gkodinov gkodinov assigned spetrunia and unassigned gkodinov Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

3 participants