From edd5754f5643e4ab185e79a58ddbc4566a283a7e Mon Sep 17 00:00:00 2001 From: factosea <285808407@qq.com> Date: Mon, 10 Feb 2025 18:02:28 +0800 Subject: [PATCH] test case --- include/common/ttypes.h | 86 ++++++++----------- tests/army/query/function/cast.py | 3 +- tests/army/query/test_case_when.py | 10 ++- tests/pytest/util/sql.py | 23 +++++ tests/script/tsim/scalar/caseWhen.sim | 22 ++--- .../script/tsim/stream/streamTwaInterpFwc.sim | 2 +- tests/system-test/2-query/cast.py | 24 +++--- 7 files changed, 92 insertions(+), 78 deletions(-) diff --git a/include/common/ttypes.h b/include/common/ttypes.h index 675f774566..d0cddd87a9 100644 --- a/include/common/ttypes.h +++ b/include/common/ttypes.h @@ -216,55 +216,43 @@ typedef struct { } \ } while (0) -#define NUM_TO_STRING(_inputType, _input, _outputBytes, _output) \ - do { \ - switch (_inputType) { \ - case TSDB_DATA_TYPE_TINYINT: \ - snprintf(_output, (int32_t)(_outputBytes), "%d", *(int8_t *)(_input)); \ - break; \ - case TSDB_DATA_TYPE_UTINYINT: \ - snprintf(_output, (int32_t)(_outputBytes), "%d", *(uint8_t *)(_input)); \ - break; \ - case TSDB_DATA_TYPE_SMALLINT: \ - snprintf(_output, (int32_t)(_outputBytes), "%d", *(int16_t *)(_input)); \ - break; \ - case TSDB_DATA_TYPE_USMALLINT: \ - snprintf(_output, (int32_t)(_outputBytes), "%d", *(uint16_t *)(_input)); \ - break; \ - case TSDB_DATA_TYPE_TIMESTAMP: \ - case TSDB_DATA_TYPE_BIGINT: \ - snprintf(_output, (int32_t)(_outputBytes), "%" PRId64, *(int64_t *)(_input)); \ - break; \ - case TSDB_DATA_TYPE_UBIGINT: \ - snprintf(_output, (int32_t)(_outputBytes), "%" PRIu64, *(uint64_t *)(_input)); \ - break; \ - case TSDB_DATA_TYPE_FLOAT: { \ - int32_t n = snprintf(_output, (int32_t)(_outputBytes), "%.7f", *(float *)(_input)); \ - if (n >= (_outputBytes)) { \ - n = snprintf(_output, (int32_t)(_outputBytes), "%.7e", *(float *)(_input)); \ - if (n >= (_outputBytes)) { \ - snprintf(_output, (int32_t)(_outputBytes), "%e", *(float *)(_input)); \ - } \ - } \ - break; \ - } \ - case TSDB_DATA_TYPE_DOUBLE: { \ - int32_t n = snprintf(_output, (int32_t)(_outputBytes), "%.15f", *(double *)(_input)); \ - if (n >= (_outputBytes)) { \ - n = snprintf(_output, (int32_t)(_outputBytes), "%.15e", *(double *)(_input)); \ - if (n >= (_outputBytes)) { \ - snprintf(_output, (int32_t)(_outputBytes), "%e", *(double *)(_input)); \ - } \ - } \ - break; \ - } \ - case TSDB_DATA_TYPE_UINT: \ - snprintf(_output, (int32_t)(_outputBytes), "%u", *(uint32_t *)(_input)); \ - break; \ - default: \ - snprintf(_output, (int32_t)(_outputBytes), "%d", *(int32_t *)(_input)); \ - break; \ - } \ +#define NUM_TO_STRING(_inputType, _input, _outputBytes, _output) \ + do { \ + switch (_inputType) { \ + case TSDB_DATA_TYPE_TINYINT: \ + snprintf(_output, (int32_t)(_outputBytes), "%d", *(int8_t *)(_input)); \ + break; \ + case TSDB_DATA_TYPE_UTINYINT: \ + snprintf(_output, (int32_t)(_outputBytes), "%d", *(uint8_t *)(_input)); \ + break; \ + case TSDB_DATA_TYPE_SMALLINT: \ + snprintf(_output, (int32_t)(_outputBytes), "%d", *(int16_t *)(_input)); \ + break; \ + case TSDB_DATA_TYPE_USMALLINT: \ + snprintf(_output, (int32_t)(_outputBytes), "%d", *(uint16_t *)(_input)); \ + break; \ + case TSDB_DATA_TYPE_TIMESTAMP: \ + case TSDB_DATA_TYPE_BIGINT: \ + snprintf(_output, (int32_t)(_outputBytes), "%" PRId64, *(int64_t *)(_input)); \ + break; \ + case TSDB_DATA_TYPE_UBIGINT: \ + snprintf(_output, (int32_t)(_outputBytes), "%" PRIu64, *(uint64_t *)(_input)); \ + break; \ + case TSDB_DATA_TYPE_FLOAT: { \ + snprintf(_output, (int32_t)(_outputBytes), "%.*g", FLT_DIG, *(float *)(_input)); \ + break; \ + } \ + case TSDB_DATA_TYPE_DOUBLE: { \ + snprintf(_output, (int32_t)(_outputBytes), "%.*g", DBL_DIG, *(double *)(_input)); \ + break; \ + } \ + case TSDB_DATA_TYPE_UINT: \ + snprintf(_output, (int32_t)(_outputBytes), "%u", *(uint32_t *)(_input)); \ + break; \ + default: \ + snprintf(_output, (int32_t)(_outputBytes), "%d", *(int32_t *)(_input)); \ + break; \ + } \ } while (0) // TODO: use varchar(0) to represent NULL type diff --git a/tests/army/query/function/cast.py b/tests/army/query/function/cast.py index 2765250d93..977ec82697 100644 --- a/tests/army/query/function/cast.py +++ b/tests/army/query/function/cast.py @@ -265,8 +265,9 @@ class TDTestCase(TBase): tdSql.checkData(0, 0, 3.141) sql = f"select cast({float_1001} as binary(10)) as re;" + tdLog.debug(sql) tdSql.query(sql) - tdSql.checkData(0, 0, 3.141593) + tdSql.checkData(0, 0, 3.14159265) tdSql.query(f"select cast({float_1001} as nchar(5));") tdSql.checkData(0, 0, 3.141) diff --git a/tests/army/query/test_case_when.py b/tests/army/query/test_case_when.py index e772914183..101311d6c0 100644 --- a/tests/army/query/test_case_when.py +++ b/tests/army/query/test_case_when.py @@ -142,13 +142,14 @@ class TDTestCase(TBase): assert(tdSql.checkRows(10) and all([item[0] is None for item in tdSql.res])) tdSql.query("select case when c_float is not null then c_float else c_null end from st1;") - assert(tdSql.checkRows(10) and tdSql.res == [('2.200000',), ('3.300000',), ('4.400000',), ('5.500000',), ('6.600000',), ('7.700000',), ('8.800000',), ('9.900000',), ('10.100000',), (None,)]) + assert(tdSql.checkRows(10) and tdSql.res == [('2.2',), ('3.3',), ('4.4',), ('5.5',), ('6.6',), ('7.7',), ('8.8',), ('9.9',), ('10.1',), (None,)]) tdSql.query("select case when c_double is null then c_double else c_null end from st1;") assert(tdSql.checkRows(10) and all([item[0] is None for item in tdSql.res])) - + tdSql.query("select case when c_double is not null then c_double else c_null end from st1;") - assert(tdSql.checkRows(10) and tdSql.res == [('2.220000',), ('3.330000',), ('4.440000',), ('5.550000',), ('6.660000',), ('7.770000',), ('8.880000',), ('9.990000',), ('10.101000',), (None,)]) + + assert(tdSql.checkRows(10) and tdSql.res == [('2.22',), ('3.33',), ('4.44',), ('5.55',), ('6.66',), ('7.77',), ('8.88',), ('9.99',), ('10.101',), (None,)]) tdSql.query("select case when c_varchar is null then c_varchar else c_null end from st1;") assert(tdSql.checkRows(10) and all([item[0] is None for item in tdSql.res])) @@ -344,7 +345,8 @@ class TDTestCase(TBase): assert(tdSql.checkRows(10) and tdSql.res == [('-2147483648',), ('three',), ('4294967295',), ('4294967295',), ('4294967295',), ('4294967295',), ('4294967295',), ('4294967295',), ('4294967295',), ('4294967295',)]) tdSql.query("select case c_float when 2.2 then 9.2233720e+18 when 3.3 then -9.2233720e+18 else 'aa' end from st1;") - assert(tdSql.checkRows(10) and tdSql.res == [('9223372000000000000.000000',), ('-9223372000000000000.000000',), ('aa',), ('aa',), ('aa',), ('aa',), ('aa',), ('aa',), ('aa',), ('aa',)]) + print(tdSql.res) + assert(tdSql.checkRows(10) and tdSql.res == [('9.223372e+18',), ('-9.223372e+18',), ('aa',), ('aa',), ('aa',), ('aa',), ('aa',), ('aa',), ('aa',), ('aa',)]) tdSql.query("select case t1.c_int when 2 then 'run' when t1.c_int is null then 'other' else t2.c_varchar end from st1 t1, st2 t2 where t1.ts=t2.ts;") print(tdSql.res) diff --git a/tests/pytest/util/sql.py b/tests/pytest/util/sql.py index cdfe3ce8a0..8d247a690a 100644 --- a/tests/pytest/util/sql.py +++ b/tests/pytest/util/sql.py @@ -414,6 +414,29 @@ class TDSql: self.checkRowCol(row, col) return self.cursor.istype(col, dataType) + def checkFloatString(self, row, col, data, show = False): + if row >= self.queryRows: + caller = inspect.getframeinfo(inspect.stack()[1][0]) + args = (caller.filename, caller.lineno, self.sql, row+1, self.queryRows) + tdLog.exit("%s(%d) failed: sql:%s, row:%d is larger than queryRows:%d" % args) + if col >= self.queryCols: + caller = inspect.getframeinfo(inspect.stack()[1][0]) + args = (caller.filename, caller.lineno, self.sql, col+1, self.queryCols) + tdLog.exit("%s(%d) failed: sql:%s, col:%d is larger than queryCols:%d" % args) + + self.checkRowCol(row, col) + + val = float(self.queryResult[row][col]) + if abs(data) >= 1 and abs((val - data) / data) <= 0.000001: + if(show): + tdLog.info("check successfully") + elif abs(data) < 1 and abs(val - data) <= 0.000001: + if(show): + tdLog.info("check successfully") + else: + caller = inspect.getframeinfo(inspect.stack()[1][0]) + args = (caller.filename, caller.lineno, self.sql, row, col, self.queryResult[row][col], data) + tdLog.exit("%s(%d) failed: sql:%s row:%d col:%d data:%s != expect:%s" % args) def checkData(self, row, col, data, show = False): if row >= self.queryRows: diff --git a/tests/script/tsim/scalar/caseWhen.sim b/tests/script/tsim/scalar/caseWhen.sim index 4753e24513..0fbec52572 100644 --- a/tests/script/tsim/scalar/caseWhen.sim +++ b/tests/script/tsim/scalar/caseWhen.sim @@ -397,7 +397,7 @@ sql select case when f1 then f1 when f1 + 1 then f1 + 1 else f1 is null end from if $rows != 4 then return -1 endi -if $data00 != 1.000000 then +if $data00 != 1 then return -1 endi if $data10 != 1 then @@ -659,10 +659,10 @@ endi if $data00 != 0 then return -1 endi -if $data10 != 10.000000 then +if $data10 != 10 then return -1 endi -if $data20 != 50.000000 then +if $data20 != 50 then return -1 endi if $data30 != -1 then @@ -889,19 +889,19 @@ endi if $data10 != 0 then return -1 endi -if $data11 != -99.000000 then +if $data11 != -99 then return -1 endi if $data20 != 1 then return -1 endi -if $data21 != 100.000000 then +if $data21 != 100 then return -1 endi if $data30 != 5 then return -1 endi -if $data31 != -94.000000 then +if $data31 != -94 then return -1 endi @@ -1028,7 +1028,7 @@ endi if $data00 != NULL then return -1 endi -if $data10 != -99.000000 then +if $data10 != -99 then return -1 endi if $data20 != 1 then @@ -1051,10 +1051,10 @@ endi if $data21 != NULL then return -1 endi -if $data31 != 101.000000 then +if $data31 != 101 then return -1 endi -if $data41 != 103.000000 then +if $data41 != 103 then return -1 endi @@ -1071,7 +1071,7 @@ sql select case when c_int > 100 then c_float else c_int end as result from t_te if $rows != 1 then return -1 endi -if $data00 != 123.449997 then +if $data00 != 123.45 then return -1 endi @@ -1079,7 +1079,7 @@ sql select case when c_bigint > 100000 then c_double else c_bigint end as result if $rows != 1 then return -1 endi -if $data00 != 678.900000 then +if $data00 != 678.9 then return -1 endi diff --git a/tests/script/tsim/stream/streamTwaInterpFwc.sim b/tests/script/tsim/stream/streamTwaInterpFwc.sim index 050e0282e2..34e6ed8cb7 100644 --- a/tests/script/tsim/stream/streamTwaInterpFwc.sim +++ b/tests/script/tsim/stream/streamTwaInterpFwc.sim @@ -435,7 +435,7 @@ if $data01 != @t12@ then return -1 endi -if $data03 != @100000.000@ then +if $data03 != @100000@ then return -1 endi diff --git a/tests/system-test/2-query/cast.py b/tests/system-test/2-query/cast.py index 11013e09a8..298039f7cc 100644 --- a/tests/system-test/2-query/cast.py +++ b/tests/system-test/2-query/cast.py @@ -305,30 +305,30 @@ class TDTestCase: tdLog.printNoPrefix("==========step21: cast float to binary, expect changes to str(int) ") tdSql.query(f"select cast(c5 as binary(32)) as b from {self.dbname}.ct4") for i in range(len(data_ct4_c5)): - tdSql.checkData( i, 0, str(data_ct4_c5[i]) ) if data_ct4_c5[i] is None else tdSql.checkData( i, 0, f'{data_ct4_c5[i]:.6f}' ) + tdSql.checkData( i, 0, str(data_ct4_c5[i]) ) if data_ct4_c5[i] is None else tdSql.checkFloatString( i, 0, data_ct4_c5[i]) tdSql.query(f"select cast(c5 as binary(32)) as b from {self.dbname}.t1") for i in range(len(data_t1_c5)): - tdSql.checkData( i, 0, str(data_t1_c5[i]) ) if data_t1_c5[i] is None else tdSql.checkData( i, 0, f'{data_t1_c5[i]:.6f}' ) + tdSql.checkData( i, 0, str(data_t1_c5[i]) ) if data_t1_c5[i] is None else tdSql.checkFloatString( i, 0, data_t1_c5[i]) tdSql.query(f"select cast(c5 as binary) as b from {self.dbname}.ct4") for i in range(len(data_ct4_c5)): - tdSql.checkData( i, 0, str(data_ct4_c5[i]) ) if data_ct4_c5[i] is None else tdSql.checkData( i, 0, f'{data_ct4_c5[i]:.6f}' ) + tdSql.checkData( i, 0, str(data_ct4_c5[i]) ) if data_ct4_c5[i] is None else tdSql.checkFloatString( i, 0, data_ct4_c5[i]) tdSql.query(f"select cast(c5 as binary) as b from {self.dbname}.t1") for i in range(len(data_t1_c5)): - tdSql.checkData( i, 0, str(data_t1_c5[i]) ) if data_t1_c5[i] is None else tdSql.checkData( i, 0, f'{data_t1_c5[i]:.6f}' ) + tdSql.checkData( i, 0, str(data_t1_c5[i]) ) if data_t1_c5[i] is None else tdSql.checkFloatString( i, 0, data_t1_c5[i]) tdLog.printNoPrefix("==========step22: cast float to nchar, expect changes to str(int) ") tdSql.query(f"select cast(c5 as nchar(32)) as b from {self.dbname}.ct4") for i in range(len(data_ct4_c5)): - tdSql.checkData( i, 0, None ) if data_ct4_c5[i] is None else tdSql.checkData( i, 0, f'{data_ct4_c5[i]:.6f}' ) + tdSql.checkData( i, 0, None ) if data_ct4_c5[i] is None else tdSql.checkFloatString( i, 0, data_ct4_c5[i]) tdSql.query(f"select cast(c5 as nchar(32)) as b from {self.dbname}.t1") for i in range(len(data_t1_c5)): - tdSql.checkData( i, 0, None ) if data_t1_c5[i] is None else tdSql.checkData( i, 0, f'{data_t1_c5[i]:.6f}' ) + tdSql.checkData( i, 0, None ) if data_t1_c5[i] is None else tdSql.checkFloatString( i, 0, data_t1_c5[i]) tdSql.query(f"select cast(c5 as nchar) as b from {self.dbname}.t1") for i in range(len(data_t1_c5)): - tdSql.checkData( i, 0, None ) if data_t1_c5[i] is None else tdSql.checkData( i, 0, f'{data_t1_c5[i]:.6f}' ) + tdSql.checkData( i, 0, None ) if data_t1_c5[i] is None else tdSql.checkFloatString( i, 0, data_t1_c5[i]) tdSql.query(f"select cast(c5 as varchar) as b from {self.dbname}.t1") for i in range(len(data_t1_c5)): - tdSql.checkData( i, 0, None ) if data_t1_c5[i] is None else tdSql.checkData( i, 0, f'{data_t1_c5[i]:.6f}' ) + tdSql.checkData( i, 0, None ) if data_t1_c5[i] is None else tdSql.checkFloatString( i, 0, data_t1_c5[i]) tdLog.printNoPrefix("==========step23: cast float to timestamp, expect changes to timestamp ") tdSql.query(f"select cast(c5 as timestamp) as b from {self.dbname}.ct4") @@ -367,18 +367,18 @@ class TDTestCase: tdLog.printNoPrefix("==========step25: cast double to binary, expect changes to str(int) ") tdSql.query(f"select cast(c6 as binary(32)) as b from {self.dbname}.ct4") for i in range(len(data_ct4_c6)): - tdSql.checkData( i, 0, None ) if data_ct4_c6[i] is None else tdSql.checkData( i, 0, f'{data_ct4_c6[i]:.6f}' ) + tdSql.checkData( i, 0, None ) if data_ct4_c6[i] is None else tdSql.checkFloatString( i, 0, data_ct4_c6[i]) tdSql.query(f"select cast(c6 as binary(32)) as b from {self.dbname}.t1") for i in range(len(data_t1_c6)): - tdSql.checkData( i, 0, None ) if data_t1_c6[i] is None else tdSql.checkData( i, 0, f'{data_t1_c6[i]:.6f}' ) + tdSql.checkData( i, 0, None ) if data_t1_c6[i] is None else tdSql.checkFloatString( i, 0, data_t1_c6[i]) tdLog.printNoPrefix("==========step26: cast double to nchar, expect changes to str(int) ") tdSql.query(f"select cast(c6 as nchar(32)) as b from {self.dbname}.ct4") for i in range(len(data_ct4_c6)): - tdSql.checkData( i, 0, None ) if data_ct4_c6[i] is None else tdSql.checkData( i, 0, f'{data_ct4_c6[i]:.6f}' ) + tdSql.checkData( i, 0, None ) if data_ct4_c6[i] is None else tdSql.checkFloatString( i, 0, data_ct4_c6[i]) tdSql.query(f"select cast(c6 as nchar(32)) as b from {self.dbname}.t1") for i in range(len(data_t1_c6)): - tdSql.checkData( i, 0, None ) if data_t1_c6[i] is None else tdSql.checkData( i, 0, f'{data_t1_c6[i]:.6f}' ) + tdSql.checkData( i, 0, None ) if data_t1_c6[i] is None else tdSql.checkFloatString( i, 0, data_t1_c6[i]) tdLog.printNoPrefix("==========step27: cast double to timestamp, expect changes to timestamp ") tdSql.query(f"select cast(c6 as timestamp) as b from {self.dbname}.ct4")