Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: Fix unexpected panic when using IF function. (#21132) #21711

Merged
merged 6 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7282,6 +7282,75 @@ func (s *testIntegrationSuite) TestIssue20860(c *C) {
c.Assert(tk.ExecToErr("update t set d = adddate(d, interval 1 day) where id < 10"), NotNil)
}

<<<<<<< HEAD
Copy link
Contributor

Choose a reason for hiding this comment

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

conflicts

=======
func (s *testIntegrationSerialSuite) TestIssue20608(c *C) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)
tk := testkit.NewTestKit(c, s.store)
tk.MustQuery("select '䇇Հ' collate utf8mb4_bin like '___Հ';").Check(testkit.Rows("0"))
}

func (s *testIntegrationSuite2) TestIssue15847(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop view if exists t15847")
tk.MustExec("CREATE VIEW t15847(c0) AS SELECT NULL;")
tk.MustQuery("SELECT * FROM t15847 WHERE (NOT (IF(t15847.c0, NULL, NULL)));").Check(testkit.Rows())
tk.MustExec("drop view if exists t15847")
}

func (s *testIntegrationSerialSuite) TestIssue20161(c *C) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)

tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test;`)
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(raw JSON);`)
tk.MustExec(`insert into t(raw) values('["a","ab"]'), ('["a"]'), (null);`)
tk.MustQuery(`SELECT JSON_SEARCH(raw,'one','c') FROM t;`).
Check(testkit.Rows("<nil>", "<nil>", "<nil>"))
}

func (s *testIntegrationSuite) TestIssue10462(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustQuery("select json_array(true)").Check(testkit.Rows("[true]"))
tk.MustQuery("select json_array(1=2)").Check(testkit.Rows("[false]"))
tk.MustQuery("select json_array(1!=2)").Check(testkit.Rows("[true]"))
tk.MustQuery("select json_array(1<2)").Check(testkit.Rows("[true]"))
tk.MustQuery("select json_array(1<=2)").Check(testkit.Rows("[true]"))
tk.MustQuery("select json_array(1>2)").Check(testkit.Rows("[false]"))
tk.MustQuery("select json_array(1>=2)").Check(testkit.Rows("[false]"))
tk.MustQuery("select json_object(true, null <=> null)").Check(testkit.Rows("{\"1\": true}"))
tk.MustQuery("select json_object(false, 1 and 2)").Check(testkit.Rows("{\"0\": true}"))
tk.MustQuery("select json_object(false, 1 and 0)").Check(testkit.Rows("{\"0\": false}"))
tk.MustQuery("select json_object(false, 1 or 0)").Check(testkit.Rows("{\"0\": true}"))
tk.MustQuery("select json_object(false, 1 xor 0)").Check(testkit.Rows("{\"0\": true}"))
tk.MustQuery("select json_object(false, 1 xor 1)").Check(testkit.Rows("{\"0\": false}"))
tk.MustQuery("select json_object(false, not 1)").Check(testkit.Rows("{\"0\": false}"))
tk.MustQuery("select json_array(null and 1)").Check(testkit.Rows("[null]"))
tk.MustQuery("select json_array(null and 0)").Check(testkit.Rows("[false]"))
tk.MustQuery("select json_array(null or 1)").Check(testkit.Rows("[true]"))
tk.MustQuery("select json_array(null or 0)").Check(testkit.Rows("[null]"))
tk.MustQuery("select json_array(1.15 or 0)").Check(testkit.Rows("[true]"))
tk.MustQuery("select json_array('abc' or 0)").Check(testkit.Rows("[false]"))
tk.MustQuery("select json_array('1abc' or 0)").Check(testkit.Rows("[true]"))
tk.MustQuery("select json_array(null is true)").Check(testkit.Rows("[false]"))
tk.MustQuery("select json_array(null is null)").Check(testkit.Rows("[true]"))
tk.MustQuery("select json_array(1 in (1, 2))").Check(testkit.Rows("[true]"))
tk.MustQuery("select json_array(0 in (1, 2))").Check(testkit.Rows("[false]"))
tk.MustQuery("select json_array(0 not in (1, 2))").Check(testkit.Rows("[true]"))
tk.MustQuery("select json_array(1 between 0 and 2)").Check(testkit.Rows("[true]"))
tk.MustQuery("select json_array(1 not between 0 and 2)").Check(testkit.Rows("[false]"))
tk.MustQuery("select json_array('123' like '123')").Check(testkit.Rows("[true]"))
tk.MustQuery("select json_array('abcdef' rlike 'a.*c.*')").Check(testkit.Rows("[true]"))
tk.MustQuery("select json_array(is_ipv4('127.0.0.1'))").Check(testkit.Rows("[true]"))
tk.MustQuery("select json_array(is_ipv6('1a6b:8888:ff66:77ee:0000:1234:5678:bcde'))").Check(testkit.Rows("[true]"))
}

>>>>>>> 3eaa63d67... expression: Fix unexpected panic when using IF function. (#21132)
func (s *testIntegrationSerialSuite) TestIssue21290(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
8 changes: 7 additions & 1 deletion expression/scalar_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@ func newFunctionImpl(ctx sessionctx.Context, fold int, funcName string, retType
}
funcArgs := make([]Expression, len(args))
copy(funcArgs, args)
typeInferForNull(funcArgs)
switch funcName {
case ast.If, ast.Ifnull, ast.Nullif:
// Do nothing. Because it will call InferType4ControlFuncs.
default:
typeInferForNull(funcArgs)
}

f, err := fc.getFunction(ctx, funcArgs)
if err != nil {
return nil, err
Expand Down
12 changes: 12 additions & 0 deletions expression/typeinfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,7 @@ func (s *testInferTypeSuite) createTestCase4CompareFuncs() []typeInferTestCase {
{"nullif(c_float_d , 123)", mysql.TypeDouble, charset.CharsetBin, mysql.BinaryFlag, 12, types.UnspecifiedLength},
{"nullif(c_double_d , 123)", mysql.TypeDouble, charset.CharsetBin, mysql.BinaryFlag, 22, types.UnspecifiedLength},
{"nullif(c_decimal , 123)", mysql.TypeNewDecimal, charset.CharsetBin, mysql.BinaryFlag, 6, 3},
<<<<<<< HEAD
{"nullif(c_datetime , 123)", mysql.TypeVarchar, charset.CharsetUTF8MB4, 0, 22, types.UnspecifiedLength},
{"nullif(c_time_d , 123)", mysql.TypeVarchar, charset.CharsetUTF8MB4, 0, 10, types.UnspecifiedLength},
{"nullif(c_timestamp_d, 123)", mysql.TypeVarchar, charset.CharsetUTF8MB4, 0, 19, types.UnspecifiedLength},
Expand All @@ -1024,6 +1025,17 @@ func (s *testInferTypeSuite) createTestCase4CompareFuncs() []typeInferTestCase {
{"nullif(c_binary , 123)", mysql.TypeString, charset.CharsetBin, mysql.BinaryFlag, 20, types.UnspecifiedLength}, // TODO: tp should be TypeVarString
{"nullif(c_varbinary , 123)", mysql.TypeVarchar, charset.CharsetBin, mysql.BinaryFlag, 20, types.UnspecifiedLength}, // TODO: tp should be TypeVarString
{"nullif(c_blob_d , 123)", mysql.TypeBlob, charset.CharsetBin, mysql.BinaryFlag, 65535, types.UnspecifiedLength}, // TODO: tp should be TypeVarString
=======
{"nullif(c_datetime , 123)", mysql.TypeDatetime, charset.CharsetBin, mysql.BinaryFlag, 22, 2},
{"nullif(c_time_d , 123)", mysql.TypeDuration, charset.CharsetBin, mysql.BinaryFlag, 10, 0},
{"nullif(c_timestamp_d, 123)", mysql.TypeTimestamp, charset.CharsetBin, mysql.BinaryFlag, 19, 0},
{"nullif(c_char , 123)", mysql.TypeString, charset.CharsetUTF8MB4, 0, 20, types.UnspecifiedLength},
{"nullif(c_varchar , 123)", mysql.TypeVarchar, charset.CharsetUTF8MB4, 0, 20, types.UnspecifiedLength}, // TODO: tp should be TypeVarString
{"nullif(c_text_d , 123)", mysql.TypeBlob, charset.CharsetUTF8MB4, 0, 65535, types.UnspecifiedLength}, // TODO: tp should be TypeMediumBlob
{"nullif(c_binary , 123)", mysql.TypeString, charset.CharsetBin, mysql.BinaryFlag, 20, types.UnspecifiedLength}, // TODO: tp should be TypeVarString
{"nullif(c_varbinary , 123)", mysql.TypeVarchar, charset.CharsetBin, mysql.BinaryFlag, 20, types.UnspecifiedLength}, // TODO: tp should be TypeVarString
{"nullif(c_blob_d , 123)", mysql.TypeBlob, charset.CharsetBin, mysql.BinaryFlag, 65535, types.UnspecifiedLength}, // TODO: tp should be TypeVarString
>>>>>>> 3eaa63d67... expression: Fix unexpected panic when using IF function. (#21132)

{"interval(c_int_d, c_int_d, c_int_d)", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag, mysql.MaxIntWidth, 0},
{"interval(c_int_d, c_float_d, c_double_d)", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag, mysql.MaxIntWidth, 0},
Expand Down