From 3507a52e60a20b01dd1d200f5a4f8b601cb0f55b Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 21 Nov 2004 12:04:27 +0300 Subject: [PATCH] A fix and test case for Bug#6297 "prepared statement, wrong handling of IS NULL": we must not only set Item::null_value in Item_param, but implement Item_param::is_null() to work well with IS NULL/IS NOT NULL clauses. mysql-test/r/ps.result: Test case for Bug#6297: test results fixed. mysql-test/t/ps.test: A test case for Bug#6297 "prepared statement, wrong handling of IS NULL" sql/item.h: A fix for Bug#6297: we must not only set null_value in Item_param, but also implement Item_param::is_null() to work well with IS NULL/ IS NOT NULL. Item::is_null() commented. --- mysql-test/r/ps.result | 21 +++++++++++++++++++++ mysql-test/t/ps.test | 14 ++++++++++++++ sql/item.h | 10 ++++++++++ 3 files changed, 45 insertions(+) diff --git a/mysql-test/r/ps.result b/mysql-test/r/ps.result index 6d9cfabb5a7..4a4c8fe22e4 100644 --- a/mysql-test/r/ps.result +++ b/mysql-test/r/ps.result @@ -450,3 +450,24 @@ PREPARE stmt FROM 'UPDATE t1 AS P1 INNER JOIN (SELECT N FROM t1 GROUP BY N HAVIN EXECUTE stmt; DEALLOCATE PREPARE stmt; DROP TABLE t1; +prepare stmt from "select ? is null, ? is not null, ?"; +select @no_such_var is null, @no_such_var is not null, @no_such_var; +@no_such_var is null @no_such_var is not null @no_such_var +1 0 NULL +execute stmt using @no_such_var, @no_such_var, @no_such_var; +? is null ? is not null ? +1 0 NULL +set @var='abc'; +select @var is null, @var is not null, @var; +@var is null @var is not null @var +0 1 abc +execute stmt using @var, @var, @var; +? is null ? is not null ? +0 1 abc +set @var=null; +select @var is null, @var is not null, @var; +@var is null @var is not null @var +1 0 NULL +execute stmt using @var, @var, @var; +? is null ? is not null ? +1 0 NULL diff --git a/mysql-test/t/ps.test b/mysql-test/t/ps.test index 2b3e961fc28..7fe88ad0ddc 100644 --- a/mysql-test/t/ps.test +++ b/mysql-test/t/ps.test @@ -458,3 +458,17 @@ EXECUTE stmt; DEALLOCATE PREPARE stmt; DROP TABLE t1; +# +# Bug#6297 "prepared statement, wrong handling of IS NULL" +# Test that placeholders work with IS NULL/IS NOT NULL clauses. +# +prepare stmt from "select ? is null, ? is not null, ?"; +select @no_such_var is null, @no_such_var is not null, @no_such_var; +execute stmt using @no_such_var, @no_such_var, @no_such_var; +set @var='abc'; +select @var is null, @var is not null, @var; +execute stmt using @var, @var, @var; +set @var=null; +select @var is null, @var is not null, @var; +execute stmt using @var, @var, @var; + diff --git a/sql/item.h b/sql/item.h index 547577a7ee0..ccb0fda1c49 100644 --- a/sql/item.h +++ b/sql/item.h @@ -266,6 +266,14 @@ public: virtual bool get_time(TIME *ltime); virtual bool get_date_result(TIME *ltime,uint fuzzydate) { return get_date(ltime,fuzzydate); } + /* + This function is used only in Item_func_isnull/Item_func_isnotnull + (implementations of IS NULL/IS NOT NULL clauses). Item_func_is{not}null + calls this method instead of one of val/result*() methods, which + normally will set null_value. This allows to determine nullness of + a complex expression without fully evaluating it. + Any new item which can be NULL must implement this call. + */ virtual bool is_null() { return 0; } /* it is "top level" item of WHERE clause and we do not need correct NULL @@ -573,6 +581,8 @@ public: void print(String *str) { str->append('?'); } /* parameter never equal to other parameter of other item */ bool eq(const Item *item, bool binary_cmp) const { return 0; } + bool is_null() + { DBUG_ASSERT(state != NO_VALUE); return state == NULL_VALUE; } }; class Item_int :public Item_num