Skip to content

Commit

Permalink
fix(tianmu): bit operators and functions problems stoneatom#1173
Browse files Browse the repository at this point in the history
Cause:
  The mysql original item will round when time convert
to longlong, but tianmu won't.
Solution:
  Do rounding use mysql funciton.
  • Loading branch information
Double0101 committed Jun 8, 2023
1 parent 1a7fe0f commit 26619fd
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 3 deletions.
50 changes: 50 additions & 0 deletions mysql-test/suite/tianmu/r/issue1173.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
DROP DATABASE IF EXISTS issue1173_test_db;
CREATE DATABASE issue1173_test_db;
USE issue1173_test_db;
CREATE TABLE t1 (a TIME(1) NOT NULL)engine=tianmu;
INSERT INTO t1 VALUES ('00:00:02.9');
INSERT INTO t1 VALUES ('00:00:02');
INSERT INTO t1 VALUES ('00:00:02.98');
INSERT INTO t1 VALUES ('00:00:02.5');
INSERT INTO t1 VALUES ('00:00:02.51');
INSERT INTO t1 VALUES ('00:00:02.49');
CREATE TABLE t1i (a TIME(1) NOT NULL)engine=innodb;
INSERT INTO t1i VALUES ('00:00:02.9');
INSERT INTO t1i VALUES ('00:00:02');
INSERT INTO t1i VALUES ('00:00:02.98');
INSERT INTO t1i VALUES ('00:00:02.5');
INSERT INTO t1i VALUES ('00:00:02.51');
INSERT INTO t1i VALUES ('00:00:02.49');
select * from t1;
a
00:00:02.9
00:00:02.0
00:00:03.0
00:00:02.5
00:00:02.5
00:00:02.5
select * from t1i;
00:00:02.9
00:00:02.0
00:00:03.0
00:00:02.5
00:00:02.5
00:00:02.5
SELECT a, a & a, a | a, a^0, a<<1, a<<0, a>>1, bit_count(a) FROM t1;
a a & a a | a a^0 a<<1 a<<0 a>>1 bit_count(a)
00:00:02.9 3 3 3 6 3 1 2
00:00:02.0 2 2 2 4 2 1 1
00:00:03.0 3 3 3 6 3 1 2
00:00:02.5 3 3 3 6 3 1 2
00:00:02.5 3 3 3 6 3 1 2
00:00:02.5 3 3 3 6 3 1 2
CREATE TABLE t2 (a DATETIME(1) NOT NULL);
INSERT INTO t2 VALUES ('2001-01-01 00:00:02.9');
INSERT INTO t2 VALUES ('2001-01-01 23:59:59.9');
INSERT INTO t2 VALUES ('9999-12-31 23:59:59.9');
SELECT a, a & a, a | a, a^0, a<<1, a<<0, a>>1, bit_count(a) FROM t2;
a a & a a | a a^0 a<<1 a<<0 a>>1 bit_count(a)
2001-01-01 00:00:02.9 20010101000003 20010101000003 20010101000003 40020202000006 20010101000003 10005050500001 24
2001-01-01 23:59:59.9 20010102000000 20010102000000 20010102000000 40020204000000 20010102000000 10005051000000 18
9999-12-31 23:59:59.9 99991231000000 99991231000000 99991231000000 199982462000000 99991231000000 49995615500000 24
DROP DATABASE issue1173_test_db;
38 changes: 38 additions & 0 deletions mysql-test/suite/tianmu/t/issue1173.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--source include/have_tianmu.inc

--disable_warnings
DROP DATABASE IF EXISTS issue1173_test_db;
--enable_warnings
CREATE DATABASE issue1173_test_db;
USE issue1173_test_db;

CREATE TABLE t1 (a TIME(1) NOT NULL)engine=tianmu;
INSERT INTO t1 VALUES ('00:00:02.9');
INSERT INTO t1 VALUES ('00:00:02');
INSERT INTO t1 VALUES ('00:00:02.98');
INSERT INTO t1 VALUES ('00:00:02.5');
INSERT INTO t1 VALUES ('00:00:02.51');
INSERT INTO t1 VALUES ('00:00:02.49');

CREATE TABLE t1i (a TIME(1) NOT NULL)engine=innodb;
INSERT INTO t1i VALUES ('00:00:02.9');
INSERT INTO t1i VALUES ('00:00:02');
INSERT INTO t1i VALUES ('00:00:02.98');
INSERT INTO t1i VALUES ('00:00:02.5');
INSERT INTO t1i VALUES ('00:00:02.51');
INSERT INTO t1i VALUES ('00:00:02.49');

select * from t1;

select * from t1i;

SELECT a, a & a, a | a, a^0, a<<1, a<<0, a>>1, bit_count(a) FROM t1;

CREATE TABLE t2 (a DATETIME(1) NOT NULL);
INSERT INTO t2 VALUES ('2001-01-01 00:00:02.9');
INSERT INTO t2 VALUES ('2001-01-01 23:59:59.9');
INSERT INTO t2 VALUES ('9999-12-31 23:59:59.9');

SELECT a, a & a, a | a, a^0, a<<1, a<<0, a>>1, bit_count(a) FROM t2;

DROP DATABASE issue1173_test_db;
20 changes: 17 additions & 3 deletions storage/tianmu/core/item_tianmu_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "core/quick_math.h"
#include "core/transaction.h"
#include "optimizer/compile/compilation_tools.h"
#include "util/log_ctl.h"

namespace Tianmu {
namespace core {
Expand Down Expand Up @@ -266,8 +267,9 @@ bool Item_tianmudatetime_base::get_time(MYSQL_TIME *ltime) {
}

longlong Item_tianmudatetime::val_int() {
return dt.year * 10000000000LL + dt.month * 100000000 + dt.day * 1000000 + dt.hour * 10000 + dt.minute * 100 +
dt.second;
MYSQL_TIME my_time;
dt.Store(&my_time, MYSQL_TIMESTAMP_DATETIME);
return (longlong)TIME_to_ulonglong_datetime_round(&my_time);
}

String *Item_tianmudatetime::val_str(String *s) {
Expand Down Expand Up @@ -317,7 +319,19 @@ bool Item_tianmudate::get_time(MYSQL_TIME *ltime) {
return false;
}

longlong Item_tianmutime::val_int() { return dt.hour * 10000 + dt.minute * 100 + dt.second; }
longlong Item_tianmutime::val_int() {
/* fix for issue#1173
* This logic is consistent with innodb.
* Reffer to the function Field_timef::val_int() in field.cc
*/
MYSQL_TIME my_time;
dt.Store(&my_time, MYSQL_TIMESTAMP_TIME);
TIANMU_LOG(LogCtl_Level::INFO, "convert time: %d:%d:%d.%d", dt.hour, dt.minute, dt.second, dt.microsecond);
longlong tmp = (longlong)TIME_to_ulonglong_time_round(&my_time);
TIANMU_LOG(LogCtl_Level::INFO, "convert result: %lld", tmp);
return dt.Neg() ? -tmp : tmp;
}

String *Item_tianmutime::val_str(String *s) {
MYSQL_TIME ltime;
get_time(&ltime);
Expand Down

0 comments on commit 26619fd

Please sign in to comment.