Skip to content

Commit

Permalink
Merge pull request #269 from taosdata/fix/TD-32477-3.0
Browse files Browse the repository at this point in the history
fix: add check timestamp range and add except check frame
  • Loading branch information
YamingPei authored Oct 11, 2024
2 parents cda9bac + 32274b1 commit 925a549
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 52 deletions.
7 changes: 7 additions & 0 deletions taos/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
136 changes: 84 additions & 52 deletions tests/test_stmt2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import taos
import math
import traceback
from taos.statement2 import *
from taos.constants import FieldType
from taos import log
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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)
Expand All @@ -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]
]

Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 925a549

Please sign in to comment.