Skip to content

MDEV-40245 Match equivalent NULLIF expressions during view analysis - #5774

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

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

Conversation

@DerZc

@DerZc DerZc commented Sep 28, 2026 •

Copy link
Copy Markdown

During view analysis, NULLIF can have two arguments while its fixed form has a third argument for the return value. Generic function comparison rejects these equivalent expressions because their argument counts differ. A view using DISTINCT and ORDER BY can consequently lose distinct rows.

Recognize the two- and three-argument NULLIF forms as equal when both original operands match and the extra argument repeats the first operand.

Regression coverage

Add a seven-line regression to main.view: create one table with two rows, create a view using DISTINCT, ORDER BY and division by NULLIF(1,0), query it once, and clean up. Both distinct values must survive. No extra SET, database creation, manual prepared statements or duplicate queries are needed.

Validation

On current 11.4 at f49e838d367f2154b3edb43effa99ff9eb0d8602:

  • The unmodified server fails main.view only on the new query, omitting (NULL, 4.0000).
  • The patched Release build succeeds.
  • Normal protocol: main.view, main.func_group, main.group_by, main.select and main.func_if pass.
  • Prepared-statement protocol: main.view passes.
  • View protocol: the same seven-line regression passes as a temporary standalone MTR test. Full main.view is skipped in this mode because its existing count_sessions.inc requires the view protocol to be disabled.
  • Expected rows are independently derived from NULLIF(1,0) = 1 and exact decimal division of the distinct input values.
  • The full database regression suite was not run.

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

@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.

Please simplify the test case.

Comment thread mysql-test/main/view.result Outdated
Comment thread mysql-test/main/view.result Outdated
NULLIF has two arguments during view analysis and gains a third
return-value argument when fixed. The generic function comparison
rejects these equivalent forms because their argument counts differ.
A view using DISTINCT and ORDER BY can consequently lose distinct rows.

Recognize the two- and three-argument forms as equal when both original
operands match and the extra argument repeats the first operand.

Add a view using DISTINCT, ORDER BY and division by NULLIF(1,0).
Check that querying it preserves both distinct input values.

Bug report: https://jira.mariadb.org/browse/MDEV-40245
@DerZc DerZc changed the title MDEV-40245 Wrong result with DISTINCT and ORDER BY in VIEW using NULLIF expression MDEV-40245 Match equivalent NULLIF expressions during view analysis 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.

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

@gkodinov gkodinov assigned spetrunia and unassigned gkodinov Sep 29, 2026
@gkodinov
gkodinov requested a review from spetrunia September 29, 2026 07:58
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.

Development

Successfully merging this pull request may close these issues.

3 participants