diff --git a/expression/integration_serial_test/BUILD.bazel b/expression/integration_serial_test/BUILD.bazel index bd5d60461c46f..0ba73c9a2fbf7 100644 --- a/expression/integration_serial_test/BUILD.bazel +++ b/expression/integration_serial_test/BUILD.bazel @@ -8,11 +8,10 @@ go_test( "main_test.go", ], flaky = True, - shard_count = 17, + shard_count = 12, deps = [ "//config", "//expression", - "//parser/mysql", "//parser/terror", "//planner/core", "//session", diff --git a/expression/integration_serial_test/integration_serial_test.go b/expression/integration_serial_test/integration_serial_test.go index 49e5c6c6046c1..5eb857b49f157 100644 --- a/expression/integration_serial_test/integration_serial_test.go +++ b/expression/integration_serial_test/integration_serial_test.go @@ -27,7 +27,6 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/failpoint" "github.com/pingcap/tidb/expression" - "github.com/pingcap/tidb/parser/mysql" "github.com/pingcap/tidb/parser/terror" plannercore "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/session" @@ -38,470 +37,6 @@ import ( "github.com/tikv/client-go/v2/oracle" ) -func TestWeightString(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - - type testCase struct { - input []string - result []string - resultAsChar1 []string - resultAsChar3 []string - resultAsBinary1 []string - resultAsBinary5 []string - resultExplicitCollateBin []string - } - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (id int, a varchar(20) collate utf8mb4_general_ci)") - cases := testCase{ - input: []string{"aAÁàãăâ", "a", "a ", "中", "中 "}, - result: []string{"\x00A\x00A\x00A\x00A\x00A\x00A\x00A", "\x00A", "\x00A", "\x4E\x2D", "\x4E\x2D"}, - resultAsChar1: []string{"\x00A", "\x00A", "\x00A", "\x4E\x2D", "\x4E\x2D"}, - resultAsChar3: []string{"\x00A\x00A\x00A", "\x00A", "\x00A", "\x4E\x2D", "\x4E\x2D"}, - resultAsBinary1: []string{"a", "a", "a", "\xE4", "\xE4"}, - resultAsBinary5: []string{"aA\xc3\x81\xc3", "a\x00\x00\x00\x00", "a \x00\x00", "中\x00\x00", "中 \x00"}, - resultExplicitCollateBin: []string{"aAÁàãăâ", "a", "a", "中", "中"}, - } - values := make([]string, len(cases.input)) - for i, input := range cases.input { - values[i] = fmt.Sprintf("(%d, '%s')", i, input) - } - tk.MustExec("insert into t values " + strings.Join(values, ",")) - rows := tk.MustQuery("select weight_string(a) from t order by id").Rows() - for i, out := range cases.result { - require.Equal(t, out, rows[i][0].(string)) - } - rows = tk.MustQuery("select weight_string(a as char(1)) from t order by id").Rows() - for i, out := range cases.resultAsChar1 { - require.Equal(t, out, rows[i][0].(string)) - } - rows = tk.MustQuery("select weight_string(a as char(3)) from t order by id").Rows() - for i, out := range cases.resultAsChar3 { - require.Equal(t, out, rows[i][0].(string)) - } - rows = tk.MustQuery("select weight_string(a as binary(1)) from t order by id").Rows() - for i, out := range cases.resultAsBinary1 { - require.Equal(t, out, rows[i][0].(string)) - } - rows = tk.MustQuery("select weight_string(a as binary(5)) from t order by id").Rows() - for i, out := range cases.resultAsBinary5 { - require.Equal(t, out, rows[i][0].(string)) - } - require.Equal(t, "", tk.MustQuery("select weight_string(NULL);").Rows()[0][0]) - require.Equal(t, "", tk.MustQuery("select weight_string(7);").Rows()[0][0]) - require.Equal(t, "", tk.MustQuery("select weight_string(cast(7 as decimal(5)));").Rows()[0][0]) - require.Equal(t, "2019-08-21", tk.MustQuery("select weight_string(cast(20190821 as date));").Rows()[0][0]) - require.Equal(t, "2019-", tk.MustQuery("select weight_string(cast(20190821 as date) as binary(5));").Rows()[0][0]) - require.Equal(t, "", tk.MustQuery("select weight_string(7.0);").Rows()[0][0]) - require.Equal(t, "7\x00", tk.MustQuery("select weight_string(7 AS BINARY(2));").Rows()[0][0]) - // test explicit collation - require.Equal(t, "\x4E\x2D", tk.MustQuery("select weight_string('中 ' collate utf8mb4_general_ci);").Rows()[0][0]) - require.Equal(t, "中", tk.MustQuery("select weight_string('中 ' collate utf8mb4_bin);").Rows()[0][0]) - require.Equal(t, "\xFB\x40\xCE\x2D", tk.MustQuery("select weight_string('中 ' collate utf8mb4_unicode_ci);").Rows()[0][0]) - require.Equal(t, "utf8mb4_general_ci", tk.MustQuery("select collation(a collate utf8mb4_general_ci) from t order by id").Rows()[0][0]) - require.Equal(t, "utf8mb4_general_ci", tk.MustQuery("select collation('中 ' collate utf8mb4_general_ci);").Rows()[0][0]) - rows = tk.MustQuery("select weight_string(a collate utf8mb4_bin) from t order by id").Rows() - for i, out := range cases.resultExplicitCollateBin { - require.Equal(t, out, rows[i][0].(string)) - } - tk.MustGetErrMsg("select weight_string(a collate utf8_general_ci) from t order by id", "[ddl:1253]COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'utf8mb4'") - tk.MustGetErrMsg("select weight_string('中' collate utf8_bin)", "[ddl:1253]COLLATION 'utf8_bin' is not valid for CHARACTER SET 'utf8mb4'") -} - -func TestMathBuiltin(t *testing.T) { - t.Skip("it has been broken. Please fix it as soon as possible.") - ctx := context.Background() - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - // for degrees - result := tk.MustQuery("select degrees(0), degrees(1)") - result.Check(testkit.Rows("0 57.29577951308232")) - result = tk.MustQuery("select degrees(2), degrees(5)") - result.Check(testkit.Rows("114.59155902616465 286.4788975654116")) - - // for sin - result = tk.MustQuery("select sin(0), sin(1.5707963267949)") - result.Check(testkit.Rows("0 1")) - result = tk.MustQuery("select sin(1), sin(100)") - result.Check(testkit.Rows("0.8414709848078965 -0.5063656411097588")) - result = tk.MustQuery("select sin('abcd')") - result.Check(testkit.Rows("0")) - - // for cos - result = tk.MustQuery("select cos(0), cos(3.1415926535898)") - result.Check(testkit.Rows("1 -1")) - result = tk.MustQuery("select cos('abcd')") - result.Check(testkit.Rows("1")) - - // for tan - result = tk.MustQuery("select tan(0.00), tan(PI()/4)") - result.Check(testkit.Rows("0 1")) - result = tk.MustQuery("select tan('abcd')") - result.Check(testkit.Rows("0")) - - // for log2 - result = tk.MustQuery("select log2(0.0)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select log2(4)") - result.Check(testkit.Rows("2")) - result = tk.MustQuery("select log2('8.0abcd')") - result.Check(testkit.Rows("3")) - result = tk.MustQuery("select log2(-1)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select log2(NULL)") - result.Check(testkit.Rows("")) - - // for log10 - result = tk.MustQuery("select log10(0.0)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select log10(100)") - result.Check(testkit.Rows("2")) - result = tk.MustQuery("select log10('1000.0abcd')") - result.Check(testkit.Rows("3")) - result = tk.MustQuery("select log10(-1)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select log10(NULL)") - result.Check(testkit.Rows("")) - - // for log - result = tk.MustQuery("select log(0.0)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select log(100)") - result.Check(testkit.Rows("4.605170185988092")) - result = tk.MustQuery("select log('100.0abcd')") - result.Check(testkit.Rows("4.605170185988092")) - result = tk.MustQuery("select log(-1)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select log(NULL)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select log(NULL, NULL)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select log(1, 100)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select log(0.5, 0.25)") - result.Check(testkit.Rows("2")) - result = tk.MustQuery("select log(-1, 0.25)") - result.Check(testkit.Rows("")) - - // for atan - result = tk.MustQuery("select atan(0), atan(-1), atan(1), atan(1,2)") - result.Check(testkit.Rows("0 -0.7853981633974483 0.7853981633974483 0.4636476090008061")) - result = tk.MustQuery("select atan('tidb')") - result.Check(testkit.Rows("0")) - - // for asin - result = tk.MustQuery("select asin(0), asin(-2), asin(2), asin(1)") - result.Check(testkit.Rows("0 1.5707963267948966")) - result = tk.MustQuery("select asin('tidb')") - result.Check(testkit.Rows("0")) - - // for acos - result = tk.MustQuery("select acos(0), acos(-2), acos(2), acos(1)") - result.Check(testkit.Rows("1.5707963267948966 0")) - result = tk.MustQuery("select acos('tidb')") - result.Check(testkit.Rows("1.5707963267948966")) - - // for pi - result = tk.MustQuery("select pi()") - result.Check(testkit.Rows("3.141592653589793")) - - // for floor - result = tk.MustQuery("select floor(0), floor(null), floor(1.23), floor(-1.23), floor(1)") - result.Check(testkit.Rows("0 1 -2 1")) - result = tk.MustQuery("select floor('tidb'), floor('1tidb'), floor('tidb1')") - result.Check(testkit.Rows("0 1 0")) - result = tk.MustQuery("SELECT floor(t.c_datetime) FROM (select CAST('2017-07-19 00:00:00' AS DATETIME) AS c_datetime) AS t") - result.Check(testkit.Rows("20170719000000")) - result = tk.MustQuery("SELECT floor(t.c_time) FROM (select CAST('12:34:56' AS TIME) AS c_time) AS t") - result.Check(testkit.Rows("123456")) - result = tk.MustQuery("SELECT floor(t.c_time) FROM (select CAST('00:34:00' AS TIME) AS c_time) AS t") - result.Check(testkit.Rows("3400")) - result = tk.MustQuery("SELECT floor(t.c_time) FROM (select CAST('00:00:00' AS TIME) AS c_time) AS t") - result.Check(testkit.Rows("0")) - result = tk.MustQuery("SELECT floor(t.c_decimal) FROM (SELECT CAST('-10.01' AS DECIMAL(10,2)) AS c_decimal) AS t") - result.Check(testkit.Rows("-11")) - result = tk.MustQuery("SELECT floor(t.c_decimal) FROM (SELECT CAST('-10.01' AS DECIMAL(10,1)) AS c_decimal) AS t") - result.Check(testkit.Rows("-10")) - - // for ceil/ceiling - result = tk.MustQuery("select ceil(0), ceil(null), ceil(1.23), ceil(-1.23), ceil(1)") - result.Check(testkit.Rows("0 2 -1 1")) - result = tk.MustQuery("select ceiling(0), ceiling(null), ceiling(1.23), ceiling(-1.23), ceiling(1)") - result.Check(testkit.Rows("0 2 -1 1")) - result = tk.MustQuery("select ceil('tidb'), ceil('1tidb'), ceil('tidb1'), ceiling('tidb'), ceiling('1tidb'), ceiling('tidb1')") - result.Check(testkit.Rows("0 1 0 0 1 0")) - result = tk.MustQuery("select ceil(t.c_datetime), ceiling(t.c_datetime) from (select cast('2017-07-20 00:00:00' as datetime) as c_datetime) as t") - result.Check(testkit.Rows("20170720000000 20170720000000")) - result = tk.MustQuery("select ceil(t.c_time), ceiling(t.c_time) from (select cast('12:34:56' as time) as c_time) as t") - result.Check(testkit.Rows("123456 123456")) - result = tk.MustQuery("select ceil(t.c_time), ceiling(t.c_time) from (select cast('00:34:00' as time) as c_time) as t") - result.Check(testkit.Rows("3400 3400")) - result = tk.MustQuery("select ceil(t.c_time), ceiling(t.c_time) from (select cast('00:00:00' as time) as c_time) as t") - result.Check(testkit.Rows("0 0")) - result = tk.MustQuery("select ceil(t.c_decimal), ceiling(t.c_decimal) from (select cast('-10.01' as decimal(10,2)) as c_decimal) as t") - result.Check(testkit.Rows("-10 -10")) - result = tk.MustQuery("select ceil(t.c_decimal), ceiling(t.c_decimal) from (select cast('-10.01' as decimal(10,1)) as c_decimal) as t") - result.Check(testkit.Rows("-10 -10")) - result = tk.MustQuery("select floor(18446744073709551615), ceil(18446744073709551615)") - result.Check(testkit.Rows("18446744073709551615 18446744073709551615")) - result = tk.MustQuery("select floor(18446744073709551615.1233), ceil(18446744073709551615.1233)") - result.Check(testkit.Rows("18446744073709551615 18446744073709551616")) - result = tk.MustQuery("select floor(-18446744073709551617), ceil(-18446744073709551617), floor(-18446744073709551617.11), ceil(-18446744073709551617.11)") - result.Check(testkit.Rows("-18446744073709551617 -18446744073709551617 -18446744073709551618 -18446744073709551617")) - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a decimal(40,20) UNSIGNED);") - tk.MustExec("insert into t values(2.99999999900000000000), (12), (0);") - tk.MustQuery("select a, ceil(a) from t where ceil(a) > 1;").Check(testkit.Rows("2.99999999900000000000 3", "12.00000000000000000000 12")) - tk.MustQuery("select a, ceil(a) from t;").Check(testkit.Rows("2.99999999900000000000 3", "12.00000000000000000000 12", "0.00000000000000000000 0")) - tk.MustQuery("select ceil(-29464);").Check(testkit.Rows("-29464")) - tk.MustQuery("select a, floor(a) from t where floor(a) > 1;").Check(testkit.Rows("2.99999999900000000000 2", "12.00000000000000000000 12")) - tk.MustQuery("select a, floor(a) from t;").Check(testkit.Rows("2.99999999900000000000 2", "12.00000000000000000000 12", "0.00000000000000000000 0")) - tk.MustQuery("select floor(-29464);").Check(testkit.Rows("-29464")) - - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(a decimal(40,20), b bigint);`) - tk.MustExec(`insert into t values(-2.99999990000000000000, -1);`) - tk.MustQuery(`select floor(a), floor(a), floor(a) from t;`).Check(testkit.Rows(`-3 -3 -3`)) - tk.MustQuery(`select b, floor(b) from t;`).Check(testkit.Rows(`-1 -1`)) - - // for cot - result = tk.MustQuery("select cot(1), cot(-1), cot(NULL)") - result.Check(testkit.Rows("0.6420926159343308 -0.6420926159343308 ")) - result = tk.MustQuery("select cot('1tidb')") - result.Check(testkit.Rows("0.6420926159343308")) - rs, err := tk.Exec("select cot(0)") - require.NoError(t, err) - _, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.Error(t, err) - terr := errors.Cause(err).(*terror.Error) - require.Equal(t, errors.ErrCode(mysql.ErrDataOutOfRange), terr.Code()) - require.NoError(t, rs.Close()) - - // for exp - result = tk.MustQuery("select exp(0), exp(1), exp(-1), exp(1.2), exp(NULL)") - result.Check(testkit.Rows("1 2.718281828459045 0.36787944117144233 3.3201169227365472 ")) - result = tk.MustQuery("select exp('tidb'), exp('1tidb')") - result.Check(testkit.Rows("1 2.718281828459045")) - rs, err = tk.Exec("select exp(1000000)") - require.NoError(t, err) - _, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.Error(t, err) - terr = errors.Cause(err).(*terror.Error) - require.Equal(t, errors.ErrCode(mysql.ErrDataOutOfRange), terr.Code()) - require.NoError(t, rs.Close()) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a float)") - tk.MustExec("insert into t values(1000000)") - rs, err = tk.Exec("select exp(a) from t") - require.NoError(t, err) - _, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.Error(t, err) - terr = errors.Cause(err).(*terror.Error) - require.Equal(t, errors.ErrCode(mysql.ErrDataOutOfRange), terr.Code()) - require.EqualError(t, err, "[types:1690]DOUBLE value is out of range in 'exp(test.t.a)'") - require.NoError(t, rs.Close()) - - // for conv - result = tk.MustQuery("SELECT CONV('a', 16, 2);") - result.Check(testkit.Rows("1010")) - result = tk.MustQuery("SELECT CONV('6E', 18, 8);") - result.Check(testkit.Rows("172")) - result = tk.MustQuery("SELECT CONV(-17, 10, -18);") - result.Check(testkit.Rows("-H")) - result = tk.MustQuery("SELECT CONV(10+'10'+'10'+X'0a', 10, 10);") - result.Check(testkit.Rows("40")) - result = tk.MustQuery("SELECT CONV('a', 1, 10);") - result.Check(testkit.Rows("")) - result = tk.MustQuery("SELECT CONV('a', 37, 10);") - result.Check(testkit.Rows("")) - result = tk.MustQuery("SELECT CONV(0x0020, 2, 2);") - result.Check(testkit.Rows("100000")) - result = tk.MustQuery("SELECT CONV(0b10, 16, 2)") - result.Check(testkit.Rows("10")) - result = tk.MustQuery("SELECT CONV(0b10, 16, 8)") - result.Check(testkit.Rows("2")) - tk.MustExec("drop table if exists bit") - tk.MustExec("create table bit(b bit(10))") - tk.MustExec(`INSERT INTO bit (b) VALUES - (0b0000010101), - (0b0000010101), - (NULL), - (0b0000000001), - (0b0000000000), - (0b1111111111), - (0b1111111111), - (0b1111111111), - (0b0000000000), - (0b0000000000), - (0b0000000000), - (0b0000000000), - (0b0000100000);`) - tk.MustQuery("select conv(b, 2, 2) from `bit`").Check(testkit.Rows( - "10101", - "10101", - "", - "1", - "0", - "1111111111", - "1111111111", - "1111111111", - "0", - "0", - "0", - "0", - "100000")) - - // for abs - result = tk.MustQuery("SELECT ABS(-1);") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("SELECT ABS('abc');") - result.Check(testkit.Rows("0")) - result = tk.MustQuery("SELECT ABS(18446744073709551615);") - result.Check(testkit.Rows("18446744073709551615")) - result = tk.MustQuery("SELECT ABS(123.4);") - result.Check(testkit.Rows("123.4")) - result = tk.MustQuery("SELECT ABS(-123.4);") - result.Check(testkit.Rows("123.4")) - result = tk.MustQuery("SELECT ABS(1234E-1);") - result.Check(testkit.Rows("123.4")) - result = tk.MustQuery("SELECT ABS(-9223372036854775807);") - result.Check(testkit.Rows("9223372036854775807")) - result = tk.MustQuery("SELECT ABS(NULL);") - result.Check(testkit.Rows("")) - rs, err = tk.Exec("SELECT ABS(-9223372036854775808);") - require.NoError(t, err) - _, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.Error(t, err) - terr = errors.Cause(err).(*terror.Error) - require.Equal(t, errors.ErrCode(mysql.ErrDataOutOfRange), terr.Code()) - require.NoError(t, rs.Close()) - - // for round - result = tk.MustQuery("SELECT ROUND(2.5), ROUND(-2.5), ROUND(25E-1);") - result.Check(testkit.Rows("3 -3 2")) - result = tk.MustQuery("SELECT ROUND(2.5, NULL), ROUND(NULL, 4), ROUND(NULL, NULL), ROUND(NULL);") - result.Check(testkit.Rows(" ")) - result = tk.MustQuery("SELECT ROUND('123.4'), ROUND('123e-2');") - result.Check(testkit.Rows("123 1")) - result = tk.MustQuery("SELECT ROUND(-9223372036854775808);") - result.Check(testkit.Rows("-9223372036854775808")) - result = tk.MustQuery("SELECT ROUND(123.456, 0), ROUND(123.456, 1), ROUND(123.456, 2), ROUND(123.456, 3), ROUND(123.456, 4), ROUND(123.456, -1), ROUND(123.456, -2), ROUND(123.456, -3), ROUND(123.456, -4);") - result.Check(testkit.Rows("123 123.5 123.46 123.456 123.4560 120 100 0 0")) - result = tk.MustQuery("SELECT ROUND(123456E-3, 0), ROUND(123456E-3, 1), ROUND(123456E-3, 2), ROUND(123456E-3, 3), ROUND(123456E-3, 4), ROUND(123456E-3, -1), ROUND(123456E-3, -2), ROUND(123456E-3, -3), ROUND(123456E-3, -4);") - result.Check(testkit.Rows("123 123.5 123.46 123.456 123.456 120 100 0 0")) // TODO: Column 5 should be 123.4560 - result = tk.MustQuery("SELECT ROUND(1e14, 1), ROUND(1e15, 1), ROUND(1e308, 1)") - result.Check(testkit.Rows("100000000000000 1000000000000000 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")) - result = tk.MustQuery("SELECT ROUND(1e-14, 1), ROUND(1e-15, 1), ROUND(1e-308, 1)") - result.Check(testkit.Rows("0 0 0")) - - // for truncate - result = tk.MustQuery("SELECT truncate(123, -2), truncate(123, 2), truncate(123, 1), truncate(123, -1);") - result.Check(testkit.Rows("100 123 123 120")) - result = tk.MustQuery("SELECT truncate(123.456, -2), truncate(123.456, 2), truncate(123.456, 1), truncate(123.456, 3), truncate(1.23, 100), truncate(123456E-3, 2);") - result.Check(testkit.Rows("100 123.45 123.4 123.456 1.230000000000000000000000000000 123.45")) - result = tk.MustQuery("SELECT truncate(9223372036854775807, -7), truncate(9223372036854775808, -10), truncate(cast(-1 as unsigned), -10);") - result.Check(testkit.Rows("9223372036850000000 9223372030000000000 18446744070000000000")) - // issue 17181,19390 - tk.MustQuery("select truncate(42, -9223372036854775808);").Check(testkit.Rows("0")) - tk.MustQuery("select truncate(42, 9223372036854775808);").Check(testkit.Rows("42")) - tk.MustQuery("select truncate(42, -2147483648);").Check(testkit.Rows("0")) - tk.MustQuery("select truncate(42, 2147483648);").Check(testkit.Rows("42")) - tk.MustQuery("select truncate(42, 18446744073709551615);").Check(testkit.Rows("42")) - tk.MustQuery("select truncate(42, 4294967295);").Check(testkit.Rows("42")) - tk.MustQuery("select truncate(42, -0);").Check(testkit.Rows("42")) - tk.MustQuery("select truncate(42, -307);").Check(testkit.Rows("0")) - tk.MustQuery("select truncate(42, -308);").Check(testkit.Rows("0")) - tk.MustQuery("select truncate(42, -309);").Check(testkit.Rows("0")) - tk.MustExec(`drop table if exists t;`) - tk.MustExec("create table t (a bigint unsigned);") - tk.MustExec("insert into t values (18446744073709551615), (4294967295), (9223372036854775808), (2147483648);") - tk.MustQuery("select truncate(42, a) from t;").Check(testkit.Rows("42", "42", "42", "42")) - - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(a date, b datetime, c timestamp, d varchar(20));`) - tk.MustExec(`insert into t select "1234-12-29", "1234-12-29 16:24:13.9912", "2014-12-29 16:19:28", "12.34567";`) - - // NOTE: the actually result is: 12341220 12341229.0 12341200 12341229.00, - // but Datum.ToString() don't format decimal length for float numbers. - result = tk.MustQuery(`select truncate(a, -1), truncate(a, 1), truncate(a, -2), truncate(a, 2) from t;`) - result.Check(testkit.Rows("12341220 12341229 12341200 12341229")) - - // NOTE: the actually result is: 12341229162410 12341229162414.0 12341229162400 12341229162414.00, - // but Datum.ToString() don't format decimal length for float numbers. - result = tk.MustQuery(`select truncate(b, -1), truncate(b, 1), truncate(b, -2), truncate(b, 2) from t;`) - result.Check(testkit.Rows("12341229162410 12341229162414 12341229162400 12341229162414")) - - // NOTE: the actually result is: 20141229161920 20141229161928.0 20141229161900 20141229161928.00, - // but Datum.ToString() don't format decimal length for float numbers. - result = tk.MustQuery(`select truncate(c, -1), truncate(c, 1), truncate(c, -2), truncate(c, 2) from t;`) - result.Check(testkit.Rows("20141229161920 20141229161928 20141229161900 20141229161928")) - - result = tk.MustQuery(`select truncate(d, -1), truncate(d, 1), truncate(d, -2), truncate(d, 2) from t;`) - result.Check(testkit.Rows("10 12.3 0 12.34")) - - result = tk.MustQuery(`select truncate(json_array(), 1), truncate("cascasc", 1);`) - result.Check(testkit.Rows("0 0")) - - // for pow - result = tk.MustQuery("SELECT POW('12', 2), POW(1.2e1, '2.0'), POW(12, 2.0);") - result.Check(testkit.Rows("144 144 144")) - result = tk.MustQuery("SELECT POW(null, 2), POW(2, null), POW(null, null);") - result.Check(testkit.Rows(" ")) - result = tk.MustQuery("SELECT POW(0, 0);") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("SELECT POW(0, 0.1), POW(0, 0.5), POW(0, 1);") - result.Check(testkit.Rows("0 0 0")) - rs, err = tk.Exec("SELECT POW(0, -1);") - require.NoError(t, err) - _, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.Error(t, err) - terr = errors.Cause(err).(*terror.Error) - require.Equal(t, errors.ErrCode(mysql.ErrDataOutOfRange), terr.Code()) - require.NoError(t, rs.Close()) - - // for sign - result = tk.MustQuery("SELECT SIGN('12'), SIGN(1.2e1), SIGN(12), SIGN(0.0000012);") - result.Check(testkit.Rows("1 1 1 1")) - result = tk.MustQuery("SELECT SIGN('-12'), SIGN(-1.2e1), SIGN(-12), SIGN(-0.0000012);") - result.Check(testkit.Rows("-1 -1 -1 -1")) - result = tk.MustQuery("SELECT SIGN('0'), SIGN('-0'), SIGN(0);") - result.Check(testkit.Rows("0 0 0")) - result = tk.MustQuery("SELECT SIGN(NULL);") - result.Check(testkit.Rows("")) - result = tk.MustQuery("SELECT SIGN(-9223372036854775808), SIGN(9223372036854775808);") - result.Check(testkit.Rows("-1 1")) - - // for sqrt - result = tk.MustQuery("SELECT SQRT(-10), SQRT(144), SQRT(4.84), SQRT(0.04), SQRT(0);") - result.Check(testkit.Rows(" 12 2.2 0.2 0")) - - // for crc32 - result = tk.MustQuery("SELECT crc32(0), crc32(-0), crc32('0'), crc32('abc'), crc32('ABC'), crc32(NULL), crc32(''), crc32('hello world!')") - result.Check(testkit.Rows("4108050209 4108050209 4108050209 891568578 2743272264 0 62177901")) - - // for radians - result = tk.MustQuery("SELECT radians(1.0), radians(pi()), radians(pi()/2), radians(180), radians(1.009);") - result.Check(testkit.Rows("0.017453292519943295 0.05483113556160754 0.02741556778080377 3.141592653589793 0.01761037215262278")) - - // for rand - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int)") - tk.MustExec("insert into t values(1),(2),(3)") - tk.Session().GetSessionVars().MaxChunkSize = 1 - tk.MustQuery("select rand(1) from t").Sort().Check(testkit.Rows("0.1418603212962489", "0.40540353712197724", "0.8716141803857071")) - tk.MustQuery("select rand(a) from t").Check(testkit.Rows("0.40540353712197724", "0.6555866465490187", "0.9057697559760601")) - tk.MustQuery("select rand(1), rand(2), rand(3)").Check(testkit.Rows("0.40540353712197724 0.6555866465490187 0.9057697559760601")) - tk.MustQuery("set @@rand_seed1=10000000,@@rand_seed2=1000000") - tk.MustQuery("select rand()").Check(testkit.Rows("0.028870999839968048")) - tk.MustQuery("select rand(1)").Check(testkit.Rows("0.40540353712197724")) - tk.MustQuery("select rand()").Check(testkit.Rows("0.11641535266900002")) -} - func TestTimeBuiltin(t *testing.T) { store := testkit.CreateMockStore(t) @@ -1683,776 +1218,6 @@ func TestTimeBuiltin(t *testing.T) { result.Check(testkit.Rows("2000-01-05 00:00:00.00000")) } -func TestBuiltin(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - // for is true && is false - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a int, b int, index idx_b (b))") - tk.MustExec("insert t values (1, 1)") - tk.MustExec("insert t values (2, 2)") - tk.MustExec("insert t values (3, 2)") - result := tk.MustQuery("select * from t where b is true") - result.Check(testkit.Rows("1 1", "2 2", "3 2")) - result = tk.MustQuery("select all + a from t where a = 1") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select * from t where a is false") - result.Check(nil) - result = tk.MustQuery("select * from t where a is not true") - result.Check(nil) - result = tk.MustQuery(`select 1 is true, 0 is true, null is true, "aaa" is true, "" is true, -12.00 is true, 0.0 is true, 0.0000001 is true;`) - result.Check(testkit.Rows("1 0 0 0 0 1 0 1")) - result = tk.MustQuery(`select 1 is false, 0 is false, null is false, "aaa" is false, "" is false, -12.00 is false, 0.0 is false, 0.0000001 is false;`) - result.Check(testkit.Rows("0 1 0 1 1 0 1 0")) - // Issue https://github.com/pingcap/tidb/issues/19986 - result = tk.MustQuery("select 1 from dual where sec_to_time(2/10) is true") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select 1 from dual where sec_to_time(2/10) is false") - result.Check(nil) - // Issue https://github.com/pingcap/tidb/issues/19999 - result = tk.MustQuery("select 1 from dual where timediff((7/'2014-07-07 02:30:02'),'2012-01-16') is true") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select 1 from dual where timediff((7/'2014-07-07 02:30:02'),'2012-01-16') is false") - result.Check(nil) - // Issue https://github.com/pingcap/tidb/issues/20001 - result = tk.MustQuery("select 1 from dual where time(0.0001) is true") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select 1 from dual where time(0.0001) is false") - result.Check(nil) - - // for in - result = tk.MustQuery("select * from t where b in (a)") - result.Check(testkit.Rows("1 1", "2 2")) - result = tk.MustQuery("select * from t where b not in (a)") - result.Check(testkit.Rows("3 2")) - - // test cast - result = tk.MustQuery("select cast(1 as decimal(3,2))") - result.Check(testkit.Rows("1.00")) - result = tk.MustQuery("select cast('1991-09-05 11:11:11' as datetime)") - result.Check(testkit.Rows("1991-09-05 11:11:11")) - result = tk.MustQuery("select cast(cast('1991-09-05 11:11:11' as datetime) as char)") - result.Check(testkit.Rows("1991-09-05 11:11:11")) - result = tk.MustQuery("select cast('11:11:11' as time)") - result.Check(testkit.Rows("11:11:11")) - result = tk.MustQuery("select * from t where a > cast(2 as decimal)") - result.Check(testkit.Rows("3 2")) - result = tk.MustQuery("select cast(-1 as unsigned)") - result.Check(testkit.Rows("18446744073709551615")) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a decimal(3, 1), b double, c datetime, d time, e int)") - tk.MustExec("insert into t value(12.3, 1.23, '2017-01-01 12:12:12', '12:12:12', 123)") - result = tk.MustQuery("select cast(a as json), cast(b as json), cast(c as json), cast(d as json), cast(e as json) from t") - result.Check(testkit.Rows(`12.3 1.23 "2017-01-01 12:12:12.000000" "12:12:12.000000" 123`)) - result = tk.MustQuery(`select cast(10101000000 as time);`) - result.Check(testkit.Rows("00:00:00")) - result = tk.MustQuery(`select cast(10101001000 as time);`) - result.Check(testkit.Rows("00:10:00")) - result = tk.MustQuery(`select cast(10000000000 as time);`) - result.Check(testkit.Rows("")) - result = tk.MustQuery(`select cast(20171222020005 as time);`) - result.Check(testkit.Rows("02:00:05")) - result = tk.MustQuery(`select cast(8380000 as time);`) - result.Check(testkit.Rows("838:00:00")) - result = tk.MustQuery(`select cast(8390000 as time);`) - result.Check(testkit.Rows("")) - result = tk.MustQuery(`select cast(8386000 as time);`) - result.Check(testkit.Rows("")) - result = tk.MustQuery(`select cast(8385960 as time);`) - result.Check(testkit.Rows("")) - result = tk.MustQuery(`select cast(cast('2017-01-01 01:01:11.12' as date) as datetime(2));`) - result.Check(testkit.Rows("2017-01-01 00:00:00.00")) - result = tk.MustQuery(`select cast(20170118.999 as datetime);`) - result.Check(testkit.Rows("2017-01-18 00:00:00")) - tk.MustQuery(`select convert(a2.a, unsigned int) from (select cast('"9223372036854775808"' as json) as a) as a2;`) - - tk.MustExec(`create table tb5(a bigint(64) unsigned, b double);`) - tk.MustExec(`insert into tb5 (a, b) values (9223372036854776000, 9223372036854776000);`) - tk.MustExec(`insert into tb5 (a, b) select * from (select cast(a as json) as a1, b from tb5) as t where t.a1 = t.b;`) - tk.MustExec(`drop table tb5;`) - - tk.MustExec(`create table tb5(a float(53));`) - tk.MustExec(`insert into tb5(a) values (13835058055282163712);`) - tk.MustQuery(`select convert(t.a1, signed int) from (select convert(a, json) as a1 from tb5) as t`) - tk.MustExec(`drop table tb5;`) - - // test builtinCastIntAsIntSig - // Cast MaxUint64 to unsigned should be -1 - tk.MustQuery("select cast(0xffffffffffffffff as signed);").Check(testkit.Rows("-1")) - tk.MustQuery("select cast(0x9999999999999999999999999999999999999999999 as signed);").Check(testkit.Rows("-1")) - tk.MustExec("create table tb5(a bigint);") - tk.MustExec("set sql_mode=''") - tk.MustExec("insert into tb5(a) values (0xfffffffffffffffffffffffff);") - tk.MustQuery("select * from tb5;").Check(testkit.Rows("9223372036854775807")) - tk.MustExec("drop table tb5;") - - tk.MustExec(`create table tb5(a double);`) - tk.MustExec(`insert into test.tb5 (a) values (18446744073709551616);`) - tk.MustExec(`insert into test.tb5 (a) values (184467440737095516160);`) - result = tk.MustQuery(`select cast(a as unsigned) from test.tb5;`) - // Note: MySQL will return 9223372036854775807, and it should be a bug. - result.Check(testkit.Rows("18446744073709551615", "18446744073709551615")) - tk.MustExec(`drop table tb5;`) - - // test builtinCastIntAsDecimalSig - tk.MustExec(`create table tb5(a bigint(64) unsigned, b decimal(64, 10));`) - tk.MustExec(`insert into tb5 (a, b) values (9223372036854775808, 9223372036854775808);`) - tk.MustExec(`insert into tb5 (select * from tb5 where a = b);`) - result = tk.MustQuery(`select * from tb5;`) - result.Check(testkit.Rows("9223372036854775808 9223372036854775808.0000000000", "9223372036854775808 9223372036854775808.0000000000")) - tk.MustExec(`drop table tb5;`) - - // test builtinCastIntAsRealSig - tk.MustExec(`create table tb5(a bigint(64) unsigned, b double(64, 10));`) - tk.MustExec(`insert into tb5 (a, b) values (13835058000000000000, 13835058000000000000);`) - tk.MustExec(`insert into tb5 (select * from tb5 where a = b);`) - result = tk.MustQuery(`select * from tb5;`) - result.Check(testkit.Rows("13835058000000000000 13835058000000000000", "13835058000000000000 13835058000000000000")) - tk.MustExec(`drop table tb5;`) - - // test builtinCastRealAsIntSig - tk.MustExec(`create table tb5(a double, b float);`) - tk.MustExec(`insert into tb5 (a, b) values (184467440737095516160, 184467440737095516160);`) - tk.MustQuery(`select * from tb5 where cast(a as unsigned int)=0;`).Check(testkit.Rows()) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1690 constant 1.844674407370955e+20 overflows bigint")) - _ = tk.MustQuery(`select * from tb5 where cast(b as unsigned int)=0;`) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1690 constant 1.844674407370955e+20 overflows bigint")) - tk.MustExec(`drop table tb5;`) - tk.MustExec(`create table tb5(a double, b bigint unsigned);`) - tk.MustExec(`insert into tb5 (a, b) values (18446744073709551616, 18446744073709551615);`) - _ = tk.MustQuery(`select * from tb5 where cast(a as unsigned int)=b;`) - // TODO `obtained string = "[18446744073709552000 18446744073709551615]` - // result.Check(testkit.Rows("18446744073709551616 18446744073709551615")) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1690 constant 1.8446744073709552e+19 overflows bigint")) - tk.MustExec(`drop table tb5;`) - - // test builtinCastJSONAsIntSig - tk.MustExec(`create table tb5(a json, b bigint unsigned);`) - tk.MustExec(`insert into tb5 (a, b) values ('184467440737095516160', 18446744073709551615);`) - _ = tk.MustQuery(`select * from tb5 where cast(a as unsigned int)=b;`) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1690 constant 1.844674407370955e+20 overflows bigint")) - _ = tk.MustQuery(`select * from tb5 where cast(b as unsigned int)=0;`) - tk.MustQuery("show warnings;").Check(testkit.Rows()) - tk.MustExec(`drop table tb5;`) - tk.MustExec(`create table tb5(a json, b bigint unsigned);`) - tk.MustExec(`insert into tb5 (a, b) values ('92233720368547758080', 18446744073709551615);`) - _ = tk.MustQuery(`select * from tb5 where cast(a as signed int)=b;`) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1690 constant 9.223372036854776e+19 overflows bigint")) - tk.MustExec(`drop table tb5;`) - - // test builtinCastIntAsStringSig - tk.MustExec(`create table tb5(a bigint(64) unsigned,b varchar(50));`) - tk.MustExec(`insert into tb5(a, b) values (9223372036854775808, '9223372036854775808');`) - tk.MustExec(`insert into tb5(select * from tb5 where a = b);`) - result = tk.MustQuery(`select * from tb5;`) - result.Check(testkit.Rows("9223372036854775808 9223372036854775808", "9223372036854775808 9223372036854775808")) - tk.MustExec(`drop table tb5;`) - - // test builtinCastIntAsDecimalSig - tk.MustExec(`drop table if exists tb5`) - tk.MustExec(`create table tb5 (a decimal(65), b bigint(64) unsigned);`) - tk.MustExec(`insert into tb5 (a, b) values (9223372036854775808, 9223372036854775808);`) - result = tk.MustQuery(`select cast(b as decimal(64)) from tb5 union all select b from tb5;`) - result.Check(testkit.Rows("9223372036854775808", "9223372036854775808")) - tk.MustExec(`drop table tb5`) - - // test builtinCastIntAsRealSig - tk.MustExec(`drop table if exists tb5`) - tk.MustExec(`create table tb5 (a bigint(64) unsigned, b double(64, 10));`) - tk.MustExec(`insert into tb5 (a, b) values (9223372036854775808, 9223372036854775808);`) - result = tk.MustQuery(`select a from tb5 where a = b union all select b from tb5;`) - result.Check(testkit.Rows("9223372036854776000", "9223372036854776000")) - tk.MustExec(`drop table tb5`) - - // Test corner cases of cast string as datetime - result = tk.MustQuery(`select cast("170102034" as datetime);`) - result.Check(testkit.Rows("2017-01-02 03:04:00")) - result = tk.MustQuery(`select cast("1701020304" as datetime);`) - result.Check(testkit.Rows("2017-01-02 03:04:00")) - result = tk.MustQuery(`select cast("1701020304." as datetime);`) - result.Check(testkit.Rows("2017-01-02 03:04:00")) - result = tk.MustQuery(`select cast("1701020304.1" as datetime);`) - result.Check(testkit.Rows("2017-01-02 03:04:01")) - result = tk.MustQuery(`select cast("1701020304.111" as datetime);`) - result.Check(testkit.Rows("2017-01-02 03:04:11")) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1292 Truncated incorrect datetime value: '1701020304.111'")) - result = tk.MustQuery(`select cast("17011" as datetime);`) - result.Check(testkit.Rows("2017-01-01 00:00:00")) - result = tk.MustQuery(`select cast("150101." as datetime);`) - result.Check(testkit.Rows("2015-01-01 00:00:00")) - result = tk.MustQuery(`select cast("150101.a" as datetime);`) - result.Check(testkit.Rows("2015-01-01 00:00:00")) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1292 Truncated incorrect datetime value: '150101.a'")) - result = tk.MustQuery(`select cast("150101.1a" as datetime);`) - result.Check(testkit.Rows("2015-01-01 01:00:00")) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1292 Truncated incorrect datetime value: '150101.1a'")) - result = tk.MustQuery(`select cast("150101.1a1" as datetime);`) - result.Check(testkit.Rows("2015-01-01 01:00:00")) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1292 Truncated incorrect datetime value: '150101.1a1'")) - result = tk.MustQuery(`select cast("1101010101.111" as datetime);`) - result.Check(testkit.Rows("2011-01-01 01:01:11")) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1292 Truncated incorrect datetime value: '1101010101.111'")) - result = tk.MustQuery(`select cast("1101010101.11aaaaa" as datetime);`) - result.Check(testkit.Rows("2011-01-01 01:01:11")) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1292 Truncated incorrect datetime value: '1101010101.11aaaaa'")) - result = tk.MustQuery(`select cast("1101010101.a1aaaaa" as datetime);`) - result.Check(testkit.Rows("2011-01-01 01:01:00")) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1292 Truncated incorrect datetime value: '1101010101.a1aaaaa'")) - result = tk.MustQuery(`select cast("1101010101.11" as datetime);`) - result.Check(testkit.Rows("2011-01-01 01:01:11")) - tk.MustQuery("select @@warning_count;").Check(testkit.Rows("0")) - result = tk.MustQuery(`select cast("1101010101.111" as datetime);`) - result.Check(testkit.Rows("2011-01-01 01:01:11")) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1292 Truncated incorrect datetime value: '1101010101.111'")) - result = tk.MustQuery(`select cast("970101.111" as datetime);`) - result.Check(testkit.Rows("1997-01-01 11:01:00")) - tk.MustQuery("select @@warning_count;").Check(testkit.Rows("0")) - result = tk.MustQuery(`select cast("970101.11111" as datetime);`) - result.Check(testkit.Rows("1997-01-01 11:11:01")) - tk.MustQuery("select @@warning_count;").Check(testkit.Rows("0")) - result = tk.MustQuery(`select cast("970101.111a1" as datetime);`) - result.Check(testkit.Rows("1997-01-01 11:01:00")) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1292 Truncated incorrect datetime value: '970101.111a1'")) - - // for ISNULL - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a int, b int, c int, d char(10), e datetime, f float, g decimal(10, 3))") - tk.MustExec("insert t values (1, 0, null, null, null, null, null)") - result = tk.MustQuery("select ISNULL(a), ISNULL(b), ISNULL(c), ISNULL(d), ISNULL(e), ISNULL(f), ISNULL(g) from t") - result.Check(testkit.Rows("0 0 1 1 1 1 1")) - - // fix issue #3942 - result = tk.MustQuery("select cast('-24 100:00:00' as time);") - result.Check(testkit.Rows("-676:00:00")) - result = tk.MustQuery("select cast('12:00:00.000000' as datetime);") - result.Check(testkit.Rows("2012-00-00 00:00:00")) - result = tk.MustQuery("select cast('-34 100:00:00' as time);") - result.Check(testkit.Rows("-838:59:59")) - - // fix issue #4324. cast decimal/int/string to time compatibility. - invalidTimes := []string{ - "10009010", - "239010", - "233070", - "23:90:10", - "23:30:70", - "239010.2", - "233070.8", - } - tk.MustExec("DROP TABLE IF EXISTS t;") - tk.MustExec("CREATE TABLE t (ix TIME);") - tk.MustExec("SET SQL_MODE='';") - for _, invalidTime := range invalidTimes { - msg := fmt.Sprintf("Warning 1292 Truncated incorrect time value: '%s'", invalidTime) - result = tk.MustQuery(fmt.Sprintf("select cast('%s' as time);", invalidTime)) - result.Check(testkit.Rows("")) - result = tk.MustQuery("show warnings") - result.Check(testkit.Rows(msg)) - _, err := tk.Exec(fmt.Sprintf("insert into t select cast('%s' as time);", invalidTime)) - require.NoError(t, err) - result = tk.MustQuery("show warnings") - result.Check(testkit.Rows(msg)) - } - tk.MustExec("set sql_mode = 'STRICT_TRANS_TABLES'") - for _, invalidTime := range invalidTimes { - msg := fmt.Sprintf("Warning 1292 Truncated incorrect time value: '%s'", invalidTime) - result = tk.MustQuery(fmt.Sprintf("select cast('%s' as time);", invalidTime)) - result.Check(testkit.Rows("")) - result = tk.MustQuery("show warnings") - result.Check(testkit.Rows(msg)) - _, err := tk.Exec(fmt.Sprintf("insert into t select cast('%s' as time);", invalidTime)) - require.Error(t, err, fmt.Sprintf("[types:1292]Truncated incorrect time value: '%s'", invalidTime)) - } - - // Fix issue #3691, cast compatibility. - result = tk.MustQuery("select cast('18446744073709551616' as unsigned);") - result.Check(testkit.Rows("18446744073709551615")) - result = tk.MustQuery("select cast('18446744073709551616' as signed);") - result.Check(testkit.Rows("-1")) - result = tk.MustQuery("select cast('9223372036854775808' as signed);") - result.Check(testkit.Rows("-9223372036854775808")) - result = tk.MustQuery("select cast('9223372036854775809' as signed);") - result.Check(testkit.Rows("-9223372036854775807")) - result = tk.MustQuery("select cast('9223372036854775807' as signed);") - result.Check(testkit.Rows("9223372036854775807")) - result = tk.MustQuery("select cast('18446744073709551615' as signed);") - result.Check(testkit.Rows("-1")) - result = tk.MustQuery("select cast('18446744073709551614' as signed);") - result.Check(testkit.Rows("-2")) - result = tk.MustQuery("select cast(18446744073709551615 as unsigned);") - result.Check(testkit.Rows("18446744073709551615")) - result = tk.MustQuery("select cast(18446744073709551616 as unsigned);") - result.Check(testkit.Rows("18446744073709551615")) - result = tk.MustQuery("select cast(18446744073709551616 as signed);") - result.Check(testkit.Rows("9223372036854775807")) - result = tk.MustQuery("select cast(18446744073709551617 as signed);") - result.Check(testkit.Rows("9223372036854775807")) - result = tk.MustQuery("select cast(18446744073709551615 as signed);") - result.Check(testkit.Rows("-1")) - result = tk.MustQuery("select cast(18446744073709551614 as signed);") - result.Check(testkit.Rows("-2")) - result = tk.MustQuery("select cast(-18446744073709551616 as signed);") - result.Check(testkit.Rows("-9223372036854775808")) - result = tk.MustQuery("select cast(18446744073709551614.9 as unsigned);") // Round up - result.Check(testkit.Rows("18446744073709551615")) - result = tk.MustQuery("select cast(18446744073709551614.4 as unsigned);") // Round down - result.Check(testkit.Rows("18446744073709551614")) - result = tk.MustQuery("select cast(-9223372036854775809 as signed);") - result.Check(testkit.Rows("-9223372036854775808")) - result = tk.MustQuery("select cast(-9223372036854775809 as unsigned);") - result.Check(testkit.Rows("0")) - result = tk.MustQuery("select cast(-9223372036854775808 as unsigned);") - result.Check(testkit.Rows("9223372036854775808")) - result = tk.MustQuery("select cast('-9223372036854775809' as unsigned);") - result.Check(testkit.Rows("9223372036854775808")) - result = tk.MustQuery("select cast('-9223372036854775807' as unsigned);") - result.Check(testkit.Rows("9223372036854775809")) - result = tk.MustQuery("select cast('-2' as unsigned);") - result.Check(testkit.Rows("18446744073709551614")) - result = tk.MustQuery("select cast(cast(1-2 as unsigned) as signed integer);") - result.Check(testkit.Rows("-1")) - result = tk.MustQuery("select cast(1 as signed int)") - result.Check(testkit.Rows("1")) - - // test cast as double - result = tk.MustQuery("select cast(1 as double)") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(cast(12345 as unsigned) as double)") - result.Check(testkit.Rows("12345")) - result = tk.MustQuery("select cast(1.1 as double)") - result.Check(testkit.Rows("1.1")) - result = tk.MustQuery("select cast(-1.1 as double)") - result.Check(testkit.Rows("-1.1")) - result = tk.MustQuery("select cast('123.321' as double)") - result.Check(testkit.Rows("123.321")) - result = tk.MustQuery("select cast('12345678901234567890' as double) = 1.2345678901234567e19") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(-1 as double)") - result.Check(testkit.Rows("-1")) - result = tk.MustQuery("select cast(null as double)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select cast(12345678901234567890 as double) = 1.2345678901234567e19") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(cast(-1 as unsigned) as double) = 1.8446744073709552e19") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(1e100 as double) = 1e100") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(123456789012345678901234567890 as double) = 1.2345678901234568e29") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(0x12345678 as double)") - result.Check(testkit.Rows("305419896")) - - // test cast as float - result = tk.MustQuery("select cast(1 as float)") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(cast(12345 as unsigned) as float)") - result.Check(testkit.Rows("12345")) - result = tk.MustQuery("select cast(1.1 as float) = 1.1") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(-1.1 as float) = -1.1") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast('123.321' as float) =123.321") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast('12345678901234567890' as float) = 1.2345678901234567e19") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(-1 as float)") - result.Check(testkit.Rows("-1")) - result = tk.MustQuery("select cast(null as float)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select cast(12345678901234567890 as float) = 1.2345678901234567e19") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(cast(-1 as unsigned) as float) = 1.8446744073709552e19") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(1e100 as float(40)) = 1e100") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(123456789012345678901234567890 as float(40)) = 1.2345678901234568e29") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(0x12345678 as float(40)) = 305419896") - result.Check(testkit.Rows("1")) - - // test cast as real - result = tk.MustQuery("select cast(1 as real)") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(cast(12345 as unsigned) as real)") - result.Check(testkit.Rows("12345")) - result = tk.MustQuery("select cast(1.1 as real) = 1.1") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(-1.1 as real) = -1.1") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast('123.321' as real) =123.321") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast('12345678901234567890' as real) = 1.2345678901234567e19") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(-1 as real)") - result.Check(testkit.Rows("-1")) - result = tk.MustQuery("select cast(null as real)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select cast(12345678901234567890 as real) = 1.2345678901234567e19") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(cast(-1 as unsigned) as real) = 1.8446744073709552e19") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(1e100 as real) = 1e100") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(123456789012345678901234567890 as real) = 1.2345678901234568e29") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select cast(0x12345678 as real) = 305419896") - result.Check(testkit.Rows("1")) - - // test cast time as decimal overflow - tk.MustExec("drop table if exists t1") - tk.MustExec("create table t1(s1 time);") - tk.MustExec("insert into t1 values('11:11:11');") - result = tk.MustQuery("select cast(s1 as decimal(7, 2)) from t1;") - result.Check(testkit.Rows("99999.99")) - result = tk.MustQuery("select cast(s1 as decimal(8, 2)) from t1;") - result.Check(testkit.Rows("111111.00")) - _, err := tk.Exec("insert into t1 values(cast('111111.00' as decimal(7, 2)));") - require.Error(t, err) - - result = tk.MustQuery(`select CAST(0x8fffffffffffffff as signed) a, - CAST(0xfffffffffffffffe as signed) b, - CAST(0xffffffffffffffff as unsigned) c;`) - result.Check(testkit.Rows("-8070450532247928833 -2 18446744073709551615")) - - result = tk.MustQuery(`select cast("1:2:3" as TIME) = "1:02:03"`) - result.Check(testkit.Rows("0")) - - // fixed issue #3471 - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a time(6));") - tk.MustExec("insert into t value('12:59:59.999999')") - result = tk.MustQuery("select cast(a as signed) from t") - result.Check(testkit.Rows("130000")) - - // fixed issue #3762 - result = tk.MustQuery("select -9223372036854775809;") - result.Check(testkit.Rows("-9223372036854775809")) - result = tk.MustQuery("select --9223372036854775809;") - result.Check(testkit.Rows("9223372036854775809")) - result = tk.MustQuery("select -9223372036854775808;") - result.Check(testkit.Rows("-9223372036854775808")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a bigint(30));") - _, err = tk.Exec("insert into t values(-9223372036854775809)") - require.Error(t, err) - - // test case decimal precision less than the scale. - _, err = tk.Exec("select cast(12.1 as decimal(3, 4));") - require.Error(t, err, "[types:1427]For float(M,D), double(M,D) or decimal(M,D), M must be >= D (column '12.1').") - - // test case cast(EXPR as datetime(x)) precision more than the scale(6). - _, err = tk.Exec("SELECT CAST(1 AS DATETIME(7));") - require.Error(t, err, "[types:1427]Too big precision 7 specified for column 'CAST'. Maximum is 6.") - - // test unhex and hex - result = tk.MustQuery("select unhex('4D7953514C')") - result.Check(testkit.Rows("MySQL")) - result = tk.MustQuery("select unhex(hex('string'))") - result.Check(testkit.Rows("string")) - result = tk.MustQuery("select unhex('ggg')") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select unhex(-1)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select hex(unhex('1267'))") - result.Check(testkit.Rows("1267")) - result = tk.MustQuery("select hex(unhex(1267))") - result.Check(testkit.Rows("1267")) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a binary(8))") - tk.MustExec(`insert into t values('test')`) - result = tk.MustQuery("select hex(a) from t") - result.Check(testkit.Rows("7465737400000000")) - result = tk.MustQuery("select unhex(a) from t") - result.Check(testkit.Rows("")) - - // select from_unixtime - // NOTE (#17013): make from_unixtime stable in different timezone: the result of from_unixtime - // depends on the local time zone of the test environment, thus the result checking must - // consider the time zone convert. - tz := tk.Session().GetSessionVars().StmtCtx.TimeZone - result = tk.MustQuery("select from_unixtime(1451606400)") - unixTime := time.Unix(1451606400, 0).In(tz).String()[:19] - result.Check(testkit.Rows(unixTime)) - result = tk.MustQuery("select from_unixtime(14516064000/10)") - result.Check(testkit.Rows(fmt.Sprintf("%s.0000", unixTime))) - result = tk.MustQuery("select from_unixtime('14516064000'/10)") - result.Check(testkit.Rows(fmt.Sprintf("%s.000000", unixTime))) - result = tk.MustQuery("select from_unixtime(cast(1451606400 as double))") - result.Check(testkit.Rows(fmt.Sprintf("%s.000000", unixTime))) - result = tk.MustQuery("select from_unixtime(cast(cast(1451606400 as double) as DECIMAL))") - result.Check(testkit.Rows(unixTime)) - result = tk.MustQuery("select from_unixtime(cast(cast(1451606400 as double) as DECIMAL(65,1)))") - result.Check(testkit.Rows(fmt.Sprintf("%s.0", unixTime))) - result = tk.MustQuery("select from_unixtime(1451606400.123456)") - unixTime = time.Unix(1451606400, 123456000).In(tz).String()[:26] - result.Check(testkit.Rows(unixTime)) - result = tk.MustQuery("select from_unixtime(1451606400.1234567)") - unixTime = time.Unix(1451606400, 123456700).In(tz).Round(time.Microsecond).Format("2006-01-02 15:04:05.000000")[:26] - result.Check(testkit.Rows(unixTime)) - result = tk.MustQuery("select from_unixtime(1451606400.999999)") - unixTime = time.Unix(1451606400, 999999000).In(tz).String()[:26] - result.Check(testkit.Rows(unixTime)) - result = tk.MustQuery("select from_unixtime(1511247196661)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select from_unixtime('1451606400.123');") - unixTime = time.Unix(1451606400, 0).In(tz).String()[:19] - result.Check(testkit.Rows(fmt.Sprintf("%s.123000", unixTime))) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a int);") - tk.MustExec("insert into t value(1451606400);") - result = tk.MustQuery("select from_unixtime(a) from t;") - result.Check(testkit.Rows(unixTime)) - - // test strcmp - result = tk.MustQuery("select strcmp('abc', 'def')") - result.Check(testkit.Rows("-1")) - result = tk.MustQuery("select strcmp('abc', 'aba')") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select strcmp('abc', 'abc')") - result.Check(testkit.Rows("0")) - result = tk.MustQuery("select substr(null, 1, 2)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select substr('123', null, 2)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select substr('123', 1, null)") - result.Check(testkit.Rows("")) - - // for case - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a varchar(255), b int)") - tk.MustExec("insert t values ('str1', 1)") - result = tk.MustQuery("select * from t where a = case b when 1 then 'str1' when 2 then 'str2' end") - result.Check(testkit.Rows("str1 1")) - result = tk.MustQuery("select * from t where a = case b when 1 then 'str2' when 2 then 'str3' end") - result.Check(nil) - tk.MustExec("insert t values ('str2', 2)") - result = tk.MustQuery("select * from t where a = case b when 2 then 'str2' when 3 then 'str3' end") - result.Check(testkit.Rows("str2 2")) - tk.MustExec("insert t values ('str3', 3)") - result = tk.MustQuery("select * from t where a = case b when 4 then 'str4' when 5 then 'str5' else 'str3' end") - result.Check(testkit.Rows("str3 3")) - result = tk.MustQuery("select * from t where a = case b when 4 then 'str4' when 5 then 'str5' else 'str6' end") - result.Check(nil) - result = tk.MustQuery("select * from t where a = case when b then 'str3' when 1 then 'str1' else 'str2' end") - result.Check(testkit.Rows("str3 3")) - tk.MustExec("delete from t") - tk.MustExec("insert t values ('str2', 0)") - result = tk.MustQuery("select * from t where a = case when b then 'str3' when 0 then 'str1' else 'str2' end") - result.Check(testkit.Rows("str2 0")) - tk.MustExec("insert t values ('str1', null)") - result = tk.MustQuery("select * from t where a = case b when null then 'str3' when 10 then 'str1' else 'str2' end") - result.Check(testkit.Rows("str2 0")) - result = tk.MustQuery("select * from t where a = case null when b then 'str3' when 10 then 'str1' else 'str2' end") - result.Check(testkit.Rows("str2 0")) - tk.MustExec("insert t values (null, 4)") - result = tk.MustQuery("select * from t where b < case a when null then 0 when 'str2' then 0 else 9 end") - result.Check(testkit.Rows(" 4")) - result = tk.MustQuery("select * from t where b = case when a is null then 4 when a = 'str5' then 7 else 9 end") - result.Check(testkit.Rows(" 4")) - result = tk.MustQuery(`SELECT -Max(+23) * -+Cast(--10 AS SIGNED) * -CASE - WHEN 0 > 85 THEN NULL - WHEN NOT - CASE +55 - WHEN +( +82 ) + -89 * -69 THEN +Count(-88) - WHEN +CASE 57 - WHEN +89 THEN -89 * Count(*) - WHEN 17 THEN NULL - END THEN ( -10 ) - END IS NULL THEN NULL - ELSE 83 + 48 - END AS col0; `) - result.Check(testkit.Rows("-30130")) - - // return type of case when expr should not include NotNullFlag. issue-23036 - tk.MustExec("drop table if exists t1") - tk.MustExec("create table t1(c1 int not null)") - tk.MustExec("insert into t1 values(1)") - result = tk.MustQuery("select (case when null then c1 end) is null from t1") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select (case when null then c1 end) is not null from t1") - result.Check(testkit.Rows("0")) - - // test warnings - tk.MustQuery("select case when b=0 then 1 else 1/b end from t") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select if(b=0, 1, 1/b) from t") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select ifnull(b, b/0) from t") - tk.MustQuery("show warnings").Check(testkit.Rows()) - - tk.MustQuery("select case when 1 then 1 else 1/0 end") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery(" select if(1,1,1/0)") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select ifnull(1, 1/0)") - tk.MustQuery("show warnings").Check(testkit.Rows()) - - tk.MustExec("delete from t") - tk.MustExec("insert t values ('str2', 0)") - tk.MustQuery("select case when b < 1 then 1 else 1/0 end from t") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select case when b < 1 then 1 when 1/0 then b else 1/0 end from t") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select if(b < 1 , 1, 1/0) from t") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select ifnull(b, 1/0) from t") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select COALESCE(1, b, b/0) from t") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select 0 and b/0 from t") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select 1 or b/0 from t") - tk.MustQuery("show warnings").Check(testkit.Rows()) - - tk.MustQuery("select 1 or 1/0") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select 0 and 1/0") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select COALESCE(1, 1/0)") - tk.MustQuery("show warnings").Check(testkit.Rows()) - tk.MustQuery("select interval(1,0,1,2,1/0)") - tk.MustQuery("show warnings").Check(testkit.Rows()) - - tk.MustQuery("select case 2.0 when 2.0 then 3.0 when 3.0 then 2.0 end").Check(testkit.Rows("3.0")) - tk.MustQuery("select case 2.0 when 3.0 then 2.0 when 4.0 then 3.0 else 5.0 end").Check(testkit.Rows("5.0")) - tk.MustQuery("select case cast('2011-01-01' as date) when cast('2011-01-01' as date) then cast('2011-02-02' as date) end").Check(testkit.Rows("2011-02-02")) - tk.MustQuery("select case cast('2012-01-01' as date) when cast('2011-01-01' as date) then cast('2011-02-02' as date) else cast('2011-03-03' as date) end").Check(testkit.Rows("2011-03-03")) - tk.MustQuery("select case cast('10:10:10' as time) when cast('10:10:10' as time) then cast('11:11:11' as time) end").Check(testkit.Rows("11:11:11")) - tk.MustQuery("select case cast('10:10:13' as time) when cast('10:10:10' as time) then cast('11:11:11' as time) else cast('22:22:22' as time) end").Check(testkit.Rows("22:22:22")) - - // for cast - result = tk.MustQuery("select cast(1234 as char(3))") - result.Check(testkit.Rows("123")) - result = tk.MustQuery("select cast(1234 as char(0))") - result.Check(testkit.Rows("")) - result = tk.MustQuery("show warnings") - result.Check(testkit.Rows("Warning 1406 Data Too Long, field len 0, data len 4")) - result = tk.MustQuery("select CAST( - 8 AS DECIMAL ) * + 52 + 87 < - 86") - result.Check(testkit.Rows("1")) - - // for char - result = tk.MustQuery("select char(97, 100, 256, 89)") - result.Check(testkit.Rows("ad\x01\x00Y")) - result = tk.MustQuery("select char(97, null, 100, 256, 89)") - result.Check(testkit.Rows("ad\x01\x00Y")) - result = tk.MustQuery("select char(97, null, 100, 256, 89 using utf8)") - result.Check(testkit.Rows("ad\x01\x00Y")) - result = tk.MustQuery("select char(97, null, 100, 256, 89 using ascii)") - result.Check(testkit.Rows("ad\x01\x00Y")) - err = tk.ExecToErr("select char(97, null, 100, 256, 89 using tidb)") - require.Error(t, err, "[parser:1115]Unknown character set: 'tidb'") - - // issue 3884 - tk.MustExec("drop table if exists t") - tk.MustExec("CREATE TABLE t (c1 date, c2 datetime, c3 timestamp, c4 time, c5 year);") - tk.MustExec("INSERT INTO t values ('2000-01-01', '2000-01-01 12:12:12', '2000-01-01 12:12:12', '12:12:12', '2000');") - tk.MustExec("INSERT INTO t values ('2000-02-01', '2000-02-01 12:12:12', '2000-02-01 12:12:12', '13:12:12', 2000);") - tk.MustExec("INSERT INTO t values ('2000-03-01', '2000-03-01', '2000-03-01 12:12:12', '1 12:12:12', 2000);") - tk.MustExec("INSERT INTO t SET c1 = '2000-04-01', c2 = '2000-04-01', c3 = '2000-04-01 12:12:12', c4 = '-1 13:12:12', c5 = 2000;") - result = tk.MustQuery("SELECT c4 FROM t where c4 < '-13:12:12';") - result.Check(testkit.Rows("-37:12:12")) - result = tk.MustQuery(`SELECT 1 DIV - - 28 + ( - SUM( - + 25 ) ) * - CASE - 18 WHEN 44 THEN NULL ELSE - 41 + 32 + + - 70 - + COUNT( - 95 ) * 15 END + 92`) - result.Check(testkit.Rows("2442")) - - // for regexp, rlike - // https://github.com/pingcap/tidb/issues/4080 - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t (a char(10), b varchar(10), c binary(10), d varbinary(10));`) - tk.MustExec(`insert into t values ('text','text','text','text');`) - result = tk.MustQuery(`select a regexp 'xt' from t;`) - result.Check(testkit.Rows("1")) - result = tk.MustQuery(`select b regexp 'xt' from t;`) - result.Check(testkit.Rows("1")) - result = tk.MustQuery(`select b regexp binary 'Xt' from t;`) - result.Check(testkit.Rows("0")) - result = tk.MustQuery(`select c regexp 'Xt' from t;`) - result.Check(testkit.Rows("0")) - result = tk.MustQuery(`select d regexp 'Xt' from t;`) - result.Check(testkit.Rows("0")) - result = tk.MustQuery(`select a rlike 'xt' from t;`) - result.Check(testkit.Rows("1")) - result = tk.MustQuery(`select a rlike binary 'Xt' from t;`) - result.Check(testkit.Rows("0")) - result = tk.MustQuery(`select b rlike 'xt' from t;`) - result.Check(testkit.Rows("1")) - result = tk.MustQuery(`select c rlike 'Xt' from t;`) - result.Check(testkit.Rows("0")) - result = tk.MustQuery(`select d rlike 'Xt' from t;`) - result.Check(testkit.Rows("0")) - result = tk.MustQuery(`select 'a' regexp 'A', 'a' regexp binary 'A'`) - result.Check(testkit.Rows("0 0")) - - // testCase is for like and regexp - type testCase struct { - pattern string - val string - result int - } - patternMatching := func(tk *testkit.TestKit, queryOp string, data []testCase) { - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a varchar(255), b int)") - for i, d := range data { - tk.MustExec(fmt.Sprintf("insert into t values('%s', %d)", d.val, i)) - result = tk.MustQuery(fmt.Sprintf("select * from t where a %s '%s'", queryOp, d.pattern)) - if d.result == 1 { - rowStr := fmt.Sprintf("%s %d", d.val, i) - result.Check(testkit.Rows(rowStr)) - } else { - result.Check(nil) - } - tk.MustExec(fmt.Sprintf("delete from t where b = %d", i)) - } - } - // for like - likeTests := []testCase{ - {"a", "a", 1}, - {"a", "b", 0}, - {"aA", "Aa", 0}, - {`aA%`, "aAab", 1}, - {"aA_", "Aaab", 0}, - {"Aa_", "Aab", 1}, - {"", "", 1}, - {"", "a", 0}, - } - patternMatching(tk, "like", likeTests) - // for regexp - likeTests = []testCase{ - {"^$", "a", 0}, - {"a", "a", 1}, - {"a", "b", 0}, - {"aA", "aA", 1}, - {".", "a", 1}, - {"^.$", "ab", 0}, - {"..", "b", 0}, - {".ab", "aab", 1}, - {"ab.", "abcd", 1}, - {".*", "abcd", 1}, - } - patternMatching(tk, "regexp", likeTests) - - // for #9838 - result = tk.MustQuery("select cast(1 as signed) + cast(9223372036854775807 as unsigned);") - result.Check(testkit.Rows("9223372036854775808")) - result = tk.MustQuery("select cast(9223372036854775807 as unsigned) + cast(1 as signed);") - result.Check(testkit.Rows("9223372036854775808")) - err = tk.QueryToErr("select cast(9223372036854775807 as signed) + cast(9223372036854775809 as unsigned);") - require.Error(t, err) - err = tk.QueryToErr("select cast(9223372036854775809 as unsigned) + cast(9223372036854775807 as signed);") - require.Error(t, err) - err = tk.QueryToErr("select cast(-9223372036854775807 as signed) + cast(9223372036854775806 as unsigned);") - require.Error(t, err) - err = tk.QueryToErr("select cast(9223372036854775806 as unsigned) + cast(-9223372036854775807 as signed);") - require.Error(t, err) - - result = tk.MustQuery(`select 1 / '2007' div 1;`) - result.Check(testkit.Rows("0")) -} - func TestSetVariables(t *testing.T) { store := testkit.CreateMockStore(t) @@ -2694,27 +1459,6 @@ func TestCacheConstEval(t *testing.T) { tk.MustExec("admin reload expr_pushdown_blacklist") } -func TestNullValueRange(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int, b int, index(a))") - tk.MustExec("insert into t values (null, 0), (null, 1), (10, 11), (10, 12)") - tk.MustQuery("select * from t use index(a) where a is null order by b").Check(testkit.Rows(" 0", " 1")) - tk.MustQuery("select * from t use index(a) where a<=>null order by b").Check(testkit.Rows(" 0", " 1")) - tk.MustQuery("select * from t use index(a) where a<=>10 order by b").Check(testkit.Rows("10 11", "10 12")) - - tk.MustExec("drop table if exists t1") - tk.MustExec("create table t1(a int, b int, c int, unique key(a, b, c))") - tk.MustExec("insert into t1 values (1, null, 1), (1, null, 2), (1, null, 3), (1, null, 4)") - tk.MustExec("insert into t1 values (1, 1, 1), (1, 2, 2), (1, 3, 33), (1, 4, 44)") - tk.MustQuery("select c from t1 where a=1 and b<=>null and c>2 order by c").Check(testkit.Rows("3", "4")) - tk.MustQuery("select c from t1 where a=1 and b is null and c>2 order by c").Check(testkit.Rows("3", "4")) - tk.MustQuery("select c from t1 where a=1 and b is not null and c>2 order by c").Check(testkit.Rows("33", "44")) -} - // issues 14448, 19383, 17734 func TestNoopFunctions(t *testing.T) { store := testkit.CreateMockStore(t) @@ -2907,34 +1651,6 @@ PARTITION BY RANGE (c) ( tk.MustExec("set global tidb_enable_local_txn = off;") } -func TestPartitionPruningRelaxOP(t *testing.T) { - // Discovered while looking at issue 19941 (not completely related) - // relaxOP relax the op > to >= and < to <= - // Sometime we need to relax the condition, for example: - // col < const => f(col) <= const - // datetime < 2020-02-11 16:18:42 => to_days(datetime) <= to_days(2020-02-11) - // We can't say: - // datetime < 2020-02-11 16:18:42 => to_days(datetime) < to_days(2020-02-11) - - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - tk.MustExec("DROP TABLE IF EXISTS t1;") - tk.MustExec(`CREATE TABLE t1 (d date NOT NULL) PARTITION BY RANGE (YEAR(d)) - (PARTITION p2016 VALUES LESS THAN (2017), PARTITION p2017 VALUES LESS THAN (2018), PARTITION p2018 VALUES LESS THAN (2019), - PARTITION p2019 VALUES LESS THAN (2020), PARTITION pmax VALUES LESS THAN MAXVALUE)`) - - tk.MustExec(`INSERT INTO t1 VALUES ('2016-01-01'), ('2016-06-01'), ('2016-09-01'), ('2017-01-01'), - ('2017-06-01'), ('2017-09-01'), ('2018-01-01'), ('2018-06-01'), ('2018-09-01'), ('2018-10-01'), - ('2018-11-01'), ('2018-12-01'), ('2018-12-31'), ('2019-01-01'), ('2019-06-01'), ('2019-09-01'), - ('2020-01-01'), ('2020-06-01'), ('2020-09-01');`) - - tk.MustQuery("SELECT COUNT(*) FROM t1 WHERE d < '2018-01-01'").Check(testkit.Rows("6")) - tk.MustQuery("SELECT COUNT(*) FROM t1 WHERE d > '2018-01-01'").Check(testkit.Rows("12")) -} - func TestTiDBRowChecksumBuiltin(t *testing.T) { store := testkit.CreateMockStore(t) diff --git a/expression/integration_test/BUILD.bazel b/expression/integration_test/BUILD.bazel index 37a8f62afde99..7c8c025321f50 100644 --- a/expression/integration_test/BUILD.bazel +++ b/expression/integration_test/BUILD.bazel @@ -8,7 +8,7 @@ go_test( "main_test.go", ], flaky = True, - shard_count = 50, + shard_count = 41, deps = [ "//config", "//domain", @@ -31,7 +31,6 @@ go_test( "//util/codec", "//util/collate", "//util/sem", - "//util/sqlexec", "//util/timeutil", "//util/versioninfo", "@com_github_pingcap_errors//:errors", diff --git a/expression/integration_test/integration_test.go b/expression/integration_test/integration_test.go index 6de5266f8a9c9..28a318701761a 100644 --- a/expression/integration_test/integration_test.go +++ b/expression/integration_test/integration_test.go @@ -46,63 +46,11 @@ import ( "github.com/pingcap/tidb/util/codec" "github.com/pingcap/tidb/util/collate" "github.com/pingcap/tidb/util/sem" - "github.com/pingcap/tidb/util/sqlexec" "github.com/pingcap/tidb/util/versioninfo" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -func TestFuncREPEAT(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - - tk.MustExec("USE test") - tk.MustExec("DROP TABLE IF EXISTS table_string;") - tk.MustExec("CREATE TABLE table_string(a CHAR(20), b VARCHAR(20), c TINYTEXT, d TEXT(20), e MEDIUMTEXT, f LONGTEXT, g BIGINT);") - tk.MustExec("INSERT INTO table_string (a, b, c, d, e, f, g) VALUES ('a', 'b', 'c', 'd', 'e', 'f', 2);") - tk.CheckExecResult(1, 0) - - r := tk.MustQuery("SELECT REPEAT(a, g), REPEAT(b, g), REPEAT(c, g), REPEAT(d, g), REPEAT(e, g), REPEAT(f, g) FROM table_string;") - r.Check(testkit.Rows("aa bb cc dd ee ff")) - - r = tk.MustQuery("SELECT REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g) FROM table_string;") - r.Check(testkit.Rows(" ")) - - r = tk.MustQuery("SELECT REPEAT(a, NULL), REPEAT(b, NULL), REPEAT(c, NULL), REPEAT(d, NULL), REPEAT(e, NULL), REPEAT(f, NULL) FROM table_string;") - r.Check(testkit.Rows(" ")) - - r = tk.MustQuery("SELECT REPEAT(a, 2), REPEAT(b, 2), REPEAT(c, 2), REPEAT(d, 2), REPEAT(e, 2), REPEAT(f, 2) FROM table_string;") - r.Check(testkit.Rows("aa bb cc dd ee ff")) - - r = tk.MustQuery("SELECT REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2) FROM table_string;") - r.Check(testkit.Rows(" ")) - - r = tk.MustQuery("SELECT REPEAT(a, -1), REPEAT(b, -2), REPEAT(c, -2), REPEAT(d, -2), REPEAT(e, -2), REPEAT(f, -2) FROM table_string;") - r.Check(testkit.Rows(" ")) - - r = tk.MustQuery("SELECT REPEAT(a, 0), REPEAT(b, 0), REPEAT(c, 0), REPEAT(d, 0), REPEAT(e, 0), REPEAT(f, 0) FROM table_string;") - r.Check(testkit.Rows(" ")) - - r = tk.MustQuery("SELECT REPEAT(a, 16777217), REPEAT(b, 16777217), REPEAT(c, 16777217), REPEAT(d, 16777217), REPEAT(e, 16777217), REPEAT(f, 16777217) FROM table_string;") - r.Check(testkit.Rows(" ")) -} - -func TestFuncLpadAndRpad(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - - tk.MustExec(`USE test;`) - tk.MustExec(`DROP TABLE IF EXISTS t;`) - tk.MustExec(`CREATE TABLE t(a BINARY(10), b CHAR(10));`) - tk.MustExec(`INSERT INTO t SELECT "中文", "abc";`) - result := tk.MustQuery(`SELECT LPAD(a, 11, "a"), LPAD(b, 2, "xx") FROM t;`) - result.Check(testkit.Rows("a中文\x00\x00\x00\x00 ab")) - result = tk.MustQuery(`SELECT RPAD(a, 11, "a"), RPAD(b, 2, "xx") FROM t;`) - result.Check(testkit.Rows("中文\x00\x00\x00\x00a ab")) -} - func TestGetLock(t *testing.T) { ctx := context.Background() store := testkit.CreateMockStore(t) @@ -340,472 +288,6 @@ func TestMiscellaneousBuiltin(t *testing.T) { tk.MustQuery(`SELECT RELEASE_ALL_LOCKS()`).Check(testkit.Rows("0")) // none acquired } -func TestConvertToBit(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t, t1") - tk.MustExec("create table t (a bit(64))") - tk.MustExec("create table t1 (a varchar(2))") - tk.MustExec(`insert t1 value ('10')`) - tk.MustExec(`insert t select a from t1`) - tk.MustQuery("select a+0 from t").Check(testkit.Rows("12592")) - - tk.MustExec("drop table if exists t, t1") - tk.MustExec("create table t (a bit(64))") - tk.MustExec("create table t1 (a binary(2))") - tk.MustExec(`insert t1 value ('10')`) - tk.MustExec(`insert t select a from t1`) - tk.MustQuery("select a+0 from t").Check(testkit.Rows("12592")) - - tk.MustExec("drop table if exists t, t1") - tk.MustExec("create table t (a bit(64))") - tk.MustExec("create table t1 (a datetime)") - tk.MustExec(`insert t1 value ('09-01-01')`) - tk.MustExec(`insert t select a from t1`) - tk.MustQuery("select a+0 from t").Check(testkit.Rows("20090101000000")) -} - -func TestStringBuiltin(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - var err error - - // for length - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int, b double, c datetime, d time, e char(20), f bit(10))") - tk.MustExec(`insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef", 0b10101)`) - result := tk.MustQuery("select length(a), length(b), length(c), length(d), length(e), length(f), length(null) from t") - result.Check(testkit.Rows("1 3 19 8 6 2 ")) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a char(20))") - tk.MustExec(`insert into t values("tidb "), (concat("a ", "b "))`) - result = tk.MustQuery("select a, length(a) from t") - result.Check(testkit.Rows("tidb 4", "a b 4")) - - // for concat - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int, b double, c datetime, d time, e char(20))") - tk.MustExec(`insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef")`) - result = tk.MustQuery("select concat(a, b, c, d, e) from t") - result.Check(testkit.Rows("11.12017-01-01 12:01:0112:01:01abcdef")) - result = tk.MustQuery("select concat(null)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select concat(null, a, b) from t") - result.Check(testkit.Rows("")) - - // for concat_ws - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int, b double, c datetime, d time, e char(20))") - tk.MustExec(`insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef")`) - result = tk.MustQuery("select concat_ws('|', a, b, c, d, e) from t") - result.Check(testkit.Rows("1|1.1|2017-01-01 12:01:01|12:01:01|abcdef")) - result = tk.MustQuery("select concat_ws(null, null)") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select concat_ws(null, a, b) from t") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select concat_ws(',', 'a', 'b')") - result.Check(testkit.Rows("a,b")) - result = tk.MustQuery("select concat_ws(',','First name',NULL,'Last Name')") - result.Check(testkit.Rows("First name,Last Name")) - - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(a tinyint(2), b varchar(10));`) - tk.MustExec(`insert into t values (1, 'a'), (12, 'a'), (126, 'a'), (127, 'a')`) - tk.MustQuery(`select concat_ws('#', a, b) from t;`).Check(testkit.Rows( - `1#a`, - `12#a`, - `126#a`, - `127#a`, - )) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a binary(3))") - tk.MustExec("insert into t values('a')") - result = tk.MustQuery(`select concat_ws(',', a, 'test') = 'a\0\0,test' from t`) - result.Check(testkit.Rows("1")) - - // for ascii - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a char(10), b int, c double, d datetime, e time, f bit(4))") - tk.MustExec(`insert into t values('2', 2, 2.3, "2017-01-01 12:01:01", "12:01:01", 0b1010)`) - result = tk.MustQuery("select ascii(a), ascii(b), ascii(c), ascii(d), ascii(e), ascii(f) from t") - result.Check(testkit.Rows("50 50 50 50 49 10")) - result = tk.MustQuery("select ascii('123'), ascii(123), ascii(''), ascii('你好'), ascii(NULL)") - result.Check(testkit.Rows("49 49 0 228 ")) - - // for lower - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int, b double, c datetime, d time, e char(20), f binary(3), g binary(3))") - tk.MustExec(`insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef", 'aa', 'BB')`) - result = tk.MustQuery("select lower(a), lower(b), lower(c), lower(d), lower(e), lower(f), lower(g), lower(null) from t") - result.Check(testkit.Rows("1 1.1 2017-01-01 12:01:01 12:01:01 abcdef aa\x00 BB\x00 ")) - - // for upper - result = tk.MustQuery("select upper(a), upper(b), upper(c), upper(d), upper(e), upper(f), upper(g), upper(null) from t") - result.Check(testkit.Rows("1 1.1 2017-01-01 12:01:01 12:01:01 ABCDEF aa\x00 BB\x00 ")) - - // for strcmp - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a char(10), b int, c double, d datetime, e time)") - tk.MustExec(`insert into t values("123", 123, 12.34, "2017-01-01 12:01:01", "12:01:01")`) - result = tk.MustQuery(`select strcmp(a, "123"), strcmp(b, "123"), strcmp(c, "12.34"), strcmp(d, "2017-01-01 12:01:01"), strcmp(e, "12:01:01") from t`) - result.Check(testkit.Rows("0 0 0 0 0")) - result = tk.MustQuery(`select strcmp("1", "123"), strcmp("123", "1"), strcmp("123", "45"), strcmp("123", null), strcmp(null, "123")`) - result.Check(testkit.Rows("-1 1 -1 ")) - result = tk.MustQuery(`select strcmp("", "123"), strcmp("123", ""), strcmp("", ""), strcmp("", null), strcmp(null, "")`) - result.Check(testkit.Rows("-1 1 0 ")) - - // for left - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a char(10), b int, c double, d datetime, e time)") - tk.MustExec(`insert into t values('abcde', 1234, 12.34, "2017-01-01 12:01:01", "12:01:01")`) - result = tk.MustQuery("select left(a, 2), left(b, 2), left(c, 2), left(d, 2), left(e, 2) from t") - result.Check(testkit.Rows("ab 12 12 20 12")) - result = tk.MustQuery(`select left("abc", 0), left("abc", -1), left(NULL, 1), left("abc", NULL)`) - result.Check(testkit.Rows(" ")) - result = tk.MustQuery(`select left("abc", "a"), left("abc", 1.9), left("abc", 1.2)`) - result.Check(testkit.Rows(" ab a")) - result = tk.MustQuery(`select left("中文abc", 2), left("中文abc", 3), left("中文abc", 4)`) - result.Check(testkit.Rows("中文 中文a 中文ab")) - // for right, reuse the table created for left - result = tk.MustQuery("select right(a, 3), right(b, 3), right(c, 3), right(d, 3), right(e, 3) from t") - result.Check(testkit.Rows("cde 234 .34 :01 :01")) - result = tk.MustQuery(`select right("abcde", 0), right("abcde", -1), right("abcde", 100), right(NULL, 1), right("abcde", NULL)`) - result.Check(testkit.Rows(" abcde ")) - result = tk.MustQuery(`select right("abcde", "a"), right("abcde", 1.9), right("abcde", 1.2)`) - result.Check(testkit.Rows(" de e")) - result = tk.MustQuery(`select right("中文abc", 2), right("中文abc", 4), right("中文abc", 5)`) - result.Check(testkit.Rows("bc 文abc 中文abc")) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a binary(10))") - tk.MustExec(`insert into t select "中文abc"`) - result = tk.MustQuery(`select left(a, 3), left(a, 6), left(a, 7) from t`) - result.Check(testkit.Rows("中 中文 中文a")) - result = tk.MustQuery(`select right(a, 2), right(a, 7) from t`) - result.Check(testkit.Rows("c\x00 文abc\x00")) - - // for ord - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a char(10), b int, c double, d datetime, e time, f bit(4), g binary(20), h blob(10), i text(30))") - tk.MustExec(`insert into t values('2', 2, 2.3, "2017-01-01 12:01:01", "12:01:01", 0b1010, "512", "48", "tidb")`) - result = tk.MustQuery("select ord(a), ord(b), ord(c), ord(d), ord(e), ord(f), ord(g), ord(h), ord(i) from t") - result.Check(testkit.Rows("50 50 50 50 49 10 53 52 116")) - result = tk.MustQuery("select ord('123'), ord(123), ord(''), ord('你好'), ord(NULL), ord('👍')") - result.Check(testkit.Rows("49 49 0 14990752 4036989325")) - result = tk.MustQuery("select ord(X''), ord(X'6161'), ord(X'e4bd'), ord(X'e4bda0'), ord(_ascii'你'), ord(_latin1'你')") - result.Check(testkit.Rows("0 97 228 228 228 228")) - - // for space - result = tk.MustQuery(`select space(0), space(2), space(-1), space(1.1), space(1.9)`) - result.Check(testkit.RowsWithSep(",", ", ,, , ")) - result = tk.MustQuery(`select space("abc"), space("2"), space("1.1"), space(''), space(null)`) - result.Check(testkit.RowsWithSep(",", ", , ,,")) - - // for replace - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a char(20), b int, c double, d datetime, e time)") - tk.MustExec(`insert into t values('www.mysql.com', 1234, 12.34, "2017-01-01 12:01:01", "12:01:01")`) - result = tk.MustQuery(`select replace(a, 'mysql', 'pingcap'), replace(b, 2, 55), replace(c, 34, 0), replace(d, '-', '/'), replace(e, '01', '22') from t`) - result.Check(testkit.RowsWithSep(",", "www.pingcap.com,15534,12.0,2017/01/01 12:01:01,12:22:22")) - result = tk.MustQuery(`select replace('aaa', 'a', ''), replace(null, 'a', 'b'), replace('a', null, 'b'), replace('a', 'b', null)`) - result.Check(testkit.Rows(" ")) - - // for tobase64 - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int, b double, c datetime, d time, e char(20), f bit(10), g binary(20), h blob(10))") - tk.MustExec(`insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef", 0b10101, "512", "abc")`) - result = tk.MustQuery("select to_base64(a), to_base64(b), to_base64(c), to_base64(d), to_base64(e), to_base64(f), to_base64(g), to_base64(h), to_base64(null) from t") - result.Check(testkit.Rows("MQ== MS4x MjAxNy0wMS0wMSAxMjowMTowMQ== MTI6MDE6MDE= YWJjZGVm ABU= NTEyAAAAAAAAAAAAAAAAAAAAAAA= YWJj ")) - - // for from_base64 - result = tk.MustQuery(`select from_base64("abcd"), from_base64("asc")`) - result.Check(testkit.Rows("i\xb7\x1d ")) - result = tk.MustQuery(`select from_base64("MQ=="), from_base64(1234)`) - result.Check(testkit.Rows("1 \xd7m\xf8")) - - // for substr - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a char(10), b int, c double, d datetime, e time)") - tk.MustExec(`insert into t values('Sakila', 12345, 123.45, "2017-01-01 12:01:01", "12:01:01")`) - result = tk.MustQuery(`select substr(a, 3), substr(b, 2, 3), substr(c, -3), substr(d, -8), substr(e, -3, 100) from t`) - result.Check(testkit.Rows("kila 234 .45 12:01:01 :01")) - result = tk.MustQuery(`select substr('Sakila', 100), substr('Sakila', -100), substr('Sakila', -5, 3), substr('Sakila', 2, -1)`) - result.Check(testkit.RowsWithSep(",", ",,aki,")) - result = tk.MustQuery(`select substr('foobarbar' from 4), substr('Sakila' from -4 for 2)`) - result.Check(testkit.Rows("barbar ki")) - result = tk.MustQuery(`select substr(null, 2, 3), substr('foo', null, 3), substr('foo', 2, null)`) - result.Check(testkit.Rows(" ")) - result = tk.MustQuery(`select substr('中文abc', 2), substr('中文abc', 3), substr("中文abc", 1, 2)`) - result.Check(testkit.Rows("文abc abc 中文")) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a binary(10))") - tk.MustExec(`insert into t select "中文abc"`) - result = tk.MustQuery(`select substr(a, 4), substr(a, 1, 3), substr(a, 1, 6) from t`) - result.Check(testkit.Rows("文abc\x00 中 中文")) - result = tk.MustQuery(`select substr("string", -1), substr("string", -2), substr("中文", -1), substr("中文", -2) from t`) - result.Check(testkit.Rows("g ng 文 中文")) - - // for bit_length - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int, b double, c datetime, d time, e char(20), f bit(10), g binary(20), h varbinary(20))") - tk.MustExec(`insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef", 0b10101, "g", "h")`) - result = tk.MustQuery("select bit_length(a), bit_length(b), bit_length(c), bit_length(d), bit_length(e), bit_length(f), bit_length(g), bit_length(h), bit_length(null) from t") - result.Check(testkit.Rows("8 24 152 64 48 16 160 8 ")) - - // for substring_index - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a char(20), b int, c double, d datetime, e time)") - tk.MustExec(`insert into t values('www.pingcap.com', 12345, 123.45, "2017-01-01 12:01:01", "12:01:01")`) - result = tk.MustQuery(`select substring_index(a, '.', 2), substring_index(b, '.', 2), substring_index(c, '.', -1), substring_index(d, '-', 1), substring_index(e, ':', -2) from t`) - result.Check(testkit.Rows("www.pingcap 12345 45 2017 01:01")) - result = tk.MustQuery(`select substring_index('www.pingcap.com', '.', 0), substring_index('www.pingcap.com', '.', 100), substring_index('www.pingcap.com', '.', -100)`) - result.Check(testkit.Rows(" www.pingcap.com www.pingcap.com")) - result = tk.MustQuery(`select substring_index('www.pingcap.com', 'd', 1), substring_index('www.pingcap.com', '', 1), substring_index('', '.', 1)`) - result.Check(testkit.RowsWithSep(",", "www.pingcap.com,,")) - result = tk.MustQuery(`select substring_index(null, '.', 1), substring_index('www.pingcap.com', null, 1), substring_index('www.pingcap.com', '.', null)`) - result.Check(testkit.Rows(" ")) - - // for substring_index with overflow - tk.MustQuery(`select substring_index('xyz', 'abc', 9223372036854775808)`).Check(testkit.Rows(`xyz`)) - tk.MustQuery(`select substring_index("aaa.bbb.ccc.ddd.eee",'.',18446744073709551613);`).Check(testkit.Rows(`aaa.bbb.ccc.ddd.eee`)) - tk.MustQuery(`select substring_index("aaa.bbb.ccc.ddd.eee",'.',-18446744073709551613);`).Check(testkit.Rows(`aaa.bbb.ccc.ddd.eee`)) - tk.MustQuery(`select substring_index('aaa.bbb.ccc.ddd.eee', '.', 18446744073709551615 - 1 + id) from (select 1 as id) as t1`).Check(testkit.Rows(`aaa.bbb.ccc.ddd.eee`)) - tk.MustQuery(`select substring_index('aaa.bbb.ccc.ddd.eee', '.', -18446744073709551615 - 1 + id) from (select 1 as id) as t1`).Check(testkit.Rows(`aaa.bbb.ccc.ddd.eee`)) - - tk.MustExec("set tidb_enable_vectorized_expression = 0;") - tk.MustQuery(`select substring_index("aaa.bbb.ccc.ddd.eee",'.',18446744073709551613);`).Check(testkit.Rows(`aaa.bbb.ccc.ddd.eee`)) - tk.MustQuery(`select substring_index("aaa.bbb.ccc.ddd.eee",'.',-18446744073709551613);`).Check(testkit.Rows(`aaa.bbb.ccc.ddd.eee`)) - tk.MustQuery(`select substring_index('aaa.bbb.ccc.ddd.eee', '.', 18446744073709551615 - 1 + id) from (select 1 as id) as t1`).Check(testkit.Rows(`aaa.bbb.ccc.ddd.eee`)) - tk.MustQuery(`select substring_index('aaa.bbb.ccc.ddd.eee', '.', -18446744073709551615 - 1 + id) from (select 1 as id) as t1`).Check(testkit.Rows(`aaa.bbb.ccc.ddd.eee`)) - tk.MustExec("set tidb_enable_vectorized_expression = 1;") - - // for hex - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a char(20), b int, c double, d datetime, e time, f decimal(5, 2), g bit(4))") - tk.MustExec(`insert into t values('www.pingcap.com', 12345, 123.45, "2017-01-01 12:01:01", "12:01:01", 123.45, 0b1100)`) - result = tk.MustQuery(`select hex(a), hex(b), hex(c), hex(d), hex(e), hex(f), hex(g) from t`) - result.Check(testkit.Rows("7777772E70696E676361702E636F6D 3039 7B 323031372D30312D30312031323A30313A3031 31323A30313A3031 7B C")) - result = tk.MustQuery(`select hex('abc'), hex('你好'), hex(12), hex(12.3), hex(12.8)`) - result.Check(testkit.Rows("616263 E4BDA0E5A5BD C C D")) - result = tk.MustQuery(`select hex(-1), hex(-12.3), hex(-12.8), hex(0x12), hex(null)`) - result.Check(testkit.Rows("FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFF4 FFFFFFFFFFFFFFF3 12 ")) - tk.MustExec("drop table if exists t") - tk.MustExec("CREATE TABLE t(i int primary key auto_increment, a binary, b binary(0), c binary(20), d binary(255)) character set utf8 collate utf8_bin;") - tk.MustExec("insert into t(a, b, c, d) values ('a', NULL, 'a','a');") - tk.MustQuery("select i, hex(a), hex(b), hex(c), hex(d) from t;").Check(testkit.Rows("1 61 6100000000000000000000000000000000000000 610000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")) - - // for unhex - result = tk.MustQuery(`select unhex('4D7953514C'), unhex('313233'), unhex(313233), unhex('')`) - result.Check(testkit.Rows("MySQL 123 123 ")) - result = tk.MustQuery(`select unhex('string'), unhex('你好'), unhex(123.4), unhex(null)`) - result.Check(testkit.Rows(" ")) - - // for ltrim and rtrim - result = tk.MustQuery(`select ltrim(' bar '), ltrim('bar'), ltrim(''), ltrim(null)`) - result.Check(testkit.RowsWithSep(",", "bar ,bar,,")) - result = tk.MustQuery(`select rtrim(' bar '), rtrim('bar'), rtrim(''), rtrim(null)`) - result.Check(testkit.RowsWithSep(",", " bar,bar,,")) - result = tk.MustQuery(`select ltrim("\t bar "), ltrim(" \tbar"), ltrim("\n bar"), ltrim("\r bar")`) - result.Check(testkit.RowsWithSep(",", "\t bar ,\tbar,\n bar,\r bar")) - result = tk.MustQuery(`select rtrim(" bar \t"), rtrim("bar\t "), rtrim("bar \n"), rtrim("bar \r")`) - result.Check(testkit.RowsWithSep(",", " bar \t,bar\t,bar \n,bar \r")) - - // for reverse - tk.MustExec(`DROP TABLE IF EXISTS t;`) - tk.MustExec(`CREATE TABLE t(a BINARY(6));`) - tk.MustExec(`INSERT INTO t VALUES("中文");`) - result = tk.MustQuery(`SELECT a, REVERSE(a), REVERSE("中文"), REVERSE("123 ") FROM t;`) - result.Check(testkit.Rows("中文 \x87\x96歸\xe4 文中 321")) - result = tk.MustQuery(`SELECT REVERSE(123), REVERSE(12.09) FROM t;`) - result.Check(testkit.Rows("321 90.21")) - - // for trim - result = tk.MustQuery(`select trim(' bar '), trim(leading 'x' from 'xxxbarxxx'), trim(trailing 'xyz' from 'barxxyz'), trim(both 'x' from 'xxxbarxxx')`) - result.Check(testkit.Rows("bar barxxx barx bar")) - result = tk.MustQuery(`select trim('\t bar\n '), trim(' \rbar \t')`) - result.Check(testkit.RowsWithSep(",", "\t bar\n,\rbar \t")) - result = tk.MustQuery(`select trim(leading from ' bar'), trim('x' from 'xxxbarxxx'), trim('x' from 'bar'), trim('' from ' bar ')`) - result.Check(testkit.RowsWithSep(",", "bar,bar,bar, bar ")) - result = tk.MustQuery(`select trim(''), trim('x' from '')`) - result.Check(testkit.RowsWithSep(",", ",")) - result = tk.MustQuery(`select trim(null from 'bar'), trim('x' from null), trim(null), trim(leading null from 'bar')`) - result.Check(testkit.Rows(" ")) - - // for locate - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a char(20), b int, c double, d datetime, e time, f binary(5))") - tk.MustExec(`insert into t values('www.pingcap.com', 12345, 123.45, "2017-01-01 12:01:01", "12:01:01", "HelLo")`) - result = tk.MustQuery(`select locate(".ping", a), locate(".ping", a, 5) from t`) - result.Check(testkit.Rows("4 0")) - result = tk.MustQuery(`select locate("234", b), locate("235", b, 10) from t`) - result.Check(testkit.Rows("2 0")) - result = tk.MustQuery(`select locate(".45", c), locate(".35", b) from t`) - result.Check(testkit.Rows("4 0")) - result = tk.MustQuery(`select locate("El", f), locate("ll", f), locate("lL", f), locate("Lo", f), locate("lo", f) from t`) - result.Check(testkit.Rows("0 0 3 4 0")) - result = tk.MustQuery(`select locate("01 12", d) from t`) - result.Check(testkit.Rows("9")) - result = tk.MustQuery(`select locate("文", "中文字符串", 2)`) - result.Check(testkit.Rows("2")) - result = tk.MustQuery(`select locate("文", "中文字符串", 3)`) - result.Check(testkit.Rows("0")) - result = tk.MustQuery(`select locate("文", "中文字符串")`) - result.Check(testkit.Rows("2")) - - // for bin - result = tk.MustQuery(`select bin(-1);`) - result.Check(testkit.Rows("1111111111111111111111111111111111111111111111111111111111111111")) - result = tk.MustQuery(`select bin(5);`) - result.Check(testkit.Rows("101")) - result = tk.MustQuery(`select bin("中文");`) - result.Check(testkit.Rows("0")) - - // for character_length - result = tk.MustQuery(`select character_length(null), character_length("Hello"), character_length("a中b文c"), - character_length(123), character_length(12.3456);`) - result.Check(testkit.Rows(" 5 5 3 7")) - - // for char_length - result = tk.MustQuery(`select char_length(null), char_length("Hello"), char_length("a中b文c"), char_length(123),char_length(12.3456);`) - result.Check(testkit.Rows(" 5 5 3 7")) - result = tk.MustQuery(`select char_length(null), char_length("Hello"), char_length("a 中 b 文 c"), char_length("НОЧЬ НА ОКРАИНЕ МОСКВЫ");`) - result.Check(testkit.Rows(" 5 9 22")) - // for char_length, binary string type - result = tk.MustQuery(`select char_length(null), char_length(binary("Hello")), char_length(binary("a 中 b 文 c")), char_length(binary("НОЧЬ НА ОКРАИНЕ МОСКВЫ"));`) - result.Check(testkit.Rows(" 5 13 41")) - - // for elt - result = tk.MustQuery(`select elt(0, "abc", "def"), elt(2, "hello", "中文", "tidb"), elt(4, "hello", "中文", - "tidb");`) - result.Check(testkit.Rows(" 中文 ")) - - // for instr - result = tk.MustQuery(`select instr("中国", "国"), instr("中国", ""), instr("abc", ""), instr("", ""), instr("", "abc");`) - result.Check(testkit.Rows("2 1 1 1 0")) - result = tk.MustQuery(`select instr("中国", null), instr(null, ""), instr(null, null);`) - result.Check(testkit.Rows(" ")) - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(a binary(20), b char(20));`) - tk.MustExec(`insert into t values("中国", cast("国" as binary)), ("中国", ""), ("abc", ""), ("", ""), ("", "abc");`) - result = tk.MustQuery(`select instr(a, b) from t;`) - result.Check(testkit.Rows("4", "1", "1", "1", "0")) - - // for oct - result = tk.MustQuery(`select oct("aaaa"), oct("-1.9"), oct("-9999999999999999999999999"), oct("9999999999999999999999999");`) - result.Check(testkit.Rows("0 1777777777777777777777 1777777777777777777777 1777777777777777777777")) - result = tk.MustQuery(`select oct(-1.9), oct(1.9), oct(-1), oct(1), oct(-9999999999999999999999999), oct(9999999999999999999999999);`) - result.Check(testkit.Rows("1777777777777777777777 1 1777777777777777777777 1 1777777777777777777777 1777777777777777777777")) - - // for find_in_set - result = tk.MustQuery(`select find_in_set("", ""), find_in_set("", ","), find_in_set("中文", "字符串,中文"), find_in_set("b,", "a,b,c,d");`) - result.Check(testkit.Rows("0 1 2 0")) - result = tk.MustQuery(`select find_in_set(NULL, ""), find_in_set("", NULL), find_in_set(1, "2,3,1");`) - result.Check(testkit.Rows(" 3")) - - // for make_set - result = tk.MustQuery(`select make_set(0, "12"), make_set(3, "aa", "11"), make_set(3, NULL, "中文"), make_set(NULL, "aa");`) - result.Check(testkit.Rows(" aa,11 中文 ")) - - // for quote - result = tk.MustQuery(`select quote("aaaa"), quote(""), quote("\"\""), quote("\n\n");`) - result.Check(testkit.Rows("'aaaa' '' '\"\"' '\n\n'")) - result = tk.MustQuery(`select quote(0121), quote(0000), quote("中文"), quote(NULL);`) - result.Check(testkit.Rows("'121' '0' '中文' NULL")) - tk.MustQuery(`select quote(null) is NULL;`).Check(testkit.Rows(`0`)) - tk.MustQuery(`select quote(null) is NOT NULL;`).Check(testkit.Rows(`1`)) - tk.MustQuery(`select length(quote(null));`).Check(testkit.Rows(`4`)) - tk.MustQuery(`select quote(null) REGEXP binary 'null'`).Check(testkit.Rows(`0`)) - tk.MustQuery(`select quote(null) REGEXP binary 'NULL'`).Check(testkit.Rows(`1`)) - tk.MustQuery(`select quote(null) REGEXP 'NULL'`).Check(testkit.Rows(`1`)) - tk.MustQuery(`select quote(null) REGEXP 'null'`).Check(testkit.Rows(`0`)) - - // for convert - result = tk.MustQuery(`select convert("123" using "binary"), convert("中文" using "binary"), convert("中文" using "utf8"), convert("中文" using "utf8mb4"), convert(cast("中文" as binary) using "utf8");`) - result.Check(testkit.Rows("123 中文 中文 中文 中文")) - // charset 866 does not have a default collation configured currently, so this will return error. - err = tk.ExecToErr(`select convert("123" using "866");`) - require.Error(t, err, "[parser:1115]Unknown character set: '866'") - - // for insert - result = tk.MustQuery(`select insert("中文", 1, 1, cast("aaa" as binary)), insert("ba", -1, 1, "aaa"), insert("ba", 1, 100, "aaa"), insert("ba", 100, 1, "aaa");`) - result.Check(testkit.Rows("aaa\xb8\xad文 ba aaa ba")) - result = tk.MustQuery(`select insert("bb", NULL, 1, "aa"), insert("bb", 1, NULL, "aa"), insert(NULL, 1, 1, "aaa"), insert("bb", 1, 1, NULL);`) - result.Check(testkit.Rows(" ")) - result = tk.MustQuery(`SELECT INSERT("bb", 0, 1, NULL), INSERT("bb", 0, NULL, "aaa");`) - result.Check(testkit.Rows(" ")) - result = tk.MustQuery(`SELECT INSERT("中文", 0, 1, NULL), INSERT("中文", 0, NULL, "aaa");`) - result.Check(testkit.Rows(" ")) - - // for export_set - result = tk.MustQuery(`select export_set(7, "1", "0", ",", 65);`) - result.Check(testkit.Rows("1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0")) - result = tk.MustQuery(`select export_set(7, "1", "0", ",", -1);`) - result.Check(testkit.Rows("1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0")) - result = tk.MustQuery(`select export_set(7, "1", "0", ",");`) - result.Check(testkit.Rows("1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0")) - result = tk.MustQuery(`select export_set(7, "1", "0");`) - result.Check(testkit.Rows("1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0")) - result = tk.MustQuery(`select export_set(NULL, "1", "0", ",", 65);`) - result.Check(testkit.Rows("")) - result = tk.MustQuery(`select export_set(7, "1", "0", ",", 1);`) - result.Check(testkit.Rows("1")) - - // for format - result = tk.MustQuery(`select format(12332.1, 4), format(12332.2, 0), format(12332.2, 2,'en_US');`) - result.Check(testkit.Rows("12,332.1000 12,332 12,332.20")) - result = tk.MustQuery(`select format(NULL, 4), format(12332.2, NULL);`) - result.Check(testkit.Rows(" ")) - result = tk.MustQuery(`select format(12332.2, 2,'es_EC');`) - result.Check(testkit.Rows("12,332.20")) - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1649 Unknown locale: 'es_EC'")) - - // for field - result = tk.MustQuery(`select field(1, 2, 1), field(1, 0, NULL), field(1, NULL, 2, 1), field(NULL, 1, 2, NULL);`) - result.Check(testkit.Rows("2 0 3 0")) - result = tk.MustQuery(`select field("1", 2, 1), field(1, "0", NULL), field("1", NULL, 2, 1), field(NULL, 1, "2", NULL);`) - result.Check(testkit.Rows("2 0 3 0")) - result = tk.MustQuery(`select field("1", 2, 1), field(1, "abc", NULL), field("1", NULL, 2, 1), field(NULL, 1, "2", NULL);`) - result.Check(testkit.Rows("2 0 3 0")) - result = tk.MustQuery(`select field("abc", "a", 1), field(1.3, "1.3", 1.5);`) - result.Check(testkit.Rows("1 1")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a decimal(11, 8), b decimal(11,8))") - tk.MustExec("insert into t values('114.57011441','38.04620115'), ('-38.04620119', '38.04620115');") - result = tk.MustQuery("select a,b,concat_ws(',',a,b) from t") - result.Check(testkit.Rows("114.57011441 38.04620115 114.57011441,38.04620115", - "-38.04620119 38.04620115 -38.04620119,38.04620115")) - - // issue 44359 - tk.MustExec("drop table if exists t1") - tk.MustExec("CREATE TABLE t1 (c1 INT UNSIGNED NOT NULL )") - tk.MustExec("INSERT INTO t1 VALUES (0)") - tk.MustQuery("SELECT c1 FROM t1 WHERE c1 <> CAST(POW(-'0', 1) AS BINARY)").Check(testkit.Rows()) - tk.MustQuery("SELECT c1 FROM t1 WHERE c1 = CAST('-000' AS BINARY)").Check(testkit.Rows("0")) -} - -func TestInvalidStrings(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - // Test convert invalid string. - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a binary(5));") - tk.MustExec("insert into t values (0x1e240), ('ABCDE');") - tk.MustExec("set tidb_enable_vectorized_expression = on;") - tk.MustQuery("select convert(t.a using utf8) from t;").Check(testkit.Rows("", "ABCDE")) - tk.MustQuery("select convert(0x1e240 using utf8);").Check(testkit.Rows("")) - tk.MustExec("set tidb_enable_vectorized_expression = off;") - tk.MustQuery("select convert(t.a using utf8) from t;").Check(testkit.Rows("", "ABCDE")) - tk.MustQuery("select convert(0x1e240 using utf8);").Check(testkit.Rows("")) -} - func TestEncryptionBuiltin(t *testing.T) { store := testkit.CreateMockStore(t) @@ -1008,53 +490,6 @@ func TestEncryptionBuiltin(t *testing.T) { tk.MustQuery("SELECT VALIDATE_PASSWORD_STRENGTH(CAST(0xd2 AS BINARY(10)))").Check(testkit.Rows("50")) } -func TestOpBuiltin(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - // for logicAnd - result := tk.MustQuery("select 1 && 1, 1 && 0, 0 && 1, 0 && 0, 2 && -1, null && 1, '1a' && 'a'") - result.Check(testkit.Rows("1 0 0 0 1 0")) - // for bitNeg - result = tk.MustQuery("select ~123, ~-123, ~null") - result.Check(testkit.Rows("18446744073709551492 122 ")) - // for logicNot - result = tk.MustQuery("select !1, !123, !0, !null") - result.Check(testkit.Rows("0 0 1 ")) - // for logicalXor - result = tk.MustQuery("select 1 xor 1, 1 xor 0, 0 xor 1, 0 xor 0, 2 xor -1, null xor 1, '1a' xor 'a'") - result.Check(testkit.Rows("0 1 1 0 0 1")) - // for bitAnd - result = tk.MustQuery("select 123 & 321, -123 & 321, null & 1") - result.Check(testkit.Rows("65 257 ")) - // for bitOr - result = tk.MustQuery("select 123 | 321, -123 | 321, null | 1") - result.Check(testkit.Rows("379 18446744073709551557 ")) - // for bitXor - result = tk.MustQuery("select 123 ^ 321, -123 ^ 321, null ^ 1") - result.Check(testkit.Rows("314 18446744073709551300 ")) - // for leftShift - result = tk.MustQuery("select 123 << 2, -123 << 2, null << 1") - result.Check(testkit.Rows("492 18446744073709551124 ")) - // for rightShift - result = tk.MustQuery("select 123 >> 2, -123 >> 2, null >> 1") - result.Check(testkit.Rows("30 4611686018427387873 ")) - // for logicOr - result = tk.MustQuery("select 1 || 1, 1 || 0, 0 || 1, 0 || 0, 2 || -1, null || 1, '1a' || 'a'") - result.Check(testkit.Rows("1 1 1 0 1 1 1")) - // for unaryPlus - result = tk.MustQuery(`select +1, +0, +(-9), +(-0.001), +0.999, +null, +"aaa"`) - result.Check(testkit.Rows("1 0 -9 -0.001 0.999 aaa")) - // for unaryMinus - tk.MustExec("drop table if exists f") - tk.MustExec("create table f(a decimal(65,0))") - tk.MustExec("insert into f value (-17000000000000000000)") - result = tk.MustQuery("select a from f") - result.Check(testkit.Rows("-17000000000000000000")) -} - func TestDatetimeOverflow(t *testing.T) { store := testkit.CreateMockStore(t) @@ -1086,23 +521,6 @@ func TestDatetimeOverflow(t *testing.T) { tk.MustQuery("select * from t1").Check(testkit.Rows(rows...)) } -func TestExprDateTimeOnDST(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - // insert DST datetime - tk.MustExec("set @@session.time_zone = 'Europe/Amsterdam'") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (id int, dt datetime, primary key (id, dt))") - tk.MustExec("insert into t values (1, date_add('2023-03-26 00:00:00', interval 2 hour))") - tk.MustExec("insert into t values (4,'2023-03-26 02:00:00')") - - // check DST datetime - tk.MustQuery("select * from t").Check(testkit.Rows("1 2023-03-26 02:00:00", "4 2023-03-26 02:00:00")) -} - func TestInfoBuiltin(t *testing.T) { store := testkit.CreateMockStore(t) @@ -1254,966 +672,9 @@ func TestInfoBuiltin(t *testing.T) { tk.MustExec("insert t values (3, 4)") err = tk.ExecToErr(oneColumnQuery) require.Error(t, err) - // 2 * 2, error. - err = tk.ExecToErr(twoColumnQuery) - require.Error(t, err) -} - -func TestControlBuiltin(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - // for ifnull - result := tk.MustQuery("select ifnull(1, 2)") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select ifnull(null, 2)") - result.Check(testkit.Rows("2")) - result = tk.MustQuery("select ifnull(1, null)") - result.Check(testkit.Rows("1")) - result = tk.MustQuery("select ifnull(null, null)") - result.Check(testkit.Rows("")) - - tk.MustExec("drop table if exists t1") - tk.MustExec("create table t1(a bigint not null)") - result = tk.MustQuery("select ifnull(max(a),0) from t1") - result.Check(testkit.Rows("0")) - - tk.MustExec("drop table if exists t1") - tk.MustExec("drop table if exists t2") - tk.MustExec("create table t1(a decimal(20,4))") - tk.MustExec("create table t2(a decimal(20,4))") - tk.MustExec("insert into t1 select 1.2345") - tk.MustExec("insert into t2 select 1.2345") - - result = tk.MustQuery(`select sum(ifnull(a, 0)) from ( - select ifnull(a, 0) as a from t1 - union all - select ifnull(a, 0) as a from t2 - ) t;`) - result.Check(testkit.Rows("2.4690")) - - // for if - result = tk.MustQuery(`select IF(0,"ERROR","this"),IF(1,"is","ERROR"),IF(NULL,"ERROR","a"),IF(1,2,3)|0,IF(1,2.0,3.0)+0;`) - result.Check(testkit.Rows("this is a 2 2.0")) - tk.MustExec("drop table if exists t1;") - tk.MustExec("CREATE TABLE t1 (st varchar(255) NOT NULL, u int(11) NOT NULL);") - tk.MustExec("INSERT INTO t1 VALUES ('a',1),('A',1),('aa',1),('AA',1),('a',1),('aaa',0),('BBB',0);") - result = tk.MustQuery("select if(1,st,st) s from t1 order by s;") - result.Check(testkit.Rows("A", "AA", "BBB", "a", "a", "aa", "aaa")) - result = tk.MustQuery("select if(u=1,st,st) s from t1 order by s;") - result.Check(testkit.Rows("A", "AA", "BBB", "a", "a", "aa", "aaa")) - tk.MustExec("drop table if exists t1;") - tk.MustExec("CREATE TABLE t1 (a varchar(255), b time, c int)") - tk.MustExec("INSERT INTO t1 VALUE('abc', '12:00:00', 0)") - tk.MustExec("INSERT INTO t1 VALUE('1abc', '00:00:00', 1)") - tk.MustExec("INSERT INTO t1 VALUE('0abc', '12:59:59', 0)") - result = tk.MustQuery("select if(a, b, c), if(b, a, c), if(c, a, b) from t1") - result.Check(testkit.Rows("0 abc 12:00:00", "00:00:00 1 1abc", "0 0abc 12:59:59")) - result = tk.MustQuery("select if(1, 1.0, 1)") - result.Check(testkit.Rows("1.0")) - // FIXME: MySQL returns `1.0`. - result = tk.MustQuery("select if(1, 1, 1.0)") - result.Check(testkit.Rows("1")) - tk.MustQuery("select if(count(*), cast('2000-01-01' as date), cast('2011-01-01' as date)) from t1").Check(testkit.Rows("2000-01-01")) - tk.MustQuery("select if(count(*)=0, cast('2000-01-01' as date), cast('2011-01-01' as date)) from t1").Check(testkit.Rows("2011-01-01")) - tk.MustQuery("select if(count(*), cast('[]' as json), cast('{}' as json)) from t1").Check(testkit.Rows("[]")) - tk.MustQuery("select if(count(*)=0, cast('[]' as json), cast('{}' as json)) from t1").Check(testkit.Rows("{}")) - - result = tk.MustQuery("SELECT 79 + + + CASE -87 WHEN -30 THEN COALESCE(COUNT(*), +COALESCE(+15, -33, -12 ) + +72) WHEN +COALESCE(+AVG(DISTINCT(60)), 21) THEN NULL ELSE NULL END AS col0;") - result.Check(testkit.Rows("")) - - result = tk.MustQuery("SELECT -63 + COALESCE ( - 83, - 61 + - + 72 * - CAST( NULL AS SIGNED ) + + 3 );") - result.Check(testkit.Rows("-146")) -} - -func TestArithmeticBuiltin(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - ctx := context.Background() - - // for plus - tk.MustExec("DROP TABLE IF EXISTS t;") - tk.MustExec("CREATE TABLE t(a DECIMAL(4, 2), b DECIMAL(5, 3));") - tk.MustExec("INSERT INTO t(a, b) VALUES(1.09, 1.999), (-1.1, -0.1);") - result := tk.MustQuery("SELECT a+b FROM t;") - result.Check(testkit.Rows("3.089", "-1.200")) - result = tk.MustQuery("SELECT b+12, b+0.01, b+0.00001, b+12.00001 FROM t;") - result.Check(testkit.Rows("13.999 2.009 1.99901 13.99901", "11.900 -0.090 -0.09999 11.90001")) - result = tk.MustQuery("SELECT 1+12, 21+0.01, 89+\"11\", 12+\"a\", 12+NULL, NULL+1, NULL+NULL;") - result.Check(testkit.Rows("13 21.01 100 12 ")) - tk.MustExec("DROP TABLE IF EXISTS t;") - tk.MustExec("CREATE TABLE t(a BIGINT UNSIGNED, b BIGINT UNSIGNED);") - tk.MustExec("INSERT INTO t SELECT 1<<63, 1<<63;") - rs, err := tk.Exec("SELECT a+b FROM t;") - require.NoError(t, err) - require.NotNil(t, rs) - rows, err := session.GetRows4Test(ctx, tk.Session(), rs) - require.Nil(t, rows) - require.Error(t, err) - require.Error(t, err, "[types:1690]BIGINT UNSIGNED value is out of range in '(test.t.a + test.t.b)'") - require.NoError(t, rs.Close()) - rs, err = tk.Exec("select cast(-3 as signed) + cast(2 as unsigned);") - require.NoError(t, err) - require.NotNil(t, rs) - rows, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.Nil(t, rows) - require.Error(t, err) - require.Error(t, err, "[types:1690]BIGINT UNSIGNED value is out of range in '(-3 + 2)'") - require.NoError(t, rs.Close()) - rs, err = tk.Exec("select cast(2 as unsigned) + cast(-3 as signed);") - require.NoError(t, err) - require.NotNil(t, rs) - rows, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.Nil(t, rows) - require.Error(t, err) - require.Error(t, err, "[types:1690]BIGINT UNSIGNED value is out of range in '(2 + -3)'") - require.NoError(t, rs.Close()) - - // for minus - tk.MustExec("DROP TABLE IF EXISTS t;") - tk.MustExec("CREATE TABLE t(a DECIMAL(4, 2), b DECIMAL(5, 3));") - tk.MustExec("INSERT INTO t(a, b) VALUES(1.09, 1.999), (-1.1, -0.1);") - result = tk.MustQuery("SELECT a-b FROM t;") - result.Check(testkit.Rows("-0.909", "-1.000")) - result = tk.MustQuery("SELECT b-12, b-0.01, b-0.00001, b-12.00001 FROM t;") - result.Check(testkit.Rows("-10.001 1.989 1.99899 -10.00101", "-12.100 -0.110 -0.10001 -12.10001")) - result = tk.MustQuery("SELECT 1-12, 21-0.01, 89-\"11\", 12-\"a\", 12-NULL, NULL-1, NULL-NULL;") - result.Check(testkit.Rows("-11 20.99 78 12 ")) - - tk.MustExec("DROP TABLE IF EXISTS t;") - tk.MustExec("CREATE TABLE t(a BIGINT UNSIGNED, b BIGINT UNSIGNED);") - tk.MustExec("INSERT INTO t SELECT 1, 4;") - err = tk.QueryToErr("SELECT a-b FROM t;") - require.Error(t, err) - require.Error(t, err, "[types:1690]BIGINT UNSIGNED value is out of range in '(test.t.a - test.t.b)'") - - err = tk.QueryToErr("select cast(1 as unsigned) - cast(4 as unsigned);") - require.Error(t, err) - // TODO: make error compatible with MySQL, should be BIGINT UNSIGNED value is out of range in '(cast(1 as unsigned) - cast(4 as unsigned)) - require.Error(t, err, "[types:1690]BIGINT UNSIGNED value is out of range in '(1 - 4)'") - - err = tk.QueryToErr("select cast(-1 as signed) - cast(-1 as unsigned);") - require.Error(t, err) - require.Error(t, err, "[types:1690]BIGINT UNSIGNED value is out of range in '(-1 - 18446744073709551615)'") - - err = tk.QueryToErr("select cast(1 as signed) - cast(-1 as unsigned);") - require.Error(t, err) - require.Error(t, err, "[types:1690]BIGINT UNSIGNED value is out of range in '(1 - 18446744073709551615)'") - - err = tk.QueryToErr("select cast(-1 as unsigned) - cast(-1 as signed);") - require.Error(t, err) - require.Error(t, err, "[types:1690]BIGINT UNSIGNED value is out of range in '(18446744073709551615 - -1)'") - - err = tk.QueryToErr("select cast(-9223372036854775808 as unsigned) - (-9223372036854775808);") - require.Error(t, err) - require.Error(t, err, "[types:1690]BIGINT UNSIGNED value is out of range in '(9223372036854775808 - -9223372036854775808)'") - - err = tk.QueryToErr("select cast(12 as unsigned) - (14);") - require.Error(t, err) - require.Error(t, err, "[types:1690]BIGINT UNSIGNED value is out of range in '(12 - 14)'") - - err = tk.QueryToErr("select cast(9223372036854775807 as signed) - cast(-1 as signed);") - require.Error(t, err, "[types:1690]BIGINT value is out of range in '(9223372036854775807 - -1)'") - - err = tk.QueryToErr("select cast(-9223372036854775808 as signed) - cast(1 as signed);") - require.Error(t, err) - require.Error(t, err, "[types:1690]BIGINT value is out of range in '(-9223372036854775808 - 1)'") - - err = tk.QueryToErr("select cast(12 as signed) - cast(-9223372036854775808 as signed);") - require.Error(t, err) - require.Error(t, err, "[types:1690]BIGINT value is out of range in '(12 - -9223372036854775808)'") - - tk.MustExec(`create table tb5(a int(10));`) - tk.MustExec(`insert into tb5 (a) values (10);`) - e := tk.QueryToErr(`select * from tb5 where a - -9223372036854775808;`) - require.NotNil(t, e) - require.True(t, strings.HasSuffix(e.Error(), `BIGINT value is out of range in '(Column#0 - -9223372036854775808)'`), "err: %v", err) - - tk.MustExec(`drop table tb5`) - tk.MustQuery("select cast(-9223372036854775808 as unsigned) - (-9223372036854775807);").Check(testkit.Rows("18446744073709551615")) - tk.MustQuery("select cast(-3 as unsigned) - cast(-1 as signed);").Check(testkit.Rows("18446744073709551614")) - tk.MustQuery("select 1.11 - 1.11;").Check(testkit.Rows("0.00")) - tk.MustQuery("select cast(-1 as unsigned) - cast(-12 as unsigned);").Check(testkit.Rows("11")) - tk.MustQuery("select cast(-1 as unsigned) - cast(0 as unsigned);").Check(testkit.Rows("18446744073709551615")) - - // for multiply - tk.MustQuery("select 1234567890 * 1234567890").Check(testkit.Rows("1524157875019052100")) - rs, err = tk.Exec("select 1234567890 * 12345671890") - require.NoError(t, err) - _, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.True(t, terror.ErrorEqual(err, types.ErrOverflow)) - require.NoError(t, rs.Close()) - tk.MustQuery("select cast(1234567890 as unsigned int) * 12345671890").Check(testkit.Rows("15241570095869612100")) - tk.MustQuery("select 123344532434234234267890.0 * 1234567118923479823749823749.230").Check(testkit.Rows("152277104042296270209916846800130443726237424001224.7000")) - rs, err = tk.Exec("select 123344532434234234267890.0 * 12345671189234798237498232384982309489238402830480239849238048239084749.230") - require.NoError(t, err) - _, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.True(t, terror.ErrorEqual(err, types.ErrOverflow)) - require.NoError(t, rs.Close()) - // FIXME: There is something wrong in showing float number. - // tk.MustQuery("select 1.797693134862315708145274237317043567981e+308 * 1").Check(testkit.Rows("1.7976931348623157e308")) - // tk.MustQuery("select 1.797693134862315708145274237317043567981e+308 * -1").Check(testkit.Rows("-1.7976931348623157e308")) - rs, err = tk.Exec("select 1.797693134862315708145274237317043567981e+308 * 1.1") - require.NoError(t, err) - _, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.True(t, terror.ErrorEqual(err, types.ErrOverflow)) - require.NoError(t, rs.Close()) - rs, err = tk.Exec("select 1.797693134862315708145274237317043567981e+308 * -1.1") - require.NoError(t, err) - _, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.True(t, terror.ErrorEqual(err, types.ErrOverflow)) - require.NoError(t, rs.Close()) - tk.MustQuery("select 0.0 * -1;").Check(testkit.Rows("0.0")) - - tk.MustExec("DROP TABLE IF EXISTS t;") - tk.MustExec("CREATE TABLE t(a DECIMAL(4, 2), b DECIMAL(5, 3));") - tk.MustExec("INSERT INTO t(a, b) VALUES(-1.09, 1.999);") - result = tk.MustQuery("SELECT a/b, a/12, a/-0.01, b/12, b/-0.01, b/0.000, NULL/b, b/NULL, NULL/NULL FROM t;") - result.Check(testkit.Rows("-0.545273 -0.090833 109.000000 0.1665833 -199.9000000 ")) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1365 Division by 0")) - rs, err = tk.Exec("select 1e200/1e-200") - require.NoError(t, err) - _, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.True(t, terror.ErrorEqual(err, types.ErrOverflow)) - require.NoError(t, rs.Close()) - - // for intDiv - result = tk.MustQuery("SELECT 13 DIV 12, 13 DIV 0.01, -13 DIV 2, 13 DIV NULL, NULL DIV 13, NULL DIV NULL;") - result.Check(testkit.Rows("1 1300 -6 ")) - result = tk.MustQuery("SELECT 2.4 div 1.1, 2.4 div 1.2, 2.4 div 1.3;") - result.Check(testkit.Rows("2 2 1")) - result = tk.MustQuery("SELECT 1.175494351E-37 div 1.7976931348623157E+308, 1.7976931348623157E+308 div -1.7976931348623157E+307, 1 div 1e-82;") - result.Check(testkit.Rows("0 -1 ")) - tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", - "Warning|1292|Truncated incorrect DECIMAL value: '1.7976931348623157e+308'", - "Warning|1292|Truncated incorrect DECIMAL value: '1.7976931348623157e+308'", - "Warning|1292|Truncated incorrect DECIMAL value: '-1.7976931348623158e+307'", - "Warning|1365|Division by 0")) - rs, err = tk.Exec("select 1e300 DIV 1.5") - require.NoError(t, err) - _, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.True(t, terror.ErrorEqual(err, types.ErrOverflow)) - require.NoError(t, rs.Close()) - - tk.MustExec("drop table if exists t;") - tk.MustExec("CREATE TABLE t (c_varchar varchar(255), c_time time, nonzero int, zero int, c_int_unsigned int unsigned, c_timestamp timestamp, c_enum enum('a','b','c'));") - tk.MustExec("INSERT INTO t VALUE('abc', '12:00:00', 12, 0, 5, '2017-08-05 18:19:03', 'b');") - result = tk.MustQuery("select c_varchar div nonzero, c_time div nonzero, c_time div zero, c_timestamp div nonzero, c_timestamp div zero, c_varchar div zero from t;") - result.Check(testkit.Rows("0 10000 1680900431825 ")) - result = tk.MustQuery("select c_enum div nonzero from t;") - result.Check(testkit.Rows("0")) - tk.MustQuery("select c_enum div zero from t").Check(testkit.Rows("")) - tk.MustQuery("select nonzero div zero from t").Check(testkit.Rows("")) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1365 Division by 0")) - result = tk.MustQuery("select c_time div c_enum, c_timestamp div c_time, c_timestamp div c_enum from t;") - result.Check(testkit.Rows("60000 168090043 10085402590951")) - result = tk.MustQuery("select c_int_unsigned div nonzero, nonzero div c_int_unsigned, c_int_unsigned div zero from t;") - result.Check(testkit.Rows("0 2 ")) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1365 Division by 0")) - - // for mod - result = tk.MustQuery("SELECT CAST(1 AS UNSIGNED) MOD -9223372036854775808, -9223372036854775808 MOD CAST(1 AS UNSIGNED);") - result.Check(testkit.Rows("1 0")) - result = tk.MustQuery("SELECT 13 MOD 12, 13 MOD 0.01, -13 MOD 2, 13 MOD NULL, NULL MOD 13, NULL DIV NULL;") - result.Check(testkit.Rows("1 0.00 -1 ")) - result = tk.MustQuery("SELECT 2.4 MOD 1.1, 2.4 MOD 1.2, 2.4 mod 1.30;") - result.Check(testkit.Rows("0.2 0.0 1.10")) - tk.MustExec("drop table if exists t;") - tk.MustExec("CREATE TABLE t (c_varchar varchar(255), c_time time, nonzero int, zero int, c_timestamp timestamp, c_enum enum('a','b','c'));") - tk.MustExec("INSERT INTO t VALUE('abc', '12:00:00', 12, 0, '2017-08-05 18:19:03', 'b');") - result = tk.MustQuery("select c_varchar MOD nonzero, c_time MOD nonzero, c_timestamp MOD nonzero, c_enum MOD nonzero from t;") - result.Check(testkit.Rows("0 0 3 2")) - result = tk.MustQuery("select c_time MOD c_enum, c_timestamp MOD c_time, c_timestamp MOD c_enum from t;") - result.Check(testkit.Rows("0 21903 1")) - tk.MustQuery("select c_enum MOD zero from t;").Check(testkit.Rows("")) - tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1365 Division by 0")) - tk.MustExec("SET SQL_MODE='ERROR_FOR_DIVISION_BY_ZERO,STRICT_ALL_TABLES';") - tk.MustExec("drop table if exists t;") - tk.MustExec("CREATE TABLE t (v int);") - tk.MustExec("INSERT IGNORE INTO t VALUE(12 MOD 0);") - tk.MustQuery("show warnings;").CheckContain("Division by 0") - tk.MustQuery("select v from t;").Check(testkit.Rows("")) - tk.MustQuery("select 0.000 % 0.11234500000000000000;").Check(testkit.Rows("0.00000000000000000000")) - - tk.MustGetDBError("INSERT INTO t VALUE(12 MOD 0);", expression.ErrDivisionByZero) - - tk.MustQuery("select sum(1.2e2) * 0.1").Check(testkit.Rows("12")) - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a double)") - tk.MustExec("insert into t value(1.2)") - tk.MustQuery("select sum(a) * 0.1 from t").Check(testkit.Rows("0.12")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a double)") - tk.MustExec("insert into t value(1.2)") - result = tk.MustQuery("select * from t where a/0 > 1") - result.Check(testkit.Rows()) - tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning|1365|Division by 0")) - - tk.MustExec("USE test;") - tk.MustExec("DROP TABLE IF EXISTS t;") - tk.MustExec("CREATE TABLE t(a BIGINT, b DECIMAL(6, 2));") - tk.MustExec("INSERT INTO t VALUES(0, 1.12), (1, 1.21);") - tk.MustQuery("SELECT a/b FROM t;").Check(testkit.Rows("0.0000", "0.8264")) -} - -func TestGreatestTimeType(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - tk.MustExec("drop table if exists t1;") - tk.MustExec("create table t1(c_time time(5), c_dt datetime(4), c_ts timestamp(3), c_d date, c_str varchar(100));") - tk.MustExec("insert into t1 values('-800:10:10', '2021-10-10 10:10:10.1234', '2021-10-10 10:10:10.1234', '2021-10-11', '2021-10-10 10:10:10.1234');") - - for i := 0; i < 2; i++ { - if i == 0 { - tk.MustExec("set @@tidb_enable_vectorized_expression = off;") - } else { - tk.MustExec("set @@tidb_enable_vectorized_expression = on;") - } - tk.MustQuery("select greatest(c_time, c_time) from t1;").Check(testkit.Rows("-800:10:10.00000")) - tk.MustQuery("select greatest(c_dt, c_dt) from t1;").Check(testkit.Rows("2021-10-10 10:10:10.1234")) - tk.MustQuery("select greatest(c_ts, c_ts) from t1;").Check(testkit.Rows("2021-10-10 10:10:10.123")) - tk.MustQuery("select greatest(c_d, c_d) from t1;").Check(testkit.Rows("2021-10-11")) - tk.MustQuery("select greatest(c_str, c_str) from t1;").Check(testkit.Rows("2021-10-10 10:10:10.1234")) - - tk.MustQuery("select least(c_time, c_time) from t1;").Check(testkit.Rows("-800:10:10.00000")) - tk.MustQuery("select least(c_dt, c_dt) from t1;").Check(testkit.Rows("2021-10-10 10:10:10.1234")) - tk.MustQuery("select least(c_ts, c_ts) from t1;").Check(testkit.Rows("2021-10-10 10:10:10.123")) - tk.MustQuery("select least(c_d, c_d) from t1;").Check(testkit.Rows("2021-10-11")) - tk.MustQuery("select least(c_str, c_str) from t1;").Check(testkit.Rows("2021-10-10 10:10:10.1234")) - - tk.MustQuery("select greatest(c_time, cast('10:01:01' as time)) from t1;").Check(testkit.Rows("10:01:01.00000")) - tk.MustQuery("select least(c_time, cast('10:01:01' as time)) from t1;").Check(testkit.Rows("-800:10:10.00000")) - - tk.MustQuery("select greatest(c_d, cast('1999-10-10' as date)) from t1;").Check(testkit.Rows("2021-10-11")) - tk.MustQuery("select least(c_d, cast('1999-10-10' as date)) from t1;").Check(testkit.Rows("1999-10-10")) - - tk.MustQuery("select greatest(c_dt, cast('1999-10-10 10:10:10.1234' as datetime)) from t1;").Check(testkit.Rows("2021-10-10 10:10:10.1234")) - tk.MustQuery("select least(c_dt, cast('1999-10-10 10:10:10.1234' as datetime)) from t1;").Check(testkit.Rows("1999-10-10 10:10:10")) - } -} - -func TestCompareBuiltin(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - // compare as JSON - tk.MustExec("drop table if exists t") - tk.MustExec("CREATE TABLE t (pk int NOT NULL PRIMARY KEY AUTO_INCREMENT, i INT, j JSON);") - tk.MustExec(`INSERT INTO t(i, j) VALUES (0, NULL)`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (1, '{"a": 2}')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (2, '[1,2]')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (3, '{"a":"b", "c":"d","ab":"abc", "bc": ["x", "y"]}')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (4, '["here", ["I", "am"], "!!!"]')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (5, '"scalar string"')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (6, 'true')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (7, 'false')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (8, 'null')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (9, '-1')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (10, CAST(CAST(1 AS UNSIGNED) AS JSON))`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (11, '32767')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (12, '32768')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (13, '-32768')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (14, '-32769')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (15, '2147483647')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (16, '2147483648')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (17, '-2147483648')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (18, '-2147483649')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (19, '18446744073709551615')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (20, '18446744073709551616')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (21, '3.14')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (22, '{}')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (23, '[]')`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (24, CAST(CAST('2015-01-15 23:24:25' AS DATETIME) AS JSON))`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (25, CAST(CAST('23:24:25' AS TIME) AS JSON))`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (26, CAST(CAST('2015-01-15' AS DATE) AS JSON))`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (27, CAST(TIMESTAMP('2015-01-15 23:24:25') AS JSON))`) - tk.MustExec(`INSERT INTO t(i, j) VALUES (28, CAST('[]' AS CHAR CHARACTER SET 'ascii'))`) - - result := tk.MustQuery(`SELECT i, - (j = '"scalar string"') AS c1, - (j = 'scalar string') AS c2, - (j = CAST('"scalar string"' AS JSON)) AS c3, - (j = CAST(CAST(j AS CHAR CHARACTER SET 'utf8mb4') AS JSON)) AS c4, - (j = CAST(NULL AS JSON)) AS c5, - (j = NULL) AS c6, - (j <=> NULL) AS c7, - (j <=> CAST(NULL AS JSON)) AS c8, - (j IN (-1, 2, 32768, 3.14)) AS c9, - (j IN (CAST('[1, 2]' AS JSON), CAST('{}' AS JSON), CAST(3.14 AS JSON))) AS c10, - (j = (SELECT j FROM t WHERE j = CAST('null' AS JSON))) AS c11, - (j = (SELECT j FROM t WHERE j IS NULL)) AS c12, - (j = (SELECT j FROM t WHERE 1<>1)) AS c13, - (j = DATE('2015-01-15')) AS c14, - (j = TIME('23:24:25')) AS c15, - (j = TIMESTAMP('2015-01-15 23:24:25')) AS c16, - (j = CURRENT_TIMESTAMP) AS c17, - (JSON_EXTRACT(j, '$.a') = 2) AS c18 - FROM t - ORDER BY i;`) - result.Check(testkit.Rows("0 1 1 ", - "1 0 0 0 1 0 0 0 0 0 0 0 0 0 1", - "2 0 0 0 1 0 0 0 1 0 0 0 0 0 ", - "3 0 0 0 1 0 0 0 0 0 0 0 0 0 0", - "4 0 0 0 1 0 0 0 0 0 0 0 0 0 ", - "5 0 1 1 1 0 0 0 0 0 0 0 0 0 ", - "6 0 0 0 1 0 0 0 0 0 0 0 0 0 ", - "7 0 0 0 1 0 0 0 0 0 0 0 0 0 ", - "8 0 0 0 1 0 0 0 0 1 0 0 0 0 ", - "9 0 0 0 1 0 0 1 0 0 0 0 0 0 ", - "10 0 0 0 1 0 0 0 0 0 0 0 0 0 ", - "11 0 0 0 1 0 0 0 0 0 0 0 0 0 ", - "12 0 0 0 1 0 0 1 0 0 0 0 0 0 ", - "13 0 0 0 1 0 0 0 0 0 0 0 0 0 ", - "14 0 0 0 1 0 0 0 0 0 0 0 0 0 ", - "15 0 0 0 1 0 0 0 0 0 0 0 0 0 ", - "16 0 0 0 1 0 0 0 0 0 0 0 0 0 ", - "17 0 0 0 1 0 0 0 0 0 0 0 0 0 ", - "18 0 0 0 1 0 0 0 0 0 0 0 0 0 ", - "19 0 0 0 1 0 0 0 0 0 0 0 0 0 ", - "20 0 0 0 1 0 0 0 0 0 0 0 0 0 ", - "21 0 0 0 1 0 0 1 1 0 0 0 0 0 ", - "22 0 0 0 1 0 0 0 1 0 0 0 0 0 ", - "23 0 0 0 1 0 0 0 0 0 0 0 0 0 ", - "24 0 0 0 0 0 0 0 0 0 0 0 1 0 ", - "25 0 0 0 0 0 0 0 0 0 0 1 0 0 ", - "26 0 0 0 0 0 0 0 0 0 1 0 0 0 ", - "27 0 0 0 0 0 0 0 0 0 0 0 1 0 ", - "28 0 0 0 1 0 0 0 0 0 0 0 0 0 ")) - - // for coalesce - result = tk.MustQuery("select coalesce(NULL), coalesce(NULL, NULL), coalesce(NULL, NULL, NULL);") - result.Check(testkit.Rows(" ")) - tk.MustQuery(`select coalesce(cast(1 as json), cast(2 as json));`).Check(testkit.Rows(`1`)) - tk.MustQuery(`select coalesce(NULL, cast(2 as json));`).Check(testkit.Rows(`2`)) - tk.MustQuery(`select coalesce(cast(1 as json), NULL);`).Check(testkit.Rows(`1`)) - tk.MustQuery(`select coalesce(NULL, NULL);`).Check(testkit.Rows(``)) - - tk.MustExec("drop table if exists t2") - tk.MustExec("create table t2(a int, b double, c datetime, d time, e char(20), f bit(10))") - tk.MustExec(`insert into t2 values(1, 1.1, "2017-08-01 12:01:01", "12:01:01", "abcdef", 0b10101)`) - - result = tk.MustQuery("select coalesce(NULL, a), coalesce(NULL, b, a), coalesce(c, NULL, a, b), coalesce(d, NULL), coalesce(d, c), coalesce(NULL, NULL, e, 1), coalesce(f), coalesce(1, a, b, c, d, e, f) from t2") - // coalesce(col_bit) is not same with MySQL, because it's a bug of MySQL(https://bugs.mysql.com/bug.php?id=103289&thanks=4) - result.Check(testkit.Rows(fmt.Sprintf("1 1.1 2017-08-01 12:01:01 12:01:01 %s 12:01:01 abcdef \x00\x15 1", time.Now().In(tk.Session().GetSessionVars().Location()).Format(time.DateOnly)))) - - // nullif - result = tk.MustQuery(`SELECT NULLIF(NULL, 1), NULLIF(1, NULL), NULLIF(1, 1), NULLIF(NULL, NULL);`) - result.Check(testkit.Rows(" 1 ")) - - result = tk.MustQuery(`SELECT NULLIF(1, 1.0), NULLIF(1, "1.0");`) - result.Check(testkit.Rows(" ")) - - result = tk.MustQuery(`SELECT NULLIF("abc", 1);`) - result.Check(testkit.Rows("abc")) - - result = tk.MustQuery(`SELECT NULLIF(1+2, 1);`) - result.Check(testkit.Rows("3")) - - result = tk.MustQuery(`SELECT NULLIF(1, 1+2);`) - result.Check(testkit.Rows("1")) - - result = tk.MustQuery(`SELECT NULLIF(2+3, 1+2);`) - result.Check(testkit.Rows("5")) - - result = tk.MustQuery(`SELECT HEX(NULLIF("abc", 1));`) - result.Check(testkit.Rows("616263")) - - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a date)") - result = tk.MustQuery("desc select a = a from t") - result.Check(testkit.Rows( - "Projection_3 10000.00 root eq(test.t.a, test.t.a)->Column#3", - "└─TableReader_5 10000.00 root data:TableFullScan_4", - " └─TableFullScan_4 10000.00 cop[tikv] table:t keep order:false, stats:pseudo", - )) - - // for interval - result = tk.MustQuery(`select interval(null, 1, 2), interval(1, 2, 3), interval(2, 1, 3)`) - result.Check(testkit.Rows("-1 0 1")) - result = tk.MustQuery(`select interval(3, 1, 2), interval(0, "b", "1", "2"), interval("a", "b", "1", "2")`) - result.Check(testkit.Rows("2 1 1")) - result = tk.MustQuery(`select interval(23, 1, 23, 23, 23, 30, 44, 200), interval(23, 1.7, 15.3, 23.1, 30, 44, 200), interval(9007199254740992, 9007199254740993)`) - result.Check(testkit.Rows("4 2 0")) - result = tk.MustQuery(`select interval(cast(9223372036854775808 as unsigned), cast(9223372036854775809 as unsigned)), interval(9223372036854775807, cast(9223372036854775808 as unsigned)), interval(-9223372036854775807, cast(9223372036854775808 as unsigned))`) - result.Check(testkit.Rows("0 0 0")) - result = tk.MustQuery(`select interval(cast(9223372036854775806 as unsigned), 9223372036854775807), interval(cast(9223372036854775806 as unsigned), -9223372036854775807), interval("9007199254740991", "9007199254740992")`) - result.Check(testkit.Rows("0 1 0")) - result = tk.MustQuery(`select interval(9007199254740992, "9007199254740993"), interval("9007199254740992", 9007199254740993), interval("9007199254740992", "9007199254740993")`) - result.Check(testkit.Rows("1 1 1")) - result = tk.MustQuery(`select INTERVAL(100, NULL, NULL, NULL, NULL, NULL, 100);`) - result.Check(testkit.Rows("6")) - result = tk.MustQuery(`SELECT INTERVAL(0,(1*5)/2) + INTERVAL(5,4,3);`) - result.Check(testkit.Rows("2")) - - // for greatest - result = tk.MustQuery(`select greatest(1, 2, 3), greatest("a", "b", "c"), greatest(1.1, 1.2, 1.3), greatest("123a", 1, 2)`) - result.Check(testkit.Rows("3 c 1.3 2")) - tk.MustQuery("show warnings").Check(testkit.Rows()) - result = tk.MustQuery(`select greatest(cast("2017-01-01" as datetime), "123", "234", cast("2018-01-01" as date)), greatest(cast("2017-01-01" as date), "123", null)`) - result.Check(testkit.Rows("234 ")) - tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning|1292|Incorrect time value: '123'", "Warning|1292|Incorrect time value: '234'", "Warning|1292|Incorrect time value: '123'")) - // for least - result = tk.MustQuery(`select least(1, 2, 3), least("a", "b", "c"), least(1.1, 1.2, 1.3), least("123a", 1, 2)`) - result.Check(testkit.Rows("1 a 1.1 1")) - tk.MustQuery("show warnings").Check(testkit.Rows()) - result = tk.MustQuery(`select least(cast("2017-01-01" as datetime), "123", "234", cast("2018-01-01" as date)), least(cast("2017-01-01" as date), "123", null)`) - result.Check(testkit.Rows("123 ")) - tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning|1292|Incorrect time value: '123'", "Warning|1292|Incorrect time value: '234'", "Warning|1292|Incorrect time value: '123'")) - tk.MustQuery(`select 1 < 17666000000000000000, 1 > 17666000000000000000, 1 = 17666000000000000000`).Check(testkit.Rows("1 0 0")) - - tk.MustExec("drop table if exists t") - - // insert value at utc timezone - tk.MustExec("set time_zone = '+00:00'") - tk.MustExec("create table t(a timestamp)") - tk.MustExec("insert into t value('1991-05-06 04:59:28')") - // check daylight saving time in Asia/Shanghai - tk.MustExec("set time_zone='Asia/Shanghai'") - tk.MustQuery("select * from t").Check(testkit.Rows("1991-05-06 13:59:28")) - // insert an nonexistent time - tk.MustExec("set time_zone = 'America/Los_Angeles'") - tk.MustExecToErr("insert into t value('2011-03-13 02:00:00')") - // reset timezone to a +8 offset - tk.MustExec("set time_zone = '+08:00'") - tk.MustQuery("select * from t").Check(testkit.Rows("1991-05-06 12:59:28")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a bigint unsigned)") - tk.MustExec("insert into t value(17666000000000000000)") - tk.MustQuery("select * from t where a = 17666000000000000000").Check(testkit.Rows("17666000000000000000")) - - // test for compare row - result = tk.MustQuery(`select row(1,2,3)=row(1,2,3)`) - result.Check(testkit.Rows("1")) - result = tk.MustQuery(`select row(1,2,3)=row(1+3,2,3)`) - result.Check(testkit.Rows("0")) - result = tk.MustQuery(`select row(1,2,3)<>row(1,2,3)`) - result.Check(testkit.Rows("0")) - result = tk.MustQuery(`select row(1,2,3)<>row(1+3,2,3)`) - result.Check(testkit.Rows("1")) - result = tk.MustQuery(`select row(1+3,2,3)<>row(1+3,2,3)`) - result.Check(testkit.Rows("0")) -} - -// #23157: make sure if Nullif expr is correct combined with IsNull expr. -func TestNullifWithIsNull(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int not null);") - tk.MustExec("insert into t values(1),(2);") - rows := tk.MustQuery("select * from t where nullif(a,a) is null;") - rows.Check(testkit.Rows("1", "2")) -} - -func TestAggregationBuiltin(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a decimal(7, 6))") - tk.MustExec("insert into t values(1.123456), (1.123456)") - result := tk.MustQuery("select avg(a) from t") - result.Check(testkit.Rows("1.1234560000")) - - tk.MustExec("use test") - tk.MustExec("drop table t") - tk.MustExec("CREATE TABLE `t` ( `a` int, KEY `idx_a` (`a`))") - result = tk.MustQuery("select avg(a) from t") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select max(a), min(a) from t") - result.Check(testkit.Rows(" ")) - result = tk.MustQuery("select distinct a from t") - result.Check(testkit.Rows()) - result = tk.MustQuery("select sum(a) from t") - result.Check(testkit.Rows("")) - result = tk.MustQuery("select count(a) from t") - result.Check(testkit.Rows("0")) - result = tk.MustQuery("select bit_or(a) from t") - result.Check(testkit.Rows("0")) - result = tk.MustQuery("select bit_xor(a) from t") - result.Check(testkit.Rows("0")) - result = tk.MustQuery("select bit_and(a) from t") - result.Check(testkit.Rows("18446744073709551615")) - result = tk.MustQuery("select count(1) from (select count(1) from t) as t1") - result.Check(testkit.Rows("1")) -} - -func TestAggregationBuiltinBitOr(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a bigint)") - tk.MustExec("insert into t values(null);") - result := tk.MustQuery("select bit_or(a) from t") - result.Check(testkit.Rows("0")) - tk.MustExec("insert into t values(1);") - result = tk.MustQuery("select bit_or(a) from t") - result.Check(testkit.Rows("1")) - tk.MustExec("insert into t values(2);") - result = tk.MustQuery("select bit_or(a) from t") - result.Check(testkit.Rows("3")) - tk.MustExec("insert into t values(4);") - result = tk.MustQuery("select bit_or(a) from t") - result.Check(testkit.Rows("7")) - result = tk.MustQuery("select a, bit_or(a) from t group by a order by a") - result.Check(testkit.Rows(" 0", "1 1", "2 2", "4 4")) - tk.MustExec("insert into t values(-1);") - result = tk.MustQuery("select bit_or(a) from t") - result.Check(testkit.Rows("18446744073709551615")) -} - -func TestAggregationBuiltinBitXor(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a bigint)") - tk.MustExec("insert into t values(null);") - result := tk.MustQuery("select bit_xor(a) from t") - result.Check(testkit.Rows("0")) - tk.MustExec("insert into t values(1);") - result = tk.MustQuery("select bit_xor(a) from t") - result.Check(testkit.Rows("1")) - tk.MustExec("insert into t values(2);") - result = tk.MustQuery("select bit_xor(a) from t") - result.Check(testkit.Rows("3")) - tk.MustExec("insert into t values(3);") - result = tk.MustQuery("select bit_xor(a) from t") - result.Check(testkit.Rows("0")) - tk.MustExec("insert into t values(3);") - result = tk.MustQuery("select bit_xor(a) from t") - result.Check(testkit.Rows("3")) - result = tk.MustQuery("select a, bit_xor(a) from t group by a order by a") - result.Check(testkit.Rows(" 0", "1 1", "2 2", "3 0")) -} - -func TestAggregationBuiltinBitAnd(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t(a bigint)") - tk.MustExec("insert into t values(null);") - result := tk.MustQuery("select bit_and(a) from t") - result.Check(testkit.Rows("18446744073709551615")) - tk.MustExec("insert into t values(7);") - result = tk.MustQuery("select bit_and(a) from t") - result.Check(testkit.Rows("7")) - tk.MustExec("insert into t values(5);") - result = tk.MustQuery("select bit_and(a) from t") - result.Check(testkit.Rows("5")) - tk.MustExec("insert into t values(3);") - result = tk.MustQuery("select bit_and(a) from t") - result.Check(testkit.Rows("1")) - tk.MustExec("insert into t values(2);") - result = tk.MustQuery("select bit_and(a) from t") - result.Check(testkit.Rows("0")) - result = tk.MustQuery("select a, bit_and(a) from t group by a order by a desc") - result.Check(testkit.Rows("7 7", "5 5", "3 3", "2 2", " 18446744073709551615")) -} - -func TestAggregationBuiltinGroupConcat(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create table t(a varchar(100))") - tk.MustExec("create table d(a varchar(100))") - tk.MustExec("insert into t values('hello'), ('hello')") - result := tk.MustQuery("select group_concat(a) from t") - result.Check(testkit.Rows("hello,hello")) - - tk.MustExec("set @@group_concat_max_len=7") - result = tk.MustQuery("select group_concat(a) from t") - result.Check(testkit.Rows("hello,h")) - tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning 1260 Some rows were cut by GROUPCONCAT(test.t.a)")) - - tk.MustGetErrCode("insert into d select group_concat(a) from t", mysql.ErrCutValueGroupConcat) - - tk.MustExec("set sql_mode=''") - tk.MustExec("insert into d select group_concat(a) from t") - tk.MustQuery("show warnings").Check(testkit.RowsWithSep("|", "Warning 1260 Some rows were cut by GROUPCONCAT(test.t.a)")) - tk.MustQuery("select * from d").Check(testkit.Rows("hello,h")) -} - -func TestOtherBuiltin(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int, b double, c varchar(20), d datetime, e time)") - tk.MustExec("insert into t value(1, 2, 'string', '2017-01-01 12:12:12', '12:12:12')") - - // for in - result := tk.MustQuery("select 1 in (a, b, c), 'string' in (a, b, c), '2017-01-01 12:12:12' in (c, d, e), '12:12:12' in (c, d, e) from t") - result.Check(testkit.Rows("1 1 1 1")) - result = tk.MustQuery("select 1 in (null, c), 2 in (null, c) from t") - result.Check(testkit.Rows(" ")) - result = tk.MustQuery("select 0 in (a, b, c), 0 in (a, b, c), 3 in (a, b, c), 4 in (a, b, c) from t") - result.Check(testkit.Rows("1 1 0 0")) - result = tk.MustQuery("select (0,1) in ((0,1), (0,2)), (0,1) in ((0,0), (0,2))") - result.Check(testkit.Rows("1 0")) - - result = tk.MustQuery(`select bit_count(121), bit_count(-1), bit_count(null), bit_count("1231aaa");`) - result.Check(testkit.Rows("5 64 7")) - - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int primary key, b time, c double, d varchar(10))") - tk.MustExec(`insert into t values(1, '01:01:01', 1.1, "1"), (2, '02:02:02', 2.2, "2")`) - tk.MustExec(`insert into t(a, b) values(1, '12:12:12') on duplicate key update a = values(b)`) - result = tk.MustQuery(`select a from t order by a`) - result.Check(testkit.Rows("2", "121212")) - tk.MustExec(`insert into t values(2, '12:12:12', 1.1, "3.3") on duplicate key update a = values(c) + values(d)`) - result = tk.MustQuery(`select a from t order by a`) - result.Check(testkit.Rows("4", "121212")) - - // for setvar, getvar - tk.MustExec(`set @varname = "Abc"`) - result = tk.MustQuery(`select @varname, @VARNAME`) - result.Check(testkit.Rows("Abc Abc")) - - // for values - tk.MustExec("drop table t") - tk.MustExec("CREATE TABLE `t` (`id` varchar(32) NOT NULL, `count` decimal(18,2), PRIMARY KEY (`id`));") - tk.MustExec("INSERT INTO t (id,count)VALUES('abc',2) ON DUPLICATE KEY UPDATE count=if(VALUES(count) > count,VALUES(count),count)") - result = tk.MustQuery("select count from t where id = 'abc'") - result.Check(testkit.Rows("2.00")) - tk.MustExec("INSERT INTO t (id,count)VALUES('abc',265.0) ON DUPLICATE KEY UPDATE count=if(VALUES(count) > count,VALUES(count),count)") - result = tk.MustQuery("select count from t where id = 'abc'") - result.Check(testkit.Rows("265.00")) - - // for values(issue #4884) - tk.MustExec("drop table if exists t;") - tk.MustExec("create table test(id int not null, val text, primary key(id));") - tk.MustExec("insert into test values(1,'hello');") - result = tk.MustQuery("select * from test;") - result.Check(testkit.Rows("1 hello")) - tk.MustExec("insert into test values(1, NULL) on duplicate key update val = VALUES(val);") - result = tk.MustQuery("select * from test;") - result.Check(testkit.Rows("1 ")) - - tk.MustExec("drop table if exists test;") - tk.MustExec(`create table test( - id int not null, - a text, - b blob, - c varchar(20), - d int, - e float, - f DECIMAL(6,4), - g JSON, - primary key(id));`) - - tk.MustExec(`insert into test values(1,'txt hello', 'blb hello', 'vc hello', 1, 1.1, 1.0, '{"key1": "value1", "key2": "value2"}');`) - tk.MustExec(`insert into test values(1, NULL, NULL, NULL, NULL, NULL, NULL, NULL) - on duplicate key update - a = values(a), - b = values(b), - c = values(c), - d = values(d), - e = values(e), - f = values(f), - g = values(g);`) - - result = tk.MustQuery("select * from test;") - result.Check(testkit.Rows("1 ")) -} - -func TestDateBuiltin(t *testing.T) { - ctx := context.Background() - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("USE test;") - tk.MustExec("DROP TABLE IF EXISTS t;") - tk.MustExec("create table t (d date);") - tk.MustExec("insert into t values ('1997-01-02')") - tk.MustExec("insert into t values ('1998-01-02')") - r := tk.MustQuery("select * from t where d < date '1998-01-01';") - r.Check(testkit.Rows("1997-01-02")) - - r = tk.MustQuery("select date'20171212'") - r.Check(testkit.Rows("2017-12-12")) - - r = tk.MustQuery("select date'2017/12/12'") - r.Check(testkit.Rows("2017-12-12")) - - r = tk.MustQuery("select date'2017/12-12'") - r.Check(testkit.Rows("2017-12-12")) - - tk.MustExec("set sql_mode = ''") - r = tk.MustQuery("select date '0000-00-00';") - r.Check(testkit.Rows("0000-00-00")) - - tk.MustExec("set sql_mode = 'NO_ZERO_IN_DATE'") - r = tk.MustQuery("select date '0000-00-00';") - r.Check(testkit.Rows("0000-00-00")) - - tk.MustExec("set sql_mode = 'NO_ZERO_DATE'") - rs, err := tk.Exec("select date '0000-00-00';") - require.NoError(t, err) - _, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.Error(t, err) - require.True(t, terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateStr, "0000-00-00"))) - require.NoError(t, rs.Close()) - - tk.MustExec("set sql_mode = ''") - r = tk.MustQuery("select date '2007-10-00';") - r.Check(testkit.Rows("2007-10-00")) - - tk.MustExec("set sql_mode = 'NO_ZERO_IN_DATE'") - rs, _ = tk.Exec("select date '2007-10-00';") - _, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.Error(t, err) - require.True(t, terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateStr, "2017-10-00"))) - require.NoError(t, rs.Close()) - - tk.MustExec("set sql_mode = 'NO_ZERO_DATE'") - r = tk.MustQuery("select date '2007-10-00';") - r.Check(testkit.Rows("2007-10-00")) - - tk.MustExec("set sql_mode = 'NO_ZERO_IN_DATE,NO_ZERO_DATE'") - - rs, _ = tk.Exec("select date '2007-10-00';") - _, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.Error(t, err) - require.True(t, terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateStr, "2017-10-00"))) - require.NoError(t, rs.Close()) - - rs, err = tk.Exec("select date '0000-00-00';") - require.NoError(t, err) - _, err = session.GetRows4Test(ctx, tk.Session(), rs) - require.Error(t, err) - require.True(t, terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateStr, "2017-10-00"))) - require.NoError(t, rs.Close()) - - r = tk.MustQuery("select date'1998~01~02'") - r.Check(testkit.Rows("1998-01-02")) - - r = tk.MustQuery("select date'731124', date '011124'") - r.Check(testkit.Rows("1973-11-24 2001-11-24")) - - err = tk.ExecToErr("select date '0000-00-00 00:00:00';") - require.Error(t, err) - require.True(t, terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateStr, "0000-00-00 00:00:00"))) - - tk.MustGetDBError("select date '2017-99-99';", types.ErrWrongValue) - tk.MustGetDBError("select date '2017-2-31';", types.ErrWrongValue) - - err = tk.ExecToErr("select date '201712-31';") - require.Error(t, err) - require.True(t, terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateStr, "201712-31")), "err: %v", err) - - err = tk.ExecToErr("select date 'abcdefg';") - require.Error(t, err) - require.True(t, terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateStr, "abcdefg")), "err: %v", err) -} - -func TestTimeLiteral(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - - r := tk.MustQuery("select time '117:01:12';") - r.Check(testkit.Rows("117:01:12")) - - r = tk.MustQuery("select time '01:00:00.999999';") - r.Check(testkit.Rows("01:00:00.999999")) - - r = tk.MustQuery("select time '1 01:00:00';") - r.Check(testkit.Rows("25:00:00")) - - r = tk.MustQuery("select time '110:00:00';") - r.Check(testkit.Rows("110:00:00")) - - r = tk.MustQuery("select time'-1:1:1.123454656';") - r.Check(testkit.Rows("-01:01:01.123455")) - - r = tk.MustQuery("select time '33:33';") - r.Check(testkit.Rows("33:33:00")) - - r = tk.MustQuery("select time '1.1';") - r.Check(testkit.Rows("00:00:01.1")) - - r = tk.MustQuery("select time '21';") - r.Check(testkit.Rows("00:00:21")) - - r = tk.MustQuery("select time '20 20:20';") - r.Check(testkit.Rows("500:20:00")) - - err := tk.ExecToErr("select time '2017-01-01 00:00:00';") - require.Error(t, err) - require.True(t, terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.TimeStr, "2017-01-01 00:00:00"))) - - err = tk.ExecToErr("select time '071231235959.999999';") - require.Error(t, err) - require.True(t, terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.TimeStr, "071231235959.999999"))) - - err = tk.ExecToErr("select time '20171231235959.999999';") - require.Error(t, err) - require.True(t, terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.TimeStr, "20171231235959.999999"))) - - tk.MustExec("select ADDDATE('2008-01-34', -1);") - tk.MustQuery("Show warnings;").Check(testkit.RowsWithSep("|", - "Warning|1292|Incorrect datetime value: '2008-01-34'")) -} - -func TestTimestampLiteral(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - - r := tk.MustQuery("select timestamp '2017-01-01 00:00:00';") - r.Check(testkit.Rows("2017-01-01 00:00:00")) - - r = tk.MustQuery("select timestamp '2017@01@01 00:00:00';") - r.Check(testkit.Rows("2017-01-01 00:00:00")) - - r = tk.MustQuery("select timestamp '2017@01@01 00~00~00';") - r.Check(testkit.Rows("2017-01-01 00:00:00")) - - r = tk.MustQuery("select timestamp '2017@01@0001 00~00~00.333';") - r.Check(testkit.Rows("2017-01-01 00:00:00.333")) - - err := tk.ExecToErr("select timestamp '00:00:00';") - require.Error(t, err) - require.True(t, terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "00:00:00"))) - - err = tk.ExecToErr("select timestamp '1992-01-03';") - require.Error(t, err) - require.True(t, terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "1992-01-03"))) - - err = tk.ExecToErr("select timestamp '20171231235959.999999';") + // 2 * 2, error. + err = tk.ExecToErr(twoColumnQuery) require.Error(t, err) - require.True(t, terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "20171231235959.999999"))) -} - -func TestLiterals(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - r := tk.MustQuery("SELECT LENGTH(b''), LENGTH(B''), b''+1, b''-1, B''+1;") - r.Check(testkit.Rows("0 0 1 -1 1")) } func TestColumnInfoModified(t *testing.T) { @@ -2558,23 +1019,6 @@ func TestTiDBDecodeKeyFunc(t *testing.T) { tk.MustQuery(sql).Check(testkit.Rows(rs)) } -func TestTwoDecimalTruncate(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - - tk.MustExec("use test") - tk.MustExec("set sql_mode=''") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t1(a decimal(10,5), b decimal(10,1))") - tk.MustExec("insert into t1 values(123.12345, 123.12345)") - tk.MustExec("update t1 set b = a") - res := tk.MustQuery("select a, b from t1") - res.Check(testkit.Rows("123.12345 123.1")) - res = tk.MustQuery("select 2.00000000000000000000000000000001 * 1.000000000000000000000000000000000000000000002") - res.Check(testkit.Rows("2.000000000000000000000000000000")) -} - func TestPrefixIndex(t *testing.T) { store := testkit.CreateMockStore(t) @@ -2607,51 +1051,6 @@ func TestPrefixIndex(t *testing.T) { res.Check(testkit.Rows("7 ÿÿ", "8 ÿÿ0", "9 ÿÿÿ")) } -func TestDecimalMul(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("USE test") - tk.MustExec("create table t(a decimal(38, 17));") - tk.MustExec("insert into t select 0.5999991229316*0.918755041726043;") - res := tk.MustQuery("select * from t;") - res.Check(testkit.Rows("0.55125221922461136")) -} - -func TestDecimalDiv(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustQuery("select cast(1 as decimal(60,30)) / cast(1 as decimal(60,30)) / cast(1 as decimal(60, 30))").Check(testkit.Rows("1.000000000000000000000000000000")) - tk.MustQuery("select cast(1 as decimal(60,30)) / cast(3 as decimal(60,30)) / cast(7 as decimal(60, 30))").Check(testkit.Rows("0.047619047619047619047619047619")) - tk.MustQuery("select cast(1 as decimal(60,30)) / cast(3 as decimal(60,30)) / cast(7 as decimal(60, 30)) / cast(13 as decimal(60, 30))").Check(testkit.Rows("0.003663003663003663003663003663")) -} - -func TestUnknowHintIgnore(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("USE test") - tk.MustExec("create table t(a int)") - tk.MustQuery("select /*+ unknown_hint(c1)*/ 1").Check(testkit.Rows("1")) - tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1064 Optimizer hint syntax error at line 1 column 23 near \"unknown_hint(c1)*/\" ")) - rs, err := tk.Exec("select 1 from /*+ test1() */ t") - require.NoError(t, err) - rs.Close() -} - -func TestValuesInNonInsertStmt(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec(`use test;`) - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t(a bigint, b double, c decimal, d varchar(20), e datetime, f time, g json);`) - tk.MustExec(`insert into t values(1, 1.1, 2.2, "abc", "2018-10-24", NOW(), "12");`) - res := tk.MustQuery(`select values(a), values(b), values(c), values(d), values(e), values(f), values(g) from t;`) - res.Check(testkit.Rows(` `)) -} - func TestUserVarMockWindFunc(t *testing.T) { store := testkit.CreateMockStore(t) @@ -2727,106 +1126,6 @@ func TestUserVarMockWindFunc(t *testing.T) { )) } -func TestCastAsTime(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec(`use test;`) - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t (col1 bigint, col2 double, col3 decimal, col4 varchar(20), col5 json);`) - tk.MustExec(`insert into t values (1, 1, 1, "1", "1");`) - tk.MustExec(`insert into t values (null, null, null, null, null);`) - tk.MustQuery(`select cast(col1 as time), cast(col2 as time), cast(col3 as time), cast(col4 as time), cast(col5 as time) from t where col1 = 1;`).Check(testkit.Rows( - `00:00:01 00:00:01 00:00:01 00:00:01 `, - )) - tk.MustQuery(`select cast(col1 as time), cast(col2 as time), cast(col3 as time), cast(col4 as time), cast(col5 as time) from t where col1 is null;`).Check(testkit.Rows( - ` `, - )) - - err := tk.ExecToErr(`select cast(col1 as time(31)) from t where col1 is null;`) - require.Error(t, err, "[expression:1426]Too big precision 31 specified for column 'CAST'. Maximum is 6.") - - err = tk.ExecToErr(`select cast(col2 as time(31)) from t where col1 is null;`) - require.Error(t, err, "[expression:1426]Too big precision 31 specified for column 'CAST'. Maximum is 6.") - - err = tk.ExecToErr(`select cast(col3 as time(31)) from t where col1 is null;`) - require.Error(t, err, "[expression:1426]Too big precision 31 specified for column 'CAST'. Maximum is 6.") - - err = tk.ExecToErr(`select cast(col4 as time(31)) from t where col1 is null;`) - require.Error(t, err, "[expression:1426]Too big precision 31 specified for column 'CAST'. Maximum is 6.") - - err = tk.ExecToErr(`select cast(col5 as time(31)) from t where col1 is null;`) - require.Error(t, err, "[expression:1426]Too big precision 31 specified for column 'CAST'. Maximum is 6.") -} - -func TestValuesFloat32(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t (i int key, j float);`) - tk.MustExec(`insert into t values (1, 0.01);`) - tk.MustQuery(`select * from t;`).Check(testkit.Rows(`1 0.01`)) - tk.MustExec(`insert into t values (1, 0.02) on duplicate key update j = values (j);`) - tk.MustQuery(`select * from t;`).Check(testkit.Rows(`1 0.02`)) -} - -func TestFuncNameConst(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - - tk.MustExec("USE test;") - tk.MustExec("DROP TABLE IF EXISTS t;") - tk.MustExec("CREATE TABLE t(a CHAR(20), b VARCHAR(20), c BIGINT);") - tk.MustExec("INSERT INTO t (b, c) values('hello', 1);") - - r := tk.MustQuery("SELECT name_const('test_int', 1), name_const('test_float', 3.1415);") - r.Check(testkit.Rows("1 3.1415")) - r = tk.MustQuery("SELECT name_const('test_string', 'hello'), name_const('test_nil', null);") - r.Check(testkit.Rows("hello ")) - r = tk.MustQuery("SELECT name_const('test_string', 1) + c FROM t;") - r.Check(testkit.Rows("2")) - r = tk.MustQuery("SELECT concat('hello', name_const('test_string', 'world')) FROM t;") - r.Check(testkit.Rows("helloworld")) - r = tk.MustQuery("SELECT NAME_CONST('come', -1);") - r.Check(testkit.Rows("-1")) - r = tk.MustQuery("SELECT NAME_CONST('come', -1.0);") - r.Check(testkit.Rows("-1.0")) - err := tk.ExecToErr(`select name_const(a,b) from t;`) - require.Error(t, err, "[planner:1210]Incorrect arguments to NAME_CONST") - err = tk.ExecToErr(`select name_const(a,"hello") from t;`) - require.Error(t, err, "[planner:1210]Incorrect arguments to NAME_CONST") - err = tk.ExecToErr(`select name_const("hello", b) from t;`) - require.Error(t, err, "[planner:1210]Incorrect arguments to NAME_CONST") - err = tk.ExecToErr(`select name_const("hello", 1+1) from t;`) - require.Error(t, err, "[planner:1210]Incorrect arguments to NAME_CONST") - err = tk.ExecToErr(`select name_const(concat('a', 'b'), 555) from t;`) - require.Error(t, err, "[planner:1210]Incorrect arguments to NAME_CONST") - err = tk.ExecToErr(`select name_const(555) from t;`) - require.Error(t, err, "[expression:1582]Incorrect parameter count in the call to native function 'name_const'") - - var rs sqlexec.RecordSet - rs, err = tk.Exec(`select name_const("hello", 1);`) - require.NoError(t, err) - require.Len(t, rs.Fields(), 1) - require.Equal(t, "hello", rs.Fields()[0].Column.Name.L) -} - -func TestValuesEnum(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t (a bigint primary key, b enum('a','b','c'));`) - tk.MustExec(`insert into t values (1, "a");`) - tk.MustQuery(`select * from t;`).Check(testkit.Rows(`1 a`)) - tk.MustExec(`insert into t values (1, "b") on duplicate key update b = values(b);`) - tk.MustQuery(`select * from t;`).Check(testkit.Rows(`1 b`)) -} - func TestIssue9710(t *testing.T) { store := testkit.CreateMockStore(t) @@ -2858,108 +1157,6 @@ func TestIssue9710(t *testing.T) { } } -// TestDecimalConvertToTime for issue #9770 -func TestDecimalConvertToTime(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a datetime(6), b timestamp)") - tk.MustExec("insert t values (20010101100000.123456, 20110707101112.123456)") - tk.MustQuery("select * from t").Check(testkit.Rows("2001-01-01 10:00:00.123456 2011-07-07 10:11:12")) -} - -func TestDaynameArithmetic(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - - cases := []struct { - sql string - result string - }{ - {`select dayname("1962-03-01")+0;`, "3"}, - {`select dayname("1962-03-02")+0;`, "4"}, - {`select dayname("1962-03-03")+0;`, "5"}, - {`select dayname("1962-03-04")+0;`, "6"}, - {`select dayname("1962-03-05")+0;`, "0"}, - {`select dayname("1962-03-06")+0;`, "1"}, - {`select dayname("1962-03-07")+0;`, "2"}, - {`select dayname("1962-03-08")+0;`, "3"}, - {`select dayname("1962-03-01")+1;`, "4"}, - {`select dayname("1962-03-01")+2;`, "5"}, - {`select dayname("1962-03-01")+3;`, "6"}, - {`select dayname("1962-03-01")+4;`, "7"}, - {`select dayname("1962-03-01")+5;`, "8"}, - {`select dayname("1962-03-01")+6;`, "9"}, - {`select dayname("1962-03-01")+7;`, "10"}, - {`select dayname("1962-03-01")+2333;`, "2336"}, - {`select dayname("1962-03-01")+2.333;`, "5.333"}, - {`select dayname("1962-03-01")>2;`, "1"}, - {`select dayname("1962-03-01")<2;`, "0"}, - {`select dayname("1962-03-01")=3;`, "1"}, - {`select dayname("1962-03-01")!=3;`, "0"}, - {`select dayname("1962-03-01")<4;`, "1"}, - {`select dayname("1962-03-01")>4;`, "0"}, - {`select !dayname("1962-03-01");`, "0"}, - {`select dayname("1962-03-01")&1;`, "1"}, - {`select dayname("1962-03-01")&3;`, "3"}, - {`select dayname("1962-03-01")&7;`, "3"}, - {`select dayname("1962-03-01")|1;`, "3"}, - {`select dayname("1962-03-01")|3;`, "3"}, - {`select dayname("1962-03-01")|7;`, "7"}, - {`select dayname("1962-03-01")^1;`, "2"}, - {`select dayname("1962-03-01")^3;`, "0"}, - {`select dayname("1962-03-01")^7;`, "4"}, - } - - for _, c := range cases { - tk.MustQuery(c.sql).Check(testkit.Rows(c.result)) - } -} - -func TestTimestampDatumEncode(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec(`drop table if exists t;`) - tk.MustExec(`create table t (a bigint primary key, b timestamp)`) - tk.MustExec(`insert into t values (1, "2019-04-29 11:56:12")`) - tk.MustQuery(`explain format = 'brief' select * from t where b = (select max(b) from t)`).Check(testkit.Rows( - "TableReader 10.00 root data:Selection", - "└─Selection 10.00 cop[tikv] eq(test.t.b, 2019-04-29 11:56:12)", - " └─TableFullScan 10000.00 cop[tikv] table:t keep order:false, stats:pseudo", - )) - tk.MustQuery(`select * from t where b = (select max(b) from t)`).Check(testkit.Rows(`1 2019-04-29 11:56:12`)) -} - -func TestDateTimeAddReal(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - - cases := []struct { - sql string - result string - }{ - {`SELECT "1900-01-01 00:00:00" + INTERVAL 1.123456789e3 SECOND;`, "1900-01-01 00:18:43.456789"}, - {`SELECT 19000101000000 + INTERVAL 1.123456789e3 SECOND;`, "1900-01-01 00:18:43.456789"}, - {`select date("1900-01-01") + interval 1.123456789e3 second;`, "1900-01-01 00:18:43.456789"}, - {`SELECT "1900-01-01 00:18:43.456789" - INTERVAL 1.123456789e3 SECOND;`, "1900-01-01 00:00:00"}, - {`SELECT 19000101001843.456789 - INTERVAL 1.123456789e3 SECOND;`, "1900-01-01 00:00:00"}, - {`SELECT 19000101000000.0005 + INTERVAL 0.0005 SECOND;`, "1900-01-01 00:00:00.001000"}, - {`select date("1900-01-01") - interval 1.123456789e3 second;`, "1899-12-31 23:41:16.543211"}, - {`select 19000101000000 - interval 1.123456789e3 second;`, "1899-12-31 23:41:16.543211"}, - } - - for _, c := range cases { - tk.MustQuery(c.sql).Check(testkit.Rows(c.result)) - } -} - func TestExprPushdown(t *testing.T) { store := testkit.CreateMockStore(t) @@ -3103,175 +1300,6 @@ func TestIssue10804(t *testing.T) { tk.MustQuery(`SELECT @@GLOBAL.information_schema_stats_expiry`).Check(testkit.Rows(`0`)) } -func TestDatetimeMicrosecond(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - // For int - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 SECOND_MICROSECOND);`).Check( - testkit.Rows("2007-03-28 22:08:27.800000")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 MINUTE_MICROSECOND);`).Check( - testkit.Rows("2007-03-28 22:08:27.800000")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 HOUR_MICROSECOND);`).Check( - testkit.Rows("2007-03-28 22:08:27.800000")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 DAY_MICROSECOND);`).Check( - testkit.Rows("2007-03-28 22:08:27.800000")) - - // For decimal - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR_MINUTE);`).Check( - testkit.Rows("2007-03-29 00:10:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MINUTE_SECOND);`).Check( - testkit.Rows("2007-03-28 22:10:30")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR_MONTH);`).Check( - testkit.Rows("2009-05-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_HOUR);`).Check( - testkit.Rows("2007-03-31 00:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_MINUTE);`).Check( - testkit.Rows("2007-03-29 00:10:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_SECOND);`).Check( - testkit.Rows("2007-03-28 22:10:30")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR_SECOND);`).Check( - testkit.Rows("2007-03-28 22:10:30")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 SECOND);`).Check( - testkit.Rows("2007-03-28 22:08:30.200000")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR);`).Check( - testkit.Rows("2009-03-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 QUARTER);`).Check( - testkit.Rows("2007-09-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MONTH);`).Check( - testkit.Rows("2007-05-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 WEEK);`).Check( - testkit.Rows("2007-04-11 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY);`).Check( - testkit.Rows("2007-03-30 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR);`).Check( - testkit.Rows("2007-03-29 00:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MINUTE);`).Check( - testkit.Rows("2007-03-28 22:10:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MICROSECOND);`).Check( - testkit.Rows("2007-03-28 22:08:28.000002")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_MINUTE);`).Check( - testkit.Rows("2007-03-28 20:06:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE_SECOND);`).Check( - testkit.Rows("2007-03-28 22:06:26")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 YEAR_MONTH);`).Check( - testkit.Rows("2005-01-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_HOUR);`).Check( - testkit.Rows("2007-03-26 20:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_MINUTE);`).Check( - testkit.Rows("2007-03-28 20:06:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_SECOND);`).Check( - testkit.Rows("2007-03-28 22:06:26")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_SECOND);`).Check( - testkit.Rows("2007-03-28 22:06:26")) - // tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 SECOND);`).Check( - // testkit.Rows("2007-03-28 22:08:25.800000")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 YEAR);`).Check( - testkit.Rows("2005-03-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 QUARTER);`).Check( - testkit.Rows("2006-09-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MONTH);`).Check( - testkit.Rows("2007-01-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 WEEK);`).Check( - testkit.Rows("2007-03-14 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY);`).Check( - testkit.Rows("2007-03-26 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR);`).Check( - testkit.Rows("2007-03-28 20:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE);`).Check( - testkit.Rows("2007-03-28 22:06:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MICROSECOND);`).Check( - testkit.Rows("2007-03-28 22:08:27.999998")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR_MINUTE);`).Check( - testkit.Rows("2007-03-28 20:06:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MINUTE_SECOND);`).Check( - testkit.Rows("2007-03-28 22:06:26")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" YEAR_MONTH);`).Check( - testkit.Rows("2005-01-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_HOUR);`).Check( - testkit.Rows("2007-03-26 20:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_MINUTE);`).Check( - testkit.Rows("2007-03-28 20:06:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_SECOND);`).Check( - testkit.Rows("2007-03-28 22:06:26")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR_SECOND);`).Check( - testkit.Rows("2007-03-28 22:06:26")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" SECOND);`).Check( - testkit.Rows("2007-03-28 22:08:25.800000")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" YEAR);`).Check( - testkit.Rows("2005-03-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" QUARTER);`).Check( - testkit.Rows("2006-09-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MONTH);`).Check( - testkit.Rows("2007-01-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" WEEK);`).Check( - testkit.Rows("2007-03-14 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY);`).Check( - testkit.Rows("2007-03-26 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR);`).Check( - testkit.Rows("2007-03-28 20:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MINUTE);`).Check( - testkit.Rows("2007-03-28 22:06:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MICROSECOND);`).Check( - testkit.Rows("2007-03-28 22:08:27.999998")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR_MINUTE);`).Check( - testkit.Rows("2007-03-28 20:06:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MINUTE_SECOND);`).Check( - testkit.Rows("2007-03-28 22:06:26")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" YEAR_MONTH);`).Check( - testkit.Rows("2005-01-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_HOUR);`).Check( - testkit.Rows("2007-03-26 20:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_MINUTE);`).Check( - testkit.Rows("2007-03-28 20:06:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_SECOND);`).Check( - testkit.Rows("2007-03-28 22:06:26")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR_SECOND);`).Check( - testkit.Rows("2007-03-28 22:06:26")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" SECOND);`).Check( - testkit.Rows("2007-03-28 22:08:26")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.+2" SECOND);`).Check( - testkit.Rows("2007-03-28 22:08:26")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.*2" SECOND);`).Check( - testkit.Rows("2007-03-28 22:08:26")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2./2" SECOND);`).Check( - testkit.Rows("2007-03-28 22:08:26")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.a2" SECOND);`).Check( - testkit.Rows("2007-03-28 22:08:26")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" YEAR);`).Check( - testkit.Rows("2005-03-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" QUARTER);`).Check( - testkit.Rows("2006-09-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MONTH);`).Check( - testkit.Rows("2007-01-28 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" WEEK);`).Check( - testkit.Rows("2007-03-14 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY);`).Check( - testkit.Rows("2007-03-26 22:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR);`).Check( - testkit.Rows("2007-03-28 20:08:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MINUTE);`).Check( - testkit.Rows("2007-03-28 22:06:28")) - tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MICROSECOND);`).Check( - testkit.Rows("2007-03-28 22:08:27.999998")) -} - -func TestFuncCaseWithLeftJoin(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - - tk.MustExec("create table kankan1(id int, name text)") - tk.MustExec("insert into kankan1 values(1, 'a')") - tk.MustExec("insert into kankan1 values(2, 'a')") - - tk.MustExec("create table kankan2(id int, h1 text)") - tk.MustExec("insert into kankan2 values(2, 'z')") - - tk.MustQuery("select t1.id from kankan1 t1 left join kankan2 t2 on t1.id = t2.id where (case when t1.name='b' then 'case2' when t1.name='a' then 'case1' else NULL end) = 'case1' order by t1.id").Check(testkit.Rows("1", "2")) -} - func TestNotExistFunc(t *testing.T) { store := testkit.CreateMockStore(t) @@ -3339,115 +1367,6 @@ func TestDecodetoChunkReuse(t *testing.T) { rs.Close() } -func TestInMeetsPrepareAndExecute(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("prepare pr1 from 'select ? in (1,?,?)'") - tk.MustExec("set @a=1, @b=2, @c=3") - tk.MustQuery("execute pr1 using @a,@b,@c").Check(testkit.Rows("1")) - - tk.MustExec("prepare pr2 from 'select 3 in (1,?,?)'") - tk.MustExec("set @a=2, @b=3") - tk.MustQuery("execute pr2 using @a,@b").Check(testkit.Rows("1")) - - tk.MustExec("prepare pr3 from 'select ? in (1,2,3)'") - tk.MustExec("set @a=4") - tk.MustQuery("execute pr3 using @a").Check(testkit.Rows("0")) - - tk.MustExec("prepare pr4 from 'select ? in (?,?,?)'") - tk.MustExec("set @a=1, @b=2, @c=3, @d=4") - tk.MustQuery("execute pr4 using @a,@b,@c,@d").Check(testkit.Rows("0")) -} - -func TestCastStrToInt(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - cases := []struct { - sql string - result int - }{ - {"select cast('' as signed)", 0}, - {"select cast('12345abcde' as signed)", 12345}, - {"select cast('123e456' as signed)", 123}, - {"select cast('-12345abcde' as signed)", -12345}, - {"select cast('-123e456' as signed)", -123}, - } - for _, ca := range cases { - tk.Session().GetSessionVars().StmtCtx.SetWarnings(nil) - tk.MustQuery(ca.sql).Check(testkit.Rows(fmt.Sprintf("%v", ca.result))) - require.True(t, terror.ErrorEqual(tk.Session().GetSessionVars().StmtCtx.GetWarnings()[0].Err, types.ErrTruncatedWrongVal)) - } -} - -func TestOrderByFuncPlanCache(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec(`set tidb_enable_prepared_plan_cache=1`) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int)") - tk.MustExec("prepare stmt from 'SELECT * FROM t order by rand()'") - tk.MustQuery("execute stmt").Check(testkit.Rows()) - tk.MustExec("prepare stmt from 'SELECT * FROM t order by now()'") - tk.MustQuery("execute stmt").Check(testkit.Rows()) -} - -func TestSelectLimitPlanCache(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec(`set tidb_enable_prepared_plan_cache=1`) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a int)") - tk.MustExec("insert into t values(1), (2), (3)") - tk.MustExec("set @@session.sql_select_limit = 1") - tk.MustExec("prepare stmt from 'SELECT * FROM t'") - tk.MustQuery("execute stmt").Check(testkit.Rows("1")) - tk.MustExec("set @@session.sql_select_limit = default") - tk.MustQuery("execute stmt").Check(testkit.Rows("1", "2", "3")) - tk.MustExec("set @@session.sql_select_limit = 2") - tk.MustQuery("execute stmt").Check(testkit.Rows("1", "2")) - tk.MustExec("set @@session.sql_select_limit = 1") - tk.MustQuery("execute stmt").Check(testkit.Rows("1")) - tk.MustExec("set @@session.sql_select_limit = default") - tk.MustQuery("execute stmt").Check(testkit.Rows("1", "2", "3")) - tk.MustExec("set @@session.sql_select_limit = 2") - tk.MustQuery("execute stmt").Check(testkit.Rows("1", "2")) -} - -func TestVirtualGeneratedColumnAndLimit(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t;") - tk.MustExec("create table t (a int, b int as (a + 1));") - tk.MustExec("insert into t(a) values (1);") - tk.MustQuery("select /*+ LIMIT_TO_COP() */ b from t limit 1;").Check(testkit.Rows("2")) - tk.MustQuery("select /*+ LIMIT_TO_COP() */ b from t order by b limit 1;").Check(testkit.Rows("2")) -} - -func TestNegativeZeroForHashJoin(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test;") - tk.MustExec("drop table if exists t0, t1") - tk.MustExec("CREATE TABLE t0(c0 float);") - tk.MustExec("CREATE TABLE t1(c0 float);") - tk.MustExec("INSERT INTO t1(c0) VALUES (0);") - tk.MustExec("INSERT INTO t0(c0) VALUES (0);") - tk.MustQuery("SELECT t1.c0 FROM t1, t0 WHERE t0.c0=-t1.c0;").Check(testkit.Rows("0")) - tk.MustExec("drop TABLE t0;") - tk.MustExec("drop table t1;") -} - func TestCTEWithDML(t *testing.T) { store := testkit.CreateMockStore(t) @@ -3809,16 +1728,6 @@ func TestDatetimeUserVariable(t *testing.T) { require.NotEqual(t, "", tk.MustQuery("select @p").Rows()[0][0]) } -func TestCastCoer(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - - tk.MustQuery("select coercibility(binary('a'))").Check(testkit.Rows("2")) - tk.MustQuery("select coercibility(cast('a' as char(10)))").Check(testkit.Rows("2")) - tk.MustQuery("select coercibility(convert('abc', char(10)));").Check(testkit.Rows("2")) -} - func TestEnumPushDown(t *testing.T) { store := testkit.CreateMockStore(t) @@ -3922,32 +1831,6 @@ func TestEnumPushDown(t *testing.T) { tk.MustQuery("select c12+0 from tdm").Check(testkit.Rows("0")) } -func TestJiraSetInnoDBDefaultRowFormat(t *testing.T) { - // For issue #23541 - // JIRA needs to be able to set this to be happy. - // See: https://nova.moe/run-jira-on-tidb/ - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("set global innodb_default_row_format = dynamic") - tk.MustExec("set global innodb_default_row_format = 'dynamic'") - tk.MustQuery("SHOW VARIABLES LIKE 'innodb_default_row_format'").Check(testkit.Rows("innodb_default_row_format dynamic")) - tk.MustQuery("SHOW VARIABLES LIKE 'character_set_server'").Check(testkit.Rows("character_set_server utf8mb4")) - tk.MustQuery("SHOW VARIABLES LIKE 'innodb_file_format'").Check(testkit.Rows("innodb_file_format Barracuda")) - tk.MustQuery("SHOW VARIABLES LIKE 'innodb_large_prefix'").Check(testkit.Rows("innodb_large_prefix ON")) -} - -func TestApproximatePercentile(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t (a bit(10))") - tk.MustExec("insert into t values(b'1111')") - tk.MustQuery("select approx_percentile(a, 10) from t").Check(testkit.Rows("")) -} - func TestSecurityEnhancedMode(t *testing.T) { store := testkit.CreateMockStore(t) @@ -4003,21 +1886,6 @@ func TestCTEInvalidUsage(t *testing.T) { tk.MustGetErrCode("with recursive cte(n) as (select 1 union select 1 except select * from cte) select * from cte;", errno.ErrNotSupportedYet) } -func TestRefineArgNullValues(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create table t(id int primary key, a int)") - tk.MustExec("create table s(a int)") - tk.MustExec("insert into s values(1),(2)") - - tk.MustQuery("select t.id = 1.234 from t right join s on t.a = s.a").Check(testkit.Rows( - "", - "", - )) -} - func TestEnumIndex(t *testing.T) { elems := []string{"\"a\"", "\"b\"", "\"c\""} rand.Shuffle(len(elems), func(i, j int) { @@ -4254,24 +2122,6 @@ func TestControlFunctionWithEnumOrSet(t *testing.T) { tk.MustQuery("SELECT '1' = (case when 0 <=> 1 then a else a end) from t;").Check(testkit.Rows("1")) } -func TestComplexShowVariables(t *testing.T) { - // This is an example SHOW VARIABLES from mysql-connector-java-5.1.34 - // It returns 19 rows in MySQL 5.7 (the language sysvar no longer exists in 5.6+) - // and 16 rows in MySQL 8.0 (the aliases for tx_isolation is removed, along with query cache) - // In the event that we hide noop sysvars in future, we must keep these variables. - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - require.Len(t, tk.MustQuery(`SHOW VARIABLES WHERE Variable_name ='language' OR Variable_name = 'net_write_timeout' OR Variable_name = 'interactive_timeout' -OR Variable_name = 'wait_timeout' OR Variable_name = 'character_set_client' OR Variable_name = 'character_set_connection' -OR Variable_name = 'character_set' OR Variable_name = 'character_set_server' OR Variable_name = 'tx_isolation' -OR Variable_name = 'transaction_isolation' OR Variable_name = 'character_set_results' OR Variable_name = 'timezone' -OR Variable_name = 'time_zone' OR Variable_name = 'system_time_zone' -OR Variable_name = 'lower_case_table_names' OR Variable_name = 'max_allowed_packet' OR Variable_name = 'net_buffer_length' -OR Variable_name = 'sql_mode' OR Variable_name = 'query_cache_type' OR Variable_name = 'query_cache_size' -OR Variable_name = 'license' OR Variable_name = 'init_connect'`).Rows(), 19) -} - func TestBuiltinFuncJSONMergePatch_InColumn(t *testing.T) { ctx := context.Background() store := testkit.CreateMockStore(t) @@ -4462,16 +2312,6 @@ func TestBuiltinFuncJSONMergePatch_InExpression(t *testing.T) { } } -func TestFloat64Inf(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustQuery("select '1e800' + 1e100;").Check( - testkit.Rows("179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")) - tk.MustQuery("select '-1e800' - 1e100;").Check( - testkit.Rows("-179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")) -} - // issue https://github.com/pingcap/tidb/issues/28544 func TestPrimaryKeyRequiredSysvar(t *testing.T) { store := testkit.CreateMockStore(t) @@ -4525,42 +2365,6 @@ func TestPrimaryKeyRequiredSysvar(t *testing.T) { } } -// issue https://github.com/pingcap/tidb/issues/26111 -func TestRailsFKUsage(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec(`CREATE TABLE author_addresses ( - id bigint(20) NOT NULL AUTO_INCREMENT, - PRIMARY KEY (id) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`) - tk.MustExec(`CREATE TABLE authors ( - id bigint(20) NOT NULL AUTO_INCREMENT, - name varchar(255) NOT NULL, - author_address_id bigint(20) DEFAULT NULL, - author_address_extra_id bigint(20) DEFAULT NULL, - organization_id varchar(255) DEFAULT NULL, - owned_essay_id varchar(255) DEFAULT NULL, - PRIMARY KEY (id), - KEY index_authors_on_author_address_id (author_address_id), - KEY index_authors_on_author_address_extra_id (author_address_extra_id), - CONSTRAINT fk_rails_94423a17a3 FOREIGN KEY (author_address_id) REFERENCES author_addresses (id) ON UPDATE CASCADE ON DELETE RESTRICT - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`) - tk.MustQuery(`SELECT fk.referenced_table_name AS 'to_table', - fk.referenced_column_name AS 'primary_key', - fk.column_name AS 'column', - fk.constraint_name AS 'name', - rc.update_rule AS 'on_update', - rc.delete_rule AS 'on_delete' - FROM information_schema.referential_constraints rc - JOIN information_schema.key_column_usage fk - USING (constraint_schema, constraint_name) - WHERE fk.referenced_column_name IS NOT NULL - AND fk.table_schema = database() - AND fk.table_name = 'authors';`).Check(testkit.Rows("author_addresses id author_address_id fk_rails_94423a17a3 CASCADE RESTRICT")) -} - func TestTranslate(t *testing.T) { cases := []string{"'ABC'", "'AABC'", "'A.B.C'", "'aaaaabbbbb'", "'abc'", "'aaa'", "NULL"} store := testkit.CreateMockStore(t) @@ -4617,22 +2421,6 @@ func TestConstPropNullFunctions(t *testing.T) { tk.MustQuery("select * from t2 where t2.i2=((select count(1) from t1 where t1.i1=t2.i2))").Check(testkit.Rows("1 0.1")) } -func TestLastInsertId(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec(`use test;`) - tk.MustExec(`drop table if exists lastinsertid;`) - tk.MustExec(`create table lastinsertid (id int not null primary key auto_increment);`) - tk.MustQuery("SELECT @@last_insert_id;").Check(testkit.Rows("0")) - tk.MustExec(`INSERT INTO lastinsertid VALUES (NULL);`) - tk.MustQuery("SELECT @@last_insert_id, LAST_INSERT_ID()").Check(testkit.Rows("1 1")) - tk.MustExec(`INSERT INTO lastinsertid VALUES (NULL);`) - tk.MustQuery("SELECT @@last_insert_id, LAST_INSERT_ID()").Check(testkit.Rows("2 2")) - tk.MustExec(`INSERT INTO lastinsertid VALUES (NULL);`) - tk.MustQuery("SELECT @@last_insert_id, LAST_INSERT_ID()").Check(testkit.Rows("3 3")) -} - func TestTimestamp(t *testing.T) { store := testkit.CreateMockStore(t) @@ -4682,103 +2470,6 @@ func TestTimestamp(t *testing.T) { require.Less(t, now2.UnixNano(), now3.UnixNano()) } -func TestIdentity(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec(`use test;`) - tk.MustExec(`drop table if exists identity;`) - tk.MustExec(`create table identity (id int not null primary key auto_increment);`) - tk.MustQuery("SELECT @@identity;").Check(testkit.Rows("0")) - tk.MustExec(`INSERT INTO identity VALUES (NULL);`) - tk.MustQuery("SELECT @@identity, LAST_INSERT_ID()").Check(testkit.Rows("1 1")) - tk.MustExec(`INSERT INTO identity VALUES (NULL);`) - tk.MustQuery("SELECT @@identity, LAST_INSERT_ID()").Check(testkit.Rows("2 2")) - tk.MustExec(`INSERT INTO identity VALUES (NULL);`) - tk.MustQuery("SELECT @@identity, LAST_INSERT_ID()").Check(testkit.Rows("3 3")) -} - -func TestTimestampAddWithFractionalSecond(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(a date)") - tk.MustExec("insert into t values ('2021-08-20');") - tk.MustQuery("select timestampadd(microsecond, 1, a) from t").Check(testkit.Rows("2021-08-20 00:00:00.000001")) - tk.MustQuery("select timestampadd(second, 6/4, a) from t").Check(testkit.Rows("2021-08-20 00:00:01.500000")) - tk.MustQuery("select timestampadd(second, 9.9999e2, a) from t").Check(testkit.Rows("2021-08-20 00:16:39.990000")) - tk.MustQuery("select timestampadd(second, 1, '2021-08-20 00:00:01.0001')").Check(testkit.Rows("2021-08-20 00:00:02.000100")) - tk.MustQuery("select timestampadd(minute, 1.5, '2021-08-20 00:00:00')").Check(testkit.Rows("2021-08-20 00:02:00")) - tk.MustQuery("select timestampadd(minute, 1.5, '2021-08-20 00:00:00.0001')").Check(testkit.Rows("2021-08-20 00:02:00.000100")) - // overflow - tk.MustQuery("SELECT timestampadd(year,1.212208e+308,'1995-01-05 06:32:20.859724') as result").Check(testkit.Rows("")) - warnings := tk.Session().GetSessionVars().StmtCtx.GetWarnings() - require.Len(t, warnings, 1) - for _, warning := range warnings { - require.EqualError(t, warning.Err, "[types:1441]Datetime function: datetime field overflow") - } -} - -func TestDateAddForNonExistingTimestamp(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - tk.MustExec("set time_zone = 'CET'") - tk.MustExec("use test") - tk.MustExec("drop table if exists t") - tk.MustExec("create table t(ts timestamp)") - tk.MustExec("set time_zone = 'UTC'") - tk.MustExec("insert into t values('2022-03-27 00:30:00')") - tk.MustExec("insert into t values('2022-10-30 00:30:00')") - tk.MustExec("insert into t values('2022-10-30 01:30:00')") - tk.MustExec("set time_zone = 'Europe/Amsterdam'") - // Non-existing CET timestamp. - tk.MustGetErrCode("insert into t values('2022-03-27 02:30:00')", errno.ErrTruncatedWrongValue) - tk.MustQuery("select date_add(ts, interval 1 hour) from t order by ts").Check([][]interface{}{ - {"2022-03-27 02:30:00"}, - {"2022-10-30 03:30:00"}, - {"2022-10-30 03:30:00"}, - }) - tk.MustExec("drop table t") -} - -func TestCastRealAsTime(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create table t(d1 double, f float, d2 decimal(24,8))") - - // zero input - tk.MustExec("insert into t values(0, 0, 0)") - - // const - tk.MustQuery("select cast(111.1 as datetime) from t").Check(testkit.Rows("2000-01-11 00:00:00")) - tk.MustQuery("select cast(1311.1 as datetime) from t").Check(testkit.Rows("")) - - // vec - // valid input - tk.MustExec("insert into t values(111.1, 1122.1, 31212.111)") - tk.MustExec("insert into t values(121212.1111, 1121212.111111, 11121212.111111)") - tk.MustExec("insert into t values(99991111.1111111, 101.1111111, 20121212121212.1111111)") - // null input - tk.MustExec("insert into t values(NULL, NULL, NULL)") - // invalid input - tk.MustExec("insert into t values(1.1, 48.1, 100.1)") - tk.MustExec("insert into t values(1301.11, 1131.111, 100001111.111)") - tk.MustExec("insert into t values(20121212121260.1111111, 20121212126012.1111111, 20121212241212.1111111)") - tk.MustQuery("select cast(d1 as datetime), cast(f as datetime), cast(d2 as datetime) from t").Check(testkit.Rows( - "0000-00-00 00:00:00 0000-00-00 00:00:00 0000-00-00 00:00:00", - "2000-01-11 00:00:00 2000-11-22 00:00:00 2003-12-12 00:00:00", - "2012-12-12 00:00:00 0112-12-12 00:00:00 1112-12-12 00:00:00", - "9999-11-11 00:00:00 2000-01-01 00:00:00 2012-12-12 12:12:12", - " ", - " ", - " ", - " ")) -} - func TestCastJSONTimeDuration(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) @@ -4848,14 +2539,3 @@ func TestRegexpPushdown(t *testing.T) { " └─TableReader_7 10000.00 root data:TableFullScan_6", " └─TableFullScan_6 10000.00 cop[tikv] table:regbin keep order:false, stats:pseudo")) } - -func TestIfNullParamMarker(t *testing.T) { - store := testkit.CreateMockStore(t) - tk := testkit.NewTestKit(t, store) - tk.MustExec("use test") - tk.MustExec("create table t (c1 varchar(100), c2 varchar(128));") - tk.MustExec(`prepare pr1 from "insert into t values(ifnull(?,' '),ifnull(?,' '))";`) - tk.MustExec(`set @a='1',@b=repeat('x', 80);`) - // Should not report 'Data too long for column' error. - tk.MustExec(`execute pr1 using @a,@b;`) -} diff --git a/tests/integrationtest/r/expression/builtin.result b/tests/integrationtest/r/expression/builtin.result new file mode 100644 index 0000000000000..a69697cc4d9c3 Binary files /dev/null and b/tests/integrationtest/r/expression/builtin.result differ diff --git a/tests/integrationtest/r/expression/cast.result b/tests/integrationtest/r/expression/cast.result new file mode 100644 index 0000000000000..1f82bffc5e685 --- /dev/null +++ b/tests/integrationtest/r/expression/cast.result @@ -0,0 +1,80 @@ +select cast('' as signed); +cast('' as signed) +0 +show warnings; +Level Code Message +select cast('12345abcde' as signed); +cast('12345abcde' as signed) +12345 +show warnings; +Level Code Message +select cast('123e456' as signed); +cast('123e456' as signed) +123 +show warnings; +Level Code Message +select cast('-12345abcde' as signed); +cast('-12345abcde' as signed) +-12345 +show warnings; +Level Code Message +select cast('-123e456' as signed); +cast('-123e456' as signed) +-123 +show warnings; +Level Code Message +select coercibility(binary('a')); +coercibility(binary('a')) +2 +select coercibility(cast('a' as char(10))); +coercibility(cast('a' as char(10))) +2 +select coercibility(convert('abc', char(10))); +coercibility(convert('abc', char(10))) +2 +drop table if exists t; +create table t(d1 double, f float, d2 decimal(24,8)); +insert into t values(0, 0, 0); +select cast(111.1 as datetime) from t; +cast(111.1 as datetime) +2000-01-11 00:00:00 +select cast(1311.1 as datetime) from t; +cast(1311.1 as datetime) +NULL +insert into t values(111.1, 1122.1, 31212.111); +insert into t values(121212.1111, 1121212.111111, 11121212.111111); +insert into t values(99991111.1111111, 101.1111111, 20121212121212.1111111); +insert into t values(NULL, NULL, NULL); +insert into t values(1.1, 48.1, 100.1); +insert into t values(1301.11, 1131.111, 100001111.111); +insert into t values(20121212121260.1111111, 20121212126012.1111111, 20121212241212.1111111); +select cast(d1 as datetime), cast(f as datetime), cast(d2 as datetime) from t; +cast(d1 as datetime) cast(f as datetime) cast(d2 as datetime) +NULL NULL NULL +NULL NULL NULL +NULL NULL NULL +NULL NULL NULL +0000-00-00 00:00:00 0000-00-00 00:00:00 0000-00-00 00:00:00 +2000-01-11 00:00:00 2000-11-22 00:00:00 2003-12-12 00:00:00 +2012-12-12 00:00:00 0112-12-12 00:00:00 1112-12-12 00:00:00 +9999-11-11 00:00:00 2000-01-01 00:00:00 2012-12-12 12:12:12 +drop table if exists t; +create table t (col1 bigint, col2 double, col3 decimal, col4 varchar(20), col5 json); +insert into t values (1, 1, 1, "1", "1"); +insert into t values (null, null, null, null, null); +select cast(col1 as time), cast(col2 as time), cast(col3 as time), cast(col4 as time), cast(col5 as time) from t where col1 = 1; +cast(col1 as time) cast(col2 as time) cast(col3 as time) cast(col4 as time) cast(col5 as time) +00:00:01 00:00:01 00:00:01 00:00:01 NULL +select cast(col1 as time), cast(col2 as time), cast(col3 as time), cast(col4 as time), cast(col5 as time) from t where col1 is null; +cast(col1 as time) cast(col2 as time) cast(col3 as time) cast(col4 as time) cast(col5 as time) +NULL NULL NULL NULL NULL +select cast(col1 as time(31)) from t where col1 is null; +Error 1426: Too big precision 31 specified for column 'CAST'. Maximum is 6. +select cast(col2 as time(31)) from t where col1 is null; +Error 1426: Too big precision 31 specified for column 'CAST'. Maximum is 6. +select cast(col3 as time(31)) from t where col1 is null; +Error 1426: Too big precision 31 specified for column 'CAST'. Maximum is 6. +select cast(col4 as time(31)) from t where col1 is null; +Error 1426: Too big precision 31 specified for column 'CAST'. Maximum is 6. +select cast(col5 as time(31)) from t where col1 is null; +Error 1426: Too big precision 31 specified for column 'CAST'. Maximum is 6. diff --git a/tests/integrationtest/r/expression/issues.result b/tests/integrationtest/r/expression/issues.result index 57378ea95bb11..d7a56e64c558d 100644 --- a/tests/integrationtest/r/expression/issues.result +++ b/tests/integrationtest/r/expression/issues.result @@ -2628,3 +2628,34 @@ NULL 0 0 0 0 0 1 1 +drop table if exists author_addresses, authors; +CREATE TABLE author_addresses ( +id bigint(20) NOT NULL AUTO_INCREMENT, +PRIMARY KEY (id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +CREATE TABLE authors ( +id bigint(20) NOT NULL AUTO_INCREMENT, +name varchar(255) NOT NULL, +author_address_id bigint(20) DEFAULT NULL, +author_address_extra_id bigint(20) DEFAULT NULL, +organization_id varchar(255) DEFAULT NULL, +owned_essay_id varchar(255) DEFAULT NULL, +PRIMARY KEY (id), +KEY index_authors_on_author_address_id (author_address_id), +KEY index_authors_on_author_address_extra_id (author_address_extra_id), +CONSTRAINT fk_rails_94423a17a3 FOREIGN KEY (author_address_id) REFERENCES author_addresses (id) ON UPDATE CASCADE ON DELETE RESTRICT +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +SELECT fk.referenced_table_name AS 'to_table', +fk.referenced_column_name AS 'primary_key', +fk.column_name AS 'column', +fk.constraint_name AS 'name', +rc.update_rule AS 'on_update', +rc.delete_rule AS 'on_delete' +FROM information_schema.referential_constraints rc +JOIN information_schema.key_column_usage fk +USING (constraint_schema, constraint_name) +WHERE fk.referenced_column_name IS NOT NULL +AND fk.table_schema = database() +AND fk.table_name = 'authors'; +to_table primary_key column name on_update on_delete +author_addresses id author_address_id fk_rails_94423a17a3 CASCADE RESTRICT diff --git a/tests/integrationtest/r/expression/misc.result b/tests/integrationtest/r/expression/misc.result new file mode 100644 index 0000000000000..b6d047019e241 --- /dev/null +++ b/tests/integrationtest/r/expression/misc.result @@ -0,0 +1,316 @@ +SELECT LENGTH(b''), LENGTH(B''), b''+1, b''-1, B''+1; +LENGTH(b'') LENGTH(B'') b''+1 b''-1 B''+1 +0 0 1 -1 1 +select timestamp '2017-01-01 00:00:00'; +timestamp '2017-01-01 00:00:00' +2017-01-01 00:00:00 +select timestamp '2017@01@01 00:00:00'; +timestamp '2017@01@01 00:00:00' +2017-01-01 00:00:00 +select timestamp '2017@01@01 00~00~00'; +timestamp '2017@01@01 00~00~00' +2017-01-01 00:00:00 +select timestamp '2017@01@0001 00~00~00.333'; +timestamp '2017@01@0001 00~00~00.333' +2017-01-01 00:00:00.333 +select timestamp '00:00:00'; +Error 1292: Incorrect datetime value: '00:00:00' +select timestamp '1992-01-03'; +Error 1292: Incorrect datetime value: '1992-01-03' +select timestamp '20171231235959.999999'; +Error 1292: Incorrect datetime value: '20171231235959.999999' +select time '117:01:12'; +time '117:01:12' +117:01:12 +select time '01:00:00.999999'; +time '01:00:00.999999' +01:00:00.999999 +select time '1 01:00:00'; +time '1 01:00:00' +25:00:00 +select time '110:00:00'; +time '110:00:00' +110:00:00 +select time'-1:1:1.123454656'; +time'-1:1:1.123454656' +-01:01:01.123455 +select time '33:33'; +time '33:33' +33:33:00 +select time '1.1'; +time '1.1' +00:00:01.1 +select time '21'; +time '21' +00:00:21 +select time '20 20:20'; +time '20 20:20' +500:20:00 +select time '2017-01-01 00:00:00'; +Error 1292: Incorrect time value: '2017-01-01 00:00:00' +select time '071231235959.999999'; +Error 1292: Incorrect time value: '071231235959.999999' +select time '20171231235959.999999'; +Error 1292: Incorrect time value: '20171231235959.999999' +select ADDDATE('2008-01-34', -1); +ADDDATE('2008-01-34', -1) +NULL +Show warnings; +Level Code Message +set sql_mode=''; +drop table if exists t; +create table t1(a decimal(10,5), b decimal(10,1)); +insert into t1 values(123.12345, 123.12345); +update t1 set b = a; +select a, b from t1; +a b +123.12345 123.1 +select 2.00000000000000000000000000000001 * 1.000000000000000000000000000000000000000000002; +2.00000000000000000000000000000001 * 1.000000000000000000000000000000000000000000002 +2.000000000000000000000000000000 +set sql_mode=default; +drop table if exists t; +create table t(a decimal(38, 17)); +insert into t select 0.5999991229316*0.918755041726043; +select * from t; +a +0.55125221922461136 +select cast(1 as decimal(60,30)) / cast(1 as decimal(60,30)) / cast(1 as decimal(60, 30)); +cast(1 as decimal(60,30)) / cast(1 as decimal(60,30)) / cast(1 as decimal(60, 30)) +1.000000000000000000000000000000 +select cast(1 as decimal(60,30)) / cast(3 as decimal(60,30)) / cast(7 as decimal(60, 30)); +cast(1 as decimal(60,30)) / cast(3 as decimal(60,30)) / cast(7 as decimal(60, 30)) +0.047619047619047619047619047619 +select cast(1 as decimal(60,30)) / cast(3 as decimal(60,30)) / cast(7 as decimal(60, 30)) / cast(13 as decimal(60, 30)); +cast(1 as decimal(60,30)) / cast(3 as decimal(60,30)) / cast(7 as decimal(60, 30)) / cast(13 as decimal(60, 30)) +0.003663003663003663003663003663 +drop table if exists t; +create table t (i int key, j float); +insert into t values (1, 0.01); +select * from t; +i j +1 0.01 +insert into t values (1, 0.02) on duplicate key update j = values (j); +select * from t; +i j +1 0.02 +drop table if exists t; +create table t (a bigint primary key, b enum('a','b','c')); +insert into t values (1, "a"); +select * from t; +a b +1 a +insert into t values (1, "b") on duplicate key update b = values(b); +select * from t; +a b +1 b +drop table if exists t; +drop table if exists s; +create table t(id int primary key, a int); +create table s(a int); +insert into s values(1),(2); +select t.id = 1.234 from t right join s on t.a = s.a; +t.id = 1.234 +NULL +NULL +SHOW VARIABLES WHERE Variable_name ='language' OR Variable_name = 'net_write_timeout' OR Variable_name = 'interactive_timeout' +OR Variable_name = 'wait_timeout' OR Variable_name = 'character_set_client' OR Variable_name = 'character_set_connection' +OR Variable_name = 'character_set' OR Variable_name = 'character_set_server' OR Variable_name = 'tx_isolation' +OR Variable_name = 'transaction_isolation' OR Variable_name = 'character_set_results' OR Variable_name = 'timezone' +OR Variable_name = 'time_zone' OR Variable_name = 'system_time_zone' +OR Variable_name = 'lower_case_table_names' OR Variable_name = 'max_allowed_packet' OR Variable_name = 'net_buffer_length' +OR Variable_name = 'sql_mode' OR Variable_name = 'query_cache_type' OR Variable_name = 'query_cache_size' +OR Variable_name = 'license' OR Variable_name = 'init_connect'; +Variable_name Value +character_set_client utf8mb4 +character_set_connection utf8mb4 +character_set_results utf8mb4 +character_set_server utf8mb4 +init_connect +interactive_timeout 28800 +license Apache License 2.0 +lower_case_table_names 2 +max_allowed_packet 67108864 +net_buffer_length 16384 +net_write_timeout 60 +query_cache_size 1048576 +query_cache_type OFF +sql_mode ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION +system_time_zone Asia/Shanghai +time_zone Asia/Shanghai +transaction_isolation REPEATABLE-READ +tx_isolation REPEATABLE-READ +wait_timeout 28800 +select '1e800' + 1e100; +'1e800' + 1e100 +1.7976931348623157e308 +select '-1e800' - 1e100; +'-1e800' - 1e100 +-1.7976931348623157e308 +drop table if exists identity; +create table identity (id int not null primary key auto_increment); +SELECT @@identity; +@@identity +0 +INSERT INTO identity VALUES (NULL); +SELECT @@identity, LAST_INSERT_ID(); +@@identity LAST_INSERT_ID() +1 1 +INSERT INTO identity VALUES (NULL); +SELECT @@identity, LAST_INSERT_ID(); +@@identity LAST_INSERT_ID() +2 2 +INSERT INTO identity VALUES (NULL); +SELECT @@identity, LAST_INSERT_ID(); +@@identity LAST_INSERT_ID() +3 3 +drop table if exists lastinsertid; +create table lastinsertid (id int not null primary key auto_increment); +SELECT @@last_insert_id; +@@last_insert_id +3 +INSERT INTO lastinsertid VALUES (NULL); +SELECT @@last_insert_id, LAST_INSERT_ID(); +@@last_insert_id LAST_INSERT_ID() +1 1 +INSERT INTO lastinsertid VALUES (NULL); +SELECT @@last_insert_id, LAST_INSERT_ID(); +@@last_insert_id LAST_INSERT_ID() +2 2 +INSERT INTO lastinsertid VALUES (NULL); +SELECT @@last_insert_id, LAST_INSERT_ID(); +@@last_insert_id LAST_INSERT_ID() +3 3 +drop table if exists t; +create table t (a int, b int as (a + 1)); +insert into t(a) values (1); +select /*+ LIMIT_TO_COP() */ b from t limit 1; +b +2 +select /*+ LIMIT_TO_COP() */ b from t order by b limit 1; +b +2 +drop table if exists t0, t1; +CREATE TABLE t0(c0 float); +CREATE TABLE t1(c0 float); +INSERT INTO t1(c0) VALUES (0); +INSERT INTO t0(c0) VALUES (0); +SELECT t1.c0 FROM t1, t0 WHERE t0.c0=-t1.c0; +c0 +0 +drop table if exists kankan1, kankan2; +create table kankan1(id int, name text); +insert into kankan1 values(1, 'a'); +insert into kankan1 values(2, 'a'); +create table kankan2(id int, h1 text); +insert into kankan2 values(2, 'z'); +select t1.id from kankan1 t1 left join kankan2 t2 on t1.id = t2.id where (case when t1.name='b' then 'case2' when t1.name='a' then 'case1' else NULL end) = 'case1' order by t1.id; +id +1 +2 +drop table if exists t, t1; +create table t (a bit(64)); +create table t1 (a varchar(2)); +insert t1 value ('10'); +insert t select a from t1; +select a+0 from t; +a+0 +12592 +drop table if exists t, t1; +create table t (a bit(64)); +create table t1 (a binary(2)); +insert t1 value ('10'); +insert t select a from t1; +select a+0 from t; +a+0 +12592 +drop table if exists t, t1; +create table t (a bit(64)); +create table t1 (a datetime); +insert t1 value ('09-01-01'); +insert t select a from t1; +select a+0 from t; +a+0 +20090101000000 +drop table if exists t; +create table t(a int); +select /*+ unknown_hint(c1)*/ 1; +1 +1 +show warnings; +Level Code Message +select 1 from /*+ test1() */ t; +1 +show warnings; +Level Code Message +drop table if exists t; +create table t(a bigint, b double, c decimal, d varchar(20), e datetime, f time, g json); +insert into t values(1, 1.1, 2.2, "abc", "2018-10-24", NOW(), "12"); +select values(a), values(b), values(c), values(d), values(e), values(f), values(g) from t; +values(a) values(b) values(c) values(d) values(e) values(f) values(g) +NULL NULL NULL NULL NULL NULL NULL +set global innodb_default_row_format = dynamic; +set global innodb_default_row_format = 'dynamic'; +SHOW VARIABLES LIKE 'innodb_default_row_format'; +Variable_name Value +innodb_default_row_format dynamic +SHOW VARIABLES LIKE 'character_set_server'; +Variable_name Value +character_set_server utf8mb4 +SHOW VARIABLES LIKE 'innodb_file_format'; +Variable_name Value +innodb_file_format Barracuda +SHOW VARIABLES LIKE 'innodb_large_prefix'; +Variable_name Value +innodb_large_prefix ON +drop table if exists t; +create table t (c1 varchar(100), c2 varchar(128)); +prepare pr1 from "insert into t values(ifnull(?,' '),ifnull(?,' '))"; +set @a='1',@b=repeat('x', 80); +execute pr1 using @a,@b; + +drop table if exists t; +create table t(a int, b int, index(a)); +insert into t values (null, 0), (null, 1), (10, 11), (10, 12); +select * from t use index(a) where a is null order by b; +a b +NULL 0 +NULL 1 +select * from t use index(a) where a<=>null order by b; +a b +NULL 0 +NULL 1 +select * from t use index(a) where a<=>10 order by b; +a b +10 11 +10 12 +drop table if exists t1; +create table t1(a int, b int, c int, unique key(a, b, c)); +insert into t1 values (1, null, 1), (1, null, 2), (1, null, 3), (1, null, 4); +insert into t1 values (1, 1, 1), (1, 2, 2), (1, 3, 33), (1, 4, 44); +select c from t1 where a=1 and b<=>null and c>2 order by c; +c +3 +4 +select c from t1 where a=1 and b is null and c>2 order by c; +c +3 +4 +select c from t1 where a=1 and b is not null and c>2 order by c; +c +33 +44 +DROP TABLE IF EXISTS t1; +CREATE TABLE t1 (d date NOT NULL) PARTITION BY RANGE (YEAR(d)) +(PARTITION p2016 VALUES LESS THAN (2017), PARTITION p2017 VALUES LESS THAN (2018), PARTITION p2018 VALUES LESS THAN (2019), +PARTITION p2019 VALUES LESS THAN (2020), PARTITION pmax VALUES LESS THAN MAXVALUE); +INSERT INTO t1 VALUES ('2016-01-01'), ('2016-06-01'), ('2016-09-01'), ('2017-01-01'), +('2017-06-01'), ('2017-09-01'), ('2018-01-01'), ('2018-06-01'), ('2018-09-01'), ('2018-10-01'), +('2018-11-01'), ('2018-12-01'), ('2018-12-31'), ('2019-01-01'), ('2019-06-01'), ('2019-09-01'), +('2020-01-01'), ('2020-06-01'), ('2020-09-01'); +SELECT COUNT(*) FROM t1 WHERE d < '2018-01-01'; +COUNT(*) +6 +SELECT COUNT(*) FROM t1 WHERE d > '2018-01-01'; +COUNT(*) +12 diff --git a/tests/integrationtest/r/expression/plan_cache.result b/tests/integrationtest/r/expression/plan_cache.result new file mode 100644 index 0000000000000..3b6d1332c804a --- /dev/null +++ b/tests/integrationtest/r/expression/plan_cache.result @@ -0,0 +1,63 @@ +prepare pr1 from 'select ? in (1,?,?)'; +set @a=1, @b=2, @c=3; +execute pr1 using @a,@b,@c; +? in (1,?,?) +1 +prepare pr2 from 'select 3 in (1,?,?)'; +set @a=2, @b=3; +execute pr2 using @a,@b; +3 in (1,?,?) +1 +prepare pr3 from 'select ? in (1,2,3)'; +set @a=4; +execute pr3 using @a; +? in (1,2,3) +0 +prepare pr4 from 'select ? in (?,?,?)'; +set @a=1, @b=2, @c=3, @d=4; +execute pr4 using @a,@b,@c,@d; +? in (?,?,?) +0 +drop table if exists t; +create table t(a int); +prepare stmt from 'SELECT * FROM t order by rand()'; +execute stmt; +a +prepare stmt from 'SELECT * FROM t order by now()'; +execute stmt; +a +drop table if exists t; +create table t(a int); +insert into t values(1), (2), (3); +set @@session.sql_select_limit = 1; +prepare stmt from 'SELECT * FROM t'; +execute stmt; +a +1 +set @@session.sql_select_limit = default; +execute stmt; +a +1 +2 +3 +set @@session.sql_select_limit = 2; +execute stmt; +a +1 +2 +set @@session.sql_select_limit = 1; +execute stmt; +a +1 +set @@session.sql_select_limit = default; +execute stmt; +a +1 +2 +3 +set @@session.sql_select_limit = 2; +execute stmt; +a +1 +2 +set sql_select_limit=default; diff --git a/tests/integrationtest/r/expression/time.result b/tests/integrationtest/r/expression/time.result new file mode 100644 index 0000000000000..64e165babeb23 --- /dev/null +++ b/tests/integrationtest/r/expression/time.result @@ -0,0 +1,507 @@ +select dayname("1962-03-01")+0; +dayname("1962-03-01")+0 +3 +select dayname("1962-03-02")+0; +dayname("1962-03-02")+0 +4 +select dayname("1962-03-03")+0; +dayname("1962-03-03")+0 +5 +select dayname("1962-03-04")+0; +dayname("1962-03-04")+0 +6 +select dayname("1962-03-05")+0; +dayname("1962-03-05")+0 +0 +select dayname("1962-03-06")+0; +dayname("1962-03-06")+0 +1 +select dayname("1962-03-07")+0; +dayname("1962-03-07")+0 +2 +select dayname("1962-03-08")+0; +dayname("1962-03-08")+0 +3 +select dayname("1962-03-01")+1; +dayname("1962-03-01")+1 +4 +select dayname("1962-03-01")+2; +dayname("1962-03-01")+2 +5 +select dayname("1962-03-01")+3; +dayname("1962-03-01")+3 +6 +select dayname("1962-03-01")+4; +dayname("1962-03-01")+4 +7 +select dayname("1962-03-01")+5; +dayname("1962-03-01")+5 +8 +select dayname("1962-03-01")+6; +dayname("1962-03-01")+6 +9 +select dayname("1962-03-01")+7; +dayname("1962-03-01")+7 +10 +select dayname("1962-03-01")+2333; +dayname("1962-03-01")+2333 +2336 +select dayname("1962-03-01")+2.333; +dayname("1962-03-01")+2.333 +5.333 +select dayname("1962-03-01")>2; +dayname("1962-03-01")>2 +1 +select dayname("1962-03-01")<2; +dayname("1962-03-01")<2 +0 +select dayname("1962-03-01")=3; +dayname("1962-03-01")=3 +1 +select dayname("1962-03-01")!=3; +dayname("1962-03-01")!=3 +0 +select dayname("1962-03-01")<4; +dayname("1962-03-01")<4 +1 +select dayname("1962-03-01")>4; +dayname("1962-03-01")>4 +0 +select !dayname("1962-03-01"); +!dayname("1962-03-01") +0 +select dayname("1962-03-01")&1; +dayname("1962-03-01")&1 +1 +select dayname("1962-03-01")&3; +dayname("1962-03-01")&3 +3 +select dayname("1962-03-01")&7; +dayname("1962-03-01")&7 +3 +select dayname("1962-03-01")|1; +dayname("1962-03-01")|1 +3 +select dayname("1962-03-01")|3; +dayname("1962-03-01")|3 +3 +select dayname("1962-03-01")|7; +dayname("1962-03-01")|7 +7 +select dayname("1962-03-01")^1; +dayname("1962-03-01")^1 +2 +select dayname("1962-03-01")^3; +dayname("1962-03-01")^3 +0 +select dayname("1962-03-01")^7; +dayname("1962-03-01")^7 +4 +drop table if exists t; +create table t (a bigint primary key, b timestamp); +insert into t values (1, "2019-04-29 11:56:12"); +explain format = 'brief' select * from t where b = (select max(b) from t); +id estRows task access object operator info +TableReader 10.00 root data:Selection +└─Selection 10.00 cop[tikv] eq(expression__time.t.b, 2019-04-29 11:56:12) + └─TableFullScan 10000.00 cop[tikv] table:t keep order:false, stats:pseudo +select * from t where b = (select max(b) from t); +a b +1 2019-04-29 11:56:12 +SELECT "1900-01-01 00:00:00" + INTERVAL 1.123456789e3 SECOND; +"1900-01-01 00:00:00" + INTERVAL 1.123456789e3 SECOND +1900-01-01 00:18:43.456789 +SELECT 19000101000000 + INTERVAL 1.123456789e3 SECOND; +19000101000000 + INTERVAL 1.123456789e3 SECOND +1900-01-01 00:18:43.456789 +select date("1900-01-01") + interval 1.123456789e3 second; +date("1900-01-01") + interval 1.123456789e3 second +1900-01-01 00:18:43.456789 +SELECT "1900-01-01 00:18:43.456789" - INTERVAL 1.123456789e3 SECOND; +"1900-01-01 00:18:43.456789" - INTERVAL 1.123456789e3 SECOND +1900-01-01 00:00:00 +SELECT 19000101001843.456789 - INTERVAL 1.123456789e3 SECOND; +19000101001843.456789 - INTERVAL 1.123456789e3 SECOND +1900-01-01 00:00:00 +SELECT 19000101000000.0005 + INTERVAL 0.0005 SECOND; +19000101000000.0005 + INTERVAL 0.0005 SECOND +1900-01-01 00:00:00.001000 +select date("1900-01-01") - interval 1.123456789e3 second; +date("1900-01-01") - interval 1.123456789e3 second +1899-12-31 23:41:16.543211 +select 19000101000000 - interval 1.123456789e3 second; +19000101000000 - interval 1.123456789e3 second +1899-12-31 23:41:16.543211 +drop table if exists t; +create table t(a datetime(6), b timestamp); +insert t values (20010101100000.123456, 20110707101112.123456); +select * from t; +a b +2001-01-01 10:00:00.123456 2011-07-07 10:11:12 +set time_zone = 'CET'; +drop table if exists t; +create table t(ts timestamp); +set time_zone = 'UTC'; +insert into t values('2022-03-27 00:30:00'); +insert into t values('2022-10-30 00:30:00'); +insert into t values('2022-10-30 01:30:00'); +set time_zone = 'Europe/Amsterdam'; +insert into t values('2022-03-27 02:30:00'); +Error 1292: Incorrect timestamp value: '2022-03-27 02:30:00' for column 'ts' at row 1 +select date_add(ts, interval 1 hour) from t order by ts; +date_add(ts, interval 1 hour) +2022-03-27 02:30:00 +2022-10-30 03:30:00 +2022-10-30 03:30:00 +set time_zone = default; +drop table if exists t; +create table t(a date); +insert into t values ('2021-08-20'); +select timestampadd(microsecond, 1, a) from t; +timestampadd(microsecond, 1, a) +2021-08-20 00:00:00.000001 +select timestampadd(second, 6/4, a) from t; +timestampadd(second, 6/4, a) +2021-08-20 00:00:01.500000 +select timestampadd(second, 9.9999e2, a) from t; +timestampadd(second, 9.9999e2, a) +2021-08-20 00:16:39.990000 +select timestampadd(second, 1, '2021-08-20 00:00:01.0001'); +timestampadd(second, 1, '2021-08-20 00:00:01.0001') +2021-08-20 00:00:02.000100 +select timestampadd(minute, 1.5, '2021-08-20 00:00:00'); +timestampadd(minute, 1.5, '2021-08-20 00:00:00') +2021-08-20 00:02:00 +select timestampadd(minute, 1.5, '2021-08-20 00:00:00.0001'); +timestampadd(minute, 1.5, '2021-08-20 00:00:00.0001') +2021-08-20 00:02:00.000100 +SELECT timestampadd(year,1.212208e+308,'1995-01-05 06:32:20.859724') as result; +result +NULL +show warnings; +Level Code Message +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 SECOND_MICROSECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 SECOND_MICROSECOND) +2007-03-28 22:08:27.800000 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 MINUTE_MICROSECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 MINUTE_MICROSECOND) +2007-03-28 22:08:27.800000 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 HOUR_MICROSECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 HOUR_MICROSECOND) +2007-03-28 22:08:27.800000 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 DAY_MICROSECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 DAY_MICROSECOND) +2007-03-28 22:08:27.800000 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR_MINUTE); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR_MINUTE) +2007-03-29 00:10:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MINUTE_SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MINUTE_SECOND) +2007-03-28 22:10:30 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR_MONTH); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR_MONTH) +2009-05-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_HOUR); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_HOUR) +2007-03-31 00:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_MINUTE); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_MINUTE) +2007-03-29 00:10:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_SECOND) +2007-03-28 22:10:30 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR_SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR_SECOND) +2007-03-28 22:10:30 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 SECOND) +2007-03-28 22:08:30.200000 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR) +2009-03-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 QUARTER); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 QUARTER) +2007-09-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MONTH); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MONTH) +2007-05-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 WEEK); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 WEEK) +2007-04-11 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY) +2007-03-30 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR) +2007-03-29 00:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MINUTE); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MINUTE) +2007-03-28 22:10:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MICROSECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MICROSECOND) +2007-03-28 22:08:28.000002 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_MINUTE); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_MINUTE) +2007-03-28 20:06:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE_SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE_SECOND) +2007-03-28 22:06:26 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 YEAR_MONTH); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 YEAR_MONTH) +2005-01-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_HOUR); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_HOUR) +2007-03-26 20:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_MINUTE); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_MINUTE) +2007-03-28 20:06:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_SECOND) +2007-03-28 22:06:26 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_SECOND) +2007-03-28 22:06:26 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 YEAR); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 YEAR) +2005-03-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 QUARTER); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 QUARTER) +2006-09-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MONTH); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MONTH) +2007-01-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 WEEK); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 WEEK) +2007-03-14 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY) +2007-03-26 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR) +2007-03-28 20:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE) +2007-03-28 22:06:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MICROSECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MICROSECOND) +2007-03-28 22:08:27.999998 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR_MINUTE); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR_MINUTE) +2007-03-28 20:06:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MINUTE_SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MINUTE_SECOND) +2007-03-28 22:06:26 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" YEAR_MONTH); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" YEAR_MONTH) +2005-01-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_HOUR); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_HOUR) +2007-03-26 20:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_MINUTE); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_MINUTE) +2007-03-28 20:06:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_SECOND) +2007-03-28 22:06:26 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR_SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR_SECOND) +2007-03-28 22:06:26 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" SECOND) +2007-03-28 22:08:25.800000 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" YEAR); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" YEAR) +2005-03-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" QUARTER); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" QUARTER) +2006-09-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MONTH); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MONTH) +2007-01-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" WEEK); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" WEEK) +2007-03-14 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY) +2007-03-26 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR) +2007-03-28 20:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MINUTE); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MINUTE) +2007-03-28 22:06:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MICROSECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MICROSECOND) +2007-03-28 22:08:27.999998 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR_MINUTE); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR_MINUTE) +2007-03-28 20:06:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MINUTE_SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MINUTE_SECOND) +2007-03-28 22:06:26 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" YEAR_MONTH); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" YEAR_MONTH) +2005-01-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_HOUR); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_HOUR) +2007-03-26 20:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_MINUTE); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_MINUTE) +2007-03-28 20:06:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_SECOND) +2007-03-28 22:06:26 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR_SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR_SECOND) +2007-03-28 22:06:26 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" SECOND) +2007-03-28 22:08:26 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.+2" SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.+2" SECOND) +2007-03-28 22:08:26 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.*2" SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.*2" SECOND) +2007-03-28 22:08:26 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2./2" SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2./2" SECOND) +2007-03-28 22:08:26 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.a2" SECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.a2" SECOND) +2007-03-28 22:08:26 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" YEAR); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" YEAR) +2005-03-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" QUARTER); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" QUARTER) +2006-09-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MONTH); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MONTH) +2007-01-28 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" WEEK); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" WEEK) +2007-03-14 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY) +2007-03-26 22:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR) +2007-03-28 20:08:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MINUTE); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MINUTE) +2007-03-28 22:06:28 +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MICROSECOND); +DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MICROSECOND) +2007-03-28 22:08:27.999998 +set @@session.time_zone = 'Europe/Amsterdam'; +drop table if exists t; +create table t (id int, dt datetime, primary key (id, dt)); +insert into t values (1, date_add('2023-03-26 00:00:00', interval 2 hour)); +insert into t values (4,'2023-03-26 02:00:00'); +select * from t; +id dt +1 2023-03-26 02:00:00 +4 2023-03-26 02:00:00 +set time_zone = default; +drop table if exists t1; +create table t1(c_time time(5), c_dt datetime(4), c_ts timestamp(3), c_d date, c_str varchar(100)); +insert into t1 values('-800:10:10', '2021-10-10 10:10:10.1234', '2021-10-10 10:10:10.1234', '2021-10-11', '2021-10-10 10:10:10.1234'); +set @@tidb_enable_vectorized_expression = off; +select greatest(c_time, c_time) from t1; +greatest(c_time, c_time) +-800:10:10.00000 +select greatest(c_dt, c_dt) from t1; +greatest(c_dt, c_dt) +2021-10-10 10:10:10.1234 +select greatest(c_ts, c_ts) from t1; +greatest(c_ts, c_ts) +2021-10-10 10:10:10.123 +select greatest(c_d, c_d) from t1; +greatest(c_d, c_d) +2021-10-11 +select greatest(c_str, c_str) from t1; +greatest(c_str, c_str) +2021-10-10 10:10:10.1234 +select least(c_time, c_time) from t1; +least(c_time, c_time) +-800:10:10.00000 +select least(c_dt, c_dt) from t1; +least(c_dt, c_dt) +2021-10-10 10:10:10.1234 +select least(c_ts, c_ts) from t1; +least(c_ts, c_ts) +2021-10-10 10:10:10.123 +select least(c_d, c_d) from t1; +least(c_d, c_d) +2021-10-11 +select least(c_str, c_str) from t1; +least(c_str, c_str) +2021-10-10 10:10:10.1234 +select greatest(c_time, cast('10:01:01' as time)) from t1; +greatest(c_time, cast('10:01:01' as time)) +10:01:01.00000 +select least(c_time, cast('10:01:01' as time)) from t1; +least(c_time, cast('10:01:01' as time)) +-800:10:10.00000 +select greatest(c_d, cast('1999-10-10' as date)) from t1; +greatest(c_d, cast('1999-10-10' as date)) +2021-10-11 +select least(c_d, cast('1999-10-10' as date)) from t1; +least(c_d, cast('1999-10-10' as date)) +1999-10-10 +select greatest(c_dt, cast('1999-10-10 10:10:10.1234' as datetime)) from t1; +greatest(c_dt, cast('1999-10-10 10:10:10.1234' as datetime)) +2021-10-10 10:10:10.1234 +select least(c_dt, cast('1999-10-10 10:10:10.1234' as datetime)) from t1; +least(c_dt, cast('1999-10-10 10:10:10.1234' as datetime)) +1999-10-10 10:10:10 +set @@tidb_enable_vectorized_expression = on; +select greatest(c_time, c_time) from t1; +greatest(c_time, c_time) +-800:10:10.00000 +select greatest(c_dt, c_dt) from t1; +greatest(c_dt, c_dt) +2021-10-10 10:10:10.1234 +select greatest(c_ts, c_ts) from t1; +greatest(c_ts, c_ts) +2021-10-10 10:10:10.123 +select greatest(c_d, c_d) from t1; +greatest(c_d, c_d) +2021-10-11 +select greatest(c_str, c_str) from t1; +greatest(c_str, c_str) +2021-10-10 10:10:10.1234 +select least(c_time, c_time) from t1; +least(c_time, c_time) +-800:10:10.00000 +select least(c_dt, c_dt) from t1; +least(c_dt, c_dt) +2021-10-10 10:10:10.1234 +select least(c_ts, c_ts) from t1; +least(c_ts, c_ts) +2021-10-10 10:10:10.123 +select least(c_d, c_d) from t1; +least(c_d, c_d) +2021-10-11 +select least(c_str, c_str) from t1; +least(c_str, c_str) +2021-10-10 10:10:10.1234 +select greatest(c_time, cast('10:01:01' as time)) from t1; +greatest(c_time, cast('10:01:01' as time)) +10:01:01.00000 +select least(c_time, cast('10:01:01' as time)) from t1; +least(c_time, cast('10:01:01' as time)) +-800:10:10.00000 +select greatest(c_d, cast('1999-10-10' as date)) from t1; +greatest(c_d, cast('1999-10-10' as date)) +2021-10-11 +select least(c_d, cast('1999-10-10' as date)) from t1; +least(c_d, cast('1999-10-10' as date)) +1999-10-10 +select greatest(c_dt, cast('1999-10-10 10:10:10.1234' as datetime)) from t1; +greatest(c_dt, cast('1999-10-10 10:10:10.1234' as datetime)) +2021-10-10 10:10:10.1234 +select least(c_dt, cast('1999-10-10 10:10:10.1234' as datetime)) from t1; +least(c_dt, cast('1999-10-10 10:10:10.1234' as datetime)) +1999-10-10 10:10:10 +set @@tidb_enable_vectorized_expression = default; diff --git a/tests/integrationtest/t/expression/builtin.test b/tests/integrationtest/t/expression/builtin.test new file mode 100644 index 0000000000000..e5edd41138d4d --- /dev/null +++ b/tests/integrationtest/t/expression/builtin.test @@ -0,0 +1,1468 @@ +# TestWeightString +drop table if exists t; +create table t (id int, a varchar(20) collate utf8mb4_general_ci); +insert into t values (0, 'aAÁàãăâ'),(1, 'a'),(2, 'a '),(3, '中'),(4, '中 '); +select weight_string(a) from t order by id; +select weight_string(a as char(1)) from t order by id; +select weight_string(a as char(3)) from t order by id; +select weight_string(a as binary(1)) from t order by id; +select weight_string(a as binary(5)) from t order by id; +select weight_string(NULL); +select weight_string(7); +select weight_string(cast(7 as decimal(5))); +select weight_string(cast(20190821 as date)); +select weight_string(cast(20190821 as date) as binary(5)); +select weight_string(7.0); +select weight_string(7 AS BINARY(2)); +select weight_string('中 ' collate utf8mb4_general_ci); +select weight_string('中 ' collate utf8mb4_bin); +select weight_string('中 ' collate utf8mb4_unicode_ci); +select collation(a collate utf8mb4_general_ci) from t order by id; +select collation('中 ' collate utf8mb4_general_ci); +select weight_string(a collate utf8mb4_bin) from t order by id; +-- error 1253 +-- error 1253 +select weight_string(a collate utf8_general_ci) from t order by id; +-- error 1253 +-- error 1253 +select weight_string('中' collate utf8_bin); + +# TestMathBuiltin +select degrees(0), degrees(1); +select degrees(2), degrees(5); +select sin(0), sin(1.5707963267949); +select sin(1), sin(100); +select sin('abcd'); +select cos(0), cos(3.1415926535898); +select cos('abcd'); +select tan(0.00), tan(PI()/4); +select tan('abcd'); +select log2(0.0); +select log2(4); +select log2('8.0abcd'); +select log2(-1); +select log2(NULL); +select log10(0.0); +select log10(100); +select log10('1000.0abcd'); +select log10(-1); +select log10(NULL); +select log(0.0); +select log(100); +select log('100.0abcd'); +select log(-1); +select log(NULL); +select log(NULL, NULL); +select log(1, 100); +select log(0.5, 0.25); +select log(-1, 0.25); +select atan(0), atan(-1), atan(1), atan(1,2); +select atan('tidb'); +select asin(0), asin(-2), asin(2), asin(1); +select asin('tidb'); +select acos(0), acos(-2), acos(2), acos(1); +select acos('tidb'); +select pi(); +select floor(0), floor(null), floor(1.23), floor(-1.23), floor(1); +select floor('tidb'), floor('1tidb'), floor('tidb1'); +SELECT floor(t.c_datetime) FROM (select CAST('2017-07-19 00:00:00' AS DATETIME) AS c_datetime) AS t; +SELECT floor(t.c_time) FROM (select CAST('12:34:56' AS TIME) AS c_time) AS t; +SELECT floor(t.c_time) FROM (select CAST('00:34:00' AS TIME) AS c_time) AS t; +SELECT floor(t.c_time) FROM (select CAST('00:00:00' AS TIME) AS c_time) AS t; +SELECT floor(t.c_decimal) FROM (SELECT CAST('-10.01' AS DECIMAL(10,2)) AS c_decimal) AS t; +SELECT floor(t.c_decimal) FROM (SELECT CAST('-10.01' AS DECIMAL(10,1)) AS c_decimal) AS t; +select ceil(0), ceil(null), ceil(1.23), ceil(-1.23), ceil(1); +select ceiling(0), ceiling(null), ceiling(1.23), ceiling(-1.23), ceiling(1); +select ceil('tidb'), ceil('1tidb'), ceil('tidb1'), ceiling('tidb'), ceiling('1tidb'), ceiling('tidb1'); +select ceil(t.c_datetime), ceiling(t.c_datetime) from (select cast('2017-07-20 00:00:00' as datetime) as c_datetime) as t; +select ceil(t.c_time), ceiling(t.c_time) from (select cast('12:34:56' as time) as c_time) as t; +select ceil(t.c_time), ceiling(t.c_time) from (select cast('00:34:00' as time) as c_time) as t; +select ceil(t.c_time), ceiling(t.c_time) from (select cast('00:00:00' as time) as c_time) as t; +select ceil(t.c_decimal), ceiling(t.c_decimal) from (select cast('-10.01' as decimal(10,2)) as c_decimal) as t; +select ceil(t.c_decimal), ceiling(t.c_decimal) from (select cast('-10.01' as decimal(10,1)) as c_decimal) as t; +select floor(18446744073709551615), ceil(18446744073709551615); +select floor(18446744073709551615.1233), ceil(18446744073709551615.1233); +select floor(-18446744073709551617), ceil(-18446744073709551617), floor(-18446744073709551617.11), ceil(-18446744073709551617.11); +drop table if exists t; +create table t(a decimal(40,20) UNSIGNED); +insert into t values(2.99999999900000000000), (12), (0); +select a, ceil(a) from t where ceil(a) > 1; +select a, ceil(a) from t; +select ceil(-29464); +select a, floor(a) from t where floor(a) > 1; +select a, floor(a) from t; +select floor(-29464); +drop table if exists t; +create table t(a decimal(40,20), b bigint); +insert into t values(-2.99999990000000000000, -1); +select floor(a), floor(a), floor(a) from t; +select b, floor(b) from t; +select cot(1), cot(-1), cot(NULL); +select cot('1tidb'); +-- error 1690 +select cot(0); +select exp(0), exp(1), exp(-1), exp(1.2), exp(NULL); +select exp('tidb'), exp('1tidb'); +-- error 1690 +select exp(1000000); +drop table if exists t; +create table t(a float); +insert into t values(1000000); +-- error 1690 +select exp(a) from t; +SELECT CONV('a', 16, 2); +SELECT CONV('6E', 18, 8); +SELECT CONV(-17, 10, -18); +SELECT CONV(10+'10'+'10'+X'0a', 10, 10); +SELECT CONV('a', 1, 10); +SELECT CONV('a', 37, 10); +SELECT CONV(0x0020, 2, 2); +SELECT CONV(0b10, 16, 2); +SELECT CONV(0b10, 16, 8); +drop table if exists bit; +create table bit(b bit(10)); +INSERT INTO bit (b) VALUES + (0b0000010101), + (0b0000010101), + (NULL), + (0b0000000001), + (0b0000000000), + (0b1111111111), + (0b1111111111), + (0b1111111111), + (0b0000000000), + (0b0000000000), + (0b0000000000), + (0b0000000000), + (0b0000100000); +select conv(b, 2, 2) from `bit`; +SELECT ABS(-1); +SELECT ABS('abc'); +SELECT ABS(18446744073709551615); +SELECT ABS(123.4); +SELECT ABS(-123.4); +SELECT ABS(1234E-1); +SELECT ABS(-9223372036854775807); +SELECT ABS(NULL); +-- error 1690 +SELECT ABS(-9223372036854775808); +SELECT ROUND(2.5), ROUND(-2.5), ROUND(25E-1); +SELECT ROUND(2.5, NULL), ROUND(NULL, 4), ROUND(NULL, NULL), ROUND(NULL); +SELECT ROUND('123.4'), ROUND('123e-2'); +SELECT ROUND(-9223372036854775808); +SELECT ROUND(123.456, 0), ROUND(123.456, 1), ROUND(123.456, 2), ROUND(123.456, 3), ROUND(123.456, 4), ROUND(123.456, -1), ROUND(123.456, -2), ROUND(123.456, -3), ROUND(123.456, -4); +SELECT ROUND(123456E-3, 0), ROUND(123456E-3, 1), ROUND(123456E-3, 2), ROUND(123456E-3, 3), ROUND(123456E-3, 4), ROUND(123456E-3, -1), ROUND(123456E-3, -2), ROUND(123456E-3, -3), ROUND(123456E-3, -4); +SELECT ROUND(1e14, 1), ROUND(1e15, 1), ROUND(1e308, 1); +SELECT ROUND(1e-14, 1), ROUND(1e-15, 1), ROUND(1e-308, 1); +SELECT truncate(123, -2), truncate(123, 2), truncate(123, 1), truncate(123, -1); +SELECT truncate(123.456, -2), truncate(123.456, 2), truncate(123.456, 1), truncate(123.456, 3), truncate(1.23, 100), truncate(123456E-3, 2); +SELECT truncate(9223372036854775807, -7), truncate(9223372036854775808, -10), truncate(cast(-1 as unsigned), -10); +select truncate(42, -9223372036854775808); +select truncate(42, 9223372036854775808); +select truncate(42, -2147483648); +select truncate(42, 2147483648); +select truncate(42, 18446744073709551615); +select truncate(42, 4294967295); +select truncate(42, -0); +select truncate(42, -307); +select truncate(42, -308); +select truncate(42, -309); +drop table if exists t; +create table t (a bigint unsigned); +insert into t values (18446744073709551615), (4294967295), (9223372036854775808), (2147483648); +select truncate(42, a) from t; +drop table if exists t; +create table t(a date, b datetime, c timestamp, d varchar(20)); +insert into t select "1234-12-29", "1234-12-29 16:24:13.9912", "2014-12-29 16:19:28", "12.34567"; +select truncate(a, -1), truncate(a, 1), truncate(a, -2), truncate(a, 2) from t; +select truncate(b, -1), truncate(b, 1), truncate(b, -2), truncate(b, 2) from t; +select truncate(c, -1), truncate(c, 1), truncate(c, -2), truncate(c, 2) from t; +select truncate(d, -1), truncate(d, 1), truncate(d, -2), truncate(d, 2) from t; +select truncate(json_array(), 1), truncate("cascasc", 1); +SELECT POW('12', 2), POW(1.2e1, '2.0'), POW(12, 2.0); +SELECT POW(null, 2), POW(2, null), POW(null, null); +SELECT POW(0, 0); +SELECT POW(0, 0.1), POW(0, 0.5), POW(0, 1); +-- error 1690 +SELECT POW(0, -1); +SELECT SIGN('12'), SIGN(1.2e1), SIGN(12), SIGN(0.0000012); +SELECT SIGN('-12'), SIGN(-1.2e1), SIGN(-12), SIGN(-0.0000012); +SELECT SIGN('0'), SIGN('-0'), SIGN(0); +SELECT SIGN(NULL); +SELECT SIGN(-9223372036854775808), SIGN(9223372036854775808); +SELECT SQRT(-10), SQRT(144), SQRT(4.84), SQRT(0.04), SQRT(0); +SELECT crc32(0), crc32(-0), crc32('0'), crc32('abc'), crc32('ABC'), crc32(NULL), crc32(''), crc32('hello world!'); +SELECT radians(1.0), radians(pi()), radians(pi()/2), radians(180), radians(1.009); +drop table if exists t; +create table t(a int); +insert into t values(1),(2),(3); +-- sorted_result +select rand(1) from t; +select rand(a) from t; +select rand(1), rand(2), rand(3); +set @@rand_seed1=10000000,@@rand_seed2=1000000; +select rand(); +select rand(1); +select rand(); + +# TestBuiltin +drop table if exists t; +create table t (a int, b int, index idx_b (b)); +insert t values (1, 1); +insert t values (2, 2); +insert t values (3, 2); +select * from t where b is true; +select all + a from t where a = 1; +select * from t where a is false; +select * from t where a is not true; +select 1 is true, 0 is true, null is true, "aaa" is true, "" is true, -12.00 is true, 0.0 is true, 0.0000001 is true; +select 1 is false, 0 is false, null is false, "aaa" is false, "" is false, -12.00 is false, 0.0 is false, 0.0000001 is false; +select 1 from dual where sec_to_time(2/10) is true; +select 1 from dual where sec_to_time(2/10) is false; +select 1 from dual where timediff((7/'2014-07-07 02:30:02'),'2012-01-16') is true; +select 1 from dual where timediff((7/'2014-07-07 02:30:02'),'2012-01-16') is false; +select 1 from dual where time(0.0001) is true; +select 1 from dual where time(0.0001) is false; +select * from t where b in (a); +select * from t where b not in (a); +select cast(1 as decimal(3,2)); +select cast('1991-09-05 11:11:11' as datetime); +select cast(cast('1991-09-05 11:11:11' as datetime) as char); +select cast('11:11:11' as time); +select * from t where a > cast(2 as decimal); +select cast(-1 as unsigned); +drop table if exists t; +create table t(a decimal(3, 1), b double, c datetime, d time, e int); +insert into t value(12.3, 1.23, '2017-01-01 12:12:12', '12:12:12', 123); +select cast(a as json), cast(b as json), cast(c as json), cast(d as json), cast(e as json) from t; +select cast(10101000000 as time); +select cast(10101001000 as time); +select cast(10000000000 as time); +select cast(20171222020005 as time); +select cast(8380000 as time); +select cast(8390000 as time); +select cast(8386000 as time); +select cast(8385960 as time); +select cast(cast('2017-01-01 01:01:11.12' as date) as datetime(2)); +select cast(20170118.999 as datetime); +select convert(a2.a, unsigned int) from (select cast('"9223372036854775808"' as json) as a) as a2; +create table tb5(a bigint(64) unsigned, b double); +insert into tb5 (a, b) values (9223372036854776000, 9223372036854776000); +insert into tb5 (a, b) select * from (select cast(a as json) as a1, b from tb5) as t where t.a1 = t.b; +drop table tb5; +create table tb5(a float(53)); +insert into tb5(a) values (13835058055282163712); +select convert(t.a1, signed int) from (select convert(a, json) as a1 from tb5) as t; +drop table tb5; +select cast(0xffffffffffffffff as signed); +select cast(0x9999999999999999999999999999999999999999999 as signed); +create table tb5(a bigint); +set sql_mode=''; +insert into tb5(a) values (0xfffffffffffffffffffffffff); +select * from tb5; +drop table tb5; +create table tb5(a double); +insert into tb5 (a) values (18446744073709551616); +insert into tb5 (a) values (184467440737095516160); +select cast(a as unsigned) from tb5; +drop table tb5; +create table tb5(a bigint(64) unsigned, b decimal(64, 10)); +insert into tb5 (a, b) values (9223372036854775808, 9223372036854775808); +insert into tb5 (select * from tb5 where a = b); +select * from tb5; +drop table tb5; +create table tb5(a bigint(64) unsigned, b double(64, 10)); +insert into tb5 (a, b) values (13835058000000000000, 13835058000000000000); +insert into tb5 (select * from tb5 where a = b); +select * from tb5; +drop table tb5; +create table tb5(a double, b float); +insert into tb5 (a, b) values (184467440737095516160, 184467440737095516160); +select * from tb5 where cast(a as unsigned int)=0; +show warnings; +select * from tb5 where cast(b as unsigned int)=0; +show warnings; +drop table tb5; +create table tb5(a double, b bigint unsigned); +insert into tb5 (a, b) values (18446744073709551616, 18446744073709551615); +select * from tb5 where cast(a as unsigned int)=b; +show warnings; +drop table tb5; +create table tb5(a json, b bigint unsigned); +insert into tb5 (a, b) values ('184467440737095516160', 18446744073709551615); +select * from tb5 where cast(a as unsigned int)=b; +show warnings; +select * from tb5 where cast(b as unsigned int)=0; +show warnings; +drop table tb5; +create table tb5(a json, b bigint unsigned); +insert into tb5 (a, b) values ('92233720368547758080', 18446744073709551615); +select * from tb5 where cast(a as signed int)=b; +show warnings; +drop table tb5; +create table tb5(a bigint(64) unsigned,b varchar(50)); +insert into tb5(a, b) values (9223372036854775808, '9223372036854775808'); +insert into tb5(select * from tb5 where a = b); +select * from tb5; +drop table tb5; +drop table if exists tb5; +create table tb5 (a decimal(65), b bigint(64) unsigned); +insert into tb5 (a, b) values (9223372036854775808, 9223372036854775808); +select cast(b as decimal(64)) from tb5 union all select b from tb5; +drop table tb5; +drop table if exists tb5; +create table tb5 (a bigint(64) unsigned, b double(64, 10)); +insert into tb5 (a, b) values (9223372036854775808, 9223372036854775808); +select a from tb5 where a = b union all select b from tb5; +drop table tb5; +select cast("170102034" as datetime); +select cast("1701020304" as datetime); +select cast("1701020304." as datetime); +select cast("1701020304.1" as datetime); +select cast("1701020304.111" as datetime); +show warnings; +select cast("17011" as datetime); +select cast("150101." as datetime); +select cast("150101.a" as datetime); +show warnings; +select cast("150101.1a" as datetime); +show warnings; +select cast("150101.1a1" as datetime); +show warnings; +select cast("1101010101.111" as datetime); +show warnings; +select cast("1101010101.11aaaaa" as datetime); +show warnings; +select cast("1101010101.a1aaaaa" as datetime); +show warnings; +select cast("1101010101.11" as datetime); +select @@warning_count; +select cast("1101010101.111" as datetime); +show warnings; +select cast("970101.111" as datetime); +select @@warning_count; +select cast("970101.11111" as datetime); +select @@warning_count; +select cast("970101.111a1" as datetime); +show warnings; +drop table if exists t; +create table t (a int, b int, c int, d char(10), e datetime, f float, g decimal(10, 3)); +insert t values (1, 0, null, null, null, null, null); +select ISNULL(a), ISNULL(b), ISNULL(c), ISNULL(d), ISNULL(e), ISNULL(f), ISNULL(g) from t; +select cast('-24 100:00:00' as time); +select cast('12:00:00.000000' as datetime); +select cast('-34 100:00:00' as time); +DROP TABLE IF EXISTS t; +CREATE TABLE t (ix TIME); +SET SQL_MODE=''; +select cast('10009010' as time); +show warnings; +insert into t select cast('10009010' as time); +show warnings; +select cast('239010' as time); +show warnings; +insert into t select cast('239010' as time); +show warnings; +select cast('233070' as time); +show warnings; +insert into t select cast('233070' as time); +show warnings; +select cast('23:90:10' as time); +show warnings; +insert into t select cast('23:90:10' as time); +show warnings; +select cast('23:30:70' as time); +show warnings; +insert into t select cast('23:30:70' as time); +show warnings; +select cast('239010.2' as time); +show warnings; +insert into t select cast('239010.2' as time); +show warnings; +select cast('233070.8' as time); +show warnings; +insert into t select cast('233070.8' as time); +show warnings; +set sql_mode = 'STRICT_TRANS_TABLES'; +select cast('10009010' as time); +show warnings; +-- error 1292 +insert into t select cast('10009010' as time); +select cast('239010' as time); +show warnings; +-- error 1292 +insert into t select cast('239010' as time); +select cast('233070' as time); +show warnings; +-- error 1292 +insert into t select cast('233070' as time); +select cast('23:90:10' as time); +show warnings; +-- error 1292 +insert into t select cast('23:90:10' as time); +select cast('23:30:70' as time); +show warnings; +-- error 1292 +insert into t select cast('23:30:70' as time); +select cast('239010.2' as time); +show warnings; +-- error 1292 +insert into t select cast('239010.2' as time); +select cast('233070.8' as time); +show warnings; +-- error 1292 +insert into t select cast('233070.8' as time); +select cast('18446744073709551616' as unsigned); +select cast('18446744073709551616' as signed); +select cast('9223372036854775808' as signed); +select cast('9223372036854775809' as signed); +select cast('9223372036854775807' as signed); +select cast('18446744073709551615' as signed); +select cast('18446744073709551614' as signed); +select cast(18446744073709551615 as unsigned); +select cast(18446744073709551616 as unsigned); +select cast(18446744073709551616 as signed); +select cast(18446744073709551617 as signed); +select cast(18446744073709551615 as signed); +select cast(18446744073709551614 as signed); +select cast(-18446744073709551616 as signed); +select cast(18446744073709551614.9 as unsigned); +select cast(18446744073709551614.4 as unsigned); +select cast(-9223372036854775809 as signed); +select cast(-9223372036854775809 as unsigned); +select cast(-9223372036854775808 as unsigned); +select cast('-9223372036854775809' as unsigned); +select cast('-9223372036854775807' as unsigned); +select cast('-2' as unsigned); +select cast(cast(1-2 as unsigned) as signed integer); +select cast(1 as signed int); +select cast(1 as double); +select cast(cast(12345 as unsigned) as double); +select cast(1.1 as double); +select cast(-1.1 as double); +select cast('123.321' as double); +select cast('12345678901234567890' as double) = 1.2345678901234567e19; +select cast(-1 as double); +select cast(null as double); +select cast(12345678901234567890 as double) = 1.2345678901234567e19; +select cast(cast(-1 as unsigned) as double) = 1.8446744073709552e19; +select cast(1e100 as double) = 1e100; +select cast(123456789012345678901234567890 as double) = 1.2345678901234568e29; +select cast(0x12345678 as double); +select cast(1 as float); +select cast(cast(12345 as unsigned) as float); +select cast(1.1 as float) = 1.1; +select cast(-1.1 as float) = -1.1; +select cast('123.321' as float) =123.321; +select cast('12345678901234567890' as float) = 1.2345678901234567e19; +select cast(-1 as float); +select cast(null as float); +select cast(12345678901234567890 as float) = 1.2345678901234567e19; +select cast(cast(-1 as unsigned) as float) = 1.8446744073709552e19; +select cast(1e100 as float(40)) = 1e100; +select cast(123456789012345678901234567890 as float(40)) = 1.2345678901234568e29; +select cast(0x12345678 as float(40)) = 305419896; +select cast(1 as real); +select cast(cast(12345 as unsigned) as real); +select cast(1.1 as real) = 1.1; +select cast(-1.1 as real) = -1.1; +select cast('123.321' as real) =123.321; +select cast('12345678901234567890' as real) = 1.2345678901234567e19; +select cast(-1 as real); +select cast(null as real); +select cast(12345678901234567890 as real) = 1.2345678901234567e19; +select cast(cast(-1 as unsigned) as real) = 1.8446744073709552e19; +select cast(1e100 as real) = 1e100; +select cast(123456789012345678901234567890 as real) = 1.2345678901234568e29; +select cast(0x12345678 as real) = 305419896; +drop table if exists t1; +create table t1(s1 time); +insert into t1 values('11:11:11'); +select cast(s1 as decimal(7, 2)) from t1; +select cast(s1 as decimal(8, 2)) from t1; +-- error 1690 +insert into t1 values(cast('111111.00' as decimal(7, 2))); +select CAST(0x8fffffffffffffff as signed) a, + CAST(0xfffffffffffffffe as signed) b, + CAST(0xffffffffffffffff as unsigned) c; +select cast("1:2:3" as TIME) = "1:02:03"; +drop table if exists t; +create table t(a time(6)); +insert into t value('12:59:59.999999'); +select cast(a as signed) from t; +select -9223372036854775809; +select --9223372036854775809; +select -9223372036854775808; +drop table if exists t; +create table t(a bigint(30)); +-- error 1264 +insert into t values(-9223372036854775809); +-- error 1427 +select cast(12.1 as decimal(3, 4)); +-- error 1426 +SELECT CAST(1 AS DATETIME(7)); +select unhex('4D7953514C'); +select unhex(hex('string')); +select unhex('ggg'); +select unhex(-1); +select hex(unhex('1267')); +select hex(unhex(1267)); +drop table if exists t; +create table t(a binary(8)); +insert into t values('test'); +select hex(a) from t; +select unhex(a) from t; +select from_unixtime(1451606400); +select from_unixtime(14516064000/10); +select from_unixtime('14516064000'/10); +select from_unixtime(cast(1451606400 as double)); +select from_unixtime(cast(cast(1451606400 as double) as DECIMAL)); +select from_unixtime(cast(cast(1451606400 as double) as DECIMAL(65,1))); +select from_unixtime(1451606400.123456); +select from_unixtime(1451606400.1234567); +select from_unixtime(1451606400.999999); +select from_unixtime(1511247196661); +select from_unixtime('1451606400.123'); +drop table if exists t; +create table t(a int); +insert into t value(1451606400); +select from_unixtime(a) from t; +select strcmp('abc', 'def'); +select strcmp('abc', 'aba'); +select strcmp('abc', 'abc'); +select substr(null, 1, 2); +select substr('123', null, 2); +select substr('123', 1, null); +drop table if exists t; +create table t (a varchar(255), b int); +insert t values ('str1', 1); +select * from t where a = case b when 1 then 'str1' when 2 then 'str2' end; +select * from t where a = case b when 1 then 'str2' when 2 then 'str3' end; +insert t values ('str2', 2); +select * from t where a = case b when 2 then 'str2' when 3 then 'str3' end; +insert t values ('str3', 3); +select * from t where a = case b when 4 then 'str4' when 5 then 'str5' else 'str3' end; +select * from t where a = case b when 4 then 'str4' when 5 then 'str5' else 'str6' end; +select * from t where a = case when b then 'str3' when 1 then 'str1' else 'str2' end; +delete from t; +insert t values ('str2', 0); +select * from t where a = case when b then 'str3' when 0 then 'str1' else 'str2' end; +insert t values ('str1', null); +select * from t where a = case b when null then 'str3' when 10 then 'str1' else 'str2' end; +select * from t where a = case null when b then 'str3' when 10 then 'str1' else 'str2' end; +insert t values (null, 4); +select * from t where b < case a when null then 0 when 'str2' then 0 else 9 end; +select * from t where b = case when a is null then 4 when a = 'str5' then 7 else 9 end; +SELECT -Max(+23) * -+Cast(--10 AS SIGNED) * -CASE + WHEN 0 > 85 THEN NULL + WHEN NOT + CASE +55 + WHEN +( +82 ) + -89 * -69 THEN +Count(-88) + WHEN +CASE 57 + WHEN +89 THEN -89 * Count(*) + WHEN 17 THEN NULL + END THEN ( -10 ) + END IS NULL THEN NULL + ELSE 83 + 48 + END AS col0; ; +drop table if exists t1; +create table t1(c1 int not null); +insert into t1 values(1); +select (case when null then c1 end) is null from t1; +select (case when null then c1 end) is not null from t1; +select case when b=0 then 1 else 1/b end from t; +show warnings; +select if(b=0, 1, 1/b) from t; +show warnings; +select ifnull(b, b/0) from t; +show warnings; +select case when 1 then 1 else 1/0 end; +show warnings; +select if(1,1,1/0); +show warnings; +select ifnull(1, 1/0); +show warnings; +delete from t; +insert t values ('str2', 0); +select case when b < 1 then 1 else 1/0 end from t; +show warnings; +select case when b < 1 then 1 when 1/0 then b else 1/0 end from t; +show warnings; +select if(b < 1 , 1, 1/0) from t; +show warnings; +select ifnull(b, 1/0) from t; +show warnings; +select COALESCE(1, b, b/0) from t; +show warnings; +select 0 and b/0 from t; +show warnings; +select 1 or b/0 from t; +show warnings; +select 1 or 1/0; +show warnings; +select 0 and 1/0; +show warnings; +select COALESCE(1, 1/0); +show warnings; +select interval(1,0,1,2,1/0); +show warnings; +select case 2.0 when 2.0 then 3.0 when 3.0 then 2.0 end; +select case 2.0 when 3.0 then 2.0 when 4.0 then 3.0 else 5.0 end; +select case cast('2011-01-01' as date) when cast('2011-01-01' as date) then cast('2011-02-02' as date) end; +select case cast('2012-01-01' as date) when cast('2011-01-01' as date) then cast('2011-02-02' as date) else cast('2011-03-03' as date) end; +select case cast('10:10:10' as time) when cast('10:10:10' as time) then cast('11:11:11' as time) end; +select case cast('10:10:13' as time) when cast('10:10:10' as time) then cast('11:11:11' as time) else cast('22:22:22' as time) end; +select cast(1234 as char(3)); +select cast(1234 as char(0)); +show warnings; +select CAST( - 8 AS DECIMAL ) * + 52 + 87 < - 86; +select char(97, 100, 256, 89); +select char(97, null, 100, 256, 89); +select char(97, null, 100, 256, 89 using utf8); +select char(97, null, 100, 256, 89 using ascii); +-- error 1115 +select char(97, null, 100, 256, 89 using tidb); +drop table if exists t; +CREATE TABLE t (c1 date, c2 datetime, c3 timestamp, c4 time, c5 year); +INSERT INTO t values ('2000-01-01', '2000-01-01 12:12:12', '2000-01-01 12:12:12', '12:12:12', '2000'); +INSERT INTO t values ('2000-02-01', '2000-02-01 12:12:12', '2000-02-01 12:12:12', '13:12:12', 2000); +INSERT INTO t values ('2000-03-01', '2000-03-01', '2000-03-01 12:12:12', '1 12:12:12', 2000); +INSERT INTO t SET c1 = '2000-04-01', c2 = '2000-04-01', c3 = '2000-04-01 12:12:12', c4 = '-1 13:12:12', c5 = 2000; +SELECT c4 FROM t where c4 < '-13:12:12'; +SELECT 1 DIV - - 28 + ( - SUM( - + 25 ) ) * - CASE - 18 WHEN 44 THEN NULL ELSE - 41 + 32 + + - 70 - + COUNT( - 95 ) * 15 END + 92; +drop table if exists t; +create table t (a char(10), b varchar(10), c binary(10), d varbinary(10)); +insert into t values ('text','text','text','text'); +select a regexp 'xt' from t; +select b regexp 'xt' from t; +select b regexp binary 'Xt' from t; +select c regexp 'Xt' from t; +select d regexp 'Xt' from t; +select a rlike 'xt' from t; +select a rlike binary 'Xt' from t; +select b rlike 'xt' from t; +select c rlike 'Xt' from t; +select d rlike 'Xt' from t; +select 'a' regexp 'A', 'a' regexp binary 'A'; +drop table if exists t; +create table t (a varchar(255), b int); +insert into t values('a', 0); +select * from t where a like 'a'; +delete from t where b = 0; +insert into t values('b', 1); +select * from t where a like 'a'; +delete from t where b = 1; +insert into t values('Aa', 2); +select * from t where a like 'aA'; +delete from t where b = 2; +insert into t values('aAab', 3); +select * from t where a like 'aA%'; +delete from t where b = 3; +insert into t values('Aaab', 4); +select * from t where a like 'aA_'; +delete from t where b = 4; +insert into t values('Aab', 5); +select * from t where a like 'Aa_'; +delete from t where b = 5; +insert into t values('', 6); +select * from t where a like ''; +delete from t where b = 6; +insert into t values('a', 7); +select * from t where a like ''; +delete from t where b = 7; +drop table if exists t; +create table t (a varchar(255), b int); +insert into t values('a', 0); +select * from t where a regexp '^$'; +delete from t where b = 0; +insert into t values('a', 1); +select * from t where a regexp 'a'; +delete from t where b = 1; +insert into t values('b', 2); +select * from t where a regexp 'a'; +delete from t where b = 2; +insert into t values('aA', 3); +select * from t where a regexp 'aA'; +delete from t where b = 3; +insert into t values('a', 4); +select * from t where a regexp '.'; +delete from t where b = 4; +insert into t values('ab', 5); +select * from t where a regexp '^.$'; +delete from t where b = 5; +insert into t values('b', 6); +select * from t where a regexp '..'; +delete from t where b = 6; +insert into t values('aab', 7); +select * from t where a regexp '.ab'; +delete from t where b = 7; +insert into t values('abcd', 8); +select * from t where a regexp 'ab.'; +delete from t where b = 8; +insert into t values('abcd', 9); +select * from t where a regexp '.*'; +delete from t where b = 9; +select cast(1 as signed) + cast(9223372036854775807 as unsigned); +select cast(9223372036854775807 as unsigned) + cast(1 as signed); +-- error 1690 +select cast(9223372036854775807 as signed) + cast(9223372036854775809 as unsigned); +-- error 1690 +select cast(9223372036854775809 as unsigned) + cast(9223372036854775807 as signed); +-- error 1690 +select cast(-9223372036854775807 as signed) + cast(9223372036854775806 as unsigned); +-- error 1690 +select cast(9223372036854775806 as unsigned) + cast(-9223372036854775807 as signed); +select 1 / '2007' div 1; +set sql_mode=default + +# TestFuncREPEAT +DROP TABLE IF EXISTS table_string; +CREATE TABLE table_string(a CHAR(20), b VARCHAR(20), c TINYTEXT, d TEXT(20), e MEDIUMTEXT, f LONGTEXT, g BIGINT); +INSERT INTO table_string (a, b, c, d, e, f, g) VALUES ('a', 'b', 'c', 'd', 'e', 'f', 2); +SELECT REPEAT(a, g), REPEAT(b, g), REPEAT(c, g), REPEAT(d, g), REPEAT(e, g), REPEAT(f, g) FROM table_string; +SELECT REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g) FROM table_string; +SELECT REPEAT(a, NULL), REPEAT(b, NULL), REPEAT(c, NULL), REPEAT(d, NULL), REPEAT(e, NULL), REPEAT(f, NULL) FROM table_string; +SELECT REPEAT(a, 2), REPEAT(b, 2), REPEAT(c, 2), REPEAT(d, 2), REPEAT(e, 2), REPEAT(f, 2) FROM table_string; +SELECT REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2) FROM table_string; +SELECT REPEAT(a, -1), REPEAT(b, -2), REPEAT(c, -2), REPEAT(d, -2), REPEAT(e, -2), REPEAT(f, -2) FROM table_string; +SELECT REPEAT(a, 0), REPEAT(b, 0), REPEAT(c, 0), REPEAT(d, 0), REPEAT(e, 0), REPEAT(f, 0) FROM table_string; +SELECT REPEAT(a, 16777217), REPEAT(b, 16777217), REPEAT(c, 16777217), REPEAT(d, 16777217), REPEAT(e, 16777217), REPEAT(f, 16777217) FROM table_string; + +# TestFuncLpadAndRpad +DROP TABLE IF EXISTS t; +CREATE TABLE t(a BINARY(10), b CHAR(10)); +INSERT INTO t SELECT "中文", "abc"; +SELECT LPAD(a, 11, "a"), LPAD(b, 2, "xx") FROM t; +SELECT RPAD(a, 11, "a"), RPAD(b, 2, "xx") FROM t; + +# TestStringBuiltin +drop table if exists t; +create table t(a int, b double, c datetime, d time, e char(20), f bit(10)); +insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef", 0b10101); +select length(a), length(b), length(c), length(d), length(e), length(f), length(null) from t; +drop table if exists t; +create table t(a char(20)); +insert into t values("tidb "), (concat("a ", "b ")); +select a, length(a) from t; +drop table if exists t; +create table t(a int, b double, c datetime, d time, e char(20)); +insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef"); +select concat(a, b, c, d, e) from t; +select concat(null); +select concat(null, a, b) from t; +drop table if exists t; +create table t(a int, b double, c datetime, d time, e char(20)); +insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef"); +select concat_ws('|', a, b, c, d, e) from t; +select concat_ws(null, null); +select concat_ws(null, a, b) from t; +select concat_ws(',', 'a', 'b'); +select concat_ws(',','First name',NULL,'Last Name'); +drop table if exists t; +create table t(a tinyint(2), b varchar(10)); +insert into t values (1, 'a'), (12, 'a'), (126, 'a'), (127, 'a'); +select concat_ws('#', a, b) from t; +drop table if exists t; +create table t(a binary(3)); +insert into t values('a'); +select concat_ws(',', a, 'test') = 'a\0\0,test' from t; +drop table if exists t; +create table t(a char(10), b int, c double, d datetime, e time, f bit(4)); +insert into t values('2', 2, 2.3, "2017-01-01 12:01:01", "12:01:01", 0b1010); +select ascii(a), ascii(b), ascii(c), ascii(d), ascii(e), ascii(f) from t; +select ascii('123'), ascii(123), ascii(''), ascii('你好'), ascii(NULL); +drop table if exists t; +create table t(a int, b double, c datetime, d time, e char(20), f binary(3), g binary(3)); +insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef", 'aa', 'BB'); +select lower(a), lower(b), lower(c), lower(d), lower(e), lower(f), lower(g), lower(null) from t; +select upper(a), upper(b), upper(c), upper(d), upper(e), upper(f), upper(g), upper(null) from t; +drop table if exists t; +create table t(a char(10), b int, c double, d datetime, e time); +insert into t values("123", 123, 12.34, "2017-01-01 12:01:01", "12:01:01"); +select strcmp(a, "123"), strcmp(b, "123"), strcmp(c, "12.34"), strcmp(d, "2017-01-01 12:01:01"), strcmp(e, "12:01:01") from t; +select strcmp("1", "123"), strcmp("123", "1"), strcmp("123", "45"), strcmp("123", null), strcmp(null, "123"); +select strcmp("", "123"), strcmp("123", ""), strcmp("", ""), strcmp("", null), strcmp(null, ""); +drop table if exists t; +create table t(a char(10), b int, c double, d datetime, e time); +insert into t values('abcde', 1234, 12.34, "2017-01-01 12:01:01", "12:01:01"); +select left(a, 2), left(b, 2), left(c, 2), left(d, 2), left(e, 2) from t; +select left("abc", 0), left("abc", -1), left(NULL, 1), left("abc", NULL); +select left("abc", "a"), left("abc", 1.9), left("abc", 1.2); +select left("中文abc", 2), left("中文abc", 3), left("中文abc", 4); +select right(a, 3), right(b, 3), right(c, 3), right(d, 3), right(e, 3) from t; +select right("abcde", 0), right("abcde", -1), right("abcde", 100), right(NULL, 1), right("abcde", NULL); +select right("abcde", "a"), right("abcde", 1.9), right("abcde", 1.2); +select right("中文abc", 2), right("中文abc", 4), right("中文abc", 5); +drop table if exists t; +create table t(a binary(10)); +insert into t select "中文abc"; +select left(a, 3), left(a, 6), left(a, 7) from t; +select right(a, 2), right(a, 7) from t; +drop table if exists t; +create table t(a char(10), b int, c double, d datetime, e time, f bit(4), g binary(20), h blob(10), i text(30)); +insert into t values('2', 2, 2.3, "2017-01-01 12:01:01", "12:01:01", 0b1010, "512", "48", "tidb"); +select ord(a), ord(b), ord(c), ord(d), ord(e), ord(f), ord(g), ord(h), ord(i) from t; +select ord('123'), ord(123), ord(''), ord('你好'), ord(NULL), ord('👍'); +select ord(X''), ord(X'6161'), ord(X'e4bd'), ord(X'e4bda0'), ord(_ascii'你'), ord(_latin1'你'); +select space(0), space(2), space(-1), space(1.1), space(1.9); +select space("abc"), space("2"), space("1.1"), space(''), space(null); +drop table if exists t; +create table t(a char(20), b int, c double, d datetime, e time); +insert into t values('www.mysql.com', 1234, 12.34, "2017-01-01 12:01:01", "12:01:01"); +select replace(a, 'mysql', 'pingcap'), replace(b, 2, 55), replace(c, 34, 0), replace(d, '-', '/'), replace(e, '01', '22') from t; +select replace('aaa', 'a', ''), replace(null, 'a', 'b'), replace('a', null, 'b'), replace('a', 'b', null); +drop table if exists t; +create table t(a int, b double, c datetime, d time, e char(20), f bit(10), g binary(20), h blob(10)); +insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef", 0b10101, "512", "abc"); +select to_base64(a), to_base64(b), to_base64(c), to_base64(d), to_base64(e), to_base64(f), to_base64(g), to_base64(h), to_base64(null) from t; +select from_base64("abcd"), from_base64("asc"); +select from_base64("MQ=="), from_base64(1234); +drop table if exists t; +create table t(a char(10), b int, c double, d datetime, e time); +insert into t values('Sakila', 12345, 123.45, "2017-01-01 12:01:01", "12:01:01"); +select substr(a, 3), substr(b, 2, 3), substr(c, -3), substr(d, -8), substr(e, -3, 100) from t; +select substr('Sakila', 100), substr('Sakila', -100), substr('Sakila', -5, 3), substr('Sakila', 2, -1); +select substr('foobarbar' from 4), substr('Sakila' from -4 for 2); +select substr(null, 2, 3), substr('foo', null, 3), substr('foo', 2, null); +select substr('中文abc', 2), substr('中文abc', 3), substr("中文abc", 1, 2); +drop table if exists t; +create table t(a binary(10)); +insert into t select "中文abc"; +select substr(a, 4), substr(a, 1, 3), substr(a, 1, 6) from t; +select substr("string", -1), substr("string", -2), substr("中文", -1), substr("中文", -2) from t; +drop table if exists t; +create table t(a int, b double, c datetime, d time, e char(20), f bit(10), g binary(20), h varbinary(20)); +insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef", 0b10101, "g", "h"); +select bit_length(a), bit_length(b), bit_length(c), bit_length(d), bit_length(e), bit_length(f), bit_length(g), bit_length(h), bit_length(null) from t; +drop table if exists t; +create table t(a char(20), b int, c double, d datetime, e time); +insert into t values('www.pingcap.com', 12345, 123.45, "2017-01-01 12:01:01", "12:01:01"); +select substring_index(a, '.', 2), substring_index(b, '.', 2), substring_index(c, '.', -1), substring_index(d, '-', 1), substring_index(e, ':', -2) from t; +select substring_index('www.pingcap.com', '.', 0), substring_index('www.pingcap.com', '.', 100), substring_index('www.pingcap.com', '.', -100); +select substring_index('www.pingcap.com', 'd', 1), substring_index('www.pingcap.com', '', 1), substring_index('', '.', 1); +select substring_index(null, '.', 1), substring_index('www.pingcap.com', null, 1), substring_index('www.pingcap.com', '.', null); +select substring_index('xyz', 'abc', 9223372036854775808); +select substring_index("aaa.bbb.ccc.ddd.eee",'.',18446744073709551613); +select substring_index("aaa.bbb.ccc.ddd.eee",'.',-18446744073709551613); +select substring_index('aaa.bbb.ccc.ddd.eee', '.', 18446744073709551615 - 1 + id) from (select 1 as id) as t1; +select substring_index('aaa.bbb.ccc.ddd.eee', '.', -18446744073709551615 - 1 + id) from (select 1 as id) as t1; +set tidb_enable_vectorized_expression = 0; +select substring_index("aaa.bbb.ccc.ddd.eee",'.',18446744073709551613); +select substring_index("aaa.bbb.ccc.ddd.eee",'.',-18446744073709551613); +select substring_index('aaa.bbb.ccc.ddd.eee', '.', 18446744073709551615 - 1 + id) from (select 1 as id) as t1; +select substring_index('aaa.bbb.ccc.ddd.eee', '.', -18446744073709551615 - 1 + id) from (select 1 as id) as t1; +set tidb_enable_vectorized_expression = 1; +drop table if exists t; +create table t(a char(20), b int, c double, d datetime, e time, f decimal(5, 2), g bit(4)); +insert into t values('www.pingcap.com', 12345, 123.45, "2017-01-01 12:01:01", "12:01:01", 123.45, 0b1100); +select hex(a), hex(b), hex(c), hex(d), hex(e), hex(f), hex(g) from t; +select hex('abc'), hex('你好'), hex(12), hex(12.3), hex(12.8); +select hex(-1), hex(-12.3), hex(-12.8), hex(0x12), hex(null); +drop table if exists t; +CREATE TABLE t(i int primary key auto_increment, a binary, b binary(0), c binary(20), d binary(255)) character set utf8 collate utf8_bin; +insert into t(a, b, c, d) values ('a', NULL, 'a','a'); +select i, hex(a), hex(b), hex(c), hex(d) from t; +select unhex('4D7953514C'), unhex('313233'), unhex(313233), unhex(''); +select unhex('string'), unhex('你好'), unhex(123.4), unhex(null); +select ltrim(' bar '), ltrim('bar'), ltrim(''), ltrim(null); +select rtrim(' bar '), rtrim('bar'), rtrim(''), rtrim(null); +select ltrim("\t bar "), ltrim(" \tbar"), ltrim("\n bar"), ltrim("\r bar"); +select rtrim(" bar \t"), rtrim("bar\t "), rtrim("bar \n"), rtrim("bar \r"); +DROP TABLE IF EXISTS t; +CREATE TABLE t(a BINARY(6)); +INSERT INTO t VALUES("中文"); +SELECT a, REVERSE(a), REVERSE("中文"), REVERSE("123 ") FROM t; +SELECT REVERSE(123), REVERSE(12.09) FROM t; +select trim(' bar '), trim(leading 'x' from 'xxxbarxxx'), trim(trailing 'xyz' from 'barxxyz'), trim(both 'x' from 'xxxbarxxx'); +select trim('\t bar\n '), trim(' \rbar \t'); +select trim(leading from ' bar'), trim('x' from 'xxxbarxxx'), trim('x' from 'bar'), trim('' from ' bar '); +select trim(''), trim('x' from ''); +select trim(null from 'bar'), trim('x' from null), trim(null), trim(leading null from 'bar'); +drop table if exists t; +create table t(a char(20), b int, c double, d datetime, e time, f binary(5)); +insert into t values('www.pingcap.com', 12345, 123.45, "2017-01-01 12:01:01", "12:01:01", "HelLo"); +select locate(".ping", a), locate(".ping", a, 5) from t; +select locate("234", b), locate("235", b, 10) from t; +select locate(".45", c), locate(".35", b) from t; +select locate("El", f), locate("ll", f), locate("lL", f), locate("Lo", f), locate("lo", f) from t; +select locate("01 12", d) from t; +select locate("文", "中文字符串", 2); +select locate("文", "中文字符串", 3); +select locate("文", "中文字符串"); +select bin(-1); +select bin(5); +select bin("中文"); +select character_length(null), character_length("Hello"), character_length("a中b文c"), + character_length(123), character_length(12.3456); +select char_length(null), char_length("Hello"), char_length("a中b文c"), char_length(123),char_length(12.3456); +select char_length(null), char_length("Hello"), char_length("a 中 b 文 c"), char_length("НОЧЬ НА ОКРАИНЕ МОСКВЫ"); +select char_length(null), char_length(binary("Hello")), char_length(binary("a 中 b 文 c")), char_length(binary("НОЧЬ НА ОКРАИНЕ МОСКВЫ")); +select elt(0, "abc", "def"), elt(2, "hello", "中文", "tidb"), elt(4, "hello", "中文", + "tidb"); +select instr("中国", "国"), instr("中国", ""), instr("abc", ""), instr("", ""), instr("", "abc"); +select instr("中国", null), instr(null, ""), instr(null, null); +drop table if exists t; +create table t(a binary(20), b char(20)); +insert into t values("中国", cast("国" as binary)), ("中国", ""), ("abc", ""), ("", ""), ("", "abc"); +select instr(a, b) from t; +select oct("aaaa"), oct("-1.9"), oct("-9999999999999999999999999"), oct("9999999999999999999999999"); +select oct(-1.9), oct(1.9), oct(-1), oct(1), oct(-9999999999999999999999999), oct(9999999999999999999999999); +select find_in_set("", ""), find_in_set("", ","), find_in_set("中文", "字符串,中文"), find_in_set("b,", "a,b,c,d"); +select find_in_set(NULL, ""), find_in_set("", NULL), find_in_set(1, "2,3,1"); +select make_set(0, "12"), make_set(3, "aa", "11"), make_set(3, NULL, "中文"), make_set(NULL, "aa"); +select quote("aaaa"), quote(""), quote("\"\""), quote("\n\n"); +select quote(0121), quote(0000), quote("中文"), quote(NULL); +select quote(null) is NULL; +select quote(null) is NOT NULL; +select length(quote(null)); +select quote(null) REGEXP binary 'null'; +select quote(null) REGEXP binary 'NULL'; +select quote(null) REGEXP 'NULL'; +select quote(null) REGEXP 'null'; +select convert("123" using "binary"), convert("中文" using "binary"), convert("中文" using "utf8"), convert("中文" using "utf8mb4"), convert(cast("中文" as binary) using "utf8"); +-- error 1115 +select convert("123" using "866"); +select insert("中文", 1, 1, cast("aaa" as binary)), insert("ba", -1, 1, "aaa"), insert("ba", 1, 100, "aaa"), insert("ba", 100, 1, "aaa"); +select insert("bb", NULL, 1, "aa"), insert("bb", 1, NULL, "aa"), insert(NULL, 1, 1, "aaa"), insert("bb", 1, 1, NULL); +SELECT INSERT("bb", 0, 1, NULL), INSERT("bb", 0, NULL, "aaa"); +SELECT INSERT("中文", 0, 1, NULL), INSERT("中文", 0, NULL, "aaa"); +select export_set(7, "1", "0", ",", 65); +select export_set(7, "1", "0", ",", -1); +select export_set(7, "1", "0", ","); +select export_set(7, "1", "0"); +select export_set(NULL, "1", "0", ",", 65); +select export_set(7, "1", "0", ",", 1); +select format(12332.1, 4), format(12332.2, 0), format(12332.2, 2,'en_US'); +select format(NULL, 4), format(12332.2, NULL); +select format(12332.2, 2,'es_EC'); +show warnings; +select field(1, 2, 1), field(1, 0, NULL), field(1, NULL, 2, 1), field(NULL, 1, 2, NULL); +select field("1", 2, 1), field(1, "0", NULL), field("1", NULL, 2, 1), field(NULL, 1, "2", NULL); +select field("1", 2, 1), field(1, "abc", NULL), field("1", NULL, 2, 1), field(NULL, 1, "2", NULL); +select field("abc", "a", 1), field(1.3, "1.3", 1.5); +drop table if exists t; +create table t(a decimal(11, 8), b decimal(11,8)); +insert into t values('114.57011441','38.04620115'), ('-38.04620119', '38.04620115'); +select a,b,concat_ws(',',a,b) from t; +drop table if exists t1; +CREATE TABLE t1 (c1 INT UNSIGNED NOT NULL ); +INSERT INTO t1 VALUES (0); +SELECT c1 FROM t1 WHERE c1 <> CAST(POW(-'0', 1) AS BINARY); +SELECT c1 FROM t1 WHERE c1 = CAST('-000' AS BINARY); + +# TestInvalidStrings +drop table if exists t; +create table t (a binary(5)); +insert into t values (0x1e240), ('ABCDE'); +set tidb_enable_vectorized_expression = on; +select convert(t.a using utf8) from t; +select convert(0x1e240 using utf8); +set tidb_enable_vectorized_expression = off; +select convert(t.a using utf8) from t; +select convert(0x1e240 using utf8); +set tidb_enable_vectorized_expression = default; + +# TestOpBuiltin +select 1 && 1, 1 && 0, 0 && 1, 0 && 0, 2 && -1, null && 1, '1a' && 'a'; +select ~123, ~-123, ~null; +select !1, !123, !0, !null; +select 1 xor 1, 1 xor 0, 0 xor 1, 0 xor 0, 2 xor -1, null xor 1, '1a' xor 'a'; +select 123 & 321, -123 & 321, null & 1; +select 123 | 321, -123 | 321, null | 1; +select 123 ^ 321, -123 ^ 321, null ^ 1; +select 123 << 2, -123 << 2, null << 1; +select 123 >> 2, -123 >> 2, null >> 1; +select 1 || 1, 1 || 0, 0 || 1, 0 || 0, 2 || -1, null || 1, '1a' || 'a'; +select +1, +0, +(-9), +(-0.001), +0.999, +null, +"aaa"; +drop table if exists f; +create table f(a decimal(65,0)); +insert into f value (-17000000000000000000); +select a from f; + +# TestControlBuiltin +select ifnull(1, 2); +select ifnull(null, 2); +select ifnull(1, null); +select ifnull(null, null); +drop table if exists t1; +create table t1(a bigint not null); +select ifnull(max(a),0) from t1; +drop table if exists t1; +drop table if exists t2; +create table t1(a decimal(20,4)); +create table t2(a decimal(20,4)); +insert into t1 select 1.2345; +insert into t2 select 1.2345; +select sum(ifnull(a, 0)) from ( + select ifnull(a, 0) as a from t1 + union all + select ifnull(a, 0) as a from t2 + ) t; +select IF(0,"ERROR","this"),IF(1,"is","ERROR"),IF(NULL,"ERROR","a"),IF(1,2,3)|0,IF(1,2.0,3.0)+0; +drop table if exists t1; +CREATE TABLE t1 (st varchar(255) NOT NULL, u int(11) NOT NULL); +INSERT INTO t1 VALUES ('a',1),('A',1),('aa',1),('AA',1),('a',1),('aaa',0),('BBB',0); +select if(1,st,st) s from t1 order by s; +select if(u=1,st,st) s from t1 order by s; +drop table if exists t1; +CREATE TABLE t1 (a varchar(255), b time, c int); +INSERT INTO t1 VALUE('abc', '12:00:00', 0); +INSERT INTO t1 VALUE('1abc', '00:00:00', 1); +INSERT INTO t1 VALUE('0abc', '12:59:59', 0); +select if(a, b, c), if(b, a, c), if(c, a, b) from t1; +select if(1, 1.0, 1); +select if(1, 1, 1.0); +select if(count(*), cast('2000-01-01' as date), cast('2011-01-01' as date)) from t1; +select if(count(*)=0, cast('2000-01-01' as date), cast('2011-01-01' as date)) from t1; +select if(count(*), cast('[]' as json), cast('{}' as json)) from t1; +select if(count(*)=0, cast('[]' as json), cast('{}' as json)) from t1; +SELECT 79 + + + CASE -87 WHEN -30 THEN COALESCE(COUNT(*), +COALESCE(+15, -33, -12 ) + +72) WHEN +COALESCE(+AVG(DISTINCT(60)), 21) THEN NULL ELSE NULL END AS col0; +SELECT -63 + COALESCE ( - 83, - 61 + - + 72 * - CAST( NULL AS SIGNED ) + + 3 ); + +# TestArithmeticBuiltin +DROP TABLE IF EXISTS t; +CREATE TABLE t(a DECIMAL(4, 2), b DECIMAL(5, 3)); +INSERT INTO t(a, b) VALUES(1.09, 1.999), (-1.1, -0.1); +SELECT a+b FROM t; +SELECT b+12, b+0.01, b+0.00001, b+12.00001 FROM t; +SELECT 1+12, 21+0.01, 89+"11", 12+"a", 12+NULL, NULL+1, NULL+NULL; +DROP TABLE IF EXISTS t; +CREATE TABLE t(a BIGINT UNSIGNED, b BIGINT UNSIGNED); +INSERT INTO t SELECT 1<<63, 1<<63; +-- error 1690 +SELECT a+b FROM t; +-- error 1690 +select cast(-3 as signed) + cast(2 as unsigned); +-- error 1690 +select cast(2 as unsigned) + cast(-3 as signed); +DROP TABLE IF EXISTS t; +CREATE TABLE t(a DECIMAL(4, 2), b DECIMAL(5, 3)); +INSERT INTO t(a, b) VALUES(1.09, 1.999), (-1.1, -0.1); +SELECT a-b FROM t; +SELECT b-12, b-0.01, b-0.00001, b-12.00001 FROM t; +SELECT 1-12, 21-0.01, 89-"11", 12-"a", 12-NULL, NULL-1, NULL-NULL; +DROP TABLE IF EXISTS t; +CREATE TABLE t(a BIGINT UNSIGNED, b BIGINT UNSIGNED); +INSERT INTO t SELECT 1, 4; +-- error 1690 +SELECT a-b FROM t; +-- error 1690 +select cast(1 as unsigned) - cast(4 as unsigned); +-- error 1690 +select cast(-1 as signed) - cast(-1 as unsigned); +-- error 1690 +select cast(1 as signed) - cast(-1 as unsigned); +-- error 1690 +select cast(-1 as unsigned) - cast(-1 as signed); +-- error 1690 +select cast(-9223372036854775808 as unsigned) - (-9223372036854775808); +-- error 1690 +select cast(12 as unsigned) - (14); +-- error 1690 +select cast(9223372036854775807 as signed) - cast(-1 as signed); +-- error 1690 +select cast(-9223372036854775808 as signed) - cast(1 as signed); +-- error 1690 +select cast(12 as signed) - cast(-9223372036854775808 as signed); +create table tb5(a int(10)); +insert into tb5 (a) values (10); +-- error 1690 +select * from tb5 where a - -9223372036854775808; +drop table tb5; +select cast(-9223372036854775808 as unsigned) - (-9223372036854775807); +select cast(-3 as unsigned) - cast(-1 as signed); +select 1.11 - 1.11; +select cast(-1 as unsigned) - cast(-12 as unsigned); +select cast(-1 as unsigned) - cast(0 as unsigned); +select 1234567890 * 1234567890; +-- error 1690 +select 1234567890 * 12345671890; +select cast(1234567890 as unsigned int) * 12345671890; +select 123344532434234234267890.0 * 1234567118923479823749823749.230; +-- error 1690 +select 123344532434234234267890.0 * 12345671189234798237498232384982309489238402830480239849238048239084749.230; +-- error 1690 +select 1.797693134862315708145274237317043567981e+308 * 1.1; +-- error 1690 +select 1.797693134862315708145274237317043567981e+308 * -1.1; +select 0.0 * -1; +DROP TABLE IF EXISTS t; +CREATE TABLE t(a DECIMAL(4, 2), b DECIMAL(5, 3)); +INSERT INTO t(a, b) VALUES(-1.09, 1.999); +SELECT a/b, a/12, a/-0.01, b/12, b/-0.01, b/0.000, NULL/b, b/NULL, NULL/NULL FROM t; +show warnings; +-- error 1690 +select 1e200/1e-200; +SELECT 13 DIV 12, 13 DIV 0.01, -13 DIV 2, 13 DIV NULL, NULL DIV 13, NULL DIV NULL; +SELECT 2.4 div 1.1, 2.4 div 1.2, 2.4 div 1.3; +SELECT 1.175494351E-37 div 1.7976931348623157E+308, 1.7976931348623157E+308 div -1.7976931348623157E+307, 1 div 1e-82; +show warnings; +-- error 1690 +select 1e300 DIV 1.5; +drop table if exists t; +CREATE TABLE t (c_varchar varchar(255), c_time time, nonzero int, zero int, c_int_unsigned int unsigned, c_timestamp timestamp, c_enum enum('a','b','c')); +INSERT INTO t VALUE('abc', '12:00:00', 12, 0, 5, '2017-08-05 18:19:03', 'b'); +select c_varchar div nonzero, c_time div nonzero, c_time div zero, c_timestamp div nonzero, c_timestamp div zero, c_varchar div zero from t; +select c_enum div nonzero from t; +select c_enum div zero from t; +select nonzero div zero from t; +show warnings; +select c_time div c_enum, c_timestamp div c_time, c_timestamp div c_enum from t; +select c_int_unsigned div nonzero, nonzero div c_int_unsigned, c_int_unsigned div zero from t; +show warnings; +SELECT CAST(1 AS UNSIGNED) MOD -9223372036854775808, -9223372036854775808 MOD CAST(1 AS UNSIGNED); +SELECT 13 MOD 12, 13 MOD 0.01, -13 MOD 2, 13 MOD NULL, NULL MOD 13, NULL DIV NULL; +SELECT 2.4 MOD 1.1, 2.4 MOD 1.2, 2.4 mod 1.30; +drop table if exists t; +CREATE TABLE t (c_varchar varchar(255), c_time time, nonzero int, zero int, c_timestamp timestamp, c_enum enum('a','b','c')); +INSERT INTO t VALUE('abc', '12:00:00', 12, 0, '2017-08-05 18:19:03', 'b'); +select c_varchar MOD nonzero, c_time MOD nonzero, c_timestamp MOD nonzero, c_enum MOD nonzero from t; +select c_time MOD c_enum, c_timestamp MOD c_time, c_timestamp MOD c_enum from t; +select c_enum MOD zero from t; +show warnings; +SET SQL_MODE='ERROR_FOR_DIVISION_BY_ZERO,STRICT_ALL_TABLES'; +drop table if exists t; +CREATE TABLE t (v int); +INSERT IGNORE INTO t VALUE(12 MOD 0); +show warnings; +select v from t; +select 0.000 % 0.11234500000000000000; +-- error 1365 +INSERT INTO t VALUE(12 MOD 0); +select sum(1.2e2) * 0.1; +drop table if exists t; +create table t(a double); +insert into t value(1.2); +select sum(a) * 0.1 from t; +drop table if exists t; +create table t(a double); +insert into t value(1.2); +select * from t where a/0 > 1; +show warnings; +DROP TABLE IF EXISTS t; +CREATE TABLE t(a BIGINT, b DECIMAL(6, 2)); +INSERT INTO t VALUES(0, 1.12), (1, 1.21); +SELECT a/b FROM t; + +# TestCompareBuiltin +drop table if exists t; +CREATE TABLE t (pk int NOT NULL PRIMARY KEY AUTO_INCREMENT, i INT, j JSON); +INSERT INTO t(i, j) VALUES (0, NULL); +INSERT INTO t(i, j) VALUES (1, '{"a": 2}'); +INSERT INTO t(i, j) VALUES (2, '[1,2]'); +INSERT INTO t(i, j) VALUES (3, '{"a":"b", "c":"d","ab":"abc", "bc": ["x", "y"]}'); +INSERT INTO t(i, j) VALUES (4, '["here", ["I", "am"], "!!!"]'); +INSERT INTO t(i, j) VALUES (5, '"scalar string"'); +INSERT INTO t(i, j) VALUES (6, 'true'); +INSERT INTO t(i, j) VALUES (7, 'false'); +INSERT INTO t(i, j) VALUES (8, 'null'); +INSERT INTO t(i, j) VALUES (9, '-1'); +INSERT INTO t(i, j) VALUES (10, CAST(CAST(1 AS UNSIGNED) AS JSON)); +INSERT INTO t(i, j) VALUES (11, '32767'); +INSERT INTO t(i, j) VALUES (12, '32768'); +INSERT INTO t(i, j) VALUES (13, '-32768'); +INSERT INTO t(i, j) VALUES (14, '-32769'); +INSERT INTO t(i, j) VALUES (15, '2147483647'); +INSERT INTO t(i, j) VALUES (16, '2147483648'); +INSERT INTO t(i, j) VALUES (17, '-2147483648'); +INSERT INTO t(i, j) VALUES (18, '-2147483649'); +INSERT INTO t(i, j) VALUES (19, '18446744073709551615'); +INSERT INTO t(i, j) VALUES (20, '18446744073709551616'); +INSERT INTO t(i, j) VALUES (21, '3.14'); +INSERT INTO t(i, j) VALUES (22, '{}'); +INSERT INTO t(i, j) VALUES (23, '[]'); +INSERT INTO t(i, j) VALUES (24, CAST(CAST('2015-01-15 23:24:25' AS DATETIME) AS JSON)); +INSERT INTO t(i, j) VALUES (25, CAST(CAST('23:24:25' AS TIME) AS JSON)); +INSERT INTO t(i, j) VALUES (26, CAST(CAST('2015-01-15' AS DATE) AS JSON)); +INSERT INTO t(i, j) VALUES (27, CAST(TIMESTAMP('2015-01-15 23:24:25') AS JSON)); +INSERT INTO t(i, j) VALUES (28, CAST('[]' AS CHAR CHARACTER SET 'ascii')); +SELECT i, + (j = '"scalar string"') AS c1, + (j = 'scalar string') AS c2, + (j = CAST('"scalar string"' AS JSON)) AS c3, + (j = CAST(CAST(j AS CHAR CHARACTER SET 'utf8mb4') AS JSON)) AS c4, + (j = CAST(NULL AS JSON)) AS c5, + (j = NULL) AS c6, + (j <=> NULL) AS c7, + (j <=> CAST(NULL AS JSON)) AS c8, + (j IN (-1, 2, 32768, 3.14)) AS c9, + (j IN (CAST('[1, 2]' AS JSON), CAST('{}' AS JSON), CAST(3.14 AS JSON))) AS c10, + (j = (SELECT j FROM t WHERE j = CAST('null' AS JSON))) AS c11, + (j = (SELECT j FROM t WHERE j IS NULL)) AS c12, + (j = (SELECT j FROM t WHERE 1<>1)) AS c13, + (j = DATE('2015-01-15')) AS c14, + (j = TIME('23:24:25')) AS c15, + (j = TIMESTAMP('2015-01-15 23:24:25')) AS c16, + (j = CURRENT_TIMESTAMP) AS c17, + (JSON_EXTRACT(j, '$.a') = 2) AS c18 + FROM t + ORDER BY i; +select coalesce(NULL), coalesce(NULL, NULL), coalesce(NULL, NULL, NULL); +select coalesce(cast(1 as json), cast(2 as json)); +select coalesce(NULL, cast(2 as json)); +select coalesce(cast(1 as json), NULL); +select coalesce(NULL, NULL); +drop table if exists t2; +create table t2(a int, b double, c datetime, d time, e char(20), f bit(10)); +insert into t2 values(1, 1.1, "2017-08-01 12:01:01", "12:01:01", "abcdef", 0b10101); +select coalesce(NULL, a), coalesce(NULL, b, a), coalesce(c, NULL, a, b), coalesce(d, NULL), coalesce(d, c), coalesce(NULL, NULL, e, 1), coalesce(f), coalesce(1, a, b, c, d, e, f) from t2; +SELECT NULLIF(NULL, 1), NULLIF(1, NULL), NULLIF(1, 1), NULLIF(NULL, NULL); +SELECT NULLIF(1, 1.0), NULLIF(1, "1.0"); +SELECT NULLIF("abc", 1); +SELECT NULLIF(1+2, 1); +SELECT NULLIF(1, 1+2); +SELECT NULLIF(2+3, 1+2); +SELECT HEX(NULLIF("abc", 1)); +drop table if exists t; +create table t(a date); +desc select a = a from t; +select interval(null, 1, 2), interval(1, 2, 3), interval(2, 1, 3); +select interval(3, 1, 2), interval(0, "b", "1", "2"), interval("a", "b", "1", "2"); +select interval(23, 1, 23, 23, 23, 30, 44, 200), interval(23, 1.7, 15.3, 23.1, 30, 44, 200), interval(9007199254740992, 9007199254740993); +select interval(cast(9223372036854775808 as unsigned), cast(9223372036854775809 as unsigned)), interval(9223372036854775807, cast(9223372036854775808 as unsigned)), interval(-9223372036854775807, cast(9223372036854775808 as unsigned)); +select interval(cast(9223372036854775806 as unsigned), 9223372036854775807), interval(cast(9223372036854775806 as unsigned), -9223372036854775807), interval("9007199254740991", "9007199254740992"); +select interval(9007199254740992, "9007199254740993"), interval("9007199254740992", 9007199254740993), interval("9007199254740992", "9007199254740993"); +select INTERVAL(100, NULL, NULL, NULL, NULL, NULL, 100); +SELECT INTERVAL(0,(1*5)/2) + INTERVAL(5,4,3); +select greatest(1, 2, 3), greatest("a", "b", "c"), greatest(1.1, 1.2, 1.3), greatest("123a", 1, 2); +show warnings; +select greatest(cast("2017-01-01" as datetime), "123", "234", cast("2018-01-01" as date)), greatest(cast("2017-01-01" as date), "123", null); +show warnings; +select least(1, 2, 3), least("a", "b", "c"), least(1.1, 1.2, 1.3), least("123a", 1, 2); +show warnings; +select least(cast("2017-01-01" as datetime), "123", "234", cast("2018-01-01" as date)), least(cast("2017-01-01" as date), "123", null); +show warnings; +select 1 < 17666000000000000000, 1 > 17666000000000000000, 1 = 17666000000000000000; +drop table if exists t; +set time_zone = '+00:00'; +create table t(a timestamp); +insert into t value('1991-05-06 04:59:28'); +set time_zone='Asia/Shanghai'; +select * from t; +set time_zone = 'America/Los_Angeles'; +-- error 1292 +insert into t value('2011-03-13 02:00:00'); +set time_zone = '+08:00'; +select * from t; +drop table if exists t; +create table t(a bigint unsigned); +insert into t value(17666000000000000000); +select * from t where a = 17666000000000000000; +select row(1,2,3)=row(1,2,3); +select row(1,2,3)=row(1+3,2,3); +select row(1,2,3)<>row(1,2,3); +select row(1,2,3)<>row(1+3,2,3); +select row(1+3,2,3)<>row(1+3,2,3); +set time_zone=default + +# TestNullifWithIsNull +# issue 23157: make sure if Nullif expr is correct combined with IsNull expr. +drop table if exists t; +create table t(a int not null); +insert into t values(1),(2); +select * from t where nullif(a,a) is null; + +# TestAggregationBuiltin +drop table if exists t; +create table t(a decimal(7, 6)); +insert into t values(1.123456), (1.123456); +select avg(a) from t; +drop table t; +CREATE TABLE `t` ( `a` int, KEY `idx_a` (`a`)); +select avg(a) from t; +select max(a), min(a) from t; +select distinct a from t; +select sum(a) from t; +select count(a) from t; +select bit_or(a) from t; +select bit_xor(a) from t; +select bit_and(a) from t; +select count(1) from (select count(1) from t) as t1; + +# TestAggregationBuiltinBitOr +drop table if exists t; +create table t(a bigint); +insert into t values(null); +select bit_or(a) from t; +insert into t values(1); +select bit_or(a) from t; +insert into t values(2); +select bit_or(a) from t; +insert into t values(4); +select bit_or(a) from t; +select a, bit_or(a) from t group by a order by a; +insert into t values(-1); +select bit_or(a) from t; + +# TestAggregationBuiltinBitXor +drop table if exists t; +create table t(a bigint); +insert into t values(null); +select bit_xor(a) from t; +insert into t values(1); +select bit_xor(a) from t; +insert into t values(2); +select bit_xor(a) from t; +insert into t values(3); +select bit_xor(a) from t; +insert into t values(3); +select bit_xor(a) from t; +select a, bit_xor(a) from t group by a order by a; + +# TestAggregationBuiltinBitAnd +drop table if exists t; +create table t(a bigint); +insert into t values(null); +select bit_and(a) from t; +insert into t values(7); +select bit_and(a) from t; +insert into t values(5); +select bit_and(a) from t; +insert into t values(3); +select bit_and(a) from t; +insert into t values(2); +select bit_and(a) from t; +select a, bit_and(a) from t group by a order by a desc; + +# TestAggregationBuiltinGroupConcat +drop table if exists t, d; +create table t(a varchar(100)); +create table d(a varchar(100)); +insert into t values('hello'), ('hello'); +select group_concat(a) from t; +set @@group_concat_max_len=7; +select group_concat(a) from t; +show warnings; +-- error 1260 +insert into d select group_concat(a) from t; +set sql_mode=''; +insert into d select group_concat(a) from t; +show warnings; +select * from d; +set group_concat_max_len=default; +set sql_mode=default; + +# TestOtherBuiltin +drop table if exists t; +create table t(a int, b double, c varchar(20), d datetime, e time); +insert into t value(1, 2, 'string', '2017-01-01 12:12:12', '12:12:12'); +select 1 in (a, b, c), 'string' in (a, b, c), '2017-01-01 12:12:12' in (c, d, e), '12:12:12' in (c, d, e) from t; +select 1 in (null, c), 2 in (null, c) from t; +select 0 in (a, b, c), 0 in (a, b, c), 3 in (a, b, c), 4 in (a, b, c) from t; +select (0,1) in ((0,1), (0,2)), (0,1) in ((0,0), (0,2)); +select bit_count(121), bit_count(-1), bit_count(null), bit_count("1231aaa"); +drop table if exists t; +create table t(a int primary key, b time, c double, d varchar(10)); +insert into t values(1, '01:01:01', 1.1, "1"), (2, '02:02:02', 2.2, "2"); +insert into t(a, b) values(1, '12:12:12') on duplicate key update a = values(b); +select a from t order by a; +insert into t values(2, '12:12:12', 1.1, "3.3") on duplicate key update a = values(c) + values(d); +select a from t order by a; +set @varname = "Abc"; +select @varname, @VARNAME; +drop table t; +CREATE TABLE `t` (`id` varchar(32) NOT NULL, `count` decimal(18,2), PRIMARY KEY (`id`)); +INSERT INTO t (id,count)VALUES('abc',2) ON DUPLICATE KEY UPDATE count=if(VALUES(count) > count,VALUES(count),count); +select count from t where id = 'abc'; +INSERT INTO t (id,count)VALUES('abc',265.0) ON DUPLICATE KEY UPDATE count=if(VALUES(count) > count,VALUES(count),count); +select count from t where id = 'abc'; +drop table if exists t; +create table test(id int not null, val text, primary key(id)); +insert into test values(1,'hello'); +select * from test; +insert into test values(1, NULL) on duplicate key update val = VALUES(val); +select * from test; +drop table if exists test; +create table test( + id int not null, + a text, + b blob, + c varchar(20), + d int, + e float, + f DECIMAL(6,4), + g JSON, + primary key(id)); +insert into test values(1,'txt hello', 'blb hello', 'vc hello', 1, 1.1, 1.0, '{"key1": "value1", "key2": "value2"}'); +insert into test values(1, NULL, NULL, NULL, NULL, NULL, NULL, NULL) + on duplicate key update + a = values(a), + b = values(b), + c = values(c), + d = values(d), + e = values(e), + f = values(f), + g = values(g); +select * from test; + +# TestDateBuiltin +DROP TABLE IF EXISTS t; +create table t (d date); +insert into t values ('1997-01-02'); +insert into t values ('1998-01-02'); +select * from t where d < date '1998-01-01'; +select date'20171212'; +select date'2017/12/12'; +select date'2017/12-12'; +set sql_mode = ''; +select date '0000-00-00'; +set sql_mode = 'NO_ZERO_IN_DATE'; +select date '0000-00-00'; +set sql_mode = 'NO_ZERO_DATE'; +-- error 1292 +select date '0000-00-00'; +set sql_mode = ''; +select date '2007-10-00'; +set sql_mode = 'NO_ZERO_IN_DATE'; +-- error 1292 +select date '2007-10-00'; +set sql_mode = 'NO_ZERO_DATE'; +select date '2007-10-00'; +set sql_mode = 'NO_ZERO_IN_DATE,NO_ZERO_DATE'; +-- error 1292 +select date '2007-10-00'; +-- error 1292 +select date '0000-00-00'; +select date'1998~01~02'; +select date'731124', date '011124'; +-- error 1292 +select date '0000-00-00 00:00:00'; +-- error 1292 +select date '2017-99-99'; +-- error 1292 +select date '2017-2-31'; +-- error 1292 +select date '201712-31'; +-- error 1292 +select date 'abcdefg'; +set sql_mode = default; + +# TestApproximatePercentile +drop table if exists t; +create table t (a bit(10)); +insert into t values(b'1111'); +select approx_percentile(a, 10) from t; + +# TestFuncNameConst +DROP TABLE IF EXISTS t; +CREATE TABLE t(a CHAR(20), b VARCHAR(20), c BIGINT); +INSERT INTO t (b, c) values('hello', 1); +SELECT name_const('test_int', 1), name_const('test_float', 3.1415); +SELECT name_const('test_string', 'hello'), name_const('test_nil', null); +SELECT name_const('test_string', 1) + c FROM t; +SELECT concat('hello', name_const('test_string', 'world')) FROM t; +SELECT NAME_CONST('come', -1); +SELECT NAME_CONST('come', -1.0); +-- error 1210 +select name_const(a,b) from t; +-- error 1210 +select name_const(a,"hello") from t; +-- error 1210 +select name_const("hello", b) from t; +-- error 1210 +select name_const("hello", 1+1) from t; +-- error 1210 +select name_const(concat('a', 'b'), 555) from t; +-- error 1210 +select name_const(555) from t; +select name_const("hello", 1); diff --git a/tests/integrationtest/t/expression/cast.test b/tests/integrationtest/t/expression/cast.test new file mode 100644 index 0000000000000..37f9a35650968 --- /dev/null +++ b/tests/integrationtest/t/expression/cast.test @@ -0,0 +1,50 @@ +# TestCastStrToInt +select cast('' as signed); +show warnings; +select cast('12345abcde' as signed); +show warnings; +select cast('123e456' as signed); +show warnings; +select cast('-12345abcde' as signed); +show warnings; +select cast('-123e456' as signed); +show warnings; + +# TestCastCoer +select coercibility(binary('a')); +select coercibility(cast('a' as char(10))); +select coercibility(convert('abc', char(10))); + +# TestCastRealAsTime +drop table if exists t; +create table t(d1 double, f float, d2 decimal(24,8)); +insert into t values(0, 0, 0); +select cast(111.1 as datetime) from t; +select cast(1311.1 as datetime) from t; +insert into t values(111.1, 1122.1, 31212.111); +insert into t values(121212.1111, 1121212.111111, 11121212.111111); +insert into t values(99991111.1111111, 101.1111111, 20121212121212.1111111); +insert into t values(NULL, NULL, NULL); +insert into t values(1.1, 48.1, 100.1); +insert into t values(1301.11, 1131.111, 100001111.111); +insert into t values(20121212121260.1111111, 20121212126012.1111111, 20121212241212.1111111); +-- sorted_result +select cast(d1 as datetime), cast(f as datetime), cast(d2 as datetime) from t; + +# TestCastAsTime +drop table if exists t; +create table t (col1 bigint, col2 double, col3 decimal, col4 varchar(20), col5 json); +insert into t values (1, 1, 1, "1", "1"); +insert into t values (null, null, null, null, null); +select cast(col1 as time), cast(col2 as time), cast(col3 as time), cast(col4 as time), cast(col5 as time) from t where col1 = 1; +select cast(col1 as time), cast(col2 as time), cast(col3 as time), cast(col4 as time), cast(col5 as time) from t where col1 is null; +-- error 1426 +select cast(col1 as time(31)) from t where col1 is null; +-- error 1426 +select cast(col2 as time(31)) from t where col1 is null; +-- error 1426 +select cast(col3 as time(31)) from t where col1 is null; +-- error 1426 +select cast(col4 as time(31)) from t where col1 is null; +-- error 1426 +select cast(col5 as time(31)) from t where col1 is null; diff --git a/tests/integrationtest/t/expression/issues.test b/tests/integrationtest/t/expression/issues.test index 3cb8f2ebb0a71..70d1e8e034aa9 100644 --- a/tests/integrationtest/t/expression/issues.test +++ b/tests/integrationtest/t/expression/issues.test @@ -1767,3 +1767,34 @@ select `col_float_key_signed` , `col_float_key_signed` % `col_float_key_signed` -- sorted_result select `col_float_key_signed` , (`col_float_key_signed` % `col_float_key_signed`) IS FALSE from t1; +# TestRailsFKUsage +# issue https://github.com/pingcap/tidb/issues/26111 +drop table if exists author_addresses, authors; +CREATE TABLE author_addresses ( + id bigint(20) NOT NULL AUTO_INCREMENT, + PRIMARY KEY (id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +CREATE TABLE authors ( + id bigint(20) NOT NULL AUTO_INCREMENT, + name varchar(255) NOT NULL, + author_address_id bigint(20) DEFAULT NULL, + author_address_extra_id bigint(20) DEFAULT NULL, + organization_id varchar(255) DEFAULT NULL, + owned_essay_id varchar(255) DEFAULT NULL, + PRIMARY KEY (id), + KEY index_authors_on_author_address_id (author_address_id), + KEY index_authors_on_author_address_extra_id (author_address_extra_id), + CONSTRAINT fk_rails_94423a17a3 FOREIGN KEY (author_address_id) REFERENCES author_addresses (id) ON UPDATE CASCADE ON DELETE RESTRICT +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; +SELECT fk.referenced_table_name AS 'to_table', + fk.referenced_column_name AS 'primary_key', + fk.column_name AS 'column', + fk.constraint_name AS 'name', + rc.update_rule AS 'on_update', + rc.delete_rule AS 'on_delete' +FROM information_schema.referential_constraints rc +JOIN information_schema.key_column_usage fk +USING (constraint_schema, constraint_name) +WHERE fk.referenced_column_name IS NOT NULL + AND fk.table_schema = database() + AND fk.table_name = 'authors'; diff --git a/tests/integrationtest/t/expression/misc.test b/tests/integrationtest/t/expression/misc.test new file mode 100644 index 0000000000000..60f1d81501267 --- /dev/null +++ b/tests/integrationtest/t/expression/misc.test @@ -0,0 +1,225 @@ +# TestLiterals +SELECT LENGTH(b''), LENGTH(B''), b''+1, b''-1, B''+1; + +# TestTimestampLiteral +select timestamp '2017-01-01 00:00:00'; +select timestamp '2017@01@01 00:00:00'; +select timestamp '2017@01@01 00~00~00'; +select timestamp '2017@01@0001 00~00~00.333'; +-- error 1292 +select timestamp '00:00:00'; +-- error 1292 +select timestamp '1992-01-03'; +-- error 1292 +select timestamp '20171231235959.999999'; + +# TestTimeLiteral +select time '117:01:12'; +select time '01:00:00.999999'; +select time '1 01:00:00'; +select time '110:00:00'; +select time'-1:1:1.123454656'; +select time '33:33'; +select time '1.1'; +select time '21'; +select time '20 20:20'; +-- error 1292 +select time '2017-01-01 00:00:00'; +-- error 1292 +select time '071231235959.999999'; +-- error 1292 +select time '20171231235959.999999'; +select ADDDATE('2008-01-34', -1); +Show warnings; + +# TestTwoDecimalTruncate +set sql_mode=''; +drop table if exists t; +create table t1(a decimal(10,5), b decimal(10,1)); +insert into t1 values(123.12345, 123.12345); +update t1 set b = a; +select a, b from t1; +select 2.00000000000000000000000000000001 * 1.000000000000000000000000000000000000000000002; +set sql_mode=default; + +# TestDecimalMul +drop table if exists t; +create table t(a decimal(38, 17)); +insert into t select 0.5999991229316*0.918755041726043; +select * from t; + +# TestDecimalDiv +select cast(1 as decimal(60,30)) / cast(1 as decimal(60,30)) / cast(1 as decimal(60, 30)); +select cast(1 as decimal(60,30)) / cast(3 as decimal(60,30)) / cast(7 as decimal(60, 30)); +select cast(1 as decimal(60,30)) / cast(3 as decimal(60,30)) / cast(7 as decimal(60, 30)) / cast(13 as decimal(60, 30)); + +# TestValuesFloat32 +drop table if exists t; +create table t (i int key, j float); +insert into t values (1, 0.01); +select * from t; +insert into t values (1, 0.02) on duplicate key update j = values (j); +select * from t; + +# TestValuesEnum +drop table if exists t; +create table t (a bigint primary key, b enum('a','b','c')); +insert into t values (1, "a"); +select * from t; +insert into t values (1, "b") on duplicate key update b = values(b); +select * from t; + +# TestRefineArgNullValues +drop table if exists t; +drop table if exists s; +create table t(id int primary key, a int); +create table s(a int); +insert into s values(1),(2); +select t.id = 1.234 from t right join s on t.a = s.a; + +# TestComplexShowVariables +# This is an example SHOW VARIABLES from mysql-connector-java-5.1.34 +# It returns 19 rows in MySQL 5.7 (the language sysvar no longer exists in 5.6+) +# and 16 rows in MySQL 8.0 (the aliases for tx_isolation is removed, along with query cache) +# In the event that we hide noop sysvars in future, we must keep these variables. +SHOW VARIABLES WHERE Variable_name ='language' OR Variable_name = 'net_write_timeout' OR Variable_name = 'interactive_timeout' +OR Variable_name = 'wait_timeout' OR Variable_name = 'character_set_client' OR Variable_name = 'character_set_connection' +OR Variable_name = 'character_set' OR Variable_name = 'character_set_server' OR Variable_name = 'tx_isolation' +OR Variable_name = 'transaction_isolation' OR Variable_name = 'character_set_results' OR Variable_name = 'timezone' +OR Variable_name = 'time_zone' OR Variable_name = 'system_time_zone' +OR Variable_name = 'lower_case_table_names' OR Variable_name = 'max_allowed_packet' OR Variable_name = 'net_buffer_length' +OR Variable_name = 'sql_mode' OR Variable_name = 'query_cache_type' OR Variable_name = 'query_cache_size' +OR Variable_name = 'license' OR Variable_name = 'init_connect'; + +# TestFloat64Inf +select '1e800' + 1e100; +select '-1e800' - 1e100; + +# TestIdentity +drop table if exists identity; +create table identity (id int not null primary key auto_increment); +SELECT @@identity; +INSERT INTO identity VALUES (NULL); +SELECT @@identity, LAST_INSERT_ID(); +INSERT INTO identity VALUES (NULL); +SELECT @@identity, LAST_INSERT_ID(); +INSERT INTO identity VALUES (NULL); +SELECT @@identity, LAST_INSERT_ID(); + +# TestLastInsertId +drop table if exists lastinsertid; +create table lastinsertid (id int not null primary key auto_increment); +SELECT @@last_insert_id; +INSERT INTO lastinsertid VALUES (NULL); +SELECT @@last_insert_id, LAST_INSERT_ID(); +INSERT INTO lastinsertid VALUES (NULL); +SELECT @@last_insert_id, LAST_INSERT_ID(); +INSERT INTO lastinsertid VALUES (NULL); +SELECT @@last_insert_id, LAST_INSERT_ID(); + +# TestVirtualGeneratedColumnAndLimit +drop table if exists t; +create table t (a int, b int as (a + 1)); +insert into t(a) values (1); +select /*+ LIMIT_TO_COP() */ b from t limit 1; +select /*+ LIMIT_TO_COP() */ b from t order by b limit 1; + +# TestNegativeZeroForHashJoin +drop table if exists t0, t1; +CREATE TABLE t0(c0 float); +CREATE TABLE t1(c0 float); +INSERT INTO t1(c0) VALUES (0); +INSERT INTO t0(c0) VALUES (0); +SELECT t1.c0 FROM t1, t0 WHERE t0.c0=-t1.c0; + +# TestFuncCaseWithLeftJoin +drop table if exists kankan1, kankan2; +create table kankan1(id int, name text); +insert into kankan1 values(1, 'a'); +insert into kankan1 values(2, 'a'); +create table kankan2(id int, h1 text); +insert into kankan2 values(2, 'z'); +select t1.id from kankan1 t1 left join kankan2 t2 on t1.id = t2.id where (case when t1.name='b' then 'case2' when t1.name='a' then 'case1' else NULL end) = 'case1' order by t1.id; + +# TestConvertToBit +drop table if exists t, t1; +create table t (a bit(64)); +create table t1 (a varchar(2)); +insert t1 value ('10'); +insert t select a from t1; +select a+0 from t; +drop table if exists t, t1; +create table t (a bit(64)); +create table t1 (a binary(2)); +insert t1 value ('10'); +insert t select a from t1; +select a+0 from t; +drop table if exists t, t1; +create table t (a bit(64)); +create table t1 (a datetime); +insert t1 value ('09-01-01'); +insert t select a from t1; +select a+0 from t; + +# TestUnknowHintIgnore +drop table if exists t; +create table t(a int); +select /*+ unknown_hint(c1)*/ 1; +show warnings; +select 1 from /*+ test1() */ t; +show warnings; + +# TestValuesInNonInsertStmt +drop table if exists t; +create table t(a bigint, b double, c decimal, d varchar(20), e datetime, f time, g json); +insert into t values(1, 1.1, 2.2, "abc", "2018-10-24", NOW(), "12"); +select values(a), values(b), values(c), values(d), values(e), values(f), values(g) from t; + +# TestJiraSetInnoDBDefaultRowFormat +set global innodb_default_row_format = dynamic; +set global innodb_default_row_format = 'dynamic'; +SHOW VARIABLES LIKE 'innodb_default_row_format'; +SHOW VARIABLES LIKE 'character_set_server'; +SHOW VARIABLES LIKE 'innodb_file_format'; +SHOW VARIABLES LIKE 'innodb_large_prefix'; + +# TestIfNullParamMarker +drop table if exists t; +create table t (c1 varchar(100), c2 varchar(128)); +prepare pr1 from "insert into t values(ifnull(?,' '),ifnull(?,' '))"; +set @a='1',@b=repeat('x', 80); +execute pr1 using @a,@b; + +# TestNullValueRange +drop table if exists t; +create table t(a int, b int, index(a)); +insert into t values (null, 0), (null, 1), (10, 11), (10, 12); +select * from t use index(a) where a is null order by b; +select * from t use index(a) where a<=>null order by b; +select * from t use index(a) where a<=>10 order by b; +drop table if exists t1; +create table t1(a int, b int, c int, unique key(a, b, c)); +insert into t1 values (1, null, 1), (1, null, 2), (1, null, 3), (1, null, 4); +insert into t1 values (1, 1, 1), (1, 2, 2), (1, 3, 33), (1, 4, 44); +select c from t1 where a=1 and b<=>null and c>2 order by c; +select c from t1 where a=1 and b is null and c>2 order by c; +select c from t1 where a=1 and b is not null and c>2 order by c; + +# TestPartitionPruningRelaxOP +# Discovered while looking at issue 19941 (not completely related) +# relaxOP relax the op > to >= and < to <= +# Sometime we need to relax the condition, for example: +# col < const => f(col) <= const +# datetime < 2020-02-11 16:18:42 => to_days(datetime) <= to_days(2020-02-11) +# We can't say: +# datetime < 2020-02-11 16:18:42 => to_days(datetime) < to_days(2020-02-11) +DROP TABLE IF EXISTS t1; +CREATE TABLE t1 (d date NOT NULL) PARTITION BY RANGE (YEAR(d)) + (PARTITION p2016 VALUES LESS THAN (2017), PARTITION p2017 VALUES LESS THAN (2018), PARTITION p2018 VALUES LESS THAN (2019), + PARTITION p2019 VALUES LESS THAN (2020), PARTITION pmax VALUES LESS THAN MAXVALUE); +INSERT INTO t1 VALUES ('2016-01-01'), ('2016-06-01'), ('2016-09-01'), ('2017-01-01'), +('2017-06-01'), ('2017-09-01'), ('2018-01-01'), ('2018-06-01'), ('2018-09-01'), ('2018-10-01'), +('2018-11-01'), ('2018-12-01'), ('2018-12-31'), ('2019-01-01'), ('2019-06-01'), ('2019-09-01'), +('2020-01-01'), ('2020-06-01'), ('2020-09-01'); +SELECT COUNT(*) FROM t1 WHERE d < '2018-01-01'; +SELECT COUNT(*) FROM t1 WHERE d > '2018-01-01'; diff --git a/tests/integrationtest/t/expression/plan_cache.test b/tests/integrationtest/t/expression/plan_cache.test new file mode 100644 index 0000000000000..232c614760196 --- /dev/null +++ b/tests/integrationtest/t/expression/plan_cache.test @@ -0,0 +1,40 @@ +# TestInMeetsPrepareAndExecute +prepare pr1 from 'select ? in (1,?,?)'; +set @a=1, @b=2, @c=3; +execute pr1 using @a,@b,@c; +prepare pr2 from 'select 3 in (1,?,?)'; +set @a=2, @b=3; +execute pr2 using @a,@b; +prepare pr3 from 'select ? in (1,2,3)'; +set @a=4; +execute pr3 using @a; +prepare pr4 from 'select ? in (?,?,?)'; +set @a=1, @b=2, @c=3, @d=4; +execute pr4 using @a,@b,@c,@d; + +# TestOrderByFuncPlanCache +drop table if exists t; +create table t(a int); +prepare stmt from 'SELECT * FROM t order by rand()'; +execute stmt; +prepare stmt from 'SELECT * FROM t order by now()'; +execute stmt; + +# TestSelectLimitPlanCache +drop table if exists t; +create table t(a int); +insert into t values(1), (2), (3); +set @@session.sql_select_limit = 1; +prepare stmt from 'SELECT * FROM t'; +execute stmt; +set @@session.sql_select_limit = default; +execute stmt; +set @@session.sql_select_limit = 2; +execute stmt; +set @@session.sql_select_limit = 1; +execute stmt; +set @@session.sql_select_limit = default; +execute stmt; +set @@session.sql_select_limit = 2; +execute stmt; +set sql_select_limit=default; diff --git a/tests/integrationtest/t/expression/time.test b/tests/integrationtest/t/expression/time.test new file mode 100644 index 0000000000000..9415338841766 --- /dev/null +++ b/tests/integrationtest/t/expression/time.test @@ -0,0 +1,210 @@ +# TestDaynameArithmetic +select dayname("1962-03-01")+0; +select dayname("1962-03-02")+0; +select dayname("1962-03-03")+0; +select dayname("1962-03-04")+0; +select dayname("1962-03-05")+0; +select dayname("1962-03-06")+0; +select dayname("1962-03-07")+0; +select dayname("1962-03-08")+0; +select dayname("1962-03-01")+1; +select dayname("1962-03-01")+2; +select dayname("1962-03-01")+3; +select dayname("1962-03-01")+4; +select dayname("1962-03-01")+5; +select dayname("1962-03-01")+6; +select dayname("1962-03-01")+7; +select dayname("1962-03-01")+2333; +select dayname("1962-03-01")+2.333; +select dayname("1962-03-01")>2; +select dayname("1962-03-01")<2; +select dayname("1962-03-01")=3; +select dayname("1962-03-01")!=3; +select dayname("1962-03-01")<4; +select dayname("1962-03-01")>4; +select !dayname("1962-03-01"); +select dayname("1962-03-01")&1; +select dayname("1962-03-01")&3; +select dayname("1962-03-01")&7; +select dayname("1962-03-01")|1; +select dayname("1962-03-01")|3; +select dayname("1962-03-01")|7; +select dayname("1962-03-01")^1; +select dayname("1962-03-01")^3; +select dayname("1962-03-01")^7; + +# TestTimestampDatumEncode +drop table if exists t; +create table t (a bigint primary key, b timestamp); +insert into t values (1, "2019-04-29 11:56:12"); +explain format = 'brief' select * from t where b = (select max(b) from t); +select * from t where b = (select max(b) from t); + +# TestDateTimeAddReal +SELECT "1900-01-01 00:00:00" + INTERVAL 1.123456789e3 SECOND; +SELECT 19000101000000 + INTERVAL 1.123456789e3 SECOND; +select date("1900-01-01") + interval 1.123456789e3 second; +SELECT "1900-01-01 00:18:43.456789" - INTERVAL 1.123456789e3 SECOND; +SELECT 19000101001843.456789 - INTERVAL 1.123456789e3 SECOND; +SELECT 19000101000000.0005 + INTERVAL 0.0005 SECOND; +select date("1900-01-01") - interval 1.123456789e3 second; +select 19000101000000 - interval 1.123456789e3 second; + +# TestDecimalConvertToTime +# for issue #9770 +drop table if exists t; +create table t(a datetime(6), b timestamp); +insert t values (20010101100000.123456, 20110707101112.123456); +select * from t; + +# TestDateAddForNonExistingTimestamp +set time_zone = 'CET'; +drop table if exists t; +create table t(ts timestamp); +set time_zone = 'UTC'; +insert into t values('2022-03-27 00:30:00'); +insert into t values('2022-10-30 00:30:00'); +insert into t values('2022-10-30 01:30:00'); +set time_zone = 'Europe/Amsterdam'; +-- error 1292 +insert into t values('2022-03-27 02:30:00'); +select date_add(ts, interval 1 hour) from t order by ts; +set time_zone = default; + +# TestTimestampAddWithFractionalSecond +drop table if exists t; +create table t(a date); +insert into t values ('2021-08-20'); +select timestampadd(microsecond, 1, a) from t; +select timestampadd(second, 6/4, a) from t; +select timestampadd(second, 9.9999e2, a) from t; +select timestampadd(second, 1, '2021-08-20 00:00:01.0001'); +select timestampadd(minute, 1.5, '2021-08-20 00:00:00'); +select timestampadd(minute, 1.5, '2021-08-20 00:00:00.0001'); +SELECT timestampadd(year,1.212208e+308,'1995-01-05 06:32:20.859724') as result; +show warnings; + +# TestDatetimeMicrosecond +# for int +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 SECOND_MICROSECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 MINUTE_MICROSECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 HOUR_MICROSECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 DAY_MICROSECOND); +# for decimal +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR_MINUTE); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MINUTE_SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR_MONTH); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_HOUR); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_MINUTE); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR_SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 QUARTER); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MONTH); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 WEEK); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MINUTE); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MICROSECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_MINUTE); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE_SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 YEAR_MONTH); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_HOUR); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_MINUTE); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 YEAR); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 QUARTER); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MONTH); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 WEEK); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MICROSECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR_MINUTE); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MINUTE_SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" YEAR_MONTH); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_HOUR); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_MINUTE); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR_SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" YEAR); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" QUARTER); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MONTH); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" WEEK); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MINUTE); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MICROSECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR_MINUTE); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MINUTE_SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" YEAR_MONTH); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_HOUR); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_MINUTE); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR_SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.+2" SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.*2" SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2./2" SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.a2" SECOND); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" YEAR); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" QUARTER); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MONTH); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" WEEK); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MINUTE); +select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MICROSECOND); +# select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 SECOND); + +# TestExprDateTimeOnDST +set @@session.time_zone = 'Europe/Amsterdam'; +drop table if exists t; +create table t (id int, dt datetime, primary key (id, dt)); +insert into t values (1, date_add('2023-03-26 00:00:00', interval 2 hour)); +insert into t values (4,'2023-03-26 02:00:00'); +select * from t; +set time_zone = default; + +# TestGreatestTimeType +drop table if exists t1; +create table t1(c_time time(5), c_dt datetime(4), c_ts timestamp(3), c_d date, c_str varchar(100)); +insert into t1 values('-800:10:10', '2021-10-10 10:10:10.1234', '2021-10-10 10:10:10.1234', '2021-10-11', '2021-10-10 10:10:10.1234'); +set @@tidb_enable_vectorized_expression = off; +select greatest(c_time, c_time) from t1; +select greatest(c_dt, c_dt) from t1; +select greatest(c_ts, c_ts) from t1; +select greatest(c_d, c_d) from t1; +select greatest(c_str, c_str) from t1; +select least(c_time, c_time) from t1; +select least(c_dt, c_dt) from t1; +select least(c_ts, c_ts) from t1; +select least(c_d, c_d) from t1; +select least(c_str, c_str) from t1; +select greatest(c_time, cast('10:01:01' as time)) from t1; +select least(c_time, cast('10:01:01' as time)) from t1; +select greatest(c_d, cast('1999-10-10' as date)) from t1; +select least(c_d, cast('1999-10-10' as date)) from t1; +select greatest(c_dt, cast('1999-10-10 10:10:10.1234' as datetime)) from t1; +select least(c_dt, cast('1999-10-10 10:10:10.1234' as datetime)) from t1; +set @@tidb_enable_vectorized_expression = on; +select greatest(c_time, c_time) from t1; +select greatest(c_dt, c_dt) from t1; +select greatest(c_ts, c_ts) from t1; +select greatest(c_d, c_d) from t1; +select greatest(c_str, c_str) from t1; +select least(c_time, c_time) from t1; +select least(c_dt, c_dt) from t1; +select least(c_ts, c_ts) from t1; +select least(c_d, c_d) from t1; +select least(c_str, c_str) from t1; +select greatest(c_time, cast('10:01:01' as time)) from t1; +select least(c_time, cast('10:01:01' as time)) from t1; +select greatest(c_d, cast('1999-10-10' as date)) from t1; +select least(c_d, cast('1999-10-10' as date)) from t1; +select greatest(c_dt, cast('1999-10-10 10:10:10.1234' as datetime)) from t1; +select least(c_dt, cast('1999-10-10 10:10:10.1234' as datetime)) from t1; +set @@tidb_enable_vectorized_expression = default;