From ae4141ed75124cc4bbae5fa22662ef0e7f00b122 Mon Sep 17 00:00:00 2001
From: unknown <mhansson/martin@linux-st28.site>
Date: Tue, 9 Oct 2007 14:58:09 +0200
Subject: [PATCH] Bug#31160: MAKETIME() crashes server when returning NULL in
 ORDER BY using filesort

Even though it returns NULL, the MAKETIME function did not have this property set,
causing a failed assertion (designed to catch exactly this).
Fixed by setting the nullability property of MAKETIME().


mysql-test/r/func_sapdb.result:
  Bug#31160: Changed test result.
mysql-test/r/func_time.result:
  Bug#31160: Test result.
mysql-test/t/func_time.test:
  Bug#31160: Test case.
sql/item_timefunc.h:
  Bug#31160: The fix: Initializing maybe_null to true
---
 mysql-test/r/func_sapdb.result |  2 +-
 mysql-test/r/func_time.result  |  9 +++++++++
 mysql-test/t/func_time.test    | 10 ++++++++++
 sql/item_timefunc.h            |  5 ++++-
 4 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/mysql-test/r/func_sapdb.result b/mysql-test/r/func_sapdb.result
index dbae7e551e..e2ed9c98ad 100644
--- a/mysql-test/r/func_sapdb.result
+++ b/mysql-test/r/func_sapdb.result
@@ -196,7 +196,7 @@ f2	datetime	YES		NULL
 f3	time	YES		NULL	
 f4	time	YES		NULL	
 f5	time	YES		NULL	
-f6	time	NO		00:00:00	
+f6	time	YES		NULL	
 f7	datetime	YES		NULL	
 f8	date	YES		NULL	
 f9	time	YES		NULL	
diff --git a/mysql-test/r/func_time.result b/mysql-test/r/func_time.result
index ee8b8c1e90..74859be4d0 100644
--- a/mysql-test/r/func_time.result
+++ b/mysql-test/r/func_time.result
@@ -1027,6 +1027,15 @@ fmtddate	field2
 Sep-4 12:00AM	abcd
 DROP TABLE testBug8868;
 SET NAMES DEFAULT;
+CREATE TABLE t1 (
+a TIMESTAMP
+);
+INSERT INTO t1 VALUES (now()), (now());
+SELECT 1 FROM t1 ORDER BY MAKETIME(1, 1, a);
+1
+1
+1
+DROP TABLE t1;
 (select time_format(timediff(now(), DATE_SUB(now(),INTERVAL 5 DAY)),'%H') As H)
 union
 (select time_format(timediff(now(), DATE_SUB(now(),INTERVAL 5 DAY)),'%H') As H);
diff --git a/mysql-test/t/func_time.test b/mysql-test/t/func_time.test
index 86c848983f..c0a449ac3f 100644
--- a/mysql-test/t/func_time.test
+++ b/mysql-test/t/func_time.test
@@ -545,6 +545,16 @@ DROP TABLE testBug8868;
 
 SET NAMES DEFAULT;
 
+#
+# Bug #31160: MAKETIME() crashes server when returning NULL in ORDER BY using 
+# filesort
+#
+CREATE TABLE t1 (
+  a TIMESTAMP
+);
+INSERT INTO t1 VALUES (now()), (now());
+SELECT 1 FROM t1 ORDER BY MAKETIME(1, 1, a);
+DROP TABLE t1;
 
 #
 # Bug #19844 time_format in Union truncates values
diff --git a/sql/item_timefunc.h b/sql/item_timefunc.h
index 8e925a0156..3e860017d8 100644
--- a/sql/item_timefunc.h
+++ b/sql/item_timefunc.h
@@ -962,7 +962,10 @@ class Item_func_maketime :public Item_str_timefunc
 {
 public:
   Item_func_maketime(Item *a, Item *b, Item *c)
-    :Item_str_timefunc(a, b ,c) {}
+    :Item_str_timefunc(a, b, c) 
+  {
+    maybe_null= TRUE;
+  }
   String *val_str(String *str);
   const char *func_name() const { return "maketime"; }
 };
-- 
2.30.9