Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,89 @@ type ScriptTestAssertion struct {
// Unlike other engine tests, ScriptTests must be self-contained. No other tables are created outside the definition of
// the tests.
var ScriptTests = []ScriptTest{
{
// https://github.com/dolthub/dolt/issues/9927
// https://github.com/dolthub/dolt/issues/9053
Name: "double negation of integer minimum values",
Dialect: "mysql",
SetUpScript: []string{
"CREATE TABLE t0(c0 BIGINT);",
"INSERT INTO t0(c0) VALUES (-9223372036854775808);",
"CREATE TABLE t1(c0 INT);",
"INSERT INTO t1(c0) VALUES (-2147483648);",
"CREATE TABLE t2(c0 SMALLINT);",
"INSERT INTO t2(c0) VALUES (-32768);",
"CREATE TABLE t3(c0 TINYINT);",
"INSERT INTO t3(c0) VALUES (-128);",
},
Assertions: []ScriptTestAssertion{
{
Query: "SELECT -(-128);",
Expected: []sql.Row{{int16(128)}},
ExpectedColumns: sql.Schema{{Name: "-(-128)", Type: types.Int64}},
},
{
Query: "SELECT -(-32768);",
Expected: []sql.Row{{int32(32768)}},
ExpectedColumns: sql.Schema{{Name: "-(-32768)", Type: types.Int64}},
},
{
Query: "SELECT -(-2147483648);",
Expected: []sql.Row{{int64(2147483648)}},
ExpectedColumns: sql.Schema{{Name: "-(-2147483648)", Type: types.Int64}},
},
{
Query: "SELECT -(-9223372036854775808)",
Expected: []sql.Row{{"9223372036854775808"}},
ExpectedColumns: sql.Schema{{Name: "-(-9223372036854775808)", Type: types.InternalDecimalType}},
},
{
Query: "SELECT -t0.c0 FROM t0;",
ExpectedErr: sql.ErrValueOutOfRange,
},
{
Query: "SELECT -t1.c0 FROM t1;",
Expected: []sql.Row{{2147483648}},
},
{
Query: "SELECT -t2.c0 FROM t2;",
Expected: []sql.Row{{32768}},
},
{
Query: "SELECT -t3.c0 FROM t3;",
Expected: []sql.Row{{128}},
},
{
Query: "SELECT -(-t1.c0 + 1) FROM t1;",
Expected: []sql.Row{{-2147483649}},
},
{
Query: "SELECT -(-(t2.c0 - 1)) FROM t2;",
Expected: []sql.Row{{-32769}},
},
{
Query: "SELECT -(-t3.c0 * 2) FROM t3;",
Expected: []sql.Row{{-256}},
},
{
Query: "SELECT -(-(-128));",
Expected: []sql.Row{{int8(-128)}},
},
{
Query: "SELECT -(-(-(-128)));",
Expected: []sql.Row{{int16(128)}},
},
{
Query: "SELECT -(-NULL);",
Expected: []sql.Row{{nil}},
},
{
Query: "SELECT -(-CAST(-128 AS SIGNED));",
Expected: []sql.Row{{int64(-128)}},
ExpectedColumns: sql.Schema{{Name: "-(-CAST(-128 AS SIGNED))", Type: types.Int64}},
},
},
},
{
// https://github.com/dolthub/dolt/issues/9865
Name: "Stored procedure containing a transaction does not return EOF",
Expand Down
35 changes: 19 additions & 16 deletions sql/expression/arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/dolthub/vitess/go/mysql"
"github.com/dolthub/vitess/go/vt/sqlparser"
"github.com/shopspring/decimal"
errors "gopkg.in/src-d/go-errors.v1"
"gopkg.in/src-d/go-errors.v1"

"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/types"
Expand Down Expand Up @@ -705,25 +705,17 @@ func (e *UnaryMinus) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return -n, nil
case float32:
return -n, nil
case int:
return -n, nil
case int8:
if n == math.MinInt8 {
return -int16(n), nil
}
return -n, nil
return -int64(n), nil
case int16:
if n == math.MinInt16 {
return -int32(n), nil
}
return -n, nil
return -int64(n), nil
case int32:
if n == math.MinInt32 {
return -int64(n), nil
}
return -n, nil
return -int64(n), nil
case int64:
if n == math.MinInt64 {
if _, ok := e.Child.(*Literal); ok {
return decimal.NewFromInt(n).Neg(), nil
}
return nil, sql.ErrValueOutOfRange.New("BIGINT", fmt.Sprintf("%d", n))
}
return -n, nil
Expand Down Expand Up @@ -760,6 +752,17 @@ func (e *UnaryMinus) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
// Type implements the sql.Expression interface.
func (e *UnaryMinus) Type() sql.Type {
typ := e.Child.Type()
switch typ {
case types.Int8, types.Int16, types.Int32:
typ = types.Int64
case types.Int64:
if lit, ok := e.Child.(*Literal); ok {
if lit.Value().(int64) == math.MinInt64 {
typ = types.InternalDecimalType
}
}
}

if !types.IsNumber(typ) {
return types.Float64
}
Expand All @@ -772,7 +775,7 @@ func (e *UnaryMinus) Type() sql.Type {
return types.Int64
}

return e.Child.Type()
return typ
}

// CollationCoercibility implements the interface sql.CollationCoercible.
Expand Down
9 changes: 8 additions & 1 deletion sql/expression/arithmetic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package expression

import (
"fmt"
"math"
"testing"
"time"

Expand Down Expand Up @@ -568,7 +569,9 @@ func TestUnaryMinus(t *testing.T) {
typ sql.Type
expected interface{}
}{
{"int32", int32(1), types.Int32, int32(-1)},
{"int8", int8(1), types.Int8, int64(-1)},
{"int16", int16(1), types.Int16, int64(-1)},
{"int32", int32(1), types.Int32, int64(-1)},
{"uint32", uint32(1), types.Uint32, int32(-1)},
{"int64", int64(1), types.Int64, int64(-1)},
{"uint64", uint64(1), types.Uint64, int64(-1)},
Expand All @@ -577,6 +580,10 @@ func TestUnaryMinus(t *testing.T) {
{"int text", "1", types.LongText, "-1"},
{"float text", "1.2", types.LongText, "-1.2"},
{"nil", nil, types.LongText, nil},
{"minint8", int8(math.MinInt8), types.Int8, int64(128)},
{"minint16", int16(math.MinInt16), types.Int16, int64(32768)},
{"minint32", int32(math.MinInt32), types.Int32, int64(2147483648)},
{"minint64", int64(math.MinInt64), types.Int64, "9223372036854775808"},
}

for _, tt := range testCases {
Expand Down