New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stats: refine estimate in buckets #4601
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a567621
stats: refine estimate in buckets
9d6d88c
tiny change
ee6172c
address comment
b27ef49
fix test
d23ccb9
remove useless variable
087e3c4
check inf
540be22
optimize code
737f9f0
address comment
98d8818
address comment
3558f2c
Merge branch 'master' into xhb/est
alivxxx File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2017 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package statistics | ||
|
||
import ( | ||
"encoding/binary" | ||
"math" | ||
|
||
"github.com/pingcap/tidb/util/types" | ||
) | ||
|
||
// calcFraction is used to calculate the fraction of the interval [lower, upper] that lies within the [lower, value] | ||
// using the continuous-value assumption. | ||
func calcFraction(lower, upper, value *types.Datum) float64 { | ||
lowerScalar, upperScalar, valueScalar := convertToScalar(lower, upper, value) | ||
if upperScalar <= lowerScalar { | ||
return 0.5 | ||
} | ||
if valueScalar <= lowerScalar { | ||
return 0 | ||
} | ||
if valueScalar >= upperScalar { | ||
return 1 | ||
} | ||
frac := (valueScalar - lowerScalar) / (upperScalar - lowerScalar) | ||
if math.IsNaN(frac) || math.IsInf(frac, 0) || frac < 0 || frac > 1 { | ||
return 0.5 | ||
} | ||
return frac | ||
} | ||
|
||
// convertToScalar converts the datum to scalar values. | ||
// TODO: We may cache results for some types. | ||
func convertToScalar(lower, upper, value *types.Datum) (float64, float64, float64) { | ||
switch value.Kind() { | ||
case types.KindFloat32: | ||
return float64(lower.GetFloat32()), float64(upper.GetFloat32()), float64(value.GetFloat32()) | ||
case types.KindFloat64: | ||
return lower.GetFloat64(), upper.GetFloat64(), value.GetFloat64() | ||
case types.KindInt64: | ||
return float64(lower.GetInt64()), float64(upper.GetInt64()), float64(value.GetInt64()) | ||
case types.KindUint64: | ||
return float64(lower.GetUint64()), float64(upper.GetUint64()), float64(value.GetUint64()) | ||
case types.KindMysqlDecimal: | ||
return convertDecimalToScalar(lower, upper, value) | ||
case types.KindMysqlDuration: | ||
return float64(lower.GetMysqlDuration().Duration), float64(upper.GetMysqlDuration().Duration), float64(value.GetMysqlDuration().Duration) | ||
case types.KindMysqlTime: | ||
lowerTime := lower.GetMysqlTime() | ||
upperTime := upper.GetMysqlTime() | ||
valueTime := value.GetMysqlTime() | ||
return 0, float64(upperTime.Sub(&lowerTime).Duration), float64(valueTime.Sub(&lowerTime).Duration) | ||
case types.KindString, types.KindBytes: | ||
return convertBytesToScalar(lower.GetBytes(), upper.GetBytes(), value.GetBytes()) | ||
default: | ||
// do not know how to convert | ||
return 0, 0, 0 | ||
} | ||
} | ||
|
||
// Decimal types are simply converted to their equivalent float64 values. | ||
func convertDecimalToScalar(lower, upper, value *types.Datum) (float64, float64, float64) { | ||
lowerScalar, err := lower.GetMysqlDecimal().ToFloat64() | ||
if err != nil { | ||
return 0, 0, 0 | ||
} | ||
upperScalar, err := upper.GetMysqlDecimal().ToFloat64() | ||
if err != nil { | ||
return 0, 0, 0 | ||
} | ||
valueScalar, err := value.GetMysqlDecimal().ToFloat64() | ||
if err != nil { | ||
return 0, 0, 0 | ||
} | ||
return lowerScalar, upperScalar, valueScalar | ||
} | ||
|
||
// Bytes type is viewed as a base-256 value. | ||
func convertBytesToScalar(lower, upper, value []byte) (float64, float64, float64) { | ||
minLen := len(lower) | ||
if len(upper) < minLen { | ||
minLen = len(upper) | ||
} | ||
if len(value) < minLen { | ||
minLen = len(value) | ||
} | ||
// remove their common prefix | ||
common := 0 | ||
for common < minLen { | ||
if lower[common] == upper[common] && lower[common] == value[common] { | ||
common++ | ||
} else { | ||
break | ||
} | ||
} | ||
return convertOneBytesToScalar(lower[common:]), convertOneBytesToScalar(upper[common:]), convertOneBytesToScalar(value[common:]) | ||
} | ||
|
||
func convertOneBytesToScalar(value []byte) float64 { | ||
// Since the base is 256, we only consider at most 8 bytes. | ||
var buf [8]byte | ||
copy(buf[:], value) | ||
return float64(binary.BigEndian.Uint64(buf[:])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright 2017 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package statistics | ||
|
||
import ( | ||
"math" | ||
|
||
. "github.com/pingcap/check" | ||
"github.com/pingcap/tidb/mysql" | ||
"github.com/pingcap/tidb/util/types" | ||
) | ||
|
||
const eps = 1e-9 | ||
|
||
func getDecimal(value float64) *types.MyDecimal { | ||
dec := &types.MyDecimal{} | ||
dec.FromFloat64(value) | ||
return dec | ||
} | ||
|
||
func getDuration(value string) types.Duration { | ||
dur, _ := types.ParseDuration(value, 0) | ||
return dur | ||
} | ||
|
||
func getTime(value string) types.Time { | ||
t, _ := types.ParseTime(value, mysql.TypeDate, 0) | ||
return t | ||
} | ||
|
||
func getBinaryLiteral(value string) types.BinaryLiteral { | ||
b, _ := types.ParseBitStr(value) | ||
return b | ||
} | ||
|
||
func (t *testStatisticsSuite) TestCalcFraction(c *C) { | ||
tests := []struct { | ||
lower types.Datum | ||
upper types.Datum | ||
value types.Datum | ||
fraction float64 | ||
}{ | ||
{ | ||
lower: types.NewIntDatum(0), | ||
upper: types.NewIntDatum(4), | ||
value: types.NewIntDatum(1), | ||
fraction: 0.25, | ||
}, | ||
{ | ||
lower: types.NewIntDatum(0), | ||
upper: types.NewIntDatum(4), | ||
value: types.NewIntDatum(4), | ||
fraction: 1, | ||
}, | ||
{ | ||
lower: types.NewIntDatum(0), | ||
upper: types.NewIntDatum(4), | ||
value: types.NewIntDatum(-1), | ||
fraction: 0, | ||
}, | ||
{ | ||
lower: types.NewUintDatum(0), | ||
upper: types.NewUintDatum(4), | ||
value: types.NewUintDatum(1), | ||
fraction: 0.25, | ||
}, | ||
{ | ||
lower: types.NewFloat64Datum(0), | ||
upper: types.NewFloat64Datum(4), | ||
value: types.NewFloat64Datum(1), | ||
fraction: 0.25, | ||
}, | ||
{ | ||
lower: types.NewFloat32Datum(0), | ||
upper: types.NewFloat32Datum(4), | ||
value: types.NewFloat32Datum(1), | ||
fraction: 0.25, | ||
}, | ||
{ | ||
lower: types.NewDecimalDatum(getDecimal(0)), | ||
upper: types.NewDecimalDatum(getDecimal(4)), | ||
value: types.NewDecimalDatum(getDecimal(1)), | ||
fraction: 0.25, | ||
}, | ||
{ | ||
lower: types.NewMysqlBitDatum(getBinaryLiteral("0b0")), | ||
upper: types.NewMysqlBitDatum(getBinaryLiteral("0b100")), | ||
value: types.NewMysqlBitDatum(getBinaryLiteral("0b1")), | ||
fraction: 0.5, | ||
}, | ||
{ | ||
lower: types.NewDurationDatum(getDuration("0:00:00")), | ||
upper: types.NewDurationDatum(getDuration("4:00:00")), | ||
value: types.NewDurationDatum(getDuration("1:00:00")), | ||
fraction: 0.25, | ||
}, | ||
{ | ||
lower: types.NewTimeDatum(getTime("2017-01-01")), | ||
upper: types.NewTimeDatum(getTime("2017-04-01")), | ||
value: types.NewTimeDatum(getTime("2017-02-01")), | ||
fraction: 0.34444444444444444, | ||
}, | ||
{ | ||
lower: types.NewStringDatum("aasad"), | ||
upper: types.NewStringDatum("addad"), | ||
value: types.NewStringDatum("abfsd"), | ||
fraction: 0.32280253984063745, | ||
}, | ||
{ | ||
lower: types.NewBytesDatum([]byte("aasad")), | ||
upper: types.NewBytesDatum([]byte("asdff")), | ||
value: types.NewBytesDatum([]byte("abfsd")), | ||
fraction: 0.0529216802217269, | ||
}, | ||
} | ||
for _, test := range tests { | ||
fraction := calcFraction(&test.lower, &test.upper, &test.value) | ||
c.Check(math.Abs(fraction-test.fraction) < eps, IsTrue) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is complex, how about using
BigEndian.Uint64
?