Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros committed Nov 15, 2018
1 parent 2adb8dd commit ea60d70
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions planner/core/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ func (p *LogicalJoin) deriveInnerJoinStatsWithHist(leftKeys, rightKeys []*expres
ndv *= float64(newHist.NDV)
lPosNew := p.schema.ColumnIndex(leftKeys[i])
rPosNew := p.schema.ColumnIndex(rightKeys[i])
cardinality[lPosNew] = ndv
cardinality[rPosNew] = ndv
cardinality[lPosNew] = float64(newHist.NDV)
cardinality[rPosNew] = float64(newHist.NDV)
newColID2Hist[leftKeys[i].UniqueID] = leftCol
newColID2Hist[rightKeys[i].UniqueID] = rightCol
continue
Expand Down
32 changes: 16 additions & 16 deletions statistics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,19 +698,19 @@ func MergeHistogramForInnerJoin(lSide *Histogram, rSide *Histogram, tp *types.Fi
totCnt := int64(0)
totNdv := float64(0)
newHist := NewHistogram(0, 0, 0, 0, tp, 256, 0)
for i, j := 0, 0; i < lSide.Bounds.NumRows() && j < rSide.Bounds.NumRows(); {
lLow := lSide.Bounds.GetRow(i)
lHigh := lSide.Bounds.GetRow(i + 1)
rLow := rSide.Bounds.GetRow(j)
rHigh := rSide.Bounds.GetRow(j + 1)
for i, j := 0, 0; i < lSide.Len() && j < rSide.Len(); {
lLow := lSide.Bounds.GetRow(i * 2)
lHigh := lSide.Bounds.GetRow(i*2 + 1)
rLow := rSide.Bounds.GetRow(j * 2)
rHigh := rSide.Bounds.GetRow(j*2 + 1)
// If [lLow, lHigh] is totally behind [rLow, rHigh], move r point.
if cmpFunc(lLow, 0, rHigh, 0) > 0 {
j += 2
j += 1
continue
}
// If [rLow, rHigh] is totally behind [lLow, lHigh], move l point.
if cmpFunc(rLow, 0, lHigh, 0) > 0 {
i += 2
i += 1
continue
}
var overlapLow, overLapHigh chunk.Row
Expand All @@ -725,14 +725,14 @@ func MergeHistogramForInnerJoin(lSide *Histogram, rSide *Histogram, tp *types.Fi
overLapHigh = lHigh
}
// Calculate overlap ratio.
leftOverLap := lSide.calcRangeFraction(i/2, overlapLow, overLapHigh)
rightOverLap := rSide.calcRangeFraction(j/2, overlapLow, overLapHigh)
lCount := float64(lSide.bucketCount(i/2)) * leftOverLap
rCount := float64(rSide.bucketCount(j/2)) * rightOverLap
leftOverLap := lSide.calcRangeFraction(i, overlapLow, overLapHigh)
rightOverLap := rSide.calcRangeFraction(j, overlapLow, overLapHigh)
lCount := float64(lSide.bucketCount(i)) * leftOverLap
rCount := float64(rSide.bucketCount(j)) * rightOverLap
lNdv := lCount / lAvgPerVal
rNdv := rCount / rAvgPerVal
// bucketCount is lCount/lNdv * rCount/rNdv * finalNdv, where finalNdv is min(lNdv, rNdv).
bucketCount := lCount * rCount / math.Max(lNdv, rNdv)
bucketCount := lAvgPerVal * rAvgPerVal * math.Min(lNdv, rNdv)
// Update histogram.
totCnt += int64(bucketCount)
newHist.Bounds.AppendRow(overlapLow)
Expand All @@ -742,12 +742,12 @@ func MergeHistogramForInnerJoin(lSide *Histogram, rSide *Histogram, tp *types.Fi
// Move i and j by compare result.
switch cmpFunc(lHigh, 0, rHigh, 0) {
case -1:
i += 2
i += 1
case 0:
i += 2
j += 2
i += 1
j += 1
case 1:
j += 2
j += 1
}
}
newHist.NDV = int64(totNdv)
Expand Down

0 comments on commit ea60d70

Please sign in to comment.