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
36.61k stars 5.76k forks source link

LogicalPlans that contain Window Function are ambiguous #22852

Closed An-DJ closed 3 years ago

An-DJ commented 3 years ago

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

  1. add test case in planner/cascades/transformation_rules_test.go. The number of iterations can be greater than 100 :
    func (s *testTransformationRuleSuite) TestWindowLogicalPlan(c *C) {
    s.optimizer.ResetTransformationRules(map[memo.Operand][]Transformation{
        memo.OperandProjection: {
            NewRuleMergeAdjacentProjection(),
            NewRuleEliminateProjection(),
        },
    })
    defer func() {
        s.optimizer.ResetTransformationRules(DefaultRuleBatches...)
    }()
    input := "select a, max(a) over(), sum(a) over() from t"
    output1 := "DataScan(t)->Projection->Projection->Window(max(test.t.a)->Column#15)->Projection->Window(sum(cast(test.t.a, decimal(65,0) BINARY))->Column#16)->Projection"
    output2 := "DataScan(t)->Projection->Projection->Window(sum(cast(test.t.a, decimal(65,0) BINARY))->Column#15)->Projection->Window(max(test.t.a)->Column#16)->Projection"
    hasOut1 := false
    hasOut2 := false
    for i := 0; i < 100; i++ {
        stmt, err := s.ParseOneStmt(input, "", "")
        c.Assert(err, IsNil)
        p, _, err := plannercore.BuildLogicalPlan(context.Background(), s.sctx, stmt, s.is)
        str := plannercore.ToString(p)
        if str == output1{
            hasOut1 = true
        }
        if str == output2{
            hasOut2 = true
        }
    }
    fmt.Println(hasOut1, hasOut2)
    }
  2. execute go test -check.f "testTransformationRuleSuite.TestWindowLogicalPlan" in path planner/cascades

2. What did you expect to see? (Required)

sql select a, max(a) over(), sum(a) over() from t should be built into only one LogicalPlan.

go test -check.f "testTransformationRuleSuite.TestWindowLogicalPlan" false true PASS: transformation_rules_test.go:528: testTransformationRuleSuite.TestWindowLogicalPlan 0.011s OK: 1 passed PASS ok github.com/pingcap/tidb/planner/cascades 0.132s

3. What did you see instead (Required)

sql select a, max(a) over(), sum(a) over() from t is built into two different LogicalPlans. go test -check.f "testTransformationRuleSuite.TestWindowLogicalPlan" true true PASS: transformation_rules_test.go:528: testTransformationRuleSuite.TestWindowLogicalPlan 0.011s OK: 1 passed PASS ok github.com/pingcap/tidb/planner/cascades 0.132s

4. What is your TiDB version? (Required)

code in branch master.

ti-srebot commented 3 years ago

Please edit this comment or add a new comment to complete the following information

Not a bug

  1. Remove the 'type/bug' label
  2. Add notes to indicate why it is not a bug

Duplicate bug

  1. Add the 'type/duplicate' label
  2. Add the link to the original bug

Bug

Note: Make Sure that 'component', and 'severity' labels are added Example for how to fill out the template: https://github.com/pingcap/tidb/issues/20100

1. Root Cause Analysis (RCA) (optional)

2. Symptom (optional)

3. All Trigger Conditions (optional)

4. Workaround (optional)

5. Affected versions

6. Fixed versions