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: Add warning info for exprs that can not be pushed to storage layer #22713

Merged
merged 9 commits into from
Feb 20, 2021

Conversation

windtalker
Copy link
Contributor

What problem does this PR solve?

Issue Number: close #xxx

Problem Summary:

Currently, when user running a query, and found the query is slower than expected because some part of the query(e.g. Selection or Aggregation) is not pushed down to coprocessor, there is no easy way to find out which function not supported by coprocessor.

What is changed and how it works?

Proposal: xxx

What's Changed:

How it Works:

According to Mysql document:

The EXPLAIN statement produces extra (“extended”) information that is not part of EXPLAIN output but can be viewed by issuing a SHOW WARNINGS statement following EXPLAIN. As of MySQL 8.0.12, extended information is available for SELECT, DELETE, INSERT, REPLACE, and UPDATE statements. Prior to 8.0.12, extended information is available only for SELECT statements.
The Message value in SHOW WARNINGS output displays how the optimizer qualifies table and column names in the SELECT statement, what the SELECT looks like after the application of rewriting and optimization rules, and possibly other notes about the optimization process.

It is reasonable to add warnings about functions that can not be pushed to coprocessor when user executing an explain statement, so this pr add this information to make user easier to figure out why Selection or Aggregation is not pushed to coprocessor.

Related changes

  • Need to cherry-pick to the release branch

Check List

Tests

  • Integration test
  • Manual test (add detailed scripts or steps below)

Side effects

Release note

  • Add warning info for exprs that can not be pushed to storage layer for explain statement.

@windtalker windtalker requested review from a team as code owners February 4, 2021 02:43
@windtalker windtalker requested review from wshwsh12 and winoros and removed request for a team February 4, 2021 02:43
if storeType == kv.UnSpecified {
storageName = "storage layer"
}
pc.sc.AppendWarning(errors.New("Scalar function '" + scalarFunc.FuncName.L + "'(signature: " + scalarFunc.Function.PbCode().String() + ") can not be pushed to " + storageName))
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like each time only one function can be shown. For a real world case it will be tricky.

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 TiDB implementation, for each DNF expression, If a function can not be pushed down, then all the other functions in the same DNF expression will not be checked, as far as I known, TiDB will convert filter expression into a list of DNF before predicate pushdown, so if function_1 and function_2 is not supported by the storage layer, for query:
select * from a where function_1 or function_2, only function_1 will be recorded.
but for query
select * from a where function_1 and function_2, both function_1 and function_2 will be recorded.

Copy link
Member

Choose a reason for hiding this comment

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

why not reading the explain result to find which functions are not pushed down?

@windtalker
Copy link
Contributor Author

/run-all-tests

Copy link
Member

@hanfei1991 hanfei1991 left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-srebot ti-srebot added the status/LGT1 Indicates that a PR has LGTM 1. label Feb 4, 2021
@@ -1161,6 +1161,13 @@ func canScalarFuncPushDown(scalarFunc *ScalarFunction, pc PbConverter, storeType

// Check whether this function can be pushed.
if !canFuncBePushed(scalarFunc, storeType) {
if pc.sc.InExplainStmt {
Copy link
Contributor

Choose a reason for hiding this comment

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

for example: select fun1(), agg_fun2() from t where func3() group by c having fun4(); if funx() cannot be pushed down, will this shows 4 warnings, even though it first encounters func3()?

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 pr only record the unsupported functions, it does not change the behavior of optimizer, so if func3 is not supported, and the optimizer decide not pushdown agg to the storage layer, then only func3 will be recorded.

Copy link
Contributor

Choose a reason for hiding this comment

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

other unsupported functions also will be recorded?

@@ -1184,6 +1191,9 @@ func canScalarFuncPushDown(scalarFunc *ScalarFunction, pc PbConverter, storeType

func canExprPushDown(expr Expression, pc PbConverter, storeType kv.StoreType) bool {
if storeType == kv.TiFlash && expr.GetType().Tp == mysql.TypeDuration {
if pc.sc.InExplainStmt {
Copy link
Contributor

Choose a reason for hiding this comment

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

duration and func() will rusult two warnings? if func() cannot be pushed down

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 the expr contains Duration, it will return directly without checking function in this expr

if sc.InExplainStmt {
storageName := storeType.Name()
if storeType == kv.UnSpecified {
storageName = "storage layer"
Copy link
Member

Choose a reason for hiding this comment

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

when will we encounter this branch?

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 predicate_push_down rule, it uses Unspecified to make sure it can pushdown all the possible expressions, and when generating the cop task, the DataSource will use the specific store type(TiFlash/TiKV/TiDB) to make sure only pushdown supported expressions to each store.

if storeType == kv.UnSpecified {
storageName = "storage layer"
}
pc.sc.AppendWarning(errors.New("Scalar function '" + scalarFunc.FuncName.L + "'(signature: " + scalarFunc.Function.PbCode().String() + ") can not be pushed to " + storageName))
Copy link
Member

Choose a reason for hiding this comment

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

why not reading the explain result to find which functions are not pushed down?

@windtalker
Copy link
Contributor Author

Explain result only tells that Filter/Agg is not pushed down to cop, it can not answer the question "why filter/agg is not pushed down". For example:

  • if the filter is func1 and func2, and it is not pushed down, user does not have a convenient way to tell whether func1 or func2 is not supported in storage layer.
  • if an agg is not pushed down, user does not have a convenient way to tell whether the agg is not pushed to storage layer because some of the expressions in agg is not supported in storage layer or just because the CBO think it is not worth pushing down the agg to storage layer.

Copy link
Contributor

@fzhedu fzhedu left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-srebot ti-srebot removed the status/LGT1 Indicates that a PR has LGTM 1. label Feb 19, 2021
@ti-srebot ti-srebot added the status/LGT2 Indicates that a PR has LGTM 2. label Feb 19, 2021
@fzhedu
Copy link
Contributor

fzhedu commented Feb 19, 2021

/merge

@ti-srebot ti-srebot added the status/can-merge Indicates a PR has been approved by a committer. label Feb 19, 2021
@ti-srebot
Copy link
Contributor

/run-all-tests

@ti-srebot
Copy link
Contributor

@windtalker merge failed.

@windtalker
Copy link
Contributor Author

/run-integration-copr-test

@fzhedu
Copy link
Contributor

fzhedu commented Feb 20, 2021

/merge

@ti-srebot
Copy link
Contributor

/run-all-tests

@windtalker
Copy link
Contributor Author

/label needs-cherry-pick-4.0

1 similar comment
@Rustin170506
Copy link
Member

/label needs-cherry-pick-4.0

@Rustin170506
Copy link
Member

/remove-label needs-cherry-pick-4.0

@windtalker
Copy link
Contributor Author

/label needs-cherry-pick-4.0

@ti-srebot
Copy link
Contributor

cherry pick to release-4.0 in PR #23020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression sig/execution SIG execution status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants