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: fix a bug caused by cast function signature wrong in aggregation push down #28805

Merged
merged 9 commits into from
Nov 1, 2021

Conversation

tiancaiamao
Copy link
Contributor

What problem does this PR solve?

Issue Number: close #28804

Problem Summary:

What is changed and how it works?

Remove the special handle of cast function in ColumnSubstituteImpl

The bug is caused by this:

Before ColumnSubstitute, the function signature of cast(floor(hour(ts) / 4) as char) is cast(Int -> Varstring) ...
Then, the ColumnSubstitute will substitute the column ts of expression floor(hour(ts) / 4)
After that, the expression floor(hour(ts) / 4) signature changed, it's return type is not Int any more.
Expression floor(hour(ts) / 4) returns Decimal instead.

But the signature of cast() function is still cast(Int -> Varstring), so the cast expect floor(hour(ts) / 4) to return Int and the later returns Decimal, the error is throw finally.

What's Changed:

Remove the special handle of the cast function.

How it Works:

The bug is caused by the special handle of the Cast function.
Its function args are re-generated (signature re-calculated), but the Cast function it self does not refresh.
So the signature mismatch...

OK, "why the signature change from Int to Decimal" is another story, it's caused by the wrong Flen and that's another big issue.
I'm not meant to solve it here.

Check List

Tests

  • [] Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

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

Fix `ColumnSubstitute` cause wrong signature of the Cast() function, this bug could led TiDB return "baseBuiltinFunc.vecEvalInt() should never be called" error in some aggregation push down scenario.

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Oct 14, 2021

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • guo-shaoge
  • wjhuang2016

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

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

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Oct 14, 2021
@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 17, 2021
@ti-chi-bot ti-chi-bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Oct 19, 2021
@ti-chi-bot ti-chi-bot added status/LGT1 Indicates that a PR has LGTM 1. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Oct 20, 2021
expression/util.go Outdated Show resolved Hide resolved
@tiancaiamao
Copy link
Contributor Author

Unstable CI #24679

[2021-10-26T08:26:43.833Z] ----------------------------------------------------------------------
[2021-10-26T08:26:43.833Z] FAIL: handle_test.go:957: testSerialStatsSuite.TestAnalyzeGlobalStatsWithOpts2
[2021-10-26T08:26:43.833Z] 
[2021-10-26T08:26:43.833Z] handle_test.go:968:
[2021-10-26T08:26:43.833Z]     s.checkForGlobalStatsWithOpts(c, tk, "test_gstats_opt2", "p1", 1, 50)
[2021-10-26T08:26:43.833Z] handle_test.go:913:
[2021-10-26T08:26:43.833Z]     // since the hist-building algorithm doesn't stipulate the final bucket number to be equal to the expected number exactly,
[2021-10-26T08:26:43.833Z]     // we have to check the results by a range here.
[2021-10-26T08:26:43.833Z]     c.Assert(numBuckets, GreaterEqual, buckets-delta)
[2021-10-26T08:26:43.833Z] ... compare_one int = 0
[2021-10-26T08:26:43.833Z] ... compare_two int = 15

/run-check_dev_2

@tiancaiamao
Copy link
Contributor Author

CI failed again, I can't reproduce it on my local environment:

[2021-10-26T08:37:21.334Z] FAIL: integration_test.go:1911: testIntegrationSerialSuite.TestIndexMerge
[2021-10-26T08:37:21.334Z] 
[2021-10-26T08:37:21.334Z] integration_test.go:1933:
[2021-10-26T08:37:21.334Z]     tk.MustQuery(tt).Check(testkit.Rows(output[i].Plan...))
[2021-10-26T08:37:21.334Z] /home/jenkins/agent/workspace/tidb_ghpr_check_2/go/src/github.com/pingcap/tidb/util/testkit/testkit.go:66:
[2021-10-26T08:37:21.334Z]     res.c.Assert(resBuff.String(), check.Equals, needBuff.String(), res.comment)
[2021-10-26T08:37:21.334Z] ... obtained string = "" +
[2021-10-26T08:37:21.334Z] ...     "[IndexMerge 8.00 root  ]\n" +
[2021-10-26T08:37:21.334Z] ...     "[├─IndexRangeScan(Build) 1.00 cop[tikv] table:t, index:a(a) range:[1,1], keep order:false]\n" +
[2021-10-26T08:37:21.334Z] ...     "[├─Selection(Build) 0.80 cop[tikv]  1]\n" +
[2021-10-26T08:37:21.334Z] ...     "[│ └─IndexRangeScan 1.00 cop[tikv] table:t, index:b(b) range:[1,1], keep order:false]\n" +
[2021-10-26T08:37:21.334Z] ...     "[└─TableRowIDScan(Probe) 8.00 cop[tikv] table:t keep order:false]\n"
[2021-10-26T08:37:21.334Z] ... expected string = "" +
[2021-10-26T08:37:21.334Z] ...     "[IndexMerge 1.72 root  ]\n" +
[2021-10-26T08:37:21.334Z] ...     "[├─IndexRangeScan(Build) 1.00 cop[tikv] table:t, index:a(a) range:[1,1], keep order:false]\n" +
[2021-10-26T08:37:21.334Z] ...     "[├─Selection(Build) 0.80 cop[tikv]  eq(length(cast(1, var_string(20))), 1)]\n" +
[2021-10-26T08:37:21.334Z] ...     "[│ └─IndexRangeScan 1.00 cop[tikv] table:t, index:b(b) range:[1,1], keep order:false]\n" +
[2021-10-26T08:37:21.334Z] ...     "[└─TableRowIDScan(Probe) 1.72 cop[tikv] table:t keep order:false]\n"
[2021-10-26T08:37:21.334Z] ... sql:desc format='brief' select /*+ use_index_merge(t) */ * from t where a =1 or (b=1 and length(b)=1), args:[]

@tiancaiamao
Copy link
Contributor Author

/run-check_dev_2

@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Oct 28, 2021
@XuHuaiyu XuHuaiyu added the type/bugfix This PR fixes a bug. label Nov 1, 2021
@tiancaiamao
Copy link
Contributor Author

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: b0072d9

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Nov 1, 2021
"├─IndexRangeScan(Build) 1.00 cop[tikv] table:t, index:a(a) range:[1,1], keep order:false",
"├─Selection(Build) 0.80 cop[tikv] eq(length(cast(1, var_string(20))), 1)",
"├─Selection(Build) 0.80 cop[tikv] 1",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change is expected, because after fixing the bug, the constant propagate can infer that
b = 1 and length(b) = 1 is always true.
So the this filter condition changes and the cost of the plan changes...

@tiancaiamao
Copy link
Contributor Author

/run-check_dev_2

@ti-chi-bot ti-chi-bot merged commit a18977a into pingcap:master Nov 1, 2021
@tiancaiamao tiancaiamao deleted the fix-issue-28804 branch November 1, 2021 10:15
ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Nov 1, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-5.1 in PR #29316

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Nov 2, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-5.2 in PR #29345

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-cherry-pick-release-5.1 needs-cherry-pick-release-5.2 release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. 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.

baseBuiltinFunc.vecEvalInt() should never be called, please contact the TiDB team for help
6 participants