-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
server: Make float rounding compatible with mysql for types: float, float(m,n) and double(m,n) #22823
Open
johan-j
wants to merge
7
commits into
pingcap:master
Choose a base branch
from
johan-j:issue22791
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+202
−102
Open
server: Make float rounding compatible with mysql for types: float, float(m,n) and double(m,n) #22823
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d3091ae
executioner: Fix rouding issues for float and custom colWidth
johan-j fb02499
Merge branch 'master' into issue22791
johan-j e7190ef
Server: update function description for string formatter
johan-j 44cceda
Merge branch 'master' into issue22791
johan-j db25742
Merge branch 'master' into issue22791
AilinKid ef346f9
Merge branch 'master' into issue22791
johan-j 7095725
Merge branch 'master' into issue22791
AilinKid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 | ||||
---|---|---|---|---|---|---|
|
@@ -37,10 +37,12 @@ package server | |||||
import ( | ||||||
"bytes" | ||||||
"encoding/binary" | ||||||
"fmt" | ||||||
"io" | ||||||
"math" | ||||||
"net/http" | ||||||
"strconv" | ||||||
"strings" | ||||||
"time" | ||||||
|
||||||
"github.com/pingcap/parser/mysql" | ||||||
|
@@ -307,18 +309,10 @@ func dumpTextRow(buffer []byte, columns []*ColumnInfo, row chunk.Row) ([]byte, e | |||||
} | ||||||
buffer = dumpLengthEncodedString(buffer, tmp) | ||||||
case mysql.TypeFloat: | ||||||
prec := -1 | ||||||
if columns[i].Decimal > 0 && int(col.Decimal) != mysql.NotFixedDec && col.Table == "" { | ||||||
prec = int(col.Decimal) | ||||||
} | ||||||
tmp = appendFormatFloat(tmp[:0], float64(row.GetFloat32(i)), prec, 32) | ||||||
tmp = appendFormatFloat(tmp[:0], float64(row.GetFloat32(i)), 32, col.Decimal, col.ColumnLength, col.Table == "") | ||||||
buffer = dumpLengthEncodedString(buffer, tmp) | ||||||
case mysql.TypeDouble: | ||||||
prec := types.UnspecifiedLength | ||||||
if col.Decimal > 0 && int(col.Decimal) != mysql.NotFixedDec && col.Table == "" { | ||||||
prec = int(col.Decimal) | ||||||
} | ||||||
tmp = appendFormatFloat(tmp[:0], row.GetFloat64(i), prec, 64) | ||||||
tmp = appendFormatFloat(tmp[:0], row.GetFloat64(i), 64, col.Decimal, col.ColumnLength, col.Table == "") | ||||||
buffer = dumpLengthEncodedString(buffer, tmp) | ||||||
case mysql.TypeNewDecimal: | ||||||
buffer = dumpLengthEncodedString(buffer, hack.Slice(row.GetMyDecimal(i).String())) | ||||||
|
@@ -359,52 +353,118 @@ func lengthEncodedIntSize(n uint64) int { | |||||
} | ||||||
|
||||||
const ( | ||||||
expFormatBig = 1e15 | ||||||
expFormatSmall = 1e-15 | ||||||
defaultMySQLPrec = 5 | ||||||
expFormatBig = 1e15 | ||||||
expFormatSmall = 1e-15 | ||||||
defaultMySQLPrec = 5 | ||||||
defaultMySQLPrec64 = 16 | ||||||
) | ||||||
|
||||||
func appendFormatFloat(in []byte, fVal float64, prec, bitSize int) []byte { | ||||||
/* | ||||||
Formats a float value into a string (byte slice) and returns. | ||||||
|
||||||
The rounding/formatting is split into the following cases: | ||||||
1. Scientific notation (1.23.e4): Used for big numbers except when value comes from a custom width column or from the select part of the query | ||||||
2. mysql.TypeDouble: return string with as much precision as needed (precision = -1) | ||||||
3. mysql.TypeFloat: return a rounded value that corresponds to mysql's (precision = 5) | ||||||
4. mysql.TypeDouble(m,n): | ||||||
4.1 If the columnLength < defaultMySQLPrec64, return string with as much precision as needed (precision = n) | ||||||
4.2 else: return rounded string to precision defaultMySQLPrec64, and padded with 0's. | ||||||
5. mysql.TypeFloat(m,n): return a rounded value rounded with (precision = 5) and formatted with (precision = n) | ||||||
|
||||||
Note: Custom float/double column width is deprecated in mysql 8.0.14. | ||||||
However for compatibility, we still try to make the output the same as mysql for these types. | ||||||
*/ | ||||||
func appendFormatFloat(in []byte, fVal float64, bitSize int, colDecimal uint8, colLength uint32, isFromSelect bool) []byte { | ||||||
isCustomColumnWidth := false | ||||||
prec := types.UnspecifiedLength | ||||||
if colDecimal > 0 && colDecimal != mysql.NotFixedDec { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
prec = int(colDecimal) | ||||||
isCustomColumnWidth = true | ||||||
} | ||||||
|
||||||
absVal := math.Abs(fVal) | ||||||
if absVal > math.MaxFloat64 || math.IsNaN(absVal) { | ||||||
return []byte{'0'} | ||||||
} | ||||||
isEFormat := false | ||||||
if bitSize == 32 { | ||||||
isEFormat = float32(absVal) >= expFormatBig || (float32(absVal) != 0 && float32(absVal) < expFormatSmall) | ||||||
} else { | ||||||
isEFormat = absVal >= expFormatBig || (absVal != 0 && absVal < expFormatSmall) | ||||||
} | ||||||
var out []byte | ||||||
if isEFormat { | ||||||
// Scientific format (1.2e3) should be used for big numbers. However, it is never used for columns with custom length/decimal | ||||||
if !isCustomColumnWidth || isFromSelect { | ||||||
if bitSize == 32 { | ||||||
prec = defaultMySQLPrec | ||||||
isEFormat = float32(absVal) >= expFormatBig || (float32(absVal) != 0 && float32(absVal) < expFormatSmall) | ||||||
} else { | ||||||
isEFormat = absVal >= expFormatBig || (absVal != 0 && absVal < expFormatSmall) | ||||||
} | ||||||
out = strconv.AppendFloat(in, fVal, 'e', prec, bitSize) | ||||||
valStr := out[len(in):] | ||||||
// remove the '+' from the string for compatibility. | ||||||
plusPos := bytes.IndexByte(valStr, '+') | ||||||
if plusPos > 0 { | ||||||
plusPosInOut := len(in) + plusPos | ||||||
out = append(out[:plusPosInOut], out[plusPosInOut+1:]...) | ||||||
} | ||||||
|
||||||
switch { | ||||||
case isEFormat: | ||||||
return formatFloatScientificNotation(in, fVal, prec, bitSize) | ||||||
case bitSize == 32 && isCustomColumnWidth: | ||||||
return strconv.AppendFloat(in, fVal, 'f', prec, bitSize) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's same as the default logic |
||||||
case bitSize == 32: | ||||||
roundedValue := roundFloatToPrecision(fVal, defaultMySQLPrec) | ||||||
return strconv.AppendFloat(in, roundedValue, 'f', -1, bitSize) | ||||||
case isCustomColumnWidth && colLength > defaultMySQLPrec64: | ||||||
roundedValue := roundFloatToPrecision(fVal, defaultMySQLPrec64) | ||||||
valueStr := strconv.FormatFloat(roundedValue, 'f', -1, bitSize) | ||||||
// Pad with 0's if the value has no decimals | ||||||
if len(strings.Split(valueStr, ".")) == 1 { | ||||||
valueStr = valueStr + fmt.Sprintf(".%0*v", prec, 0) | ||||||
} | ||||||
// remove extra '0' | ||||||
ePos := bytes.IndexByte(valStr, 'e') | ||||||
pointPos := bytes.IndexByte(valStr, '.') | ||||||
ePosInOut := len(in) + ePos | ||||||
pointPosInOut := len(in) + pointPos | ||||||
validPos := ePosInOut | ||||||
for i := ePosInOut - 1; i >= pointPosInOut; i-- { | ||||||
if out[i] == '0' || out[i] == '.' { | ||||||
validPos = i | ||||||
} else { | ||||||
break | ||||||
} | ||||||
return []byte(valueStr) | ||||||
default: | ||||||
return strconv.AppendFloat(in, fVal, 'f', prec, bitSize) | ||||||
} | ||||||
} | ||||||
|
||||||
func roundFloatToPrecision(fValue float64, precision int) float64 { | ||||||
base := math.Pow10(precision) | ||||||
|
||||||
// Calculate the mantissa and exponent (scientific notation) | ||||||
exponent := math.Floor(math.Log10(math.Abs(fValue))) | ||||||
mantissa := fValue / math.Pow(10, exponent) | ||||||
|
||||||
// Round the mantissa to <precision> number of digits, and multiply with exponent to get the result | ||||||
roundedValue := math.Round(mantissa*base) / base * math.Pow(10, exponent) | ||||||
|
||||||
if math.IsNaN(roundedValue) { | ||||||
return 0 | ||||||
} | ||||||
return roundedValue | ||||||
} | ||||||
|
||||||
func formatFloatScientificNotation(in []byte, fVal float64, prec, bitSize int) []byte { | ||||||
absVal := math.Abs(fVal) | ||||||
if absVal > math.MaxFloat64 || math.IsNaN(absVal) { | ||||||
return []byte{'0'} | ||||||
} | ||||||
if bitSize == 32 { | ||||||
prec = defaultMySQLPrec | ||||||
} | ||||||
var out []byte | ||||||
out = strconv.AppendFloat(in, fVal, 'e', prec, bitSize) | ||||||
valStr := out[len(in):] | ||||||
// remove the '+' from the string for compatibility. | ||||||
plusPos := bytes.IndexByte(valStr, '+') | ||||||
if plusPos > 0 { | ||||||
plusPosInOut := len(in) + plusPos | ||||||
out = append(out[:plusPosInOut], out[plusPosInOut+1:]...) | ||||||
} | ||||||
// remove extra '0' | ||||||
ePos := bytes.IndexByte(valStr, 'e') | ||||||
pointPos := bytes.IndexByte(valStr, '.') | ||||||
ePosInOut := len(in) + ePos | ||||||
pointPosInOut := len(in) + pointPos | ||||||
validPos := ePosInOut | ||||||
for i := ePosInOut - 1; i >= pointPosInOut; i-- { | ||||||
if out[i] == '0' || out[i] == '.' { | ||||||
validPos = i | ||||||
} else { | ||||||
break | ||||||
} | ||||||
out = append(out[:validPos], out[ePosInOut:]...) | ||||||
} else { | ||||||
out = strconv.AppendFloat(in, fVal, 'f', prec, bitSize) | ||||||
} | ||||||
out = append(out[:validPos], out[ePosInOut:]...) | ||||||
|
||||||
return out | ||||||
} | ||||||
|
||||||
|
Oops, something went wrong.
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.
better add a mysql referrence link here, and could you descript the imcompatiable behavior with mysql here a little bit?