Repository: pingcap/tidb
File: pkg/statistics/histogram.go (Go)

Code before:
			return DefaultRowEst(0)
		}
	}

	// Step 6: Convert the lower and upper bound of the histogram to scalar value(float64)
	histL := convertDatumToScalar(hg.GetLower(0), commonPrefix)
	histR := convertDatumToScalar(hg.GetUpper(hg.Len()-1), commonPrefix)
	histWidth := histR - histL
	// If we find that the histogram width is too small or too large - we still may need to consider
	// the impact of modifications to the table. Reset the histogram width to 0.
	if histWidth < 0 {
		histWidth = 0
	}
	if math.IsInf(histWidth, 1) {
		histWidth = 0
	}
	boundL := histL - histWidth
	boundR := histR + histWidth

	// Step 7: Calculate the width of the predicate
	// TODO: If predWidth == 0, it may be because the common prefix is too large.
	// In future - out of range for equal predicates should also use this logic
	// for consistency. We need to handle both "equal" and "large common prefix".
	predWidth := r - l
	if predWidth < 0 {
		// This should never happen.
		return DefaultRowEst(0)
	} else if predWidth == 0 {
		// Set histWidth to 0 so that we can still return a minimum of oneValue,
		// and return the max as worst case.
		histWidth = 0
	}

	// Step 8: Calculate the out of range percentages
	// Calculate left overlap percentage if the range overlaps with (boundL, histL)
	leftPercent := calculateLeftOverlapPercent(l, r, boundL, histL, histWidth)
	// Calculate right overlap percentage if the range overlaps with (histR, boundR)
	rightPercent := calculateRightOverlapPercent(l, r, histR, boundR, histWidth)

	totalPercent := min(leftPercent*0.5+rightPercent*0.5, 1.0)
	maxTotalPercent := min(leftPercent+rightPercent, 1.0)

	// Step 9: Calculate the added rows
	// Use absolute value to account for the case where rows may have been added on one side,
	// but deleted from the other, resulting in qualifying out of range rows even though
	// realtimeRowCount is less than histogram count
	addedRows := hg.AbsRowCountDifference(realtimeRowCount)
	maxAddedRows := addedRows

	// Step 10: Calculate the estimated rows
	estRows := oneValue

Review comment (suggestion):
```suggestion
		// This should never happen.
		intest.Assert(false, "Right bound should not be less than left bound")

```

Code after:
			return DefaultRowEst(0)
		}
	}

	// Step 6: Convert the lower and upper bound of the histogram to scalar value(float64)
	histL := convertDatumToScalar(hg.GetLower(0), commonPrefix)
	histR := convertDatumToScalar(hg.GetUpper(hg.Len()-1), commonPrefix)
	histWidth := histR - histL
	// If we find that the histogram width is too small or too large - we still may need to consider
	// the impact of modifications to the table. Reset the histogram width to 0.
	if histWidth < 0 {
		histWidth = 0
	}
	if math.IsInf(histWidth, 1) {
		histWidth = 0
	}
	boundL := histL - histWidth
	boundR := histR + histWidth

	// Step 7: Calculate the width of the predicate
	// TODO: If predWidth == 0, it may be because the common prefix is too large.
	// In future - out of range for equal predicates should also use this logic
	// for consistency. We need to handle both "equal" and "large common prefix".
	predWidth := r - l
	if predWidth < 0 {
		// This should never happen.
		intest.Assert(false, "Right bound should not be less than left bound")

		return DefaultRowEst(0)
	} else if predWidth == 0 {
		// Set histWidth to 0 so that we can still return a minimum of oneValue,
		// and return the max as worst case.
		histWidth = 0
	}

	// Step 8: Calculate the out of range percentages
	// Calculate left overlap percentage if the range overlaps with (boundL, histL)
	leftPercent := calculateLeftOverlapPercent(l, r, boundL, histL, histWidth)
	// Calculate right overlap percentage if the range overlaps with (histR, boundR)
	rightPercent := calculateRightOverlapPercent(l, r, histR, boundR, histWidth)

	totalPercent := min(leftPercent*0.5+rightPercent*0.5, 1.0)
	maxTotalPercent := min(leftPercent+rightPercent, 1.0)

	// Step 9: Calculate the added rows
	// Use absolute value to account for the case where rows may have been added on one side,
	// but deleted from the other, resulting in qualifying out of range rows even though
	// realtimeRowCount is less than histogram count
	addedRows := hg.AbsRowCountDifference(realtimeRowCount)
	maxAddedRows := addedRows