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: Fixing Expression Rewrite Bug in Predicate Push Down for NULLIF with Type Mismatch #58588

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dash12653
Copy link
Contributor

@dash12653 dash12653 commented Dec 27, 2024

What problem does this PR solve?

Issue Number: close #57647

Problem Summary:

What changed and how does it work?

The sql could be simplified as:

select *
from `t0`
where (
  nullif(3^1, case when (`c0`) then 91 end)
) in (select 1 from `t0`);

When rewriting the expression nullif(3^1, case when (c0) then 91 end), it is transformed into:
if(equals(3^1, case when (c0) then 91 end), null, 3^1).

When building this function, the RetType of null is inferred based on the type of 3^1, and a new RetType is set.

func (c *ifFunctionClass) getFunction(ctx BuildContext, args []Expression) (sig builtinFunc, err error) {
if err = c.verifyArgs(args); err != nil {
return nil, err
}
retTp, err := InferType4ControlFuncs(ctx, c.funcName, args[1], args[2])
if err != nil {
return nil, err
}

Then, the SQL:

where (nullif(3^1, case when (c0) then 91 end)) in (select 1 from t0);
is rewritten to an inner join with otherConditions as:

nullif(3^1, case when (c0) then 91 end) = 1.

During predicate push down, it will try moving otherConditions into equal conditions:

for i := range leftKeys {
lKey, rKey := leftKeys[i], rightKeys[i]
if lProj != nil {
lKey = lProj.AppendExpr(lKey)
}
if rProj != nil {
rKey = rProj.AppendExpr(rKey)
}
eqCond := expression.NewFunctionInternal(p.SCtx().GetExprCtx(), ast.EQ, types.NewFieldType(mysql.TypeTiny), lKey, rKey)

But when rewriting the equal condition, the RetType of null has already been reset based on the earlier inference. So re-inferring of the RetType of null and 3^1 will not match previous result, and the type finally becomes decimal.

} else {
resultFieldType = types.AggFieldType(notNullFields)
var tempFlag uint
evalType := types.AggregateEvalType(notNullFields, &tempFlag)
resultFieldType.SetFlag(tempFlag)
setDecimalFromArgs(evalType, resultFieldType, notNullFields...)
err := addCollateAndCharsetAndFlagFromArgs(ctx, funcName, evalType, resultFieldType, args...)
if err != nil {
return nil, err
}
setFlenFromArgs(evalType, resultFieldType, notNullFields...)

The constant 1 will be wrapped with a (cast as decimal), making it no longer a column, which ultimately leads to the error.

I modified the ifInferType4ControlFuncs() method to determine whether a value is null. If a constant's value.k is null, it will also be treated as null.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-triage-completed release-note-none Denotes a PR that doesn't merit a release note. labels Dec 27, 2024
Copy link

ti-chi-bot bot commented Dec 27, 2024

Hi @dash12653. Thanks for your PR.

I'm waiting for a pingcap member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot ti-chi-bot bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. labels Dec 27, 2024
Copy link

ti-chi-bot bot commented Dec 27, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign zanmato1984 for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

tiprow bot commented Dec 27, 2024

Hi @dash12653. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@hawkingrei
Copy link
Member

/ok-to-test

@ti-chi-bot ti-chi-bot bot added ok-to-test Indicates a PR is ready to be tested. and removed needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. labels Dec 31, 2024
Copy link

codecov bot commented Dec 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.5408%. Comparing base (068b9a8) to head (76b60c8).

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #58588        +/-   ##
================================================
+ Coverage   73.1052%   73.5408%   +0.4356%     
================================================
  Files          1676       1676                
  Lines        463369     463413        +44     
================================================
+ Hits         338747     340798      +2051     
+ Misses       103803     101772      -2031     
- Partials      20819      20843        +24     
Flag Coverage Δ
integration 43.0337% <100.0000%> (?)
unit 72.2979% <100.0000%> (-0.0031%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 52.6910% <ø> (ø)
parser ∅ <ø> (∅)
br 45.8023% <ø> (-0.0090%) ⬇️

@hawkingrei hawkingrei changed the title planner: Fixing Expression Rewrite Bug in Predicate Push Down for NULLIF with Type Mismatch expression: Fixing Expression Rewrite Bug in Predicate Push Down for NULLIF with Type Mismatch Dec 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ok-to-test Indicates a PR is ready to be tested. release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scalar function causes database crash.
2 participants