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

     1  // Copyright 2015 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"
    22  	"github.com/insionng/yougam/libraries/pingcap/tidb/optimizer/plan"
    23  	"github.com/insionng/yougam/libraries/pingcap/tidb/sessionctx"
    24  )
    25  
    26  // Compiler compiles an ast.StmtNode to a stmt.Statement.
    27  type Compiler struct {
    28  }
    29  
    30  // Compile compiles an ast.StmtNode to a stmt.Statement.
    31  // If it is supported to use new plan and executer, it optimizes the node to
    32  // a plan, and we wrap the plan in an adapter as stmt.Statement.
    33  // If it is not supported, the node will be converted to old statement.
    34  func (c *Compiler) Compile(ctx context.Context, node ast.StmtNode) (ast.Statement, error) {
    35  	ast.SetFlag(node)
    36  
    37  	is := sessionctx.GetDomain(ctx).InfoSchema()
    38  	if err := optimizer.Preprocess(node, is, ctx); err != nil {
    39  		return nil, errors.Trace(err)
    40  	}
    41  	// Validate should be after NameResolve.
    42  	if err := optimizer.Validate(node, false); err != nil {
    43  		return nil, errors.Trace(err)
    44  	}
    45  	sb := NewSubQueryBuilder(is)
    46  	p, err := optimizer.Optimize(ctx, node, sb)
    47  	if err != nil {
    48  		return nil, errors.Trace(err)
    49  	}
    50  	sa := &statement{
    51  		is:   is,
    52  		plan: p,
    53  	}
    54  	return sa, nil
    55  }
    56  
    57  // NewSubQueryBuilder builds and returns a new SubQuery builder.
    58  func NewSubQueryBuilder(is infoschema.InfoSchema) plan.SubQueryBuilder {
    59  	return &subqueryBuilder{is: is}
    60  }