* evaluator: avoid evaluate independent subquery multiple times. * ast: rename UseOuterContext to Correlated.
154 lines
3.3 KiB
Go
154 lines
3.3 KiB
Go
// Copyright 2015 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 (
|
|
"github.com/juju/errors"
|
|
"github.com/pingcap/tidb/ast"
|
|
"github.com/pingcap/tidb/context"
|
|
"github.com/pingcap/tidb/evaluator"
|
|
"github.com/pingcap/tidb/infoschema"
|
|
"github.com/pingcap/tidb/optimizer/plan"
|
|
)
|
|
|
|
// recordSet wraps an executor, implements ast.RecordSet interface
|
|
type recordSet struct {
|
|
fields []*ast.ResultField
|
|
executor Executor
|
|
}
|
|
|
|
func (a *recordSet) Fields() ([]*ast.ResultField, error) {
|
|
return a.fields, nil
|
|
}
|
|
|
|
func (a *recordSet) Next() (*ast.Row, error) {
|
|
row, err := a.executor.Next()
|
|
if err != nil || row == nil {
|
|
return nil, errors.Trace(err)
|
|
}
|
|
return &ast.Row{Data: row.Data}, nil
|
|
}
|
|
|
|
func (a *recordSet) Close() error {
|
|
return a.executor.Close()
|
|
}
|
|
|
|
type statement struct {
|
|
is infoschema.InfoSchema
|
|
plan plan.Plan
|
|
node ast.Node
|
|
}
|
|
|
|
func (a *statement) OriginText() string {
|
|
return ""
|
|
}
|
|
|
|
func (a *statement) SetText(text string) {
|
|
return
|
|
}
|
|
|
|
func (a *statement) IsDDL() bool {
|
|
return false
|
|
}
|
|
|
|
func (a *statement) Exec(ctx context.Context) (ast.RecordSet, error) {
|
|
b := newExecutorBuilder(ctx, a.is)
|
|
e := b.build(a.plan)
|
|
if b.err != nil {
|
|
return nil, errors.Trace(b.err)
|
|
}
|
|
|
|
if executorExec, ok := e.(*ExecuteExec); ok {
|
|
err := executorExec.Build()
|
|
if err != nil {
|
|
return nil, errors.Trace(err)
|
|
}
|
|
e = executorExec.StmtExec
|
|
a.node = executorExec.Stmt
|
|
}
|
|
|
|
// Execute independent subquery.
|
|
subExec := &subqueryExecutor{ctx: ctx}
|
|
a.node.Accept(subExec)
|
|
if subExec.err != nil {
|
|
return nil, errors.Trace(subExec.err)
|
|
}
|
|
|
|
if len(e.Fields()) == 0 {
|
|
// No result fields means no Recordset.
|
|
defer e.Close()
|
|
for {
|
|
row, err := e.Next()
|
|
if err != nil {
|
|
return nil, errors.Trace(err)
|
|
}
|
|
if row == nil {
|
|
return nil, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
fs := e.Fields()
|
|
for _, f := range fs {
|
|
if len(f.ColumnAsName.O) == 0 {
|
|
f.ColumnAsName = f.Column.Name
|
|
}
|
|
}
|
|
return &recordSet{
|
|
executor: e,
|
|
fields: fs,
|
|
}, nil
|
|
}
|
|
|
|
// subqueryExecutor executes independent subquery.
|
|
type subqueryExecutor struct {
|
|
ctx context.Context
|
|
multipleRows bool
|
|
existRow bool
|
|
err error
|
|
}
|
|
|
|
func (e *subqueryExecutor) Enter(in ast.Node) (ast.Node, bool) {
|
|
switch in.(type) {
|
|
case *ast.ExistsSubqueryExpr:
|
|
e.existRow = true
|
|
case *ast.CompareSubqueryExpr:
|
|
e.multipleRows = true
|
|
case *ast.PatternInExpr:
|
|
e.multipleRows = true
|
|
}
|
|
return in, false
|
|
}
|
|
|
|
func (e *subqueryExecutor) Leave(in ast.Node) (ast.Node, bool) {
|
|
switch x := in.(type) {
|
|
case *ast.CompareSubqueryExpr:
|
|
e.multipleRows = false
|
|
case *ast.PatternInExpr:
|
|
e.multipleRows = false
|
|
case *ast.ExistsSubqueryExpr:
|
|
e.existRow = false
|
|
case *ast.SubqueryExpr:
|
|
if !x.Correlated {
|
|
err := evaluator.EvalSubquery(e.ctx, x, e.multipleRows, e.existRow)
|
|
if err != nil {
|
|
e.err = errors.Trace(err)
|
|
return in, false
|
|
}
|
|
return in, true
|
|
}
|
|
}
|
|
return in, true
|
|
}
|