Skip to content

Commit

Permalink
Fix issue related to rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
dveeden committed Sep 26, 2024
1 parent 634341f commit 9502211
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 59 deletions.
56 changes: 0 additions & 56 deletions pkg/expression/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -3296,66 +3296,10 @@ func evalNumDecArgsForFormat(ctx EvalContext, f builtinFunc, row chunk.Row) (str
} else if d > formatMaxDecimals {
d = formatMaxDecimals
}
xStr = roundFormatArgs(xStr, int(d))
dStr := strconv.FormatInt(d, 10)
return xStr, dStr, false, nil
}

func roundFormatArgs(xStr string, maxNumDecimals int) string {
if !strings.Contains(xStr, ".") {
return xStr
}

sign := false
// xStr cannot have '+' prefix now.
// It is built in `evalNumDecArgsFormat` after evaluating `Evalxxx` method.
if strings.HasPrefix(xStr, "-") {
xStr = strings.Trim(xStr, "-")
sign = true
}

xArr := strings.Split(xStr, ".")
integerPart := xArr[0]
decimalPart := xArr[1]

if len(decimalPart) > maxNumDecimals {
t := []byte(decimalPart)
carry := false
if t[maxNumDecimals] >= '5' {
carry = true
}
for i := maxNumDecimals - 1; i >= 0 && carry; i-- {
if t[i] == '9' {
t[i] = '0'
} else {
t[i] = t[i] + 1
carry = false
}
}
decimalPart = string(t)
t = []byte(integerPart)
for i := len(integerPart) - 1; i >= 0 && carry; i-- {
if t[i] == '9' {
t[i] = '0'
} else {
t[i] = t[i] + 1
carry = false
}
}
if carry {
integerPart = "1" + string(t)
} else {
integerPart = string(t)
}
}

xStr = integerPart + "." + decimalPart
if sign {
xStr = "-" + xStr
}
return xStr
}

type builtinFormatWithLocaleSig struct {
baseBuiltinFunc
}
Expand Down
6 changes: 3 additions & 3 deletions tests/integrationtest/r/expression/format.result
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ FORMAT(12345678.9,999999999999999999,'de_CH')
12’345’678.900000000372529029846191406250
SELECT FORMAT(12345678.9,9999999999999999999,'de_CH');
FORMAT(12345678.9,9999999999999999999,'de_CH')
12’345’680
12’345’679
SELECT FORMAT(12345678.9,99999999999999999999,'de_CH');
FORMAT(12345678.9,99999999999999999999,'de_CH')
12’345’678.900000000372529029846191406250
Expand All @@ -77,10 +77,10 @@ FORMAT(1.05, 1)
1.1
SELECT FORMAT(1.06, 1);
FORMAT(1.06, 1)
1.2
1.1
SELECT FORMAT(1.06, 1.4);
FORMAT(1.06, 1.4)
1.2
1.1
SELECT FORMAT(1.06, 1.6);
FORMAT(1.06, 1.6)
1.06
Expand Down

0 comments on commit 9502211

Please sign in to comment.