pingcap / tidb

TiDB is an open-source, cloud-native, distributed, MySQL-Compatible database for elastic scale and real-time analytics. Try AI-powered Chat2Query free at : https://www.pingcap.com/tidb-serverless/
https://pingcap.com
Apache License 2.0
37k stars 5.82k forks source link

Call For Participation: add rules for cascades optimizer #13709

Open winoros opened 4 years ago

winoros commented 4 years ago

In https://github.com/pingcap/tidb/blob/master/docs/design/2018-08-29-new-planner.md, we proposed a new planner based on cascades. We create this issue to track the dev progress of the new planner.

If you're interested in this project, feel free to join the discussion by entering the slack channel #sig-planner in the tidbcommunity slack workspace. Or just pick some TODO issues listed in this tracking issue using the following working flow:

  1. Find one issue which you're interested in.
  2. If it's a simple work you can just reply in this issue to let the community know that issue is picked by you. Or you can create a separate issue to describe the rough implementation and discussing with the community.
  3. File new pull requests.

You can find the basic workflow for adding a transformation rule here.

The issues below with [easy] tag are highly recommended for new contributors.

Issues

francis0407 commented 4 years ago

I highly recommend new contributors try rules like 'Limit PushDown' and 'TopN PushDown' above. They are truly easy to implement and can help you get familiar with the cascades framework.

tangwz commented 4 years ago

let me fix Limit PushDown.

hsqlu commented 4 years ago

I'd like to take TopN PushDown.

francis0407 commented 4 years ago

That's great. You'd better implement only one transformation rule in one PR. cc @tangwz @hsqlu

jiangyuzhao commented 4 years ago

@francis0407 Do you have some other recommendations for new contributors? Thank you very much!

francis0407 commented 4 years ago

You can try this transformation rule: PushSelDownWindow which pushes the Selection down to the child of Window. The current implementation is here: https://github.com/pingcap/tidb/blob/d438c860a660f00c755094e1d6c9e61c18715edf/planner/core/rule_predicate_push_down.go#L564.

The logic of this rule is simple, I think. You only need to implement this rule and add tests in cascades/testdata/transformation_rules_test_in.json/TestPredicatePushDown.

If you encounter any difficulties, you can ask us in slack. @jiangyuzhao

jiangyuzhao commented 4 years ago

@francis0407 OK. Thank you very much. I will try to implement PushSelDownWindow. I'm a new contributor, and I'm not familiar with slack. Do you mean I can send comment below the issue?

francis0407 commented 4 years ago

@jiangyuzhao Slack is a powerful software for online communication. You may need to sign up at first. Then check this link: https://pingcap.com/tidbslack/. We can discuss issues about cascades optimizer in the channel sig-planner.

jiangyuzhao commented 4 years ago

@francis0407 Thanks for your explanation~

gauss1314 commented 4 years ago

Let me fix PushSelDownUnion from push the selection down to union of Predicate pushdown.

pingyu commented 4 years ago

Let me fix merge the adjacent projection of Projection elimination

francis0407 commented 4 years ago

Hi guys, If you are not sure how to implement a rule or how to add unit test for the rule, I think https://github.com/pingcap/tidb/pull/13106 and https://github.com/pingcap/tidb/pull/13288 could be good examples. Note that GetPattern() is not used to define the Pattern anymore, you have to define a function like NewRuleXXXXX to create the rule you have implemented and define the pattern of the rule inside this function.

francis0407 commented 4 years ago

Workflow for Adding a Transformation Rule

Here is the basic workflow for adding a Transformation rule in cascades optimizer. Add Optimization Rules for Cascades Optimizer(in Chinese only) may provide more information.

Implement a Transformation Rule

  1. Define a struct for the rule, like PushSelDownAggregation.
  2. Implement two methods OnMatch() and OnTransform().
  3. Add a function NewRuleXXX to create such a rule, and register it's Pattern inside.

Add unit test for a Transformatino Rule

  1. Add the rule into transformation_rules.go/defaultTransformationMap. The position where it supposed to be depends on the top Operand of its Pattern. For example, if its Pattern is Selection->Projection, it should be in the group of memo.OperandSelection: { ... }.
  2. Add unit test case in transformation_rules_test.go, TestPredicatePushDown is a good example.
  3. Add the rule into the test optimizer's transformation map.
  4. Add test SQL in file testdata/transformation_rules_suite_in.json. Make sure those SQLs will trigger the rule you just implemented.
  5. Run go test with arguments --record, it will generate test results into file testdata/transformation_rules_suite_out.json.
  6. Check if the result shows the rule works.
edytagarbarz commented 4 years ago

Let me fix Stream aggregation

francis0407 commented 4 years ago

Hi, @edytagarbarz Stream aggregation might not be ready to be implemented now. It requires a bottom-up method to prune physical properties called PreparePossibleProperty(currently in file planner/core/property_cols_prune.go), which I'm still working on. Could you please try other tasks? I think the implementationRule for LogicalWindow is quite easy to do. When I finish the PreparePossibleProperty, I will let you know and invite you to fix the Stream agg.

edytagarbarz commented 4 years ago

@francis0407 definitely! I'll do implementationRule for LogicalWindow then, thanks!

jiangyuzhao commented 4 years ago

You can try this transformation rule: PushSelDownWindow which pushes the Selection down to the child of Window. The current implementation is here:

https://github.com/pingcap/tidb/blob/d438c860a660f00c755094e1d6c9e61c18715edf/planner/core/rule_predicate_push_down.go#L564

. The logic of this rule is simple, I think. You only need to implement this rule and add tests in cascades/testdata/transformation_rules_test_in.json/TestPredicatePushDown.

If you encounter any difficulties, you can ask us in slack. @jiangyuzhao

@francis0407 I'm sorry for being busy this week. I will finish the task soon!

francis0407 commented 4 years ago

@jiangyuzhao Don't worry, enjoy the world of open source.

francis0407 commented 4 years ago

If you are looking for PR examples, check the cascades project.

t0350 commented 4 years ago

I'd like to fix eliminate the projection whose output columns are the same with its child from projection elimination.

gauss1314 commented 4 years ago

Let me fix push the top-n down to gather of push operators down to tikv gather.

Reminiscent commented 4 years ago

Let me fix:

hellojujian commented 4 years ago

let me fix

doggeral commented 4 years ago

let me fix push the limit down to projection

winoros commented 4 years ago

@doggeral If you need some help, you can contact us via slack channel #sig-planner in the tidbcommunity

edytagarbarz commented 4 years ago

Let me fix Aggregation elimination implement XfromAggToProj

gauss1314 commented 4 years ago

Let me fix Aggregation elimination implement XfromAggToProj

Hi @edytagarbarz , can I do this if you haven't started yet?

SeaRise commented 4 years ago

@gauss1314 I am so sorry...I've started to implement XfromAggToProj....

jackwener commented 3 years ago

The task merge the adjacent Window. [easy] is completed by An-DJ.

jackwener commented 3 years ago

Hi, I'm willing to take the Group merging for deduplication. [medium] up.

However, some of the backgrounds are rather scarce. Can you provide some background?