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

expression, plan: aggressive constant fold for null parameter expression to simplify outer join #7696

Merged
merged 24 commits into from
Sep 23, 2018

Conversation

eurekaka
Copy link
Contributor

@eurekaka eurekaka commented Sep 14, 2018

What problem does this PR solve?

Fix #7679

Currently, foldConstant only handles expressions like constant op constant, for expressions like constant op column, it does nothing. Actually, if constant in constant op column is null, for a lot of operators, this expression can be folded into null. This PR works on this enhancement.

What is changed and how it works?

Substitute expression in constant op expression to constant One if the constant is null, and evaluate the new expression. If the result is null, we can fold this expression, otherwise, keep the original expression.

Check List

Tests

  • Unit test
  • Integration test

Side effects

  • Increased code complexity

@eurekaka eurekaka added type/enhancement The issue or PR belongs to an enhancement. status/WIP component/expression labels Sep 14, 2018
@eurekaka
Copy link
Contributor Author

/run-all-tests

@eurekaka eurekaka added the sig/planner SIG: Planner label Sep 15, 2018
@eurekaka
Copy link
Contributor Author

/run-all-tests

@eurekaka eurekaka changed the title expression: try to fold constant for expression with null parameter expression, plan: aggressive constant fold for null parameter expression to simplify outer join Sep 17, 2018
@eurekaka
Copy link
Contributor Author

@zz-jason @XuHuaiyu @winoros PTAL

if !hasNullArg || !sc.InNullRejectCheck {
return expr, isDeferredConst
}
dummyScalarFunc, _ := x.Clone().(*ScalarFunction)
Copy link
Member

Choose a reason for hiding this comment

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

Can we use NewFunction instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

if !canFold {
return expr, isDeferredConst
if len(nonConstArgIdx) > 0 {
if !hasNullArg || !sc.InNullRejectCheck {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to check whether it's in null-reject?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes we need, for cases like select null and a < 1 from t;, if we fold null constant for the projection part, the result would be null for all rows, but actually for t.a >= 1, the result should be 0

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

@@ -60,6 +60,7 @@ type StatementContext struct {
UseCache bool
PadCharToFullLength bool
BatchCheck bool
InNullRejectCheck bool
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make this an input parameter for foldConstant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we make it a parameter of foldConstant, we have to modify signature of FoldConstant, NewFunction, NewFunctionInternal, EvaluateExprWithNull, this would lead to a lot of unnecessary code changes

if argIsConst[i] {
constArgs[i] = arg
} else {
constArgs[i] = One
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be incorrect if the ScalarFunction is NullEQ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is null and is not null should have only one parameter, so they would not fall into code path of this patch IMHO.

Copy link
Contributor

Choose a reason for hiding this comment

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

What I mentioned here is the operator <=>.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh you are right, will fix this, thanks.

@@ -548,8 +547,8 @@ func (s *testPlanSuite) TestOuterWherePredicatePushDown(c *C) {
}{
// issue #7628, left join with where condition
{
sql: "select * from t as t1 left join t as t2 on t1.b = t2.b where (t1.a=1 and t2.a=1) or (t1.a=2 and t2.a=2)",
sel: "[or(and(eq(t1.a, 1), eq(t2.a, 1)), and(eq(t1.a, 2), eq(t2.a, 2)))]",
sql: "select * from t as t1 left join t as t2 on t1.b = t2.b where (t1.a=1 and t2.a is null) or (t1.a=2 and t2.a=2)",
Copy link
Contributor

Choose a reason for hiding this comment

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

Why change this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The purpose of this test case is to verify predicate pushdown over outer join, after applying this PR, the outer join would be converted to inner join now, so I changed the where condition to keep the outer join.

@eurekaka
Copy link
Contributor Author

/run-all-tests

if !conOK {
canFold = false
con, conOK := foldedArg.(*Constant)
if conOK {
Copy link
Member

Choose a reason for hiding this comment

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

how about:

argIsConst[i] = conOk
allConstArg = allConstArg && conOk
hasNullArg = hasNullArg || (conOk && con.Value.IsNull())

Maybe this is more clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, patch updated

if x.Value.IsNull() {
return true
} else if isTrue, err := x.Value.ToBool(sc); err != nil || isTrue == 0 {
} else if isTrue, err := x.Value.ToBool(sc); err == nil && isTrue == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

should it be err == nil && isTrue != 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

isTrue != 0 means the where condition is true if the inner table columns are null, so it cannot be rejected?

@eurekaka
Copy link
Contributor Author

@zz-jason @XuHuaiyu comments addressed, PTAL

Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

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

LGTM

@zz-jason zz-jason added the status/LGT1 Indicates that a PR has LGTM 1. label Sep 21, 2018
@eurekaka eurekaka added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Sep 21, 2018
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression sig/planner SIG: Planner status/LGT2 Indicates that a PR has LGTM 2. type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants