From 32274b149247791ef2dfc5e5c5d99b584f056b1c Mon Sep 17 00:00:00 2001 From: Alex Duan <417921451@qq.com> Date: Fri, 11 Oct 2024 16:50:59 +0800 Subject: [PATCH] fix: add check timestamp range and add except check frame --- taos/utils.py | 7 +++ tests/test_stmt2.py | 136 +++++++++++++++++++++++++++----------------- 2 files changed, 91 insertions(+), 52 deletions(-) diff --git a/taos/utils.py b/taos/utils.py index fbf55192..cb4ef111 100644 --- a/taos/utils.py +++ b/taos/utils.py @@ -146,6 +146,13 @@ def checkTypeValid(buffer_type, values): if type(value) not in (int, str, datetime.datetime): err = f"timestamp type bind not support type = {type(value)}" raise DataTypeAndRangeError(err) + elif type(value) is int: + # check range (same bigint) + min = -2**63 + max = 2**63-1 + if value < min or value > max: + err = f"timestamp type value:{value} exceeds the indicated range [{min}, {max}]" + raise DataTypeAndRangeError(err) elif buffer_type == FieldType.C_BOOL: for value in values: if value is None: diff --git a/tests/test_stmt2.py b/tests/test_stmt2.py index 2043fc51..a5c3742a 100644 --- a/tests/test_stmt2.py +++ b/tests/test_stmt2.py @@ -6,6 +6,7 @@ import taos import math +import traceback from taos.statement2 import * from taos.constants import FieldType from taos import log @@ -120,7 +121,7 @@ def insert_bind_param(conn, stmt2, dbname, stbname): # # table info , write 5 lines to 3 child tables d0, d1, d2 with super table # - tbanmes = ["d1","d2","d3"] + tbnames = ["d1","d2","d3"] tags = [ ["grade1", 1], @@ -154,11 +155,11 @@ def insert_bind_param(conn, stmt2, dbname, stbname): ] ] - stmt2.bind_param(tbanmes, tags, datas) + stmt2.bind_param(tbnames, tags, datas) stmt2.execute() # check correct - checkResultCorrects(conn, dbname, stbname, tbanmes, tags, datas) + checkResultCorrects(conn, dbname, stbname, tbnames, tags, datas) def insert_bind_param_normal_tables(conn, stmt2, dbname): @@ -186,7 +187,7 @@ def insert_bind_param_normal_tables(conn, stmt2, dbname): # insert with single table (performance is lower) def insert_bind_param_with_tables(conn, stmt2, dbname, stbname): - tbanmes = ["t1", "t2", "t3"] + tbnames = ["t1", "t2", "t3"] tags = [ ["grade2", 1], ["grade2", 2], @@ -221,9 +222,9 @@ def insert_bind_param_with_tables(conn, stmt2, dbname, stbname): ] ] - table0 = BindTable(tbanmes[0], tags[0]) - table1 = BindTable(tbanmes[1], tags[1]) - table2 = BindTable(tbanmes[2], tags[2]) + table0 = BindTable(tbnames[0], tags[0]) + table1 = BindTable(tbnames[1], tags[1]) + table2 = BindTable(tbnames[2], tags[2]) for data in datas[0]: table0.add_col_data(data) @@ -237,45 +238,40 @@ def insert_bind_param_with_tables(conn, stmt2, dbname, stbname): stmt2.execute() # check correct - checkResultCorrects(conn, dbname, stbname, tbanmes, tags, datas) + checkResultCorrects(conn, dbname, stbname, tbnames, tags, datas) -# insert with single table (performance is lower) -def insert_with_normal_tables(conn, stmt2, dbname): - tbanmes = ["ntb"] - tags = [None] - # prepare data - datas = [ - # table 1 - [ - # student - [1601481600000,1601481600004,"2024-09-19 10:00:00", "2024-09-19 10:00:01.123", datetime(2024,9,20,10,11,12,456)], - [b"Mary", b"tom", b"Jack", b"Jane", None ], - [0, 3.14, True, 0, 1 ], - [98, 99.87, 60, 100, 99 ], - [None, b"POINT(121.213 31.234)", b"POINT(122.22 32.222)", None, b"POINT(124.22 34.222)"] - ] - ] +def do_check_invalid(stmt2, tbnames, tags, datas): + table0 = BindTable(tbnames[0], tags[0]) + table1 = BindTable(tbnames[1], tags[1]) + table2 = BindTable(tbnames[2], tags[2]) - table0 = BindTable(tbanmes[0], tags[0]) for data in datas[0]: table0.add_col_data(data) + for data in datas[1]: + table1.add_col_data(data) + for data in datas[2]: + table2.add_col_data(data) # bind with single table - stmt2.bind_param_with_tables([table0]) - stmt2.execute() + try: + stmt2.bind_param_with_tables([table0, table1, table2]) + stmt2.execute() + except Exception as err: + #traceback.print_stack() + print(f"failed to do_check_invalid. err={err}") + return - # check correct - checkResultCorrects(conn, dbname, None, tbanmes, tags, datas) + print(f"input invalid data passed , unexpect. \ntbnames={tbnames}\ntags={tags} \ndatas={datas} \n") + assert False -# insert except test -def insert_except_test(conn, stmt2): +def check_input_invalid_param(conn, stmt2, dbname, stbname): - tbanmes = ["t1", "t2", "t3"] + tbnames = ["t1", "t2", "t3"] tags = [ ["grade2", 1], - None, + ["grade2", 2], ["grade2", 3] ] @@ -289,25 +285,63 @@ def insert_except_test(conn, stmt2): [0, 1, 1, 0, 1 ], [98, 80, 60, 100, 99 ] ], - None, - None + # table 2 + [ + # student + [1601481600000,1601481600001,1601481600002,1601481600003,1601481600004], + ["Mary2", "Tom2", "Jack2", "Jane2", "alex2" ], + [0, 1, 1, 0, 1 ], + [298, 280, 260, 2100, 299 ] + ], + # table 3 + [ + # student + [1601481600000,1601481600001,1601481600002,1601481600003,1601481600004], + ["Mary3", "Tom3", "Jack3", "Jane3", "alex3" ], + [0, 1, 1, 0, 1 ], + [398, 380, 360, 3100, 399 ] + ] ] - table0 = BindTable(tbanmes[0], tags[0]) - table1 = BindTable(tbanmes[1], tags[1]) - table2 = BindTable(tbanmes[2], tags[2]) + # some tags is none + tags1 = [ ["grade2", 1], None, ["grade2", 3] ] + do_check_invalid(stmt2, tbnames, tags1, datas) + + # timestamp is over range + origin = datas[0][0][0] + datas[0][0][0] = 100000000000000000000000 + do_check_invalid(stmt2, tbnames, tags, datas) + datas[0][0][0] = origin # restore + +# insert with single table (performance is lower) +def insert_with_normal_tables(conn, stmt2, dbname): + + tbnames = ["ntb"] + tags = [None] + # prepare data + datas = [ + # table 1 + [ + # student + [1601481600000,1601481600004,"2024-09-19 10:00:00", "2024-09-19 10:00:01.123", datetime(2024,9,20,10,11,12,456)], + [b"Mary", b"tom", b"Jack", b"Jane", None ], + [0, 3.14, True, 0, 1 ], + [98, 99.87, 60, 100, 99 ], + [None, b"POINT(121.213 31.234)", b"POINT(122.22 32.222)", None, b"POINT(124.22 34.222)"] + ] + ] + + table0 = BindTable(tbnames[0], tags[0]) for data in datas[0]: table0.add_col_data(data) - table1.add_col_data(datas[1]) - table2.add_col_data(datas[2]) - # bind with single table - try: - stmt2.bind_param_with_tables([table0, table1, table2]) - except Exception as err: - print(f"check except is pass. err={err}") + stmt2.bind_param_with_tables([table0]) + stmt2.execute() + + # check correct + checkResultCorrects(conn, dbname, None, tbnames, tags, datas) def test_stmt2_prepare_empty_sql(conn): @@ -350,6 +384,8 @@ def test_stmt2_insert(conn): # insert with table insert_bind_param_with_tables(conn, stmt2, dbname, stbname) print("insert bind with tables ....................... ok\n") + check_input_invalid_param(conn, stmt2, dbname, stbname) + print("check input invalid params .................... ok\n") # insert with split args insert_bind_param(conn, stmt2, dbname, stbname) @@ -361,10 +397,6 @@ def test_stmt2_insert(conn): insert_with_normal_tables(conn, stmt2, dbname) print("insert normal tables .......................... ok\n") - # insert except test - insert_except_test(conn, stmt2) - print("test insert except ............................ ok\n") - #conn.execute("drop database if exists %s" % dbname) stmt2.close() @@ -387,8 +419,8 @@ def test_stmt2_insert(conn): # def query_bind_param(conn, stmt2): # set param - #tbanmes = ["d2"] - tbanmes = None + #tbnames = ["d2"] + tbnames = None tags = None datas = [ # class 1 @@ -404,7 +436,7 @@ def query_bind_param(conn, stmt2): stmt2.set_columns_type(types) # bind - stmt2.bind_param(tbanmes, tags, datas) + stmt2.bind_param(tbnames, tags, datas) # compare