|
| 1 | +package cost |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/efritz/gostgres/internal/execution/expressions" |
| 5 | + "github.com/efritz/gostgres/internal/shared/impls" |
| 6 | + "github.com/efritz/gostgres/internal/shared/ordering" |
| 7 | + "github.com/efritz/gostgres/internal/shared/rows" |
| 8 | +) |
| 9 | + |
| 10 | +const defaultSelectivity = 0.7 // TODO |
| 11 | + |
| 12 | +func estimateExpressionSelectivity(cond impls.Expression, rowCount int, columnStatistics []impls.ColumnStatistics) (s float64) { |
| 13 | + // fmt.Printf("ESTIMATING %s -> \n", cond) |
| 14 | + // defer func() { fmt.Printf("ESTIMATING %s -> %.12f\n", cond, s) }() |
| 15 | + |
| 16 | + if cond == nil { |
| 17 | + return 1.0 |
| 18 | + } |
| 19 | + |
| 20 | + if expressions.IsConjunction(cond) { |
| 21 | + selectivity := 1.0 |
| 22 | + for _, conjunction := range expressions.Conjunctions(cond) { |
| 23 | + selectivity *= estimateExpressionSelectivity(conjunction, rowCount, columnStatistics) |
| 24 | + } |
| 25 | + |
| 26 | + return selectivity |
| 27 | + } |
| 28 | + |
| 29 | + if expressions.IsDisjunction(cond) { |
| 30 | + selectivity := 1.0 |
| 31 | + for _, disjunction := range expressions.Disjunctions(cond) { |
| 32 | + selectivity *= 1 - estimateExpressionSelectivity(disjunction, rowCount, columnStatistics) |
| 33 | + } |
| 34 | + |
| 35 | + return 1 - selectivity |
| 36 | + } |
| 37 | + |
| 38 | + if inner, ok := expressions.IsNegation(cond); ok { |
| 39 | + return 1 - estimateExpressionSelectivity(inner, rowCount, columnStatistics) |
| 40 | + } |
| 41 | + |
| 42 | + comparisonType, left, right := expressions.IsComparison(cond) |
| 43 | + if comparisonType == expressions.ComparisonTypeUnknown { |
| 44 | + return defaultSelectivity |
| 45 | + } |
| 46 | + |
| 47 | + if _, ok := findStatisticsForColumn(columnStatistics, right); ok { |
| 48 | + left, right = right, left |
| 49 | + comparisonType = comparisonType.Flip() |
| 50 | + } |
| 51 | + |
| 52 | + stats, ok := findStatisticsForColumn(columnStatistics, left) |
| 53 | + if !ok { |
| 54 | + return defaultSelectivity |
| 55 | + } |
| 56 | + |
| 57 | + value, err := right.ValueFrom(impls.EmptyExecutionContext, rows.Row{}) |
| 58 | + if err != nil { |
| 59 | + return defaultSelectivity |
| 60 | + } |
| 61 | + |
| 62 | + switch comparisonType { |
| 63 | + case expressions.ComparisonTypeEquals: |
| 64 | + return estimateEqualitySelectivity(stats, rowCount, value) |
| 65 | + |
| 66 | + case |
| 67 | + expressions.ComparisonTypeLessThan, |
| 68 | + expressions.ComparisonTypeLessThanEquals, |
| 69 | + expressions.ComparisonTypeGreaterThan, |
| 70 | + expressions.ComparisonTypeGreaterThanEquals: |
| 71 | + return estimateComparisonSelectivity(stats, rowCount, comparisonType, value) |
| 72 | + } |
| 73 | + |
| 74 | + return defaultSelectivity |
| 75 | +} |
| 76 | + |
| 77 | +func findStatisticsForColumn(columnStatistics []impls.ColumnStatistics, expr impls.Expression) (impls.ColumnStatistics, bool) { |
| 78 | + for _, columnStats := range columnStatistics { |
| 79 | + // TODO - better way to determine field |
| 80 | + if expr.Equal(expressions.NewNamed(columnStats.Field)) { |
| 81 | + return columnStats, true |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return impls.ColumnStatistics{}, false |
| 86 | +} |
| 87 | + |
| 88 | +func estimateEqualitySelectivity(columnStatistics impls.ColumnStatistics, rowCount int, value any) float64 { |
| 89 | + nonNullFraction := 1 - columnStatistics.NullFraction |
| 90 | + |
| 91 | + for _, mcv := range columnStatistics.MostCommonValues { |
| 92 | + if ordering.CompareValues(mcv.Value, value) == ordering.OrderTypeEqual { |
| 93 | + return mcv.Frequency * nonNullFraction |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + remainingFrequency := 1.0 |
| 98 | + for _, mcv := range columnStatistics.MostCommonValues { |
| 99 | + remainingFrequency -= mcv.Frequency |
| 100 | + } |
| 101 | + |
| 102 | + distinctCount := columnStatistics.DistinctFraction * float64(rowCount) |
| 103 | + remainingDistinctCount := distinctCount - float64(len(columnStatistics.MostCommonValues)) |
| 104 | + if remainingDistinctCount == 0 { |
| 105 | + return 0 |
| 106 | + } |
| 107 | + |
| 108 | + return (remainingFrequency / remainingDistinctCount) * nonNullFraction |
| 109 | +} |
| 110 | + |
| 111 | +func estimateComparisonSelectivity(columnStatistics impls.ColumnStatistics, rowCount int, comparisonType expressions.ComparisonType, value any) float64 { |
| 112 | + bucketIndex := len(columnStatistics.HistogramBounds) |
| 113 | + for i, bound := range columnStatistics.HistogramBounds { |
| 114 | + if cmp := ordering.CompareValues(value, bound); cmp == ordering.OrderTypeBefore || cmp == ordering.OrderTypeEqual { |
| 115 | + bucketIndex = i |
| 116 | + break |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + cumulativeHistogramFrequency := float64(bucketIndex) / float64(len(columnStatistics.HistogramBounds)) |
| 121 | + if comparisonType == expressions.ComparisonTypeGreaterThan || comparisonType == expressions.ComparisonTypeGreaterThanEquals { |
| 122 | + cumulativeHistogramFrequency = 1 - cumulativeHistogramFrequency |
| 123 | + } |
| 124 | + |
| 125 | + mcvAdjustment := 0.0 |
| 126 | + for _, mcv := range columnStatistics.MostCommonValues { |
| 127 | + if matchOrderWithComparisonType(mcv.Value, value, comparisonType) { // TODO - is this order correct? |
| 128 | + mcvAdjustment += mcv.Frequency |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + nonNullFraction := 1 - columnStatistics.NullFraction |
| 133 | + return (cumulativeHistogramFrequency + mcvAdjustment) * nonNullFraction |
| 134 | +} |
| 135 | + |
| 136 | +func matchOrderWithComparisonType(left, right any, comparisonType expressions.ComparisonType) bool { |
| 137 | + switch ordering.CompareValues(left, right) { |
| 138 | + case ordering.OrderTypeBefore: |
| 139 | + return comparisonType == expressions.ComparisonTypeLessThan || comparisonType == expressions.ComparisonTypeLessThanEquals |
| 140 | + |
| 141 | + case ordering.OrderTypeEqual: |
| 142 | + return comparisonType == expressions.ComparisonTypeLessThanEquals || comparisonType == expressions.ComparisonTypeGreaterThanEquals |
| 143 | + |
| 144 | + case ordering.OrderTypeAfter: |
| 145 | + return comparisonType == expressions.ComparisonTypeGreaterThan || comparisonType == expressions.ComparisonTypeGreaterThanEquals |
| 146 | + } |
| 147 | + |
| 148 | + return false |
| 149 | +} |
0 commit comments