github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/executor/subquery.go (about)

     1  // Copyright 2016 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package executor
    15  
    16  import (
    17  	"github.com/insionng/yougam/libraries/juju/errors"
    18  	"github.com/insionng/yougam/libraries/pingcap/tidb/ast"
    19  	"github.com/insionng/yougam/libraries/pingcap/tidb/context"
    20  	"github.com/insionng/yougam/libraries/pingcap/tidb/infoschema"
    21  	"github.com/insionng/yougam/libraries/pingcap/tidb/optimizer/plan"
    22  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/types"
    23  )
    24  
    25  var _ ast.SubqueryExec = &subquery{}
    26  
    27  // subquery is an exprNode with a plan.
    28  type subquery struct {
    29  	types.Datum
    30  	Type *types.FieldType
    31  	flag uint64
    32  	text string
    33  	plan plan.Plan
    34  	is   infoschema.InfoSchema
    35  }
    36  
    37  func (sq *subquery) EvalRows(ctx context.Context, rowCount int) ([]types.Datum, error) {
    38  	b := newExecutorBuilder(ctx, sq.is)
    39  	plan.Refine(sq.plan)
    40  	e := b.build(sq.plan)
    41  	if b.err != nil {
    42  		return nil, errors.Trace(b.err)
    43  	}
    44  	defer e.Close()
    45  	if len(e.Fields()) == 0 {
    46  		// No result fields means no Recordset.
    47  		for {
    48  			row, err := e.Next()
    49  			if err != nil {
    50  				return nil, errors.Trace(err)
    51  			}
    52  			if row == nil {
    53  				return nil, nil
    54  			}
    55  		}
    56  	}
    57  	var (
    58  		err  error
    59  		row  *Row
    60  		rows []types.Datum
    61  	)
    62  	for rowCount != 0 {
    63  		row, err = e.Next()
    64  		if err != nil {
    65  			return rows, errors.Trace(err)
    66  		}
    67  		if row == nil {
    68  			break
    69  		}
    70  		if len(row.Data) == 1 {
    71  			rows = append(rows, row.Data[0])
    72  		} else {
    73  			var d types.Datum
    74  			d.SetRow(row.Data)
    75  			rows = append(rows, d)
    76  		}
    77  		if rowCount > 0 {
    78  			rowCount--
    79  		}
    80  	}
    81  	return rows, nil
    82  }
    83  
    84  func (sq *subquery) ColumnCount() (int, error) {
    85  	return len(sq.plan.Fields()), nil
    86  }
    87  
    88  type subqueryBuilder struct {
    89  	is infoschema.InfoSchema
    90  }
    91  
    92  func (sb *subqueryBuilder) Build(p plan.Plan) ast.SubqueryExec {
    93  	return &subquery{
    94  		is:   sb.is,
    95  		plan: p,
    96  	}
    97  }