diff --git a/mysql-test/r/func_time.result b/mysql-test/r/func_time.result index fd7c6897574..790cce9f1c1 100644 --- a/mysql-test/r/func_time.result +++ b/mysql-test/r/func_time.result @@ -6,8 +6,8 @@ now()-now() weekday(curdate())-weekday(now()) unix_timestamp()-unix_timestamp(no 0 0 0 from_unixtime(unix_timestamp("1994-03-02 10:11:12")) from_unixtime(unix_timestamp("1994-03-02 10:11:12"),"%Y-%m-%d %h:%i:%s") from_unixtime(unix_timestamp("1994-03-02 10:11:12"))+0 1994-03-02 10:11:12 1994-03-02 10:11:12 19940302101112 -sec_to_time(9001) sec_to_time(9001)+0 time_to_sec("15:12:22") -02:30:01 23001 54742 +sec_to_time(9001) sec_to_time(9001)+0 time_to_sec("15:12:22") sec_to_time(time_to_sec("0:30:47")/6.21) +02:30:01 23001 54742 00:04:57 now()-curdate()*1000000-curtime() 0 strcmp(current_timestamp(),concat(current_date()," ",current_time())) diff --git a/mysql-test/t/func_time.test b/mysql-test/t/func_time.test index a91844ab986..cb021d25ae3 100644 --- a/mysql-test/t/func_time.test +++ b/mysql-test/t/func_time.test @@ -7,7 +7,8 @@ select from_days(to_days("960101")),to_days(960201)-to_days("19960101"),to_days( select period_add("9602",-12),period_diff(199505,"9404") ; select now()-now(),weekday(curdate())-weekday(now()),unix_timestamp()-unix_timestamp(now()); select from_unixtime(unix_timestamp("1994-03-02 10:11:12")),from_unixtime(unix_timestamp("1994-03-02 10:11:12"),"%Y-%m-%d %h:%i:%s"),from_unixtime(unix_timestamp("1994-03-02 10:11:12"))+0; -select sec_to_time(9001),sec_to_time(9001)+0,time_to_sec("15:12:22"); +select sec_to_time(9001),sec_to_time(9001)+0,time_to_sec("15:12:22"), + sec_to_time(time_to_sec("0:30:47")/6.21); select now()-curdate()*1000000-curtime(); select strcmp(current_timestamp(),concat(current_date()," ",current_time())); select date_format("1997-01-02 03:04:05", "%M %W %D %Y %y %m %d %h %i %s %w"); diff --git a/sql/item_func.cc b/sql/item_func.cc index 10298ce67f2..1d1a72d35eb 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -309,11 +309,14 @@ double Item_func_div::val() longlong Item_func_div::val_int() { - longlong value=args[0]->val_int(); - longlong val2=args[1]->val_int(); - if ((null_value= val2 == 0 || args[0]->null_value || args[1]->null_value)) - return 0; - return value/val2; + // the integer result of division of two arguments needs to be computed + // as a type-cast division of val(), not as diviion of val_int() of each + // argument. For example, val_int(41.5/3.4) = val_int(12.206) = 12, but + // if you do val_int(41.5)/val_int(3.4), as in the old code, we get 42/3= + // 14, which is wrong. This would break sec_to_time(a/b), + // from_unixtime(a/b), and + // all functions that do val_int() on their arguments + return (longlong)val(); } void Item_func_div::fix_length_and_dec()