diff --git a/enginetest/queries/script_queries.go b/enginetest/queries/script_queries.go index 72bcd72ecc..081d024909 100644 --- a/enginetest/queries/script_queries.go +++ b/enginetest/queries/script_queries.go @@ -14865,6 +14865,24 @@ select * from t1 except ( }, }, }, + { + Name: "Greatest and least with decimal arguments", + // TODO: This should work in Doltgres https://github.com/dolthub/doltgresql/issues/2378 + Dialect: "mysql", + SetUpScript: []string{ + "create table t(a decimal(6, 2), b decimal(8, 5), c decimal(5, 1));", + "insert into t values (2.75, 8.8, 3.1);", + }, + Assertions: []ScriptTestAssertion{ + { + // https://github.com/dolthub/dolt/issues/10562 + Query: "select greatest(a, b, c), least(a, b, c) from t;", + // TODO: greatest and least currently return a float64 for decimals. MySQL returns a decimal with the + // highest precision https://github.com/dolthub/dolt/issues/10567 + Expected: []sql.Row{{8.8, 2.75}}, + }, + }, + }, } var SpatialScriptTests = []ScriptTest{ diff --git a/go.sum b/go.sum index ca5261386d..9fa63a32e1 100644 --- a/go.sum +++ b/go.sum @@ -18,12 +18,6 @@ github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 h1:bMGS25NWAGTE github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71/go.mod h1:2/2zjLQ/JOOSbbSboojeg+cAwcRV0fDLzIiWch/lhqI= github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81 h1:7/v8q9XGFa6q5Ap4Z/OhNkAMBaK5YeuEzwJt+NZdhiE= github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81/go.mod h1:siLfyv2c92W1eN/R4QqG/+RjjX5W2+gCTRjZxBjI3TY= -github.com/dolthub/vitess v0.0.0-20260128180459-bd171d35a7e2 h1:QMqgG+oegtoQxcHXk/EU5SznJaRPkRHsLpWOJDhGQPU= -github.com/dolthub/vitess v0.0.0-20260128180459-bd171d35a7e2/go.mod h1:eLLslh1CSPMf89pPcaMG4yM72PQbTN9OUYJeAy0fAis= -github.com/dolthub/vitess v0.0.0-20260202234501-b14ed9b1632b h1:B8QS0U5EHtJTiOptjti1cH/OiE6uczyhePtvVFigf3w= -github.com/dolthub/vitess v0.0.0-20260202234501-b14ed9b1632b/go.mod h1:eLLslh1CSPMf89pPcaMG4yM72PQbTN9OUYJeAy0fAis= -github.com/dolthub/vitess v0.0.0-20260223195511-98894ce0c3ca h1:59IsCKj+MBdq1IjoWkF1qsvcZQ2lXh4rPr08THDMxeY= -github.com/dolthub/vitess v0.0.0-20260223195511-98894ce0c3ca/go.mod h1:eLLslh1CSPMf89pPcaMG4yM72PQbTN9OUYJeAy0fAis= github.com/dolthub/vitess v0.0.0-20260224233013-568e5b4a9dbb h1:O/30JH3vVYQfIuUjuRW/KLV2xni/+2hJsrLdrflHmSg= github.com/dolthub/vitess v0.0.0-20260224233013-568e5b4a9dbb/go.mod h1:eLLslh1CSPMf89pPcaMG4yM72PQbTN9OUYJeAy0fAis= github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= diff --git a/sql/expression/function/greatest_least.go b/sql/expression/function/greatest_least.go index 4c71dc0aaa..77fd7e1aa2 100644 --- a/sql/expression/function/greatest_least.go +++ b/sql/expression/function/greatest_least.go @@ -20,6 +20,7 @@ import ( "strings" "time" + "github.com/shopspring/decimal" "gopkg.in/src-d/go-errors.v1" "github.com/dolthub/go-mysql-server/sql" @@ -52,6 +53,8 @@ func compEval( return nil, err } + // TODO: this switch statement can be cleaned up a lot. A lot of these type conversions and conversions are + // also unnecessary because they get converted during compare switch t := val.(type) { case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: @@ -117,6 +120,11 @@ func compEval( if i == 0 || cmp(t, selectedTime) { selectedTime = t } + case decimal.Decimal: + fval, _ := t.Float64() + if i == 0 || cmp(fval, selectedNum) { + selectedNum = fval + } case nil: return nil, nil default: @@ -166,8 +174,7 @@ func compRetType(args ...sql.Expression) (sql.Type, error) { } else if types.IsNumber(argType) { allString = false allDatetime = false - if types.IsFloat(argType) { - allString = false + if !types.IsInteger(argType) { allInt = false } } else if types.IsText(argType) { @@ -186,6 +193,7 @@ func compRetType(args ...sql.Expression) (sql.Type, error) { } } + // TODO: return Decimal type if all Decimals. Account for Decimals of different scales and precisions if allString { return types.LongText, nil } else if allInt {