github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/optimizer/plan/new_plans.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 plan
    15  
    16  import (
    17  	"github.com/insionng/yougam/libraries/juju/errors"
    18  	"github.com/insionng/yougam/libraries/pingcap/tidb/ast"
    19  )
    20  
    21  // JoinType contains CrossJoin, InnerJoin, LeftOuterJoin, RightOuterJoin, FullOuterJoin, SemiJoin.
    22  type JoinType int
    23  
    24  const (
    25  	// CrossJoin means Cartesian Product, but not used now.
    26  	CrossJoin JoinType = iota
    27  	// InnerJoin means inner join.
    28  	InnerJoin
    29  	// LeftOuterJoin means left join.
    30  	LeftOuterJoin
    31  	// RightOuterJoin means right join.
    32  	RightOuterJoin
    33  	// TODO: support semi join.
    34  )
    35  
    36  // Join is the logical join plan.
    37  type Join struct {
    38  	basePlan
    39  
    40  	JoinType JoinType
    41  
    42  	EqualConditions []ast.ExprNode
    43  	LeftConditions  []ast.ExprNode
    44  	RightConditions []ast.ExprNode
    45  	OtherConditions []ast.ExprNode
    46  }
    47  
    48  // AddChild for parent.
    49  func addChild(parent Plan, child Plan) {
    50  	if child == nil || parent == nil {
    51  		return
    52  	}
    53  	child.AddParent(parent)
    54  	parent.AddChild(child)
    55  }
    56  
    57  // InsertPlan means inserting plan between two plans.
    58  func InsertPlan(parent Plan, child Plan, insert Plan) error {
    59  	err := child.ReplaceParent(parent, insert)
    60  	if err != nil {
    61  		return errors.Trace(err)
    62  	}
    63  	err = parent.ReplaceChild(child, insert)
    64  	if err != nil {
    65  		return errors.Trace(err)
    66  	}
    67  	insert.AddChild(child)
    68  	insert.AddParent(parent)
    69  	return nil
    70  }
    71  
    72  // RemovePlan means removing a plan.
    73  func RemovePlan(p Plan) error {
    74  	parents := p.GetParents()
    75  	children := p.GetChildren()
    76  	if len(parents) != 1 || len(children) != 1 {
    77  		return SystemInternalErrorType.Gen("can't remove this plan")
    78  	}
    79  	parent, child := parents[0], children[0]
    80  	err := parent.ReplaceChild(p, child)
    81  	if err != nil {
    82  		return errors.Trace(err)
    83  	}
    84  	err = child.ReplaceParent(p, parent)
    85  	return errors.Trace(err)
    86  }