From 1ec8bf9ef959782d6976548fc9d41013a5e38bf6 Mon Sep 17 00:00:00 2001 From: Bob Liu Date: Wed, 6 Dec 2023 10:11:52 +0800 Subject: [PATCH] add error case --- source/common/src/tvariant.c | 23 ++++++++++++--------- tests/system-test/1-insert/insert_double.py | 13 +++++++----- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/source/common/src/tvariant.c b/source/common/src/tvariant.c index 25f025ad3d..d78c76a543 100644 --- a/source/common/src/tvariant.c +++ b/source/common/src/tvariant.c @@ -126,16 +126,19 @@ int32_t toDoubleEx(const char *z, int32_t n, uint32_t type, double* value) { *value = 0; return TSDB_CODE_SUCCESS; } - // rm tail space - while (n > 1 && z[n-1] == ' ') { - n--; - } errno = 0; char* endPtr = NULL; *value = taosStr2Double(z, &endPtr); - if (errno == ERANGE || errno == EINVAL || endPtr - z != n) { + if (errno == ERANGE || errno == EINVAL) { + return TSDB_CODE_FAILED; + } + // rm tail space + while (n > 1 && z[n-1] == ' ') { + n--; + } + if (endPtr - z != n) { return TSDB_CODE_FAILED; } return TSDB_CODE_SUCCESS; @@ -152,12 +155,12 @@ int32_t toIntegerEx(const char *z, int32_t n, uint32_t type, int64_t *value) { parsed = true; } break; case TK_NK_FLOAT: { - double val = taosStr2Double(z, &endPtr); + double val = round(taosStr2Double(z, &endPtr)); parsed = true; if (!IS_VALID_INT64(val)) { return TSDB_CODE_FAILED; } - *value = round(val); + *value = val; } break; default: break; @@ -167,7 +170,7 @@ int32_t toIntegerEx(const char *z, int32_t n, uint32_t type, int64_t *value) { if (errno == ERANGE || errno == EINVAL) { return TSDB_CODE_FAILED; } - while (n > 0 && z[n-1] == ' ') { + while (n > 1 && z[n-1] == ' ') { n--; } if (endPtr - z != n) { @@ -176,7 +179,7 @@ int32_t toIntegerEx(const char *z, int32_t n, uint32_t type, int64_t *value) { return TSDB_CODE_SUCCESS; } - // parse string + // parse as string if (n == 0) { *value = 0; return TSDB_CODE_SUCCESS; @@ -231,7 +234,7 @@ int32_t toUIntegerEx(const char *z, int32_t n, uint32_t type, uint64_t *value) { break; } - // parse string + // parse as string if (n == 0) { *value = 0; return TSDB_CODE_SUCCESS; diff --git a/tests/system-test/1-insert/insert_double.py b/tests/system-test/1-insert/insert_double.py index 54df7e0352..56861f3cd4 100644 --- a/tests/system-test/1-insert/insert_double.py +++ b/tests/system-test/1-insert/insert_double.py @@ -72,11 +72,14 @@ class TDTestCase: tdSql.query(f"select * from {table_name}") tdSql.checkRows(26) - # fail - tdSql.error(f"insert into {table_name} values(now, 0, {max_u+1})", "error") - tdSql.error(f"insert into {table_name} values(now, 0, {max_u+1})", "error") - tdSql.error(f"insert into {table_name} values(now, {max_i+1}, -1)", "error") - + # error case + tdSql.error(f"insert into {table_name} values(now, 0, {max_u+1})") + tdSql.error(f"insert into {table_name} values(now, 0, -1)") + tdSql.error(f"insert into {table_name} values(now, 0, -2.0)") + tdSql.error(f"insert into {table_name} values(now, 0, '-2.0')") + tdSql.error(f"insert into {table_name} values(now, {max_i+1}, 0)") + tdSql.error(f"insert into {table_name} values(now, {min_i-1}, 0)") + tdSql.error(f"insert into {table_name} values(now, '{min_i-1}', 0)") def test_tags(self, stable_name, dtype, bits): tdSql.execute(f"create stable {stable_name}(ts timestamp, i1 {dtype}, i2 {dtype} unsigned) tags(id {dtype})")