Skip to content
/ server Public

MDEV-37713/MDEV-37714 Fold boolean literals in SELECT-list#4754

Open
jaeheonshim wants to merge 1 commit intoMariaDB:mainfrom
jaeheonshim:MDEV-37713
Open

MDEV-37713/MDEV-37714 Fold boolean literals in SELECT-list#4754
jaeheonshim wants to merge 1 commit intoMariaDB:mainfrom
jaeheonshim:MDEV-37713

Conversation

@jaeheonshim
Copy link

@jaeheonshim jaeheonshim commented Mar 8, 2026

The order of evaluation of the expressions that appear in a SELECT-list is undefined. This change exploits this fact by recursively folding TRUE/FALSE literals in OR/AND expressions which may allow for skipping evaluation of some parts of the expression or even the whole expression.

Example

Setup

CREATE TABLE t1(c0 INT8);
CREATE TABLE t2(c0 INT8);
INSERT INTO t1 VALUES(1);
INSERT INTO t2 SELECT seq FROM seq_1_to_10000000;

Before

SELECT (SELECT MIN(c0) FROM t2)<0 OR true;
+------------------------------------+
| (SELECT MIN(c0) FROM t2)<0 OR true |
+------------------------------------+
|                                  1 |
+------------------------------------+
1 row in set (1.335 sec)


SELECT ((SELECT MIN(c0) FROM t2)<0 AND false) OR (SELECT MAX(c0) FROM t1)>0;
+----------------------------------------------------------------------+
| ((SELECT MIN(c0) FROM t2)<0 AND false) OR (SELECT MAX(c0) FROM t1)>0 |
+----------------------------------------------------------------------+
|                                                                    1 |
+----------------------------------------------------------------------+
1 row in set (1.413 sec)

After

SELECT (SELECT MIN(c0) FROM t2)<0 OR true;
+------------------------------------+
| (SELECT MIN(c0) FROM t2)<0 OR true |
+------------------------------------+
|                                  1 |
+------------------------------------+
1 row in set (0.000 sec)


SELECT ((SELECT MIN(c0) FROM t2)<0 AND false) OR (SELECT MAX(c0) FROM t1)>0;
+----------------------------------------------------------------------+
| ((SELECT MIN(c0) FROM t2)<0 AND false) OR (SELECT MAX(c0) FROM t1)>0 |
+----------------------------------------------------------------------+
|                                                                    1 |
+----------------------------------------------------------------------+
1 row in set (0.000 sec)

@jaeheonshim jaeheonshim force-pushed the MDEV-37713 branch 2 times, most recently from eba7fae to 8acdfc3 Compare March 8, 2026 07:51
The order of evaluation of the expressions that appear in a SELECT-list
is undefined. This change exploits this fact by recursively folding
TRUE/FALSE literals in OR/AND expressions which may allow for skipping
evaluation of some parts of the expression or even the whole expression.
@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Mar 9, 2026
Copy link
Member

@gkodinov gkodinov left a comment

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 also add a test case.

And, the jira mentions one optimization you're not really doing:

SELECT (a > 0 AND a < 0) FROM t1;
SELECT (a > 0 OR a <=0) FROM t1;

Would you consider doing this too? Or, if you wont, we'll need to open a sub-task Jira instead of using the current one.

li2.remove();
}
}
if (list.is_empty())
Copy link
Member

Choose a reason for hiding this comment

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

Can the list ever be empty, given that you're removing one of the branches only if another branch exists?

I'd say no. And then this is dead code. And should be turned into an assert.

If item is a SELECT-list COND_ITEM, rewrite it on the first time this
query is optimized to fold boolean expressions
*/
if (thd->lex->current_select->context_analysis_place == SELECT_LIST &&
Copy link
Member

Choose a reason for hiding this comment

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

Any specific reason why you're doing this only for the SELECT list expressions?
Why not do this during fix_fields for all Item_conds for example?

Copy link
Member

Choose a reason for hiding this comment

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

Forgot to mention one thing: When doing the optimization you are doing you need to check for NULL-ness!

SELECT true AND NULL FROM t1

should return NULL and not true.

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.

2 participants