From 5b3509409bf8196def81a6c43faea98a310137c5 Mon Sep 17 00:00:00 2001 From: Max Hoffman Date: Mon, 7 Apr 2025 11:07:02 -0700 Subject: [PATCH] fix ifnull typing --- server/handler_test.go | 36 +++++++++++++++++++++++++++++++++++- sql/expression/boolean.go | 3 +++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/server/handler_test.go b/server/handler_test.go index 94923244eb..9dae8b9ed4 100644 --- a/server/handler_test.go +++ b/server/handler_test.go @@ -537,7 +537,7 @@ func TestHandlerComPrepareExecuteWithPreparedDisabled(t *testing.T) { for _, test := range []testcase{ { - name: "select statement returns nil schema", + name: "select statement returns nil schema bug", prepare: &mysql.PrepareData{ StatementID: 0, PrepareStmt: "select c1 from test where c1 < ?", @@ -555,6 +555,40 @@ func TestHandlerComPrepareExecuteWithPreparedDisabled(t *testing.T) { {0}, {1}, {2}, {3}, {4}, }, }, + { + name: "ifnull typing", + prepare: &mysql.PrepareData{ + StatementID: 0, + PrepareStmt: "select ifnull(not null, 1000) as a", + ParamsCount: 0, + ParamsType: nil, + ColumnNames: nil, + BindVars: nil, + }, + schema: []*query.Field{ + {Name: "a", OrgName: "a", Table: "", OrgTable: "", Database: "", Type: query.Type_INT16, Charset: uint32(sql.CharacterSet_utf8mb4), ColumnLength: 6, Flags: uint32(query.MySqlFlag_NOT_NULL_FLAG)}, + }, + expected: []sql.Row{ + {1000}, + }, + }, + { + name: "ifnull typing negative", + prepare: &mysql.PrepareData{ + StatementID: 0, + PrepareStmt: "select ifnull(not null, -129) as a", + ParamsCount: 0, + ParamsType: nil, + ColumnNames: nil, + BindVars: nil, + }, + schema: []*query.Field{ + {Name: "a", OrgName: "a", Table: "", OrgTable: "", Database: "", Type: query.Type_INT16, Charset: uint32(sql.CharacterSet_utf8mb4), ColumnLength: 6, Flags: uint32(query.MySqlFlag_NOT_NULL_FLAG)}, + }, + expected: []sql.Row{ + {-129}, + }, + }, } { t.Run(test.name, func(t *testing.T) { handler.ComInitDB(dummyConn, "test") diff --git a/sql/expression/boolean.go b/sql/expression/boolean.go index 0ebd4c2a58..85b8a09d42 100644 --- a/sql/expression/boolean.go +++ b/sql/expression/boolean.go @@ -36,6 +36,9 @@ func NewNot(child sql.Expression) *Not { // Type implements the Expression interface. func (e *Not) Type() sql.Type { + if types.IsNull(e.Child) { + return types.Null + } return types.Boolean }