-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executor: support window func for aggregate without frame clause
- Loading branch information
Showing
25 changed files
with
898 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// Copyright 2018 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package executor | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/opentracing/opentracing-go" | ||
"github.com/pingcap/errors" | ||
"github.com/pingcap/tidb/executor/windowfuncs" | ||
"github.com/pingcap/tidb/expression" | ||
"github.com/pingcap/tidb/util/chunk" | ||
) | ||
|
||
// WindowExec is the executor for window functions. | ||
type WindowExec struct { | ||
baseExecutor | ||
|
||
group *group | ||
inputIter *chunk.Iterator4Chunk | ||
inputRow chunk.Row | ||
groupRows []chunk.Row | ||
childResults []*chunk.Chunk | ||
wf windowfuncs.WindowFunc | ||
executed bool | ||
childCols []*expression.Column | ||
} | ||
|
||
// Close implements the Executor Close interface. | ||
func (e *WindowExec) Close() error { | ||
e.childResults = nil | ||
return errors.Trace(e.baseExecutor.Close()) | ||
} | ||
|
||
// Next implements the Executor Next interface. | ||
func (e *WindowExec) Next(ctx context.Context, chk *chunk.Chunk) error { | ||
if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil { | ||
span1 := span.Tracer().StartSpan("windowExec.Next", opentracing.ChildOf(span.Context())) | ||
defer span1.Finish() | ||
} | ||
if e.runtimeStats != nil { | ||
start := time.Now() | ||
defer func() { e.runtimeStats.Record(time.Now().Sub(start), chk.NumRows()) }() | ||
} | ||
chk.Reset() | ||
if e.wf.RemainResult() { | ||
e.appendResult2Chunk(chk) | ||
} | ||
for !e.executed && (chk.NumRows() == 0 || chk.RemainedRows(chk.NumCols()-1) > 0) { | ||
err := e.consumeOneGroup(ctx, chk) | ||
if err != nil { | ||
e.executed = true | ||
return errors.Trace(err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (e *WindowExec) consumeOneGroup(ctx context.Context, chk *chunk.Chunk) error { | ||
for !e.executed { | ||
if err := e.fetchChildIfNecessary(ctx, chk); err != nil { | ||
return errors.Trace(err) | ||
} | ||
for ; e.inputRow != e.inputIter.End(); e.inputRow = e.inputIter.Next() { | ||
meetNewGroup, err := e.group.meetNewGroup(e.inputRow) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
if meetNewGroup { | ||
err := e.consumeGroupRows(chk) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
err = e.appendResult2Chunk(chk) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
} | ||
e.groupRows = append(e.groupRows, e.inputRow) | ||
if meetNewGroup { | ||
e.inputRow = e.inputIter.Next() | ||
return nil | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (e *WindowExec) consumeGroupRows(chk *chunk.Chunk) error { | ||
if len(e.groupRows) == 0 { | ||
return nil | ||
} | ||
e.copyChk(chk) | ||
var err error | ||
e.groupRows, err = e.wf.ProcessOneChunk(e.ctx, e.groupRows, chk) | ||
return err | ||
} | ||
|
||
func (e *WindowExec) fetchChildIfNecessary(ctx context.Context, chk *chunk.Chunk) (err error) { | ||
if e.inputIter != nil && e.inputRow != e.inputIter.End() { | ||
return nil | ||
} | ||
|
||
// Before fetching a new batch of input, we should consume the last group. | ||
err = e.consumeGroupRows(chk) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
|
||
childResult := e.children[0].newFirstChunk() | ||
err = e.children[0].Next(ctx, childResult) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
e.childResults = append(e.childResults, childResult) | ||
// No more data. | ||
if childResult.NumRows() == 0 { | ||
e.executed = true | ||
err = e.appendResult2Chunk(chk) | ||
return errors.Trace(err) | ||
} | ||
|
||
e.inputIter = chunk.NewIterator4Chunk(childResult) | ||
e.inputRow = e.inputIter.Begin() | ||
return nil | ||
} | ||
|
||
// appendResult2Chunk appends result of all the aggregation functions to the | ||
// result chunk, and reset the evaluation context for each aggregation. | ||
func (e *WindowExec) appendResult2Chunk(chk *chunk.Chunk) error { | ||
e.copyChk(chk) | ||
var err error | ||
e.groupRows, err = e.wf.ExhaustResult(e.ctx, e.groupRows, chk) | ||
return err | ||
} | ||
|
||
func (e *WindowExec) copyChk(chk *chunk.Chunk) { | ||
if len(e.childResults) == 0 || chk.NumRows() > 0 { | ||
return | ||
} | ||
childResult := e.childResults[0] | ||
e.childResults = e.childResults[1:] | ||
for i, col := range e.childCols { | ||
chk.CopyColumns(childResult, i, col.Index) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2018 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package executor_test | ||
|
||
import ( | ||
. "github.com/pingcap/check" | ||
"github.com/pingcap/tidb/util/testkit" | ||
) | ||
|
||
func (s *testSuite2) TestWindowFunctions(c *C) { | ||
tk := testkit.NewTestKit(c, s.store) | ||
tk.MustExec("use test") | ||
tk.MustExec("drop table if exists t") | ||
tk.MustExec("create table t (a int, b int, c int)") | ||
tk.MustExec("set @@tidb_enable_window_function = 1") | ||
defer func() { | ||
tk.MustExec("set @@tidb_enable_window_function = 0") | ||
}() | ||
tk.MustExec("insert into t values (1,2,3),(4,3,2),(2,3,4)") | ||
result := tk.MustQuery("select count(a) over () from t") | ||
result.Check(testkit.Rows("3", "3", "3")) | ||
result = tk.MustQuery("select sum(a) over () + count(a) over () from t") | ||
result.Check(testkit.Rows("10", "10", "10")) | ||
result = tk.MustQuery("select sum(a) over (partition by a) from t") | ||
result.Check(testkit.Rows("1", "2", "4")) | ||
result = tk.MustQuery("select 1 + sum(a) over (), count(a) over () from t") | ||
result.Check(testkit.Rows("8 3", "8 3", "8 3")) | ||
result = tk.MustQuery("select sum(t1.a) over() from t t1, t t2") | ||
result.Check(testkit.Rows("21", "21", "21", "21", "21", "21", "21", "21", "21")) | ||
} |
Oops, something went wrong.