Skip to content

Commit

Permalink
planner: make logical join's elements all public for later pkg move (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
AilinKid authored Jun 20, 2024
1 parent 6ad9cac commit 4f3aba8
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 173 deletions.
54 changes: 27 additions & 27 deletions pkg/planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ func (*LogicalJoin) checkJoinKeyCollation(leftKeys, rightKeys []*expression.Colu

// GetMergeJoin convert the logical join to physical merge join based on the physical property.
func (p *LogicalJoin) GetMergeJoin(prop *property.PhysicalProperty, schema *expression.Schema, statsInfo *property.StatsInfo, leftStatsInfo *property.StatsInfo, rightStatsInfo *property.StatsInfo) []base.PhysicalPlan {
joins := make([]base.PhysicalPlan, 0, len(p.leftProperties)+1)
// The leftProperties caches all the possible properties that are provided by its children.
joins := make([]base.PhysicalPlan, 0, len(p.LeftProperties)+1)
// The LeftProperties caches all the possible properties that are provided by its children.
leftJoinKeys, rightJoinKeys, isNullEQ, hasNullEQ := p.GetJoinKeys()

// EnumType/SetType Unsupported: merge join conflicts with index order.
Expand All @@ -182,7 +182,7 @@ func (p *LogicalJoin) GetMergeJoin(prop *property.PhysicalProperty, schema *expr
if hasNullEQ {
return nil
}
for _, lhsChildProperty := range p.leftProperties {
for _, lhsChildProperty := range p.LeftProperties {
offsets := getMaxSortPrefix(lhsChildProperty, leftJoinKeys)
// If not all equal conditions hit properties. We ban merge join heuristically. Because in this case, merge join
// may get a very low performance. In executor, executes join results before other conditions filter it.
Expand All @@ -197,7 +197,7 @@ func (p *LogicalJoin) GetMergeJoin(prop *property.PhysicalProperty, schema *expr
newIsNullEQ = append(newIsNullEQ, isNullEQ[offset])
}

prefixLen := findMaxPrefixLen(p.rightProperties, rightKeys)
prefixLen := findMaxPrefixLen(p.RightProperties, rightKeys)
if prefixLen == 0 {
continue
}
Expand Down Expand Up @@ -236,8 +236,8 @@ func (p *LogicalJoin) GetMergeJoin(prop *property.PhysicalProperty, schema *expr
}
}

if p.preferJoinType&h.PreferNoMergeJoin > 0 {
if p.preferJoinType&h.PreferMergeJoin == 0 {
if p.PreferJoinType&h.PreferNoMergeJoin > 0 {
if p.PreferJoinType&h.PreferMergeJoin == 0 {
return nil
}
p.SCtx().GetSessionVars().StmtCtx.SetHintWarning(
Expand All @@ -246,7 +246,7 @@ func (p *LogicalJoin) GetMergeJoin(prop *property.PhysicalProperty, schema *expr

// If TiDB_SMJ hint is existed, it should consider enforce merge join,
// because we can't trust lhsChildProperty completely.
if (p.preferJoinType&h.PreferMergeJoin) > 0 ||
if (p.PreferJoinType&h.PreferMergeJoin) > 0 ||
p.shouldSkipHashJoin() { // if hash join is not allowed, generate as many other types of join as possible to avoid 'cant-find-plan' error.
joins = append(joins, p.getEnforcedMergeJoin(prop, schema, statsInfo)...)
}
Expand Down Expand Up @@ -388,16 +388,16 @@ func (p *PhysicalMergeJoin) initCompareFuncs() {
}

func (p *LogicalJoin) shouldSkipHashJoin() bool {
return (p.preferJoinType&h.PreferNoHashJoin) > 0 || (p.SCtx().GetSessionVars().DisableHashJoin)
return (p.PreferJoinType&h.PreferNoHashJoin) > 0 || (p.SCtx().GetSessionVars().DisableHashJoin)
}

func (p *LogicalJoin) getHashJoins(prop *property.PhysicalProperty) (joins []base.PhysicalPlan, forced bool) {
if !prop.IsSortItemEmpty() { // hash join doesn't promise any orders
return
}

forceLeftToBuild := ((p.preferJoinType & h.PreferLeftAsHJBuild) > 0) || ((p.preferJoinType & h.PreferRightAsHJProbe) > 0)
forceRightToBuild := ((p.preferJoinType & h.PreferRightAsHJBuild) > 0) || ((p.preferJoinType & h.PreferLeftAsHJProbe) > 0)
forceLeftToBuild := ((p.PreferJoinType & h.PreferLeftAsHJBuild) > 0) || ((p.PreferJoinType & h.PreferRightAsHJProbe) > 0)
forceRightToBuild := ((p.PreferJoinType & h.PreferRightAsHJBuild) > 0) || ((p.PreferJoinType & h.PreferLeftAsHJProbe) > 0)
if forceLeftToBuild && forceRightToBuild {
p.SCtx().GetSessionVars().StmtCtx.SetHintWarning("Some HASH_JOIN_BUILD and HASH_JOIN_PROBE hints are conflicts, please check the hints")
forceLeftToBuild = false
Expand Down Expand Up @@ -439,7 +439,7 @@ func (p *LogicalJoin) getHashJoins(prop *property.PhysicalProperty) (joins []bas
}
}

forced = (p.preferJoinType&h.PreferHashJoin > 0) || forceLeftToBuild || forceRightToBuild
forced = (p.PreferJoinType&h.PreferHashJoin > 0) || forceLeftToBuild || forceRightToBuild
if !forced && p.shouldSkipHashJoin() {
return nil, false
} else if forced && p.shouldSkipHashJoin() {
Expand Down Expand Up @@ -589,7 +589,7 @@ func (p *LogicalJoin) constructIndexMergeJoin(
compareFilters *ColWithCmpFuncManager,
) []base.PhysicalPlan {
hintExists := false
if (outerIdx == 1 && (p.preferJoinType&h.PreferLeftAsINLMJInner) > 0) || (outerIdx == 0 && (p.preferJoinType&h.PreferRightAsINLMJInner) > 0) {
if (outerIdx == 1 && (p.PreferJoinType&h.PreferLeftAsINLMJInner) > 0) || (outerIdx == 0 && (p.PreferJoinType&h.PreferRightAsINLMJInner) > 0) {
hintExists = true
}
indexJoins := p.constructIndexJoin(prop, outerIdx, innerTask, ranges, keyOff2IdxOff, path, compareFilters, !hintExists)
Expand Down Expand Up @@ -737,7 +737,7 @@ func (p *LogicalJoin) getIndexJoinByOuterIdx(prop *property.PhysicalProperty, ou

var avgInnerRowCnt float64
if outerChild.StatsInfo().RowCount > 0 {
avgInnerRowCnt = p.equalCondOutCnt / outerChild.StatsInfo().RowCount
avgInnerRowCnt = p.EqualCondOutCnt / outerChild.StatsInfo().RowCount
}
joins = p.buildIndexJoinInner2TableScan(prop, innerChildWrapper, innerJoinKeys, outerJoinKeys, outerIdx, avgInnerRowCnt)
if joins != nil {
Expand Down Expand Up @@ -2097,7 +2097,7 @@ func filterIndexJoinBySessionVars(sc base.PlanContext, indexJoins []base.Physica

func (p *LogicalJoin) preferAny(joinFlags ...uint) bool {
for _, flag := range joinFlags {
if p.preferJoinType&flag > 0 {
if p.PreferJoinType&flag > 0 {
return true
}
}
Expand Down Expand Up @@ -2229,8 +2229,8 @@ func (p *LogicalJoin) handleForceIndexJoinHints(prop *property.PhysicalProperty,
// So we only need to generate warning message when the property is empty.
if prop.IsSortItemEmpty() {
var indexJoinTables, indexHashJoinTables, indexMergeJoinTables []h.HintedTable
if p.hintInfo != nil {
t := p.hintInfo.IndexJoin
if p.HintInfo != nil {
t := p.HintInfo.IndexJoin
indexJoinTables, indexHashJoinTables, indexMergeJoinTables = t.INLJTables, t.INLHJTables, t.INLMJTables
}
var errMsg string
Expand Down Expand Up @@ -2389,11 +2389,11 @@ func (p *LogicalJoin) ExhaustPhysicalPlans(prop *property.PhysicalProperty) ([]b
}
})

if !isJoinHintSupportedInMPPMode(p.preferJoinType) {
if hasMPPJoinHints(p.preferJoinType) {
if !isJoinHintSupportedInMPPMode(p.PreferJoinType) {
if hasMPPJoinHints(p.PreferJoinType) {
// If there are MPP hints but has some conflicts join method hints, all the join hints are invalid.
p.SCtx().GetSessionVars().StmtCtx.SetHintWarning("The MPP join hints are in conflict, and you can only specify join method hints that are currently supported by MPP mode now")
p.preferJoinType = 0
p.PreferJoinType = 0
} else {
// If there are no MPP hints but has some conflicts join method hints, the MPP mode will be blocked.
p.SCtx().GetSessionVars().RaiseWarningWhenMPPEnforced("MPP mode may be blocked because you have used hint to specify a join algorithm which is not supported by mpp now.")
Expand All @@ -2408,12 +2408,12 @@ func (p *LogicalJoin) ExhaustPhysicalPlans(prop *property.PhysicalProperty) ([]b
joins := make([]base.PhysicalPlan, 0, 8)
canPushToTiFlash := p.CanPushToCop(kv.TiFlash)
if p.SCtx().GetSessionVars().IsMPPAllowed() && canPushToTiFlash {
if (p.preferJoinType & h.PreferShuffleJoin) > 0 {
if (p.PreferJoinType & h.PreferShuffleJoin) > 0 {
if shuffleJoins := p.tryToGetMppHashJoin(prop, false); len(shuffleJoins) > 0 {
return shuffleJoins, true, nil
}
}
if (p.preferJoinType & h.PreferBCJoin) > 0 {
if (p.PreferJoinType & h.PreferBCJoin) > 0 {
if bcastJoins := p.tryToGetMppHashJoin(prop, true); len(bcastJoins) > 0 {
return bcastJoins, true, nil
}
Expand All @@ -2428,11 +2428,11 @@ func (p *LogicalJoin) ExhaustPhysicalPlans(prop *property.PhysicalProperty) ([]b
} else {
hasMppHints := false
var errMsg string
if (p.preferJoinType & h.PreferShuffleJoin) > 0 {
if (p.PreferJoinType & h.PreferShuffleJoin) > 0 {
errMsg = "The join can not push down to the MPP side, the shuffle_join() hint is invalid"
hasMppHints = true
}
if (p.preferJoinType & h.PreferBCJoin) > 0 {
if (p.PreferJoinType & h.PreferBCJoin) > 0 {
errMsg = "The join can not push down to the MPP side, the broadcast_join() hint is invalid"
hasMppHints = true
}
Expand All @@ -2447,7 +2447,7 @@ func (p *LogicalJoin) ExhaustPhysicalPlans(prop *property.PhysicalProperty) ([]b
if !p.isNAAJ() {
// naaj refuse merge join and index join.
mergeJoins := p.GetMergeJoin(prop, p.Schema(), p.StatsInfo(), p.Children()[0].StatsInfo(), p.Children()[1].StatsInfo())
if (p.preferJoinType&h.PreferMergeJoin) > 0 && len(mergeJoins) > 0 {
if (p.PreferJoinType&h.PreferMergeJoin) > 0 && len(mergeJoins) > 0 {
return mergeJoins, true, nil
}
joins = append(joins, mergeJoins...)
Expand All @@ -2465,7 +2465,7 @@ func (p *LogicalJoin) ExhaustPhysicalPlans(prop *property.PhysicalProperty) ([]b
}
joins = append(joins, hashJoins...)

if p.preferJoinType > 0 {
if p.PreferJoinType > 0 {
// If we reach here, it means we have a hint that doesn't work.
// It might be affected by the required property, so we enforce
// this property and try the hint again.
Expand Down Expand Up @@ -2556,8 +2556,8 @@ func (p *LogicalJoin) tryToGetMppHashJoin(prop *property.PhysicalProperty, useBC
RightNAJoinKeys: rNAKeys,
}
// It indicates which side is the build side.
forceLeftToBuild := ((p.preferJoinType & h.PreferLeftAsHJBuild) > 0) || ((p.preferJoinType & h.PreferRightAsHJProbe) > 0)
forceRightToBuild := ((p.preferJoinType & h.PreferRightAsHJBuild) > 0) || ((p.preferJoinType & h.PreferLeftAsHJProbe) > 0)
forceLeftToBuild := ((p.PreferJoinType & h.PreferLeftAsHJBuild) > 0) || ((p.PreferJoinType & h.PreferRightAsHJProbe) > 0)
forceRightToBuild := ((p.PreferJoinType & h.PreferRightAsHJBuild) > 0) || ((p.PreferJoinType & h.PreferLeftAsHJProbe) > 0)
if forceLeftToBuild && forceRightToBuild {
p.SCtx().GetSessionVars().StmtCtx.SetHintWarning(
"Some HASH_JOIN_BUILD and HASH_JOIN_PROBE hints are conflicts, please check the hints")
Expand Down
6 changes: 3 additions & 3 deletions pkg/planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2471,13 +2471,13 @@ func findFieldNameFromNaturalUsingJoin(p base.LogicalPlan, v *ast.ColumnName) (c
case *LogicalLimit, *LogicalSelection, *LogicalTopN, *LogicalSort, *LogicalMaxOneRow:
return findFieldNameFromNaturalUsingJoin(p.Children()[0], v)
case *LogicalJoin:
if x.fullSchema != nil {
idx, err := expression.FindFieldName(x.fullNames, v)
if x.FullSchema != nil {
idx, err := expression.FindFieldName(x.FullNames, v)
if err != nil {
return nil, nil, err
}
if idx >= 0 {
return x.fullSchema.Columns[idx], x.fullNames[idx], nil
return x.FullSchema.Columns[idx], x.FullNames[idx], nil
}
}
}
Expand Down
Loading

0 comments on commit 4f3aba8

Please sign in to comment.