github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/causetstore/petri/acyclic/causet/implementation/join.go (about)

     1  // Copyright 2020 WHTCORPS INC, 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 implementation
    15  
    16  import (
    17  	causetembedded "github.com/whtcorpsinc/milevadb/causet/embedded"
    18  	"github.com/whtcorpsinc/milevadb/causet/memo"
    19  )
    20  
    21  // HashJoinImpl is the implementation for PhysicalHashJoin.
    22  type HashJoinImpl struct {
    23  	baseImpl
    24  }
    25  
    26  // CalcCost implements Implementation CalcCost interface.
    27  func (impl *HashJoinImpl) CalcCost(outCount float64, children ...memo.Implementation) float64 {
    28  	hashJoin := impl.plan.(*causetembedded.PhysicalHashJoin)
    29  	// The children here are only used to calculate the cost.
    30  	hashJoin.SetChildren(children[0].GetCauset(), children[1].GetCauset())
    31  	selfCost := hashJoin.GetCost(children[0].GetCauset().StatsCount(), children[1].GetCauset().StatsCount())
    32  	impl.cost = selfCost + children[0].GetCost() + children[1].GetCost()
    33  	return impl.cost
    34  }
    35  
    36  // AttachChildren implements Implementation AttachChildren interface.
    37  func (impl *HashJoinImpl) AttachChildren(children ...memo.Implementation) memo.Implementation {
    38  	hashJoin := impl.plan.(*causetembedded.PhysicalHashJoin)
    39  	hashJoin.SetChildren(children[0].GetCauset(), children[1].GetCauset())
    40  	return impl
    41  }
    42  
    43  // NewHashJoinImpl creates a new HashJoinImpl.
    44  func NewHashJoinImpl(hashJoin *causetembedded.PhysicalHashJoin) *HashJoinImpl {
    45  	return &HashJoinImpl{baseImpl{plan: hashJoin}}
    46  }
    47  
    48  // MergeJoinImpl is the implementation for PhysicalMergeJoin.
    49  type MergeJoinImpl struct {
    50  	baseImpl
    51  }
    52  
    53  // CalcCost implements Implementation CalcCost interface.
    54  func (impl *MergeJoinImpl) CalcCost(outCount float64, children ...memo.Implementation) float64 {
    55  	mergeJoin := impl.plan.(*causetembedded.PhysicalMergeJoin)
    56  	// The children here are only used to calculate the cost.
    57  	mergeJoin.SetChildren(children[0].GetCauset(), children[1].GetCauset())
    58  	selfCost := mergeJoin.GetCost(children[0].GetCauset().StatsCount(), children[1].GetCauset().StatsCount())
    59  	impl.cost = selfCost + children[0].GetCost() + children[1].GetCost()
    60  	return impl.cost
    61  }
    62  
    63  // AttachChildren implements Implementation AttachChildren interface.
    64  func (impl *MergeJoinImpl) AttachChildren(children ...memo.Implementation) memo.Implementation {
    65  	mergeJoin := impl.plan.(*causetembedded.PhysicalMergeJoin)
    66  	mergeJoin.SetChildren(children[0].GetCauset(), children[1].GetCauset())
    67  	return impl
    68  }
    69  
    70  // NewMergeJoinImpl creates a new MergeJoinImpl.
    71  func NewMergeJoinImpl(mergeJoin *causetembedded.PhysicalMergeJoin) *MergeJoinImpl {
    72  	return &MergeJoinImpl{baseImpl{plan: mergeJoin}}
    73  }