Skip to content
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

expression: handle empty input and improve compatibility for format #8797

Merged
merged 9 commits into from
Jan 2, 2019
15 changes: 12 additions & 3 deletions expression/builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2974,20 +2974,29 @@ func (b *builtinFormatSig) Clone() builtinFunc {
return newSig
}

const formatMaxDecimals int64 = 30

// evalString evals FORMAT(X,D).
// See https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_format
func (b *builtinFormatSig) evalString(row chunk.Row) (string, bool, error) {
x, isNull, err := b.args[0].EvalString(b.ctx, row)
x, isNull, err := b.args[0].EvalReal(b.ctx, row)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The argument types are all ETString: https://github.com/pingcap/tidb/pull/8797/files#diff-314e997a9df9b116e8f0aad4149df468R2920. We should not change EvalString() to EvalReal().

Copy link
Contributor Author

@eurekaka eurekaka Dec 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the output of MySQL:

mysql> select format("1a","2a");
+-------------------+
| format("1a","2a") |
+-------------------+
| 1.00              |
+-------------------+
1 row in set, 2 warnings (0.00 sec)

mysql> show warnings;
+---------+------+-----------------------------------------+
| Level   | Code | Message                                 |
+---------+------+-----------------------------------------+
| Warning | 1292 | Truncated incorrect INTEGER value: '2a' |
| Warning | 1292 | Truncated incorrect DOUBLE value: '1a'  |
+---------+------+-----------------------------------------+
2 rows in set (0.00 sec)

Seems MySQL believes the type of parameters to be Double and Integer, but it is not reflected in https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_format.

Copy link
Contributor Author

@eurekaka eurekaka Dec 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In MySQL source code, for function format(X, D), X is evaluated as decimal or double type, while D is evaluated as integer type.

  dec= (int) args[1]->val_int();
  if (args[0]->result_type() == DECIMAL_RESULT ||
      args[0]->result_type() == INT_RESULT)
  {
    res= args[0]->val_decimal(&dec_val);
  }
  else
  {
    double nr= args[0]->val_real();
  }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if isNull || err != nil {
return "", true, err
}
xStr := strconv.FormatFloat(x, 'f', -1, 64)

d, isNull, err := b.args[1].EvalString(b.ctx, row)
d, isNull, err := b.args[1].EvalInt(b.ctx, row)
if isNull || err != nil {
return "", true, err
}
if d < 0 {
d = 0
} else if d > formatMaxDecimals {
d = formatMaxDecimals
}
dStr := strconv.FormatInt(d, 10)

formatString, err := mysql.GetLocaleFormatFunction("en_US")(x, d)
formatString, err := mysql.GetLocaleFormatFunction("en_US")(xStr, dStr)
return formatString, err != nil, err
}

Expand Down
65 changes: 40 additions & 25 deletions expression/builtin_string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1606,31 +1606,35 @@ func (s *testEvaluatorSuite) TestFormat(c *C) {
number interface{}
precision interface{}
ret interface{}
warnings int
}{
{12332.123456, 4, "12,332.1234"},
{12332.123456, 0, "12,332"},
{12332.123456, -4, "12,332"},
{-12332.123456, 4, "-12,332.1234"},
{-12332.123456, 0, "-12,332"},
{-12332.123456, -4, "-12,332"},
{"12332.123456", "4", "12,332.1234"},
{"12332.123456A", "4", "12,332.1234"},
{"-12332.123456", "4", "-12,332.1234"},
{"-12332.123456A", "4", "-12,332.1234"},
{"A123345", "4", "0.0000"},
{"-A123345", "4", "0.0000"},
{"-12332.123456", "A", "-12,332"},
{"12332.123456", "A", "12,332"},
{"-12332.123456", "4A", "-12,332.1234"},
{"12332.123456", "4A", "12,332.1234"},
{"-A12332.123456", "A", "0"},
{"A12332.123456", "A", "0"},
{"-A12332.123456", "4A", "0.0000"},
{"A12332.123456", "4A", "0.0000"},
{"-.12332.123456", "4A", "-0.1233"},
{".12332.123456", "4A", "0.1233"},
{"12332.1234567890123456789012345678901", 22, "12,332.1234567890123456789012"},
{nil, 22, nil},
{12332.123444, 4, "12,332.1234", 0},
{12332.123444, 0, "12,332", 0},
{12332.123444, -4, "12,332", 0},
{-12332.123444, 4, "-12,332.1234", 0},
{-12332.123444, 0, "-12,332", 0},
{-12332.123444, -4, "-12,332", 0},
{"12332.123444", "4", "12,332.1234", 0},
{"12332.123444A", "4", "12,332.1234", 1},
{"-12332.123444", "4", "-12,332.1234", 0},
{"-12332.123444A", "4", "-12,332.1234", 1},
{"A123345", "4", "0.0000", 1},
{"-A123345", "4", "0.0000", 1},
{"-12332.123444", "A", "-12,332", 1},
{"12332.123444", "A", "12,332", 1},
{"-12332.123444", "4A", "-12,332.1234", 1},
{"12332.123444", "4A", "12,332.1234", 1},
{"-A12332.123444", "A", "0", 2},
{"A12332.123444", "A", "0", 2},
{"-A12332.123444", "4A", "0.0000", 2},
{"A12332.123444", "4A", "0.0000", 2},
{"-.12332.123444", "4A", "-0.1233", 2},
{".12332.123444", "4A", "0.1233", 2},
{"12332.1234567890123456789012345678901", 22, "12,332.1234567890110000000000", 0},
{nil, 22, nil, 0},
{1, 1024, "1.000000000000000000000000000000", 0},
{"", 1, "0.0", 1},
{1, "", "1", 1},
}
formatTests2 := struct {
number interface{}
Expand All @@ -1655,15 +1659,26 @@ func (s *testEvaluatorSuite) TestFormat(c *C) {
c.Assert(r, testutil.DatumEquals, types.NewDatum(tt.ret))
}

origConfig := s.ctx.GetSessionVars().StmtCtx.TruncateAsWarning
s.ctx.GetSessionVars().StmtCtx.TruncateAsWarning = true
for _, tt := range formatTests1 {
fc := funcs[ast.Format]
f, err := fc.getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(tt.number, tt.precision)))
c.Assert(err, IsNil)
c.Assert(f, NotNil)
r, err := evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(r, testutil.DatumEquals, types.NewDatum(tt.ret))
if tt.warnings > 0 {
warnings := s.ctx.GetSessionVars().StmtCtx.GetWarnings()
c.Assert(len(warnings), Equals, tt.warnings, Commentf("test %v", tt))
for i := 0; i < tt.warnings; i++ {
c.Assert(terror.ErrorEqual(types.ErrTruncated, warnings[i].Err), IsTrue, Commentf("test %v", tt))
}
s.ctx.GetSessionVars().StmtCtx.SetWarnings([]stmtctx.SQLWarn{})
}
c.Assert(r, testutil.DatumEquals, types.NewDatum(tt.ret), Commentf("test %v", tt))
}
s.ctx.GetSessionVars().StmtCtx.TruncateAsWarning = origConfig

fc2 := funcs[ast.Format]
f2, err := fc2.getFunction(s.ctx, s.datumsToConstants(types.MakeDatums(formatTests2.number, formatTests2.precision, formatTests2.locale)))
Expand Down