Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: fix expression rewriter wrong compare logic #8269

Merged
merged 7 commits into from
Nov 13, 2018
Merged

planner: fix expression rewriter wrong compare logic #8269

merged 7 commits into from
Nov 13, 2018

Conversation

imtbkcat
Copy link

What problem does this PR solve?

#8248 has fixed compare logic for constant expression, but there is still error in following query:

CREATE TABLE t(a int, b int, c int);
INSERT INTO t VALUES (1, 2, 3), (NULL, 2, 3  ), 
(1, NULL, 3),(1, 2,   NULL),(NULL, 2, 3+1), (1, NULL, 3+1), 
(1, 2+1, NULL),(NULL, 2, 3-1), (1, NULL, 3-1), (1, 2-1, NULL);

TiDB:

mysql> SELECT * FROM t1 WHERE (a,b,c) >= (1,2,3);
+------+------+------+
| a    | b    | c    |
+------+------+------+
|    1 |    2 |    3 |
| NULL |    2 |    3 |
|    1 | NULL |    3 |
| NULL |    2 |    4 |
|    1 | NULL |    4 |
|    1 |    3 | NULL |
+------+------+------+
6 rows in set (0.00 sec)

MySQL:

mysql> SELECT * FROM t1 WHERE (a,b,c) >= (1,2,3);
+------+------+------+
| a    | b    | c    |
+------+------+------+
|    1 |    2 |    3 |
|    1 |    3 | NULL |
+------+------+------+
2 rows in set (0.00 sec)

What is changed and how it works?

Patch before just fixed bug when expression is constant expression, but ignore expression has variables. New patch changed expression rewrite logic.

Check List

Tests

  • Unit test

Code changes

  • Has exported variable/fields change

Side effects

  • Possible performance regression

Related changes

  • Need to cherry-pick to the release branch

@imtbkcat imtbkcat added type/bugfix This PR fixes a bug. sig/planner SIG: Planner labels Nov 12, 2018
go.mod Outdated Show resolved Hide resolved
@@ -202,9 +202,10 @@ func (er *expressionRewriter) constructBinaryOpFunction(l expression.Expression,
return expression.ComposeCNFCondition(er.ctx, funcs...), nil
default:
larg0, rarg0 := expression.GetFuncArg(l, 0), expression.GetFuncArg(r, 0)
var expr1, expr2, expr3 expression.Expression
var expr1, expr2, expr3, expr4, expr5 expression.Expression
Copy link
Contributor

Choose a reason for hiding this comment

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

Update the comments of this function.

Copy link
Author

Choose a reason for hiding this comment

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

ok

Copy link
Author

Choose a reason for hiding this comment

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

addressed

// 2. Else constructBinaryOpFunctions converts (a0,a1,a2) op (b0,b1,b2) to
// `IF( (a0 NE b0) EQ 1, a0 op b0,
// IF ( (a0 NE b0) EQ Null, Null,
// IF ( (a0 NE b0) EQ 1, a1 op b1,
Copy link
Contributor

Choose a reason for hiding this comment

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

s/a0 NE b0/a1 NE b1 ?

Copy link
Author

Choose a reason for hiding this comment

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

ok

Copy link
Contributor

@eurekaka eurekaka left a comment

Choose a reason for hiding this comment

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

rest LGTM

@eurekaka eurekaka added the status/LGT1 Indicates that a PR has LGTM 1. label Nov 12, 2018
@imtbkcat
Copy link
Author

/run-all-tests

// a1 op b1,
// a2 op b2)
// )`
// IF ( (a0 NE b0) EQ Null, Null,
Copy link
Member

Choose a reason for hiding this comment

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

One tab here may be better?

Copy link
Author

Choose a reason for hiding this comment

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

ok

@imtbkcat
Copy link
Author

/run-all-tests

1 similar comment
@imtbkcat
Copy link
Author

/run-all-tests

@imtbkcat
Copy link
Author

PTAL @XuHuaiyu @winoros

// a1 op b1,
// a2 op b2)
// )`
// IF ( (a0 NE b0) EQ Null, Null,
Copy link
Contributor

Choose a reason for hiding this comment

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

s/ (a0 NE b0) EQ Null/ IsNull(a0 NE b0)?
The result of (a0 NE b0) EQ Null will always be null

// a2 op b2)
// )`
// IF ( (a0 NE b0) EQ Null, Null,
// IF ( a0 NE b0, a1 op b1,
Copy link
Contributor

Choose a reason for hiding this comment

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

s/ a0 NE b0/ a1 NE b1 ?

Copy link
Author

Choose a reason for hiding this comment

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

@XuHuaiyu addressed

@zz-jason
Copy link
Member

LGTM

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

LGTM

@XuHuaiyu
Copy link
Contributor

Please cherry-pick this to release-2.1 and release-2.0. @imtbkcat

@zz-jason zz-jason added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Nov 13, 2018
@imtbkcat imtbkcat merged commit a28a3a7 into pingcap:master Nov 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/planner SIG: Planner status/LGT2 Indicates that a PR has LGTM 2. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants