From 612dd14fd4683d2165e2c4477746d521ff30c1ca Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Mon, 11 Mar 2019 21:10:23 +0800 Subject: [PATCH] planner/core: fix unexpected error of window function (#9567) --- go.mod | 2 +- go.sum | 5 ++--- planner/core/logical_plan_builder.go | 13 ++++++++++--- planner/core/logical_plan_test.go | 4 ++++ 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index ba8c46337028b..c7f2527732a6f 100644 --- a/go.mod +++ b/go.mod @@ -49,7 +49,7 @@ require ( github.com/pingcap/goleveldb v0.0.0-20171020122428-b9ff6c35079e github.com/pingcap/kvproto v0.0.0-20190215154024-7f2fc73ef562 github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596 - github.com/pingcap/parser v0.0.0-20190305083139-204eb41fcd56 + github.com/pingcap/parser v0.0.0-20190311093336-9fca026d9c49 github.com/pingcap/pd v2.1.0-rc.4+incompatible github.com/pingcap/tidb-tools v2.1.3-0.20190116051332-34c808eef588+incompatible github.com/pingcap/tipb v0.0.0-20190107072121-abbec73437b7 diff --git a/go.sum b/go.sum index 8fda609b5e148..28b2d92651502 100644 --- a/go.sum +++ b/go.sum @@ -9,7 +9,6 @@ github.com/blacktear23/go-proxyprotocol v0.0.0-20180807104634-af7a81e8dd0d h1:rQ github.com/blacktear23/go-proxyprotocol v0.0.0-20180807104634-af7a81e8dd0d/go.mod h1:VKt7CNAQxpFpSDz3sXyj9hY/GbVsQCr0sB3w59nE7lU= github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4= github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= -github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd h1:qMd81Ts1T2OTKmB4acZcyKaMtRnY5Y44NuXGX2GFJ1w= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= @@ -114,8 +113,8 @@ github.com/pingcap/kvproto v0.0.0-20190215154024-7f2fc73ef562 h1:32oF1/8lVnBR2JV github.com/pingcap/kvproto v0.0.0-20190215154024-7f2fc73ef562/go.mod h1:QMdbTAXCHzzygQzqcG9uVUgU2fKeSN1GmfMiykdSzzY= github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596 h1:t2OQTpPJnrPDGlvA+3FwJptMTt6MEPdzK1Wt99oaefQ= github.com/pingcap/log v0.0.0-20190307075452-bd41d9273596/go.mod h1:WpHUKhNZ18v116SvGrmjkA9CBhYmuUTKL+p8JC9ANEw= -github.com/pingcap/parser v0.0.0-20190305083139-204eb41fcd56 h1:BeepWf+BWwVyyL17t60Ri2td2vFb0F8gEhcVaS8bC6g= -github.com/pingcap/parser v0.0.0-20190305083139-204eb41fcd56/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= +github.com/pingcap/parser v0.0.0-20190311093336-9fca026d9c49 h1:dnac5Z4hlmf8Ocd+RasmkuEuI0ZJRH+QJAh3K6vMySQ= +github.com/pingcap/parser v0.0.0-20190311093336-9fca026d9c49/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA= github.com/pingcap/pd v2.1.0-rc.4+incompatible h1:/buwGk04aHO5odk/+O8ZOXGs4qkUjYTJ2UpCJXna8NE= github.com/pingcap/pd v2.1.0-rc.4+incompatible/go.mod h1:nD3+EoYes4+aNNODO99ES59V83MZSI+dFbhyr667a0E= github.com/pingcap/tidb-tools v2.1.3-0.20190116051332-34c808eef588+incompatible h1:e9Gi/LP9181HT3gBfSOeSBA+5JfemuE4aEAhqNgoE4k= diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 7ca24a87836fe..6380cd8e0ed94 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -2715,6 +2715,15 @@ func (b *PlanBuilder) buildProjectionForWindow(p LogicalPlan, expr *ast.WindowFu b.optFlag |= flagEliminateProjection var items []*ast.ByItem + if expr.Spec.Name.L != "" { + ref, ok := b.windowSpecs[expr.Spec.Name.L] + if !ok { + return nil, nil, nil, nil, ErrWindowNoSuchWindow.GenWithStackByArgs(expr.Spec.Name.O) + } + expr.Spec = ref + } else { + expr.Spec.Name = model.NewCIStr("") + } spec := expr.Spec if spec.Ref.L != "" { ref, ok := b.windowSpecs[spec.Ref.L] @@ -2726,6 +2735,7 @@ func (b *PlanBuilder) buildProjectionForWindow(p LogicalPlan, expr *ast.WindowFu return nil, nil, nil, nil, err } } + lenPartition := 0 if spec.PartitionBy != nil { items = append(items, spec.PartitionBy.Items...) @@ -2927,9 +2937,6 @@ func (b *PlanBuilder) buildWindowFunctionFrame(spec *ast.WindowSpec, orderByItem } func (b *PlanBuilder) buildWindowFunction(p LogicalPlan, expr *ast.WindowFuncExpr, aggMap map[*ast.AggregateFuncExpr]int) (*LogicalWindow, error) { - if expr.Spec.Name.O == "" { - expr.Spec.Name = model.NewCIStr("") - } p, partitionBy, orderBy, args, err := b.buildProjectionForWindow(p, expr, aggMap) if err != nil { return nil, err diff --git a/planner/core/logical_plan_test.go b/planner/core/logical_plan_test.go index e735348b75c12..c7618c1ef7a29 100644 --- a/planner/core/logical_plan_test.go +++ b/planner/core/logical_plan_test.go @@ -2127,6 +2127,10 @@ func (s *testPlanSuite) TestWindowFunction(c *C) { sql: "select sum(a) over(w) from t window w as (rows between 1 preceding AND 1 following)", result: "[planner:3582]Window 'w' has a frame definition, so cannot be referenced by another window.", }, + { + sql: "select sum(a) over w from t window w as (rows between 1 preceding AND 1 following)", + result: "TableReader(Table(t))->Window(sum(cast(test.t.a)) over(rows between 1 preceding and 1 following))->Projection", + }, { sql: "select sum(a) over(w order by b) from t window w as (order by a)", result: "[planner:3583]Window '' cannot inherit 'w' since both contain an ORDER BY clause.",