go-mysql-server icon indicating copy to clipboard operation
go-mysql-server copied to clipboard

[MySQL Compat] SUM result types

Open vmg opened this issue 2 years ago • 3 comments

As discussed in https://github.com/dolthub/go-mysql-server/pull/1476#issuecomment-1352731490 --

The result type for SUM aggregations in go-mysql-server does not match MySQL's. Even though the actual SumBuffer that performs the computation internally uses either float64 or Decimal, the type for the resulting column in the planned schema defaults to the type of the underlying column.

As an example: in MySQL, summing a column of INT (i.e. int32) results in a DECIMAL result for the output row. This is the same when summing any other integral type, regardless of whether the type has overflowed or not. In go-mysql-server, summing a column of INT returns INT. In fact, summing any integral type returns the same integral type.

The general rule for MySQL types in SUMs is as follows:

  • Any accurate numeric type (e.g. INT, BIGINT, SMALLINT, TINYINT, DECIMAL, etc) sums into DECIMAL.
  • Any approximate numeric type (e.g. FLOAT, DOUBLE) sums into DOUBLE.

Would you be interested on a PR to mimic this behavior much more closely?

vmg avatar Dec 16 '22 09:12 vmg

Yes. We would definitely take a PR here.

timsehn avatar Dec 16 '22 16:12 timsehn

Hey @vmg, GMS behavior here has changed. We currently always return DOUBLE regardless of the input types (this might also be the case for COUNT).

We can start fixing this soon. Unless you still want to :)

jycor avatar Feb 28 '24 23:02 jycor

Is there any progress? we got float64 even from select unix_timestamp('2000-01-01')

okhowang avatar Jun 19 '24 08:06 okhowang