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, expression: fix simplify outer join with cast #12701

Merged
merged 4 commits into from
Oct 15, 2019
Merged

planner, expression: fix simplify outer join with cast #12701

merged 4 commits into from
Oct 15, 2019

Conversation

justarandomstring
Copy link
Contributor

@justarandomstring justarandomstring commented Oct 14, 2019

What problem does this PR solve?

Before this PR, left outer join was incorrectly simplified into inner join:

mysql> explain select * from t t1 left join t t2 on t1.a = t2.a where cast(t1.c_str as char) = '2.3';
+--------------------------+----------+-----------+------------------------------------------------------------+
| id                       | count    | task      | operator info                                              |
+--------------------------+----------+-----------+------------------------------------------------------------+
| MergeJoin_8              | 10000.00 | root      | inner join, left key:Column#1, right key:Column#11         |
| ├─Selection_20           | 8000.00  | root      | eq(cast(Column#6), "2.3")                                  |
| │ └─TableReader_22       | 10000.00 | root      | data:TableScan_21                                          |
| │   └─TableScan_21       | 10000.00 | cop[tikv] | table:t1, range:[-inf,+inf], keep order:true, stats:pseudo |
| └─TableReader_24         | 10000.00 | root      | data:TableScan_23                                          |
|   └─TableScan_23         | 10000.00 | cop[tikv] | table:t2, range:[-inf,+inf], keep order:true, stats:pseudo |
+--------------------------+----------+-----------+------------------------------------------------------------+
6 rows in set, 1 warning (0.00 sec)

After this PR:

mysql> explain select * from t t1 left join t t2 on t1.a = t2.a where cast(t1.c_str as char) = '2.3';
+--------------------------+----------+-----------+------------------------------------------------------------+
| id                       | count    | task      | operator info                                              |
+--------------------------+----------+-----------+------------------------------------------------------------+
| MergeJoin_8              | 10000.00 | root      | left outer join, left key:Column#1, right key:Column#11    |
| ├─Selection_19           | 8000.00  | root      | eq(cast(Column#6), "2.3")                                  |
| │ └─TableReader_21       | 10000.00 | root      | data:TableScan_20                                          |
| │   └─TableScan_20       | 10000.00 | cop[tikv] | table:t1, range:[-inf,+inf], keep order:true, stats:pseudo |
| └─TableReader_23         | 10000.00 | root      | data:TableScan_22                                          |
|   └─TableScan_22         | 10000.00 | cop[tikv] | table:t2, range:[-inf,+inf], keep order:true, stats:pseudo |
+--------------------------+----------+-----------+------------------------------------------------------------+
6 rows in set (0.00 sec)

What is changed and how it works?

Handle ScalarFunction Cast in a different logic in EvaluateExprWithNull.

Before this PR, the following code is part of how EvaluateExprWithNull handles ScalarFunction.

NewFunctionInternal(ctx, x.FuncName.L, types.NewFieldType(mysql.TypeTiny), args...)

This line will set the default RetType of ScarlarFunction into mysql.TypeTiny. For those ScalarFunctions other than Cast, RetType can be determined correctly as it will be recalcuated in NewFunctionImpl. However, when handling Cast, newFunctionImpl simply pass RetType to BuildCastFunction without any other processing, which may lead a wrong RetType.

In the example, the Cast is CastStringToIntSig before this PR instead of CastStringToStringSig, which it supposed to be.

// newFunctionImpl creates a new scalar function or constant.
func newFunctionImpl(ctx sessionctx.Context, fold bool, funcName string, retType *types.FieldType, args ...Expression) (Expression, error) {
	if retType == nil {
		return nil, errors.Errorf("RetType cannot be nil for ScalarFunction.")
	}
	if funcName == ast.Cast {
		return BuildCastFunction(ctx, args[0], retType), nil
	}
	fc, ok := funcs[funcName]

Check List

Tests

  • Unit test

Code changes

  • Has exported function/method change

Side effects

  • Change some execute plan

Related changes

  • Need to cherry-pick to the release branch

Release note

  • fix bug that simplify outer join get wrong result with cast in where clause.

@sre-bot sre-bot added the contribution This PR is from a community contributor. label Oct 14, 2019
@CLAassistant
Copy link

CLAassistant commented Oct 14, 2019

CLA assistant check
All committers have signed the CLA.

@foreyes
Copy link
Contributor

foreyes commented Oct 14, 2019

/run-unit-test

@justarandomstring justarandomstring requested a review from a team as a code owner October 14, 2019 17:55
@ghost ghost requested review from qw4990 and SunRunAway and removed request for a team October 14, 2019 17:55
@foreyes
Copy link
Contributor

foreyes commented Oct 14, 2019

/run-all-tests

@codecov
Copy link

codecov bot commented Oct 14, 2019

Codecov Report

Merging #12701 into master will decrease coverage by 0.0409%.
The diff coverage is 100%.

@@               Coverage Diff                @@
##             master     #12701        +/-   ##
================================================
- Coverage   79.9122%   79.8713%   -0.0409%     
================================================
  Files           462        462                
  Lines        105308     104786       -522     
================================================
- Hits          84154      83694       -460     
  Misses        14906      14906                
+ Partials       6248       6186        -62

@zz-jason zz-jason added the sig/planner SIG: Planner label Oct 15, 2019
@qw4990 qw4990 removed their request for review October 15, 2019 02:37
@foreyes foreyes changed the title [WIP] Fix simplify outer join with cast planner, expression: fix simplify outer join with cast Oct 15, 2019
@foreyes foreyes requested a review from XuHuaiyu October 15, 2019 05:49
@@ -542,6 +542,9 @@ func EvaluateExprWithNull(ctx sessionctx.Context, schema *Schema, expr Expressio
for i, arg := range x.GetArgs() {
args[i] = EvaluateExprWithNull(ctx, schema, arg)
}
if x.FuncName.L == ast.Cast {
return NewFunctionInternal(ctx, x.FuncName.L, x.RetType, args...)
}
return NewFunctionInternal(ctx, x.FuncName.L, types.NewFieldType(mysql.TypeTiny), args...)
Copy link
Member

Choose a reason for hiding this comment

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

Did you try to only change this line to NewFunctionInternal(ctx, x.FuncName.L, x.RetType, args...)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've tried, and it can pass all the tests :)
However, according to this line, it is possible that RetType cannot be overwritten by builtinRetTp. I don't know the exact reason why using mysql.TypeTiny as the default type, but in order to reduce the potential risks, I kept the original logic, that is, use mysql.TypeTiny as the default type.

Copy link
Member

@winoros winoros Oct 15, 2019

Choose a reason for hiding this comment

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

That line won't affect x.RetType.
*retType = *builtinRetTp would affect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this scalar function, when f.getRetTp() is mysql.TypeUnspecified, sf.RetType is mysql.TypeTiny, instead of mysql.TypeUnspecified.

If we change this line to NewFunctionInternal(ctx, x.FuncName.L, x.RetType, args...), sf.RetType may not be mysql.TypeTiny anymore when f.getRetTp() is mysql.TypeUnspecified. I'm not sure if this change has bad influence.

Copy link
Member

Choose a reason for hiding this comment

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

This method just subsitute some columns with NULLs. Use original type should not be worse.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Applied, thanks!

Copy link
Member

@winoros winoros left a comment

Choose a reason for hiding this comment

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

lgtm

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.

LGTM

@eurekaka eurekaka added status/LGT2 Indicates that a PR has LGTM 2. status/can-merge Indicates a PR has been approved by a committer. labels Oct 15, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Oct 15, 2019

/run-all-tests

@sre-bot
Copy link
Contributor

sre-bot commented Oct 15, 2019

cherry pick to release-3.1 in PR #12720

@sre-bot
Copy link
Contributor

sre-bot commented Oct 15, 2019

cherry pick to release-3.0 in PR #12721

@sre-bot
Copy link
Contributor

sre-bot commented Oct 15, 2019

cherry pick to release-2.1 failed

@winoros
Copy link
Member

winoros commented Oct 15, 2019

@justarandomstring
Hi, sorry for bothering you after the pull request is merged. Would you please make a cherry-pick commit picking this pr's changes to TiDB's release-2.1 branch?

@justarandomstring
Copy link
Contributor Author

@winoros No problem. I'll do it in a few days.

eurekaka added a commit to eurekaka/tidb that referenced this pull request Oct 17, 2019
eurekaka added a commit to eurekaka/tidb that referenced this pull request Oct 17, 2019
eurekaka added a commit to eurekaka/tidb that referenced this pull request Oct 17, 2019
ngaut pushed a commit that referenced this pull request Oct 18, 2019
imtbkcat pushed a commit to SunRunAway/tidb that referenced this pull request Nov 13, 2019
imtbkcat pushed a commit to imtbkcat/tidb that referenced this pull request Nov 14, 2019
@imtbkcat imtbkcat mentioned this pull request Nov 14, 2019
XiaTianliang pushed a commit to XiaTianliang/tidb that referenced this pull request Dec 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression contribution This PR is from a community contributor. sig/planner SIG: Planner status/can-merge Indicates a PR has been approved by a committer. 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.

7 participants