From aa706fd5e7439173a108a2bae5d07945c24214b3 Mon Sep 17 00:00:00 2001 From: bryanchang0603 Date: Tue, 1 Jun 2021 13:43:19 +0800 Subject: [PATCH 01/57] [TD-4463] add test case for checking keep modification --- tests/pytest/alter/alter_keep.py | 70 ++++++++++++++++++++++++++++++++ tests/pytest/fulltest.sh | 2 +- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 tests/pytest/alter/alter_keep.py diff --git a/tests/pytest/alter/alter_keep.py b/tests/pytest/alter/alter_keep.py new file mode 100644 index 0000000000..e7c58a09c5 --- /dev/null +++ b/tests/pytest/alter/alter_keep.py @@ -0,0 +1,70 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import sys +from util.log import * +from util.cases import * +from util.sql import * + + +class TDTestCase: + def init(self, conn, logSql): + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor(), logSql) + + + def run(self): + tdSql.prepare() + tdSql.execute('create table tb (ts timestamp, speed int)') + + tdSql.query('show databases') + tdSql.checkData(0,7,'3650,3650,3650') + tdSql.execute('alter database db keep 10') + tdSql.query('show databases') + tdSql.checkData(0,7,'10,10,10') + tdSql.execute('alter database db keep 50') + tdSql.query('show databases') + tdSql.checkData(0,7,'50,50,50') + tdSql.error('alter database db keep !)') + + ##TODO: test the illegal alter keep input where input < days + # tdSql.error('alter database db keep -10') + # tdSql.error('alter database db keep 0') + + ##TODO: test keep keep hot alter, cannot be tested for now as test.py's output + ## is inconsistent with the actual output. + + # tdSql.execute('insert into tb values (now, 10)') + # tdSql.execute('insert into tb values (now + 10m, 10)') + # tdSql.query('select * from tb') + # tdSql.checkRows(2) + # tdSql.execute('alter database db keep 40,40,40') + # os.system('systemctl restart taosd') + # tdSql.execute('insert into tb values (now-60d, 10)') + # tdSql.execute('insert into tb values (now-30d, 10)') + # tdSql.query('select * from tb') + # tdSql.showQueryResult() + # tdSql.checkRows(2) + # tdSql.execute('alter database db keep 20,20,20') + # tdSql.checkRows(3) + # os.system('systemctl restart taosd') + # tdSql.query('select * from tb') + # tdSql.checkRows(2) + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) diff --git a/tests/pytest/fulltest.sh b/tests/pytest/fulltest.sh index c93fbc5eb3..a546c74da6 100755 --- a/tests/pytest/fulltest.sh +++ b/tests/pytest/fulltest.sh @@ -335,5 +335,5 @@ python3 ./test.py -f tag_lite/alter_tag.py python3 test.py -f tools/taosdemoAllTest/taosdemoTestInsertWithJson.py python3 test.py -f tools/taosdemoAllTest/taosdemoTestQueryWithJson.py python3 test.py -f insert/insert_before_use_db.py - +python3 test.py -f alter/alter_keep.py #======================p4-end=============== From c55f6121a0a9c3b4f9e3146cfed43e485209eb05 Mon Sep 17 00:00:00 2001 From: bryanchang0603 Date: Tue, 1 Jun 2021 16:13:40 +0800 Subject: [PATCH 02/57] [TD-4463] moldifying alter_keep.py --- tests/pytest/alter/alter_keep.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/pytest/alter/alter_keep.py b/tests/pytest/alter/alter_keep.py index e7c58a09c5..5e1378e249 100644 --- a/tests/pytest/alter/alter_keep.py +++ b/tests/pytest/alter/alter_keep.py @@ -37,9 +37,16 @@ class TDTestCase: tdSql.checkData(0,7,'50,50,50') tdSql.error('alter database db keep !)') - ##TODO: test the illegal alter keep input where input < days - # tdSql.error('alter database db keep -10') - # tdSql.error('alter database db keep 0') + tdSql.error('alter database db keep 1') + + ## the following sql will not raise error, but will not cause error either + # based on Li Chuang's explaination, <= 0 will not cause keep>days error + tdSql.execute('alter database db keep -10') + tdSql.query('show databases') + tdSql.checkData(0,7,'50,50,50') + tdSql.execute('alter database db keep 0') + tdSql.query('show databases') + tdSql.checkData(0,7,'50,50,50') ##TODO: test keep keep hot alter, cannot be tested for now as test.py's output ## is inconsistent with the actual output. From 60ba3849789958104a50212f384ef45e5b26e6f9 Mon Sep 17 00:00:00 2001 From: tomchon Date: Tue, 1 Jun 2021 18:16:28 +0800 Subject: [PATCH 03/57] [TD-4290]: add testcase that compact wal file --- tests/pytest/wal/insertDataDb1.json | 87 +++++++++++++++ tests/pytest/wal/insertDataDb2.json | 86 +++++++++++++++ tests/pytest/wal/insertDataDb2Newstab.json | 86 +++++++++++++++ tests/pytest/wal/sdbComp.py | 118 +++++++++++++++++++++ tests/pytest/wal/sdbCompCluster.py | 95 +++++++++++++++++ 5 files changed, 472 insertions(+) create mode 100644 tests/pytest/wal/insertDataDb1.json create mode 100644 tests/pytest/wal/insertDataDb2.json create mode 100644 tests/pytest/wal/insertDataDb2Newstab.json create mode 100644 tests/pytest/wal/sdbComp.py create mode 100644 tests/pytest/wal/sdbCompCluster.py diff --git a/tests/pytest/wal/insertDataDb1.json b/tests/pytest/wal/insertDataDb1.json new file mode 100644 index 0000000000..1dce00a4d5 --- /dev/null +++ b/tests/pytest/wal/insertDataDb1.json @@ -0,0 +1,87 @@ +{ + "filetype": "insert", + "cfgdir": "/etc/taos", + "host": "127.0.0.1", + "port": 6030, + "user": "root", + "password": "taosdata", + "thread_count": 4, + "thread_count_create_tbl": 4, + "result_file": "./insert_res.txt", + "confirm_parameter_prompt": "no", + "insert_interval": 0, + "interlace_rows": 10, + "num_of_records_per_req": 1000, + "max_sql_len": 1024000, + "databases": [{ + "dbinfo": { + "name": "db1", + "drop": "yes", + "replica": 1, + "days": 10, + "cache": 50, + "blocks": 8, + "precision": "ms", + "keep": 365, + "minRows": 100, + "maxRows": 4096, + "comp":2, + "walLevel":1, + "cachelast":0, + "quorum":1, + "fsync":3000, + "update": 0 + }, + "super_tables": [{ + "name": "stb0", + "child_table_exists":"no", + "childtable_count": 1000, + "childtable_prefix": "stb00_", + "auto_create_table": "no", + "batch_create_tbl_num": 100, + "data_source": "rand", + "insert_mode": "taosc", + "insert_rows": 100, + "childtable_limit": 0, + "childtable_offset":0, + "interlace_rows": 0, + "insert_interval":0, + "max_sql_len": 1024000, + "disorder_ratio": 0, + "disorder_range": 1000, + "timestamp_step": 1, + "start_timestamp": "2020-10-01 00:00:00.000", + "sample_format": "csv", + "sample_file": "./sample.csv", + "tags_file": "", + "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":1}, {"type": "BINARY", "len": 16, "count":1}, {"type": "BINARY", "len": 32, "count":1}], + "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":1}] + }, + { + "name": "stb1", + "child_table_exists":"no", + "childtable_count": 1000, + "childtable_prefix": "stb01_", + "auto_create_table": "no", + "batch_create_tbl_num": 10, + "data_source": "rand", + "insert_mode": "taosc", + "insert_rows": 200, + "childtable_limit": 0, + "childtable_offset":0, + "interlace_rows": 0, + "insert_interval":0, + "max_sql_len": 1024000, + "disorder_ratio": 0, + "disorder_range": 1000, + "timestamp_step": 1, + "start_timestamp": "2020-10-01 00:00:00.000", + "sample_format": "csv", + "sample_file": "./sample.csv", + "tags_file": "", + "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":1}, {"type": "BINARY", "len": 16, "count":1}, {"type": "BINARY", "len": 32, "count":1}], + "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":1}] + }] + }] +} + diff --git a/tests/pytest/wal/insertDataDb2.json b/tests/pytest/wal/insertDataDb2.json new file mode 100644 index 0000000000..2cf8af5805 --- /dev/null +++ b/tests/pytest/wal/insertDataDb2.json @@ -0,0 +1,86 @@ +{ + "filetype": "insert", + "cfgdir": "/etc/taos", + "host": "127.0.0.1", + "port": 6030, + "user": "root", + "password": "taosdata", + "thread_count": 4, + "thread_count_create_tbl": 4, + "result_file": "./insert_res.txt", + "confirm_parameter_prompt": "no", + "insert_interval": 0, + "interlace_rows": 0, + "num_of_records_per_req": 3000, + "max_sql_len": 1024000, + "databases": [{ + "dbinfo": { + "name": "db2", + "drop": "yes", + "replica": 1, + "days": 10, + "cache": 50, + "blocks": 8, + "precision": "ms", + "keep": 365, + "minRows": 100, + "maxRows": 4096, + "comp":2, + "walLevel":1, + "cachelast":0, + "quorum":1, + "fsync":3000, + "update": 0 + }, + "super_tables": [{ + "name": "stb0", + "child_table_exists":"no", + "childtable_count": 2000, + "childtable_prefix": "stb0_", + "auto_create_table": "no", + "batch_create_tbl_num": 100, + "data_source": "rand", + "insert_mode": "taosc", + "insert_rows": 2000, + "childtable_limit": 0, + "childtable_offset":0, + "interlace_rows": 0, + "insert_interval":0, + "max_sql_len": 1024000, + "disorder_ratio": 0, + "disorder_range": 1000, + "timestamp_step": 1, + "start_timestamp": "2020-10-01 00:00:00.000", + "sample_format": "csv", + "sample_file": "./sample.csv", + "tags_file": "", + "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":1}, {"type": "BINARY", "len": 16, "count":1}, {"type": "BINARY", "len": 32, "count":1}], + "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":1}] + }, + { + "name": "stb1", + "child_table_exists":"no", + "childtable_count": 2, + "childtable_prefix": "stb1_", + "auto_create_table": "no", + "batch_create_tbl_num": 10, + "data_source": "rand", + "insert_mode": "taosc", + "insert_rows": 5, + "childtable_limit": 0, + "childtable_offset":0, + "interlace_rows": 0, + "insert_interval":0, + "max_sql_len": 1024000, + "disorder_ratio": 0, + "disorder_range": 1000, + "timestamp_step": 1, + "start_timestamp": "2020-10-01 00:00:00.000", + "sample_format": "csv", + "sample_file": "./sample.csv", + "tags_file": "", + "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":1}, {"type": "BINARY", "len": 16, "count":1}, {"type": "BINARY", "len": 32, "count":1}], + "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":1}] + }] + }] +} diff --git a/tests/pytest/wal/insertDataDb2Newstab.json b/tests/pytest/wal/insertDataDb2Newstab.json new file mode 100644 index 0000000000..f9d0713385 --- /dev/null +++ b/tests/pytest/wal/insertDataDb2Newstab.json @@ -0,0 +1,86 @@ +{ + "filetype": "insert", + "cfgdir": "/etc/taos", + "host": "127.0.0.1", + "port": 6030, + "user": "root", + "password": "taosdata", + "thread_count": 4, + "thread_count_create_tbl": 4, + "result_file": "./insert_res.txt", + "confirm_parameter_prompt": "no", + "insert_interval": 0, + "interlace_rows": 0, + "num_of_records_per_req": 3000, + "max_sql_len": 1024000, + "databases": [{ + "dbinfo": { + "name": "db2", + "drop": "no", + "replica": 1, + "days": 10, + "cache": 50, + "blocks": 8, + "precision": "ms", + "keep": 365, + "minRows": 100, + "maxRows": 4096, + "comp":2, + "walLevel":1, + "cachelast":0, + "quorum":1, + "fsync":3000, + "update": 0 + }, + "super_tables": [{ + "name": "stb0", + "child_table_exists":"no", + "childtable_count": 1, + "childtable_prefix": "stb0_", + "auto_create_table": "no", + "batch_create_tbl_num": 100, + "data_source": "rand", + "insert_mode": "taosc", + "insert_rows": 0, + "childtable_limit": -1, + "childtable_offset":0, + "interlace_rows": 0, + "insert_interval":0, + "max_sql_len": 1024000, + "disorder_ratio": 0, + "disorder_range": 1000, + "timestamp_step": 1, + "start_timestamp": "2020-10-01 00:00:00.000", + "sample_format": "csv", + "sample_file": "./sample.csv", + "tags_file": "", + "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":1}, {"type": "BINARY", "len": 16, "count":1}, {"type": "BINARY", "len": 32, "count":1}], + "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":1}] + }, + { + "name": "stb1", + "child_table_exists":"yes", + "childtable_count": 1, + "childtable_prefix": "stb01_", + "auto_create_table": "no", + "batch_create_tbl_num": 10, + "data_source": "rand", + "insert_mode": "taosc", + "insert_rows": 10, + "childtable_limit": -1, + "childtable_offset":0, + "interlace_rows": 0, + "insert_interval":0, + "max_sql_len": 1024000, + "disorder_ratio": 0, + "disorder_range": 1000, + "timestamp_step": 1, + "start_timestamp": "2020-11-01 00:00:00.000", + "sample_format": "csv", + "sample_file": "./sample.csv", + "tags_file": "", + "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":1}, {"type": "BINARY", "len": 16, "count":1}], + "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":1}] + }] + }] +} diff --git a/tests/pytest/wal/sdbComp.py b/tests/pytest/wal/sdbComp.py new file mode 100644 index 0000000000..b645a0e445 --- /dev/null +++ b/tests/pytest/wal/sdbComp.py @@ -0,0 +1,118 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import sys +import os +import taos +from util.log import * +from util.cases import * +from util.sql import * +from util.dnodes import * + + +class TDTestCase: + def init(self, conn, logSql): + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor(), logSql) + + def getBuildPath(self): + selfPath = os.path.dirname(os.path.realpath(__file__)) + + if ("community" in selfPath): + projPath = selfPath[:selfPath.find("community")] + else: + projPath = selfPath[:selfPath.find("tests")] + + for root, dirs, files in os.walk(projPath): + if ("taosd" in files): + rootRealPath = os.path.dirname(os.path.realpath(root)) + if ("packaging" not in rootRealPath): + buildPath = root[:len(root)-len("/build/bin")] + break + return buildPath + + def run(self): + buildPath = self.getBuildPath() + if (buildPath == ""): + tdLog.exit("taosd not found!") + else: + tdLog.info("taosd found in %s" % buildPath) + binPath = buildPath+ "/build/bin/" + + #new db and insert data + os.system("rm -rf /home/chr/TDengine/sim/dnode1/data/mnode_tmp/") + os.system("rm -rf /home/chr/TDengine/sim/dnode1/data/mnode_bak/") + tdSql.execute("drop database if exists db2") + os.system("%staosdemo -f wal/insertDataDb1.json -y " % binPath) + tdSql.execute("drop database if exists db1") + os.system("%staosdemo -f wal/insertDataDb2.json -y " % binPath) + tdSql.execute("drop table if exists db2.stb0") + os.system("%staosdemo -f wal/insertDataDb2Newstab.json -y " % binPath) + query_pid1 = int(subprocess.getstatusoutput('ps aux|grep taosd |grep -v "grep"|awk \'{print $2}\'')[1]) + print(query_pid1) + tdSql.execute("use db2") + tdSql.execute("drop table if exists stb1_0") + tdSql.execute("drop table if exists stb1_1") + tdSql.execute("insert into stb0_0 values(1614218412000,8637,78.861045,'R','bf3')(1614218422000,8637,98.861045,'R','bf3')") + tdSql.execute("alter table db2.stb0 add column col4 int") + tdSql.execute("alter table db2.stb0 drop column col2") + tdSql.execute("alter table db2.stb0 add tag t3 int;") + tdSql.execute("alter table db2.stb0 drop tag t1") + tdSql.execute("create table if not exists stb2_0 (ts timestamp, col0 int, col1 float) ") + tdSql.execute("insert into stb2_0 values(1614218412000,8637,78.861045)") + tdSql.execute("alter table stb2_0 add column col2 binary(4)") + tdSql.execute("alter table stb2_0 drop column col1") + tdSql.execute("insert into stb2_0 values(1614218422000,8638,'R')") + + # stop taosd and compact wal file + os.system("ps -ef |grep taosd |grep -v 'grep' |awk '{print $2}'|xargs kill -9") + sleep(2) + # os.system("nohup taosd --compact-mnode-wal -c /home/chr/TDengine/sim/dnode1/cfg/ & ") + sleep(5) + os.system("nohup /home/chr/TDengine/debug/build/bin/taosd -c /home/chr/TDengine/sim/dnode1/cfg > /dev/null 2>&1 &") + sleep(4) + tdSql.execute("reset query cache") + query_pid2 = int(subprocess.getstatusoutput('ps aux|grep taosd |grep -v "grep"|awk \'{print $2}\'')[1]) + print(query_pid2) + + # use new wal file to start up tasod + # conn1 = taos.connect(host="chenhaoran02", user="root", password="taosdata", config="/home/chr/TDengine/sim/dnode1/cfg/" ) + # cur1 = conn1.cursor() + # tdSql.init(cur1, True) + + tdSql.execute("use db2") + tdSql.query("select count (tbname) from stb0") + tdSql.checkData(0, 0, 1) + tdSql.query("select count (tbname) from stb1") + tdSql.checkRows(0) + tdSql.query("select count(*) from stb0_0") + tdSql.checkData(0, 0, 2) + tdSql.query("select count(*) from stb0") + tdSql.checkData(0, 0, 2) + tdSql.query("select count(*) from stb2_0") + tdSql.checkData(0, 0, 2) + + + + os.system("rm -rf ./insert_res.txt") + os.system("rm -rf wal/sdbComp.py.sql") + + + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) diff --git a/tests/pytest/wal/sdbCompCluster.py b/tests/pytest/wal/sdbCompCluster.py new file mode 100644 index 0000000000..2cefa0a403 --- /dev/null +++ b/tests/pytest/wal/sdbCompCluster.py @@ -0,0 +1,95 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import os +import sys +sys.path.insert(0, os.getcwd()) +from util.log import * +from util.sql import * +from util.dnodes import * +import taos +import threading + + +class TwoClients: + def initConnection(self): + self.host = "chenhaoran01" + self.user = "root" + self.password = "taosdata" + self.config = "/etc/taos/" + self.port =6030 + self.rowNum = 10 + self.ts = 1537146000000 + + def getBuildPath(self): + selfPath = os.path.dirname(os.path.realpath(__file__)) + + if ("community" in selfPath): + projPath = selfPath[:selfPath.find("community")] + else: + projPath = selfPath[:selfPath.find("tests")] + + for root, dirs, files in os.walk(projPath): + if ("taosd" in files): + rootRealPath = os.path.dirname(os.path.realpath(root)) + if ("packaging" not in rootRealPath): + buildPath = root[:len(root)-len("/build/bin")] + break + return buildPath + + def run(self): + buildPath = self.getBuildPath() + if (buildPath == ""): + tdLog.exit("taosd not found!") + else: + tdLog.info("taosd found in %s" % buildPath) + binPath = buildPath+ "/build/bin/" + + # query data from cluster'db + conn1 = taos.connect(host=self.host, user=self.user, password=self.password, config=self.config ) + print(conn1) + cur1 = conn1.cursor() + tdSql.init(cur1, True) + # tdSql.init(cur2, True) + os.system("%staosdemo -f wal/insertDataDb1.json -y " % binPath) + tdSql.execute("drop database if exists db1") + os.system("%staosdemo -f wal/insertDataDb2.json -y " % binPath) + tdSql.execute("drop table if exists db2.stb0") + os.system("%staosdemo -f wal/insertDataDb2Newstab.json -y " % binPath) + tdSql.execute("alter table db2.stb0 add column col4 int") + tdSql.execute("alter table db2.stb0 drop column col2") + os.system("ps -ef |grep taosd |grep -v 'grep' |awk '{print $2}'|xargs kill -9") + os.system("nohup taosd --compact-mnode-wal & ") + sleep(5) + os.system("nohup /usr/bin/taosd > /dev/null 2>&1 &") + sleep(10) + conn2 = taos.connect(host=self.host, user=self.user, password=self.password, config=self.config ) + print(conn2) + cur2 = conn2.cursor() + tdSql.init(cur2, True) + tdSql.execute("use db2") + tdSql.query("select count (tbname) from stb0") + tdSql.checkData(0, 0, 1) + tdSql.query("select count (tbname) from stb1") + tdSql.checkData(0, 0, 2) + tdSql.query("select count(*) from stb00_0") + tdSql.checkData(0, 0, 1) + tdSql.query("select count(*) from stb0") + tdSql.checkData(0, 0, 1) + tdSql.query("select count(*) from stb1") + tdSql.checkData(0, 0, 20) + +clients = TwoClients() +clients.initConnection() +# clients.getBuildPath() +clients.run() \ No newline at end of file From 49e0643bf2bf4cdf8d95d09ddf70fb04cdcce46e Mon Sep 17 00:00:00 2001 From: bryanchang0603 Date: Wed, 2 Jun 2021 16:02:06 +0800 Subject: [PATCH 04/57] [TD-4463] update boundary case and error case --- tests/pytest/alter/alter_keep.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/pytest/alter/alter_keep.py b/tests/pytest/alter/alter_keep.py index 5e1378e249..37a8a8da55 100644 --- a/tests/pytest/alter/alter_keep.py +++ b/tests/pytest/alter/alter_keep.py @@ -41,16 +41,17 @@ class TDTestCase: ## the following sql will not raise error, but will not cause error either # based on Li Chuang's explaination, <= 0 will not cause keep>days error - tdSql.execute('alter database db keep -10') - tdSql.query('show databases') - tdSql.checkData(0,7,'50,50,50') - tdSql.execute('alter database db keep 0') + # tdSql.error('alter database db keep -10') + # tdSql.query('show databases') + # tdSql.checkData(0,7,'50,50,50') + # tdSql.error('alter database db keep 0') + # tdSql.error('alter database db keep 0.1') + tdSql.error('alter database db keep 10.1') tdSql.query('show databases') tdSql.checkData(0,7,'50,50,50') ##TODO: test keep keep hot alter, cannot be tested for now as test.py's output ## is inconsistent with the actual output. - # tdSql.execute('insert into tb values (now, 10)') # tdSql.execute('insert into tb values (now + 10m, 10)') # tdSql.query('select * from tb') From 119537014b196c88485c4e54e3895819d47ab0d1 Mon Sep 17 00:00:00 2001 From: tomchon Date: Wed, 2 Jun 2021 17:18:34 +0800 Subject: [PATCH 05/57] [TD-4290]: modify testcase that compact wal file --- tests/pytest/wal/sdbComp.py | 28 ++++++++-------- tests/pytest/wal/sdbCompCluster.py | 51 +++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/tests/pytest/wal/sdbComp.py b/tests/pytest/wal/sdbComp.py index b645a0e445..0d1fac2031 100644 --- a/tests/pytest/wal/sdbComp.py +++ b/tests/pytest/wal/sdbComp.py @@ -11,6 +11,7 @@ # -*- coding: utf-8 -*- +from distutils.log import debug import sys import os import taos @@ -18,6 +19,7 @@ from util.log import * from util.cases import * from util.sql import * from util.dnodes import * +import subprocess class TDTestCase: @@ -27,7 +29,7 @@ class TDTestCase: def getBuildPath(self): selfPath = os.path.dirname(os.path.realpath(__file__)) - + if ("community" in selfPath): projPath = selfPath[:selfPath.find("community")] else: @@ -47,11 +49,13 @@ class TDTestCase: tdLog.exit("taosd not found!") else: tdLog.info("taosd found in %s" % buildPath) - binPath = buildPath+ "/build/bin/" + binPath = buildPath+ "/build/bin/" + testPath = buildPath[:buildPath.find("debug")] + #new db and insert data - os.system("rm -rf /home/chr/TDengine/sim/dnode1/data/mnode_tmp/") - os.system("rm -rf /home/chr/TDengine/sim/dnode1/data/mnode_bak/") + os.system("rm -rf %s/sim/dnode1/data/mnode_tmp/" % testPath) + os.system("rm -rf %s/sim/dnode1/data/mnode_bak/" % testPath) tdSql.execute("drop database if exists db2") os.system("%staosdemo -f wal/insertDataDb1.json -y " % binPath) tdSql.execute("drop database if exists db1") @@ -73,23 +77,19 @@ class TDTestCase: tdSql.execute("alter table stb2_0 add column col2 binary(4)") tdSql.execute("alter table stb2_0 drop column col1") tdSql.execute("insert into stb2_0 values(1614218422000,8638,'R')") - # stop taosd and compact wal file - os.system("ps -ef |grep taosd |grep -v 'grep' |awk '{print $2}'|xargs kill -9") - sleep(2) - # os.system("nohup taosd --compact-mnode-wal -c /home/chr/TDengine/sim/dnode1/cfg/ & ") - sleep(5) - os.system("nohup /home/chr/TDengine/debug/build/bin/taosd -c /home/chr/TDengine/sim/dnode1/cfg > /dev/null 2>&1 &") + tdDnodes.stop(1) + sleep(10) + os.system("nohup %s/taosd --compact-mnode-wal -c %s/sim/dnode1/cfg/ & " %(binPath,testPath) ) + # os.system("nohup taosd --compact-mnode-wal -c %s/sim/dnode1/cfg/ & " % testPath ) + # tdDnodes.start(1) + os.system("nohup %s/taosd -c %s/sim/dnode1/cfg > /dev/null 2>&1 &" %(binPath,testPath) ) sleep(4) tdSql.execute("reset query cache") query_pid2 = int(subprocess.getstatusoutput('ps aux|grep taosd |grep -v "grep"|awk \'{print $2}\'')[1]) print(query_pid2) # use new wal file to start up tasod - # conn1 = taos.connect(host="chenhaoran02", user="root", password="taosdata", config="/home/chr/TDengine/sim/dnode1/cfg/" ) - # cur1 = conn1.cursor() - # tdSql.init(cur1, True) - tdSql.execute("use db2") tdSql.query("select count (tbname) from stb0") tdSql.checkData(0, 0, 1) diff --git a/tests/pytest/wal/sdbCompCluster.py b/tests/pytest/wal/sdbCompCluster.py index 2cefa0a403..b4411353d8 100644 --- a/tests/pytest/wal/sdbCompCluster.py +++ b/tests/pytest/wal/sdbCompCluster.py @@ -23,7 +23,7 @@ import threading class TwoClients: def initConnection(self): - self.host = "chenhaoran01" + self.host = "chenhaoran02" self.user = "root" self.password = "taosdata" self.config = "/etc/taos/" @@ -61,34 +61,61 @@ class TwoClients: cur1 = conn1.cursor() tdSql.init(cur1, True) # tdSql.init(cur2, True) + + # new db and insert data + os.system("rm -rf /var/lib/taos/mnode_bak/") + os.system("rm -rf /var/lib/taos/mnode_temp/") + tdSql.execute("drop database if exists db2") os.system("%staosdemo -f wal/insertDataDb1.json -y " % binPath) tdSql.execute("drop database if exists db1") os.system("%staosdemo -f wal/insertDataDb2.json -y " % binPath) tdSql.execute("drop table if exists db2.stb0") os.system("%staosdemo -f wal/insertDataDb2Newstab.json -y " % binPath) + query_pid1 = int(subprocess.getstatusoutput('ps aux|grep taosd |grep -v "grep"|awk \'{print $2}\'')[1]) + print(query_pid1) + tdSql.execute("use db2") + tdSql.execute("drop table if exists stb1_0") + tdSql.execute("drop table if exists stb1_1") + tdSql.execute("insert into stb0_0 values(1614218412000,8637,78.861045,'R','bf3')(1614218422000,8637,98.861045,'R','bf3')") tdSql.execute("alter table db2.stb0 add column col4 int") tdSql.execute("alter table db2.stb0 drop column col2") - os.system("ps -ef |grep taosd |grep -v 'grep' |awk '{print $2}'|xargs kill -9") - os.system("nohup taosd --compact-mnode-wal & ") - sleep(5) - os.system("nohup /usr/bin/taosd > /dev/null 2>&1 &") - sleep(10) + tdSql.execute("alter table db2.stb0 add tag t3 int") + tdSql.execute("alter table db2.stb0 drop tag t1") + tdSql.execute("create table if not exists stb2_0 (ts timestamp, col0 int, col1 float) ") + tdSql.execute("insert into stb2_0 values(1614218412000,8637,78.861045)") + tdSql.execute("alter table stb2_0 add column col2 binary(4)") + tdSql.execute("alter table stb2_0 drop column col1") + tdSql.execute("insert into stb2_0 values(1614218422000,8638,'R')") + conn2 = taos.connect(host=self.host, user=self.user, password=self.password, config=self.config ) print(conn2) cur2 = conn2.cursor() tdSql.init(cur2, True) + + # stop taosd and compact wal file + os.system("ps -ef |grep taosd |grep -v 'grep' |awk '{print $2}'|xargs kill -9") + sleep(2) + os.system("nohup taosd --compact-mnode-wal -c /etc/taos/taos.cfg & ") + sleep(5) + os.system("nohup /usr/bin/taosd > /dev/null 2>&1 &") + sleep(4) + tdSql.execute("reset query cache") + query_pid2 = int(subprocess.getstatusoutput('ps aux|grep taosd |grep -v "grep"|awk \'{print $2}\'')[1]) + print(query_pid2) + + # use new wal file to start up tasod tdSql.execute("use db2") tdSql.query("select count (tbname) from stb0") tdSql.checkData(0, 0, 1) tdSql.query("select count (tbname) from stb1") + tdSql.checkRows(0) + tdSql.query("select count(*) from stb0_0") tdSql.checkData(0, 0, 2) - tdSql.query("select count(*) from stb00_0") - tdSql.checkData(0, 0, 1) tdSql.query("select count(*) from stb0") - tdSql.checkData(0, 0, 1) - tdSql.query("select count(*) from stb1") - tdSql.checkData(0, 0, 20) - + tdSql.checkData(0, 0, 2) + tdSql.query("select count(*) from stb2_0") + tdSql.checkData(0, 0, 2) + clients = TwoClients() clients.initConnection() # clients.getBuildPath() From 17077cb92d1559b00e559dc4b16ccd2bc2ca8498 Mon Sep 17 00:00:00 2001 From: tomchon Date: Wed, 2 Jun 2021 17:46:48 +0800 Subject: [PATCH 06/57] add sleep when start compacting --- tests/pytest/wal/sdbComp.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/pytest/wal/sdbComp.py b/tests/pytest/wal/sdbComp.py index 0d1fac2031..aab21419bb 100644 --- a/tests/pytest/wal/sdbComp.py +++ b/tests/pytest/wal/sdbComp.py @@ -82,6 +82,7 @@ class TDTestCase: sleep(10) os.system("nohup %s/taosd --compact-mnode-wal -c %s/sim/dnode1/cfg/ & " %(binPath,testPath) ) # os.system("nohup taosd --compact-mnode-wal -c %s/sim/dnode1/cfg/ & " % testPath ) + sleep(5) # tdDnodes.start(1) os.system("nohup %s/taosd -c %s/sim/dnode1/cfg > /dev/null 2>&1 &" %(binPath,testPath) ) sleep(4) From 8112eeaeb9b318d27e75278a553a10a3e08c4373 Mon Sep 17 00:00:00 2001 From: lichuang Date: Wed, 2 Jun 2021 18:30:33 +0800 Subject: [PATCH 07/57] fix test script bug --- tests/pytest/wal/sdbComp.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/pytest/wal/sdbComp.py b/tests/pytest/wal/sdbComp.py index 0d1fac2031..e1f1c3e4ff 100644 --- a/tests/pytest/wal/sdbComp.py +++ b/tests/pytest/wal/sdbComp.py @@ -83,7 +83,8 @@ class TDTestCase: os.system("nohup %s/taosd --compact-mnode-wal -c %s/sim/dnode1/cfg/ & " %(binPath,testPath) ) # os.system("nohup taosd --compact-mnode-wal -c %s/sim/dnode1/cfg/ & " % testPath ) # tdDnodes.start(1) - os.system("nohup %s/taosd -c %s/sim/dnode1/cfg > /dev/null 2>&1 &" %(binPath,testPath) ) + #os.system("nohup %s/taosd -c %s/sim/dnode1/cfg > /dev/null 2>&1 &" %(binPath,testPath) ) + tdDnodes.start(1) sleep(4) tdSql.execute("reset query cache") query_pid2 = int(subprocess.getstatusoutput('ps aux|grep taosd |grep -v "grep"|awk \'{print $2}\'')[1]) From d01ea3012acf93e306307141b8081ee0407beec1 Mon Sep 17 00:00:00 2001 From: tomchon Date: Wed, 2 Jun 2021 21:24:30 +0800 Subject: [PATCH 08/57] [TD-4290]: modify testcase that compact wal file --- tests/pytest/wal/sdbComp.py | 17 +++++++++++------ tests/pytest/wal/sdbCompCluster.py | 8 +++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/tests/pytest/wal/sdbComp.py b/tests/pytest/wal/sdbComp.py index aab21419bb..e0a43d5e0e 100644 --- a/tests/pytest/wal/sdbComp.py +++ b/tests/pytest/wal/sdbComp.py @@ -44,6 +44,8 @@ class TDTestCase: return buildPath def run(self): + + # set path para buildPath = self.getBuildPath() if (buildPath == ""): tdLog.exit("taosd not found!") @@ -52,7 +54,8 @@ class TDTestCase: binPath = buildPath+ "/build/bin/" testPath = buildPath[:buildPath.find("debug")] - + walFilePath = testPath + "/sim/dnode1/data/mnode_bak/wal/" + #new db and insert data os.system("rm -rf %s/sim/dnode1/data/mnode_tmp/" % testPath) os.system("rm -rf %s/sim/dnode1/data/mnode_bak/" % testPath) @@ -77,20 +80,22 @@ class TDTestCase: tdSql.execute("alter table stb2_0 add column col2 binary(4)") tdSql.execute("alter table stb2_0 drop column col1") tdSql.execute("insert into stb2_0 values(1614218422000,8638,'R')") + # stop taosd and compact wal file tdDnodes.stop(1) sleep(10) os.system("nohup %s/taosd --compact-mnode-wal -c %s/sim/dnode1/cfg/ & " %(binPath,testPath) ) - # os.system("nohup taosd --compact-mnode-wal -c %s/sim/dnode1/cfg/ & " % testPath ) sleep(5) - # tdDnodes.start(1) - os.system("nohup %s/taosd -c %s/sim/dnode1/cfg > /dev/null 2>&1 &" %(binPath,testPath) ) - sleep(4) + assert os.path.exists(walFilePath) , "%s is not generated, compact didn't take effect " % walFilePath + + # use new wal file to start taosd + tdDnodes.start(1) + sleep(5) tdSql.execute("reset query cache") query_pid2 = int(subprocess.getstatusoutput('ps aux|grep taosd |grep -v "grep"|awk \'{print $2}\'')[1]) print(query_pid2) - # use new wal file to start up tasod + # verify that the data is correct tdSql.execute("use db2") tdSql.query("select count (tbname) from stb0") tdSql.checkData(0, 0, 1) diff --git a/tests/pytest/wal/sdbCompCluster.py b/tests/pytest/wal/sdbCompCluster.py index b4411353d8..43cf4be3c6 100644 --- a/tests/pytest/wal/sdbCompCluster.py +++ b/tests/pytest/wal/sdbCompCluster.py @@ -54,8 +54,9 @@ class TwoClients: else: tdLog.info("taosd found in %s" % buildPath) binPath = buildPath+ "/build/bin/" - - # query data from cluster'db + walFilePath = "/var/lib/taos/mnode_bak/wal/" + + # new taos client conn1 = taos.connect(host=self.host, user=self.user, password=self.password, config=self.config ) print(conn1) cur1 = conn1.cursor() @@ -95,13 +96,14 @@ class TwoClients: # stop taosd and compact wal file os.system("ps -ef |grep taosd |grep -v 'grep' |awk '{print $2}'|xargs kill -9") sleep(2) - os.system("nohup taosd --compact-mnode-wal -c /etc/taos/taos.cfg & ") + os.system("nohup taosd --compact-mnode-wal -c /etc/taos & ") sleep(5) os.system("nohup /usr/bin/taosd > /dev/null 2>&1 &") sleep(4) tdSql.execute("reset query cache") query_pid2 = int(subprocess.getstatusoutput('ps aux|grep taosd |grep -v "grep"|awk \'{print $2}\'')[1]) print(query_pid2) + assert os.path.exists(walFilePath) , "%s is not generated " % walFilePath # use new wal file to start up tasod tdSql.execute("use db2") From 555fe9e91e813d7bed571db4ce85d6f89a14aa13 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Thu, 3 Jun 2021 12:45:30 +0800 Subject: [PATCH 09/57] [TD-4426] merge in --- src/client/src/tscSQLParser.c | 97 +++++++++++++++++++++++++++++++++-- src/common/inc/texpr.h | 2 + src/common/src/texpr.c | 22 ++++++++ src/query/inc/qExecutor.h | 1 + src/query/src/qExecutor.c | 4 ++ src/query/src/qFilterfunc.c | 21 ++++++++ src/tsdb/src/tsdbRead.c | 58 ++++++++++++++++++--- src/util/src/tcompare.c | 12 +++-- 8 files changed, 202 insertions(+), 15 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 4e9e5a8654..bcd3e294d1 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -63,6 +63,9 @@ static SExprInfo* doAddProjectCol(SQueryInfo* pQueryInfo, int32_t colIndex, int3 static int32_t setShowInfo(SSqlObj* pSql, SSqlInfo* pInfo); static char* getAccountId(SSqlObj* pSql); +static bool serializeExprListToVariant(SArray* pList, tVariant **dest); +static int32_t validateParamOfRelationIn(tVariant *pVar, int32_t colType); + static bool has(SArray* pFieldList, int32_t startIdx, const char* name); static char* cloneCurrentDBName(SSqlObj* pSql); static int32_t getDelimiterIndex(SStrToken* pTableName); @@ -134,14 +137,79 @@ static bool validateDebugFlag(int32_t v); static int32_t checkQueryRangeForFill(SSqlCmd* pCmd, SQueryInfo* pQueryInfo); static int32_t loadAllTableMeta(SSqlObj* pSql, struct SSqlInfo* pInfo); -static bool isTimeWindowQuery(SQueryInfo* pQueryInfo) { +static bool isTimeWindowQuery(SQueryInfo* pQueryInfo) { return pQueryInfo->interval.interval > 0 || pQueryInfo->sessionWindow.gap > 0; } + int16_t getNewResColId(SSqlCmd* pCmd) { return pCmd->resColumnId--; } +// serialize expr in exprlist to binary +// formate "type | size | value" +bool serializeExprListToVariant(SArray* pList, tVariant **dst) { + bool ret = false; + if (!pList || pList->size <= 0) { + return ret; + } + + SBufferWriter bw = tbufInitWriter( NULL, false ); + tbufEnsureCapacity(&bw, 512); + + tSqlExprItem* item = (tSqlExprItem *)taosArrayGet(pList, 0); + int32_t size = pList->size; + int32_t type = item->pNode->token.type; + if (type < 0) { + tbufCloseWriter(&bw); + return ret; + } + + toTSDBType(item->pNode->token.type); + tbufWriteUint32(&bw, item->pNode->token.type); + tbufWriteInt32(&bw, size); + + for (int32_t i = 0; i < size; i++) { + tSqlExpr* pSub = ((tSqlExprItem*)(taosArrayGet(pList, i)))->pNode; + + // validate type in exprList + if (type != pSub->token.type) { + break; + } + tVariant var; + tVariantCreate(&var, &pSub->token); + if (type == TSDB_DATA_TYPE_BOOL || type == TSDB_DATA_TYPE_TINYINT || type == TSDB_DATA_TYPE_SMALLINT + || type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_INT) { + tbufWriteInt64(&bw, var.i64); + } else if (type == TSDB_DATA_TYPE_DOUBLE || type == TSDB_DATA_TYPE_FLOAT) { + tbufWriteDouble(&bw, var.dKey); + } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { + tbufWriteBinary(&bw, var.pz, var.nLen); + } + tVariantDestroy(&var); + + if (i == size - 1) { ret = true;} + } + + if (ret == true) { + if ((*dst = calloc(1, sizeof(tVariant))) != NULL) { + tVariantCreateFromBinary(*dst, tbufGetData(&bw, false), tbufTell(&bw), TSDB_DATA_TYPE_BINARY); + } else { + ret = false; + } + } + tbufCloseWriter(&bw); + return ret; +} + +static int32_t validateParamOfRelationIn(tVariant *pVar, int32_t colType) { + if (pVar->nType != TSDB_DATA_TYPE_BINARY) { + return -1; + } + SBufferReader br = tbufInitReader(pVar->pz, pVar->nLen, false); + return tbufReadUint32(&br) == colType ? 0: -1; +} + static uint8_t convertOptr(SStrToken *pToken) { switch (pToken->type) { case TK_LT: @@ -3205,7 +3273,27 @@ static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, retVal = tVariantDump(&pRight->value, (char*)&pColumnFilter->upperBndd, colType, false); // TK_GT,TK_GE,TK_EQ,TK_NE are based on the pColumn->lowerBndd - } else if (colType == TSDB_DATA_TYPE_BINARY) { + } else if (pExpr->tokenId == TK_IN) { + tVariant *pVal; + if (pRight->tokenId != TK_SET || !serializeExprListToVariant(pRight->pParam, &pVal)) { + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); + } + if (validateParamOfRelationIn(pVal, colType) != TSDB_CODE_SUCCESS) { + tVariantDestroy(pVal); + free(pVal); + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); + } + pColumnFilter->pz = (int64_t)calloc(1, pVal->nLen + 1); + pColumnFilter->len = pVal->nLen; + pColumnFilter->filterstr = 1; + memcpy((char *)(pColumnFilter->pz), (char *)(pVal->pz), pVal->nLen); + //retVal = tVariantDump(pVal, (char *)(pColumnFilter->pz), TSDB_DATA_TYPE_BINARY, false); + + tVariantDestroy(pVal); + free(pVal); + + } + else if (colType == TSDB_DATA_TYPE_BINARY) { pColumnFilter->pz = (int64_t)calloc(1, bufLen * TSDB_NCHAR_SIZE); pColumnFilter->len = pRight->value.nLen; retVal = tVariantDump(&pRight->value, (char*)pColumnFilter->pz, colType, false); @@ -3253,6 +3341,9 @@ static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, case TK_NOTNULL: pColumnFilter->lowerRelOptr = TSDB_RELATION_NOTNULL; break; + case TK_IN: + pColumnFilter->lowerRelOptr = TSDB_RELATION_IN; + break; default: return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); } @@ -3368,7 +3459,7 @@ static int32_t extractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SC && pExpr->tokenId != TK_ISNULL && pExpr->tokenId != TK_NOTNULL && pExpr->tokenId != TK_LIKE - ) { + && pExpr->tokenId != TK_IN) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } } else { diff --git a/src/common/inc/texpr.h b/src/common/inc/texpr.h index 275dd12fd7..2e49a69366 100644 --- a/src/common/inc/texpr.h +++ b/src/common/inc/texpr.h @@ -94,6 +94,8 @@ bool exprTreeApplyFilter(tExprNode *pExpr, const void *pItem, SExprTraverseSupp void arithmeticTreeTraverse(tExprNode *pExprs, int32_t numOfRows, char *pOutput, void *param, int32_t order, char *(*cb)(void *, const char*, int32_t)); +void buildFilterSetFromBinary(void **q, const char *buf, int32_t len); + #ifdef __cplusplus } #endif diff --git a/src/common/src/texpr.c b/src/common/src/texpr.c index f2dd3890a1..0805eff9ff 100644 --- a/src/common/src/texpr.c +++ b/src/common/src/texpr.c @@ -466,6 +466,28 @@ tExprNode* exprTreeFromTableName(const char* tbnameCond) { return expr; } +void buildFilterSetFromBinary(void **q, const char *buf, int32_t len) { + SBufferReader br = tbufInitReader(buf, len, false); + uint32_t type = tbufReadUint32(&br); + SHashObj *pObj = taosHashInit(256, taosGetDefaultHashFunction(type), true, false); + int dummy = -1; + int32_t sz = tbufReadInt32(&br); + for (int32_t i = 0; i < sz; i++) { + if (type == TSDB_DATA_TYPE_BOOL || type == TSDB_DATA_TYPE_TINYINT || type == TSDB_DATA_TYPE_SMALLINT || type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_INT) { + int64_t val = tbufReadInt64(&br); + taosHashPut(pObj, (char *)&val, sizeof(val), &dummy, sizeof(dummy)); + } else if (type == TSDB_DATA_TYPE_DOUBLE || type == TSDB_DATA_TYPE_FLOAT) { + double val = tbufReadDouble(&br); + taosHashPut(pObj, (char *)&val, sizeof(val), &dummy, sizeof(dummy)); + } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { + size_t t = 0; + const char *val = tbufReadBinary(&br, &t); + taosHashPut(pObj, (char *)val, t, &dummy, sizeof(dummy)); + } + } + *q = (void *)pObj; +} + tExprNode* exprdup(tExprNode* pNode) { if (pNode == NULL) { return NULL; diff --git a/src/query/inc/qExecutor.h b/src/query/inc/qExecutor.h index b5b4fb5854..27d1e14b45 100644 --- a/src/query/inc/qExecutor.h +++ b/src/query/inc/qExecutor.h @@ -113,6 +113,7 @@ typedef struct SColumnFilterElem { int16_t bytes; // column length __filter_func_t fp; SColumnFilterInfo filterInfo; + void *q; } SColumnFilterElem; typedef struct SSingleColumnFilterInfo { diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 25e7e446bd..350e7118a8 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -6940,6 +6940,10 @@ int32_t doCreateFilterInfo(SColumnInfo* pCols, int32_t numOfCols, int32_t numOfF } pSingleColFilter->bytes = pCols[i].bytes; + + if (lower == TSDB_RELATION_IN) { + buildFilterSetFromBinary(&pSingleColFilter->q, (char *)(pSingleColFilter->filterInfo.pz), pSingleColFilter->filterInfo.len); + } } j++; diff --git a/src/query/src/qFilterfunc.c b/src/query/src/qFilterfunc.c index dabce88423..7769ec4a57 100644 --- a/src/query/src/qFilterfunc.c +++ b/src/query/src/qFilterfunc.c @@ -253,6 +253,25 @@ bool isNullOperator(SColumnFilterElem *pFilter, const char* minval, const char* bool notNullOperator(SColumnFilterElem *pFilter, const char* minval, const char* maxval, int16_t type) { return true; } +bool inOperator(SColumnFilterElem *pFilter, const char* minval, const char* maxval, int16_t type) { + if (type == TSDB_DATA_TYPE_BOOL || type == TSDB_DATA_TYPE_TINYINT || type == TSDB_DATA_TYPE_SMALLINT || type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_INT) { + int64_t minv = -1, maxv = -1; + GET_TYPED_DATA(minv, int64_t, type, minval); + GET_TYPED_DATA(maxv, int64_t, type, maxval); + if (minv == maxv) { + return NULL != taosHashGet((SHashObj *)pFilter->q, (char *)&minv, sizeof(minv)); + } + return true; + } else if (type == TSDB_DATA_TYPE_DOUBLE || type == TSDB_DATA_TYPE_DOUBLE) { + double v; + GET_TYPED_DATA(v, double, type, minval); + return NULL != taosHashGet((SHashObj *)pFilter->q, (char *)&v, sizeof(v)); + } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR){ + return NULL != taosHashGet((SHashObj *)pFilter->q, varDataVal(minval), varDataLen(minval)); + } + + return false; +} /////////////////////////////////////////////////////////////////////////////// bool rangeFilter_ii(SColumnFilterElem *pFilter, const char *minval, const char *maxval, int16_t type) { @@ -389,6 +408,7 @@ bool (*filterOperators[])(SColumnFilterElem *pFilter, const char* minval, const likeOperator, isNullOperator, notNullOperator, + inOperator, }; bool (*rangeFilterOperators[])(SColumnFilterElem *pFilter, const char* minval, const char* maxval, int16_t type) = { @@ -397,6 +417,7 @@ bool (*rangeFilterOperators[])(SColumnFilterElem *pFilter, const char* minval, c rangeFilter_ie, rangeFilter_ei, rangeFilter_ii, + }; __filter_func_t getFilterOperator(int32_t lowerOptr, int32_t upperOptr) { diff --git a/src/tsdb/src/tsdbRead.c b/src/tsdb/src/tsdbRead.c index 1545d44395..bd609a6708 100644 --- a/src/tsdb/src/tsdbRead.c +++ b/src/tsdb/src/tsdbRead.c @@ -3153,11 +3153,22 @@ void filterPrepare(void* expr, void* param) { pInfo->sch = *pSchema; pInfo->optr = pExpr->_node.optr; - pInfo->compare = getComparFunc(pSchema->type, pInfo->optr); + pInfo->compare = getComparFunc(pInfo->sch.type, pInfo->optr); pInfo->indexed = pTSSchema->columns->colId == pInfo->sch.colId; if (pInfo->optr == TSDB_RELATION_IN) { - pInfo->q = (char*) pCond->arr; + int dummy = -1; + SHashObj *pObj = NULL; + if (pInfo->sch.colId == TSDB_TBNAME_COLUMN_INDEX) { + pObj = taosHashInit(256, taosGetDefaultHashFunction(pInfo->sch.type), true, false); + SArray *arr = (SArray *)(pCond->arr); + for (size_t i = 0; i < taosArrayGetSize(arr); i++) { + taosHashPut(pObj, (char *)taosArrayGet(arr, i), arr->elemSize, &dummy, sizeof(dummy)); + } + } else { + buildFilterSetFromBinary((void **)&pObj, pCond->pz, pCond->nLen); + } + pInfo->q = (char *)pObj; } else if (pCond != NULL) { uint32_t size = pCond->nLen * TSDB_NCHAR_SIZE; if (size < (uint32_t)pSchema->bytes) { @@ -3328,6 +3339,20 @@ static bool tableFilterFp(const void* pNode, void* param) { } else if (pInfo->optr == TSDB_RELATION_NOTNULL) { return (val != NULL) && (!isNull(val, pInfo->sch.type)); } + } else if (pInfo->optr == TSDB_RELATION_IN) { + int type = pInfo->sch.type; + if (type == TSDB_DATA_TYPE_BOOL || type == TSDB_DATA_TYPE_TINYINT || type == TSDB_DATA_TYPE_SMALLINT || type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_INT) { + int64_t v; + GET_TYPED_DATA(v, int64_t, pInfo->sch.type, val); + return NULL != taosHashGet((SHashObj *)pInfo->q, (char *)&v, sizeof(v)); + } else if (type == TSDB_DATA_TYPE_DOUBLE || type == TSDB_DATA_TYPE_DOUBLE) { + double v; + GET_TYPED_DATA(v, double, pInfo->sch.type, val); + return NULL != taosHashGet((SHashObj *)pInfo->q, (char *)&v, sizeof(v)); + } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR){ + return NULL != taosHashGet((SHashObj *)pInfo->q, varDataVal(val), varDataLen(val)); + } + } int32_t ret = 0; @@ -3670,14 +3695,18 @@ static int32_t setQueryCond(tQueryInfo *queryColInfo, SQueryCond* pCond) { if (optr == TSDB_RELATION_GREATER || optr == TSDB_RELATION_GREATER_EQUAL || optr == TSDB_RELATION_EQUAL || optr == TSDB_RELATION_NOT_EQUAL) { - pCond->start = calloc(1, sizeof(SEndPoint)); + pCond->start = calloc(1, sizeof(SEndPoint)); pCond->start->optr = queryColInfo->optr; - pCond->start->v = queryColInfo->q; + pCond->start->v = queryColInfo->q; } else if (optr == TSDB_RELATION_LESS || optr == TSDB_RELATION_LESS_EQUAL) { - pCond->end = calloc(1, sizeof(SEndPoint)); + pCond->end = calloc(1, sizeof(SEndPoint)); pCond->end->optr = queryColInfo->optr; - pCond->end->v = queryColInfo->q; - } else if (optr == TSDB_RELATION_IN || optr == TSDB_RELATION_LIKE) { + pCond->end->v = queryColInfo->q; + } else if (optr == TSDB_RELATION_IN) { + pCond->start = calloc(1, sizeof(SEndPoint)); + pCond->start->optr = queryColInfo->optr; + pCond->start->v = queryColInfo->q; + } else if (optr == TSDB_RELATION_LIKE) { assert(0); } @@ -3762,6 +3791,19 @@ static void queryIndexedColumn(SSkipList* pSkipList, tQueryInfo* pQueryInfo, SAr taosArrayPush(result, &info); } + } else if (optr == TSDB_RELATION_IN) { + while(tSkipListIterNext(iter)) { + SSkipListNode* pNode = tSkipListIterGet(iter); + + int32_t ret = pQueryInfo->compare(SL_GET_NODE_KEY(pSkipList, pNode), cond.start->v); + if (ret != 0) { + break; + } + + STableKeyInfo info = {.pTable = (void*)SL_GET_NODE_DATA(pNode), .lastKey = TSKEY_INITIAL_VAL}; + taosArrayPush(result, &info); + } + } else { assert(0); } @@ -3855,7 +3897,7 @@ void getTableListfromSkipList(tExprNode *pExpr, SSkipList *pSkipList, SArray *re param->setupInfoFn(pExpr, param->pExtInfo); tQueryInfo *pQueryInfo = pExpr->_node.info; - if (pQueryInfo->indexed && pQueryInfo->optr != TSDB_RELATION_LIKE) { + if (pQueryInfo->indexed && (pQueryInfo->optr != TSDB_RELATION_LIKE && pQueryInfo->optr != TSDB_RELATION_IN)) { queryIndexedColumn(pSkipList, pQueryInfo, result); } else { queryIndexlessColumn(pSkipList, pQueryInfo, result, param->nodeFilterFn); diff --git a/src/util/src/tcompare.c b/src/util/src/tcompare.c index 09199eaee3..f91d13e6d0 100644 --- a/src/util/src/tcompare.c +++ b/src/util/src/tcompare.c @@ -17,6 +17,7 @@ #include "ttype.h" #include "tcompare.h" #include "tarray.h" +#include "hash.h" int32_t compareInt32Val(const void *pLeft, const void *pRight) { int32_t left = GET_INT32_VAL(pLeft), right = GET_INT32_VAL(pRight); @@ -291,9 +292,12 @@ int32_t taosArrayCompareString(const void* a, const void* b) { return compareLenPrefixedStr(x, y); } -static int32_t compareFindStrInArray(const void* pLeft, const void* pRight) { - const SArray* arr = (const SArray*) pRight; - return taosArraySearchString(arr, pLeft, taosArrayCompareString, TD_EQ) == NULL ? 0 : 1; +//static int32_t compareFindStrInArray(const void* pLeft, const void* pRight) { +// const SArray* arr = (const SArray*) pRight; +// return taosArraySearchString(arr, pLeft, taosArrayCompareString, TD_EQ) == NULL ? 0 : 1; +//} +static int32_t compareFindItemInSet(const void *pLeft, const void* pRight) { + return NULL != taosHashGet((SHashObj *)pRight, varDataVal(pLeft), varDataLen(pLeft)) ? 1 : 0; } static int32_t compareWStrPatternComp(const void* pLeft, const void* pRight) { @@ -325,7 +329,7 @@ __compar_fn_t getComparFunc(int32_t type, int32_t optr) { if (optr == TSDB_RELATION_LIKE) { /* wildcard query using like operator */ comparFn = compareStrPatternComp; } else if (optr == TSDB_RELATION_IN) { - comparFn = compareFindStrInArray; + comparFn = compareFindItemInSet; } else { /* normal relational comparFn */ comparFn = compareLenPrefixedStr; } From 1b7ccdcdc45ae542ac8b80c05052366d6bd820f7 Mon Sep 17 00:00:00 2001 From: tomchon Date: Thu, 3 Jun 2021 15:19:37 +0800 Subject: [PATCH 10/57] temp From b9502d4ba6c663ed1370941f3df0d16a49d17e9c Mon Sep 17 00:00:00 2001 From: tomchon Date: Thu, 3 Jun 2021 15:23:59 +0800 Subject: [PATCH 11/57] temp From 91d2d3b85a37e46098cbe277d3728e0385a0ec02 Mon Sep 17 00:00:00 2001 From: bryanchang0603 Date: Thu, 3 Jun 2021 15:29:23 +0800 Subject: [PATCH 12/57] [TD-4463]rewrite the case to fit develop and community --- tests/pytest/alter/alter_keep.py | 112 +++++++++++++++++++++++-------- 1 file changed, 84 insertions(+), 28 deletions(-) diff --git a/tests/pytest/alter/alter_keep.py b/tests/pytest/alter/alter_keep.py index 37a8a8da55..49b82c7d15 100644 --- a/tests/pytest/alter/alter_keep.py +++ b/tests/pytest/alter/alter_keep.py @@ -22,34 +22,6 @@ class TDTestCase: tdLog.debug("start to execute %s" % __file__) tdSql.init(conn.cursor(), logSql) - - def run(self): - tdSql.prepare() - tdSql.execute('create table tb (ts timestamp, speed int)') - - tdSql.query('show databases') - tdSql.checkData(0,7,'3650,3650,3650') - tdSql.execute('alter database db keep 10') - tdSql.query('show databases') - tdSql.checkData(0,7,'10,10,10') - tdSql.execute('alter database db keep 50') - tdSql.query('show databases') - tdSql.checkData(0,7,'50,50,50') - tdSql.error('alter database db keep !)') - - tdSql.error('alter database db keep 1') - - ## the following sql will not raise error, but will not cause error either - # based on Li Chuang's explaination, <= 0 will not cause keep>days error - # tdSql.error('alter database db keep -10') - # tdSql.query('show databases') - # tdSql.checkData(0,7,'50,50,50') - # tdSql.error('alter database db keep 0') - # tdSql.error('alter database db keep 0.1') - tdSql.error('alter database db keep 10.1') - tdSql.query('show databases') - tdSql.checkData(0,7,'50,50,50') - ##TODO: test keep keep hot alter, cannot be tested for now as test.py's output ## is inconsistent with the actual output. # tdSql.execute('insert into tb values (now, 10)') @@ -68,6 +40,90 @@ class TDTestCase: # os.system('systemctl restart taosd') # tdSql.query('select * from tb') # tdSql.checkRows(2) + + def alterKeepCommunity(self): + ## community accepts both 1 paramater and 3 paramaters + ## comunity should not accept 2 paramaters + tdSql.query('show databases') + tdSql.checkData(0,7,'3650,3650,3650') + + tdSql.execute('alter database db keep 10') + tdSql.query('show databases') + tdSql.checkData(0,7,'10,10,10') + + tdSql.execute('alter database db keep 50') + tdSql.query('show databases') + tdSql.checkData(0,7,'50,50,50') + + tdSql.execute('alter database db keep 20') + tdSql.query('show databases') + tdSql.checkData(0,7,'20,20,20') + + ## the order for altering keep is keep(D), keep0, keep1. + ## if the order is changed, please modify the following test + ## to make sure the the test is accurate + tdSql.execute('alter database db keep 100, 98 ,99') + tdSql.query('show databases') + tdSql.checkData(0,7,'98,99,100') + + tdSql.execute('alter database db keep 200, 200 ,200') + tdSql.query('show databases') + tdSql.checkData(0,7,'200,200,200') + + tdSql.error('alter database db keep 198, 199 ,200') + tdSql.query('show databases') + tdSql.checkData(0,7,'200,200,200') + + tdSql.execute('alter database db keep 3650,3650,3650') + tdSql.error('alter database db keep 4000,4000') + tdSql.error('alter database db keep 5000,50') + tdSql.query('show databases') + tdSql.checkData(0,7,'3650,3650,3650') + + def alterKeepEnterprise(self): + ## enterprise only accept three inputs + ## does not accept 1 paramaters nor 3 paramaters + tdSql.query('show databases') + tdSql.checkData(0,7,'3650,3650,3650') + + tdSql.error('alter database db keep 10') + tdSql.query('show databases') + tdSql.checkData(0,7,'3650,3650,3650') + + ## the order for altering keep is keep(D), keep0, keep1. + ## if the order is changed, please modify the following test + ## to make sure the the test is accurate + + tdSql.execute('alter database db keep 100, 98 ,99') + tdSql.query('show databases') + tdSql.checkData(0,7,'98,99,100') + + tdSql.execute('alter database db keep 200, 200 ,200') + tdSql.query('show databases') + tdSql.checkData(0,7,'200,200,200') + + tdSql.error('alter database db keep 198, 199 ,200') + tdSql.query('show databases') + tdSql.checkData(0,7,'200,200,200') + + tdSql.execute('alter database db keep 3650,3650,3650') + tdSql.error('alter database db keep 4000,3640') + tdSql.error('alter database db keep 10,10') + tdSql.query('show databases') + tdSql.checkData(0,7,'3650,3650,3650') + + def run(self): + tdSql.prepare() + tdSql.execute('create table tb (ts timestamp, speed int)') + selfPath = os.path.dirname(os.path.realpath(__file__)) + + if ("community" in selfPath): + tdLog.debug('running enterprise test') + self.alterKeepEnterprise() + else: + tdLog.debug('running community test') + self.alterKeepCommunity() + def stop(self): tdSql.close() From 904bf2381e1a8cbd55652f4b14ff361c93b9b42d Mon Sep 17 00:00:00 2001 From: bryanchang0603 Date: Thu, 3 Jun 2021 16:04:48 +0800 Subject: [PATCH 13/57] [TD-4463] modifying test case for community version --- tests/pytest/alter/alter_keep.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/tests/pytest/alter/alter_keep.py b/tests/pytest/alter/alter_keep.py index 49b82c7d15..88f3529539 100644 --- a/tests/pytest/alter/alter_keep.py +++ b/tests/pytest/alter/alter_keep.py @@ -42,8 +42,9 @@ class TDTestCase: # tdSql.checkRows(2) def alterKeepCommunity(self): - ## community accepts both 1 paramater and 3 paramaters - ## comunity should not accept 2 paramaters + ## community accepts both 1 paramater, 2 parmaters and 3 paramaters + ## but paramaters other than paramater 1 will be ignored + ## only paramater 1 will be used tdSql.query('show databases') tdSql.checkData(0,7,'3650,3650,3650') @@ -59,26 +60,30 @@ class TDTestCase: tdSql.query('show databases') tdSql.checkData(0,7,'20,20,20') - ## the order for altering keep is keep(D), keep0, keep1. - ## if the order is changed, please modify the following test - ## to make sure the the test is accurate tdSql.execute('alter database db keep 100, 98 ,99') tdSql.query('show databases') - tdSql.checkData(0,7,'98,99,100') + tdSql.checkData(0,7,'100,100,100') - tdSql.execute('alter database db keep 200, 200 ,200') + tdSql.execute('alter database db keep 99, 100 ,101') + tdSql.query('show databases') + tdSql.checkData(0,7,'99,99,99') + + tdSql.execute('alter database db keep 200, 199 ,198') tdSql.query('show databases') tdSql.checkData(0,7,'200,200,200') - tdSql.error('alter database db keep 198, 199 ,200') + tdSql.execute('alter database db keep 4000,4001') tdSql.query('show databases') - tdSql.checkData(0,7,'200,200,200') + tdSql.checkData(0,7,'4000,4000,4000') - tdSql.execute('alter database db keep 3650,3650,3650') - tdSql.error('alter database db keep 4000,4000') - tdSql.error('alter database db keep 5000,50') + tdSql.execute('alter database db keep 5000,50') tdSql.query('show databases') - tdSql.checkData(0,7,'3650,3650,3650') + tdSql.checkData(0,7,'5000,5000,5000') + + tdSql.execute('alter database db keep 50,5000') + tdSql.query('show databases') + tdSql.checkData(0,7,'50,50,50') + def alterKeepEnterprise(self): ## enterprise only accept three inputs From f797c397e436d1d39536fc1adf5adabc50eee028 Mon Sep 17 00:00:00 2001 From: bryanchang0603 Date: Thu, 3 Jun 2021 16:54:15 +0800 Subject: [PATCH 14/57] [TD-4463] enterprise test pass --- tests/pytest/alter/alter_keep.py | 57 ++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/tests/pytest/alter/alter_keep.py b/tests/pytest/alter/alter_keep.py index 88f3529539..cb630963a7 100644 --- a/tests/pytest/alter/alter_keep.py +++ b/tests/pytest/alter/alter_keep.py @@ -21,25 +21,6 @@ class TDTestCase: def init(self, conn, logSql): tdLog.debug("start to execute %s" % __file__) tdSql.init(conn.cursor(), logSql) - - ##TODO: test keep keep hot alter, cannot be tested for now as test.py's output - ## is inconsistent with the actual output. - # tdSql.execute('insert into tb values (now, 10)') - # tdSql.execute('insert into tb values (now + 10m, 10)') - # tdSql.query('select * from tb') - # tdSql.checkRows(2) - # tdSql.execute('alter database db keep 40,40,40') - # os.system('systemctl restart taosd') - # tdSql.execute('insert into tb values (now-60d, 10)') - # tdSql.execute('insert into tb values (now-30d, 10)') - # tdSql.query('select * from tb') - # tdSql.showQueryResult() - # tdSql.checkRows(2) - # tdSql.execute('alter database db keep 20,20,20') - # tdSql.checkRows(3) - # os.system('systemctl restart taosd') - # tdSql.query('select * from tb') - # tdSql.checkRows(2) def alterKeepCommunity(self): ## community accepts both 1 paramater, 2 parmaters and 3 paramaters @@ -99,6 +80,10 @@ class TDTestCase: ## if the order is changed, please modify the following test ## to make sure the the test is accurate + tdSql.execute('alter database db keep 10, 10 ,10') + tdSql.query('show databases') + tdSql.checkData(0,7,'10,10,10') + tdSql.execute('alter database db keep 100, 98 ,99') tdSql.query('show databases') tdSql.checkData(0,7,'98,99,100') @@ -111,15 +96,14 @@ class TDTestCase: tdSql.query('show databases') tdSql.checkData(0,7,'200,200,200') - tdSql.execute('alter database db keep 3650,3650,3650') - tdSql.error('alter database db keep 4000,3640') - tdSql.error('alter database db keep 10,10') - tdSql.query('show databases') - tdSql.checkData(0,7,'3650,3650,3650') + # tdSql.execute('alter database db keep 3650,3650,3650') + # tdSql.error('alter database db keep 4000,3640') + # tdSql.error('alter database db keep 10,10') + # tdSql.query('show databases') + # tdSql.checkData(0,7,'3650,3650,3650') def run(self): tdSql.prepare() - tdSql.execute('create table tb (ts timestamp, speed int)') selfPath = os.path.dirname(os.path.realpath(__file__)) if ("community" in selfPath): @@ -128,6 +112,29 @@ class TDTestCase: else: tdLog.debug('running community test') self.alterKeepCommunity() + + + ##TODO: need to wait for TD-4445 to implement the following + ## tests + # tdSql.prepare() + # tdSql.execute('create table tb (ts timestamp, speed int)') + # tdSql.execute('alter database db keep 10,10,10') + # tdSql.execute('insert into tb values (now, 10)') + # tdSql.execute('insert into tb values (now + 10m, 10)') + # tdSql.query('select * from tb') + # tdSql.checkRows(2) + # tdSql.execute('alter database db keep 40,40,40') + # tdSql.query('show databases') + # tdSql.checkData(0,7,'40,40,40') + # tdSql.error('insert into tb values (now-60d, 10)') + # tdSql.execute('insert into tb values (now-30d, 10)') + # tdSql.query('select * from tb') + # tdSql.checkRows(3) + # tdSql.execute('alter database db keep 20,20,20') + # tdSql.query('show databases') + # tdSql.checkData(0,7,'20,20,20') + # tdSql.query('select * from tb') + # tdSql.checkRows(2) def stop(self): From fc2688d5438103eaffc5fe78897eafa218240b4c Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Fri, 4 Jun 2021 07:43:59 +0800 Subject: [PATCH 15/57] [TD-4426] merge in --- src/client/src/tscSQLParser.c | 15 ++++++--------- src/tsdb/src/tsdbRead.c | 3 ++- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index bcd3e294d1..7d6a5c30da 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -160,22 +160,20 @@ bool serializeExprListToVariant(SArray* pList, tVariant **dst) { tSqlExprItem* item = (tSqlExprItem *)taosArrayGet(pList, 0); int32_t size = pList->size; int32_t type = item->pNode->token.type; - if (type < 0) { - tbufCloseWriter(&bw); - return ret; - } - toTSDBType(item->pNode->token.type); - tbufWriteUint32(&bw, item->pNode->token.type); + toTSDBType(type); + tbufWriteUint32(&bw, type); tbufWriteInt32(&bw, size); for (int32_t i = 0; i < size; i++) { tSqlExpr* pSub = ((tSqlExprItem*)(taosArrayGet(pList, i)))->pNode; - // validate type in exprList + // type in exprList is same or not + toTSDBType(pSub->token.type); if (type != pSub->token.type) { break; } + tVariant var; tVariantCreate(&var, &pSub->token); if (type == TSDB_DATA_TYPE_BOOL || type == TSDB_DATA_TYPE_TINYINT || type == TSDB_DATA_TYPE_SMALLINT @@ -3292,8 +3290,7 @@ static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, tVariantDestroy(pVal); free(pVal); - } - else if (colType == TSDB_DATA_TYPE_BINARY) { + } else if (colType == TSDB_DATA_TYPE_BINARY) { pColumnFilter->pz = (int64_t)calloc(1, bufLen * TSDB_NCHAR_SIZE); pColumnFilter->len = pRight->value.nLen; retVal = tVariantDump(&pRight->value, (char*)pColumnFilter->pz, colType, false); diff --git a/src/tsdb/src/tsdbRead.c b/src/tsdb/src/tsdbRead.c index bd609a6708..436094fe2d 100644 --- a/src/tsdb/src/tsdbRead.c +++ b/src/tsdb/src/tsdbRead.c @@ -3163,7 +3163,8 @@ void filterPrepare(void* expr, void* param) { pObj = taosHashInit(256, taosGetDefaultHashFunction(pInfo->sch.type), true, false); SArray *arr = (SArray *)(pCond->arr); for (size_t i = 0; i < taosArrayGetSize(arr); i++) { - taosHashPut(pObj, (char *)taosArrayGet(arr, i), arr->elemSize, &dummy, sizeof(dummy)); + char* p = taosArrayGetP(arr, i); + taosHashPut(pObj, varDataVal(p),varDataLen(p), &dummy, sizeof(dummy)); } } else { buildFilterSetFromBinary((void **)&pObj, pCond->pz, pCond->nLen); From b0281578d557b1825a6af85c6fd5940a73cead78 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Fri, 4 Jun 2021 11:49:56 +0800 Subject: [PATCH 16/57] [td-4529] diff support super table query. --- src/client/inc/tscUtil.h | 4 +- src/client/inc/tsclient.h | 2 +- src/client/src/tscAsync.c | 8 ++-- src/client/src/tscLocal.c | 2 +- src/client/src/tscSQLParser.c | 11 +++--- src/client/src/tscServer.c | 9 +++-- src/client/src/tscSql.c | 2 +- src/client/src/tscSubquery.c | 8 ++-- src/client/src/tscUtil.c | 72 ++++++++++++++++------------------- src/common/inc/tcmdtype.h | 2 +- src/query/inc/qExecutor.h | 2 + src/query/inc/qTableMeta.h | 1 + src/query/src/qAggMain.c | 8 ++-- src/query/src/qExecutor.c | 27 ++++++++++--- src/query/src/qPlan.c | 4 +- src/query/src/queryMain.c | 7 ++-- 16 files changed, 92 insertions(+), 77 deletions(-) diff --git a/src/client/inc/tscUtil.h b/src/client/inc/tscUtil.h index f747184ce0..448fee936f 100644 --- a/src/client/inc/tscUtil.h +++ b/src/client/inc/tscUtil.h @@ -123,6 +123,7 @@ int32_t tscGetDataBlockFromList(SHashObj* pHashList, int64_t id, int32_t size, i */ bool tscIsPointInterpQuery(SQueryInfo* pQueryInfo); bool tscIsTWAQuery(SQueryInfo* pQueryInfo); +bool tscIsDiffQuery(SQueryInfo* pQueryInfo); bool tscIsSessionWindowQuery(SQueryInfo* pQueryInfo); bool tscIsSecondStageQuery(SQueryInfo* pQueryInfo); bool tsIsArithmeticQueryOnAggResult(SQueryInfo* pQueryInfo); @@ -132,7 +133,6 @@ bool hasTagValOutput(SQueryInfo* pQueryInfo); bool timeWindowInterpoRequired(SQueryInfo *pQueryInfo); bool isStabledev(SQueryInfo* pQueryInfo); bool isTsCompQuery(SQueryInfo* pQueryInfo); -bool isSimpleAggregate(SQueryInfo* pQueryInfo); bool isBlockDistQuery(SQueryInfo* pQueryInfo); bool isSimpleAggregateRv(SQueryInfo* pQueryInfo); @@ -331,7 +331,7 @@ SVgroupsInfo* tscVgroupsInfoDup(SVgroupsInfo* pVgroupsInfo); int32_t tscCreateQueryFromQueryInfo(SQueryInfo* pQueryInfo, SQueryAttr* pQueryAttr, void* addr); void tsCreateSQLFunctionCtx(SQueryInfo* pQueryInfo, SQLFunctionCtx* pCtx, SSchema* pSchema); -void* createQInfoFromQueryNode(SQueryInfo* pQueryInfo, STableGroupInfo* pTableGroupInfo, SOperatorInfo* pOperator, char* sql, void* addr, int32_t stage); +void* createQInfoFromQueryNode(SQueryInfo* pQueryInfo, STableGroupInfo* pTableGroupInfo, SOperatorInfo* pOperator, char* sql, void* addr, int32_t stage, uint64_t qId); void* malloc_throw(size_t size); void* calloc_throw(size_t nmemb, size_t size); diff --git a/src/client/inc/tsclient.h b/src/client/inc/tsclient.h index 67fd34ffd7..46aad90c42 100644 --- a/src/client/inc/tsclient.h +++ b/src/client/inc/tsclient.h @@ -319,7 +319,7 @@ int32_t tscCreateResPointerInfo(SSqlRes *pRes, SQueryInfo *pQueryInfo); void tscSetResRawPtr(SSqlRes* pRes, SQueryInfo* pQueryInfo); void tscSetResRawPtrRv(SSqlRes* pRes, SQueryInfo* pQueryInfo, SSDataBlock* pBlock); -void handleDownstreamOperator(SSqlObj** pSqlList, int32_t numOfUpstream, SQueryInfo* px, SSqlRes* pOutput); +void handleDownstreamOperator(SSqlObj** pSqlList, int32_t numOfUpstream, SQueryInfo* px, SSqlObj* pParent); void destroyTableNameList(SInsertStatementParam* pInsertParam); void tscResetSqlCmd(SSqlCmd *pCmd, bool removeMeta); diff --git a/src/client/src/tscAsync.c b/src/client/src/tscAsync.c index 15276a3888..d857d00e15 100644 --- a/src/client/src/tscAsync.c +++ b/src/client/src/tscAsync.c @@ -144,7 +144,7 @@ static void tscAsyncFetchRowsProxy(void *param, TAOS_RES *tres, int numOfRows) { } // local merge has handle this situation during super table non-projection query. - if (pCmd->command != TSDB_SQL_RETRIEVE_LOCALMERGE) { + if (pCmd->command != TSDB_SQL_RETRIEVE_GLOBALMERGE) { pRes->numOfClauseTotal += pRes->numOfRows; } @@ -174,7 +174,7 @@ static void tscProcessAsyncRetrieveImpl(void *param, TAOS_RES *tres, int numOfRo } pSql->fp = fp; - if (pCmd->command != TSDB_SQL_RETRIEVE_LOCALMERGE && pCmd->command < TSDB_SQL_LOCAL) { + if (pCmd->command != TSDB_SQL_RETRIEVE_GLOBALMERGE && pCmd->command < TSDB_SQL_LOCAL) { pCmd->command = (pCmd->command > TSDB_SQL_MGMT) ? TSDB_SQL_RETRIEVE : TSDB_SQL_FETCH; } @@ -257,14 +257,14 @@ void taos_fetch_rows_a(TAOS_RES *tres, __async_cb_func_t fp, void *param) { } return; - } else if (pCmd->command == TSDB_SQL_RETRIEVE || pCmd->command == TSDB_SQL_RETRIEVE_LOCALMERGE) { + } else if (pCmd->command == TSDB_SQL_RETRIEVE || pCmd->command == TSDB_SQL_RETRIEVE_GLOBALMERGE) { // in case of show command, return no data (*pSql->fetchFp)(param, pSql, 0); } else { assert(0); } } else { // current query is not completed, continue retrieve from node - if (pCmd->command != TSDB_SQL_RETRIEVE_LOCALMERGE && pCmd->command < TSDB_SQL_LOCAL) { + if (pCmd->command != TSDB_SQL_RETRIEVE_GLOBALMERGE && pCmd->command < TSDB_SQL_LOCAL) { pCmd->command = (pCmd->command > TSDB_SQL_MGMT) ? TSDB_SQL_RETRIEVE : TSDB_SQL_FETCH; } diff --git a/src/client/src/tscLocal.c b/src/client/src/tscLocal.c index f97f54a626..2c9717306f 100644 --- a/src/client/src/tscLocal.c +++ b/src/client/src/tscLocal.c @@ -323,7 +323,7 @@ TAOS_ROW tscFetchRow(void *param) { // current data set are exhausted, fetch more data from node if (pRes->row >= pRes->numOfRows && (pRes->completed != true || hasMoreVnodesToTry(pSql) || hasMoreClauseToTry(pSql)) && (pCmd->command == TSDB_SQL_RETRIEVE || - pCmd->command == TSDB_SQL_RETRIEVE_LOCALMERGE || + pCmd->command == TSDB_SQL_RETRIEVE_GLOBALMERGE || pCmd->command == TSDB_SQL_TABLE_JOIN_RETRIEVE || pCmd->command == TSDB_SQL_FETCH || pCmd->command == TSDB_SQL_SHOW || diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 15e40d5918..5d712bb980 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -2928,8 +2928,8 @@ void tscRestoreFuncForSTableQuery(SQueryInfo* pQueryInfo) { } bool hasUnsupportFunctionsForSTableQuery(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) { - const char* msg1 = "TWA not allowed to apply to super table directly"; - const char* msg2 = "TWA only support group by tbname for super table query"; + const char* msg1 = "TWA/Diff not allowed to apply to super table directly"; + const char* msg2 = "TWA/Diff only support group by tbname for super table query"; const char* msg3 = "function not support for super table query"; // filter sql function not supported by metric query yet. @@ -2942,7 +2942,7 @@ bool hasUnsupportFunctionsForSTableQuery(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) } } - if (tscIsTWAQuery(pQueryInfo)) { + if (tscIsTWAQuery(pQueryInfo) || tscIsDiffQuery(pQueryInfo)) { if (pQueryInfo->groupbyExpr.numOfGroupCols == 0) { invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); return true; @@ -6276,7 +6276,7 @@ int32_t doFunctionsCompatibleCheck(SSqlCmd* pCmd, SQueryInfo* pQueryInfo) { } if (IS_MULTIOUTPUT(aAggs[functId].status) && functId != TSDB_FUNC_TOP && functId != TSDB_FUNC_BOTTOM && - functId != TSDB_FUNC_TAGPRJ && functId != TSDB_FUNC_PRJ) { + functId != TSDB_FUNC_DIFF && functId != TSDB_FUNC_TAGPRJ && functId != TSDB_FUNC_PRJ) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); } @@ -6758,7 +6758,7 @@ int32_t doCheckForStream(SSqlObj* pSql, SSqlInfo* pInfo) { const char* msg6 = "from missing in subclause"; const char* msg7 = "time interval is required"; const char* msg8 = "the first column should be primary timestamp column"; - + SSqlCmd* pCmd = &pSql->cmd; SQueryInfo* pQueryInfo = tscGetQueryInfo(pCmd); assert(pQueryInfo->numOfTables == 1); @@ -7719,6 +7719,7 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf pQueryInfo->arithmeticOnAgg = tsIsArithmeticQueryOnAggResult(pQueryInfo); pQueryInfo->orderProjectQuery = tscOrderedProjectionQueryOnSTable(pQueryInfo, 0); + pQueryInfo->diffQuery = tscIsDiffQuery(pQueryInfo); SExprInfo** p = NULL; int32_t numOfExpr = 0; diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 2763005303..0fd8d6a0c7 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -1588,7 +1588,7 @@ int tscProcessLocalRetrieveRsp(SSqlObj *pSql) { return tscLocalResultCommonBuilder(pSql, numOfRes); } -int tscProcessRetrieveLocalMergeRsp(SSqlObj *pSql) { +int tscProcessRetrieveGlobalMergeRsp(SSqlObj *pSql) { SSqlRes *pRes = &pSql->res; SSqlCmd* pCmd = &pSql->cmd; @@ -1615,10 +1615,11 @@ int tscProcessRetrieveLocalMergeRsp(SSqlObj *pSql) { taosArrayPush(group, &tableKeyInfo); taosArrayPush(tableGroupInfo.pGroupList, &group); - pQueryInfo->pQInfo = createQInfoFromQueryNode(pQueryInfo, &tableGroupInfo, NULL, NULL, pRes->pMerger, MERGE_STAGE); + tscDebug("0x%"PRIx64" create QInfo 0x%"PRIx64" to execute query processing", pSql->self, pSql->self); + pQueryInfo->pQInfo = createQInfoFromQueryNode(pQueryInfo, &tableGroupInfo, NULL, NULL, pRes->pMerger, MERGE_STAGE, pSql->self); } - uint64_t localQueryId = 0; + uint64_t localQueryId = pSql->self; qTableQuery(pQueryInfo->pQInfo, &localQueryId); convertQueryResult(pRes, pQueryInfo); @@ -2689,7 +2690,7 @@ void tscInitMsgsFp() { tscProcessMsgRsp[TSDB_SQL_RETRIEVE_EMPTY_RESULT] = tscProcessEmptyResultRsp; - tscProcessMsgRsp[TSDB_SQL_RETRIEVE_LOCALMERGE] = tscProcessRetrieveLocalMergeRsp; + tscProcessMsgRsp[TSDB_SQL_RETRIEVE_GLOBALMERGE] = tscProcessRetrieveGlobalMergeRsp; tscProcessMsgRsp[TSDB_SQL_ALTER_TABLE] = tscProcessAlterTableMsgRsp; tscProcessMsgRsp[TSDB_SQL_ALTER_DB] = tscProcessAlterDbMsgRsp; diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index 554ce351eb..1e93892876 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -456,7 +456,7 @@ static bool needToFetchNewBlock(SSqlObj* pSql) { return (pRes->completed != true || hasMoreVnodesToTry(pSql) || hasMoreClauseToTry(pSql)) && (pCmd->command == TSDB_SQL_RETRIEVE || - pCmd->command == TSDB_SQL_RETRIEVE_LOCALMERGE || + pCmd->command == TSDB_SQL_RETRIEVE_GLOBALMERGE || pCmd->command == TSDB_SQL_TABLE_JOIN_RETRIEVE || pCmd->command == TSDB_SQL_FETCH || pCmd->command == TSDB_SQL_SHOW || diff --git a/src/client/src/tscSubquery.c b/src/client/src/tscSubquery.c index 4b1b3e9080..431357bf08 100644 --- a/src/client/src/tscSubquery.c +++ b/src/client/src/tscSubquery.c @@ -2422,7 +2422,7 @@ int32_t tscHandleMasterSTableQuery(SSqlObj *pSql) { // pRes->code check only serves in launching metric sub-queries if (pRes->code == TSDB_CODE_TSC_QUERY_CANCELLED) { - pCmd->command = TSDB_SQL_RETRIEVE_LOCALMERGE; // enable the abort of kill super table function. + pCmd->command = TSDB_SQL_RETRIEVE_GLOBALMERGE; // enable the abort of kill super table function. return pRes->code; } @@ -2778,7 +2778,7 @@ static void tscAllDataRetrievedFromDnode(SRetrieveSupport *trsupport, SSqlObj* p if (code == TSDB_CODE_SUCCESS && trsupport->pExtMemBuffer == NULL) { pParentSql->cmd.command = TSDB_SQL_RETRIEVE_EMPTY_RESULT; // no result, set the result empty } else { - pParentSql->cmd.command = TSDB_SQL_RETRIEVE_LOCALMERGE; + pParentSql->cmd.command = TSDB_SQL_RETRIEVE_GLOBALMERGE; } tscCreateResPointerInfo(&pParentSql->res, pPQueryInfo); @@ -3500,7 +3500,7 @@ static UNUSED_FUNC bool tscHasRemainDataInSubqueryResultSet(SSqlObj *pSql) { } void* createQInfoFromQueryNode(SQueryInfo* pQueryInfo, STableGroupInfo* pTableGroupInfo, SOperatorInfo* pSourceOperator, - char* sql, void* merger, int32_t stage) { + char* sql, void* merger, int32_t stage, uint64_t qId) { assert(pQueryInfo != NULL); SQInfo *pQInfo = (SQInfo *)calloc(1, sizeof(SQInfo)); if (pQInfo == NULL) { @@ -3509,7 +3509,7 @@ void* createQInfoFromQueryNode(SQueryInfo* pQueryInfo, STableGroupInfo* pTableGr // to make sure third party won't overwrite this structure pQInfo->signature = pQInfo; - + pQInfo->qId = qId; SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; SQueryAttr *pQueryAttr = &pQInfo->query; diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 8042f032c8..7a9e2d2289 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -222,6 +222,8 @@ bool tscIsProjectionQueryOnSTable(SQueryInfo* pQueryInfo, int32_t tableIndex) { functionId != TSDB_FUNC_TS && functionId != TSDB_FUNC_ARITHM && functionId != TSDB_FUNC_TS_COMP && + functionId != TSDB_FUNC_DIFF && + functionId != TSDB_FUNC_TS_DUMMY && functionId != TSDB_FUNC_TID_TAG) { return false; } @@ -434,6 +436,23 @@ bool tscIsTWAQuery(SQueryInfo* pQueryInfo) { return false; } + +bool tscIsDiffQuery(SQueryInfo* pQueryInfo) { + size_t num = tscNumOfExprs(pQueryInfo); + for(int32_t i = 0; i < num; ++i) { + SExprInfo* pExpr = tscExprGet(pQueryInfo, i); + if (pExpr == NULL || pExpr->base.functionId == TSDB_FUNC_TS_DUMMY) { + continue; + } + + if (pExpr->base.functionId == TSDB_FUNC_DIFF) { + return true; + } + } + + return false; +} + bool tscIsSessionWindowQuery(SQueryInfo* pQueryInfo) { return pQueryInfo->sessionWindow.gap > 0; } @@ -467,42 +486,12 @@ bool tscNeedReverseScan(SQueryInfo* pQueryInfo) { return false; } -bool isSimpleAggregate(SQueryInfo* pQueryInfo) { - if (pQueryInfo->interval.interval > 0) { - return false; - } - - // Note:top/bottom query is fixed output query - if (tscIsTopBotQuery(pQueryInfo) || tscGroupbyColumn(pQueryInfo) || isTsCompQuery(pQueryInfo)) { - return true; - } - - size_t numOfExprs = tscNumOfExprs(pQueryInfo); - for (int32_t i = 0; i < numOfExprs; ++i) { - SExprInfo* pExpr = tscExprGet(pQueryInfo, i); - if (pExpr == NULL) { - continue; - } - - int32_t functionId = pExpr->base.functionId; - if (functionId == TSDB_FUNC_TS || functionId == TSDB_FUNC_TS_DUMMY) { - continue; - } - - if (!IS_MULTIOUTPUT(aAggs[functionId].status)) { - return true; - } - } - - return false; -} - bool isSimpleAggregateRv(SQueryInfo* pQueryInfo) { if (pQueryInfo->interval.interval > 0 || pQueryInfo->sessionWindow.gap > 0) { return false; } - if (tscGroupbyColumn(pQueryInfo) || isTsCompQuery(pQueryInfo)) { + if (tscGroupbyColumn(pQueryInfo) || isTsCompQuery(pQueryInfo) || tscIsTopBotQuery(pQueryInfo) || tscIsDiffQuery(pQueryInfo)) { return false; } @@ -812,6 +801,7 @@ static void fetchNextBlockIfCompleted(SOperatorInfo* pOperator, bool* newgroup) for (int32_t i = 0; i < pOperator->numOfUpstream; ++i) { SJoinStatus* pStatus = &pJoinInfo->status[i]; if (pStatus->pBlock == NULL || pStatus->index >= pStatus->pBlock->info.rows) { + tscDebug("Retrieve nest query result, index:%d, total:%d", i, pOperator->numOfUpstream); pStatus->pBlock = pOperator->upstream[i]->exec(pOperator->upstream[i], newgroup); pStatus->index = 0; @@ -1088,7 +1078,9 @@ static void createInputDataFilterInfo(SQueryInfo* px, int32_t numOfCol1, int32_t tfree(tableCols); } -void handleDownstreamOperator(SSqlObj** pSqlObjList, int32_t numOfUpstream, SQueryInfo* px, SSqlRes* pOutput) { +void handleDownstreamOperator(SSqlObj** pSqlObjList, int32_t numOfUpstream, SQueryInfo* px, SSqlObj* pSql) { + SSqlRes* pOutput = &pSql->res; + // handle the following query process if (px->pQInfo == NULL) { SColumnInfo* pColumnInfo = extractColumnInfoFromResult(px->colList); @@ -1166,7 +1158,9 @@ void handleDownstreamOperator(SSqlObj** pSqlObjList, int32_t numOfUpstream, SQue } } - px->pQInfo = createQInfoFromQueryNode(px, &tableGroupInfo, pSourceOperator, NULL, NULL, MASTER_SCAN); + tscDebug("0x%"PRIx64" create QInfo 0x%"PRIx64" to execute the main query while all nest query is ready", pSql->self, pSql->self); + px->pQInfo = createQInfoFromQueryNode(px, &tableGroupInfo, pSourceOperator, NULL, NULL, MASTER_SCAN, pSql->self); + tfree(pColumnInfo); tfree(schema); @@ -1174,7 +1168,7 @@ void handleDownstreamOperator(SSqlObj** pSqlObjList, int32_t numOfUpstream, SQue pSourceOperator->pRuntimeEnv = &px->pQInfo->runtimeEnv; } - uint64_t qId = 0; + uint64_t qId = pSql->self; qTableQuery(px->pQInfo, &qId); convertQueryResult(pOutput, px); } @@ -1372,7 +1366,7 @@ void tscFreeSqlObj(SSqlObj* pSql) { SSqlCmd* pCmd = &pSql->cmd; int32_t cmd = pCmd->command; - if (cmd < TSDB_SQL_INSERT || cmd == TSDB_SQL_RETRIEVE_LOCALMERGE || cmd == TSDB_SQL_RETRIEVE_EMPTY_RESULT || + if (cmd < TSDB_SQL_INSERT || cmd == TSDB_SQL_RETRIEVE_GLOBALMERGE || cmd == TSDB_SQL_RETRIEVE_EMPTY_RESULT || cmd == TSDB_SQL_TABLE_JOIN_RETRIEVE) { tscRemoveFromSqlList(pSql); } @@ -3436,7 +3430,7 @@ void doRetrieveSubqueryData(SSchedMsg *pMsg) { } SQueryInfo *pQueryInfo = tscGetQueryInfo(&pSql->cmd); - handleDownstreamOperator(pSql->pSubs, pSql->subState.numOfSub, pQueryInfo, &pSql->res); + handleDownstreamOperator(pSql->pSubs, pSql->subState.numOfSub, pQueryInfo, pSql); pSql->res.qId = -1; if (pSql->res.code == TSDB_CODE_SUCCESS) { @@ -4236,10 +4230,11 @@ int32_t tscCreateQueryFromQueryInfo(SQueryInfo* pQueryInfo, SQueryAttr* pQueryAt pQueryAttr->hasTagResults = hasTagValOutput(pQueryInfo); pQueryAttr->stabledev = isStabledev(pQueryInfo); pQueryAttr->tsCompQuery = isTsCompQuery(pQueryInfo); - pQueryAttr->simpleAgg = isSimpleAggregate(pQueryInfo); + pQueryAttr->diffQuery = pQueryInfo->diffQuery; + pQueryAttr->simpleAgg = pQueryInfo->simpleAgg; pQueryAttr->needReverseScan = tscNeedReverseScan(pQueryInfo); pQueryAttr->stableQuery = QUERY_IS_STABLE_QUERY(pQueryInfo->type); - pQueryAttr->groupbyColumn = (!pQueryInfo->stateWindow) && tscGroupbyColumn(pQueryInfo); + pQueryAttr->groupbyColumn = (!pQueryInfo->stateWindow) && pQueryInfo->groupbyColumn; pQueryAttr->queryBlockDist = isBlockDistQuery(pQueryInfo); pQueryAttr->pointInterpQuery = tscIsPointInterpQuery(pQueryInfo); pQueryAttr->timeWindowInterpo = timeWindowInterpoRequired(pQueryInfo); @@ -4255,7 +4250,6 @@ int32_t tscCreateQueryFromQueryInfo(SQueryInfo* pQueryInfo, SQueryAttr* pQueryAt pQueryAttr->fillType = pQueryInfo->fillType; pQueryAttr->havingNum = pQueryInfo->havingFieldNum; - if (pQueryInfo->order.order == TSDB_ORDER_ASC) { // TODO refactor pQueryAttr->window = pQueryInfo->window; } else { diff --git a/src/common/inc/tcmdtype.h b/src/common/inc/tcmdtype.h index adf210cfeb..90426519e6 100644 --- a/src/common/inc/tcmdtype.h +++ b/src/common/inc/tcmdtype.h @@ -76,7 +76,7 @@ enum { // SQL below for client local TSDB_DEFINE_SQL_TYPE( TSDB_SQL_LOCAL, "local" ) TSDB_DEFINE_SQL_TYPE( TSDB_SQL_DESCRIBE_TABLE, "describe-table" ) - TSDB_DEFINE_SQL_TYPE( TSDB_SQL_RETRIEVE_LOCALMERGE, "retrieve-localmerge" ) + TSDB_DEFINE_SQL_TYPE( TSDB_SQL_RETRIEVE_GLOBALMERGE, "retrieve-globalmerge" ) TSDB_DEFINE_SQL_TYPE( TSDB_SQL_TABLE_JOIN_RETRIEVE, "join-retrieve" ) TSDB_DEFINE_SQL_TYPE( TSDB_SQL_SHOW_CREATE_TABLE, "show-create-table") diff --git a/src/query/inc/qExecutor.h b/src/query/inc/qExecutor.h index b5b4fb5854..93f2b73d7a 100644 --- a/src/query/inc/qExecutor.h +++ b/src/query/inc/qExecutor.h @@ -185,6 +185,7 @@ typedef struct SQueryAttr { bool queryBlockDist; // if query data block distribution bool stabledev; // super table stddev query bool tsCompQuery; // is tscomp query + bool diffQuery; // is diff query bool simpleAgg; bool pointInterpQuery; // point interpolation query bool needReverseScan; // need reverse scan @@ -386,6 +387,7 @@ typedef struct STableScanInfo { int64_t elapsedTime; int32_t tableIndex; + int32_t prevGroupId; // previous table group id } STableScanInfo; typedef struct STagScanInfo { diff --git a/src/query/inc/qTableMeta.h b/src/query/inc/qTableMeta.h index 4fc252b644..1fd78ed324 100644 --- a/src/query/inc/qTableMeta.h +++ b/src/query/inc/qTableMeta.h @@ -138,6 +138,7 @@ typedef struct SQueryInfo { bool hasFilter; bool onlyTagQuery; bool orderProjectQuery; + bool diffQuery; bool stateWindow; } SQueryInfo; diff --git a/src/query/src/qAggMain.c b/src/query/src/qAggMain.c index ba6efcabb2..74c95e7281 100644 --- a/src/query/src/qAggMain.c +++ b/src/query/src/qAggMain.c @@ -3393,7 +3393,7 @@ enum { }; static bool diff_function_setup(SQLFunctionCtx *pCtx) { - if (function_setup(pCtx)) { + if (!function_setup(pCtx)) { return false; } @@ -4606,7 +4606,7 @@ static void rate_function_f(SQLFunctionCtx *pCtx, int32_t index) { pRateInfo->lastValue = v; pRateInfo->lastKey = primaryKey[index]; - + SET_VAL(pCtx, 1, 1); // set has result flag @@ -4630,7 +4630,7 @@ static void rate_func_copy(SQLFunctionCtx *pCtx) { static void rate_finalizer(SQLFunctionCtx *pCtx) { SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); SRateInfo *pRateInfo = (SRateInfo *)GET_ROWCELL_INTERBUF(pResInfo); - + if (pRateInfo->hasResult != DATA_SET_FLAG) { setNull(pCtx->pOutput, TSDB_DATA_TYPE_DOUBLE, sizeof(double)); return; @@ -5215,7 +5215,7 @@ SAggFunctionInfo aAggs[] = {{ "diff", TSDB_FUNC_DIFF, TSDB_FUNC_INVALID_ID, - TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_NEED_TS, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS, diff_function_setup, diff_function, diff_function_f, diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 9e1534eb24..7972649e25 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -2652,7 +2652,7 @@ int32_t loadDataBlockOnDemand(SQueryRuntimeEnv* pRuntimeEnv, STableScanInfo* pTa pTableScanInfo->rowCellInfoOffset) != TSDB_CODE_SUCCESS) { longjmp(pRuntimeEnv->env, TSDB_CODE_QRY_OUT_OF_MEMORY); } - } else if (pQueryAttr->stableQuery && (!pQueryAttr->tsCompQuery)) { // stable aggregate, not interval aggregate or normal column aggregate + } else if (pQueryAttr->stableQuery && (!pQueryAttr->tsCompQuery) && (!pQueryAttr->diffQuery)) { // stable aggregate, not interval aggregate or normal column aggregate doSetTableGroupOutputBuf(pRuntimeEnv, pTableScanInfo->pResultRowInfo, pTableScanInfo->pCtx, pTableScanInfo->rowCellInfoOffset, pTableScanInfo->numOfOutput, pRuntimeEnv->current->groupIndex); @@ -4273,6 +4273,12 @@ static SSDataBlock* doTableScanImpl(void* param, bool* newgroup) { pRuntimeEnv->current = *pTableQueryInfo; doTableQueryInfoTimeWindowCheck(pQueryAttr, *pTableQueryInfo); + + if (pTableScanInfo->prevGroupId != -1 && pTableScanInfo->prevGroupId != (*pTableQueryInfo)->groupIndex) { + *newgroup = true; + } + + pTableScanInfo->prevGroupId = (*pTableQueryInfo)->groupIndex; } // this function never returns error? @@ -4419,6 +4425,7 @@ SOperatorInfo* createTableScanOperator(void* pTsdbQueryHandle, SQueryRuntimeEnv* pInfo->reverseTimes = 0; pInfo->order = pRuntimeEnv->pQueryAttr->order.order; pInfo->current = 0; +// pInfo->prevGroupId = -1; SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); pOperator->name = "TableScanOperator"; @@ -4441,6 +4448,7 @@ SOperatorInfo* createTableSeqScanOperator(void* pTsdbQueryHandle, SQueryRuntimeE pInfo->reverseTimes = 0; pInfo->order = pRuntimeEnv->pQueryAttr->order.order; pInfo->current = 0; + pInfo->prevGroupId = -1; SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); pOperator->name = "TableSeqScanOperator"; @@ -4545,6 +4553,7 @@ SOperatorInfo* createDataBlocksOptScanInfo(void* pTsdbQueryHandle, SQueryRuntime pInfo->reverseTimes = reverseTime; pInfo->current = 0; pInfo->order = pRuntimeEnv->pQueryAttr->order.order; +// pInfo->prevGroupId = -1; SOperatorInfo* pOptr = calloc(1, sizeof(SOperatorInfo)); pOptr->name = "DataBlocksOptimizedScanOperator"; @@ -4923,10 +4932,17 @@ static SSDataBlock* doArithmeticOperation(void* param, bool* newgroup) { } // Return result of the previous group in the firstly. - if (*newgroup && pRes->info.rows > 0) { - pArithInfo->existDataBlock = pBlock; - clearNumOfRes(pInfo->pCtx, pOperator->numOfOutput); - return pInfo->pRes; + if (*newgroup) { + if (pRes->info.rows > 0) { + pArithInfo->existDataBlock = pBlock; + clearNumOfRes(pInfo->pCtx, pOperator->numOfOutput); + return pInfo->pRes; + } else { // init output buffer for a new group data + for (int32_t j = 0; j < pOperator->numOfOutput; ++j) { + aAggs[pInfo->pCtx[j].functionId].xFinalize(&pInfo->pCtx[j]); + } + initCtxOutputBuffer(pInfo->pCtx, pOperator->numOfOutput); + } } STableQueryInfo* pTableQueryInfo = pRuntimeEnv->current; @@ -7442,7 +7458,6 @@ int32_t doDumpQueryResult(SQInfo *pQInfo, char *data) { doCopyQueryResultToMsg(pQInfo, (int32_t)pRuntimeEnv->outputBuf->info.rows, data); } - pRuntimeEnv->resultInfo.total += pRuntimeEnv->outputBuf->info.rows; qDebug("QInfo:0x%"PRIx64" current numOfRes rows:%d, total:%" PRId64, pQInfo->qId, pRuntimeEnv->outputBuf->info.rows, pRuntimeEnv->resultInfo.total); diff --git a/src/query/src/qPlan.c b/src/query/src/qPlan.c index 24531c7f4e..ee587a515d 100644 --- a/src/query/src/qPlan.c +++ b/src/query/src/qPlan.c @@ -531,7 +531,7 @@ SArray* createTableScanPlan(SQueryAttr* pQueryAttr) { } else { if (pQueryAttr->queryBlockDist) { op = OP_TableBlockInfoScan; - } else if (pQueryAttr->tsCompQuery || pQueryAttr->pointInterpQuery) { + } else if (pQueryAttr->tsCompQuery || pQueryAttr->pointInterpQuery || pQueryAttr->diffQuery) { op = OP_TableSeqScan; } else if (pQueryAttr->needReverseScan) { op = OP_DataBlocksOptScan; @@ -605,7 +605,7 @@ SArray* createExecOperatorPlan(SQueryAttr* pQueryAttr) { taosArrayPush(plan, &op); } } else if (pQueryAttr->simpleAgg) { - if (pQueryAttr->stableQuery && !pQueryAttr->tsCompQuery) { + if (pQueryAttr->stableQuery && !pQueryAttr->tsCompQuery && !pQueryAttr->diffQuery) { op = OP_MultiTableAggregate; } else { op = OP_Aggregate; diff --git a/src/query/src/queryMain.c b/src/query/src/queryMain.c index f00d5ceb41..fa2eac6619 100644 --- a/src/query/src/queryMain.c +++ b/src/query/src/queryMain.c @@ -241,15 +241,16 @@ bool qTableQuery(qinfo_t qinfo, uint64_t *qId) { bool newgroup = false; pRuntimeEnv->outputBuf = pRuntimeEnv->proot->exec(pRuntimeEnv->proot, &newgroup); + pRuntimeEnv->resultInfo.total += GET_NUM_OF_RESULTS(pRuntimeEnv); if (isQueryKilled(pQInfo)) { qDebug("QInfo:0x%"PRIx64" query is killed", pQInfo->qId); } else if (GET_NUM_OF_RESULTS(pRuntimeEnv) == 0) { - qDebug("QInfo:0x%"PRIx64" over, %u tables queried, %"PRId64" rows are returned", pQInfo->qId, pRuntimeEnv->tableqinfoGroupInfo.numOfTables, + qDebug("QInfo:0x%"PRIx64" over, %u tables queried, total %"PRId64" rows returned", pQInfo->qId, pRuntimeEnv->tableqinfoGroupInfo.numOfTables, pRuntimeEnv->resultInfo.total); } else { - qDebug("QInfo:0x%"PRIx64" query paused, %d rows returned, numOfTotal:%" PRId64 " rows", - pQInfo->qId, GET_NUM_OF_RESULTS(pRuntimeEnv), pRuntimeEnv->resultInfo.total + GET_NUM_OF_RESULTS(pRuntimeEnv)); + qDebug("QInfo:0x%"PRIx64" query paused, %d rows returned, total:%" PRId64 " rows", pQInfo->qId, + GET_NUM_OF_RESULTS(pRuntimeEnv), pRuntimeEnv->resultInfo.total); } return doBuildResCheck(pQInfo); From e6072702ebce4342a71d141aee73ed45c228eac6 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Fri, 4 Jun 2021 12:47:57 +0800 Subject: [PATCH 17/57] [td-4529] update log and merge develop. --- src/client/src/tscUtil.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 659f8399de..d5693a29bf 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -1160,7 +1160,7 @@ void handleDownstreamOperator(SSqlObj** pSqlObjList, int32_t numOfUpstream, SQue } } - tscDebug("0x%"PRIx64" create QInfo 0x%"PRIx64" to execute the main query while all nest query is ready", pSql->self, pSql->self); + tscDebug("0x%"PRIx64" create QInfo 0x%"PRIx64" to execute the main query while all nest queries are ready", pSql->self, pSql->self); px->pQInfo = createQInfoFromQueryNode(px, &tableGroupInfo, pSourceOperator, NULL, NULL, MASTER_SCAN, pSql->self); tfree(pColumnInfo); From ae91d43a7fa6e7df784f703b856340bc4f882665 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Fri, 4 Jun 2021 13:28:06 +0800 Subject: [PATCH 18/57] [td-4529] --- src/client/src/tscUtil.c | 10 ++++++++++ src/query/src/qExecutor.c | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index d5693a29bf..b621f89d52 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -3273,6 +3273,16 @@ SSqlObj* createSubqueryObj(SSqlObj* pSql, int16_t tableIndex, __async_cb_func_t pNewQueryInfo->pTableMetaInfo = NULL; pNewQueryInfo->bufLen = pQueryInfo->bufLen; + pNewQueryInfo->projectionQuery = pQueryInfo->projectionQuery; + pNewQueryInfo->hasFilter = pQueryInfo->hasFilter; + pNewQueryInfo->simpleAgg = pQueryInfo->simpleAgg; + pNewQueryInfo->onlyTagQuery = pQueryInfo->onlyTagQuery; + pNewQueryInfo->groupbyColumn = pQueryInfo->groupbyColumn; + + pNewQueryInfo->arithmeticOnAgg = pQueryInfo->arithmeticOnAgg; + pNewQueryInfo->orderProjectQuery = pQueryInfo->orderProjectQuery; + pNewQueryInfo->diffQuery = pQueryInfo->diffQuery; + pNewQueryInfo->buf = malloc(pQueryInfo->bufLen); if (pNewQueryInfo->buf == NULL) { terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 3aae336169..d14189657a 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -4275,7 +4275,7 @@ static SSDataBlock* doTableScanImpl(void* param, bool* newgroup) { doTableQueryInfoTimeWindowCheck(pQueryAttr, *pTableQueryInfo); if (pTableScanInfo->prevGroupId != -1 && pTableScanInfo->prevGroupId != (*pTableQueryInfo)->groupIndex) { - *newgroup = true; + *newgroup = false; } pTableScanInfo->prevGroupId = (*pTableQueryInfo)->groupIndex; From df944b80d19416043d39c24e844cd41ee2376009 Mon Sep 17 00:00:00 2001 From: Elias Soong Date: Fri, 4 Jun 2021 13:53:17 +0800 Subject: [PATCH 19/57] [TD-2639] : describe length limitation about tag names. --- documentation20/cn/12.taos-sql/docs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/documentation20/cn/12.taos-sql/docs.md b/documentation20/cn/12.taos-sql/docs.md index 6bd007ff21..acb0ee803c 100644 --- a/documentation20/cn/12.taos-sql/docs.md +++ b/documentation20/cn/12.taos-sql/docs.md @@ -1306,7 +1306,7 @@ SELECT AVG(current), MAX(current), LEASTSQUARES(current, start_val, step_val), P - 数据库名最大长度为 32 - 表名最大长度为 192,每行数据最大长度 16k 个字符(注意:数据行内每个 BINARY/NCHAR 类型的列还会额外占用 2 个字节的存储位置) - 列名最大长度为 64,最多允许 1024 列,最少需要 2 列,第一列必须是时间戳 -- 标签最多允许 128 个,可以 1 个,标签总长度不超过 16k 个字符 +- 标签名最大长度为 64,最多允许 128 个,可以 1 个,一个表中标签值的总长度不超过 16k 个字符 - SQL 语句最大长度 65480 个字符,但可通过系统配置参数 maxSQLLength 修改,最长可配置为 1M - SELECT 语句的查询结果,最多允许返回 1024 列(语句中的函数调用可能也会占用一些列空间),超限时需要显式指定较少的返回数据列,以避免语句执行报错。 - 库的数目,超级表的数目、表的数目,系统不做限制,仅受系统资源限制 From 0924b6f7f150063d3561cc3fd99f8573be0e64c9 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Fri, 4 Jun 2021 14:19:48 +0800 Subject: [PATCH 20/57] [TD-4426] merge in --- src/client/src/tscSQLParser.c | 21 ++++++++++++++++++--- src/common/src/texpr.c | 5 ++++- src/query/src/qFilterfunc.c | 6 ++++-- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 7d6a5c30da..e062cc61f6 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -204,8 +204,9 @@ static int32_t validateParamOfRelationIn(tVariant *pVar, int32_t colType) { if (pVar->nType != TSDB_DATA_TYPE_BINARY) { return -1; } - SBufferReader br = tbufInitReader(pVar->pz, pVar->nLen, false); - return tbufReadUint32(&br) == colType ? 0: -1; + return 0; + //SBufferReader br = tbufInitReader(pVar->pz, pVar->nLen, false); + //return tbufReadUint32(&br) == colType ? 0: -1; } static uint8_t convertOptr(SStrToken *pToken) { @@ -243,6 +244,8 @@ static uint8_t convertOptr(SStrToken *pToken) { return TSDB_RELATION_ISNULL; case TK_NOTNULL: return TSDB_RELATION_NOTNULL; + case TK_IN: + return TSDB_RELATION_IN; default: { return 0; } } } @@ -4507,7 +4510,11 @@ static int32_t validateTagCondExpr(SSqlCmd* pCmd, tExprNode *p) { free(tmp); } else { double tmp; - retVal = tVariantDump(vVariant, (char*)&tmp, schemaType, false); + if (p->_node.optr == TSDB_RELATION_IN) { + retVal = validateParamOfRelationIn(vVariant, schemaType); + } else { + retVal = tVariantDump(vVariant, (char*)&tmp, schemaType, false); + } } if (retVal != TSDB_CODE_SUCCESS) { @@ -7998,6 +8005,14 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS } return TSDB_CODE_SUCCESS; + } else if (pSqlExpr->tokenId == TK_SET) { + tVariant *pVal; + if (serializeExprListToVariant(pSqlExpr->pParam, &pVal) == false) { + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "not support filter expression"); + } + *pExpr = calloc(1, sizeof(tExprNode)); + (*pExpr)->nodeType = TSQL_NODE_VALUE; + (*pExpr)->pVal = pVal; } else { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "not support filter expression"); } diff --git a/src/common/src/texpr.c b/src/common/src/texpr.c index 0805eff9ff..2690c18e5e 100644 --- a/src/common/src/texpr.c +++ b/src/common/src/texpr.c @@ -479,10 +479,13 @@ void buildFilterSetFromBinary(void **q, const char *buf, int32_t len) { } else if (type == TSDB_DATA_TYPE_DOUBLE || type == TSDB_DATA_TYPE_FLOAT) { double val = tbufReadDouble(&br); taosHashPut(pObj, (char *)&val, sizeof(val), &dummy, sizeof(dummy)); - } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { + } else if (type == TSDB_DATA_TYPE_BINARY) { size_t t = 0; const char *val = tbufReadBinary(&br, &t); + taosHashPut(pObj, (char *)val, t, &dummy, sizeof(dummy)); + } else if (type == TSDB_DATA_TYPE_NCHAR) { + } } *q = (void *)pObj; diff --git a/src/query/src/qFilterfunc.c b/src/query/src/qFilterfunc.c index 7769ec4a57..5e16107f70 100644 --- a/src/query/src/qFilterfunc.c +++ b/src/query/src/qFilterfunc.c @@ -266,8 +266,10 @@ bool inOperator(SColumnFilterElem *pFilter, const char* minval, const char* maxv double v; GET_TYPED_DATA(v, double, type, minval); return NULL != taosHashGet((SHashObj *)pFilter->q, (char *)&v, sizeof(v)); - } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR){ - return NULL != taosHashGet((SHashObj *)pFilter->q, varDataVal(minval), varDataLen(minval)); + } else if (type == TSDB_DATA_TYPE_BINARY) { + return NULL != taosHashGet((SHashObj *)pFilter->q, varDataVal(minval), varDataLen(minval)); + } else if (type == TSDB_DATA_TYPE_NCHAR){ + return NULL != taosHashGet((SHashObj *)pFilter->q, varDataVal(minval), varDataLen(minval)/TSDB_NCHAR_SIZE); } return false; From 0e223ea2c13fdf366270cfaab36635572b00e3cf Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Fri, 4 Jun 2021 14:26:28 +0800 Subject: [PATCH 21/57] [td-225] fix bugs found by regression test. --- src/client/inc/tscUtil.h | 2 -- src/client/src/tscSQLParser.c | 14 +++++++------- src/client/src/tscUtil.c | 19 ++++--------------- 3 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/client/inc/tscUtil.h b/src/client/inc/tscUtil.h index 448fee936f..0a57767926 100644 --- a/src/client/inc/tscUtil.h +++ b/src/client/inc/tscUtil.h @@ -329,8 +329,6 @@ STableMeta* tscTableMetaDup(STableMeta* pTableMeta); SVgroupsInfo* tscVgroupsInfoDup(SVgroupsInfo* pVgroupsInfo); int32_t tscCreateQueryFromQueryInfo(SQueryInfo* pQueryInfo, SQueryAttr* pQueryAttr, void* addr); - -void tsCreateSQLFunctionCtx(SQueryInfo* pQueryInfo, SQLFunctionCtx* pCtx, SSchema* pSchema); void* createQInfoFromQueryNode(SQueryInfo* pQueryInfo, STableGroupInfo* pTableGroupInfo, SOperatorInfo* pOperator, char* sql, void* addr, int32_t stage, uint64_t qId); void* malloc_throw(size_t size); diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 53bea3f48b..4a80cbd340 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -7812,15 +7812,15 @@ int32_t validateSqlNode(SSqlObj* pSql, SSqlNode* pSqlNode, SQueryInfo* pQueryInf } { // set the query info - pQueryInfo->projectionQuery = tscIsProjectionQuery(pQueryInfo); - pQueryInfo->hasFilter = tscHasColumnFilter(pQueryInfo); - pQueryInfo->simpleAgg = isSimpleAggregateRv(pQueryInfo); - pQueryInfo->onlyTagQuery = onlyTagPrjFunction(pQueryInfo); - pQueryInfo->groupbyColumn = tscGroupbyColumn(pQueryInfo); + pQueryInfo->projectionQuery = tscIsProjectionQuery(pQueryInfo); + pQueryInfo->hasFilter = tscHasColumnFilter(pQueryInfo); + pQueryInfo->simpleAgg = isSimpleAggregateRv(pQueryInfo); + pQueryInfo->onlyTagQuery = onlyTagPrjFunction(pQueryInfo); + pQueryInfo->groupbyColumn = tscGroupbyColumn(pQueryInfo); - pQueryInfo->arithmeticOnAgg = tsIsArithmeticQueryOnAggResult(pQueryInfo); + pQueryInfo->arithmeticOnAgg = tsIsArithmeticQueryOnAggResult(pQueryInfo); pQueryInfo->orderProjectQuery = tscOrderedProjectionQueryOnSTable(pQueryInfo, 0); - pQueryInfo->diffQuery = tscIsDiffQuery(pQueryInfo); + pQueryInfo->diffQuery = tscIsDiffQuery(pQueryInfo); SExprInfo** p = NULL; int32_t numOfExpr = 0; diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index b621f89d52..627fbbd0ee 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -491,7 +491,7 @@ bool isSimpleAggregateRv(SQueryInfo* pQueryInfo) { return false; } - if (tscGroupbyColumn(pQueryInfo) || isTsCompQuery(pQueryInfo) || tscIsTopBotQuery(pQueryInfo) || tscIsDiffQuery(pQueryInfo)) { + if (/*tscGroupbyColumn(pQueryInfo) || */isTsCompQuery(pQueryInfo) || tscIsTopBotQuery(pQueryInfo) || tscIsDiffQuery(pQueryInfo)) { return false; } @@ -3272,17 +3272,6 @@ SSqlObj* createSubqueryObj(SSqlObj* pSql, int16_t tableIndex, __async_cb_func_t pNewQueryInfo->numOfTables = 0; pNewQueryInfo->pTableMetaInfo = NULL; pNewQueryInfo->bufLen = pQueryInfo->bufLen; - - pNewQueryInfo->projectionQuery = pQueryInfo->projectionQuery; - pNewQueryInfo->hasFilter = pQueryInfo->hasFilter; - pNewQueryInfo->simpleAgg = pQueryInfo->simpleAgg; - pNewQueryInfo->onlyTagQuery = pQueryInfo->onlyTagQuery; - pNewQueryInfo->groupbyColumn = pQueryInfo->groupbyColumn; - - pNewQueryInfo->arithmeticOnAgg = pQueryInfo->arithmeticOnAgg; - pNewQueryInfo->orderProjectQuery = pQueryInfo->orderProjectQuery; - pNewQueryInfo->diffQuery = pQueryInfo->diffQuery; - pNewQueryInfo->buf = malloc(pQueryInfo->bufLen); if (pNewQueryInfo->buf == NULL) { terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; @@ -4242,11 +4231,11 @@ int32_t tscCreateQueryFromQueryInfo(SQueryInfo* pQueryInfo, SQueryAttr* pQueryAt pQueryAttr->hasTagResults = hasTagValOutput(pQueryInfo); pQueryAttr->stabledev = isStabledev(pQueryInfo); pQueryAttr->tsCompQuery = isTsCompQuery(pQueryInfo); - pQueryAttr->diffQuery = pQueryInfo->diffQuery; - pQueryAttr->simpleAgg = pQueryInfo->simpleAgg; + pQueryAttr->diffQuery = tscIsDiffQuery(pQueryInfo); + pQueryAttr->simpleAgg = isSimpleAggregateRv(pQueryInfo); pQueryAttr->needReverseScan = tscNeedReverseScan(pQueryInfo); pQueryAttr->stableQuery = QUERY_IS_STABLE_QUERY(pQueryInfo->type); - pQueryAttr->groupbyColumn = (!pQueryInfo->stateWindow) && pQueryInfo->groupbyColumn; + pQueryAttr->groupbyColumn = (!pQueryInfo->stateWindow) && tscGroupbyColumn(pQueryInfo); pQueryAttr->queryBlockDist = isBlockDistQuery(pQueryInfo); pQueryAttr->pointInterpQuery = tscIsPointInterpQuery(pQueryInfo); pQueryAttr->timeWindowInterpo = timeWindowInterpoRequired(pQueryInfo); From 61b39d7e98cc92103f5a3102436838ff2d449d41 Mon Sep 17 00:00:00 2001 From: dapan1121 <89396746@qq.com> Date: Fri, 4 Jun 2021 14:32:37 +0800 Subject: [PATCH 22/57] support error msg --- src/client/src/tscPrepare.c | 85 +++++++----- tests/script/api/batchprepare.c | 237 +++++++++++++++++++++++++++++++- 2 files changed, 284 insertions(+), 38 deletions(-) diff --git a/src/client/src/tscPrepare.c b/src/client/src/tscPrepare.c index f199a57987..00d8b60e2f 100644 --- a/src/client/src/tscPrepare.c +++ b/src/client/src/tscPrepare.c @@ -163,8 +163,8 @@ static int normalStmtBindParam(STscStmt* stmt, TAOS_BIND* bind) { break; default: - tscDebug("0x%"PRIx64" bind column%d: type mismatch or invalid", stmt->pSql->self, i); - return TSDB_CODE_TSC_INVALID_VALUE; + tscError("0x%"PRIx64" bind column%d: type mismatch or invalid", stmt->pSql->self, i); + return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "bind type mismatch or invalid"); } } @@ -727,6 +727,7 @@ static int doBindParam(STableDataBlocks* pBlock, char* data, SParamInfo* param, #endif if (bind->buffer_type != param->type) { + tscError("column type mismatch"); return TSDB_CODE_TSC_INVALID_VALUE; } @@ -754,6 +755,7 @@ static int doBindParam(STableDataBlocks* pBlock, char* data, SParamInfo* param, case TSDB_DATA_TYPE_BINARY: if ((*bind->length) > (uintptr_t)param->bytes) { + tscError("column length is too big"); return TSDB_CODE_TSC_INVALID_VALUE; } size = (short)*bind->length; @@ -763,6 +765,7 @@ static int doBindParam(STableDataBlocks* pBlock, char* data, SParamInfo* param, case TSDB_DATA_TYPE_NCHAR: { int32_t output = 0; if (!taosMbsToUcs4(bind->buffer, *bind->length, varDataVal(data + param->offset), param->bytes - VARSTR_HEADER_SIZE, &output)) { + tscError("convert nchar failed"); return TSDB_CODE_TSC_INVALID_VALUE; } varDataSetLen(data + param->offset, output); @@ -787,6 +790,7 @@ static int doBindParam(STableDataBlocks* pBlock, char* data, SParamInfo* param, static int doBindBatchParam(STableDataBlocks* pBlock, SParamInfo* param, TAOS_MULTI_BIND* bind, int32_t rowNum) { if (bind->buffer_type != param->type || !isValidDataType(param->type)) { + tscError("column mismatch or invalid"); return TSDB_CODE_TSC_INVALID_VALUE; } @@ -892,8 +896,8 @@ static int insertStmtBindParam(STscStmt* stmt, TAOS_BIND* bind) { int code = doBindParam(pBlock, data, param, &bind[param->idx], 1); if (code != TSDB_CODE_SUCCESS) { - tscDebug("0x%"PRIx64" bind column %d: type mismatch or invalid", pStmt->pSql->self, param->idx); - return code; + tscDebug("0x%"PRIx64" bind column %d: type mismatch or invalid", pStmt->pSql->self, param->idx); + return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "bind column type mismatch or invalid"); } } @@ -957,13 +961,13 @@ static int insertStmtBindParamBatch(STscStmt* stmt, TAOS_MULTI_BIND* bind, int c SParamInfo* param = &pBlock->params[j]; if (bind[param->idx].num != rowNum) { tscError("0x%"PRIx64" param %d: num[%d:%d] not match", pStmt->pSql->self, param->idx, rowNum, bind[param->idx].num); - return TSDB_CODE_TSC_INVALID_VALUE; + return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "bind row num mismatch"); } int code = doBindBatchParam(pBlock, param, &bind[param->idx], pCmd->batchSize); if (code != TSDB_CODE_SUCCESS) { tscError("0x%"PRIx64" bind column %d: type mismatch or invalid", pStmt->pSql->self, param->idx); - return code; + return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "bind column type mismatch or invalid"); } } @@ -974,7 +978,7 @@ static int insertStmtBindParamBatch(STscStmt* stmt, TAOS_MULTI_BIND* bind, int c int code = doBindBatchParam(pBlock, param, bind, pCmd->batchSize); if (code != TSDB_CODE_SUCCESS) { tscError("0x%"PRIx64" bind column %d: type mismatch or invalid", pStmt->pSql->self, param->idx); - return code; + return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "bind column type mismatch or invalid"); } if (colIdx == (pBlock->numOfParams - 1)) { @@ -993,7 +997,7 @@ static int insertStmtUpdateBatch(STscStmt* stmt) { if (pCmd->batchSize > INT16_MAX) { tscError("too many record:%d", pCmd->batchSize); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "too many records"); } if (taosHashGetSize(pCmd->insertParam.pTableBlockHashList) == 0) { @@ -1057,7 +1061,8 @@ static int insertStmtReset(STscStmt* pStmt) { static int insertStmtExecute(STscStmt* stmt) { SSqlCmd* pCmd = &stmt->pSql->cmd; if (pCmd->batchSize == 0) { - return TSDB_CODE_TSC_INVALID_VALUE; + tscError("no records bind"); + return invalidOperationMsg(tscGetErrorMsgPayload(&stmt->pSql->cmd), "no records bind"); } if (taosHashGetSize(pCmd->insertParam.pTableBlockHashList) == 0) { @@ -1174,7 +1179,7 @@ static int insertBatchStmtExecute(STscStmt* pStmt) { if(pStmt->mtb.nameSet == false) { tscError("0x%"PRIx64" no table name set", pStmt->pSql->self); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "no table name set"); } pStmt->pSql->retry = pStmt->pSql->maxRetry + 1; //no retry @@ -1215,7 +1220,8 @@ int stmtParseInsertTbTags(SSqlObj* pSql, STscStmt* pStmt) { int32_t index = 0; SStrToken sToken = tStrGetToken(pCmd->insertParam.sql, &index, false); if (sToken.n == 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; + tscError("table is is expected, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "table name is expected", pCmd->insertParam.sql); } if (sToken.n == 1 && sToken.type == TK_QUESTION) { @@ -1237,24 +1243,28 @@ int stmtParseInsertTbTags(SSqlObj* pSql, STscStmt* pStmt) { return TSDB_CODE_SUCCESS; } - if (sToken.n <= 0 || sToken.type != TK_USING) { - return tscSQLSyntaxErrMsg(pCmd->payload, "keywords USING is expected", sToken.z); + if (sToken.n <= 0 || sToken.type != TK_USING) { + tscError("keywords USING is expected, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "keywords USING is expected", sToken.z ? sToken.z : pCmd->insertParam.sql); } sToken = tStrGetToken(pCmd->insertParam.sql, &index, false); if (sToken.n <= 0 || ((sToken.type != TK_ID) && (sToken.type != TK_STRING))) { - return tscSQLSyntaxErrMsg(pCmd->payload, "invalid token", sToken.z); + tscError("invalid token, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "invalid token", sToken.z ? sToken.z : pCmd->insertParam.sql); } pStmt->mtb.stbname = sToken; sToken = tStrGetToken(pCmd->insertParam.sql, &index, false); if (sToken.n <= 0 || sToken.type != TK_TAGS) { - return tscSQLSyntaxErrMsg(pCmd->payload, "keyword TAGS expected", sToken.z); + tscError("keyword TAGS expected, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "keyword TAGS expected", sToken.z ? sToken.z : pCmd->insertParam.sql); } sToken = tStrGetToken(pCmd->insertParam.sql, &index, false); if (sToken.n <= 0 || sToken.type != TK_LP) { - return tscSQLSyntaxErrMsg(pCmd->payload, ") expected", sToken.z); + tscError("( expected, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "( expected", sToken.z ? sToken.z : pCmd->insertParam.sql); } pStmt->mtb.tags = taosArrayInit(4, sizeof(SStrToken)); @@ -1264,7 +1274,8 @@ int stmtParseInsertTbTags(SSqlObj* pSql, STscStmt* pStmt) { while (loopCont) { sToken = tStrGetToken(pCmd->insertParam.sql, &index, false); if (sToken.n <= 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; + tscError("unexpected sql end, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "unexpected sql end", pCmd->insertParam.sql); } switch (sToken.type) { @@ -1272,7 +1283,8 @@ int stmtParseInsertTbTags(SSqlObj* pSql, STscStmt* pStmt) { loopCont = 0; break; case TK_VALUES: - return TSDB_CODE_TSC_INVALID_OPERATION; + tscError("unexpected token values, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "unexpected token", sToken.z); case TK_QUESTION: pStmt->mtb.tagSet = false; //continue default: @@ -1282,12 +1294,14 @@ int stmtParseInsertTbTags(SSqlObj* pSql, STscStmt* pStmt) { } if (taosArrayGetSize(pStmt->mtb.tags) <= 0) { - return TSDB_CODE_TSC_INVALID_OPERATION; + tscError("no tags, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "no tags", pCmd->insertParam.sql); } sToken = tStrGetToken(pCmd->insertParam.sql, &index, false); if (sToken.n <= 0 || (sToken.type != TK_VALUES && sToken.type != TK_LP)) { - return TSDB_CODE_TSC_INVALID_OPERATION; + tscError("sql error, sql:%s", pCmd->insertParam.sql); + return tscSQLSyntaxErrMsg(pCmd->payload, "sql error", sToken.z ? sToken.z : pCmd->insertParam.sql); } pStmt->mtb.values = sToken; @@ -1329,8 +1343,8 @@ int stmtGenInsertStatement(SSqlObj* pSql, STscStmt* pStmt, const char* name, TAO } else { if (tags[j].buffer == NULL) { free(str); - tscError("empty"); - return TSDB_CODE_TSC_APP_ERROR; + tscError("empty tag value in params"); + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "empty tag value in params"); } ret = converToStr(str + len, tags[j].buffer_type, tags[j].buffer, tags[j].length ? (int32_t)*tags[j].length : -1, &l); @@ -1410,6 +1424,11 @@ TAOS_STMT* taos_stmt_init(TAOS* taos) { return NULL; } + if (TSDB_CODE_SUCCESS != tscAllocPayload(&pSql->cmd, TSDB_DEFAULT_PAYLOAD_SIZE)) { + tscError("%p failed to malloc payload buffer", pSql); + return TSDB_CODE_TSC_OUT_OF_MEMORY; + } + tsem_init(&pSql->rspSem, 0, 0); pSql->signature = pSql; pSql->pTscObj = pObj; @@ -1431,7 +1450,7 @@ int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) { if (pStmt->last != STMT_INIT) { tscError("prepare status error, last:%d", pStmt->last); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "prepare status error"); } pStmt->last = STMT_PREPARE; @@ -1447,11 +1466,6 @@ int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) { pCmd->insertParam.insertType = TSDB_QUERY_TYPE_STMT_INSERT; - if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, TSDB_DEFAULT_PAYLOAD_SIZE)) { - tscError("%p failed to malloc payload buffer", pSql); - return TSDB_CODE_TSC_OUT_OF_MEMORY; - } - pSql->sqlstr = realloc(pSql->sqlstr, sqlLen + 1); if (pSql->sqlstr == NULL) { @@ -1489,6 +1503,7 @@ int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) { if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) { // wait for the callback function to post the semaphore tsem_wait(&pSql->rspSem); + pSql->res.code = code; return pSql->res.code; } @@ -1510,20 +1525,18 @@ int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags } if (name == NULL) { - terrno = TSDB_CODE_TSC_APP_ERROR; tscError("0x%"PRIx64" name is NULL", pSql->self); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "name is NULL"); } if (pStmt->multiTbInsert == false || !tscIsInsertData(pSql->sqlstr)) { - terrno = TSDB_CODE_TSC_APP_ERROR; - tscError("0x%"PRIx64" not multi table insert", pSql->self); - return TSDB_CODE_TSC_APP_ERROR; + tscError("0x%"PRIx64" not multiple table insert", pSql->self); + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "not multiple table insert"); } if (pStmt->last == STMT_INIT || pStmt->last == STMT_BIND || pStmt->last == STMT_BIND_COL) { - tscError("0x%"PRIx64" settbname status error, last:%d", pSql->self, pStmt->last); - return TSDB_CODE_TSC_APP_ERROR; + tscError("0x%"PRIx64" set_tbname_tags status error, last:%d", pSql->self, pStmt->last); + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "set_tbname_tags status error"); } pStmt->last = STMT_SETTBNAME; @@ -1552,7 +1565,7 @@ int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags } else { if (tags == NULL) { tscError("No tags set"); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "no tags set"); } int32_t ret = stmtGenInsertStatement(pSql, pStmt, name, tags); diff --git a/tests/script/api/batchprepare.c b/tests/script/api/batchprepare.c index 55eb62d9bb..af0611ce73 100644 --- a/tests/script/api/batchprepare.c +++ b/tests/script/api/batchprepare.c @@ -1860,6 +1860,224 @@ int stmt_funcb_autoctb_e2(TAOS_STMT *stmt) { + + + + +//1 tables 10 records +int stmt_funcb_autoctb_e3(TAOS_STMT *stmt) { + struct { + int64_t *ts; + int8_t b[10]; + int8_t v1[10]; + int16_t v2[10]; + int32_t v4[10]; + int64_t v8[10]; + float f4[10]; + double f8[10]; + char bin[10][40]; + } v = {0}; + + v.ts = malloc(sizeof(int64_t) * 1 * 10); + + int *lb = malloc(10 * sizeof(int)); + + TAOS_BIND *tags = calloc(1, sizeof(TAOS_BIND) * 9 * 1); + TAOS_MULTI_BIND *params = calloc(1, sizeof(TAOS_MULTI_BIND) * 1*10); + +// int one_null = 1; + int one_not_null = 0; + + char* is_null = malloc(sizeof(char) * 10); + char* no_null = malloc(sizeof(char) * 10); + + for (int i = 0; i < 10; ++i) { + lb[i] = 40; + no_null[i] = 0; + is_null[i] = (i % 10 == 2) ? 1 : 0; + v.b[i] = (int8_t)(i % 2); + v.v1[i] = (int8_t)((i+1) % 2); + v.v2[i] = (int16_t)i; + v.v4[i] = (int32_t)(i+1); + v.v8[i] = (int64_t)(i+2); + v.f4[i] = (float)(i+3); + v.f8[i] = (double)(i+4); + memset(v.bin[i], '0'+i%10, 40); + } + + for (int i = 0; i < 10; i+=10) { + params[i+0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; + params[i+0].buffer_length = sizeof(int64_t); + params[i+0].buffer = &v.ts[10*i/10]; + params[i+0].length = NULL; + params[i+0].is_null = no_null; + params[i+0].num = 10; + + params[i+1].buffer_type = TSDB_DATA_TYPE_BOOL; + params[i+1].buffer_length = sizeof(int8_t); + params[i+1].buffer = v.b; + params[i+1].length = NULL; + params[i+1].is_null = is_null; + params[i+1].num = 10; + + params[i+2].buffer_type = TSDB_DATA_TYPE_TINYINT; + params[i+2].buffer_length = sizeof(int8_t); + params[i+2].buffer = v.v1; + params[i+2].length = NULL; + params[i+2].is_null = is_null; + params[i+2].num = 10; + + params[i+3].buffer_type = TSDB_DATA_TYPE_SMALLINT; + params[i+3].buffer_length = sizeof(int16_t); + params[i+3].buffer = v.v2; + params[i+3].length = NULL; + params[i+3].is_null = is_null; + params[i+3].num = 10; + + params[i+4].buffer_type = TSDB_DATA_TYPE_INT; + params[i+4].buffer_length = sizeof(int32_t); + params[i+4].buffer = v.v4; + params[i+4].length = NULL; + params[i+4].is_null = is_null; + params[i+4].num = 10; + + params[i+5].buffer_type = TSDB_DATA_TYPE_BIGINT; + params[i+5].buffer_length = sizeof(int64_t); + params[i+5].buffer = v.v8; + params[i+5].length = NULL; + params[i+5].is_null = is_null; + params[i+5].num = 10; + + params[i+6].buffer_type = TSDB_DATA_TYPE_FLOAT; + params[i+6].buffer_length = sizeof(float); + params[i+6].buffer = v.f4; + params[i+6].length = NULL; + params[i+6].is_null = is_null; + params[i+6].num = 10; + + params[i+7].buffer_type = TSDB_DATA_TYPE_DOUBLE; + params[i+7].buffer_length = sizeof(double); + params[i+7].buffer = v.f8; + params[i+7].length = NULL; + params[i+7].is_null = is_null; + params[i+7].num = 10; + + params[i+8].buffer_type = TSDB_DATA_TYPE_BINARY; + params[i+8].buffer_length = 40; + params[i+8].buffer = v.bin; + params[i+8].length = lb; + params[i+8].is_null = is_null; + params[i+8].num = 10; + + params[i+9].buffer_type = TSDB_DATA_TYPE_BINARY; + params[i+9].buffer_length = 40; + params[i+9].buffer = v.bin; + params[i+9].length = lb; + params[i+9].is_null = is_null; + params[i+9].num = 10; + + } + + int64_t tts = 1591060628000; + for (int i = 0; i < 10; ++i) { + v.ts[i] = tts + i; + } + + + for (int i = 0; i < 1; ++i) { + tags[i+0].buffer_type = TSDB_DATA_TYPE_INT; + tags[i+0].buffer = v.v4; + tags[i+0].is_null = &one_not_null; + tags[i+0].length = NULL; + + tags[i+1].buffer_type = TSDB_DATA_TYPE_BOOL; + tags[i+1].buffer = v.b; + tags[i+1].is_null = &one_not_null; + tags[i+1].length = NULL; + + tags[i+2].buffer_type = TSDB_DATA_TYPE_TINYINT; + tags[i+2].buffer = v.v1; + tags[i+2].is_null = &one_not_null; + tags[i+2].length = NULL; + + tags[i+3].buffer_type = TSDB_DATA_TYPE_SMALLINT; + tags[i+3].buffer = v.v2; + tags[i+3].is_null = &one_not_null; + tags[i+3].length = NULL; + + tags[i+4].buffer_type = TSDB_DATA_TYPE_BIGINT; + tags[i+4].buffer = v.v8; + tags[i+4].is_null = &one_not_null; + tags[i+4].length = NULL; + + tags[i+5].buffer_type = TSDB_DATA_TYPE_FLOAT; + tags[i+5].buffer = v.f4; + tags[i+5].is_null = &one_not_null; + tags[i+5].length = NULL; + + tags[i+6].buffer_type = TSDB_DATA_TYPE_DOUBLE; + tags[i+6].buffer = v.f8; + tags[i+6].is_null = &one_not_null; + tags[i+6].length = NULL; + + tags[i+7].buffer_type = TSDB_DATA_TYPE_BINARY; + tags[i+7].buffer = v.bin; + tags[i+7].is_null = &one_not_null; + tags[i+7].length = (uintptr_t *)lb; + + tags[i+8].buffer_type = TSDB_DATA_TYPE_NCHAR; + tags[i+8].buffer = v.bin; + tags[i+8].is_null = &one_not_null; + tags[i+8].length = (uintptr_t *)lb; + } + + + unsigned long long starttime = getCurrentTime(); + + char *sql = "insert into ? using stb1 (id1,id2,id3,id4,id5,id6,id7,id8,id9) tags(?,?,?,?,?,?,?,?,?) values(?,?,?,?,?,?,?,?,?,?)"; + int code = taos_stmt_prepare(stmt, sql, 0); + if (code != 0){ + printf("failed to execute taos_stmt_prepare. code:0x%x\n", code); + return -1; + //exit(1); + } + + int id = 0; + for (int zz = 0; zz < 1; zz++) { + char buf[32]; + sprintf(buf, "m%d", zz); + code = taos_stmt_set_tbname_tags(stmt, buf, NULL); + if (code != 0){ + printf("failed to execute taos_stmt_set_tbname_tags. code:0x%x\n", code); + return -1; + } + + taos_stmt_bind_param_batch(stmt, params + id * 10); + taos_stmt_add_batch(stmt); + } + + if (taos_stmt_execute(stmt) != 0) { + printf("failed to execute insert statement.\n"); + exit(1); + } + + ++id; + + unsigned long long endtime = getCurrentTime(); + printf("insert total %d records, used %u seconds, avg:%u useconds\n", 10, (endtime-starttime)/1000000UL, (endtime-starttime)/(10)); + + free(v.ts); + free(lb); + free(params); + free(is_null); + free(no_null); + free(tags); + + return 0; +} + + + //300 tables 60 records int stmt_funcb1(TAOS_STMT *stmt) { struct { @@ -3835,7 +4053,7 @@ void* runcase(void *par) { #endif -#if 1 +#if 1 prepare(taos, 1, 1); stmt = taos_stmt_init(taos); @@ -3900,7 +4118,7 @@ void* runcase(void *par) { #endif -#if 1 +#if 1 prepare(taos, 1, 0); stmt = taos_stmt_init(taos); @@ -3930,6 +4148,21 @@ void* runcase(void *par) { #endif +#if 1 + prepare(taos, 1, 0); + + stmt = taos_stmt_init(taos); + + printf("1t+10r+bm+autoctb+e3 start\n"); + stmt_funcb_autoctb_e3(stmt); + printf("1t+10r+bm+autoctb+e3 end\n"); + printf("check result start\n"); + //check_result(taos, "m0", 1, 0); + printf("check result end\n"); + taos_stmt_close(stmt); + +#endif + #if 1 prepare(taos, 1, 1); From 15aecd488241f00822440920605d17bcd9f5fc3e Mon Sep 17 00:00:00 2001 From: Ping Xiao Date: Fri, 4 Jun 2021 16:52:18 +0800 Subject: [PATCH 23/57] [TD-4505]: add test case for last_row() --- tests/pytest/query/last_row_cache.py | 60 +++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/tests/pytest/query/last_row_cache.py b/tests/pytest/query/last_row_cache.py index a0e8147709..4aae4ce487 100644 --- a/tests/pytest/query/last_row_cache.py +++ b/tests/pytest/query/last_row_cache.py @@ -25,7 +25,7 @@ class TDTestCase: self.tables = 10 self.rows = 20 - self.columns = 50 + self.columns = 5 self.perfix = 't' self.ts = 1601481600000 @@ -34,7 +34,7 @@ class TDTestCase: sql = "create table st(ts timestamp, " for i in range(self.columns - 1): sql += "c%d int, " % (i + 1) - sql += "c50 int) tags(t1 int)" + sql += "c5 int) tags(t1 int)" tdSql.execute(sql) for i in range(self.tables): @@ -148,15 +148,38 @@ class TDTestCase: self.executeQueries() self.insertData2() self.executeQueries2() - + print("============== alter last cache") tdSql.execute("alter database test1 cachelast 1") self.executeQueries2() + + tdSql.execute("alter database test1 cachelast 2") + self.executeQueries2() + + tdSql.execute("alter database test1 cachelast 3") + self.executeQueries2() + + + print("============== alter last cache") + tdSql.execute("alter database test1 cachelast 0") + self.executeQueries2() tdDnodes.stop(1) tdDnodes.start(1) self.executeQueries2() - tdSql.execute("alter database test1 cachelast 0") + tdSql.execute("alter database test1 cachelast 1") + self.executeQueries2() + tdDnodes.stop(1) + tdDnodes.start(1) + self.executeQueries2() + + tdSql.execute("alter database test1 cachelast 2") + self.executeQueries2() + tdDnodes.stop(1) + tdDnodes.start(1) + self.executeQueries2() + + tdSql.execute("alter database test1 cachelast 3") self.executeQueries2() tdDnodes.stop(1) tdDnodes.start(1) @@ -174,10 +197,22 @@ class TDTestCase: self.executeQueries2() tdSql.execute("alter database test2 cachelast 0") + self.executeQueries2() + + tdSql.execute("alter database test2 cachelast 1") self.executeQueries2() + + tdSql.execute("alter database test2 cachelast 2") + self.executeQueries2() + + tdSql.execute("alter database test2 cachelast 3") + self.executeQueries2() + + tdSql.execute("alter database test2 cachelast 0") + self.executeQueries2() tdDnodes.stop(1) tdDnodes.start(1) - self.executeQueries2() + self.executeQueries2() tdSql.execute("alter database test2 cachelast 1") self.executeQueries2() @@ -185,6 +220,21 @@ class TDTestCase: tdDnodes.start(1) self.executeQueries2() + tdSql.execute("alter database test2 cachelast 2") + self.executeQueries2() + tdDnodes.stop(1) + tdDnodes.start(1) + self.executeQueries2() + + tdSql.execute("alter database test2 cachelast 3") + self.executeQueries2() + tdDnodes.stop(1) + tdDnodes.start(1) + self.executeQueries2() + + tdSql.query("select last_row(*) from st group by tbname") + tdSql.checkRows(10) + def stop(self): tdSql.close() tdLog.success("%s successfully executed" % __file__) From 7f77f0362ff2e90a29b05df442ea931aa58cf51c Mon Sep 17 00:00:00 2001 From: Shuduo Sang Date: Fri, 4 Jun 2021 21:48:37 +0800 Subject: [PATCH 24/57] Hotfix/sangshuduo/td 4025 fix travis ci broken for develop (#6376) * [TD-4025]: travis ci broken due to valgrind dependency missed. modify smoketest.sh to python3.8 * [TD-4025]: travis ci broken due to ubuntu 20.04 repo issue. * [TD-4025]: travis ci broken due to ubuntu 20.04 repo issue. * [TD-4025]: travis ci broken due to ubuntu 20.04 repo issue. * [TD-4025]: travis ci broken due to valgrind dependency missed. change focal to bionic. * [TD-4025]: travis ci broken due to ubuntu 20.04 repo issue. cherry pick from master. * don't check maven on windows for appveyor. * fix timezone build error with clang/linux. * add .appveyor.yml back. Co-authored-by: Shuduo Sang --- src/os/src/detail/osTime.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/os/src/detail/osTime.c b/src/os/src/detail/osTime.c index 67e0c2642e..2956dd29ad 100644 --- a/src/os/src/detail/osTime.c +++ b/src/os/src/detail/osTime.c @@ -43,7 +43,7 @@ */ int64_t user_mktime64(const unsigned int year0, const unsigned int mon0, const unsigned int day, const unsigned int hour, - const unsigned int min, const unsigned int sec, int64_t timezone) + const unsigned int min, const unsigned int sec, int64_t time_zone) { unsigned int mon = mon0, year = year0; @@ -61,7 +61,7 @@ int64_t user_mktime64(const unsigned int year0, const unsigned int mon0, res = res*24; res = ((res + hour) * 60 + min) * 60 + sec; - return (res + timezone); + return (res + time_zone); } // ==== mktime() kernel code =================// From 8d0a4d617a8fb79058557d9bc82e2d72aa6855b8 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Sat, 5 Jun 2021 03:54:42 +0800 Subject: [PATCH 25/57] [TD-4426] fix in operator bug --- src/client/src/tscSQLParser.c | 51 ++++++++++++++++++++++++----------- src/common/src/texpr.c | 4 ++- src/query/src/qExecutor.c | 3 +++ src/query/src/qFilterfunc.c | 2 +- src/tsdb/src/tsdbRead.c | 2 ++ 5 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index e062cc61f6..b2353dc92c 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -63,7 +63,7 @@ static SExprInfo* doAddProjectCol(SQueryInfo* pQueryInfo, int32_t colIndex, int3 static int32_t setShowInfo(SSqlObj* pSql, SSqlInfo* pInfo); static char* getAccountId(SSqlObj* pSql); -static bool serializeExprListToVariant(SArray* pList, tVariant **dest); +static bool serializeExprListToVariant(SArray* pList, tVariant **dest, int8_t colType); static int32_t validateParamOfRelationIn(tVariant *pVar, int32_t colType); static bool has(SArray* pFieldList, int32_t startIdx, const char* name); @@ -148,31 +148,41 @@ int16_t getNewResColId(SSqlCmd* pCmd) { // serialize expr in exprlist to binary // formate "type | size | value" -bool serializeExprListToVariant(SArray* pList, tVariant **dst) { +bool serializeExprListToVariant(SArray* pList, tVariant **dst, int8_t colType) { bool ret = false; if (!pList || pList->size <= 0) { return ret; } + tSqlExprItem* item = (tSqlExprItem *)taosArrayGet(pList, 0); + int32_t firstTokenType = item->pNode->token.type; + int32_t type = firstTokenType; + + //nchar to binary and + toTSDBType(type); + if (colType >= 0) { + if (type != colType && (type != TSDB_DATA_TYPE_BINARY || colType != TSDB_DATA_TYPE_NCHAR)) { + return false; + } + type = colType; + } + SBufferWriter bw = tbufInitWriter( NULL, false ); tbufEnsureCapacity(&bw, 512); - tSqlExprItem* item = (tSqlExprItem *)taosArrayGet(pList, 0); int32_t size = pList->size; - int32_t type = item->pNode->token.type; - - toTSDBType(type); tbufWriteUint32(&bw, type); tbufWriteInt32(&bw, size); for (int32_t i = 0; i < size; i++) { tSqlExpr* pSub = ((tSqlExprItem*)(taosArrayGet(pList, i)))->pNode; - // type in exprList is same or not - toTSDBType(pSub->token.type); - if (type != pSub->token.type) { + // check all the token type in expr list same or not + if (firstTokenType != pSub->token.type) { break; - } + } + + toTSDBType(pSub->token.type); tVariant var; tVariantCreate(&var, &pSub->token); @@ -181,8 +191,17 @@ bool serializeExprListToVariant(SArray* pList, tVariant **dst) { tbufWriteInt64(&bw, var.i64); } else if (type == TSDB_DATA_TYPE_DOUBLE || type == TSDB_DATA_TYPE_FLOAT) { tbufWriteDouble(&bw, var.dKey); - } else if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { + } else if (type == TSDB_DATA_TYPE_BINARY){ tbufWriteBinary(&bw, var.pz, var.nLen); + } else if (type == TSDB_DATA_TYPE_NCHAR) { + char *buf = (char *)calloc(1, (var.nLen + 1)*TSDB_NCHAR_SIZE); + if (tVariantDump(&var, buf, type, false) != TSDB_CODE_SUCCESS) { + free(buf); + tVariantDestroy(&var); + break; + } + tbufWriteBinary(&bw, buf, twcslen((wchar_t *)buf) * TSDB_NCHAR_SIZE); + free(buf); } tVariantDestroy(&var); @@ -204,9 +223,8 @@ static int32_t validateParamOfRelationIn(tVariant *pVar, int32_t colType) { if (pVar->nType != TSDB_DATA_TYPE_BINARY) { return -1; } - return 0; - //SBufferReader br = tbufInitReader(pVar->pz, pVar->nLen, false); - //return tbufReadUint32(&br) == colType ? 0: -1; + SBufferReader br = tbufInitReader(pVar->pz, pVar->nLen, false); + return colType == TSDB_DATA_TYPE_NCHAR ? 0 : (tbufReadUint32(&br) == colType ? 0: -1); } static uint8_t convertOptr(SStrToken *pToken) { @@ -229,6 +247,7 @@ static uint8_t convertOptr(SStrToken *pToken) { return TSDB_RELATION_EQUAL; case TK_PLUS: return TSDB_BINARY_OP_ADD; + case TK_MINUS: return TSDB_BINARY_OP_SUBTRACT; case TK_STAR: @@ -3276,7 +3295,7 @@ static int32_t doExtractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, // TK_GT,TK_GE,TK_EQ,TK_NE are based on the pColumn->lowerBndd } else if (pExpr->tokenId == TK_IN) { tVariant *pVal; - if (pRight->tokenId != TK_SET || !serializeExprListToVariant(pRight->pParam, &pVal)) { + if (pRight->tokenId != TK_SET || !serializeExprListToVariant(pRight->pParam, &pVal, colType) || colType == TSDB_DATA_TYPE_TIMESTAMP) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg); } if (validateParamOfRelationIn(pVal, colType) != TSDB_CODE_SUCCESS) { @@ -8007,7 +8026,7 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS return TSDB_CODE_SUCCESS; } else if (pSqlExpr->tokenId == TK_SET) { tVariant *pVal; - if (serializeExprListToVariant(pSqlExpr->pParam, &pVal) == false) { + if (serializeExprListToVariant(pSqlExpr->pParam, &pVal, -1) == false) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "not support filter expression"); } *pExpr = calloc(1, sizeof(tExprNode)); diff --git a/src/common/src/texpr.c b/src/common/src/texpr.c index 2690c18e5e..e78f78a12e 100644 --- a/src/common/src/texpr.c +++ b/src/common/src/texpr.c @@ -485,7 +485,9 @@ void buildFilterSetFromBinary(void **q, const char *buf, int32_t len) { taosHashPut(pObj, (char *)val, t, &dummy, sizeof(dummy)); } else if (type == TSDB_DATA_TYPE_NCHAR) { - + size_t t = 0; + const char *val = tbufReadBinary(&br, &t); + taosHashPut(pObj, (char *)val, t, &dummy, sizeof(dummy)); } } *q = (void *)pObj; diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 350e7118a8..c7aae3b93b 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -6956,6 +6956,9 @@ int32_t doCreateFilterInfo(SColumnInfo* pCols, int32_t numOfCols, int32_t numOfF void* doDestroyFilterInfo(SSingleColumnFilterInfo* pFilterInfo, int32_t numOfFilterCols) { for (int32_t i = 0; i < numOfFilterCols; ++i) { if (pFilterInfo[i].numOfFilters > 0) { + if (pFilterInfo[i].pFilters->filterInfo.lowerRelOptr == TSDB_RELATION_IN) { + taosHashCleanup((SHashObj *)(pFilterInfo[i].pFilters->q)); + } tfree(pFilterInfo[i].pFilters); } } diff --git a/src/query/src/qFilterfunc.c b/src/query/src/qFilterfunc.c index 5e16107f70..663f8814a0 100644 --- a/src/query/src/qFilterfunc.c +++ b/src/query/src/qFilterfunc.c @@ -269,7 +269,7 @@ bool inOperator(SColumnFilterElem *pFilter, const char* minval, const char* maxv } else if (type == TSDB_DATA_TYPE_BINARY) { return NULL != taosHashGet((SHashObj *)pFilter->q, varDataVal(minval), varDataLen(minval)); } else if (type == TSDB_DATA_TYPE_NCHAR){ - return NULL != taosHashGet((SHashObj *)pFilter->q, varDataVal(minval), varDataLen(minval)/TSDB_NCHAR_SIZE); + return NULL != taosHashGet((SHashObj *)pFilter->q, varDataVal(minval), varDataLen(minval)); } return false; diff --git a/src/tsdb/src/tsdbRead.c b/src/tsdb/src/tsdbRead.c index 436094fe2d..1dfba31b54 100644 --- a/src/tsdb/src/tsdbRead.c +++ b/src/tsdb/src/tsdbRead.c @@ -2397,6 +2397,8 @@ static void destroyHelper(void* param) { tQueryInfo* pInfo = (tQueryInfo*)param; if (pInfo->optr != TSDB_RELATION_IN) { tfree(pInfo->q); + } else { + taosHashCleanup((SHashObj *)(pInfo->q)); } free(param); From bb19833711a9f442d77aa98a4a8c0a0f7dd5c3c8 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Sat, 5 Jun 2021 06:06:09 +0800 Subject: [PATCH 26/57] [TD-4426] add test case --- src/client/src/tscSQLParser.c | 26 ++++-- src/common/src/texpr.c | 1 - tests/pytest/query/in.py | 163 ++++++++++++++++++++++++++++++++++ 3 files changed, 182 insertions(+), 8 deletions(-) create mode 100644 tests/pytest/query/in.py diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index b2353dc92c..41b28d0014 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -153,6 +153,9 @@ bool serializeExprListToVariant(SArray* pList, tVariant **dst, int8_t colType) { if (!pList || pList->size <= 0) { return ret; } + if (colType == TSDB_DATA_TYPE_DOUBLE || colType == TSDB_DATA_TYPE_FLOAT) { + return ret; + } tSqlExprItem* item = (tSqlExprItem *)taosArrayGet(pList, 0); int32_t firstTokenType = item->pNode->token.type; @@ -160,12 +163,11 @@ bool serializeExprListToVariant(SArray* pList, tVariant **dst, int8_t colType) { //nchar to binary and toTSDBType(type); - if (colType >= 0) { - if (type != colType && (type != TSDB_DATA_TYPE_BINARY || colType != TSDB_DATA_TYPE_NCHAR)) { - return false; - } - type = colType; - } + if (type != colType && (type != TSDB_DATA_TYPE_BINARY || colType != TSDB_DATA_TYPE_NCHAR)) { + return false; + } + type = colType; + SBufferWriter bw = tbufInitWriter( NULL, false ); tbufEnsureCapacity(&bw, 512); @@ -8025,8 +8027,18 @@ int32_t exprTreeFromSqlExpr(SSqlCmd* pCmd, tExprNode **pExpr, const tSqlExpr* pS return TSDB_CODE_SUCCESS; } else if (pSqlExpr->tokenId == TK_SET) { + int32_t type = -1; + STableMeta* pTableMeta = tscGetMetaInfo(pQueryInfo, 0)->pTableMeta; + if (pCols != NULL) { + SColIndex* idx = taosArrayGet(pCols, 0); + SSchema* pSchema = tscGetTableColumnSchema(pTableMeta, idx->colIndex); + if (pSchema != NULL) { + type = pSchema->type; + } + } + tVariant *pVal; - if (serializeExprListToVariant(pSqlExpr->pParam, &pVal, -1) == false) { + if (serializeExprListToVariant(pSqlExpr->pParam, &pVal, type) == false) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "not support filter expression"); } *pExpr = calloc(1, sizeof(tExprNode)); diff --git a/src/common/src/texpr.c b/src/common/src/texpr.c index e78f78a12e..6fa4d68734 100644 --- a/src/common/src/texpr.c +++ b/src/common/src/texpr.c @@ -482,7 +482,6 @@ void buildFilterSetFromBinary(void **q, const char *buf, int32_t len) { } else if (type == TSDB_DATA_TYPE_BINARY) { size_t t = 0; const char *val = tbufReadBinary(&br, &t); - taosHashPut(pObj, (char *)val, t, &dummy, sizeof(dummy)); } else if (type == TSDB_DATA_TYPE_NCHAR) { size_t t = 0; diff --git a/tests/pytest/query/in.py b/tests/pytest/query/in.py new file mode 100644 index 0000000000..8644b74a45 --- /dev/null +++ b/tests/pytest/query/in.py @@ -0,0 +1,163 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import sys +import taos +from util.log import tdLog +from util.cases import tdCases +from util.sql import tdSql +from util.dnodes import tdDnodes + +class TDTestCase: + def init(self, conn, logSql): + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor(), logSql) + + self.ts = 1538548685000 + + def run(self): + tdSql.prepare() + + print("==============step1") + tdSql.execute( + "create table if not exists st (ts timestamp, tagtype int) tags(dev nchar(50))") + tdSql.execute( + 'CREATE TABLE if not exists dev_001 using st tags("dev_01")') + tdSql.execute( + 'CREATE TABLE if not exists dev_002 using st tags("dev_02")') + + print("==============step2") + tdSql.error("select * from db.st where ts in ('2020-05-13 10:00:00.000')") + + tdSql.execute( + """INSERT INTO dev_001(ts, tagtype) VALUES('2020-05-13 10:00:00.000', 1), + ('2020-05-13 10:00:00.001', 1) + dev_002 VALUES('2020-05-13 10:00:00.001', 1)""") + + # TAG nchar + tdSql.query('select count(ts) from db.st where dev in ("dev_01")') + tdSql.checkRows(1) + tdSql.checkData(0, 0, 2) + + tdSql.query('select count(ts) from db.st where dev in ("dev_01", "dev_01")') + tdSql.checkRows(1) + tdSql.checkData(0, 0, 2) + + tdSql.query('select count(ts) from db.st where dev in ("dev_01", "dev_01", "dev_02")') + tdSql.checkRows(1) + tdSql.checkData(0, 0, 3) + + + # colume int + tdSql.query("select count(ts) from db.st where tagtype in (1)") + tdSql.checkRows(1) + tdSql.checkData(0, 0, 3) + + tdSql.query("select count(ts) from db.st where tagtype in (1, 2)") + tdSql.checkRows(1) + tdSql.checkData(0, 0, 3) + tdSql.execute( + """INSERT INTO dev_001(ts, tagtype) VALUES('2020-05-13 10:00:01.000', 2), + ('2020-05-13 10:00:02.001', 2) + dev_002 VALUES('2020-05-13 10:00:03.001', 2)""") + + tdSql.query("select count(ts) from db.st where tagtype in (1, 2)") + tdSql.checkRows(1) + tdSql.checkData(0, 0, 6) + + tdSql.execute("create table tb(ts timestamp, c1 int, c2 binary(10), c3 nchar(10), c4 float, c5 bool)") + for i in range(10): + tdSql.execute("insert into tb values(%d, %d, 'binary%d', 'nchar%d', %f, %d)" % (self.ts + i, i, i, i, i + 0.1, i % 2)) + + #binary + tdSql.query('select count(ts) from db.tb where c2 in ("binary1")') + tdSql.checkRows(1) + tdSql.checkData(0, 0, 1) + + tdSql.query('select count(ts) from db.tb where c2 in ("binary1", "binary2", "binary1")') + tdSql.checkRows(1) + tdSql.checkData(0, 0, 2) + + #bool + #tdSql.query('select count(ts) from db.tb where c5 in (true)') + #tdSql.checkRows(1) + #tdSql.checkData(0, 0, 5) + + #float + #tdSql.query('select count(ts) from db.tb where c4 in (0.1)') + #tdSql.checkRows(1) + #tdSql.checkData(0, 0, 1) + + #nchar + tdSql.query('select count(ts) from db.tb where c3 in ("nchar0")') + tdSql.checkRows(1) + tdSql.checkData(0, 0, 1) + + + #tdSql.query("select tbname, dev from dev_001") + #tdSql.checkRows(1) + #tdSql.checkData(0, 0, 'dev_001') + #tdSql.checkData(0, 1, 'dev_01') + + #tdSql.query("select tbname, dev, tagtype from dev_001") + #tdSql.checkRows(2) + + ### test case for https://jira.taosdata.com:18080/browse/TD-1930 + #tdSql.execute("create table tb(ts timestamp, c1 int, c2 binary(10), c3 nchar(10), c4 float, c5 bool)") + #for i in range(10): + # tdSql.execute("insert into tb values(%d, %d, 'binary%d', 'nchar%d', %f, %d)" % (self.ts + i, i, i, i, i + 0.1, i % 2)) + # + #tdSql.error("select * from tb where c2 = binary2") + #tdSql.error("select * from tb where c3 = nchar2") + + #tdSql.query("select * from tb where c2 = 'binary2' ") + #tdSql.checkRows(1) + + #tdSql.query("select * from tb where c3 = 'nchar2' ") + #tdSql.checkRows(1) + + #tdSql.query("select * from tb where c1 = '2' ") + #tdSql.checkRows(1) + + #tdSql.query("select * from tb where c1 = 2 ") + #tdSql.checkRows(1) + + #tdSql.query("select * from tb where c4 = '0.1' ") + #tdSql.checkRows(1) + + #tdSql.query("select * from tb where c4 = 0.1 ") + #tdSql.checkRows(1) + + #tdSql.query("select * from tb where c5 = true ") + #tdSql.checkRows(5) + + #tdSql.query("select * from tb where c5 = 'true' ") + #tdSql.checkRows(5) + + ## For jira: https://jira.taosdata.com:18080/browse/TD-2850 + #tdSql.execute("create database 'Test' ") + #tdSql.execute("use 'Test' ") + #tdSql.execute("create table 'TB'(ts timestamp, 'Col1' int) tags('Tag1' int)") + #tdSql.execute("insert into 'Tb0' using tb tags(1) values(now, 1)") + #tdSql.query("select * from tb") + #tdSql.checkRows(1) + + + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) From 5e40ac371031bc0a476a536fd28d4fd09c6667e8 Mon Sep 17 00:00:00 2001 From: dapan1121 <89396746@qq.com> Date: Sat, 5 Jun 2021 10:20:03 +0800 Subject: [PATCH 27/57] add error msg --- src/client/src/tscPrepare.c | 24 ++++++++++++------------ src/query/src/qExecutor.c | 10 +++++----- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/client/src/tscPrepare.c b/src/client/src/tscPrepare.c index 00d8b60e2f..f79ff671c6 100644 --- a/src/client/src/tscPrepare.c +++ b/src/client/src/tscPrepare.c @@ -1667,12 +1667,12 @@ int taos_stmt_bind_param(TAOS_STMT* stmt, TAOS_BIND* bind) { if (pStmt->multiTbInsert) { if (pStmt->last != STMT_SETTBNAME && pStmt->last != STMT_ADD_BATCH) { tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"); } } else { if (pStmt->last != STMT_PREPARE && pStmt->last != STMT_ADD_BATCH && pStmt->last != STMT_EXECUTE) { tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"); } } @@ -1695,23 +1695,23 @@ int taos_stmt_bind_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind) { if (bind == NULL || bind->num <= 0 || bind->num > INT16_MAX) { tscError("0x%"PRIx64" invalid parameter", pStmt->pSql->self); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "invalid bind param"); } if (!pStmt->isInsert) { tscError("0x%"PRIx64" not or invalid batch insert", pStmt->pSql->self); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "not or invalid batch insert"); } if (pStmt->multiTbInsert) { if (pStmt->last != STMT_SETTBNAME && pStmt->last != STMT_ADD_BATCH) { tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"); } } else { if (pStmt->last != STMT_PREPARE && pStmt->last != STMT_ADD_BATCH && pStmt->last != STMT_EXECUTE) { tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"); } } @@ -1729,23 +1729,23 @@ int taos_stmt_bind_single_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, in if (bind == NULL || bind->num <= 0 || bind->num > INT16_MAX) { tscError("0x%"PRIx64" invalid parameter", pStmt->pSql->self); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "invalid bind param"); } if (!pStmt->isInsert) { tscError("0x%"PRIx64" not or invalid batch insert", pStmt->pSql->self); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "not or invalid batch insert"); } if (pStmt->multiTbInsert) { if (pStmt->last != STMT_SETTBNAME && pStmt->last != STMT_ADD_BATCH && pStmt->last != STMT_BIND_COL) { tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"); } } else { if (pStmt->last != STMT_PREPARE && pStmt->last != STMT_ADD_BATCH && pStmt->last != STMT_BIND_COL && pStmt->last != STMT_EXECUTE) { tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"); } } @@ -1766,7 +1766,7 @@ int taos_stmt_add_batch(TAOS_STMT* stmt) { if (pStmt->isInsert) { if (pStmt->last != STMT_BIND && pStmt->last != STMT_BIND_COL) { tscError("0x%"PRIx64" add batch status error, last:%d", pStmt->pSql->self, pStmt->last); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "add batch status error"); } pStmt->last = STMT_ADD_BATCH; @@ -1796,7 +1796,7 @@ int taos_stmt_execute(TAOS_STMT* stmt) { if (pStmt->isInsert) { if (pStmt->last != STMT_ADD_BATCH) { tscError("0x%"PRIx64" exec status error, last:%d", pStmt->pSql->self, pStmt->last); - return TSDB_CODE_TSC_APP_ERROR; + return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "exec status error"); } pStmt->last = STMT_EXECUTE; diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 5ac19bba82..698a1c749b 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -4096,15 +4096,13 @@ static SFillColInfo* createFillColInfo(SExprInfo* pExpr, int32_t numOfOutput, in return pFillCol; } -int32_t doInitQInfo(SQInfo* pQInfo, STSBuf* pTsBuf, SArray* prevResult, void* tsdb, void* sourceOptr, int32_t tbScanner, +int32_t doInitQInfo(SQInfo* pQInfo, STSBuf* pTsBuf, void* tsdb, void* sourceOptr, int32_t tbScanner, SArray* pOperator, void* param) { SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; SQueryAttr *pQueryAttr = pQInfo->runtimeEnv.pQueryAttr; pQueryAttr->tsdb = tsdb; - pRuntimeEnv->prevResult = prevResult; - if (tsdb != NULL) { int32_t code = setupQueryHandle(tsdb, pRuntimeEnv, pQInfo->qId, pQueryAttr->stableQuery); if (code != TSDB_CODE_SUCCESS) { @@ -7254,7 +7252,9 @@ int32_t initQInfo(STsBufInfo* pTsBufInfo, void* tsdb, void* sourceOptr, SQInfo* SArray* prevResult = NULL; if (prevResultLen > 0) { - prevResult = interResFromBinary(param->prevResult, prevResultLen); + prevResult = interResFromBinary(param->prevResult, prevResultLen); + + pRuntimeEnv->prevResult = prevResult; } if (tsdb != NULL) { @@ -7278,7 +7278,7 @@ int32_t initQInfo(STsBufInfo* pTsBufInfo, void* tsdb, void* sourceOptr, SQInfo* } // filter the qualified - if ((code = doInitQInfo(pQInfo, pTsBuf, prevResult, tsdb, sourceOptr, param->tableScanOperator, param->pOperator, merger)) != TSDB_CODE_SUCCESS) { + if ((code = doInitQInfo(pQInfo, pTsBuf, tsdb, sourceOptr, param->tableScanOperator, param->pOperator, merger)) != TSDB_CODE_SUCCESS) { goto _error; } From d6e5fb80a179b8f267ad992de56885a597cfc522 Mon Sep 17 00:00:00 2001 From: dapan1121 <89396746@qq.com> Date: Sat, 5 Jun 2021 10:38:45 +0800 Subject: [PATCH 28/57] fix mem leak issue --- src/query/src/qExecutor.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 5ac19bba82..698a1c749b 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -4096,15 +4096,13 @@ static SFillColInfo* createFillColInfo(SExprInfo* pExpr, int32_t numOfOutput, in return pFillCol; } -int32_t doInitQInfo(SQInfo* pQInfo, STSBuf* pTsBuf, SArray* prevResult, void* tsdb, void* sourceOptr, int32_t tbScanner, +int32_t doInitQInfo(SQInfo* pQInfo, STSBuf* pTsBuf, void* tsdb, void* sourceOptr, int32_t tbScanner, SArray* pOperator, void* param) { SQueryRuntimeEnv *pRuntimeEnv = &pQInfo->runtimeEnv; SQueryAttr *pQueryAttr = pQInfo->runtimeEnv.pQueryAttr; pQueryAttr->tsdb = tsdb; - pRuntimeEnv->prevResult = prevResult; - if (tsdb != NULL) { int32_t code = setupQueryHandle(tsdb, pRuntimeEnv, pQInfo->qId, pQueryAttr->stableQuery); if (code != TSDB_CODE_SUCCESS) { @@ -7254,7 +7252,9 @@ int32_t initQInfo(STsBufInfo* pTsBufInfo, void* tsdb, void* sourceOptr, SQInfo* SArray* prevResult = NULL; if (prevResultLen > 0) { - prevResult = interResFromBinary(param->prevResult, prevResultLen); + prevResult = interResFromBinary(param->prevResult, prevResultLen); + + pRuntimeEnv->prevResult = prevResult; } if (tsdb != NULL) { @@ -7278,7 +7278,7 @@ int32_t initQInfo(STsBufInfo* pTsBufInfo, void* tsdb, void* sourceOptr, SQInfo* } // filter the qualified - if ((code = doInitQInfo(pQInfo, pTsBuf, prevResult, tsdb, sourceOptr, param->tableScanOperator, param->pOperator, merger)) != TSDB_CODE_SUCCESS) { + if ((code = doInitQInfo(pQInfo, pTsBuf, tsdb, sourceOptr, param->tableScanOperator, param->pOperator, merger)) != TSDB_CODE_SUCCESS) { goto _error; } From 19a7b0c4d62a118a33c3c5bae69717b403eac078 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Sat, 5 Jun 2021 10:56:26 +0800 Subject: [PATCH 29/57] [TD-4426] fix compile error --- src/query/src/qExecutor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index c7aae3b93b..10dac295b3 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -6942,7 +6942,7 @@ int32_t doCreateFilterInfo(SColumnInfo* pCols, int32_t numOfCols, int32_t numOfF pSingleColFilter->bytes = pCols[i].bytes; if (lower == TSDB_RELATION_IN) { - buildFilterSetFromBinary(&pSingleColFilter->q, (char *)(pSingleColFilter->filterInfo.pz), pSingleColFilter->filterInfo.len); + buildFilterSetFromBinary(&pSingleColFilter->q, (char *)(pSingleColFilter->filterInfo.pz), (int32_t)(pSingleColFilter->filterInfo.len)); } } From 9fe132abd44b3331210aedb410984cbb3a0a4fb3 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Sat, 5 Jun 2021 11:17:13 +0800 Subject: [PATCH 30/57] [TD-4426] fix compile error --- src/client/src/tscSQLParser.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 41b28d0014..e040843b0e 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -63,7 +63,7 @@ static SExprInfo* doAddProjectCol(SQueryInfo* pQueryInfo, int32_t colIndex, int3 static int32_t setShowInfo(SSqlObj* pSql, SSqlInfo* pInfo); static char* getAccountId(SSqlObj* pSql); -static bool serializeExprListToVariant(SArray* pList, tVariant **dest, int8_t colType); +static bool serializeExprListToVariant(SArray* pList, tVariant **dest, int16_t colType); static int32_t validateParamOfRelationIn(tVariant *pVar, int32_t colType); static bool has(SArray* pFieldList, int32_t startIdx, const char* name); @@ -148,7 +148,7 @@ int16_t getNewResColId(SSqlCmd* pCmd) { // serialize expr in exprlist to binary // formate "type | size | value" -bool serializeExprListToVariant(SArray* pList, tVariant **dst, int8_t colType) { +bool serializeExprListToVariant(SArray* pList, tVariant **dst, int16_t colType) { bool ret = false; if (!pList || pList->size <= 0) { return ret; From b4b0e949b454e5112271c554ef390a559bb0fa42 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Sat, 5 Jun 2021 11:35:26 +0800 Subject: [PATCH 31/57] [TD-4426] fix compile error --- src/client/src/tscSQLParser.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index e040843b0e..9263cb1170 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -168,11 +168,10 @@ bool serializeExprListToVariant(SArray* pList, tVariant **dst, int16_t colType) } type = colType; - SBufferWriter bw = tbufInitWriter( NULL, false ); tbufEnsureCapacity(&bw, 512); - int32_t size = pList->size; + int32_t size = (int32_t)(pList->size); tbufWriteUint32(&bw, type); tbufWriteInt32(&bw, size); From 046ec16ac42a2a3572fb85cf2ede5c4e4cdd0e10 Mon Sep 17 00:00:00 2001 From: tomchon Date: Sat, 5 Jun 2021 16:24:30 +0800 Subject: [PATCH 32/57] [TD-4552]: add testcase that compact wal file with replica --- tests/pytest/wal/insertDataDb1Replica2.json | 87 +++++++++++ .../wal/insertDataDb2NewstabReplica2.json | 86 +++++++++++ tests/pytest/wal/insertDataDb2Replica2.json | 86 +++++++++++ tests/pytest/wal/sdbComp.py | 6 +- tests/pytest/wal/sdbCompCluster.py | 31 ++-- tests/pytest/wal/sdbCompClusterReplica2.py | 136 ++++++++++++++++++ 6 files changed, 419 insertions(+), 13 deletions(-) create mode 100644 tests/pytest/wal/insertDataDb1Replica2.json create mode 100644 tests/pytest/wal/insertDataDb2NewstabReplica2.json create mode 100644 tests/pytest/wal/insertDataDb2Replica2.json create mode 100644 tests/pytest/wal/sdbCompClusterReplica2.py diff --git a/tests/pytest/wal/insertDataDb1Replica2.json b/tests/pytest/wal/insertDataDb1Replica2.json new file mode 100644 index 0000000000..fec38bcdec --- /dev/null +++ b/tests/pytest/wal/insertDataDb1Replica2.json @@ -0,0 +1,87 @@ +{ + "filetype": "insert", + "cfgdir": "/etc/taos", + "host": "127.0.0.1", + "port": 6030, + "user": "root", + "password": "taosdata", + "thread_count": 4, + "thread_count_create_tbl": 4, + "result_file": "./insert_res.txt", + "confirm_parameter_prompt": "no", + "insert_interval": 0, + "interlace_rows": 10, + "num_of_records_per_req": 1000, + "max_sql_len": 1024000, + "databases": [{ + "dbinfo": { + "name": "db1", + "drop": "yes", + "replica": 2, + "days": 10, + "cache": 50, + "blocks": 8, + "precision": "ms", + "keep": 365, + "minRows": 100, + "maxRows": 4096, + "comp":2, + "walLevel":1, + "cachelast":0, + "quorum":1, + "fsync":3000, + "update": 0 + }, + "super_tables": [{ + "name": "stb0", + "child_table_exists":"no", + "childtable_count": 1000, + "childtable_prefix": "stb00_", + "auto_create_table": "no", + "batch_create_tbl_num": 100, + "data_source": "rand", + "insert_mode": "taosc", + "insert_rows": 100, + "childtable_limit": 0, + "childtable_offset":0, + "interlace_rows": 0, + "insert_interval":0, + "max_sql_len": 1024000, + "disorder_ratio": 0, + "disorder_range": 1000, + "timestamp_step": 1, + "start_timestamp": "2020-10-01 00:00:00.000", + "sample_format": "csv", + "sample_file": "./sample.csv", + "tags_file": "", + "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":1}, {"type": "BINARY", "len": 16, "count":1}, {"type": "BINARY", "len": 32, "count":1}], + "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":1}] + }, + { + "name": "stb1", + "child_table_exists":"no", + "childtable_count": 1000, + "childtable_prefix": "stb01_", + "auto_create_table": "no", + "batch_create_tbl_num": 10, + "data_source": "rand", + "insert_mode": "taosc", + "insert_rows": 200, + "childtable_limit": 0, + "childtable_offset":0, + "interlace_rows": 0, + "insert_interval":0, + "max_sql_len": 1024000, + "disorder_ratio": 0, + "disorder_range": 1000, + "timestamp_step": 1, + "start_timestamp": "2020-10-01 00:00:00.000", + "sample_format": "csv", + "sample_file": "./sample.csv", + "tags_file": "", + "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":1}, {"type": "BINARY", "len": 16, "count":1}, {"type": "BINARY", "len": 32, "count":1}], + "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":1}] + }] + }] +} + diff --git a/tests/pytest/wal/insertDataDb2NewstabReplica2.json b/tests/pytest/wal/insertDataDb2NewstabReplica2.json new file mode 100644 index 0000000000..e052f2850f --- /dev/null +++ b/tests/pytest/wal/insertDataDb2NewstabReplica2.json @@ -0,0 +1,86 @@ +{ + "filetype": "insert", + "cfgdir": "/etc/taos", + "host": "127.0.0.1", + "port": 6030, + "user": "root", + "password": "taosdata", + "thread_count": 4, + "thread_count_create_tbl": 4, + "result_file": "./insert_res.txt", + "confirm_parameter_prompt": "no", + "insert_interval": 0, + "interlace_rows": 0, + "num_of_records_per_req": 3000, + "max_sql_len": 1024000, + "databases": [{ + "dbinfo": { + "name": "db2", + "drop": "no", + "replica": 2, + "days": 10, + "cache": 50, + "blocks": 8, + "precision": "ms", + "keep": 365, + "minRows": 100, + "maxRows": 4096, + "comp":2, + "walLevel":1, + "cachelast":0, + "quorum":1, + "fsync":3000, + "update": 0 + }, + "super_tables": [{ + "name": "stb0", + "child_table_exists":"no", + "childtable_count": 1, + "childtable_prefix": "stb0_", + "auto_create_table": "no", + "batch_create_tbl_num": 100, + "data_source": "rand", + "insert_mode": "taosc", + "insert_rows": 0, + "childtable_limit": -1, + "childtable_offset":0, + "interlace_rows": 0, + "insert_interval":0, + "max_sql_len": 1024000, + "disorder_ratio": 0, + "disorder_range": 1000, + "timestamp_step": 1, + "start_timestamp": "2020-10-01 00:00:00.000", + "sample_format": "csv", + "sample_file": "./sample.csv", + "tags_file": "", + "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":1}, {"type": "BINARY", "len": 16, "count":1}, {"type": "BINARY", "len": 32, "count":1}], + "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":1}] + }, + { + "name": "stb1", + "child_table_exists":"yes", + "childtable_count": 1, + "childtable_prefix": "stb01_", + "auto_create_table": "no", + "batch_create_tbl_num": 10, + "data_source": "rand", + "insert_mode": "taosc", + "insert_rows": 10, + "childtable_limit": -1, + "childtable_offset":0, + "interlace_rows": 0, + "insert_interval":0, + "max_sql_len": 1024000, + "disorder_ratio": 0, + "disorder_range": 1000, + "timestamp_step": 1, + "start_timestamp": "2020-11-01 00:00:00.000", + "sample_format": "csv", + "sample_file": "./sample.csv", + "tags_file": "", + "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":1}, {"type": "BINARY", "len": 16, "count":1}], + "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":1}] + }] + }] +} diff --git a/tests/pytest/wal/insertDataDb2Replica2.json b/tests/pytest/wal/insertDataDb2Replica2.json new file mode 100644 index 0000000000..121f70956a --- /dev/null +++ b/tests/pytest/wal/insertDataDb2Replica2.json @@ -0,0 +1,86 @@ +{ + "filetype": "insert", + "cfgdir": "/etc/taos", + "host": "127.0.0.1", + "port": 6030, + "user": "root", + "password": "taosdata", + "thread_count": 4, + "thread_count_create_tbl": 4, + "result_file": "./insert_res.txt", + "confirm_parameter_prompt": "no", + "insert_interval": 0, + "interlace_rows": 0, + "num_of_records_per_req": 3000, + "max_sql_len": 1024000, + "databases": [{ + "dbinfo": { + "name": "db2", + "drop": "yes", + "replica": 2, + "days": 10, + "cache": 50, + "blocks": 8, + "precision": "ms", + "keep": 365, + "minRows": 100, + "maxRows": 4096, + "comp":2, + "walLevel":1, + "cachelast":0, + "quorum":1, + "fsync":3000, + "update": 0 + }, + "super_tables": [{ + "name": "stb0", + "child_table_exists":"no", + "childtable_count": 2000, + "childtable_prefix": "stb0_", + "auto_create_table": "no", + "batch_create_tbl_num": 100, + "data_source": "rand", + "insert_mode": "taosc", + "insert_rows": 2000, + "childtable_limit": 0, + "childtable_offset":0, + "interlace_rows": 0, + "insert_interval":0, + "max_sql_len": 1024000, + "disorder_ratio": 0, + "disorder_range": 1000, + "timestamp_step": 1, + "start_timestamp": "2020-10-01 00:00:00.000", + "sample_format": "csv", + "sample_file": "./sample.csv", + "tags_file": "", + "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":1}, {"type": "BINARY", "len": 16, "count":1}, {"type": "BINARY", "len": 32, "count":1}], + "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":1}] + }, + { + "name": "stb1", + "child_table_exists":"no", + "childtable_count": 2, + "childtable_prefix": "stb1_", + "auto_create_table": "no", + "batch_create_tbl_num": 10, + "data_source": "rand", + "insert_mode": "taosc", + "insert_rows": 5, + "childtable_limit": 0, + "childtable_offset":0, + "interlace_rows": 0, + "insert_interval":0, + "max_sql_len": 1024000, + "disorder_ratio": 0, + "disorder_range": 1000, + "timestamp_step": 1, + "start_timestamp": "2020-10-01 00:00:00.000", + "sample_format": "csv", + "sample_file": "./sample.csv", + "tags_file": "", + "columns": [{"type": "INT"}, {"type": "DOUBLE", "count":1}, {"type": "BINARY", "len": 16, "count":1}, {"type": "BINARY", "len": 32, "count":1}], + "tags": [{"type": "TINYINT", "count":2}, {"type": "BINARY", "len": 16, "count":1}] + }] + }] +} diff --git a/tests/pytest/wal/sdbComp.py b/tests/pytest/wal/sdbComp.py index e0a43d5e0e..c0ac02610f 100644 --- a/tests/pytest/wal/sdbComp.py +++ b/tests/pytest/wal/sdbComp.py @@ -108,10 +108,10 @@ class TDTestCase: tdSql.query("select count(*) from stb2_0") tdSql.checkData(0, 0, 2) - - + # delete useless file + testcaseFilename = os.path.split(__file__)[-1] os.system("rm -rf ./insert_res.txt") - os.system("rm -rf wal/sdbComp.py.sql") + os.system("rm -rf wal/%s.sql" % testcaseFilename ) diff --git a/tests/pytest/wal/sdbCompCluster.py b/tests/pytest/wal/sdbCompCluster.py index 43cf4be3c6..4fa84817ec 100644 --- a/tests/pytest/wal/sdbCompCluster.py +++ b/tests/pytest/wal/sdbCompCluster.py @@ -61,7 +61,6 @@ class TwoClients: print(conn1) cur1 = conn1.cursor() tdSql.init(cur1, True) - # tdSql.init(cur2, True) # new db and insert data os.system("rm -rf /var/lib/taos/mnode_bak/") @@ -87,17 +86,12 @@ class TwoClients: tdSql.execute("alter table stb2_0 add column col2 binary(4)") tdSql.execute("alter table stb2_0 drop column col1") tdSql.execute("insert into stb2_0 values(1614218422000,8638,'R')") - - conn2 = taos.connect(host=self.host, user=self.user, password=self.password, config=self.config ) - print(conn2) - cur2 = conn2.cursor() - tdSql.init(cur2, True) # stop taosd and compact wal file - os.system("ps -ef |grep taosd |grep -v 'grep' |awk '{print $2}'|xargs kill -9") - sleep(2) + os.system("ps -ef |grep taosd |grep -v 'grep' |awk '{print $2}'|xargs kill -2") + sleep(10) os.system("nohup taosd --compact-mnode-wal -c /etc/taos & ") - sleep(5) + sleep(10) os.system("nohup /usr/bin/taosd > /dev/null 2>&1 &") sleep(4) tdSql.execute("reset query cache") @@ -105,7 +99,17 @@ class TwoClients: print(query_pid2) assert os.path.exists(walFilePath) , "%s is not generated " % walFilePath + # new taos connecting to server + conn2 = taos.connect(host=self.host, user=self.user, password=self.password, config=self.config ) + print(conn2) + cur2 = conn2.cursor() + tdSql.init(cur2, True) + # use new wal file to start up tasod + tdSql.query("show databases") + for i in range(tdSql.queryRows): + if tdSql.queryResult[i][0]=="db2": + assert tdSql.queryResult[i][4]==1 , "replica is wrong" tdSql.execute("use db2") tdSql.query("select count (tbname) from stb0") tdSql.checkData(0, 0, 1) @@ -117,7 +121,14 @@ class TwoClients: tdSql.checkData(0, 0, 2) tdSql.query("select count(*) from stb2_0") tdSql.checkData(0, 0, 2) - + tdSql.query("select * from stb2_0") + tdSql.checkData(1, 2, 'R') + + # delete useless file + testcaseFilename = os.path.split(__file__)[-1] + os.system("rm -rf ./insert_res.txt") + os.system("rm -rf wal/%s.sql" % testcaseFilename ) + clients = TwoClients() clients.initConnection() # clients.getBuildPath() diff --git a/tests/pytest/wal/sdbCompClusterReplica2.py b/tests/pytest/wal/sdbCompClusterReplica2.py new file mode 100644 index 0000000000..117da8ca2f --- /dev/null +++ b/tests/pytest/wal/sdbCompClusterReplica2.py @@ -0,0 +1,136 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import os +import sys +sys.path.insert(0, os.getcwd()) +from util.log import * +from util.sql import * +from util.dnodes import * +import taos +import threading + + +class TwoClients: + def initConnection(self): + self.host = "chenhaoran02" + self.user = "root" + self.password = "taosdata" + self.config = "/etc/taos/" + self.port =6030 + self.rowNum = 10 + self.ts = 1537146000000 + + def getBuildPath(self): + selfPath = os.path.dirname(os.path.realpath(__file__)) + + if ("community" in selfPath): + projPath = selfPath[:selfPath.find("community")] + else: + projPath = selfPath[:selfPath.find("tests")] + + for root, dirs, files in os.walk(projPath): + if ("taosd" in files): + rootRealPath = os.path.dirname(os.path.realpath(root)) + if ("packaging" not in rootRealPath): + buildPath = root[:len(root)-len("/build/bin")] + break + return buildPath + + def run(self): + buildPath = self.getBuildPath() + if (buildPath == ""): + tdLog.exit("taosd not found!") + else: + tdLog.info("taosd found in %s" % buildPath) + binPath = buildPath+ "/build/bin/" + walFilePath = "/var/lib/taos/mnode_bak/wal/" + + # new taos client + conn1 = taos.connect(host=self.host, user=self.user, password=self.password, config=self.config ) + print(conn1) + cur1 = conn1.cursor() + tdSql.init(cur1, True) + + # new db and insert data + os.system("rm -rf /var/lib/taos/mnode_bak/") + os.system("rm -rf /var/lib/taos/mnode_temp/") + tdSql.execute("drop database if exists db2") + os.system("%staosdemo -f wal/insertDataDb1Replica2.json -y " % binPath) + tdSql.execute("drop database if exists db1") + os.system("%staosdemo -f wal/insertDataDb2Replica2.json -y " % binPath) + tdSql.execute("drop table if exists db2.stb0") + os.system("%staosdemo -f wal/insertDataDb2NewstabReplica2.json -y " % binPath) + query_pid1 = int(subprocess.getstatusoutput('ps aux|grep taosd |grep -v "grep"|awk \'{print $2}\'')[1]) + print(query_pid1) + tdSql.execute("use db2") + tdSql.execute("drop table if exists stb1_0") + tdSql.execute("drop table if exists stb1_1") + tdSql.execute("insert into stb0_0 values(1614218412000,8637,78.861045,'R','bf3')(1614218422000,8637,98.861045,'R','bf3')") + tdSql.execute("alter table db2.stb0 add column col4 int") + tdSql.execute("alter table db2.stb0 drop column col2") + tdSql.execute("alter table db2.stb0 add tag t3 int") + tdSql.execute("alter table db2.stb0 drop tag t1") + tdSql.execute("create table if not exists stb2_0 (ts timestamp, col0 int, col1 float) ") + tdSql.execute("insert into stb2_0 values(1614218412000,8637,78.861045)") + tdSql.execute("alter table stb2_0 add column col2 binary(4)") + tdSql.execute("alter table stb2_0 drop column col1") + tdSql.execute("insert into stb2_0 values(1614218422000,8638,'R')") + + + # stop taosd and compact wal file + os.system("ps -ef |grep taosd |grep -v 'grep' |awk '{print $2}'|xargs kill -2") + sleep(10) + os.system("nohup taosd --compact-mnode-wal -c /etc/taos & ") + sleep(10) + os.system("nohup /usr/bin/taosd > /dev/null 2>&1 &") + sleep(4) + tdSql.execute("reset query cache") + query_pid2 = int(subprocess.getstatusoutput('ps aux|grep taosd |grep -v "grep"|awk \'{print $2}\'')[1]) + print(query_pid2) + assert os.path.exists(walFilePath) , "%s is not generated " % walFilePath + + # new taos connecting to server + conn2 = taos.connect(host=self.host, user=self.user, password=self.password, config=self.config ) + print(conn2) + cur2 = conn2.cursor() + tdSql.init(cur2, True) + + # use new wal file to start up tasod + tdSql.query("show databases") + for i in range(tdSql.queryRows): + if tdSql.queryResult[i][0]=="db2": + assert tdSql.queryResult[i][4]==2 , "replica is wrong" + tdSql.execute("use db2") + tdSql.query("select count (tbname) from stb0") + tdSql.checkData(0, 0, 1) + tdSql.query("select count (tbname) from stb1") + tdSql.checkRows(0) + tdSql.query("select count(*) from stb0_0") + tdSql.checkData(0, 0, 2) + tdSql.query("select count(*) from stb0") + tdSql.checkData(0, 0, 2) + tdSql.query("select count(*) from stb2_0") + tdSql.checkData(0, 0, 2) + tdSql.query("select * from stb2_0") + tdSql.checkData(1, 2, 'R') + + # delete useless file + testcaseFilename = os.path.split(__file__)[-1] + os.system("rm -rf ./insert_res.txt") + os.system("rm -rf wal/%s.sql" % testcaseFilename ) + +clients = TwoClients() +clients.initConnection() +# clients.getBuildPath() +clients.run() \ No newline at end of file From b885f2c1338db275152eab385d38f6b91f7a9885 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 5 Jun 2021 16:39:07 +0800 Subject: [PATCH 33/57] Bump httpclient in /tests/comparisonTest/opentsdb/opentsdbtest (#6371) Bumps httpclient from 4.5.4 to 4.5.13. --- updated-dependencies: - dependency-name: org.apache.httpcomponents:httpclient dependency-type: direct:production ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- tests/comparisonTest/opentsdb/opentsdbtest/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/comparisonTest/opentsdb/opentsdbtest/pom.xml b/tests/comparisonTest/opentsdb/opentsdbtest/pom.xml index 0af7491128..29c2a90f10 100644 --- a/tests/comparisonTest/opentsdb/opentsdbtest/pom.xml +++ b/tests/comparisonTest/opentsdb/opentsdbtest/pom.xml @@ -162,7 +162,7 @@ org.apache.httpcomponents httpclient - 4.5.4 + 4.5.13 From 28b0cdc3be45624535026f6dc5e633d55083fc12 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 5 Jun 2021 16:39:44 +0800 Subject: [PATCH 34/57] Bump httpclient from 4.5.8 to 4.5.13 in /src/connector/jdbc (#6372) Bumps httpclient from 4.5.8 to 4.5.13. --- updated-dependencies: - dependency-name: org.apache.httpcomponents:httpclient dependency-type: direct:production ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- src/connector/jdbc/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100755 => 100644 src/connector/jdbc/pom.xml diff --git a/src/connector/jdbc/pom.xml b/src/connector/jdbc/pom.xml old mode 100755 new mode 100644 index ef353d1d19..e350e9738e --- a/src/connector/jdbc/pom.xml +++ b/src/connector/jdbc/pom.xml @@ -47,7 +47,7 @@ org.apache.httpcomponents httpclient - 4.5.8 + 4.5.13 com.alibaba From 718bf8d9eb4758b7ddf1d06d195bdc3e8bf43160 Mon Sep 17 00:00:00 2001 From: dapan1121 <89396746@qq.com> Date: Sat, 5 Jun 2021 17:34:26 +0800 Subject: [PATCH 35/57] support error msg --- src/client/src/tscPrepare.c | 158 +++++----- src/inc/taos.h | 1 + tests/script/api/batchprepare.c | 496 +++++++++++++++++++++++++++++++- 3 files changed, 580 insertions(+), 75 deletions(-) diff --git a/src/client/src/tscPrepare.c b/src/client/src/tscPrepare.c index f79ff671c6..3f12bc811b 100644 --- a/src/client/src/tscPrepare.c +++ b/src/client/src/tscPrepare.c @@ -78,6 +78,16 @@ typedef struct STscStmt { SNormalStmt normal; } STscStmt; +#define STMT_RET(c) do { \ + int32_t _code = c; \ + if (pStmt && pStmt->pSql) { pStmt->pSql->res.code = _code; } else {terrno = _code;} \ + return _code; \ +} while (0) + +static int32_t invalidOperationMsg(char* dstBuffer, const char* errMsg) { + return tscInvalidOperationMsg(dstBuffer, errMsg, NULL); +} + static int normalStmtAddPart(SNormalStmt* stmt, bool isParam, char* str, uint32_t len) { uint16_t size = stmt->numParts + 1; if (size > stmt->sizeParts) { @@ -1401,13 +1411,15 @@ int stmtGenInsertStatement(SSqlObj* pSql, STscStmt* pStmt, const char* name, TAO TAOS_STMT* taos_stmt_init(TAOS* taos) { STscObj* pObj = (STscObj*)taos; + STscStmt* pStmt = NULL; + if (pObj == NULL || pObj->signature != pObj) { terrno = TSDB_CODE_TSC_DISCONNECTED; tscError("connection disconnected"); return NULL; } - STscStmt* pStmt = calloc(1, sizeof(STscStmt)); + pStmt = calloc(1, sizeof(STscStmt)); if (pStmt == NULL) { terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; tscError("failed to allocate memory for statement"); @@ -1425,8 +1437,11 @@ TAOS_STMT* taos_stmt_init(TAOS* taos) { } if (TSDB_CODE_SUCCESS != tscAllocPayload(&pSql->cmd, TSDB_DEFAULT_PAYLOAD_SIZE)) { - tscError("%p failed to malloc payload buffer", pSql); - return TSDB_CODE_TSC_OUT_OF_MEMORY; + free(pSql); + free(pStmt); + terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; + tscError("failed to malloc payload buffer"); + return NULL; } tsem_init(&pSql->rspSem, 0, 0); @@ -1444,13 +1459,12 @@ int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) { STscStmt* pStmt = (STscStmt*)stmt; if (stmt == NULL || pStmt->taos == NULL || pStmt->pSql == NULL) { - terrno = TSDB_CODE_TSC_DISCONNECTED; - return TSDB_CODE_TSC_DISCONNECTED; + STMT_RET(TSDB_CODE_TSC_DISCONNECTED); } if (pStmt->last != STMT_INIT) { tscError("prepare status error, last:%d", pStmt->last); - return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), "prepare status error"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "prepare status error")); } pStmt->last = STMT_PREPARE; @@ -1470,8 +1484,7 @@ int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) { if (pSql->sqlstr == NULL) { tscError("%p failed to malloc sql string buffer", pSql); - free(pCmd->payload); - return TSDB_CODE_TSC_OUT_OF_MEMORY; + STMT_RET(TSDB_CODE_TSC_OUT_OF_MEMORY); } pRes->qId = 0; @@ -1490,11 +1503,11 @@ int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) { int32_t ret = stmtParseInsertTbTags(pSql, pStmt); if (ret != TSDB_CODE_SUCCESS) { - return ret; + STMT_RET(ret); } if (pStmt->multiTbInsert) { - return TSDB_CODE_SUCCESS; + STMT_RET(TSDB_CODE_SUCCESS); } memset(&pStmt->mtb, 0, sizeof(pStmt->mtb)); @@ -1503,15 +1516,14 @@ int taos_stmt_prepare(TAOS_STMT* stmt, const char* sql, unsigned long length) { if (code == TSDB_CODE_TSC_ACTION_IN_PROGRESS) { // wait for the callback function to post the semaphore tsem_wait(&pSql->rspSem); - pSql->res.code = code; - return pSql->res.code; + STMT_RET(pSql->res.code); } - return code; + STMT_RET(code); } pStmt->isInsert = false; - return normalStmtPrepare(pStmt); + STMT_RET(normalStmtPrepare(pStmt)); } int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags) { @@ -1520,23 +1532,22 @@ int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags SSqlCmd* pCmd = &pSql->cmd; if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) { - terrno = TSDB_CODE_TSC_DISCONNECTED; - return TSDB_CODE_TSC_DISCONNECTED; + STMT_RET(TSDB_CODE_TSC_DISCONNECTED); } if (name == NULL) { tscError("0x%"PRIx64" name is NULL", pSql->self); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "name is NULL"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "name is NULL")); } if (pStmt->multiTbInsert == false || !tscIsInsertData(pSql->sqlstr)) { tscError("0x%"PRIx64" not multiple table insert", pSql->self); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "not multiple table insert"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "not multiple table insert")); } if (pStmt->last == STMT_INIT || pStmt->last == STMT_BIND || pStmt->last == STMT_BIND_COL) { tscError("0x%"PRIx64" set_tbname_tags status error, last:%d", pSql->self, pStmt->last); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "set_tbname_tags status error"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "set_tbname_tags status error")); } pStmt->last = STMT_SETTBNAME; @@ -1548,7 +1559,7 @@ int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags STableDataBlocks** t1 = (STableDataBlocks**)taosHashGet(pStmt->mtb.pTableBlockHashList, (const char*)&pStmt->mtb.currentUid, sizeof(pStmt->mtb.currentUid)); if (t1 == NULL) { tscError("0x%"PRIx64" no table data block in hash list, uid:%" PRId64 , pSql->self, pStmt->mtb.currentUid); - return TSDB_CODE_TSC_APP_ERROR; + STMT_RET(TSDB_CODE_TSC_APP_ERROR); } SSubmitBlk* pBlk = (SSubmitBlk*) (*t1)->pData; @@ -1557,7 +1568,7 @@ int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags taosHashPut(pCmd->insertParam.pTableBlockHashList, (void *)&pStmt->mtb.currentUid, sizeof(pStmt->mtb.currentUid), (void*)t1, POINTER_BYTES); tscDebug("0x%"PRIx64" table:%s is already prepared, uid:%" PRIu64, pSql->self, name, pStmt->mtb.currentUid); - return TSDB_CODE_SUCCESS; + STMT_RET(TSDB_CODE_SUCCESS); } if (pStmt->mtb.tagSet) { @@ -1565,12 +1576,12 @@ int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags } else { if (tags == NULL) { tscError("No tags set"); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "no tags set"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "no tags set")); } int32_t ret = stmtGenInsertStatement(pSql, pStmt, name, tags); if (ret != TSDB_CODE_SUCCESS) { - return ret; + STMT_RET(ret); } } @@ -1604,7 +1615,7 @@ int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags code = tscGetDataBlockFromList(pCmd->insertParam.pTableBlockHashList, pTableMeta->id.uid, TSDB_PAYLOAD_SIZE, sizeof(SSubmitBlk), pTableMeta->tableInfo.rowSize, &pTableMetaInfo->name, pTableMeta, &pBlock, NULL); if (code != TSDB_CODE_SUCCESS) { - return code; + STMT_RET(code); } SSubmitBlk* blk = (SSubmitBlk*)pBlock->pData; @@ -1619,7 +1630,7 @@ int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags tscDebug("0x%"PRIx64" table:%s is prepared, uid:%" PRIx64, pSql->self, name, pStmt->mtb.currentUid); } - return code; + STMT_RET(code); } @@ -1652,35 +1663,34 @@ int taos_stmt_close(TAOS_STMT* stmt) { } taos_free_result(pStmt->pSql); - free(pStmt); - return TSDB_CODE_SUCCESS; + tfree(pStmt); + STMT_RET(TSDB_CODE_SUCCESS); } int taos_stmt_bind_param(TAOS_STMT* stmt, TAOS_BIND* bind) { STscStmt* pStmt = (STscStmt*)stmt; if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) { - terrno = TSDB_CODE_TSC_DISCONNECTED; - return TSDB_CODE_TSC_DISCONNECTED; + STMT_RET(TSDB_CODE_TSC_DISCONNECTED); } if (pStmt->isInsert) { if (pStmt->multiTbInsert) { if (pStmt->last != STMT_SETTBNAME && pStmt->last != STMT_ADD_BATCH) { tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error")); } } else { if (pStmt->last != STMT_PREPARE && pStmt->last != STMT_ADD_BATCH && pStmt->last != STMT_EXECUTE) { tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error")); } } pStmt->last = STMT_BIND; - return insertStmtBindParam(pStmt, bind); + STMT_RET(insertStmtBindParam(pStmt, bind)); } else { - return normalStmtBindParam(pStmt, bind); + STMT_RET(normalStmtBindParam(pStmt, bind)); } } @@ -1689,69 +1699,67 @@ int taos_stmt_bind_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind) { STscStmt* pStmt = (STscStmt*)stmt; if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) { - terrno = TSDB_CODE_TSC_DISCONNECTED; - return TSDB_CODE_TSC_DISCONNECTED; + STMT_RET(TSDB_CODE_TSC_DISCONNECTED); } if (bind == NULL || bind->num <= 0 || bind->num > INT16_MAX) { tscError("0x%"PRIx64" invalid parameter", pStmt->pSql->self); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "invalid bind param"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "invalid bind param")); } if (!pStmt->isInsert) { tscError("0x%"PRIx64" not or invalid batch insert", pStmt->pSql->self); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "not or invalid batch insert"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "not or invalid batch insert")); } if (pStmt->multiTbInsert) { if (pStmt->last != STMT_SETTBNAME && pStmt->last != STMT_ADD_BATCH) { tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error")); } } else { if (pStmt->last != STMT_PREPARE && pStmt->last != STMT_ADD_BATCH && pStmt->last != STMT_EXECUTE) { tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error")); } } pStmt->last = STMT_BIND; - return insertStmtBindParamBatch(pStmt, bind, -1); + STMT_RET(insertStmtBindParamBatch(pStmt, bind, -1)); } int taos_stmt_bind_single_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int colIdx) { STscStmt* pStmt = (STscStmt*)stmt; if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) { - terrno = TSDB_CODE_TSC_DISCONNECTED; - return TSDB_CODE_TSC_DISCONNECTED; + STMT_RET(TSDB_CODE_TSC_DISCONNECTED); } if (bind == NULL || bind->num <= 0 || bind->num > INT16_MAX) { tscError("0x%"PRIx64" invalid parameter", pStmt->pSql->self); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "invalid bind param"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "invalid bind param")); } if (!pStmt->isInsert) { tscError("0x%"PRIx64" not or invalid batch insert", pStmt->pSql->self); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "not or invalid batch insert"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "not or invalid batch insert")); } if (pStmt->multiTbInsert) { if (pStmt->last != STMT_SETTBNAME && pStmt->last != STMT_ADD_BATCH && pStmt->last != STMT_BIND_COL) { tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error")); } } else { if (pStmt->last != STMT_PREPARE && pStmt->last != STMT_ADD_BATCH && pStmt->last != STMT_BIND_COL && pStmt->last != STMT_EXECUTE) { tscError("0x%"PRIx64" bind param status error, last:%d", pStmt->pSql->self, pStmt->last); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "bind param status error")); } } pStmt->last = STMT_BIND_COL; - return insertStmtBindParamBatch(pStmt, bind, colIdx); + STMT_RET(insertStmtBindParamBatch(pStmt, bind, colIdx)); } @@ -1759,44 +1767,42 @@ int taos_stmt_bind_single_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, in int taos_stmt_add_batch(TAOS_STMT* stmt) { STscStmt* pStmt = (STscStmt*)stmt; if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) { - terrno = TSDB_CODE_TSC_DISCONNECTED; - return TSDB_CODE_TSC_DISCONNECTED; + STMT_RET(TSDB_CODE_TSC_DISCONNECTED); } if (pStmt->isInsert) { if (pStmt->last != STMT_BIND && pStmt->last != STMT_BIND_COL) { tscError("0x%"PRIx64" add batch status error, last:%d", pStmt->pSql->self, pStmt->last); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "add batch status error"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "add batch status error")); } pStmt->last = STMT_ADD_BATCH; - return insertStmtAddBatch(pStmt); + STMT_RET(insertStmtAddBatch(pStmt)); } - return TSDB_CODE_COM_OPS_NOT_SUPPORT; + STMT_RET(TSDB_CODE_COM_OPS_NOT_SUPPORT); } int taos_stmt_reset(TAOS_STMT* stmt) { STscStmt* pStmt = (STscStmt*)stmt; if (pStmt->isInsert) { - return insertStmtReset(pStmt); + STMT_RET(insertStmtReset(pStmt)); } - return TSDB_CODE_SUCCESS; + STMT_RET(TSDB_CODE_SUCCESS); } int taos_stmt_execute(TAOS_STMT* stmt) { int ret = 0; STscStmt* pStmt = (STscStmt*)stmt; if (stmt == NULL || pStmt->pSql == NULL || pStmt->taos == NULL) { - terrno = TSDB_CODE_TSC_DISCONNECTED; - return TSDB_CODE_TSC_DISCONNECTED; + STMT_RET(TSDB_CODE_TSC_DISCONNECTED); } if (pStmt->isInsert) { if (pStmt->last != STMT_ADD_BATCH) { tscError("0x%"PRIx64" exec status error, last:%d", pStmt->pSql->self, pStmt->last); - return invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "exec status error"); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "exec status error")); } pStmt->last = STMT_EXECUTE; @@ -1822,7 +1828,7 @@ int taos_stmt_execute(TAOS_STMT* stmt) { } } - return ret; + STMT_RET(ret); } TAOS_RES *taos_stmt_use_result(TAOS_STMT* stmt) { @@ -1846,32 +1852,30 @@ int taos_stmt_is_insert(TAOS_STMT *stmt, int *insert) { STscStmt* pStmt = (STscStmt*)stmt; if (stmt == NULL || pStmt->taos == NULL || pStmt->pSql == NULL) { - terrno = TSDB_CODE_TSC_DISCONNECTED; - return TSDB_CODE_TSC_DISCONNECTED; + STMT_RET(TSDB_CODE_TSC_DISCONNECTED); } if (insert) *insert = pStmt->isInsert; - return TSDB_CODE_SUCCESS; + STMT_RET(TSDB_CODE_SUCCESS); } int taos_stmt_num_params(TAOS_STMT *stmt, int *nums) { STscStmt* pStmt = (STscStmt*)stmt; if (stmt == NULL || pStmt->taos == NULL || pStmt->pSql == NULL) { - terrno = TSDB_CODE_TSC_DISCONNECTED; - return TSDB_CODE_TSC_DISCONNECTED; + STMT_RET(TSDB_CODE_TSC_DISCONNECTED); } if (pStmt->isInsert) { SSqlObj* pSql = pStmt->pSql; SSqlCmd *pCmd = &pSql->cmd; *nums = pCmd->insertParam.numOfParams; - return TSDB_CODE_SUCCESS; + STMT_RET(TSDB_CODE_SUCCESS); } else { SNormalStmt* normal = &pStmt->normal; *nums = normal->numParams; - return TSDB_CODE_SUCCESS; + STMT_RET(TSDB_CODE_SUCCESS); } } @@ -1879,8 +1883,7 @@ int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) { STscStmt* pStmt = (STscStmt*)stmt; if (stmt == NULL || pStmt->taos == NULL || pStmt->pSql == NULL) { - terrno = TSDB_CODE_TSC_DISCONNECTED; - return TSDB_CODE_TSC_DISCONNECTED; + STMT_RET(TSDB_CODE_TSC_DISCONNECTED); } if (pStmt->isInsert) { @@ -1897,24 +1900,37 @@ int taos_stmt_get_param(TAOS_STMT *stmt, int idx, int *type, int *bytes) { tscGetDataBlockFromList(pCmd->insertParam.pTableBlockHashList, pTableMeta->id.uid, TSDB_PAYLOAD_SIZE, sizeof(SSubmitBlk), pTableMeta->tableInfo.rowSize, &pTableMetaInfo->name, pTableMeta, &pBlock, NULL); if (ret != 0) { - // todo handle error + STMT_RET(ret); } if (idx<0 || idx>=pBlock->numOfParams) { tscError("0x%"PRIx64" param %d: out of range", pStmt->pSql->self, idx); - abort(); + STMT_RET(invalidOperationMsg(tscGetErrorMsgPayload(&pStmt->pSql->cmd), "idx out of range")); } SParamInfo* param = &pBlock->params[idx]; if (type) *type = param->type; if (bytes) *bytes = param->bytes; - return TSDB_CODE_SUCCESS; + STMT_RET(TSDB_CODE_SUCCESS); } else { - return TSDB_CODE_TSC_APP_ERROR; + STMT_RET(TSDB_CODE_COM_OPS_NOT_SUPPORT); } } + +char *taos_stmt_errstr(TAOS_STMT *stmt) { + STscStmt* pStmt = (STscStmt*)stmt; + + if (stmt == NULL) { + return (char*) tstrerror(terrno); + } + + return taos_errstr(pStmt->pSql); +} + + + const char *taos_data_type(int type) { switch (type) { case TSDB_DATA_TYPE_NULL: return "TSDB_DATA_TYPE_NULL"; diff --git a/src/inc/taos.h b/src/inc/taos.h index d27828fc36..9f72945ef0 100644 --- a/src/inc/taos.h +++ b/src/inc/taos.h @@ -124,6 +124,7 @@ int taos_stmt_add_batch(TAOS_STMT *stmt); int taos_stmt_execute(TAOS_STMT *stmt); TAOS_RES * taos_stmt_use_result(TAOS_STMT *stmt); int taos_stmt_close(TAOS_STMT *stmt); +char * taos_stmt_errstr(TAOS_STMT *stmt); DLL_EXPORT TAOS_RES *taos_query(TAOS *taos, const char *sql); DLL_EXPORT TAOS_ROW taos_fetch_row(TAOS_RES *res); diff --git a/tests/script/api/batchprepare.c b/tests/script/api/batchprepare.c index af0611ce73..f25becd39e 100644 --- a/tests/script/api/batchprepare.c +++ b/tests/script/api/batchprepare.c @@ -1606,7 +1606,7 @@ int stmt_funcb_autoctb_e1(TAOS_STMT *stmt) { char *sql = "insert into ? using stb1 (id1,id2,id3,id4,id5,id6,id7,id8,id9) tags(1,?,2,?,4,?,6.0,?,'b') values(?,?,?,?,?,?,?,?,?,?)"; int code = taos_stmt_prepare(stmt, sql, 0); if (code != 0){ - printf("failed to execute taos_stmt_prepare. code:0x%x\n", code); + printf("failed to execute taos_stmt_prepare. error:%s\n", taos_stmt_errstr(stmt)); return -1; } @@ -1830,7 +1830,7 @@ int stmt_funcb_autoctb_e2(TAOS_STMT *stmt) { sprintf(buf, "m%d", zz); code = taos_stmt_set_tbname_tags(stmt, buf, NULL); if (code != 0){ - printf("failed to execute taos_stmt_set_tbname_tags. code:0x%x\n", code); + printf("failed to execute taos_stmt_set_tbname_tags. code:%s\n", taos_stmt_errstr(stmt)); return -1; } @@ -2037,7 +2037,7 @@ int stmt_funcb_autoctb_e3(TAOS_STMT *stmt) { char *sql = "insert into ? using stb1 (id1,id2,id3,id4,id5,id6,id7,id8,id9) tags(?,?,?,?,?,?,?,?,?) values(?,?,?,?,?,?,?,?,?,?)"; int code = taos_stmt_prepare(stmt, sql, 0); if (code != 0){ - printf("failed to execute taos_stmt_prepare. code:0x%x\n", code); + printf("failed to execute taos_stmt_prepare. code:%s\n", taos_stmt_errstr(stmt)); return -1; //exit(1); } @@ -2078,6 +2078,458 @@ int stmt_funcb_autoctb_e3(TAOS_STMT *stmt) { + +//1 tables 10 records +int stmt_funcb_autoctb_e4(TAOS_STMT *stmt) { + struct { + int64_t *ts; + int8_t b[10]; + int8_t v1[10]; + int16_t v2[10]; + int32_t v4[10]; + int64_t v8[10]; + float f4[10]; + double f8[10]; + char bin[10][40]; + } v = {0}; + + v.ts = malloc(sizeof(int64_t) * 1 * 10); + + int *lb = malloc(10 * sizeof(int)); + + TAOS_BIND *tags = calloc(1, sizeof(TAOS_BIND) * 9 * 1); + TAOS_MULTI_BIND *params = calloc(1, sizeof(TAOS_MULTI_BIND) * 1*10); + +// int one_null = 1; + int one_not_null = 0; + + char* is_null = malloc(sizeof(char) * 10); + char* no_null = malloc(sizeof(char) * 10); + + for (int i = 0; i < 10; ++i) { + lb[i] = 40; + no_null[i] = 0; + is_null[i] = (i % 10 == 2) ? 1 : 0; + v.b[i] = (int8_t)(i % 2); + v.v1[i] = (int8_t)((i+1) % 2); + v.v2[i] = (int16_t)i; + v.v4[i] = (int32_t)(i+1); + v.v8[i] = (int64_t)(i+2); + v.f4[i] = (float)(i+3); + v.f8[i] = (double)(i+4); + memset(v.bin[i], '0'+i%10, 40); + } + + for (int i = 0; i < 10; i+=10) { + params[i+0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; + params[i+0].buffer_length = sizeof(int64_t); + params[i+0].buffer = &v.ts[10*i/10]; + params[i+0].length = NULL; + params[i+0].is_null = no_null; + params[i+0].num = 10; + + params[i+1].buffer_type = TSDB_DATA_TYPE_BOOL; + params[i+1].buffer_length = sizeof(int8_t); + params[i+1].buffer = v.b; + params[i+1].length = NULL; + params[i+1].is_null = is_null; + params[i+1].num = 10; + + params[i+2].buffer_type = TSDB_DATA_TYPE_TINYINT; + params[i+2].buffer_length = sizeof(int8_t); + params[i+2].buffer = v.v1; + params[i+2].length = NULL; + params[i+2].is_null = is_null; + params[i+2].num = 10; + + params[i+3].buffer_type = TSDB_DATA_TYPE_SMALLINT; + params[i+3].buffer_length = sizeof(int16_t); + params[i+3].buffer = v.v2; + params[i+3].length = NULL; + params[i+3].is_null = is_null; + params[i+3].num = 10; + + params[i+4].buffer_type = TSDB_DATA_TYPE_INT; + params[i+4].buffer_length = sizeof(int32_t); + params[i+4].buffer = v.v4; + params[i+4].length = NULL; + params[i+4].is_null = is_null; + params[i+4].num = 10; + + params[i+5].buffer_type = TSDB_DATA_TYPE_BIGINT; + params[i+5].buffer_length = sizeof(int64_t); + params[i+5].buffer = v.v8; + params[i+5].length = NULL; + params[i+5].is_null = is_null; + params[i+5].num = 10; + + params[i+6].buffer_type = TSDB_DATA_TYPE_FLOAT; + params[i+6].buffer_length = sizeof(float); + params[i+6].buffer = v.f4; + params[i+6].length = NULL; + params[i+6].is_null = is_null; + params[i+6].num = 10; + + params[i+7].buffer_type = TSDB_DATA_TYPE_DOUBLE; + params[i+7].buffer_length = sizeof(double); + params[i+7].buffer = v.f8; + params[i+7].length = NULL; + params[i+7].is_null = is_null; + params[i+7].num = 10; + + params[i+8].buffer_type = TSDB_DATA_TYPE_BINARY; + params[i+8].buffer_length = 40; + params[i+8].buffer = v.bin; + params[i+8].length = lb; + params[i+8].is_null = is_null; + params[i+8].num = 10; + + params[i+9].buffer_type = TSDB_DATA_TYPE_BINARY; + params[i+9].buffer_length = 40; + params[i+9].buffer = v.bin; + params[i+9].length = lb; + params[i+9].is_null = is_null; + params[i+9].num = 10; + + } + + int64_t tts = 1591060628000; + for (int i = 0; i < 10; ++i) { + v.ts[i] = tts + i; + } + + + for (int i = 0; i < 1; ++i) { + tags[i+0].buffer_type = TSDB_DATA_TYPE_INT; + tags[i+0].buffer = v.v4; + tags[i+0].is_null = &one_not_null; + tags[i+0].length = NULL; + + tags[i+1].buffer_type = TSDB_DATA_TYPE_BOOL; + tags[i+1].buffer = v.b; + tags[i+1].is_null = &one_not_null; + tags[i+1].length = NULL; + + tags[i+2].buffer_type = TSDB_DATA_TYPE_TINYINT; + tags[i+2].buffer = v.v1; + tags[i+2].is_null = &one_not_null; + tags[i+2].length = NULL; + + tags[i+3].buffer_type = TSDB_DATA_TYPE_SMALLINT; + tags[i+3].buffer = v.v2; + tags[i+3].is_null = &one_not_null; + tags[i+3].length = NULL; + + tags[i+4].buffer_type = TSDB_DATA_TYPE_BIGINT; + tags[i+4].buffer = v.v8; + tags[i+4].is_null = &one_not_null; + tags[i+4].length = NULL; + + tags[i+5].buffer_type = TSDB_DATA_TYPE_FLOAT; + tags[i+5].buffer = v.f4; + tags[i+5].is_null = &one_not_null; + tags[i+5].length = NULL; + + tags[i+6].buffer_type = TSDB_DATA_TYPE_DOUBLE; + tags[i+6].buffer = v.f8; + tags[i+6].is_null = &one_not_null; + tags[i+6].length = NULL; + + tags[i+7].buffer_type = TSDB_DATA_TYPE_BINARY; + tags[i+7].buffer = v.bin; + tags[i+7].is_null = &one_not_null; + tags[i+7].length = (uintptr_t *)lb; + + tags[i+8].buffer_type = TSDB_DATA_TYPE_NCHAR; + tags[i+8].buffer = v.bin; + tags[i+8].is_null = &one_not_null; + tags[i+8].length = (uintptr_t *)lb; + } + + + unsigned long long starttime = getCurrentTime(); + + char *sql = "insert into ? using stb1 tags(?,?,?,?,?,?,?,?,?) values(?,?,?,?,?,?,?,?,?,?)"; + int code = taos_stmt_prepare(stmt, sql, 0); + if (code != 0){ + printf("failed to execute taos_stmt_prepare. code:%s\n", taos_stmt_errstr(stmt)); + exit(1); + } + + int id = 0; + for (int zz = 0; zz < 1; zz++) { + char buf[32]; + sprintf(buf, "m%d", zz); + code = taos_stmt_set_tbname_tags(stmt, buf, tags); + if (code != 0){ + printf("failed to execute taos_stmt_set_tbname_tags. error:%s\n", taos_stmt_errstr(stmt)); + exit(1); + } + + code = taos_stmt_bind_param_batch(stmt, params + id * 10); + if (code != 0) { + printf("failed to execute taos_stmt_bind_param_batch. error:%s\n", taos_stmt_errstr(stmt)); + exit(1); + } + + code = taos_stmt_bind_param_batch(stmt, params + id * 10); + if (code != 0) { + printf("failed to execute taos_stmt_bind_param_batch. error:%s\n", taos_stmt_errstr(stmt)); + return -1; + } + + taos_stmt_add_batch(stmt); + } + + if (taos_stmt_execute(stmt) != 0) { + printf("failed to execute insert statement.\n"); + exit(1); + } + + ++id; + + unsigned long long endtime = getCurrentTime(); + printf("insert total %d records, used %u seconds, avg:%u useconds\n", 10, (endtime-starttime)/1000000UL, (endtime-starttime)/(10)); + + free(v.ts); + free(lb); + free(params); + free(is_null); + free(no_null); + free(tags); + + return 0; +} + + + + + + +//1 tables 10 records +int stmt_funcb_autoctb_e5(TAOS_STMT *stmt) { + struct { + int64_t *ts; + int8_t b[10]; + int8_t v1[10]; + int16_t v2[10]; + int32_t v4[10]; + int64_t v8[10]; + float f4[10]; + double f8[10]; + char bin[10][40]; + } v = {0}; + + v.ts = malloc(sizeof(int64_t) * 1 * 10); + + int *lb = malloc(10 * sizeof(int)); + + TAOS_BIND *tags = calloc(1, sizeof(TAOS_BIND) * 9 * 1); + TAOS_MULTI_BIND *params = calloc(1, sizeof(TAOS_MULTI_BIND) * 1*10); + +// int one_null = 1; + int one_not_null = 0; + + char* is_null = malloc(sizeof(char) * 10); + char* no_null = malloc(sizeof(char) * 10); + + for (int i = 0; i < 10; ++i) { + lb[i] = 40; + no_null[i] = 0; + is_null[i] = (i % 10 == 2) ? 1 : 0; + v.b[i] = (int8_t)(i % 2); + v.v1[i] = (int8_t)((i+1) % 2); + v.v2[i] = (int16_t)i; + v.v4[i] = (int32_t)(i+1); + v.v8[i] = (int64_t)(i+2); + v.f4[i] = (float)(i+3); + v.f8[i] = (double)(i+4); + memset(v.bin[i], '0'+i%10, 40); + } + + for (int i = 0; i < 10; i+=10) { + params[i+0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; + params[i+0].buffer_length = sizeof(int64_t); + params[i+0].buffer = &v.ts[10*i/10]; + params[i+0].length = NULL; + params[i+0].is_null = no_null; + params[i+0].num = 10; + + params[i+1].buffer_type = TSDB_DATA_TYPE_BOOL; + params[i+1].buffer_length = sizeof(int8_t); + params[i+1].buffer = v.b; + params[i+1].length = NULL; + params[i+1].is_null = is_null; + params[i+1].num = 10; + + params[i+2].buffer_type = TSDB_DATA_TYPE_TINYINT; + params[i+2].buffer_length = sizeof(int8_t); + params[i+2].buffer = v.v1; + params[i+2].length = NULL; + params[i+2].is_null = is_null; + params[i+2].num = 10; + + params[i+3].buffer_type = TSDB_DATA_TYPE_SMALLINT; + params[i+3].buffer_length = sizeof(int16_t); + params[i+3].buffer = v.v2; + params[i+3].length = NULL; + params[i+3].is_null = is_null; + params[i+3].num = 10; + + params[i+4].buffer_type = TSDB_DATA_TYPE_INT; + params[i+4].buffer_length = sizeof(int32_t); + params[i+4].buffer = v.v4; + params[i+4].length = NULL; + params[i+4].is_null = is_null; + params[i+4].num = 10; + + params[i+5].buffer_type = TSDB_DATA_TYPE_BIGINT; + params[i+5].buffer_length = sizeof(int64_t); + params[i+5].buffer = v.v8; + params[i+5].length = NULL; + params[i+5].is_null = is_null; + params[i+5].num = 10; + + params[i+6].buffer_type = TSDB_DATA_TYPE_FLOAT; + params[i+6].buffer_length = sizeof(float); + params[i+6].buffer = v.f4; + params[i+6].length = NULL; + params[i+6].is_null = is_null; + params[i+6].num = 10; + + params[i+7].buffer_type = TSDB_DATA_TYPE_DOUBLE; + params[i+7].buffer_length = sizeof(double); + params[i+7].buffer = v.f8; + params[i+7].length = NULL; + params[i+7].is_null = is_null; + params[i+7].num = 10; + + params[i+8].buffer_type = TSDB_DATA_TYPE_BINARY; + params[i+8].buffer_length = 40; + params[i+8].buffer = v.bin; + params[i+8].length = lb; + params[i+8].is_null = is_null; + params[i+8].num = 10; + + params[i+9].buffer_type = TSDB_DATA_TYPE_BINARY; + params[i+9].buffer_length = 40; + params[i+9].buffer = v.bin; + params[i+9].length = lb; + params[i+9].is_null = is_null; + params[i+9].num = 10; + + } + + int64_t tts = 1591060628000; + for (int i = 0; i < 10; ++i) { + v.ts[i] = tts + i; + } + + + for (int i = 0; i < 1; ++i) { + tags[i+0].buffer_type = TSDB_DATA_TYPE_INT; + tags[i+0].buffer = v.v4; + tags[i+0].is_null = &one_not_null; + tags[i+0].length = NULL; + + tags[i+1].buffer_type = TSDB_DATA_TYPE_BOOL; + tags[i+1].buffer = v.b; + tags[i+1].is_null = &one_not_null; + tags[i+1].length = NULL; + + tags[i+2].buffer_type = TSDB_DATA_TYPE_TINYINT; + tags[i+2].buffer = v.v1; + tags[i+2].is_null = &one_not_null; + tags[i+2].length = NULL; + + tags[i+3].buffer_type = TSDB_DATA_TYPE_SMALLINT; + tags[i+3].buffer = v.v2; + tags[i+3].is_null = &one_not_null; + tags[i+3].length = NULL; + + tags[i+4].buffer_type = TSDB_DATA_TYPE_BIGINT; + tags[i+4].buffer = v.v8; + tags[i+4].is_null = &one_not_null; + tags[i+4].length = NULL; + + tags[i+5].buffer_type = TSDB_DATA_TYPE_FLOAT; + tags[i+5].buffer = v.f4; + tags[i+5].is_null = &one_not_null; + tags[i+5].length = NULL; + + tags[i+6].buffer_type = TSDB_DATA_TYPE_DOUBLE; + tags[i+6].buffer = v.f8; + tags[i+6].is_null = &one_not_null; + tags[i+6].length = NULL; + + tags[i+7].buffer_type = TSDB_DATA_TYPE_BINARY; + tags[i+7].buffer = v.bin; + tags[i+7].is_null = &one_not_null; + tags[i+7].length = (uintptr_t *)lb; + + tags[i+8].buffer_type = TSDB_DATA_TYPE_NCHAR; + tags[i+8].buffer = v.bin; + tags[i+8].is_null = &one_not_null; + tags[i+8].length = (uintptr_t *)lb; + } + + + unsigned long long starttime = getCurrentTime(); + + char *sql = "insert into ? using stb1 tags(?,?,?,?,?,?,?,?,?) values(?,?,?,?,?,?,?,?,?,?)"; + int code = taos_stmt_prepare(NULL, sql, 0); + if (code != 0){ + printf("failed to execute taos_stmt_prepare. code:%s\n", taos_stmt_errstr(NULL)); + return -1; + } + + int id = 0; + for (int zz = 0; zz < 1; zz++) { + char buf[32]; + sprintf(buf, "m%d", zz); + code = taos_stmt_set_tbname_tags(stmt, buf, tags); + if (code != 0){ + printf("failed to execute taos_stmt_set_tbname_tags. error:%s\n", taos_stmt_errstr(stmt)); + exit(1); + } + + code = taos_stmt_bind_param_batch(stmt, params + id * 10); + if (code != 0) { + printf("failed to execute taos_stmt_bind_param_batch. error:%s\n", taos_stmt_errstr(stmt)); + exit(1); + } + + code = taos_stmt_bind_param_batch(stmt, params + id * 10); + if (code != 0) { + printf("failed to execute taos_stmt_bind_param_batch. error:%s\n", taos_stmt_errstr(stmt)); + return -1; + } + + taos_stmt_add_batch(stmt); + } + + if (taos_stmt_execute(stmt) != 0) { + printf("failed to execute insert statement.\n"); + exit(1); + } + + ++id; + + unsigned long long endtime = getCurrentTime(); + printf("insert total %d records, used %u seconds, avg:%u useconds\n", 10, (endtime-starttime)/1000000UL, (endtime-starttime)/(10)); + + free(v.ts); + free(lb); + free(params); + free(is_null); + free(no_null); + free(tags); + + return 0; +} + + + //300 tables 60 records int stmt_funcb1(TAOS_STMT *stmt) { struct { @@ -3593,6 +4045,9 @@ void check_result(TAOS *taos, char *tname, int printr, int expected) { char sql[255] = "SELECT * FROM "; TAOS_RES *result; + //FORCE NO PRINT + printr = 0; + strcat(sql, tname); result = taos_query(taos, sql); @@ -3896,7 +4351,6 @@ void* runcase(void *par) { (void)idx; - #if 1 prepare(taos, 1, 1); @@ -4118,6 +4572,7 @@ void* runcase(void *par) { #endif + #if 1 prepare(taos, 1, 0); @@ -4164,6 +4619,37 @@ void* runcase(void *par) { #endif +#if 1 + prepare(taos, 1, 0); + + stmt = taos_stmt_init(taos); + + printf("1t+10r+bm+autoctb+e4 start\n"); + stmt_funcb_autoctb_e4(stmt); + printf("1t+10r+bm+autoctb+e4 end\n"); + printf("check result start\n"); + //check_result(taos, "m0", 1, 0); + printf("check result end\n"); + taos_stmt_close(stmt); + +#endif + +#if 1 + prepare(taos, 1, 0); + + stmt = taos_stmt_init(taos); + + printf("1t+10r+bm+autoctb+e5 start\n"); + stmt_funcb_autoctb_e5(stmt); + printf("1t+10r+bm+autoctb+e5 end\n"); + printf("check result start\n"); + //check_result(taos, "m0", 1, 0); + printf("check result end\n"); + taos_stmt_close(stmt); + +#endif + + #if 1 prepare(taos, 1, 1); @@ -4371,6 +4857,8 @@ void* runcase(void *par) { #endif + printf("test end\n"); + return NULL; } From 5da833257343f7b85f4538847a0c386bacd5b5e9 Mon Sep 17 00:00:00 2001 From: Ping Xiao Date: Sat, 5 Jun 2021 17:58:06 +0800 Subject: [PATCH 36/57] add test case --- .../java/com/taosdata/jdbc/TSDBPreparedStatementTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java index 277ca447f5..95a279d223 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java @@ -388,6 +388,14 @@ public class TSDBPreparedStatementTest { Assert.assertEquals(numOfRows, rows); } + + @Test + public void createTwoSameDbTest() throws SQLException { + Statement stmt = conn.createStatement(); + + stmt.execute("create database dbtest"); + Assert.assertThrows(SQLException.class, () -> stmt.execute("create database dbtest")); + } @Test public void setBoolean() throws SQLException { From e6293954137a949adf223331061f3fe803bdfc5d Mon Sep 17 00:00:00 2001 From: tomchon Date: Sat, 5 Jun 2021 18:34:36 +0800 Subject: [PATCH 37/57] add compacting wal log testcase into fulltest.sh --- tests/pytest/fulltest.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/pytest/fulltest.sh b/tests/pytest/fulltest.sh index 8fed9aa81c..be920dc1ba 100755 --- a/tests/pytest/fulltest.sh +++ b/tests/pytest/fulltest.sh @@ -283,6 +283,7 @@ python3 ./test.py -f topic/topicQuery.py python3 ./test.py -f update/merge_commit_data-0.py # wal python3 ./test.py -f wal/addOldWalTest.py +python3 ./test.py -f wal/sdbComp.py # function python3 ./test.py -f functions/all_null_value.py From aeb72fbdeb45f505272d90eb43f3eb769661eed4 Mon Sep 17 00:00:00 2001 From: Elias Soong Date: Sat, 5 Jun 2021 18:38:57 +0800 Subject: [PATCH 38/57] [TD-4533] : "taos_stmt_bind_param" can be used in both INSERT & SELECT queries. --- documentation20/cn/08.connector/docs.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/documentation20/cn/08.connector/docs.md b/documentation20/cn/08.connector/docs.md index 60f8df95f8..b6125e3cfe 100644 --- a/documentation20/cn/08.connector/docs.md +++ b/documentation20/cn/08.connector/docs.md @@ -301,7 +301,7 @@ TDengine的异步API均采用非阻塞调用模式。应用程序可以用多线 2. 调用 `taos_stmt_prepare` 解析 INSERT 语句; 3. 如果 INSERT 语句中预留了表名但没有预留 TAGS,那么调用 `taos_stmt_set_tbname` 来设置表名; 4. 如果 INSERT 语句中既预留了表名又预留了 TAGS(例如 INSERT 语句采取的是自动建表的方式),那么调用 `taos_stmt_set_tbname_tags` 来设置表名和 TAGS 的值; -5. 调用 `taos_stmt_bind_param_batch` 以多列的方式设置 VALUES 的值; +5. 调用 `taos_stmt_bind_param_batch` 以多列的方式设置 VALUES 的值,或者调用 `taos_stmt_bind_param` 以单行的方式设置 VALUES 的值; 6. 调用 `taos_stmt_add_batch` 把当前绑定的参数加入批处理; 7. 可以重复第 3~6 步,为批处理加入更多的数据行; 8. 调用 `taos_stmt_execute` 执行已经准备好的批处理指令; @@ -338,17 +338,17 @@ typedef struct TAOS_BIND { - `int taos_stmt_set_tbname(TAOS_STMT* stmt, const char* name)` - (2.1.1.0 版本新增) + (2.1.1.0 版本新增,仅支持用于替换 INSERT 语句中的参数值) 当 SQL 语句中的表名使用了 `?` 占位时,可以使用此函数绑定一个具体的表名。 - `int taos_stmt_set_tbname_tags(TAOS_STMT* stmt, const char* name, TAOS_BIND* tags)` - (2.1.2.0 版本新增) + (2.1.2.0 版本新增,仅支持用于替换 INSERT 语句中的参数值) 当 SQL 语句中的表名和 TAGS 都使用了 `?` 占位时,可以使用此函数绑定具体的表名和具体的 TAGS 取值。最典型的使用场景是使用了自动建表功能的 INSERT 语句(目前版本不支持指定具体的 TAGS 列)。tags 参数中的列数量需要与 SQL 语句中要求的 TAGS 数量完全一致。 - `int taos_stmt_bind_param_batch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind)` - (2.1.1.0 版本新增) + (2.1.1.0 版本新增,仅支持用于替换 INSERT 语句中的参数值) 以多列的方式传递待绑定的数据,需要保证这里传递的数据列的顺序、列的数量与 SQL 语句中的 VALUES 参数完全一致。TAOS_MULTI_BIND 的具体定义如下: ```c From dd2298b9542ceb8af07fcf0883b9c48793a4e247 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Sat, 5 Jun 2021 23:07:14 +0800 Subject: [PATCH 39/57] [TD-4426] fix compile error --- tests/script/general/parser/where.sim | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/script/general/parser/where.sim b/tests/script/general/parser/where.sim index 157c41ce58..d98d2f0507 100644 --- a/tests/script/general/parser/where.sim +++ b/tests/script/general/parser/where.sim @@ -151,12 +151,12 @@ sql_error select last(*) from wh_mt1 where c5 in ('1') sql_error select last(*) from wh_mt1_tb1 where c5 in ('1') sql_error select last(*) from wh_mt1 where c6 in ('1') sql_error select last(*) from wh_mt1_tb1 where c6 in ('1') -sql_error select last(*) from wh_mt1 where c7 in ('binary') -sql_error select last(*) from wh_mt1_tb1 where c7 in ('binary') -sql_error select last(*) from wh_mt1 where c8 in ('nchar') -sql_error select last(*) from wh_mt1_tb1 where c9 in (true, false) -sql_error select last(*) from wh_mt1 where c10 in ('2019-01-01 00:00:00.000') -sql_error select last(*) from wh_mt1_tb1 where c10 in ('2019-01-01 00:00:00.000') +#sql_error select last(*) from wh_mt1 where c7 in ('binary') +#sql_error select last(*) from wh_mt1_tb1 where c7 in ('binary') +#sql_error select last(*) from wh_mt1 where c8 in ('nchar') +#sql_error select last(*) from wh_mt1_tb1 where c9 in (true, false) +#sql_error select last(*) from wh_mt1 where c10 in ('2019-01-01 00:00:00.000') +#sql_error select last(*) from wh_mt1_tb1 where c10 in ('2019-01-01 00:00:00.000') sql_error select last(*) from wh_mt1 where t1 in ('binary') sql_error select last(*) from wh_mt1 where t2 in (1) sql_error select last(*) from wh_mt1 where t3 in (1) @@ -359,4 +359,4 @@ if $rows != 0 then endi -system sh/exec.sh -n dnode1 -s stop -x SIGINT \ No newline at end of file +system sh/exec.sh -n dnode1 -s stop -x SIGINT From 717f9230dd2bf74c24dadd6e581086b64623f4e2 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Sat, 5 Jun 2021 23:19:28 +0800 Subject: [PATCH 40/57] [TD-4426] fix compile error --- src/client/src/tscSQLParser.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 9263cb1170..99535c5ab5 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -3489,7 +3489,7 @@ static int32_t extractColumnFilterInfo(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, SC if (pSchema->type == TSDB_DATA_TYPE_BOOL) { int32_t t = pExpr->tokenId; - if (t != TK_EQ && t != TK_NE && t != TK_NOTNULL && t != TK_ISNULL) { + if (t != TK_EQ && t != TK_NE && t != TK_NOTNULL && t != TK_ISNULL && t != TK_IN) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg3); } } From 08cb8c361ec38c3a3f34723b679a57031a20fa5b Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Sun, 6 Jun 2021 00:28:34 +0800 Subject: [PATCH 41/57] [TD-4426] fix compile error --- tests/script/general/parser/where.sim | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/script/general/parser/where.sim b/tests/script/general/parser/where.sim index d98d2f0507..270f13eaac 100644 --- a/tests/script/general/parser/where.sim +++ b/tests/script/general/parser/where.sim @@ -155,14 +155,14 @@ sql_error select last(*) from wh_mt1_tb1 where c6 in ('1') #sql_error select last(*) from wh_mt1_tb1 where c7 in ('binary') #sql_error select last(*) from wh_mt1 where c8 in ('nchar') #sql_error select last(*) from wh_mt1_tb1 where c9 in (true, false) -#sql_error select last(*) from wh_mt1 where c10 in ('2019-01-01 00:00:00.000') -#sql_error select last(*) from wh_mt1_tb1 where c10 in ('2019-01-01 00:00:00.000') -sql_error select last(*) from wh_mt1 where t1 in ('binary') -sql_error select last(*) from wh_mt1 where t2 in (1) -sql_error select last(*) from wh_mt1 where t3 in (1) -sql_error select last(*) from wh_mt1 where t4 in (1) -sql_error select last(*) from wh_mt1 where t5 in (1) -sql_error select last(*) from wh_mt1 where t6 in (1) +sql_error select last(*) from wh_mt1 where c10 in ('2019-01-01 00:00:00.000') +sql_error select last(*) from wh_mt1_tb1 where c10 in ('2019-01-01 00:00:00.000') +#sql_error select last(*) from wh_mt1 where t1 in ('binary') +#sql_error select last(*) from wh_mt1 where t2 in (1) +#sql_error select last(*) from wh_mt1 where t3 in (1) +#sql_error select last(*) from wh_mt1 where t4 in (1) +#sql_error select last(*) from wh_mt1 where t5 in (1) +#sql_error select last(*) from wh_mt1 where t6 in (1) sql select last(*) from wh_mt1 where c1 = 1 if $rows != 1 then return -1 From b645bb4fafa5267b58ac209862affcf0eee9798f Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Sun, 6 Jun 2021 03:31:48 +0800 Subject: [PATCH 42/57] [TD-4426] fix compile error --- tests/script/general/parser/where.sim | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/script/general/parser/where.sim b/tests/script/general/parser/where.sim index 270f13eaac..6dfea3d2e7 100644 --- a/tests/script/general/parser/where.sim +++ b/tests/script/general/parser/where.sim @@ -157,12 +157,6 @@ sql_error select last(*) from wh_mt1_tb1 where c6 in ('1') #sql_error select last(*) from wh_mt1_tb1 where c9 in (true, false) sql_error select last(*) from wh_mt1 where c10 in ('2019-01-01 00:00:00.000') sql_error select last(*) from wh_mt1_tb1 where c10 in ('2019-01-01 00:00:00.000') -#sql_error select last(*) from wh_mt1 where t1 in ('binary') -#sql_error select last(*) from wh_mt1 where t2 in (1) -#sql_error select last(*) from wh_mt1 where t3 in (1) -#sql_error select last(*) from wh_mt1 where t4 in (1) -#sql_error select last(*) from wh_mt1 where t5 in (1) -#sql_error select last(*) from wh_mt1 where t6 in (1) sql select last(*) from wh_mt1 where c1 = 1 if $rows != 1 then return -1 From 89abdd980927fcfc5f5aed3713968563b9f8893d Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sun, 6 Jun 2021 15:20:19 +0800 Subject: [PATCH 43/57] [td-4555]: add derivative function --- src/client/inc/tscUtil.h | 2 +- src/client/src/tscSQLParser.c | 40 +- src/client/src/tscServer.c | 2 +- src/client/src/tscSubquery.c | 3 +- src/client/src/tscUtil.c | 12 +- src/query/inc/qAggMain.h | 22 +- src/query/inc/qExecutor.h | 1 + src/query/src/qAggMain.c | 358 ++++++++++++++++-- src/query/src/qExecutor.c | 13 +- .../general/parser/join_multitables.sim | 11 - tests/script/general/parser/testSuite.sim | 1 + 11 files changed, 383 insertions(+), 82 deletions(-) diff --git a/src/client/inc/tscUtil.h b/src/client/inc/tscUtil.h index 0a57767926..427431d3c0 100644 --- a/src/client/inc/tscUtil.h +++ b/src/client/inc/tscUtil.h @@ -214,7 +214,7 @@ void tscColumnListDestroy(SArray* pColList); void tscColumnListCopy(SArray* dst, const SArray* src, uint64_t tableUid); void tscColumnListCopyAll(SArray* dst, const SArray* src); -void convertQueryResult(SSqlRes* pRes, SQueryInfo* pQueryInfo); +void convertQueryResult(SSqlRes* pRes, SQueryInfo* pQueryInfo, uint64_t objId); void tscDequoteAndTrimToken(SStrToken* pToken); int32_t tscValidateName(SStrToken* pToken); diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 4a80cbd340..d48f193e07 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -2157,11 +2157,14 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col case TSDB_FUNC_MIN: case TSDB_FUNC_MAX: case TSDB_FUNC_DIFF: + case TSDB_FUNC_DERIVATIVE: case TSDB_FUNC_STDDEV: case TSDB_FUNC_LEASTSQR: { // 1. valid the number of parameters - if (pItem->pNode->pParam == NULL || (functionId != TSDB_FUNC_LEASTSQR && taosArrayGetSize(pItem->pNode->pParam) != 1) || - (functionId == TSDB_FUNC_LEASTSQR && taosArrayGetSize(pItem->pNode->pParam) != 3)) { + int32_t numOfParams = taosArrayGetSize(pItem->pNode->pParam); + if (pItem->pNode->pParam == NULL || + (functionId != TSDB_FUNC_LEASTSQR && functionId != TSDB_FUNC_DERIVATIVE && numOfParams != 1) || + ((functionId == TSDB_FUNC_LEASTSQR || functionId == TSDB_FUNC_DERIVATIVE) && numOfParams != 3)) { /* no parameters or more than one parameter for function */ return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg2); } @@ -2182,11 +2185,13 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col // 2. check if sql function can be applied on this column data type pTableMetaInfo = tscGetMetaInfo(pQueryInfo, index.tableIndex); + STableComInfo info = tscGetTableInfo(pTableMetaInfo->pTableMeta); + SSchema* pSchema = tscGetTableColumnSchema(pTableMetaInfo->pTableMeta, index.columnIndex); if (!IS_NUMERIC_TYPE(pSchema->type)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg1); - } else if (IS_UNSIGNED_NUMERIC_TYPE(pSchema->type) && functionId == TSDB_FUNC_DIFF) { + } else if (IS_UNSIGNED_NUMERIC_TYPE(pSchema->type) && (functionId == TSDB_FUNC_DIFF || functionId == TSDB_FUNC_DERIVATIVE)) { return invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg9); } @@ -2200,11 +2205,11 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col } // set the first column ts for diff query - if (functionId == TSDB_FUNC_DIFF) { + if (functionId == TSDB_FUNC_DIFF || functionId == TSDB_FUNC_DERIVATIVE) { colIndex += 1; SColumnIndex indexTS = {.tableIndex = index.tableIndex, .columnIndex = 0}; - SExprInfo* pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &indexTS, TSDB_DATA_TYPE_TIMESTAMP, TSDB_KEYSIZE, - getNewResColId(pCmd), TSDB_KEYSIZE, false); + SExprInfo* pExpr = tscExprAppend(pQueryInfo, TSDB_FUNC_TS_DUMMY, &indexTS, TSDB_DATA_TYPE_TIMESTAMP, + TSDB_KEYSIZE, getNewResColId(pCmd), TSDB_KEYSIZE, false); SColumnList ids = createColumnList(1, 0, 0); insertResultField(pQueryInfo, 0, &ids, TSDB_KEYSIZE, TSDB_DATA_TYPE_TIMESTAMP, aAggs[TSDB_FUNC_TS_DUMMY].name, pExpr); @@ -2230,12 +2235,29 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col return TSDB_CODE_TSC_INVALID_OPERATION; } - tscExprAddParams(&pExpr->base, val, TSDB_DATA_TYPE_DOUBLE, sizeof(double)); + tscExprAddParams(&pExpr->base, val, TSDB_DATA_TYPE_DOUBLE, DOUBLE_BYTES); } else if (functionId == TSDB_FUNC_IRATE) { - STableComInfo info = tscGetTableInfo(pTableMetaInfo->pTableMeta); int64_t prec = info.precision; - tscExprAddParams(&pExpr->base, (char*)&prec, TSDB_DATA_TYPE_BIGINT, LONG_BYTES); + } else if (functionId == TSDB_FUNC_DERIVATIVE) { + char val[8] = {0}; + + int64_t tickPerSec = 0; + if (tVariantDump(&pParamElem[1].pNode->value, (char*) &tickPerSec, TSDB_DATA_TYPE_BIGINT, true) < 0) { + return TSDB_CODE_TSC_INVALID_OPERATION; + } + + if (info.precision == TSDB_TIME_PRECISION_MILLI) { + tickPerSec /= 1000; + } + + tscExprAddParams(&pExpr->base, (char*) &tickPerSec, TSDB_DATA_TYPE_BIGINT, LONG_BYTES); + memset(val, 0, tListLen(val)); + if (tVariantDump(&pParamElem[2].pNode->value, val, TSDB_DATA_TYPE_BIGINT, true) < 0) { + return TSDB_CODE_TSC_INVALID_OPERATION; + } + + tscExprAddParams(&pExpr->base, val, TSDB_DATA_TYPE_BIGINT, LONG_BYTES); } SColumnList ids = createColumnList(1, index.tableIndex, index.columnIndex); diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 5f2de43ef0..880c58aa1c 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -1621,7 +1621,7 @@ int tscProcessRetrieveGlobalMergeRsp(SSqlObj *pSql) { uint64_t localQueryId = pSql->self; qTableQuery(pQueryInfo->pQInfo, &localQueryId); - convertQueryResult(pRes, pQueryInfo); + convertQueryResult(pRes, pQueryInfo, pSql->self); code = pRes->code; if (pRes->code == TSDB_CODE_SUCCESS) { diff --git a/src/client/src/tscSubquery.c b/src/client/src/tscSubquery.c index cf732a8f20..49ce738545 100644 --- a/src/client/src/tscSubquery.c +++ b/src/client/src/tscSubquery.c @@ -1977,9 +1977,8 @@ void tscHandleMasterJoinQuery(SSqlObj* pSql) { } memset(pSql->subState.states, 0, sizeof(*pSql->subState.states) * pSql->subState.numOfSub); - tscDebug("0x%"PRIx64" reset all sub states to 0", pSql->self); + tscDebug("0x%"PRIx64" reset all sub states to 0, start subquery, total:%d", pSql->self, pQueryInfo->numOfTables); - tscDebug("0x%"PRIx64" start subquery, total:%d", pSql->self, pQueryInfo->numOfTables); for (int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { SJoinSupporter *pSupporter = tscCreateJoinSupporter(pSql, i); if (pSupporter == NULL) { // failed to create support struct, abort current query diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 627fbbd0ee..46d49bf68d 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -491,7 +491,7 @@ bool isSimpleAggregateRv(SQueryInfo* pQueryInfo) { return false; } - if (/*tscGroupbyColumn(pQueryInfo) || */isTsCompQuery(pQueryInfo) || tscIsTopBotQuery(pQueryInfo) || tscIsDiffQuery(pQueryInfo)) { + if (tscIsDiffQuery(pQueryInfo)) { return false; } @@ -507,13 +507,13 @@ bool isSimpleAggregateRv(SQueryInfo* pQueryInfo) { continue; } - if (!IS_MULTIOUTPUT(aAggs[functionId].status)) { + if ((!IS_MULTIOUTPUT(aAggs[functionId].status)) || + (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM || functionId == TSDB_FUNC_TS_COMP)) { return true; } } return false; - } bool isBlockDistQuery(SQueryInfo* pQueryInfo) { @@ -1046,7 +1046,7 @@ SOperatorInfo* createJoinOperatorInfo(SOperatorInfo** pUpstream, int32_t numOfUp return pOperator; } -void convertQueryResult(SSqlRes* pRes, SQueryInfo* pQueryInfo) { +void convertQueryResult(SSqlRes* pRes, SQueryInfo* pQueryInfo, uint64_t objId) { // set the correct result SSDataBlock* p = pQueryInfo->pQInfo->runtimeEnv.outputBuf; pRes->numOfRows = (p != NULL)? p->info.rows: 0; @@ -1056,6 +1056,7 @@ void convertQueryResult(SSqlRes* pRes, SQueryInfo* pQueryInfo) { tscSetResRawPtrRv(pRes, pQueryInfo, p); } + tscDebug("0x%"PRIx64" retrieve result in pRes, numOfRows:%d", objId, pRes->numOfRows); pRes->row = 0; pRes->completed = (pRes->numOfRows == 0); } @@ -1172,7 +1173,7 @@ void handleDownstreamOperator(SSqlObj** pSqlObjList, int32_t numOfUpstream, SQue uint64_t qId = pSql->self; qTableQuery(px->pQInfo, &qId); - convertQueryResult(pOutput, px); + convertQueryResult(pOutput, px, pSql->self); } static void tscDestroyResPointerInfo(SSqlRes* pRes) { @@ -2171,6 +2172,7 @@ size_t tscNumOfExprs(SQueryInfo* pQueryInfo) { return taosArrayGetSize(pQueryInfo->exprList); } +// todo REFACTOR void tscExprAddParams(SSqlExpr* pExpr, char* argument, int32_t type, int32_t bytes) { assert (pExpr != NULL || argument != NULL || bytes != 0); diff --git a/src/query/inc/qAggMain.h b/src/query/inc/qAggMain.h index 47c61fc444..57e7d2982f 100644 --- a/src/query/inc/qAggMain.h +++ b/src/query/inc/qAggMain.h @@ -66,17 +66,19 @@ extern "C" { #define TSDB_FUNC_RATE 29 #define TSDB_FUNC_IRATE 30 #define TSDB_FUNC_TID_TAG 31 -#define TSDB_FUNC_BLKINFO 32 +#define TSDB_FUNC_DERIVATIVE 32 +#define TSDB_FUNC_BLKINFO 33 -#define TSDB_FUNC_HISTOGRAM 33 -#define TSDB_FUNC_HLL 34 -#define TSDB_FUNC_MODE 35 -#define TSDB_FUNC_SAMPLE 36 -#define TSDB_FUNC_CEIL 37 -#define TSDB_FUNC_FLOOR 38 -#define TSDB_FUNC_ROUND 39 -#define TSDB_FUNC_MAVG 40 -#define TSDB_FUNC_CSUM 41 + +#define TSDB_FUNC_HISTOGRAM 34 +#define TSDB_FUNC_HLL 35 +#define TSDB_FUNC_MODE 36 +#define TSDB_FUNC_SAMPLE 37 +#define TSDB_FUNC_CEIL 38 +#define TSDB_FUNC_FLOOR 39 +#define TSDB_FUNC_ROUND 40 +#define TSDB_FUNC_MAVG 41 +#define TSDB_FUNC_CSUM 42 #define TSDB_FUNCSTATE_SO 0x1u // single output #define TSDB_FUNCSTATE_MO 0x2u // dynamic number of output, not multinumber of output e.g., TOP/BOTTOM diff --git a/src/query/inc/qExecutor.h b/src/query/inc/qExecutor.h index 93f2b73d7a..7e76bd7622 100644 --- a/src/query/inc/qExecutor.h +++ b/src/query/inc/qExecutor.h @@ -246,6 +246,7 @@ typedef struct SQueryRuntimeEnv { void* pQueryHandle; int32_t prevGroupId; // previous executed group id + bool enableGroupData; SDiskbasedResultBuf* pResultBuf; // query result buffer based on blocked-wised disk file SHashObj* pResultRowHashTable; // quick locate the window object for each result char* keyBuf; // window key buffer diff --git a/src/query/src/qAggMain.c b/src/query/src/qAggMain.c index 74c95e7281..dc1829930c 100644 --- a/src/query/src/qAggMain.c +++ b/src/query/src/qAggMain.c @@ -161,6 +161,14 @@ typedef struct SRateInfo { bool isIRate; // true for IRate functions, false for Rate functions } SRateInfo; +typedef struct SDerivInfo { + double prevValue; // previous value + TSKEY prevTs; // previous timestamp + bool ignoreNegative;// ignore the negative value + int64_t tsWindow; // time window for derivative + bool valueSet; // the value has been set already +} SDerivInfo; + int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionId, int32_t param, int16_t *type, int16_t *bytes, int32_t *interBytes, int16_t extLength, bool isSuperTable) { if (!isValidDataType(dataType)) { @@ -189,7 +197,9 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI *bytes = (int16_t)(dataBytes + sizeof(int16_t) + sizeof(int64_t) + sizeof(int32_t) + sizeof(int32_t) + VARSTR_HEADER_SIZE); *interBytes = 0; return TSDB_CODE_SUCCESS; - } else if (functionId == TSDB_FUNC_BLKINFO) { + } + + if (functionId == TSDB_FUNC_BLKINFO) { *type = TSDB_DATA_TYPE_BINARY; *bytes = 16384; *interBytes = 0; @@ -216,7 +226,15 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI *interBytes = POINTER_BYTES; return TSDB_CODE_SUCCESS; } - + + if (functionId == TSDB_FUNC_DERIVATIVE) { + *type = TSDB_DATA_TYPE_DOUBLE; + *bytes = sizeof(double); // this results is compressed ts data, only one byte + *interBytes = sizeof(SDerivInfo); + return TSDB_CODE_SUCCESS; + } + + //TODO twa function definit error. if (isSuperTable) { if (functionId == TSDB_FUNC_MIN || functionId == TSDB_FUNC_MAX) { *type = TSDB_DATA_TYPE_BINARY; @@ -3402,16 +3420,266 @@ static bool diff_function_setup(SQLFunctionCtx *pCtx) { return false; } +#define DIFF_IMPL(ctx, d, type) \ + do { \ + if ((ctx)->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { \ + (ctx)->param[1].nType = (ctx)->inputType; \ + *(type *)&(ctx)->param[1].i64 = *(type *)(d); \ + } else { \ + *(type *)(ctx)->pOutput = *(type *)(d) - (*(type *)(&(ctx)->param[1].i64)); \ + *(type *)(&(ctx)->param[1].i64) = *(type *)(d); \ + *(int64_t *)(ctx)->ptsOutputBuf = GET_TS_DATA(ctx, index); \ + } \ + } while (0); + +static bool deriv_function_setup(SQLFunctionCtx *pCtx) { + if (!function_setup(pCtx)) { + return false; + } + + // diff function require the value is set to -1 + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); + SDerivInfo* pDerivInfo = GET_ROWCELL_INTERBUF(pResInfo); + + pDerivInfo->ignoreNegative = pCtx->param[2].i64; + pDerivInfo->prevTs = -1; + pDerivInfo->tsWindow = pCtx->param[0].i64; + pDerivInfo->valueSet = false; + return false; +} + +static void deriv_function(SQLFunctionCtx *pCtx) { + SResultRowCellInfo *pResInfo = GET_RES_INFO(pCtx); + SDerivInfo* pDerivInfo = GET_ROWCELL_INTERBUF(pResInfo); + + void *data = GET_INPUT_DATA_LIST(pCtx); + bool isFirstBlock = (pDerivInfo->valueSet == false); + + int32_t notNullElems = 0; + + int32_t step = GET_FORWARD_DIRECTION_FACTOR(pCtx->order); + int32_t i = (pCtx->order == TSDB_ORDER_ASC) ? 0 : pCtx->size - 1; + + TSKEY *pTimestamp = pCtx->ptsOutputBuf; + TSKEY *tsList = GET_TS_LIST(pCtx); + + double *pOutput = (double *)pCtx->pOutput; + + switch (pCtx->inputType) { + case TSDB_DATA_TYPE_INT: { + int32_t *pData = (int32_t *)data; + for (; i < pCtx->size && i >= 0; i += step) { + if (pCtx->hasNull && isNull((const char *)&pData[i], pCtx->inputType)) { + continue; + } + + if (!pDerivInfo->valueSet) { // initial value is not set yet + pDerivInfo->valueSet = true; + } else { + *pOutput = ((pData[i] - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (tsList[i] - pDerivInfo->prevTs); + if (pDerivInfo->ignoreNegative && *pOutput < 0) { + } else { + *pTimestamp = tsList[i]; + + pOutput += 1; + pTimestamp += 1; + } + } + + pDerivInfo->prevValue = pData[i]; + pDerivInfo->prevTs = tsList[i]; + notNullElems++; + } + + break; + }; + + case TSDB_DATA_TYPE_BIGINT: { + int64_t *pData = (int64_t *)data; + for (; i < pCtx->size && i >= 0; i += step) { + if (pCtx->hasNull && isNull((const char *)&pData[i], pCtx->inputType)) { + continue; + } + + if (!pDerivInfo->valueSet) { // initial value is not set yet + pDerivInfo->valueSet = true; + } else { + *pOutput = ((pData[i] - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (tsList[i] - pDerivInfo->prevTs); + if (pDerivInfo->ignoreNegative && *pOutput < 0) { + } else { + *pTimestamp = tsList[i]; + + pOutput += 1; + pTimestamp += 1; + } + } + + pDerivInfo->prevValue = pData[i]; + pDerivInfo->prevTs = tsList[i]; + notNullElems++; + } + break; + } + case TSDB_DATA_TYPE_DOUBLE: { + double *pData = (double *)data; + + for (; i < pCtx->size && i >= 0; i += step) { + if (pCtx->hasNull && isNull((const char *)&pData[i], pCtx->inputType)) { + continue; + } + + if (!pDerivInfo->valueSet) { // initial value is not set yet + pDerivInfo->valueSet = true; + } else { + *pOutput = ((pData[i] - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (tsList[i] - pDerivInfo->prevTs); + if (pDerivInfo->ignoreNegative && *pOutput < 0) { + } else { + *pTimestamp = tsList[i]; + + pOutput += 1; + pTimestamp += 1; + } + } + + pDerivInfo->prevValue = pData[i]; + pDerivInfo->prevTs = tsList[i]; + notNullElems++; + } + break; + } + + case TSDB_DATA_TYPE_FLOAT: { + float *pData = (float *)data; + + for (; i < pCtx->size && i >= 0; i += step) { + if (pCtx->hasNull && isNull((const char *)&pData[i], pCtx->inputType)) { + continue; + } + + if (!pDerivInfo->valueSet) { // initial value is not set yet + pDerivInfo->valueSet = true; + } else { + *pOutput = ((pData[i] - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (tsList[i] - pDerivInfo->prevTs); + if (pDerivInfo->ignoreNegative && *pOutput < 0) { + } else { + *pTimestamp = tsList[i]; + + pOutput += 1; + pTimestamp += 1; + } + } + + pDerivInfo->prevValue = pData[i]; + pDerivInfo->prevTs = tsList[i]; + notNullElems++; + } + break; + } + + case TSDB_DATA_TYPE_SMALLINT: { + int16_t *pData = (int16_t *)data; + for (; i < pCtx->size && i >= 0; i += step) { + if (pCtx->hasNull && isNull((const char *)&pData[i], pCtx->inputType)) { + continue; + } + + if (!pDerivInfo->valueSet) { // initial value is not set yet + pDerivInfo->valueSet = true; + } else { + *pOutput = ((pData[i] - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (tsList[i] - pDerivInfo->prevTs); + if (pDerivInfo->ignoreNegative && *pOutput < 0) { + } else { + *pTimestamp = tsList[i]; + + pOutput += 1; + pTimestamp += 1; + } + } + + pDerivInfo->prevValue = pData[i]; + pDerivInfo->prevTs = tsList[i]; + notNullElems++; + } + break; + } + + case TSDB_DATA_TYPE_TINYINT: { + int8_t *pData = (int8_t *)data; + for (; i < pCtx->size && i >= 0; i += step) { + if (pCtx->hasNull && isNull((char *)&pData[i], pCtx->inputType)) { + continue; + } + + if (!pDerivInfo->valueSet) { // initial value is not set yet + pDerivInfo->valueSet = true; + } else { + *pOutput = ((pData[i] - pDerivInfo->prevValue) * pDerivInfo->tsWindow) / (tsList[i] - pDerivInfo->prevTs); + if (pDerivInfo->ignoreNegative && *pOutput < 0) { + } else { + *pTimestamp = tsList[i]; + + pOutput += 1; + pTimestamp += 1; + } + } + + pDerivInfo->prevValue = pData[i]; + pDerivInfo->prevTs = tsList[i]; + notNullElems++; + } + break; + } + default: + qError("error input type"); + } + + // initial value is not set yet, all data block are null + if (!pDerivInfo->valueSet || notNullElems <= 0) { + /* + * 1. current block and blocks before are full of null + * 2. current block may be null value + */ + assert(pCtx->hasNull); + } else { + int32_t forwardStep = (isFirstBlock) ? notNullElems - 1 : notNullElems; + GET_RES_INFO(pCtx)->numOfRes += forwardStep; + } +} + +#define DIFF_IMPL(ctx, d, type) \ + do { \ + if ((ctx)->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { \ + (ctx)->param[1].nType = (ctx)->inputType; \ + *(type *)&(ctx)->param[1].i64 = *(type *)(d); \ + } else { \ + *(type *)(ctx)->pOutput = *(type *)(d) - (*(type *)(&(ctx)->param[1].i64)); \ + *(type *)(&(ctx)->param[1].i64) = *(type *)(d); \ + *(int64_t *)(ctx)->ptsOutputBuf = GET_TS_DATA(ctx, index); \ + } \ + } while (0); + +#define DIFF_IMPL(ctx, d, type) \ + do { \ + if ((ctx)->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { \ + (ctx)->param[1].nType = (ctx)->inputType; \ + *(type *)&(ctx)->param[1].i64 = *(type *)(d); \ + } else { \ + *(type *)(ctx)->pOutput = *(type *)(d) - (*(type *)(&(ctx)->param[1].i64)); \ + *(type *)(&(ctx)->param[1].i64) = *(type *)(d); \ + *(int64_t *)(ctx)->ptsOutputBuf = GET_TS_DATA(ctx, index); \ + } \ + } while (0); + // TODO difference in date column static void diff_function(SQLFunctionCtx *pCtx) { void *data = GET_INPUT_DATA_LIST(pCtx); bool isFirstBlock = (pCtx->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED); - + int32_t notNullElems = 0; - + int32_t step = GET_FORWARD_DIRECTION_FACTOR(pCtx->order); int32_t i = (pCtx->order == TSDB_ORDER_ASC) ? 0 : pCtx->size - 1; - + TSKEY* pTimestamp = pCtx->ptsOutputBuf; TSKEY* tsList = GET_TS_LIST(pCtx); @@ -3419,29 +3687,29 @@ static void diff_function(SQLFunctionCtx *pCtx) { case TSDB_DATA_TYPE_INT: { int32_t *pData = (int32_t *)data; int32_t *pOutput = (int32_t *)pCtx->pOutput; - + for (; i < pCtx->size && i >= 0; i += step) { if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) { continue; } - + if (pCtx->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet pCtx->param[1].i64 = pData[i]; pCtx->param[1].nType = pCtx->inputType; } else if ((i == 0 && pCtx->order == TSDB_ORDER_ASC) || (i == pCtx->size - 1 && pCtx->order == TSDB_ORDER_DESC)) { *pOutput = (int32_t)(pData[i] - pCtx->param[1].i64); *pTimestamp = tsList[i]; - + pOutput += 1; pTimestamp += 1; } else { *pOutput = (int32_t)(pData[i] - pCtx->param[1].i64); // direct previous may be null *pTimestamp = tsList[i]; - + pOutput += 1; pTimestamp += 1; } - + pCtx->param[1].i64 = pData[i]; pCtx->param[1].nType = pCtx->inputType; notNullElems++; @@ -3451,29 +3719,29 @@ static void diff_function(SQLFunctionCtx *pCtx) { case TSDB_DATA_TYPE_BIGINT: { int64_t *pData = (int64_t *)data; int64_t *pOutput = (int64_t *)pCtx->pOutput; - + for (; i < pCtx->size && i >= 0; i += step) { if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) { continue; } - + if (pCtx->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet pCtx->param[1].i64 = pData[i]; pCtx->param[1].nType = pCtx->inputType; } else if ((i == 0 && pCtx->order == TSDB_ORDER_ASC) || (i == pCtx->size - 1 && pCtx->order == TSDB_ORDER_DESC)) { *pOutput = pData[i] - pCtx->param[1].i64; *pTimestamp = tsList[i]; - + pOutput += 1; pTimestamp += 1; } else { *pOutput = pData[i] - pCtx->param[1].i64; *pTimestamp = tsList[i]; - + pOutput += 1; pTimestamp += 1; } - + pCtx->param[1].i64 = pData[i]; pCtx->param[1].nType = pCtx->inputType; notNullElems++; @@ -3483,12 +3751,12 @@ static void diff_function(SQLFunctionCtx *pCtx) { case TSDB_DATA_TYPE_DOUBLE: { double *pData = (double *)data; double *pOutput = (double *)pCtx->pOutput; - + for (; i < pCtx->size && i >= 0; i += step) { if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) { continue; } - + if (pCtx->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet pCtx->param[1].dKey = pData[i]; pCtx->param[1].nType = pCtx->inputType; @@ -3503,7 +3771,7 @@ static void diff_function(SQLFunctionCtx *pCtx) { pOutput += 1; pTimestamp += 1; } - + pCtx->param[1].dKey = pData[i]; pCtx->param[1].nType = pCtx->inputType; notNullElems++; @@ -3513,29 +3781,29 @@ static void diff_function(SQLFunctionCtx *pCtx) { case TSDB_DATA_TYPE_FLOAT: { float *pData = (float *)data; float *pOutput = (float *)pCtx->pOutput; - + for (; i < pCtx->size && i >= 0; i += step) { if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) { continue; } - + if (pCtx->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet pCtx->param[1].dKey = pData[i]; pCtx->param[1].nType = pCtx->inputType; } else if ((i == 0 && pCtx->order == TSDB_ORDER_ASC) || (i == pCtx->size - 1 && pCtx->order == TSDB_ORDER_DESC)) { *pOutput = (float)(pData[i] - pCtx->param[1].dKey); *pTimestamp = tsList[i]; - + pOutput += 1; pTimestamp += 1; } else { *pOutput = (float)(pData[i] - pCtx->param[1].dKey); *pTimestamp = tsList[i]; - + pOutput += 1; pTimestamp += 1; } - + // keep the last value, the remain may be all null pCtx->param[1].dKey = pData[i]; pCtx->param[1].nType = pCtx->inputType; @@ -3546,12 +3814,12 @@ static void diff_function(SQLFunctionCtx *pCtx) { case TSDB_DATA_TYPE_SMALLINT: { int16_t *pData = (int16_t *)data; int16_t *pOutput = (int16_t *)pCtx->pOutput; - + for (; i < pCtx->size && i >= 0; i += step) { if (pCtx->hasNull && isNull((const char*) &pData[i], pCtx->inputType)) { continue; } - + if (pCtx->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet pCtx->param[1].i64 = pData[i]; pCtx->param[1].nType = pCtx->inputType; @@ -3563,11 +3831,11 @@ static void diff_function(SQLFunctionCtx *pCtx) { } else { *pOutput = (int16_t)(pData[i] - pCtx->param[1].i64); *pTimestamp = tsList[i]; - + pOutput += 1; pTimestamp += 1; } - + pCtx->param[1].i64 = pData[i]; pCtx->param[1].nType = pCtx->inputType; notNullElems++; @@ -3577,29 +3845,29 @@ static void diff_function(SQLFunctionCtx *pCtx) { case TSDB_DATA_TYPE_TINYINT: { int8_t *pData = (int8_t *)data; int8_t *pOutput = (int8_t *)pCtx->pOutput; - + for (; i < pCtx->size && i >= 0; i += step) { if (pCtx->hasNull && isNull((char *)&pData[i], pCtx->inputType)) { continue; } - + if (pCtx->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet pCtx->param[1].i64 = pData[i]; pCtx->param[1].nType = pCtx->inputType; } else if ((i == 0 && pCtx->order == TSDB_ORDER_ASC) || (i == pCtx->size - 1 && pCtx->order == TSDB_ORDER_DESC)) { *pOutput = (int8_t)(pData[i] - pCtx->param[1].i64); *pTimestamp = tsList[i]; - + pOutput += 1; pTimestamp += 1; } else { *pOutput = (int8_t)(pData[i] - pCtx->param[1].i64); *pTimestamp = tsList[i]; - + pOutput += 1; pTimestamp += 1; } - + pCtx->param[1].i64 = pData[i]; pCtx->param[1].nType = pCtx->inputType; notNullElems++; @@ -3609,7 +3877,7 @@ static void diff_function(SQLFunctionCtx *pCtx) { default: qError("error input type"); } - + // initial value is not set yet if (pCtx->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED || notNullElems <= 0) { /* @@ -3619,7 +3887,7 @@ static void diff_function(SQLFunctionCtx *pCtx) { assert(pCtx->hasNull); } else { int32_t forwardStep = (isFirstBlock) ? notNullElems - 1 : notNullElems; - + GET_RES_INFO(pCtx)->numOfRes += forwardStep; } } @@ -3641,14 +3909,14 @@ static void diff_function_f(SQLFunctionCtx *pCtx, int32_t index) { if (pCtx->hasNull && isNull(pData, pCtx->inputType)) { return; } - + // the output start from the second source element if (pCtx->param[1].nType != INITIAL_VALUE_NOT_ASSIGNED) { // initial value is set GET_RES_INFO(pCtx)->numOfRes += 1; } - + int32_t step = 1/*GET_FORWARD_DIRECTION_FACTOR(pCtx->order)*/; - + switch (pCtx->inputType) { case TSDB_DATA_TYPE_INT: { if (pCtx->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet @@ -3684,7 +3952,7 @@ static void diff_function_f(SQLFunctionCtx *pCtx, int32_t index) { default: qError("error input type"); } - + if (GET_RES_INFO(pCtx)->numOfRes > 0) { pCtx->pOutput += pCtx->outputBytes * step; pCtx->ptsOutputBuf = (char *)pCtx->ptsOutputBuf + TSDB_KEYSIZE * step; @@ -5315,8 +5583,20 @@ SAggFunctionInfo aAggs[] = {{ noop1, dataBlockRequired, }, + { //32 + "derivative", // return table id and the corresponding tags for join match and subscribe + TSDB_FUNC_DERIVATIVE, + TSDB_FUNC_INVALID_ID, + TSDB_FUNCSTATE_MO | TSDB_FUNCSTATE_STABLE | TSDB_FUNCSTATE_NEED_TS, + deriv_function_setup, + deriv_function, + noop2, + doFinalizer, + noop1, + dataBlockRequired, + }, { - // 32 + // 33 "_block_dist", // return table id and the corresponding tags for join match and subscribe TSDB_FUNC_BLKINFO, TSDB_FUNC_BLKINFO, diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index d14189657a..338861144e 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -1681,6 +1681,8 @@ static int32_t setupQueryRuntimeEnv(SQueryRuntimeEnv *pRuntimeEnv, int32_t numOf SQueryAttr *pQueryAttr = pRuntimeEnv->pQueryAttr; pRuntimeEnv->prevGroupId = INT32_MIN; + pRuntimeEnv->enableGroupData = false; + pRuntimeEnv->pQueryAttr = pQueryAttr; pRuntimeEnv->pResultRowHashTable = taosHashInit(numOfTables, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); @@ -3119,8 +3121,8 @@ void setDefaultOutputBuf(SQueryRuntimeEnv *pRuntimeEnv, SOptrBasicInfo *pInfo, i assert(pCtx[i].pOutput != NULL); // set the timestamp output buffer for top/bottom/diff query - int32_t functionId = pCtx[i].functionId; - if (functionId == TSDB_FUNC_TOP || functionId == TSDB_FUNC_BOTTOM || functionId == TSDB_FUNC_DIFF) { + int32_t fid = pCtx[i].functionId; + if (fid == TSDB_FUNC_TOP || fid == TSDB_FUNC_BOTTOM || fid == TSDB_FUNC_DIFF || fid == TSDB_FUNC_DERIVATIVE) { pCtx[i].ptsOutputBuf = pCtx[0].pOutput; } } @@ -4274,8 +4276,10 @@ static SSDataBlock* doTableScanImpl(void* param, bool* newgroup) { pRuntimeEnv->current = *pTableQueryInfo; doTableQueryInfoTimeWindowCheck(pQueryAttr, *pTableQueryInfo); - if (pTableScanInfo->prevGroupId != -1 && pTableScanInfo->prevGroupId != (*pTableQueryInfo)->groupIndex) { - *newgroup = false; + if (pRuntimeEnv->enableGroupData) { + if(pTableScanInfo->prevGroupId != -1 && pTableScanInfo->prevGroupId != (*pTableQueryInfo)->groupIndex) { + *newgroup = true; + } } pTableScanInfo->prevGroupId = (*pTableQueryInfo)->groupIndex; @@ -4449,6 +4453,7 @@ SOperatorInfo* createTableSeqScanOperator(void* pTsdbQueryHandle, SQueryRuntimeE pInfo->order = pRuntimeEnv->pQueryAttr->order.order; pInfo->current = 0; pInfo->prevGroupId = -1; + pRuntimeEnv->enableGroupData = true; SOperatorInfo* pOperator = calloc(1, sizeof(SOperatorInfo)); pOperator->name = "TableSeqScanOperator"; diff --git a/tests/script/general/parser/join_multitables.sim b/tests/script/general/parser/join_multitables.sim index acb8be10e7..d675499640 100644 --- a/tests/script/general/parser/join_multitables.sim +++ b/tests/script/general/parser/join_multitables.sim @@ -1811,10 +1811,6 @@ if $data09 != 3 then return -1 endi - - - - sql select st0.*,st1.* from st0, st1 where st1.id1=st0.id1 and st0.ts=st1.ts and st1.ts=st0.ts and st0.id1=st1.id1 order by st0.ts limit 5 offset 5 if $rows != 5 then return -1 @@ -2294,7 +2290,6 @@ if $data19 != 9925 then return -1 endi - sql_error select tb0_1.*, tb1_1.* from tb0_1, tb1_1 where tb0_1.f1=tb1_1.f1; sql_error select tb0_1.*, tb1_1.* from tb0_1, tb1_1 where tb0_1.ts=tb1_1.ts and tb0_1.id1=tb1_1.id2; sql_error select tb0_5.*, tb1_5.*,tb2_5.*,tb3_5.*,tb4_5.*,tb5_5.*, tb6_5.*,tb7_5.*,tb8_5.*,tb9_5.*,tba_5.* from tb0_5, tb1_5, tb2_5, tb3_5, tb4_5,tb5_5, tb6_5, tb7_5, tb8_5, tb9_5, tba_5 where tb9_5.ts=tb8_5.ts and tb8_5.ts=tb7_5.ts and tb7_5.ts=tb6_5.ts and tb6_5.ts=tb5_5.ts and tb5_5.ts=tb4_5.ts and tb4_5.ts=tb3_5.ts and tb3_5.ts=tb2_5.ts and tb2_5.ts=tb1_5.ts and tb1_5.ts=tb0_5.ts and tb0_5.ts=tba_5.ts; @@ -2317,10 +2312,4 @@ sql_error select last(*) from st0, st1 where st0.ts=st1.ts and st0.id1=st1.id1 g sql_error select st0.*,st1.*,st2.*,st3.*,st4.*,st5.*,st6.*,st7.*,st8.*,st9.* from st0,st1,st2,st3,st4,st5,st6,st7,st8,st9 where st0.ts=st2.ts and st0.ts=st4.ts and st0.ts=st6.ts and st0.ts=st8.ts and st1.ts=st3.ts and st3.ts=st5.ts and st5.ts=st7.ts and st7.ts=st9.ts and st0.id1=st2.id1 and st0.id1=st4.id1 and st0.id1=st6.id1 and st0.id1=st8.id1 and st1.id1=st3.id1 and st3.id1=st5.id1 and st5.id1=st7.id1 and st7.id1=st9.id1; sql_error select st0.*,st1.*,st2.*,st3.*,st4.*,st5.*,st6.*,st7.*,st8.*,st9.* from st0,st1,st2,st3,st4,st5,st6,st7,st8,st9,sta where st0.ts=st2.ts and st0.ts=st4.ts and st0.ts=st6.ts and st0.ts=st8.ts and st1.ts=st3.ts and st3.ts=st5.ts and st5.ts=st7.ts and st7.ts=st9.ts and st0.ts=st1.ts and st0.id1=st2.id1 and st0.id1=st4.id1 and st0.id1=st6.id1 and st0.id1=st8.id1 and st1.id1=st3.id1 and st3.id1=st5.id1 and st5.id1=st7.id1 and st7.id1=st9.id1 and st0.id1=st1.id1 and st0.id1=sta.id1 and st0.ts=sta.ts; - - - - - - system sh/exec.sh -n dnode1 -s stop -x SIGINT diff --git a/tests/script/general/parser/testSuite.sim b/tests/script/general/parser/testSuite.sim index 6265fc3a02..bb220049af 100644 --- a/tests/script/general/parser/testSuite.sim +++ b/tests/script/general/parser/testSuite.sim @@ -39,6 +39,7 @@ run general/parser/slimit1.sim run general/parser/slimit_alter_tags.sim run general/parser/tbnameIn.sim run general/parser/join.sim +#run general/parser/join_multitables.sim run general/parser/join_multivnode.sim run general/parser/join_manyblocks.sim run general/parser/projection_limit_offset.sim From 154866e047569981ece0d618c64c7ea7f0e79d54 Mon Sep 17 00:00:00 2001 From: Elias Soong Date: Sun, 6 Jun 2021 15:58:11 +0800 Subject: [PATCH 44/57] [TD-4533] : describe parameter "size" in "setString" & "setNString". --- documentation20/cn/08.connector/01.java/docs.md | 1 + 1 file changed, 1 insertion(+) diff --git a/documentation20/cn/08.connector/01.java/docs.md b/documentation20/cn/08.connector/01.java/docs.md index 4fc10b542b..ffea252c86 100644 --- a/documentation20/cn/08.connector/01.java/docs.md +++ b/documentation20/cn/08.connector/01.java/docs.md @@ -361,6 +361,7 @@ public void setShort(int columnIndex, ArrayList list) throws SQLException public void setString(int columnIndex, ArrayList list, int size) throws SQLException public void setNString(int columnIndex, ArrayList list, int size) throws SQLException ``` +其中 setString 和 setNString 都要求用户在 size 参数里声明表定义中对应列的列宽。 ### 订阅 From 678cc06fbb968494d7e7c09cb21a422f1ad90759 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sun, 6 Jun 2021 16:09:58 +0800 Subject: [PATCH 45/57] [td-4546]: fix bug in taos_fetch_block while nest query existed. --- src/client/src/tscUtil.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index 46d49bf68d..280d2aa630 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -3463,13 +3463,12 @@ static void tscSubqueryRetrieveCallback(void* param, TAOS_RES* tres, int code) { } pParentSql->cmd.active = pParentSql->cmd.pQueryInfo; - - SSchedMsg schedMsg = {0}; - schedMsg.fp = doRetrieveSubqueryData; - schedMsg.ahandle = (void *)pParentSql; - schedMsg.thandle = (void *)1; - schedMsg.msg = 0; - taosScheduleTask(tscQhandle, &schedMsg); + pParentSql->res.qId = -1; + if (pSql->res.code == TSDB_CODE_SUCCESS) { + (*pSql->fp)(pParentSql->param, pParentSql, pParentSql->res.numOfRows); + } else { + tscAsyncResultOnError(pParentSql); + } } // todo handle the failure From 35beb965a9964868cd88a377d13f97335c886c34 Mon Sep 17 00:00:00 2001 From: dapan1121 <89396746@qq.com> Date: Mon, 7 Jun 2021 10:02:51 +0800 Subject: [PATCH 46/57] add test case --- tests/script/api/batchprepare.c | 182 ++++++++++++++++++++++++++++++-- 1 file changed, 176 insertions(+), 6 deletions(-) diff --git a/tests/script/api/batchprepare.c b/tests/script/api/batchprepare.c index f25becd39e..72bb9471db 100644 --- a/tests/script/api/batchprepare.c +++ b/tests/script/api/batchprepare.c @@ -1458,6 +1458,160 @@ int stmt_funcb_autoctb3(TAOS_STMT *stmt) { + +//1 tables 10 records +int stmt_funcb_autoctb4(TAOS_STMT *stmt) { + struct { + int64_t *ts; + int8_t b[10]; + int8_t v1[10]; + int16_t v2[10]; + int32_t v4[10]; + int64_t v8[10]; + float f4[10]; + double f8[10]; + char bin[10][40]; + } v = {0}; + + v.ts = malloc(sizeof(int64_t) * 1 * 10); + + int *lb = malloc(10 * sizeof(int)); + + TAOS_BIND *tags = calloc(1, sizeof(TAOS_BIND) * 9 * 1); + TAOS_MULTI_BIND *params = calloc(1, sizeof(TAOS_MULTI_BIND) * 1*5); + +// int one_null = 1; + int one_not_null = 0; + + char* is_null = malloc(sizeof(char) * 10); + char* no_null = malloc(sizeof(char) * 10); + + for (int i = 0; i < 10; ++i) { + lb[i] = 40; + no_null[i] = 0; + is_null[i] = (i % 10 == 2) ? 1 : 0; + v.b[i] = (int8_t)(i % 2); + v.v1[i] = (int8_t)((i+1) % 2); + v.v2[i] = (int16_t)i; + v.v4[i] = (int32_t)(i+1); + v.v8[i] = (int64_t)(i+2); + v.f4[i] = (float)(i+3); + v.f8[i] = (double)(i+4); + memset(v.bin[i], '0'+i%10, 40); + } + + for (int i = 0; i < 5; i+=5) { + params[i+0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP; + params[i+0].buffer_length = sizeof(int64_t); + params[i+0].buffer = &v.ts[10*i/10]; + params[i+0].length = NULL; + params[i+0].is_null = no_null; + params[i+0].num = 10; + + params[i+1].buffer_type = TSDB_DATA_TYPE_BOOL; + params[i+1].buffer_length = sizeof(int8_t); + params[i+1].buffer = v.b; + params[i+1].length = NULL; + params[i+1].is_null = is_null; + params[i+1].num = 10; + + params[i+2].buffer_type = TSDB_DATA_TYPE_INT; + params[i+2].buffer_length = sizeof(int32_t); + params[i+2].buffer = v.v4; + params[i+2].length = NULL; + params[i+2].is_null = is_null; + params[i+2].num = 10; + + params[i+3].buffer_type = TSDB_DATA_TYPE_BIGINT; + params[i+3].buffer_length = sizeof(int64_t); + params[i+3].buffer = v.v8; + params[i+3].length = NULL; + params[i+3].is_null = is_null; + params[i+3].num = 10; + + params[i+4].buffer_type = TSDB_DATA_TYPE_DOUBLE; + params[i+4].buffer_length = sizeof(double); + params[i+4].buffer = v.f8; + params[i+4].length = NULL; + params[i+4].is_null = is_null; + params[i+4].num = 10; + } + + int64_t tts = 1591060628000; + for (int i = 0; i < 10; ++i) { + v.ts[i] = tts + i; + } + + + for (int i = 0; i < 1; ++i) { + tags[i+0].buffer_type = TSDB_DATA_TYPE_BOOL; + tags[i+0].buffer = v.b; + tags[i+0].is_null = &one_not_null; + tags[i+0].length = NULL; + + tags[i+1].buffer_type = TSDB_DATA_TYPE_SMALLINT; + tags[i+1].buffer = v.v2; + tags[i+1].is_null = &one_not_null; + tags[i+1].length = NULL; + + tags[i+2].buffer_type = TSDB_DATA_TYPE_FLOAT; + tags[i+2].buffer = v.f4; + tags[i+2].is_null = &one_not_null; + tags[i+2].length = NULL; + + tags[i+3].buffer_type = TSDB_DATA_TYPE_BINARY; + tags[i+3].buffer = v.bin; + tags[i+3].is_null = &one_not_null; + tags[i+3].length = (uintptr_t *)lb; + } + + + unsigned long long starttime = getCurrentTime(); + + char *sql = "insert into ? using stb1 tags(1,?,2,?,4,?,6.0,?,'b') (ts,b,v4,v8,f8) values(?,?,?,?,?)"; + int code = taos_stmt_prepare(stmt, sql, 0); + if (code != 0){ + printf("failed to execute taos_stmt_prepare. code:0x%x\n", code); + exit(1); + } + + int id = 0; + for (int zz = 0; zz < 1; zz++) { + char buf[32]; + sprintf(buf, "m%d", zz); + code = taos_stmt_set_tbname_tags(stmt, buf, tags); + if (code != 0){ + printf("failed to execute taos_stmt_set_tbname_tags. code:0x%x\n", code); + } + + taos_stmt_bind_param_batch(stmt, params + id * 5); + taos_stmt_add_batch(stmt); + } + + if (taos_stmt_execute(stmt) != 0) { + printf("failed to execute insert statement.\n"); + exit(1); + } + + ++id; + + unsigned long long endtime = getCurrentTime(); + printf("insert total %d records, used %u seconds, avg:%u useconds\n", 10, (endtime-starttime)/1000000UL, (endtime-starttime)/(10)); + + free(v.ts); + free(lb); + free(params); + free(is_null); + free(no_null); + free(tags); + + return 0; +} + + + + + //1 tables 10 records int stmt_funcb_autoctb_e1(TAOS_STMT *stmt) { struct { @@ -4351,6 +4505,7 @@ void* runcase(void *par) { (void)idx; + #if 1 prepare(taos, 1, 1); @@ -4531,9 +4686,9 @@ void* runcase(void *par) { stmt = taos_stmt_init(taos); - printf("1t+10r+bm+autoctb start\n"); + printf("1t+10r+bm+autoctb1 start\n"); stmt_funcb_autoctb1(stmt); - printf("1t+10r+bm+autoctb end\n"); + printf("1t+10r+bm+autoctb1 end\n"); printf("check result start\n"); check_result(taos, "m0", 1, 10); printf("check result end\n"); @@ -4546,9 +4701,9 @@ void* runcase(void *par) { stmt = taos_stmt_init(taos); - printf("1t+10r+bm+autoctb start\n"); + printf("1t+10r+bm+autoctb2 start\n"); stmt_funcb_autoctb2(stmt); - printf("1t+10r+bm+autoctb end\n"); + printf("1t+10r+bm+autoctb2 end\n"); printf("check result start\n"); check_result(taos, "m0", 1, 10); printf("check result end\n"); @@ -4562,9 +4717,9 @@ void* runcase(void *par) { stmt = taos_stmt_init(taos); - printf("1t+10r+bm+autoctb start\n"); + printf("1t+10r+bm+autoctb3 start\n"); stmt_funcb_autoctb3(stmt); - printf("1t+10r+bm+autoctb end\n"); + printf("1t+10r+bm+autoctb3 end\n"); printf("check result start\n"); check_result(taos, "m0", 1, 10); printf("check result end\n"); @@ -4573,6 +4728,21 @@ void* runcase(void *par) { #endif +#if 1 + prepare(taos, 1, 0); + + stmt = taos_stmt_init(taos); + + printf("1t+10r+bm+autoctb4 start\n"); + stmt_funcb_autoctb4(stmt); + printf("1t+10r+bm+autoctb4 end\n"); + printf("check result start\n"); + check_result(taos, "m0", 1, 10); + printf("check result end\n"); + taos_stmt_close(stmt); +#endif + + #if 1 prepare(taos, 1, 0); From 9012ccf1625c494aacb3c3203f2243d1b25b7852 Mon Sep 17 00:00:00 2001 From: dapan1121 <89396746@qq.com> Date: Mon, 7 Jun 2021 10:27:13 +0800 Subject: [PATCH 47/57] add error msg --- tests/examples/c/apitest.c | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/tests/examples/c/apitest.c b/tests/examples/c/apitest.c index 2961750efc..06c6d01d0a 100644 --- a/tests/examples/c/apitest.c +++ b/tests/examples/c/apitest.c @@ -342,7 +342,9 @@ void verify_prepare(TAOS* taos) { sql = "insert into m1 values(?,?,?,?,?,?,?,?,?,?)"; code = taos_stmt_prepare(stmt, sql, 0); if (code != 0){ - printf("\033[31mfailed to execute taos_stmt_prepare. code:0x%x\033[0m\n", code); + printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; } v.ts = 1591060628000; for (int i = 0; i < 10; ++i) { @@ -365,8 +367,8 @@ void verify_prepare(TAOS* taos) { taos_stmt_add_batch(stmt); } if (taos_stmt_execute(stmt) != 0) { + printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); taos_stmt_close(stmt); - printf("\033[31mfailed to execute insert statement.\033[0m\n"); return; } taos_stmt_close(stmt); @@ -378,8 +380,8 @@ void verify_prepare(TAOS* taos) { v.v2 = 15; taos_stmt_bind_param(stmt, params + 2); if (taos_stmt_execute(stmt) != 0) { + printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); taos_stmt_close(stmt); - printf("\033[31mfailed to execute select statement.\033[0m\n"); return; } @@ -530,12 +532,16 @@ void verify_prepare2(TAOS* taos) { sql = "insert into ? (ts, b, v1, v2, v4, v8, f4, f8, bin, blob) values(?,?,?,?,?,?,?,?,?,?)"; code = taos_stmt_prepare(stmt, sql, 0); if (code != 0) { - printf("\033[31mfailed to execute taos_stmt_prepare. code:0x%x\033[0m\n", code); + printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; } code = taos_stmt_set_tbname(stmt, "m1"); if (code != 0){ - printf("\033[31mfailed to execute taos_stmt_prepare. code:0x%x\033[0m\n", code); + printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; } int64_t ts = 1591060628000; @@ -569,7 +575,8 @@ void verify_prepare2(TAOS* taos) { taos_stmt_add_batch(stmt); if (taos_stmt_execute(stmt) != 0) { - printf("\033[31mfailed to execute insert statement.\033[0m\n"); + printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); return; } @@ -596,7 +603,8 @@ void verify_prepare2(TAOS* taos) { taos_stmt_bind_param(stmt, qparams); if (taos_stmt_execute(stmt) != 0) { - printf("\033[31mfailed to execute select statement.\033[0m\n"); + printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); return; } @@ -776,12 +784,16 @@ void verify_prepare3(TAOS* taos) { sql = "insert into ? using st1 tags(?,?) values(?,?,?,?,?,?,?,?,?,?)"; code = taos_stmt_prepare(stmt, sql, 0); if (code != 0){ - printf("\033[31mfailed to execute taos_stmt_prepare. code:0x%x\033[0m\n", code); + printf("\033[31mfailed to execute taos_stmt_prepare. error:0x%x\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; } code = taos_stmt_set_tbname_tags(stmt, "m1", tags); if (code != 0){ - printf("\033[31mfailed to execute taos_stmt_prepare. code:0x%x\033[0m\n", code); + printf("\033[31mfailed to execute taos_stmt_prepare. error:0x%x\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); + return; } int64_t ts = 1591060628000; @@ -815,7 +827,8 @@ void verify_prepare3(TAOS* taos) { taos_stmt_add_batch(stmt); if (taos_stmt_execute(stmt) != 0) { - printf("\033[31mfailed to execute insert statement.\033[0m\n"); + printf("\033[31mfailed to execute insert statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); return; } taos_stmt_close(stmt); @@ -842,7 +855,8 @@ void verify_prepare3(TAOS* taos) { taos_stmt_bind_param(stmt, qparams); if (taos_stmt_execute(stmt) != 0) { - printf("\033[31mfailed to execute select statement.\033[0m\n"); + printf("\033[31mfailed to execute select statement.error:%s\033[0m\n", taos_stmt_errstr(stmt)); + taos_stmt_close(stmt); return; } From 9b073353e62d0c1c49a93ba0bba41e4a451f936a Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 7 Jun 2021 12:08:40 +0800 Subject: [PATCH 48/57] [td-225] fix bug found by regression test. --- src/client/src/tscSQLParser.c | 2 +- src/query/src/qAggMain.c | 138 +++------------------ tests/script/general/parser/last_cache.sim | 2 +- 3 files changed, 21 insertions(+), 121 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index d48f193e07..8f915346f1 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -2161,7 +2161,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col case TSDB_FUNC_STDDEV: case TSDB_FUNC_LEASTSQR: { // 1. valid the number of parameters - int32_t numOfParams = taosArrayGetSize(pItem->pNode->pParam); + int32_t numOfParams = (pItem->pNode->pParam == NULL)? 0: taosArrayGetSize(pItem->pNode->pParam); if (pItem->pNode->pParam == NULL || (functionId != TSDB_FUNC_LEASTSQR && functionId != TSDB_FUNC_DERIVATIVE && numOfParams != 1) || ((functionId == TSDB_FUNC_LEASTSQR || functionId == TSDB_FUNC_DERIVATIVE) && numOfParams != 3)) { diff --git a/src/query/src/qAggMain.c b/src/query/src/qAggMain.c index dc1829930c..baef9a57a4 100644 --- a/src/query/src/qAggMain.c +++ b/src/query/src/qAggMain.c @@ -234,7 +234,6 @@ int32_t getResultDataInfo(int32_t dataType, int32_t dataBytes, int32_t functionI return TSDB_CODE_SUCCESS; } - //TODO twa function definit error. if (isSuperTable) { if (functionId == TSDB_FUNC_MIN || functionId == TSDB_FUNC_MAX) { *type = TSDB_DATA_TYPE_BINARY; @@ -3420,18 +3419,6 @@ static bool diff_function_setup(SQLFunctionCtx *pCtx) { return false; } -#define DIFF_IMPL(ctx, d, type) \ - do { \ - if ((ctx)->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { \ - (ctx)->param[1].nType = (ctx)->inputType; \ - *(type *)&(ctx)->param[1].i64 = *(type *)(d); \ - } else { \ - *(type *)(ctx)->pOutput = *(type *)(d) - (*(type *)(&(ctx)->param[1].i64)); \ - *(type *)(&(ctx)->param[1].i64) = *(type *)(d); \ - *(int64_t *)(ctx)->ptsOutputBuf = GET_TS_DATA(ctx, index); \ - } \ - } while (0); - static bool deriv_function_setup(SQLFunctionCtx *pCtx) { if (!function_setup(pCtx)) { return false; @@ -3480,7 +3467,6 @@ static void deriv_function(SQLFunctionCtx *pCtx) { if (pDerivInfo->ignoreNegative && *pOutput < 0) { } else { *pTimestamp = tsList[i]; - pOutput += 1; pTimestamp += 1; } @@ -3508,7 +3494,6 @@ static void deriv_function(SQLFunctionCtx *pCtx) { if (pDerivInfo->ignoreNegative && *pOutput < 0) { } else { *pTimestamp = tsList[i]; - pOutput += 1; pTimestamp += 1; } @@ -3535,7 +3520,6 @@ static void deriv_function(SQLFunctionCtx *pCtx) { if (pDerivInfo->ignoreNegative && *pOutput < 0) { } else { *pTimestamp = tsList[i]; - pOutput += 1; pTimestamp += 1; } @@ -3563,7 +3547,6 @@ static void deriv_function(SQLFunctionCtx *pCtx) { if (pDerivInfo->ignoreNegative && *pOutput < 0) { } else { *pTimestamp = tsList[i]; - pOutput += 1; pTimestamp += 1; } @@ -3590,7 +3573,6 @@ static void deriv_function(SQLFunctionCtx *pCtx) { if (pDerivInfo->ignoreNegative && *pOutput < 0) { } else { *pTimestamp = tsList[i]; - pOutput += 1; pTimestamp += 1; } @@ -3646,18 +3628,6 @@ static void deriv_function(SQLFunctionCtx *pCtx) { } } -#define DIFF_IMPL(ctx, d, type) \ - do { \ - if ((ctx)->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { \ - (ctx)->param[1].nType = (ctx)->inputType; \ - *(type *)&(ctx)->param[1].i64 = *(type *)(d); \ - } else { \ - *(type *)(ctx)->pOutput = *(type *)(d) - (*(type *)(&(ctx)->param[1].i64)); \ - *(type *)(&(ctx)->param[1].i64) = *(type *)(d); \ - *(int64_t *)(ctx)->ptsOutputBuf = GET_TS_DATA(ctx, index); \ - } \ - } while (0); - #define DIFF_IMPL(ctx, d, type) \ do { \ if ((ctx)->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { \ @@ -3693,20 +3663,10 @@ static void diff_function(SQLFunctionCtx *pCtx) { continue; } - if (pCtx->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet - pCtx->param[1].i64 = pData[i]; - pCtx->param[1].nType = pCtx->inputType; - } else if ((i == 0 && pCtx->order == TSDB_ORDER_ASC) || (i == pCtx->size - 1 && pCtx->order == TSDB_ORDER_DESC)) { - *pOutput = (int32_t)(pData[i] - pCtx->param[1].i64); - *pTimestamp = tsList[i]; - - pOutput += 1; - pTimestamp += 1; - } else { + if (pCtx->param[1].nType != INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet *pOutput = (int32_t)(pData[i] - pCtx->param[1].i64); // direct previous may be null *pTimestamp = tsList[i]; - - pOutput += 1; + pOutput += 1; pTimestamp += 1; } @@ -3725,20 +3685,10 @@ static void diff_function(SQLFunctionCtx *pCtx) { continue; } - if (pCtx->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet - pCtx->param[1].i64 = pData[i]; - pCtx->param[1].nType = pCtx->inputType; - } else if ((i == 0 && pCtx->order == TSDB_ORDER_ASC) || (i == pCtx->size - 1 && pCtx->order == TSDB_ORDER_DESC)) { - *pOutput = pData[i] - pCtx->param[1].i64; + if (pCtx->param[1].nType != INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet + *pOutput = (int32_t)(pData[i] - pCtx->param[1].i64); // direct previous may be null *pTimestamp = tsList[i]; - - pOutput += 1; - pTimestamp += 1; - } else { - *pOutput = pData[i] - pCtx->param[1].i64; - *pTimestamp = tsList[i]; - - pOutput += 1; + pOutput += 1; pTimestamp += 1; } @@ -3757,22 +3707,14 @@ static void diff_function(SQLFunctionCtx *pCtx) { continue; } - if (pCtx->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet - pCtx->param[1].dKey = pData[i]; - pCtx->param[1].nType = pCtx->inputType; - } else if ((i == 0 && pCtx->order == TSDB_ORDER_ASC) || (i == pCtx->size - 1 && pCtx->order == TSDB_ORDER_DESC)) { - *pOutput = pData[i] - pCtx->param[1].dKey; + if (pCtx->param[1].nType != INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet + *pOutput = (int32_t)(pData[i] - pCtx->param[1].i64); // direct previous may be null *pTimestamp = tsList[i]; - pOutput += 1; - pTimestamp += 1; - } else { - *pOutput = pData[i] - pCtx->param[1].dKey; - *pTimestamp = tsList[i]; - pOutput += 1; + pOutput += 1; pTimestamp += 1; } - pCtx->param[1].dKey = pData[i]; + pCtx->param[1].i64 = pData[i]; pCtx->param[1].nType = pCtx->inputType; notNullElems++; } @@ -3787,25 +3729,14 @@ static void diff_function(SQLFunctionCtx *pCtx) { continue; } - if (pCtx->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet - pCtx->param[1].dKey = pData[i]; - pCtx->param[1].nType = pCtx->inputType; - } else if ((i == 0 && pCtx->order == TSDB_ORDER_ASC) || (i == pCtx->size - 1 && pCtx->order == TSDB_ORDER_DESC)) { - *pOutput = (float)(pData[i] - pCtx->param[1].dKey); + if (pCtx->param[1].nType != INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet + *pOutput = (int32_t)(pData[i] - pCtx->param[1].i64); // direct previous may be null *pTimestamp = tsList[i]; - - pOutput += 1; - pTimestamp += 1; - } else { - *pOutput = (float)(pData[i] - pCtx->param[1].dKey); - *pTimestamp = tsList[i]; - - pOutput += 1; + pOutput += 1; pTimestamp += 1; } - // keep the last value, the remain may be all null - pCtx->param[1].dKey = pData[i]; + pCtx->param[1].i64 = pData[i]; pCtx->param[1].nType = pCtx->inputType; notNullElems++; } @@ -3820,19 +3751,10 @@ static void diff_function(SQLFunctionCtx *pCtx) { continue; } - if (pCtx->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet - pCtx->param[1].i64 = pData[i]; - pCtx->param[1].nType = pCtx->inputType; - } else if ((i == 0 && pCtx->order == TSDB_ORDER_ASC) || (i == pCtx->size - 1 && pCtx->order == TSDB_ORDER_DESC)) { - *pOutput = (int16_t)(pData[i] - pCtx->param[1].i64); + if (pCtx->param[1].nType != INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet + *pOutput = (int32_t)(pData[i] - pCtx->param[1].i64); // direct previous may be null *pTimestamp = tsList[i]; - pOutput += 1; - pTimestamp += 1; - } else { - *pOutput = (int16_t)(pData[i] - pCtx->param[1].i64); - *pTimestamp = tsList[i]; - - pOutput += 1; + pOutput += 1; pTimestamp += 1; } @@ -3851,20 +3773,10 @@ static void diff_function(SQLFunctionCtx *pCtx) { continue; } - if (pCtx->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet - pCtx->param[1].i64 = pData[i]; - pCtx->param[1].nType = pCtx->inputType; - } else if ((i == 0 && pCtx->order == TSDB_ORDER_ASC) || (i == pCtx->size - 1 && pCtx->order == TSDB_ORDER_DESC)) { - *pOutput = (int8_t)(pData[i] - pCtx->param[1].i64); + if (pCtx->param[1].nType != INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet + *pOutput = (int32_t)(pData[i] - pCtx->param[1].i64); // direct previous may be null *pTimestamp = tsList[i]; - - pOutput += 1; - pTimestamp += 1; - } else { - *pOutput = (int8_t)(pData[i] - pCtx->param[1].i64); - *pTimestamp = tsList[i]; - - pOutput += 1; + pOutput += 1; pTimestamp += 1; } @@ -3892,18 +3804,6 @@ static void diff_function(SQLFunctionCtx *pCtx) { } } -#define DIFF_IMPL(ctx, d, type) \ - do { \ - if ((ctx)->param[1].nType == INITIAL_VALUE_NOT_ASSIGNED) { \ - (ctx)->param[1].nType = (ctx)->inputType; \ - *(type *)&(ctx)->param[1].i64 = *(type *)(d); \ - } else { \ - *(type *)(ctx)->pOutput = *(type *)(d) - (*(type *)(&(ctx)->param[1].i64)); \ - *(type *)(&(ctx)->param[1].i64) = *(type *)(d); \ - *(int64_t *)(ctx)->ptsOutputBuf = GET_TS_DATA(ctx, index); \ - } \ - } while (0); - static void diff_function_f(SQLFunctionCtx *pCtx, int32_t index) { char *pData = GET_INPUT_DATA(pCtx, index); if (pCtx->hasNull && isNull(pData, pCtx->inputType)) { diff --git a/tests/script/general/parser/last_cache.sim b/tests/script/general/parser/last_cache.sim index 9d7da9ddba..4b3285871b 100644 --- a/tests/script/general/parser/last_cache.sim +++ b/tests/script/general/parser/last_cache.sim @@ -9,7 +9,7 @@ sql connect print ======================== dnode1 start $db = testdb - +sql drop database if exists $db sql create database $db cachelast 2 sql use $db From 0c1d5f4a4c20ffbc669a2ee30374b8cbc8e7acec Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 7 Jun 2021 13:10:59 +0800 Subject: [PATCH 49/57] [td-225] fix compiler error. --- src/query/src/qAggMain.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/query/src/qAggMain.c b/src/query/src/qAggMain.c index baef9a57a4..ced83d539b 100644 --- a/src/query/src/qAggMain.c +++ b/src/query/src/qAggMain.c @@ -3499,7 +3499,7 @@ static void deriv_function(SQLFunctionCtx *pCtx) { } } - pDerivInfo->prevValue = pData[i]; + pDerivInfo->prevValue = (double) pData[i]; pDerivInfo->prevTs = tsList[i]; notNullElems++; } @@ -3686,7 +3686,7 @@ static void diff_function(SQLFunctionCtx *pCtx) { } if (pCtx->param[1].nType != INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet - *pOutput = (int32_t)(pData[i] - pCtx->param[1].i64); // direct previous may be null + *pOutput = pData[i] - pCtx->param[1].i64; // direct previous may be null *pTimestamp = tsList[i]; pOutput += 1; pTimestamp += 1; @@ -3708,13 +3708,13 @@ static void diff_function(SQLFunctionCtx *pCtx) { } if (pCtx->param[1].nType != INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet - *pOutput = (int32_t)(pData[i] - pCtx->param[1].i64); // direct previous may be null + *pOutput = pData[i] - pCtx->param[1].d64; // direct previous may be null *pTimestamp = tsList[i]; pOutput += 1; pTimestamp += 1; } - pCtx->param[1].i64 = pData[i]; + pCtx->param[1].d64 = pData[i]; pCtx->param[1].nType = pCtx->inputType; notNullElems++; } @@ -3730,13 +3730,13 @@ static void diff_function(SQLFunctionCtx *pCtx) { } if (pCtx->param[1].nType != INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet - *pOutput = (int32_t)(pData[i] - pCtx->param[1].i64); // direct previous may be null + *pOutput = pData[i] - pCtx->param[1].d64; // direct previous may be null *pTimestamp = tsList[i]; pOutput += 1; pTimestamp += 1; } - pCtx->param[1].i64 = pData[i]; + pCtx->param[1].d64 = pData[i]; pCtx->param[1].nType = pCtx->inputType; notNullElems++; } @@ -3752,7 +3752,7 @@ static void diff_function(SQLFunctionCtx *pCtx) { } if (pCtx->param[1].nType != INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet - *pOutput = (int32_t)(pData[i] - pCtx->param[1].i64); // direct previous may be null + *pOutput = (int16_t)(pData[i] - pCtx->param[1].i64); // direct previous may be null *pTimestamp = tsList[i]; pOutput += 1; pTimestamp += 1; @@ -3774,7 +3774,7 @@ static void diff_function(SQLFunctionCtx *pCtx) { } if (pCtx->param[1].nType != INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet - *pOutput = (int32_t)(pData[i] - pCtx->param[1].i64); // direct previous may be null + *pOutput = (int8_t)(pData[i] - pCtx->param[1].i64); // direct previous may be null *pTimestamp = tsList[i]; pOutput += 1; pTimestamp += 1; From 39c7aced4989ae4f159361858b70277e6aa47f07 Mon Sep 17 00:00:00 2001 From: dapan1121 <89396746@qq.com> Date: Mon, 7 Jun 2021 13:15:57 +0800 Subject: [PATCH 50/57] fix bug --- tests/examples/c/apitest.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/examples/c/apitest.c b/tests/examples/c/apitest.c index 06c6d01d0a..44d9d0cee6 100644 --- a/tests/examples/c/apitest.c +++ b/tests/examples/c/apitest.c @@ -784,14 +784,14 @@ void verify_prepare3(TAOS* taos) { sql = "insert into ? using st1 tags(?,?) values(?,?,?,?,?,?,?,?,?,?)"; code = taos_stmt_prepare(stmt, sql, 0); if (code != 0){ - printf("\033[31mfailed to execute taos_stmt_prepare. error:0x%x\033[0m\n", taos_stmt_errstr(stmt)); + printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); taos_stmt_close(stmt); return; } code = taos_stmt_set_tbname_tags(stmt, "m1", tags); if (code != 0){ - printf("\033[31mfailed to execute taos_stmt_prepare. error:0x%x\033[0m\n", taos_stmt_errstr(stmt)); + printf("\033[31mfailed to execute taos_stmt_prepare. error:%s\033[0m\n", taos_stmt_errstr(stmt)); taos_stmt_close(stmt); return; } From 04f1ffad9b2efee169e4f844159c8a7b099d7c6a Mon Sep 17 00:00:00 2001 From: zyyang <69311263+zyyang-taosdata@users.noreply.github.com> Date: Mon, 7 Jun 2021 14:14:06 +0800 Subject: [PATCH 51/57] [TD-4480]: fix DatabaseMetaData get wrong column value (#6381) * [TD-4480]: fix DatabaseMetaData has wrong column index bug * change * change * change * change * change * change * change * change * change * change * change * change * change * change test cases * change test cases * change * change * change * change * change * change jdbc version * change * change * change * change * change * change * change * change --- cmake/install.inc | 2 +- src/connector/jdbc/CMakeLists.txt | 3 +- src/connector/jdbc/deploy-pom.xml | 2 +- src/connector/jdbc/pom.xml | 2 +- .../jdbc/AbstractDatabaseMetaData.java | 948 +++++++------- .../jdbc/AbstractParameterMetaData.java | 23 + .../com/taosdata/jdbc/AbstractResultSet.java | 10 +- .../jdbc/DatabaseMetaDataResultSet.java | 922 +------------- .../java/com/taosdata/jdbc/TSDBConstants.java | 65 +- .../com/taosdata/jdbc/TSDBJNIConnector.java | 107 +- .../taosdata/jdbc/TSDBPreparedStatement.java | 894 +++++++------ .../java/com/taosdata/jdbc/TSDBResultSet.java | 109 +- .../taosdata/jdbc/TSDBResultSetMetaData.java | 1 + .../taosdata/jdbc/TSDBResultSetRowData.java | 270 ++-- .../java/com/taosdata/jdbc/TSDBStatement.java | 11 +- .../java/com/taosdata/jdbc/utils/Utils.java | 11 +- .../com/taosdata/jdbc/TSDBConnectionTest.java | 30 +- .../jdbc/TSDBDatabaseMetaDataTest.java | 243 +++- .../taosdata/jdbc/TSDBJNIConnectorTest.java | 6 +- .../jdbc/TSDBParameterMetaDataTest.java | 44 +- .../jdbc/TSDBPreparedStatementTest.java | 1109 +++++++++-------- .../com/taosdata/jdbc/TSDBResultSetTest.java | 10 +- .../com/taosdata/jdbc/TSDBStatementTest.java | 3 - .../jdbc/rs/RestfulDatabaseMetaDataTest.java | 235 +++- .../jdbc/rs/RestfulParameterMetaDataTest.java | 27 +- 25 files changed, 2432 insertions(+), 2655 deletions(-) diff --git a/cmake/install.inc b/cmake/install.inc index f8b3b7c3c6..63764348d3 100755 --- a/cmake/install.inc +++ b/cmake/install.inc @@ -32,7 +32,7 @@ ELSEIF (TD_WINDOWS) #INSTALL(TARGETS taos RUNTIME DESTINATION driver) #INSTALL(TARGETS shell RUNTIME DESTINATION .) IF (TD_MVN_INSTALLED) - INSTALL(FILES ${LIBRARY_OUTPUT_PATH}/taos-jdbcdriver-2.0.29.jar DESTINATION connector/jdbc) + INSTALL(FILES ${LIBRARY_OUTPUT_PATH}/taos-jdbcdriver-2.0.30.jar DESTINATION connector/jdbc) ENDIF () ELSEIF (TD_DARWIN) SET(TD_MAKE_INSTALL_SH "${TD_COMMUNITY_DIR}/packaging/tools/make_install.sh") diff --git a/src/connector/jdbc/CMakeLists.txt b/src/connector/jdbc/CMakeLists.txt index 61e976cb18..a04902a422 100644 --- a/src/connector/jdbc/CMakeLists.txt +++ b/src/connector/jdbc/CMakeLists.txt @@ -8,10 +8,9 @@ IF (TD_MVN_INSTALLED) ADD_CUSTOM_COMMAND(OUTPUT ${JDBC_CMD_NAME} POST_BUILD COMMAND mvn -Dmaven.test.skip=true install -f ${CMAKE_CURRENT_SOURCE_DIR}/pom.xml - COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/target/taos-jdbcdriver-2.0.29.jar ${LIBRARY_OUTPUT_PATH} + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/target/taos-jdbcdriver-2.0.30.jar ${LIBRARY_OUTPUT_PATH} COMMAND mvn -Dmaven.test.skip=true clean -f ${CMAKE_CURRENT_SOURCE_DIR}/pom.xml COMMENT "build jdbc driver") ADD_CUSTOM_TARGET(${JDBC_TARGET_NAME} ALL WORKING_DIRECTORY ${EXECUTABLE_OUTPUT_PATH} DEPENDS ${JDBC_CMD_NAME}) ENDIF () - diff --git a/src/connector/jdbc/deploy-pom.xml b/src/connector/jdbc/deploy-pom.xml index 968a9bf470..657645d46c 100755 --- a/src/connector/jdbc/deploy-pom.xml +++ b/src/connector/jdbc/deploy-pom.xml @@ -5,7 +5,7 @@ com.taosdata.jdbc taos-jdbcdriver - 2.0.29 + 2.0.30 jar JDBCDriver diff --git a/src/connector/jdbc/pom.xml b/src/connector/jdbc/pom.xml index e350e9738e..96e17bcd9b 100644 --- a/src/connector/jdbc/pom.xml +++ b/src/connector/jdbc/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.taosdata.jdbc taos-jdbcdriver - 2.0.29 + 2.0.30 jar JDBCDriver https://github.com/taosdata/TDengine/tree/master/src/connector/jdbc diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDatabaseMetaData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDatabaseMetaData.java index 5dcaa77ebd..48811d30a6 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDatabaseMetaData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractDatabaseMetaData.java @@ -12,6 +12,9 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da private final static int DRIVER_MAJAR_VERSION = 2; private final static int DRIVER_MINOR_VERSION = 0; + private String precision = "ms"; + private String database; + public boolean allProceduresAreCallable() throws SQLException { return false; } @@ -493,102 +496,105 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da public abstract ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) throws SQLException; + private List buildGetTablesColumnMetaDataList() { + List columnMetaDataList = new ArrayList<>(); + columnMetaDataList.add(buildTableCatalogMeta(1)); // 1. TABLE_CAT + columnMetaDataList.add(buildTableSchemaMeta(2)); // 2. TABLE_SCHEM + columnMetaDataList.add(buildTableNameMeta(3)); // 3. TABLE_NAME + columnMetaDataList.add(buildTableTypeMeta(4)); // 4. TABLE_TYPE + columnMetaDataList.add(buildRemarksMeta(5)); // 5. remarks + columnMetaDataList.add(buildTypeCatMeta(6)); // 6. TYPE_CAT + columnMetaDataList.add(buildTypeSchemaMeta(7)); // 7. TYPE_SCHEM + columnMetaDataList.add(buildTypeNameMeta(8)); // 8. TYPE_NAME + columnMetaDataList.add(buildSelfReferencingColName(9)); // 9. SELF_REFERENCING_COL_NAME + columnMetaDataList.add(buildRefGenerationMeta(10)); // 10. REF_GENERATION + return columnMetaDataList; + } + + private ColumnMetaData buildTypeCatMeta(int colIndex) { + ColumnMetaData col6 = new ColumnMetaData(); + col6.setColIndex(colIndex); + col6.setColName("TYPE_CAT"); + col6.setColType(Types.NCHAR); + return col6; + } + + private ColumnMetaData buildTypeSchemaMeta(int colIndex) { + ColumnMetaData col7 = new ColumnMetaData(); + col7.setColIndex(colIndex); + col7.setColName("TYPE_SCHEM"); + col7.setColType(Types.NCHAR); + return col7; + } + + private ColumnMetaData buildTypeNameMeta(int colIndex) { + ColumnMetaData col8 = new ColumnMetaData(); + col8.setColIndex(colIndex); + col8.setColName("TYPE_NAME"); + col8.setColType(Types.NCHAR); + return col8; + } + + private ColumnMetaData buildSelfReferencingColName(int colIndex) { + ColumnMetaData col9 = new ColumnMetaData(); + col9.setColIndex(colIndex); + col9.setColName("SELF_REFERENCING_COL_NAME"); + col9.setColType(Types.NCHAR); + return col9; + } + + private ColumnMetaData buildRefGenerationMeta(int colIndex) { + ColumnMetaData col10 = new ColumnMetaData(); + col10.setColIndex(colIndex); + col10.setColName("REF_GENERATION"); + col10.setColType(Types.NCHAR); + return col10; + } + protected ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types, Connection connection) throws SQLException { + if (catalog == null || catalog.isEmpty()) + return null; + if (!isAvailableCatalog(connection, catalog)) + return new EmptyResultSet(); + + DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); + // set column metadata list + resultSet.setColumnMetaDataList(buildGetTablesColumnMetaDataList()); + // set row data + List rowDataList = new ArrayList<>(); try (Statement stmt = connection.createStatement()) { - if (catalog == null || catalog.isEmpty()) - return null; - - ResultSet databases = stmt.executeQuery("show databases"); - String dbname = null; - while (databases.next()) { - dbname = databases.getString("name"); - if (dbname.equalsIgnoreCase(catalog)) - break; - } - databases.close(); - if (dbname == null) - return null; - - stmt.execute("use " + dbname); - DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); - List columnMetaDataList = new ArrayList<>(); - ColumnMetaData col1 = new ColumnMetaData(); - col1.setColIndex(1); - col1.setColName("TABLE_CAT"); - col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col1); - ColumnMetaData col2 = new ColumnMetaData(); - col2.setColIndex(2); - col2.setColName("TABLE_SCHEM"); - col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col2); - ColumnMetaData col3 = new ColumnMetaData(); - col3.setColIndex(3); - col3.setColName("TABLE_NAME"); - col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col3); - ColumnMetaData col4 = new ColumnMetaData(); - col4.setColIndex(4); - col4.setColName("TABLE_TYPE"); - col4.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col4); - ColumnMetaData col5 = new ColumnMetaData(); - col5.setColIndex(5); - col5.setColName("REMARKS"); - col5.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col5); - ColumnMetaData col6 = new ColumnMetaData(); - col6.setColIndex(6); - col6.setColName("TYPE_CAT"); - col6.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col6); - ColumnMetaData col7 = new ColumnMetaData(); - col7.setColIndex(7); - col7.setColName("TYPE_SCHEM"); - col7.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col7); - ColumnMetaData col8 = new ColumnMetaData(); - col8.setColIndex(8); - col8.setColName("TYPE_NAME"); - col8.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col8); - ColumnMetaData col9 = new ColumnMetaData(); - col9.setColIndex(9); - col9.setColName("SELF_REFERENCING_COL_NAME"); - col9.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col9); - ColumnMetaData col10 = new ColumnMetaData(); - col10.setColIndex(10); - col10.setColName("REF_GENERATION"); - col10.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col10); - resultSet.setColumnMetaDataList(columnMetaDataList); - - List rowDataList = new ArrayList<>(); + stmt.execute("use " + catalog); ResultSet tables = stmt.executeQuery("show tables"); while (tables.next()) { TSDBResultSetRowData rowData = new TSDBResultSetRowData(10); - rowData.setString(0, dbname); //table_cat - rowData.setString(1, null); //TABLE_SCHEM - rowData.setString(2, tables.getString("table_name")); //TABLE_NAME - rowData.setString(3, "TABLE"); //TABLE_TYPE - rowData.setString(4, ""); //REMARKS + rowData.setStringValue(1, catalog); //TABLE_CAT + rowData.setStringValue(2, null); //TABLE_SCHEM + rowData.setStringValue(3, tables.getString("table_name")); //TABLE_NAME + rowData.setStringValue(4, "TABLE"); //TABLE_TYPE + rowData.setStringValue(5, ""); //REMARKS rowDataList.add(rowData); } - ResultSet stables = stmt.executeQuery("show stables"); while (stables.next()) { TSDBResultSetRowData rowData = new TSDBResultSetRowData(10); - rowData.setString(0, dbname); //TABLE_CAT - rowData.setString(1, null); //TABLE_SCHEM - rowData.setString(2, stables.getString("name")); //TABLE_NAME - rowData.setString(3, "TABLE"); //TABLE_TYPE - rowData.setString(4, "STABLE"); //REMARKS + rowData.setStringValue(1, catalog); //TABLE_CAT + rowData.setStringValue(2, null); //TABLE_SCHEM + rowData.setStringValue(3, stables.getString("name")); //TABLE_NAME + rowData.setStringValue(4, "TABLE"); //TABLE_TYPE + rowData.setStringValue(5, "STABLE"); //REMARKS rowDataList.add(rowData); } resultSet.setRowDataList(rowDataList); - return resultSet; } + return resultSet; + } + + private ColumnMetaData buildTableTypeMeta(int colIndex) { + ColumnMetaData col4 = new ColumnMetaData(); + col4.setColIndex(colIndex); + col4.setColName("TABLE_TYPE"); + col4.setColType(Types.NCHAR); + return col4; } public ResultSet getSchemas() throws SQLException { @@ -597,25 +603,24 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da public abstract ResultSet getCatalogs() throws SQLException; + private List buildTableTypesColumnMetadataList() { + List columnMetaDataList = new ArrayList<>(); + columnMetaDataList.add(buildTableTypeMeta(1)); + return columnMetaDataList; + } + public ResultSet getTableTypes() throws SQLException { DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); // set up ColumnMetaDataList - List columnMetaDataList = new ArrayList<>(); - ColumnMetaData colMetaData = new ColumnMetaData(); - colMetaData.setColIndex(0); - colMetaData.setColName("TABLE_TYPE"); - colMetaData.setColSize(10); - colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(colMetaData); - resultSet.setColumnMetaDataList(columnMetaDataList); + resultSet.setColumnMetaDataList(buildTableTypesColumnMetadataList()); // set up rowDataList List rowDataList = new ArrayList<>(); TSDBResultSetRowData rowData = new TSDBResultSetRowData(1); - rowData.setString(0, "TABLE"); + rowData.setStringValue(1, "TABLE"); rowDataList.add(rowData); rowData = new TSDBResultSetRowData(1); - rowData.setString(0, "STABLE"); + rowData.setStringValue(1, "STABLE"); rowDataList.add(rowData); resultSet.setRowDataList(rowDataList); @@ -625,278 +630,330 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da public abstract ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException; protected ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern, Connection conn) { + if (catalog == null || catalog.isEmpty()) + return null; + if (!isAvailableCatalog(conn, catalog)) + return new EmptyResultSet(); + + DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); + // set up ColumnMetaDataList + resultSet.setColumnMetaDataList(buildGetColumnsColumnMetaDataList()); + // set up rowDataList + List rowDataList = new ArrayList<>(); try (Statement stmt = conn.createStatement()) { - if (catalog == null || catalog.isEmpty()) - return null; - - ResultSet databases = stmt.executeQuery("show databases"); - String dbname = null; - while (databases.next()) { - dbname = databases.getString("name"); - if (dbname.equalsIgnoreCase(catalog)) - break; - } - databases.close(); - if (dbname == null) - return null; - - stmt.execute("use " + dbname); - DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); - // set up ColumnMetaDataList - - List columnMetaDataList = new ArrayList<>(); - // TABLE_CAT - ColumnMetaData col1 = new ColumnMetaData(); - col1.setColIndex(1); - col1.setColName("TABLE_CAT"); - col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col1); - // TABLE_SCHEM - ColumnMetaData col2 = new ColumnMetaData(); - col2.setColIndex(2); - col2.setColName("TABLE_SCHEM"); - col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col2); - // TABLE_NAME - ColumnMetaData col3 = new ColumnMetaData(); - col3.setColIndex(3); - col3.setColName("TABLE_NAME"); - col3.setColSize(193); - col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col3); - // COLUMN_NAME - ColumnMetaData col4 = new ColumnMetaData(); - col4.setColIndex(4); - col4.setColName("COLUMN_NAME"); - col4.setColSize(65); - col4.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col4); - // DATA_TYPE - ColumnMetaData col5 = new ColumnMetaData(); - col5.setColIndex(5); - col5.setColName("DATA_TYPE"); - col5.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col5); - // TYPE_NAME - ColumnMetaData col6 = new ColumnMetaData(); - col6.setColIndex(6); - col6.setColName("TYPE_NAME"); - col6.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col6); - // COLUMN_SIZE - ColumnMetaData col7 = new ColumnMetaData(); - col7.setColIndex(7); - col7.setColName("COLUMN_SIZE"); - col7.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col7); - // BUFFER_LENGTH, not used - ColumnMetaData col8 = new ColumnMetaData(); - col8.setColIndex(8); - col8.setColName("BUFFER_LENGTH"); - columnMetaDataList.add(col8); - // DECIMAL_DIGITS - ColumnMetaData col9 = new ColumnMetaData(); - col9.setColIndex(9); - col9.setColName("DECIMAL_DIGITS"); - col9.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col9); - // add NUM_PREC_RADIX - ColumnMetaData col10 = new ColumnMetaData(); - col10.setColIndex(10); - col10.setColName("NUM_PREC_RADIX"); - col10.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col10); - // NULLABLE - ColumnMetaData col11 = new ColumnMetaData(); - col11.setColIndex(11); - col11.setColName("NULLABLE"); - col11.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col11); - // REMARKS - ColumnMetaData col12 = new ColumnMetaData(); - col12.setColIndex(12); - col12.setColName("REMARKS"); - col12.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col12); - // COLUMN_DEF - ColumnMetaData col13 = new ColumnMetaData(); - col13.setColIndex(13); - col13.setColName("COLUMN_DEF"); - col13.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col13); - //SQL_DATA_TYPE - ColumnMetaData col14 = new ColumnMetaData(); - col14.setColIndex(14); - col14.setColName("SQL_DATA_TYPE"); - col14.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col14); - //SQL_DATETIME_SUB - ColumnMetaData col15 = new ColumnMetaData(); - col15.setColIndex(15); - col15.setColName("SQL_DATETIME_SUB"); - col15.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col15); - //CHAR_OCTET_LENGTH - ColumnMetaData col16 = new ColumnMetaData(); - col16.setColIndex(16); - col16.setColName("CHAR_OCTET_LENGTH"); - col16.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col16); - //ORDINAL_POSITION - ColumnMetaData col17 = new ColumnMetaData(); - col17.setColIndex(17); - col17.setColName("ORDINAL_POSITION"); - col17.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col17); - // IS_NULLABLE - ColumnMetaData col18 = new ColumnMetaData(); - col18.setColIndex(18); - col18.setColName("IS_NULLABLE"); - col18.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col18); - //SCOPE_CATALOG - ColumnMetaData col19 = new ColumnMetaData(); - col19.setColIndex(19); - col19.setColName("SCOPE_CATALOG"); - col19.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col19); - //SCOPE_SCHEMA - ColumnMetaData col20 = new ColumnMetaData(); - col20.setColIndex(20); - col20.setColName("SCOPE_SCHEMA"); - col20.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col20); - //SCOPE_TABLE - ColumnMetaData col21 = new ColumnMetaData(); - col21.setColIndex(21); - col21.setColName("SCOPE_TABLE"); - col21.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col21); - //SOURCE_DATA_TYPE - ColumnMetaData col22 = new ColumnMetaData(); - col22.setColIndex(22); - col22.setColName("SOURCE_DATA_TYPE"); - col22.setColType(TSDBConstants.TSDB_DATA_TYPE_SMALLINT); - columnMetaDataList.add(col22); - //IS_AUTOINCREMENT - ColumnMetaData col23 = new ColumnMetaData(); - col23.setColIndex(23); - col23.setColName("IS_AUTOINCREMENT"); - col23.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col23); - //IS_GENERATEDCOLUMN - ColumnMetaData col24 = new ColumnMetaData(); - col24.setColIndex(24); - col24.setColName("IS_GENERATEDCOLUMN"); - col24.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col24); - - resultSet.setColumnMetaDataList(columnMetaDataList); - // set up rowDataList - ResultSet rs = stmt.executeQuery("describe " + dbname + "." + tableNamePattern); - List rowDataList = new ArrayList<>(); - int index = 0; + ResultSet rs = stmt.executeQuery("describe " + catalog + "." + tableNamePattern); + int rowIndex = 0; while (rs.next()) { TSDBResultSetRowData rowData = new TSDBResultSetRowData(24); // set TABLE_CAT - rowData.setString(0, dbname); + rowData.setStringValue(1, catalog); + // set TABLE_SCHEM + rowData.setStringValue(2, null); // set TABLE_NAME - rowData.setString(2, tableNamePattern); + rowData.setStringValue(3, tableNamePattern); // set COLUMN_NAME - rowData.setString(3, rs.getString("Field")); + rowData.setStringValue(4, rs.getString("Field")); // set DATA_TYPE String typeName = rs.getString("Type"); - rowData.setInt(4, getDataType(typeName)); + rowData.setIntValue(5, TSDBConstants.typeName2JdbcType(typeName)); // set TYPE_NAME - rowData.setString(5, typeName); + rowData.setStringValue(6, typeName); // set COLUMN_SIZE int length = rs.getInt("Length"); - rowData.setInt(6, getColumnSize(typeName, length)); + rowData.setIntValue(7, calculateColumnSize(typeName, precision, length)); + // set BUFFER LENGTH + rowData.setStringValue(8, null); // set DECIMAL_DIGITS - rowData.setInt(8, getDecimalDigits(typeName)); + Integer decimalDigits = calculateDecimalDigits(typeName); + if (decimalDigits != null) { + rowData.setIntValue(9, decimalDigits); + } else { + rowData.setStringValue(9, null); + } // set NUM_PREC_RADIX - rowData.setInt(9, 10); + rowData.setIntValue(10, 10); // set NULLABLE - rowData.setInt(10, getNullable(index, typeName)); + rowData.setIntValue(11, isNullable(rowIndex, typeName)); // set REMARKS - rowData.setString(11, rs.getString("Note")); + String note = rs.getString("Note"); + rowData.setStringValue(12, note.trim().isEmpty() ? null : note); rowDataList.add(rowData); - index++; + rowIndex++; } resultSet.setRowDataList(rowDataList); - return resultSet; - } catch (SQLException e) { e.printStackTrace(); } - return null; + return resultSet; } - protected int getNullable(int index, String typeName) { + private int isNullable(int index, String typeName) { if (index == 0 && "TIMESTAMP".equals(typeName)) return DatabaseMetaData.columnNoNulls; return DatabaseMetaData.columnNullable; } - protected int getColumnSize(String typeName, int length) { + private Integer calculateColumnSize(String typeName, String precisionType, int length) { switch (typeName) { case "TIMESTAMP": - return 23; - default: - return 0; - } - } - - protected int getDecimalDigits(String typeName) { - switch (typeName) { - case "FLOAT": - return 5; - case "DOUBLE": - return 9; - default: - return 0; - } - } - - protected int getDataType(String typeName) { - switch (typeName) { - case "TIMESTAMP": - return Types.TIMESTAMP; - case "INT": - return Types.INTEGER; - case "BIGINT": - return Types.BIGINT; - case "FLOAT": - return Types.FLOAT; - case "DOUBLE": - return Types.DOUBLE; - case "BINARY": - return Types.BINARY; - case "SMALLINT": - return Types.SMALLINT; - case "TINYINT": - return Types.TINYINT; + return precisionType.equals("ms") ? TSDBConstants.TIMESTAMP_MS_PRECISION : TSDBConstants.TIMESTAMP_US_PRECISION; case "BOOL": - return Types.BOOLEAN; + return TSDBConstants.BOOLEAN_PRECISION; + case "TINYINT": + return TSDBConstants.TINYINT_PRECISION; + case "SMALLINT": + return TSDBConstants.SMALLINT_PRECISION; + case "INT": + return TSDBConstants.INT_PRECISION; + case "BIGINT": + return TSDBConstants.BIGINT_PRECISION; + case "FLOAT": + return TSDBConstants.FLOAT_PRECISION; + case "DOUBLE": + return TSDBConstants.DOUBLE_PRECISION; case "NCHAR": - return Types.NCHAR; + case "BINARY": + return length; default: - return Types.NULL; + return null; } } - public ResultSet getColumnPrivileges(String catalog, String schema, String table, String columnNamePattern) - throws SQLException { + private Integer calculateDecimalDigits(String typeName) { + switch (typeName) { + case "TINYINT": + case "SMALLINT": + case "INT": + case "BIGINT": + return 0; + default: + return null; + } + } + + private ColumnMetaData buildTableCatalogMeta(int colIndex) { + ColumnMetaData col1 = new ColumnMetaData(); + col1.setColIndex(colIndex); + col1.setColName("TABLE_CAT"); + col1.setColType(Types.NCHAR); + return col1; + } + + private ColumnMetaData buildTableSchemaMeta(int colIndex) { + ColumnMetaData col2 = new ColumnMetaData(); + col2.setColIndex(colIndex); + col2.setColName("TABLE_SCHEM"); + col2.setColType(Types.NCHAR); + return col2; + } + + private ColumnMetaData buildTableNameMeta(int colIndex) { + ColumnMetaData col3 = new ColumnMetaData(); + col3.setColIndex(colIndex); + col3.setColName("TABLE_NAME"); + col3.setColSize(193); + col3.setColType(Types.NCHAR); + return col3; + } + + private ColumnMetaData buildColumnNameMeta(int colIndex) { + ColumnMetaData col4 = new ColumnMetaData(); + col4.setColIndex(colIndex); + col4.setColName("COLUMN_NAME"); + col4.setColSize(65); + col4.setColType(Types.NCHAR); + return col4; + } + + private ColumnMetaData buildDataTypeMeta(int colIndex) { + ColumnMetaData col5 = new ColumnMetaData(); + col5.setColIndex(colIndex); + col5.setColName("DATA_TYPE"); + col5.setColType(Types.INTEGER); + return col5; + } + + private ColumnMetaData buildColumnSizeMeta() { + ColumnMetaData col7 = new ColumnMetaData(); + col7.setColIndex(7); + col7.setColName("COLUMN_SIZE"); + col7.setColType(Types.INTEGER); + return col7; + } + + private ColumnMetaData buildBufferLengthMeta() { + ColumnMetaData col8 = new ColumnMetaData(); + col8.setColIndex(8); + col8.setColName("BUFFER_LENGTH"); + return col8; + } + + private ColumnMetaData buildDecimalDigitsMeta() { + ColumnMetaData col9 = new ColumnMetaData(); + col9.setColIndex(9); + col9.setColName("DECIMAL_DIGITS"); + col9.setColType(Types.INTEGER); + return col9; + } + + private ColumnMetaData buildNumPrecRadixMeta() { + ColumnMetaData col10 = new ColumnMetaData(); + col10.setColIndex(10); + col10.setColName("NUM_PREC_RADIX"); + col10.setColType(Types.INTEGER); + return col10; + } + + private ColumnMetaData buildNullableMeta() { + ColumnMetaData col11 = new ColumnMetaData(); + col11.setColIndex(11); + col11.setColName("NULLABLE"); + col11.setColType(Types.INTEGER); + return col11; + } + + private ColumnMetaData buildRemarksMeta(int colIndex) { + ColumnMetaData col12 = new ColumnMetaData(); + col12.setColIndex(colIndex); + col12.setColName("REMARKS"); + col12.setColType(Types.NCHAR); + return col12; + } + + private ColumnMetaData buildColumnDefMeta() { + ColumnMetaData col13 = new ColumnMetaData(); + col13.setColIndex(13); + col13.setColName("COLUMN_DEF"); + col13.setColType(Types.NCHAR); + return col13; + } + + private ColumnMetaData buildSqlDataTypeMeta() { + ColumnMetaData col14 = new ColumnMetaData(); + col14.setColIndex(14); + col14.setColName("SQL_DATA_TYPE"); + col14.setColType(Types.INTEGER); + return col14; + } + + private ColumnMetaData buildSqlDatetimeSubMeta() { + ColumnMetaData col15 = new ColumnMetaData(); + col15.setColIndex(15); + col15.setColName("SQL_DATETIME_SUB"); + col15.setColType(Types.INTEGER); + return col15; + } + + private ColumnMetaData buildCharOctetLengthMeta() { + ColumnMetaData col16 = new ColumnMetaData(); + col16.setColIndex(16); + col16.setColName("CHAR_OCTET_LENGTH"); + col16.setColType(Types.INTEGER); + return col16; + } + + private ColumnMetaData buildOrdinalPositionMeta() { + ColumnMetaData col17 = new ColumnMetaData(); + col17.setColIndex(17); + col17.setColName("ORDINAL_POSITION"); + col17.setColType(Types.INTEGER); + return col17; + } + + private ColumnMetaData buildIsNullableMeta() { + ColumnMetaData col18 = new ColumnMetaData(); + col18.setColIndex(18); + col18.setColName("IS_NULLABLE"); + col18.setColType(Types.NCHAR); + return col18; + } + + private ColumnMetaData buildScopeCatalogMeta() { + ColumnMetaData col19 = new ColumnMetaData(); + col19.setColIndex(19); + col19.setColName("SCOPE_CATALOG"); + col19.setColType(Types.NCHAR); + return col19; + } + + private ColumnMetaData buildScopeSchemaMeta() { + ColumnMetaData col20 = new ColumnMetaData(); + col20.setColIndex(20); + col20.setColName("SCOPE_SCHEMA"); + col20.setColType(Types.NCHAR); + return col20; + } + + private ColumnMetaData buildScopeTableMeta() { + ColumnMetaData col21 = new ColumnMetaData(); + col21.setColIndex(21); + col21.setColName("SCOPE_TABLE"); + col21.setColType(Types.NCHAR); + return col21; + } + + private ColumnMetaData buildSourceDataTypeMeta() { + ColumnMetaData col22 = new ColumnMetaData(); + col22.setColIndex(22); + col22.setColName("SOURCE_DATA_TYPE"); + col22.setColType(TSDBConstants.TSDB_DATA_TYPE_SMALLINT); + return col22; + } + + private ColumnMetaData buildIsAutoIncrementMeta() { + ColumnMetaData col23 = new ColumnMetaData(); + col23.setColIndex(23); + col23.setColName("IS_AUTOINCREMENT"); + col23.setColType(Types.NCHAR); + return col23; + } + + private ColumnMetaData buildIsGeneratedColumnMeta() { + ColumnMetaData col24 = new ColumnMetaData(); + col24.setColIndex(24); + col24.setColName("IS_GENERATEDCOLUMN"); + col24.setColType(Types.NCHAR); + return col24; + } + + private List buildGetColumnsColumnMetaDataList() { + List columnMetaDataList = new ArrayList<>(); + columnMetaDataList.add(buildTableCatalogMeta(1)); //1. TABLE_CAT + columnMetaDataList.add(buildTableSchemaMeta(2)); //2. TABLE_SCHEMA + columnMetaDataList.add(buildTableNameMeta(3)); //3. TABLE_NAME + columnMetaDataList.add(buildColumnNameMeta(4)); //4. COLUMN_NAME + columnMetaDataList.add(buildDataTypeMeta(5)); //5. DATA_TYPE + columnMetaDataList.add(buildTypeNameMeta(6)); //6. TYPE_NAME + columnMetaDataList.add(buildColumnSizeMeta()); //7. COLUMN_SIZE + columnMetaDataList.add(buildBufferLengthMeta()); //8. BUFFER_LENGTH, not used + columnMetaDataList.add(buildDecimalDigitsMeta()); //9. DECIMAL_DIGITS + columnMetaDataList.add(buildNumPrecRadixMeta()); //10. NUM_PREC_RADIX + columnMetaDataList.add(buildNullableMeta()); //11. NULLABLE + columnMetaDataList.add(buildRemarksMeta(12)); //12. REMARKS + columnMetaDataList.add(buildColumnDefMeta()); //13. COLUMN_DEF + columnMetaDataList.add(buildSqlDataTypeMeta()); //14. SQL_DATA_TYPE + columnMetaDataList.add(buildSqlDatetimeSubMeta()); //15. SQL_DATETIME_SUB + columnMetaDataList.add(buildCharOctetLengthMeta()); //16. CHAR_OCTET_LENGTH + columnMetaDataList.add(buildOrdinalPositionMeta()); //17. ORDINAL_POSITION + columnMetaDataList.add(buildIsNullableMeta()); //18. IS_NULLABLE + columnMetaDataList.add(buildScopeCatalogMeta()); //19. SCOPE_CATALOG + columnMetaDataList.add(buildScopeSchemaMeta()); //20. SCOPE_SCHEMA + columnMetaDataList.add(buildScopeTableMeta()); //21. SCOPE_TABLE + columnMetaDataList.add(buildSourceDataTypeMeta()); //22. SOURCE_DATA_TYPE + columnMetaDataList.add(buildIsAutoIncrementMeta()); //23. IS_AUTOINCREMENT + columnMetaDataList.add(buildIsGeneratedColumnMeta()); //24. IS_GENERATEDCOLUMN + return columnMetaDataList; + } + + public ResultSet getColumnPrivileges(String catalog, String schema, String table, String columnNamePattern) throws SQLException { return getEmptyResultSet(); } - public ResultSet getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern) - throws SQLException { + public ResultSet getTablePrivileges(String catalog, String schemaPattern, String tableNamePattern) throws SQLException { return getEmptyResultSet(); } - public ResultSet getBestRowIdentifier(String catalog, String schema, String table, int scope, boolean nullable) - throws SQLException { + public ResultSet getBestRowIdentifier(String catalog, String schema, String table, int scope, boolean nullable) throws SQLException { return getEmptyResultSet(); } @@ -914,8 +971,7 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da return getEmptyResultSet(); } - public ResultSet getCrossReference(String parentCatalog, String parentSchema, String parentTable, - String foreignCatalog, String foreignSchema, String foreignTable) throws SQLException { + public ResultSet getCrossReference(String parentCatalog, String parentSchema, String parentTable, String foreignCatalog, String foreignSchema, String foreignTable) throws SQLException { return getEmptyResultSet(); } @@ -923,8 +979,7 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da return getEmptyResultSet(); } - public ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) - throws SQLException { + public ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) throws SQLException { return getEmptyResultSet(); } @@ -976,8 +1031,7 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da return false; } - public ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePattern, int[] types) - throws SQLException { + public ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePattern, int[] types) throws SQLException { return getEmptyResultSet(); } @@ -1005,8 +1059,7 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da public abstract ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) throws SQLException; - public ResultSet getAttributes(String catalog, String schemaPattern, String typeNamePattern, - String attributeNamePattern) throws SQLException { + public ResultSet getAttributes(String catalog, String schemaPattern, String typeNamePattern, String attributeNamePattern) throws SQLException { return getEmptyResultSet(); } @@ -1069,18 +1122,15 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da return getEmptyResultSet(); } - public ResultSet getFunctions(String catalog, String schemaPattern, String functionNamePattern) - throws SQLException { + public ResultSet getFunctions(String catalog, String schemaPattern, String functionNamePattern) throws SQLException { return getEmptyResultSet(); } - public ResultSet getFunctionColumns(String catalog, String schemaPattern, String functionNamePattern, - String columnNamePattern) throws SQLException { + public ResultSet getFunctionColumns(String catalog, String schemaPattern, String functionNamePattern, String columnNamePattern) throws SQLException { return getEmptyResultSet(); } - public ResultSet getPseudoColumns(String catalog, String schemaPattern, String tableNamePattern, - String columnNamePattern) throws SQLException { + public ResultSet getPseudoColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException { return getEmptyResultSet(); } @@ -1093,164 +1143,142 @@ public abstract class AbstractDatabaseMetaData extends WrapperImpl implements Da } protected ResultSet getCatalogs(Connection conn) throws SQLException { + DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); + // set up ColumnMetaDataList + List columnMetaDataList = new ArrayList<>(); + columnMetaDataList.add(buildTableCatalogMeta(1)); // 1. TABLE_CAT + resultSet.setColumnMetaDataList(columnMetaDataList); + try (Statement stmt = conn.createStatement()) { - DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); - // set up ColumnMetaDataList - List columnMetaDataList = new ArrayList<>(); - // TABLE_CAT - ColumnMetaData col1 = new ColumnMetaData(); - col1.setColIndex(1); - col1.setColName("TABLE_CAT"); - col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col1); - - resultSet.setColumnMetaDataList(columnMetaDataList); - List rowDataList = new ArrayList<>(); ResultSet rs = stmt.executeQuery("show databases"); while (rs.next()) { TSDBResultSetRowData rowData = new TSDBResultSetRowData(1); - rowData.setString(0, rs.getString("name")); + rowData.setStringValue(1, rs.getString("name")); rowDataList.add(rowData); } resultSet.setRowDataList(rowDataList); - return resultSet; } + return resultSet; } protected ResultSet getPrimaryKeys(String catalog, String schema, String table, Connection conn) throws SQLException { + if (catalog == null || catalog.isEmpty()) + return null; + if (!isAvailableCatalog(conn, catalog)) + return new EmptyResultSet(); + + DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); try (Statement stmt = conn.createStatement()) { - if (catalog == null || catalog.isEmpty()) - return null; - - ResultSet databases = stmt.executeQuery("show databases"); - String dbname = null; - while (databases.next()) { - dbname = databases.getString("name"); - if (dbname.equalsIgnoreCase(catalog)) - break; - } - databases.close(); - if (dbname == null) - return null; - - stmt.execute("use " + dbname); - DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); // set up ColumnMetaDataList - List columnMetaDataList = new ArrayList<>(); - // TABLE_CAT - ColumnMetaData col1 = new ColumnMetaData(); - col1.setColIndex(0); - col1.setColName("TABLE_CAT"); - col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col1); - // TABLE_SCHEM - ColumnMetaData col2 = new ColumnMetaData(); - col2.setColIndex(1); - col2.setColName("TABLE_SCHEM"); - col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col2); - // TABLE_NAME - ColumnMetaData col3 = new ColumnMetaData(); - col3.setColIndex(2); - col3.setColName("TABLE_NAME"); - col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col3); - // COLUMN_NAME - ColumnMetaData col4 = new ColumnMetaData(); - col4.setColIndex(3); - col4.setColName("COLUMN_NAME"); - col4.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col4); - // KEY_SEQ - ColumnMetaData col5 = new ColumnMetaData(); - col5.setColIndex(4); - col5.setColName("KEY_SEQ"); - col5.setColType(TSDBConstants.TSDB_DATA_TYPE_INT); - columnMetaDataList.add(col5); - // PK_NAME - ColumnMetaData col6 = new ColumnMetaData(); - col6.setColIndex(5); - col6.setColName("PK_NAME"); - col6.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col6); - resultSet.setColumnMetaDataList(columnMetaDataList); - + resultSet.setColumnMetaDataList(buildGetPrimaryKeysMetadataList()); // set rowData List rowDataList = new ArrayList<>(); - ResultSet rs = stmt.executeQuery("describe " + dbname + "." + table); + ResultSet rs = stmt.executeQuery("describe " + catalog + "." + table); rs.next(); TSDBResultSetRowData rowData = new TSDBResultSetRowData(6); - rowData.setString(0, null); - rowData.setString(1, null); - rowData.setString(2, table); - String pkName = rs.getString(1); - rowData.setString(3, pkName); - rowData.setInt(4, 1); - rowData.setString(5, pkName); + rowData.setStringValue(1, catalog); + rowData.setStringValue(2, null); + rowData.setStringValue(3, table); + String primaryKey = rs.getString("Field"); + rowData.setStringValue(4, primaryKey); + rowData.setShortValue(5, (short) 1); + rowData.setStringValue(6, primaryKey); rowDataList.add(rowData); resultSet.setRowDataList(rowDataList); - return resultSet; } + return resultSet; + } + + private List buildGetPrimaryKeysMetadataList() { + List columnMetaDataList = new ArrayList<>(); + columnMetaDataList.add(buildTableCatalogMeta(1)); // 1. TABLE_CAT + columnMetaDataList.add(buildTableSchemaMeta(2)); // 2. TABLE_SCHEM + columnMetaDataList.add(buildTableNameMeta(3)); // 3. TABLE_NAME + columnMetaDataList.add(buildColumnNameMeta(4)); // 4. COLUMN_NAME + columnMetaDataList.add(buildKeySeqMeta(5)); // 5. KEY_SEQ + columnMetaDataList.add(buildPrimaryKeyNameMeta(6)); // 6. PK_NAME + return columnMetaDataList; + } + + private ColumnMetaData buildKeySeqMeta(int colIndex) { + ColumnMetaData col5 = new ColumnMetaData(); + col5.setColIndex(colIndex); + col5.setColName("KEY_SEQ"); + col5.setColType(Types.SMALLINT); + return col5; + } + + private ColumnMetaData buildPrimaryKeyNameMeta(int colIndex) { + ColumnMetaData col6 = new ColumnMetaData(); + col6.setColIndex(colIndex); + col6.setColName("PK_NAME"); + col6.setColType(Types.NCHAR); + return col6; + } + + private boolean isAvailableCatalog(Connection connection, String catalog) { + try (Statement stmt = connection.createStatement()) { + ResultSet databases = stmt.executeQuery("show databases"); + while (databases.next()) { + String dbname = databases.getString("name"); + this.database = dbname; + this.precision = databases.getString("precision"); + if (dbname.equalsIgnoreCase(catalog)) + return true; + } + databases.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return false; } protected ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern, Connection conn) throws SQLException { + if (catalog == null || catalog.isEmpty()) + return null; + + if (!isAvailableCatalog(conn, catalog)) { + return new EmptyResultSet(); + } + + DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); try (Statement stmt = conn.createStatement()) { - if (catalog == null || catalog.isEmpty()) - return null; - - ResultSet databases = stmt.executeQuery("show databases"); - String dbname = null; - while (databases.next()) { - dbname = databases.getString("name"); - if (dbname.equalsIgnoreCase(catalog)) - break; - } - databases.close(); - if (dbname == null) - return null; - - stmt.execute("use " + dbname); - DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet(); // set up ColumnMetaDataList - List columnMetaDataList = new ArrayList<>(); - // TABLE_CAT - ColumnMetaData col1 = new ColumnMetaData(); - col1.setColIndex(0); - col1.setColName("TABLE_CAT"); - col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col1); - // TABLE_SCHEM - ColumnMetaData col2 = new ColumnMetaData(); - col2.setColIndex(1); - col2.setColName("TABLE_SCHEM"); - col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col2); - // TABLE_NAME - ColumnMetaData col3 = new ColumnMetaData(); - col3.setColIndex(2); - col3.setColName("TABLE_NAME"); - col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col3); - // SUPERTABLE_NAME - ColumnMetaData col4 = new ColumnMetaData(); - col4.setColIndex(3); - col4.setColName("SUPERTABLE_NAME"); - col4.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR); - columnMetaDataList.add(col4); - resultSet.setColumnMetaDataList(columnMetaDataList); - + resultSet.setColumnMetaDataList(buildGetSuperTablesColumnMetaDataList()); + // set result set row data + stmt.execute("use " + catalog); ResultSet rs = stmt.executeQuery("show tables like '" + tableNamePattern + "'"); List rowDataList = new ArrayList<>(); while (rs.next()) { TSDBResultSetRowData rowData = new TSDBResultSetRowData(4); - rowData.setString(2, rs.getString(1)); - rowData.setString(3, rs.getString(4)); + rowData.setStringValue(1, catalog); + rowData.setStringValue(2, null); + rowData.setStringValue(3, rs.getString("table_name")); + rowData.setStringValue(4, rs.getString("stable_name")); rowDataList.add(rowData); } resultSet.setRowDataList(rowDataList); - return resultSet; } + return resultSet; } + + private List buildGetSuperTablesColumnMetaDataList() { + List columnMetaDataList = new ArrayList<>(); + columnMetaDataList.add(buildTableCatalogMeta(1)); // 1. TABLE_CAT + columnMetaDataList.add(buildTableSchemaMeta(2)); // 2. TABLE_SCHEM + columnMetaDataList.add(buildTableNameMeta(3)); // 3. TABLE_NAME + columnMetaDataList.add(buildSuperTableNameMeta(4)); // 4. SUPERTABLE_NAME + return columnMetaDataList; + } + + private ColumnMetaData buildSuperTableNameMeta(int colIndex) { + ColumnMetaData col4 = new ColumnMetaData(); + col4.setColIndex(colIndex); + col4.setColName("SUPERTABLE_NAME"); + col4.setColType(Types.NCHAR); + return col4; + } + } \ No newline at end of file diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractParameterMetaData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractParameterMetaData.java index 7df7252ae2..cb6c7d40ad 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractParameterMetaData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractParameterMetaData.java @@ -1,5 +1,7 @@ package com.taosdata.jdbc; +import com.sun.org.apache.xpath.internal.operations.Bool; + import java.sql.ParameterMetaData; import java.sql.SQLException; import java.sql.Timestamp; @@ -49,6 +51,22 @@ public abstract class AbstractParameterMetaData extends WrapperImpl implements P if (param < 1 && param >= parameters.length) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE); + if (parameters[param - 1] instanceof Boolean) + return TSDBConstants.BOOLEAN_PRECISION; + if (parameters[param - 1] instanceof Byte) + return TSDBConstants.TINYINT_PRECISION; + if (parameters[param - 1] instanceof Short) + return TSDBConstants.SMALLINT_PRECISION; + if (parameters[param - 1] instanceof Integer) + return TSDBConstants.INT_PRECISION; + if (parameters[param - 1] instanceof Long) + return TSDBConstants.BIGINT_PRECISION; + if (parameters[param - 1] instanceof Timestamp) + return TSDBConstants.TIMESTAMP_MS_PRECISION; + if (parameters[param - 1] instanceof Float) + return TSDBConstants.FLOAT_PRECISION; + if (parameters[param - 1] instanceof Double) + return TSDBConstants.DOUBLE_PRECISION; if (parameters[param - 1] instanceof String) return ((String) parameters[param - 1]).length(); if (parameters[param - 1] instanceof byte[]) @@ -60,6 +78,11 @@ public abstract class AbstractParameterMetaData extends WrapperImpl implements P public int getScale(int param) throws SQLException { if (param < 1 && param >= parameters.length) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE); + + if (parameters[param - 1] instanceof Float) + return TSDBConstants.FLOAT_SCALE; + if (parameters[param - 1] instanceof Double) + return TSDBConstants.DOUBLE_SCALE; return 0; } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractResultSet.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractResultSet.java index f8ea9af423..e17548055c 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractResultSet.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/AbstractResultSet.java @@ -66,10 +66,16 @@ public abstract class AbstractResultSet extends WrapperImpl implements ResultSet public abstract byte[] getBytes(int columnIndex) throws SQLException; @Override - public abstract Date getDate(int columnIndex) throws SQLException; + public Date getDate(int columnIndex) throws SQLException { + Timestamp timestamp = getTimestamp(columnIndex); + return timestamp == null ? null : new Date(timestamp.getTime()); + } @Override - public abstract Time getTime(int columnIndex) throws SQLException; + public Time getTime(int columnIndex) throws SQLException { + Timestamp timestamp = getTimestamp(columnIndex); + return timestamp == null ? null : new Time(timestamp.getTime()); + } @Override public abstract Timestamp getTimestamp(int columnIndex) throws SQLException; diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/DatabaseMetaDataResultSet.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/DatabaseMetaDataResultSet.java index f6a0fcca31..cd266529f3 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/DatabaseMetaDataResultSet.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/DatabaseMetaDataResultSet.java @@ -14,76 +14,42 @@ *****************************************************************************/ package com.taosdata.jdbc; -import java.io.InputStream; -import java.io.Reader; import java.math.BigDecimal; -import java.net.URL; -import java.sql.Date; import java.sql.*; -import java.util.*; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Iterator; +import java.util.List; /* - * TDengine only supports a subset of the standard SQL, thus this implemetation of the + * TDengine only supports a subset of the standard SQL, thus this implementation of the * standard JDBC API contains more or less some adjustments customized for certain * compatibility needs. */ -public class DatabaseMetaDataResultSet implements ResultSet { +public class DatabaseMetaDataResultSet extends AbstractResultSet { - private List columnMetaDataList; - private List rowDataList; + private List columnMetaDataList = new ArrayList<>(); + private List rowDataList = new ArrayList<>(); private TSDBResultSetRowData rowCursor; // position of cursor, starts from 0 as beforeFirst, increases as next() is called private int cursorRowNumber = 0; - public DatabaseMetaDataResultSet() { - rowDataList = new ArrayList(); - columnMetaDataList = new ArrayList(); - } - - public List getRowDataList() { - return rowDataList; - } - public void setRowDataList(List rowDataList) { this.rowDataList = rowDataList; } - public List getColumnMetaDataList() { - return columnMetaDataList; - } - public void setColumnMetaDataList(List columnMetaDataList) { this.columnMetaDataList = columnMetaDataList; } - public TSDBResultSetRowData getRowCursor() { - return rowCursor; - } - - public void setRowCursor(TSDBResultSetRowData rowCursor) { - this.rowCursor = rowCursor; - } - @Override public boolean next() throws SQLException { -// boolean ret = false; -// if (rowDataList.size() > 0) { -// ret = rowDataList.iterator().hasNext(); -// if (ret) { -// rowCursor = rowDataList.iterator().next(); -// cursorRowNumber++; -// } -// } -// return ret; - - /**** add by zyyang 2020-09-29 ****************/ boolean ret = false; if (!rowDataList.isEmpty() && cursorRowNumber < rowDataList.size()) { rowCursor = rowDataList.get(cursorRowNumber++); ret = true; } - return ret; } @@ -99,189 +65,72 @@ public class DatabaseMetaDataResultSet implements ResultSet { @Override public String getString(int columnIndex) throws SQLException { - columnIndex--; - int colType = columnMetaDataList.get(columnIndex).getColType(); - return rowCursor.getString(columnIndex, colType); + int colType = columnMetaDataList.get(columnIndex - 1).getColType(); + int nativeType = TSDBConstants.jdbcType2TaosType(colType); + return rowCursor.getString(columnIndex, nativeType); } @Override public boolean getBoolean(int columnIndex) throws SQLException { - columnIndex--; - return rowCursor.getBoolean(columnIndex, columnMetaDataList.get(columnIndex).getColType()); + int colType = columnMetaDataList.get(columnIndex - 1).getColType(); + int nativeType = TSDBConstants.jdbcType2TaosType(colType); + return rowCursor.getBoolean(columnIndex, nativeType); } @Override public byte getByte(int columnIndex) throws SQLException { - columnIndex--; - return (byte) rowCursor.getInt(columnIndex, columnMetaDataList.get(columnIndex).getColType()); + int colType = columnMetaDataList.get(columnIndex - 1).getColType(); + int nativeType = TSDBConstants.jdbcType2TaosType(colType); + return (byte) rowCursor.getInt(columnIndex, nativeType); } @Override public short getShort(int columnIndex) throws SQLException { - columnIndex--; - return (short) rowCursor.getInt(columnIndex, columnMetaDataList.get(columnIndex).getColType()); + int colType = columnMetaDataList.get(columnIndex - 1).getColType(); + int nativeType = TSDBConstants.jdbcType2TaosType(colType); + return (short) rowCursor.getInt(columnIndex, nativeType); } @Override public int getInt(int columnIndex) throws SQLException { - columnIndex--; - return rowCursor.getInt(columnIndex, columnMetaDataList.get(columnIndex).getColType()); + int colType = columnMetaDataList.get(columnIndex - 1).getColType(); + int nativeType = TSDBConstants.jdbcType2TaosType(colType); + return rowCursor.getInt(columnIndex, nativeType); } @Override public long getLong(int columnIndex) throws SQLException { - columnIndex--; - return rowCursor.getLong(columnIndex, columnMetaDataList.get(columnIndex).getColType()); + int colType = columnMetaDataList.get(columnIndex - 1).getColType(); + int nativeType = TSDBConstants.jdbcType2TaosType(colType); + return rowCursor.getLong(columnIndex, nativeType); } @Override public float getFloat(int columnIndex) throws SQLException { - columnIndex--; - return rowCursor.getFloat(columnIndex, columnMetaDataList.get(columnIndex).getColType()); + int colType = columnMetaDataList.get(columnIndex - 1).getColType(); + int nativeType = TSDBConstants.jdbcType2TaosType(colType); + return rowCursor.getFloat(columnIndex, nativeType); } @Override public double getDouble(int columnIndex) throws SQLException { - columnIndex--; - return rowCursor.getDouble(columnIndex, columnMetaDataList.get(columnIndex).getColType()); - } - - @Override - public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException { - columnIndex--; - return new BigDecimal(rowCursor.getDouble(columnIndex, columnMetaDataList.get(columnIndex).getColType())); + int colType = columnMetaDataList.get(columnIndex - 1).getColType(); + int nativeType = TSDBConstants.jdbcType2TaosType(colType); + return rowCursor.getDouble(columnIndex, nativeType); } @Override public byte[] getBytes(int columnIndex) throws SQLException { - columnIndex--; - return (rowCursor.getString(columnIndex, columnMetaDataList.get(columnIndex).getColType())).getBytes(); - } - - @Override - public Date getDate(int columnIndex) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public Time getTime(int columnIndex) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); + int colType = columnMetaDataList.get(columnIndex - 1).getColType(); + int nativeType = TSDBConstants.jdbcType2TaosType(colType); + return (rowCursor.getString(columnIndex, nativeType)).getBytes(); } @Override public Timestamp getTimestamp(int columnIndex) throws SQLException { - columnIndex--; - return rowCursor.getTimestamp(columnIndex); - } - - @Override - public InputStream getAsciiStream(int columnIndex) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public InputStream getUnicodeStream(int columnIndex) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public InputStream getBinaryStream(int columnIndex) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public String getString(String columnLabel) throws SQLException { - return getString(findColumn(columnLabel)); - } - - @Override - public boolean getBoolean(String columnLabel) throws SQLException { - return getBoolean(findColumn(columnLabel)); - } - - @Override - public byte getByte(String columnLabel) throws SQLException { - return getByte(findColumn(columnLabel)); - } - - @Override - public short getShort(String columnLabel) throws SQLException { - return getShort(findColumn(columnLabel)); - } - - @Override - public int getInt(String columnLabel) throws SQLException { - return getInt(findColumn(columnLabel)); - } - - @Override - public long getLong(String columnLabel) throws SQLException { - return getLong(findColumn(columnLabel)); - } - - @Override - public float getFloat(String columnLabel) throws SQLException { - return getFloat(findColumn(columnLabel)); - } - - @Override - public double getDouble(String columnLabel) throws SQLException { - return getDouble(findColumn(columnLabel)); - } - - @Override - public BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException { - return getBigDecimal(findColumn(columnLabel)); - } - - @Override - public byte[] getBytes(String columnLabel) throws SQLException { - return getBytes(findColumn(columnLabel)); - } - - @Override - public Date getDate(String columnLabel) throws SQLException { - return getDate(findColumn(columnLabel)); - } - - @Override - public Time getTime(String columnLabel) throws SQLException { - return getTime(findColumn(columnLabel)); - } - - @Override - public Timestamp getTimestamp(String columnLabel) throws SQLException { - return getTimestamp(findColumn(columnLabel)); - } - - @Override - public InputStream getAsciiStream(String columnLabel) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public InputStream getUnicodeStream(String columnLabel) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public InputStream getBinaryStream(String columnLabel) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public SQLWarning getWarnings() throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void clearWarnings() throws SQLException { - - } - - @Override - public String getCursorName() throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); + int colType = columnMetaDataList.get(columnIndex - 1).getColType(); + int nativeType = TSDBConstants.jdbcType2TaosType(colType); + return rowCursor.getTimestamp(columnIndex,nativeType); } @Override @@ -291,12 +140,7 @@ public class DatabaseMetaDataResultSet implements ResultSet { @Override public Object getObject(int columnIndex) throws SQLException { - return rowCursor.get(columnIndex); - } - - @Override - public Object getObject(String columnLabel) throws SQLException { - return rowCursor.get(findColumn(columnLabel)); + return rowCursor.getObject(columnIndex); } @Override @@ -304,31 +148,19 @@ public class DatabaseMetaDataResultSet implements ResultSet { Iterator colMetaDataIt = this.columnMetaDataList.iterator(); while (colMetaDataIt.hasNext()) { ColumnMetaData colMetaData = colMetaDataIt.next(); - if (colMetaData.getColName() != null && colMetaData.getColName().equalsIgnoreCase(columnLabel)) { - return colMetaData.getColIndex() + 1; + if (colMetaData.getColName() != null && colMetaData.getColName().equals(columnLabel)) { + return colMetaData.getColIndex(); } } throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE); } - @Override - public Reader getCharacterStream(int columnIndex) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public Reader getCharacterStream(String columnLabel) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - @Override public BigDecimal getBigDecimal(int columnIndex) throws SQLException { - return new BigDecimal(rowCursor.getDouble(columnIndex, columnMetaDataList.get(columnIndex).getColType())); - } - - @Override - public BigDecimal getBigDecimal(String columnLabel) throws SQLException { - return getBigDecimal(findColumn(columnLabel)); + int colType = columnMetaDataList.get(columnIndex - 1).getColType(); + int nativeType = TSDBConstants.jdbcType2TaosType(colType); + double value = rowCursor.getDouble(columnIndex, nativeType); + return new BigDecimal(value); } @Override @@ -378,7 +210,6 @@ public class DatabaseMetaDataResultSet implements ResultSet { } else { return 0; } - } @Override @@ -396,434 +227,14 @@ public class DatabaseMetaDataResultSet implements ResultSet { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } - @Override - public void setFetchDirection(int direction) throws SQLException { - - } - - @Override - public int getFetchDirection() throws SQLException { - return ResultSet.FETCH_FORWARD; - } - - @Override - public void setFetchSize(int rows) throws SQLException { - - } - - @Override - public int getFetchSize() throws SQLException { - return 0; - } - - @Override - public int getType() throws SQLException { - return ResultSet.TYPE_FORWARD_ONLY; - } - - @Override - public int getConcurrency() throws SQLException { - return ResultSet.CONCUR_READ_ONLY; - } - - @Override - public boolean rowUpdated() throws SQLException { - return false; - } - - @Override - public boolean rowInserted() throws SQLException { - return false; - } - - @Override - public boolean rowDeleted() throws SQLException { - return false; - } - - @Override - public void updateNull(int columnIndex) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateBoolean(int columnIndex, boolean x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateByte(int columnIndex, byte x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateShort(int columnIndex, short x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateInt(int columnIndex, int x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateLong(int columnIndex, long x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateFloat(int columnIndex, float x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateDouble(int columnIndex, double x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateString(int columnIndex, String x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateBytes(int columnIndex, byte[] x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateDate(int columnIndex, Date x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateTime(int columnIndex, Time x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateBinaryStream(int columnIndex, InputStream x, int length) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateCharacterStream(int columnIndex, Reader x, int length) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateObject(int columnIndex, Object x, int scaleOrLength) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateObject(int columnIndex, Object x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateNull(String columnLabel) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateBoolean(String columnLabel, boolean x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateByte(String columnLabel, byte x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateShort(String columnLabel, short x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateInt(String columnLabel, int x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateLong(String columnLabel, long x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateFloat(String columnLabel, float x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateDouble(String columnLabel, double x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateBigDecimal(String columnLabel, BigDecimal x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateString(String columnLabel, String x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateBytes(String columnLabel, byte[] x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateDate(String columnLabel, Date x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateTime(String columnLabel, Time x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateTimestamp(String columnLabel, Timestamp x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateAsciiStream(String columnLabel, InputStream x, int length) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateBinaryStream(String columnLabel, InputStream x, int length) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateCharacterStream(String columnLabel, Reader reader, int length) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateObject(String columnLabel, Object x, int scaleOrLength) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateObject(String columnLabel, Object x) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void insertRow() throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void updateRow() throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void deleteRow() throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void refreshRow() throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void cancelRowUpdates() throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void moveToInsertRow() throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public void moveToCurrentRow() throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - @Override public Statement getStatement() throws SQLException { return null; } - @Override - public Object getObject(int columnIndex, Map> map) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public Ref getRef(int columnIndex) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public Blob getBlob(int columnIndex) throws SQLException { - return null; - } - - @Override - public Clob getClob(int columnIndex) throws SQLException { - return null; - } - - @Override - public Array getArray(int columnIndex) throws SQLException { - return null; - } - - @Override - public Object getObject(String columnLabel, Map> map) throws SQLException { - return null; - } - - @Override - public Ref getRef(String columnLabel) throws SQLException { - return null; - } - - @Override - public Blob getBlob(String columnLabel) throws SQLException { - return null; - } - - @Override - public Clob getClob(String columnLabel) throws SQLException { - return null; - } - - @Override - public Array getArray(String columnLabel) throws SQLException { - return null; - } - - @Override - public Date getDate(int columnIndex, Calendar cal) throws SQLException { - return null; - } - - @Override - public Date getDate(String columnLabel, Calendar cal) throws SQLException { - return null; - } - - @Override - public Time getTime(int columnIndex, Calendar cal) throws SQLException { - return null; - } - - @Override - public Time getTime(String columnLabel, Calendar cal) throws SQLException { - return null; - } - - @Override public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException { - return null; - } - - @Override - public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException { - return null; - } - - @Override - public URL getURL(int columnIndex) throws SQLException { - return null; - } - - @Override - public URL getURL(String columnLabel) throws SQLException { - return null; - } - - @Override - public void updateRef(int columnIndex, Ref x) throws SQLException { - - } - - @Override - public void updateRef(String columnLabel, Ref x) throws SQLException { - - } - - @Override - public void updateBlob(int columnIndex, Blob x) throws SQLException { - - } - - @Override - public void updateBlob(String columnLabel, Blob x) throws SQLException { - - } - - @Override - public void updateClob(int columnIndex, Clob x) throws SQLException { - - } - - @Override - public void updateClob(String columnLabel, Clob x) throws SQLException { - - } - - @Override - public void updateArray(int columnIndex, Array x) throws SQLException { - - } - - @Override - public void updateArray(String columnLabel, Array x) throws SQLException { - - } - - @Override - public RowId getRowId(int columnIndex) throws SQLException { - return null; - } - - @Override - public RowId getRowId(String columnLabel) throws SQLException { - return null; - } - - @Override - public void updateRowId(int columnIndex, RowId x) throws SQLException { - - } - - @Override - public void updateRowId(String columnLabel, RowId x) throws SQLException { - - } - - @Override - public int getHoldability() throws SQLException { - return 0; + //TODO: calendar is not used + return getTimestamp(columnIndex); } @Override @@ -831,246 +242,9 @@ public class DatabaseMetaDataResultSet implements ResultSet { return false; } - @Override - public void updateNString(int columnIndex, String nString) throws SQLException { - - } - - @Override - public void updateNString(String columnLabel, String nString) throws SQLException { - - } - - @Override - public void updateNClob(int columnIndex, NClob nClob) throws SQLException { - - } - - @Override - public void updateNClob(String columnLabel, NClob nClob) throws SQLException { - - } - - @Override - public NClob getNClob(int columnIndex) throws SQLException { - return null; - } - - @Override - public NClob getNClob(String columnLabel) throws SQLException { - return null; - } - - @Override - public SQLXML getSQLXML(int columnIndex) throws SQLException { - return null; - } - - @Override - public SQLXML getSQLXML(String columnLabel) throws SQLException { - return null; - } - - @Override - public void updateSQLXML(int columnIndex, SQLXML xmlObject) throws SQLException { - - } - - @Override - public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException { - - } - @Override public String getNString(int columnIndex) throws SQLException { - return null; + return getString(columnIndex); } - @Override - public String getNString(String columnLabel) throws SQLException { - return null; - } - - @Override - public Reader getNCharacterStream(int columnIndex) throws SQLException { - return null; - } - - @Override - public Reader getNCharacterStream(String columnLabel) throws SQLException { - return null; - } - - @Override - public void updateNCharacterStream(int columnIndex, Reader x, long length) throws SQLException { - - } - - @Override - public void updateNCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { - - } - - @Override - public void updateAsciiStream(int columnIndex, InputStream x, long length) throws SQLException { - - } - - @Override - public void updateBinaryStream(int columnIndex, InputStream x, long length) throws SQLException { - - } - - @Override - public void updateCharacterStream(int columnIndex, Reader x, long length) throws SQLException { - - } - - @Override - public void updateAsciiStream(String columnLabel, InputStream x, long length) throws SQLException { - - } - - @Override - public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException { - - } - - @Override - public void updateCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { - - } - - @Override - public void updateBlob(int columnIndex, InputStream inputStream, long length) throws SQLException { - - } - - @Override - public void updateBlob(String columnLabel, InputStream inputStream, long length) throws SQLException { - - } - - @Override - public void updateClob(int columnIndex, Reader reader, long length) throws SQLException { - - } - - @Override - public void updateClob(String columnLabel, Reader reader, long length) throws SQLException { - - } - - @Override - public void updateNClob(int columnIndex, Reader reader, long length) throws SQLException { - - } - - @Override - public void updateNClob(String columnLabel, Reader reader, long length) throws SQLException { - - } - - @Override - public void updateNCharacterStream(int columnIndex, Reader x) throws SQLException { - - } - - @Override - public void updateNCharacterStream(String columnLabel, Reader reader) throws SQLException { - - } - - @Override - public void updateAsciiStream(int columnIndex, InputStream x) throws SQLException { - - } - - @Override - public void updateBinaryStream(int columnIndex, InputStream x) throws SQLException { - - } - - @Override - public void updateCharacterStream(int columnIndex, Reader x) throws SQLException { - - } - - @Override - public void updateAsciiStream(String columnLabel, InputStream x) throws SQLException { - - } - - @Override - public void updateBinaryStream(String columnLabel, InputStream x) throws SQLException { - - } - - @Override - public void updateCharacterStream(String columnLabel, Reader reader) throws SQLException { - - } - - @Override - public void updateBlob(int columnIndex, InputStream inputStream) throws SQLException { - - } - - @Override - public void updateBlob(String columnLabel, InputStream inputStream) throws SQLException { - - } - - @Override - public void updateClob(int columnIndex, Reader reader) throws SQLException { - - } - - @Override - public void updateClob(String columnLabel, Reader reader) throws SQLException { - - } - - @Override - public void updateNClob(int columnIndex, Reader reader) throws SQLException { - - } - - @Override - public void updateNClob(String columnLabel, Reader reader) throws SQLException { - - } - - @Override - public T getObject(int columnIndex, Class type) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public T getObject(String columnLabel, Class type) throws SQLException { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); - } - - @Override - public T unwrap(Class iface) throws SQLException { - return null; - } - - @Override - public boolean isWrapperFor(Class iface) throws SQLException { - return false; - } - - private int getTrueColumnIndex(int columnIndex) throws SQLException { - if (columnIndex < 1) { - throw new SQLException("Column Index out of range, " + columnIndex + " < " + 1); - } - - int numOfCols = this.columnMetaDataList.size(); - if (columnIndex > numOfCols) { - throw new SQLException("Column Index out of range, " + columnIndex + " > " + numOfCols); - } - - return columnIndex - 1; - } } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConstants.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConstants.java index f38555ce8a..e6406d2c6d 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConstants.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBConstants.java @@ -41,15 +41,15 @@ public abstract class TSDBConstants { public static final int TSDB_DATA_TYPE_BINARY = 8; public static final int TSDB_DATA_TYPE_TIMESTAMP = 9; public static final int TSDB_DATA_TYPE_NCHAR = 10; - /* - 系统增加新的无符号数据类型,分别是: - unsigned tinyint, 数值范围:0-254, NULL 为255 - unsigned smallint,数值范围: 0-65534, NULL 为65535 - unsigned int,数值范围:0-4294967294,NULL 为4294967295u - unsigned bigint,数值范围:0-18446744073709551614u,NULL 为18446744073709551615u。 - example: - create table tb(ts timestamp, a tinyint unsigned, b smallint unsigned, c int unsigned, d bigint unsigned); - */ + /** + * 系统增加新的无符号数据类型,分别是: + * unsigned tinyint, 数值范围:0-254, NULL 为255 + * unsigned smallint,数值范围: 0-65534, NULL 为65535 + * unsigned int,数值范围:0-4294967294,NULL 为4294967295u + * unsigned bigint,数值范围:0-18446744073709551614u,NULL 为18446744073709551615u。 + * example: + * create table tb(ts timestamp, a tinyint unsigned, b smallint unsigned, c int unsigned, d bigint unsigned); + */ public static final int TSDB_DATA_TYPE_UTINYINT = 11; //unsigned tinyint public static final int TSDB_DATA_TYPE_USMALLINT = 12; //unsigned smallint public static final int TSDB_DATA_TYPE_UINT = 13; //unsigned int @@ -57,6 +57,47 @@ public abstract class TSDBConstants { // nchar column max length public static final int maxFieldSize = 16 * 1024; + // precision for data types + public static final int BOOLEAN_PRECISION = 1; + public static final int TINYINT_PRECISION = 4; + public static final int SMALLINT_PRECISION = 6; + public static final int INT_PRECISION = 11; + public static final int BIGINT_PRECISION = 20; + public static final int FLOAT_PRECISION = 12; + public static final int DOUBLE_PRECISION = 22; + public static final int TIMESTAMP_MS_PRECISION = 23; + public static final int TIMESTAMP_US_PRECISION = 26; + // scale for data types + public static final int FLOAT_SCALE = 31; + public static final int DOUBLE_SCALE = 31; + + public static int typeName2JdbcType(String type) { + switch (type.toUpperCase()) { + case "TIMESTAMP": + return Types.TIMESTAMP; + case "INT": + return Types.INTEGER; + case "BIGINT": + return Types.BIGINT; + case "FLOAT": + return Types.FLOAT; + case "DOUBLE": + return Types.DOUBLE; + case "BINARY": + return Types.BINARY; + case "SMALLINT": + return Types.SMALLINT; + case "TINYINT": + return Types.TINYINT; + case "BOOL": + return Types.BOOLEAN; + case "NCHAR": + return Types.NCHAR; + default: + return Types.NULL; + } + } + public static int taosType2JdbcType(int taosType) throws SQLException { switch (taosType) { case TSDBConstants.TSDB_DATA_TYPE_BOOL: @@ -88,7 +129,7 @@ public abstract class TSDBConstants { } public static String taosType2JdbcTypeName(int taosType) throws SQLException { - switch (taosType){ + switch (taosType) { case TSDBConstants.TSDB_DATA_TYPE_BOOL: return "BOOL"; case TSDBConstants.TSDB_DATA_TYPE_UTINYINT: @@ -119,7 +160,7 @@ public abstract class TSDBConstants { } public static int jdbcType2TaosType(int jdbcType) throws SQLException { - switch (jdbcType){ + switch (jdbcType) { case Types.BOOLEAN: return TSDBConstants.TSDB_DATA_TYPE_BOOL; case Types.TINYINT: @@ -145,7 +186,7 @@ public abstract class TSDBConstants { } public static String jdbcType2TaosTypeName(int jdbcType) throws SQLException { - switch (jdbcType){ + switch (jdbcType) { case Types.BOOLEAN: return "BOOL"; case Types.TINYINT: diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java index 256e735285..92792d9751 100755 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBJNIConnector.java @@ -16,13 +16,13 @@ */ package com.taosdata.jdbc; +import com.taosdata.jdbc.utils.TaosInfo; + import java.nio.ByteBuffer; import java.sql.SQLException; import java.sql.SQLWarning; import java.util.List; -import com.taosdata.jdbc.utils.TaosInfo; - /** * JNI connector */ @@ -30,10 +30,10 @@ public class TSDBJNIConnector { private static volatile Boolean isInitialized = false; private TaosInfo taosInfo = TaosInfo.getInstance(); - + // Connection pointer used in C private long taos = TSDBConstants.JNI_NULL_POINTER; - + // result set status in current connection private boolean isResultsetClosed; @@ -194,7 +194,9 @@ public class TSDBJNIConnector { * Get schema metadata */ public int getSchemaMetaData(long resultSet, List columnMetaData) { - return this.getSchemaMetaDataImp(this.taos, resultSet, columnMetaData); + int ret = this.getSchemaMetaDataImp(this.taos, resultSet, columnMetaData); + columnMetaData.stream().forEach(column -> column.setColIndex(column.getColIndex() + 1)); + return ret; } private native int getSchemaMetaDataImp(long connection, long resultSet, List columnMetaData); @@ -221,7 +223,7 @@ public class TSDBJNIConnector { */ public void closeConnection() throws SQLException { int code = this.closeConnectionImp(this.taos); - + if (code < 0) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); } else if (code == 0) { @@ -229,7 +231,7 @@ public class TSDBJNIConnector { } else { throw new SQLException("Undefined error code returned by TDengine when closing a connection"); } - + // invoke closeConnectionImpl only here taosInfo.connect_close_increment(); } @@ -274,67 +276,76 @@ public class TSDBJNIConnector { } private native int validateCreateTableSqlImp(long connection, byte[] sqlBytes); - - public long prepareStmt(String sql) throws SQLException { - Long stmt = prepareStmtImp(sql.getBytes(), this.taos); - if (stmt == TSDBConstants.JNI_TDENGINE_ERROR) { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_SQL); - } else if (stmt == TSDBConstants.JNI_CONNECTION_NULL) { + + public long prepareStmt(String sql) throws SQLException { + Long stmt; + try { + stmt = prepareStmtImp(sql.getBytes(), this.taos); + } catch (Exception e) { + e.printStackTrace(); + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_ENCODING); + } + + if (stmt == TSDBConstants.JNI_CONNECTION_NULL) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_CONNECTION_NULL); - } else if (stmt == TSDBConstants.JNI_SQL_NULL) { + } + + if (stmt == TSDBConstants.JNI_SQL_NULL) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_SQL_NULL); - } else if (stmt == TSDBConstants.JNI_OUT_OF_MEMORY) { + } + + if (stmt == TSDBConstants.JNI_OUT_OF_MEMORY) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_JNI_OUT_OF_MEMORY); } - - return stmt; + + return stmt; } - + private native long prepareStmtImp(byte[] sql, long con); - + public void setBindTableName(long stmt, String tableName) throws SQLException { - int code = setBindTableNameImp(stmt, tableName, this.taos); - if (code != TSDBConstants.JNI_SUCCESS) { + int code = setBindTableNameImp(stmt, tableName, this.taos); + if (code != TSDBConstants.JNI_SUCCESS) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "failed to set table name"); - } - } - + } + } + private native int setBindTableNameImp(long stmt, String name, long conn); - + public void setBindTableNameAndTags(long stmt, String tableName, int numOfTags, ByteBuffer tags, ByteBuffer typeList, ByteBuffer lengthList, ByteBuffer nullList) throws SQLException { - int code = setTableNameTagsImp(stmt, tableName, numOfTags, tags.array(), typeList.array(), lengthList.array(), - nullList.array(), this.taos); - if (code != TSDBConstants.JNI_SUCCESS) { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "failed to bind table name and corresponding tags"); - } + int code = setTableNameTagsImp(stmt, tableName, numOfTags, tags.array(), typeList.array(), lengthList.array(), + nullList.array(), this.taos); + if (code != TSDBConstants.JNI_SUCCESS) { + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "failed to bind table name and corresponding tags"); + } } - + private native int setTableNameTagsImp(long stmt, String name, int numOfTags, byte[] tags, byte[] typeList, byte[] lengthList, byte[] nullList, long conn); - - public void bindColumnDataArray(long stmt, ByteBuffer colDataList, ByteBuffer lengthList, ByteBuffer isNullList, int type, int bytes, int numOfRows,int columnIndex) throws SQLException { - int code = bindColDataImp(stmt, colDataList.array(), lengthList.array(), isNullList.array(), type, bytes, numOfRows, columnIndex, this.taos); - if (code != TSDBConstants.JNI_SUCCESS) { + + public void bindColumnDataArray(long stmt, ByteBuffer colDataList, ByteBuffer lengthList, ByteBuffer isNullList, int type, int bytes, int numOfRows, int columnIndex) throws SQLException { + int code = bindColDataImp(stmt, colDataList.array(), lengthList.array(), isNullList.array(), type, bytes, numOfRows, columnIndex, this.taos); + if (code != TSDBConstants.JNI_SUCCESS) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "failed to bind column data"); - } - } - + } + } + private native int bindColDataImp(long stmt, byte[] colDataList, byte[] lengthList, byte[] isNullList, int type, int bytes, int numOfRows, int columnIndex, long conn); - + public void executeBatch(long stmt) throws SQLException { - int code = executeBatchImp(stmt, this.taos); - if (code != TSDBConstants.JNI_SUCCESS) { + int code = executeBatchImp(stmt, this.taos); + if (code != TSDBConstants.JNI_SUCCESS) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "failed to execute batch bind"); - } + } } - + private native int executeBatchImp(long stmt, long con); - + public void closeBatch(long stmt) throws SQLException { - int code = closeStmt(stmt, this.taos); - if (code != TSDBConstants.JNI_SUCCESS) { + int code = closeStmt(stmt, this.taos); + if (code != TSDBConstants.JNI_SUCCESS) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "failed to close batch bind"); - } + } } - + private native int closeStmt(long stmt, long con); } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java index f6810237c0..c3d5abf35c 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBPreparedStatement.java @@ -39,18 +39,18 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat private String rawSql; private Object[] parameters; private boolean isPrepared; - + private ArrayList colData; private ArrayList tableTags; private int tagValueLength; - + private String tableName; private long nativeStmtHandle = 0; - + private volatile TSDBParameterMetaData parameterMetaData; TSDBPreparedStatement(TSDBConnection connection, String sql) { - super(connection); + super(connection); init(sql); int parameterCnt = 0; @@ -64,11 +64,11 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat this.isPrepared = true; } - if (parameterCnt > 1) { - // the table name is also a parameter, so ignore it. - this.colData = new ArrayList(); - this.tableTags = new ArrayList(); - } + if (parameterCnt > 1) { + // the table name is also a parameter, so ignore it. + this.colData = new ArrayList(); + this.tableTags = new ArrayList(); + } } private void init(String sql) { @@ -205,9 +205,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat @Override public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { - if (isClosed()) - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); + setObject(parameterIndex, x.doubleValue()); } @Override @@ -222,16 +220,12 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat @Override public void setDate(int parameterIndex, Date x) throws SQLException { - if (isClosed()) - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); + setObject(parameterIndex, new Timestamp(x.getTime())); } @Override public void setTime(int parameterIndex, Time x) throws SQLException { - if (isClosed()) - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); + setObject(parameterIndex, new Timestamp(x.getTime())); } @Override @@ -279,11 +273,10 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat if (isClosed()) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); } - + if (parameterIndex < 1 && parameterIndex >= parameters.length) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_PARAMETER_INDEX_OUT_RANGE); } - parameters[parameterIndex - 1] = x; } @@ -323,7 +316,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat if (isClosed()) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); } - + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } @@ -350,9 +343,9 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat @Override public ResultSetMetaData getMetaData() throws SQLException { - if (isClosed()) - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); + if (this.getResultSet() == null) + return null; + return getResultSet().getMetaData(); } @Override @@ -396,10 +389,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat if (isClosed()) throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); - if (parameterMetaData == null) { - this.parameterMetaData = new TSDBParameterMetaData(parameters); - } - return this.parameterMetaData; + return new TSDBParameterMetaData(parameters); } @Override @@ -411,9 +401,7 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat @Override public void setNString(int parameterIndex, String value) throws SQLException { - if (isClosed()) - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); + setString(parameterIndex, value); } @Override @@ -536,489 +524,495 @@ public class TSDBPreparedStatement extends TSDBStatement implements PreparedStat throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD); } - + /////////////////////////////////////////////////////////////////////// // NOTE: the following APIs are not JDBC compatible // set the bind table name private static class ColumnInfo { - @SuppressWarnings("rawtypes") - private ArrayList data; - private int type; - private int bytes; - private boolean typeIsSet; - - public ColumnInfo() { - this.typeIsSet = false; - } - - public void setType(int type) throws SQLException { - if (this.isTypeSet()) { + @SuppressWarnings("rawtypes") + private ArrayList data; + private int type; + private int bytes; + private boolean typeIsSet; + + public ColumnInfo() { + this.typeIsSet = false; + } + + public void setType(int type) throws SQLException { + if (this.isTypeSet()) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "column data type has been set"); - } + } + + this.typeIsSet = true; + this.type = type; + } + + public boolean isTypeSet() { + return this.typeIsSet; + } + } + + ; - this.typeIsSet = true; - this.type = type; - } - - public boolean isTypeSet() { - return this.typeIsSet; - } - }; - private static class TableTagInfo { - private boolean isNull; - private Object value; - private int type; - public TableTagInfo(Object value, int type) { - this.value = value; - this.type = type; - } - - public static TableTagInfo createNullTag(int type) { - TableTagInfo info = new TableTagInfo(null, type); - info.isNull = true; - return info; - } - }; - - public void setTableName(String name) { - this.tableName = name; - } - - private void ensureTagCapacity(int index) { - if (this.tableTags.size() < index + 1) { - int delta = index + 1 - this.tableTags.size(); - this.tableTags.addAll(Collections.nCopies(delta, null)); - } - } - - public void setTagNull(int index, int type) { - ensureTagCapacity(index); - this.tableTags.set(index, TableTagInfo.createNullTag(type)); - } - - public void setTagBoolean(int index, boolean value) { - ensureTagCapacity(index); - this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_BOOL)); - this.tagValueLength += Byte.BYTES; - } - - public void setTagInt(int index, int value) { - ensureTagCapacity(index); - this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_INT)); - this.tagValueLength += Integer.BYTES; - } - - public void setTagByte(int index, byte value) { - ensureTagCapacity(index); - this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_TINYINT)); - this.tagValueLength += Byte.BYTES; - } - - public void setTagShort(int index, short value) { - ensureTagCapacity(index); - this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_SMALLINT)); - this.tagValueLength += Short.BYTES; - } - - public void setTagLong(int index, long value) { - ensureTagCapacity(index); - this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_BIGINT)); - this.tagValueLength += Long.BYTES; - } - - public void setTagTimestamp(int index, long value) { - ensureTagCapacity(index); - this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP)); - this.tagValueLength += Long.BYTES; - } - - public void setTagFloat(int index, float value) { - ensureTagCapacity(index); - this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_FLOAT)); - this.tagValueLength += Float.BYTES; - } - - public void setTagDouble(int index, double value) { - ensureTagCapacity(index); - this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_DOUBLE)); - this.tagValueLength += Double.BYTES; - } - - public void setTagString(int index, String value) { - ensureTagCapacity(index); - this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_BINARY)); - this.tagValueLength += value.getBytes().length; - } - - public void setTagNString(int index, String value) { - ensureTagCapacity(index); - this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_NCHAR)); - - String charset = TaosGlobalConfig.getCharset(); - try { - this.tagValueLength += value.getBytes(charset).length; - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - } - - public void setValueImpl(int columnIndex, ArrayList list, int type, int bytes) throws SQLException { - if (this.colData.size() == 0) { - this.colData.addAll(Collections.nCopies(this.parameters.length - 1 - this.tableTags.size(), null)); + private boolean isNull; + private Object value; + private int type; - } - ColumnInfo col = (ColumnInfo) this.colData.get(columnIndex); - if (col == null) { - ColumnInfo p = new ColumnInfo(); - p.setType(type); - p.bytes = bytes; - p.data = (ArrayList) list.clone(); - this.colData.set(columnIndex, p); - } else { - if (col.type != type) { + public TableTagInfo(Object value, int type) { + this.value = value; + this.type = type; + } + + public static TableTagInfo createNullTag(int type) { + TableTagInfo info = new TableTagInfo(null, type); + info.isNull = true; + return info; + } + } + + ; + + public void setTableName(String name) { + this.tableName = name; + } + + private void ensureTagCapacity(int index) { + if (this.tableTags.size() < index + 1) { + int delta = index + 1 - this.tableTags.size(); + this.tableTags.addAll(Collections.nCopies(delta, null)); + } + } + + public void setTagNull(int index, int type) { + ensureTagCapacity(index); + this.tableTags.set(index, TableTagInfo.createNullTag(type)); + } + + public void setTagBoolean(int index, boolean value) { + ensureTagCapacity(index); + this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_BOOL)); + this.tagValueLength += Byte.BYTES; + } + + public void setTagInt(int index, int value) { + ensureTagCapacity(index); + this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_INT)); + this.tagValueLength += Integer.BYTES; + } + + public void setTagByte(int index, byte value) { + ensureTagCapacity(index); + this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_TINYINT)); + this.tagValueLength += Byte.BYTES; + } + + public void setTagShort(int index, short value) { + ensureTagCapacity(index); + this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_SMALLINT)); + this.tagValueLength += Short.BYTES; + } + + public void setTagLong(int index, long value) { + ensureTagCapacity(index); + this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_BIGINT)); + this.tagValueLength += Long.BYTES; + } + + public void setTagTimestamp(int index, long value) { + ensureTagCapacity(index); + this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP)); + this.tagValueLength += Long.BYTES; + } + + public void setTagFloat(int index, float value) { + ensureTagCapacity(index); + this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_FLOAT)); + this.tagValueLength += Float.BYTES; + } + + public void setTagDouble(int index, double value) { + ensureTagCapacity(index); + this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_DOUBLE)); + this.tagValueLength += Double.BYTES; + } + + public void setTagString(int index, String value) { + ensureTagCapacity(index); + this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_BINARY)); + this.tagValueLength += value.getBytes().length; + } + + public void setTagNString(int index, String value) { + ensureTagCapacity(index); + this.tableTags.set(index, new TableTagInfo(value, TSDBConstants.TSDB_DATA_TYPE_NCHAR)); + + String charset = TaosGlobalConfig.getCharset(); + try { + this.tagValueLength += value.getBytes(charset).length; + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + + public void setValueImpl(int columnIndex, ArrayList list, int type, int bytes) throws SQLException { + if (this.colData.size() == 0) { + this.colData.addAll(Collections.nCopies(this.parameters.length - 1 - this.tableTags.size(), null)); + } + + ColumnInfo col = (ColumnInfo) this.colData.get(columnIndex); + if (col == null) { + ColumnInfo p = new ColumnInfo(); + p.setType(type); + p.bytes = bytes; + p.data = (ArrayList) list.clone(); + this.colData.set(columnIndex, p); + } else { + if (col.type != type) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "column data type mismatch"); - } - col.data.addAll(list); - } + } + col.data.addAll(list); + } } - + public void setInt(int columnIndex, ArrayList list) throws SQLException { - setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_INT, Integer.BYTES); + setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_INT, Integer.BYTES); } - + public void setFloat(int columnIndex, ArrayList list) throws SQLException { - setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_FLOAT, Float.BYTES); + setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_FLOAT, Float.BYTES); } - + public void setTimestamp(int columnIndex, ArrayList list) throws SQLException { - setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP, Long.BYTES); + setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP, Long.BYTES); } - + public void setLong(int columnIndex, ArrayList list) throws SQLException { - setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_BIGINT, Long.BYTES); + setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_BIGINT, Long.BYTES); } - + public void setDouble(int columnIndex, ArrayList list) throws SQLException { - setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_DOUBLE, Double.BYTES); + setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_DOUBLE, Double.BYTES); } - + public void setBoolean(int columnIndex, ArrayList list) throws SQLException { - setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_BOOL, Byte.BYTES); + setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_BOOL, Byte.BYTES); } - + public void setByte(int columnIndex, ArrayList list) throws SQLException { - setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_TINYINT, Byte.BYTES); + setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_TINYINT, Byte.BYTES); } - + public void setShort(int columnIndex, ArrayList list) throws SQLException { - setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_SMALLINT, Short.BYTES); + setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_SMALLINT, Short.BYTES); } - + public void setString(int columnIndex, ArrayList list, int size) throws SQLException { - setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_BINARY, size); + setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_BINARY, size); } - + // note: expand the required space for each NChar character public void setNString(int columnIndex, ArrayList list, int size) throws SQLException { - setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_NCHAR, size * Integer.BYTES); + setValueImpl(columnIndex, list, TSDBConstants.TSDB_DATA_TYPE_NCHAR, size * Integer.BYTES); } - + public void columnDataAddBatch() throws SQLException { - // pass the data block to native code - if (rawSql == null) { + // pass the data block to native code + if (rawSql == null) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "sql statement not set yet"); - } - - // table name is not set yet, abort - if (this.tableName == null) { + } + + // table name is not set yet, abort + if (this.tableName == null) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "table name not set yet"); - } - - int numOfCols = this.colData.size(); - if (numOfCols == 0) { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "column data not bind"); - } - - TSDBJNIConnector connector = ((TSDBConnection) this.getConnection()).getConnector(); - this.nativeStmtHandle = connector.prepareStmt(rawSql); - - if (this.tableTags == null) { - connector.setBindTableName(this.nativeStmtHandle, this.tableName); - } else { - int num = this.tableTags.size(); - ByteBuffer tagDataList = ByteBuffer.allocate(this.tagValueLength); - tagDataList.order(ByteOrder.LITTLE_ENDIAN); + } - ByteBuffer typeList = ByteBuffer.allocate(num); - typeList.order(ByteOrder.LITTLE_ENDIAN); + int numOfCols = this.colData.size(); + if (numOfCols == 0) { + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "column data not bind"); + } - ByteBuffer lengthList = ByteBuffer.allocate(num * Long.BYTES); - lengthList.order(ByteOrder.LITTLE_ENDIAN); + TSDBJNIConnector connector = ((TSDBConnection) this.getConnection()).getConnector(); + this.nativeStmtHandle = connector.prepareStmt(rawSql); - ByteBuffer isNullList = ByteBuffer.allocate(num * Integer.BYTES); - isNullList.order(ByteOrder.LITTLE_ENDIAN); + if (this.tableTags == null) { + connector.setBindTableName(this.nativeStmtHandle, this.tableName); + } else { + int num = this.tableTags.size(); + ByteBuffer tagDataList = ByteBuffer.allocate(this.tagValueLength); + tagDataList.order(ByteOrder.LITTLE_ENDIAN); - for (int i = 0; i < num; ++i) { - TableTagInfo tag = this.tableTags.get(i); - if (tag.isNull) { - typeList.put((byte) tag.type); - isNullList.putInt(1); - lengthList.putLong(0); - continue; - } - - switch (tag.type) { - case TSDBConstants.TSDB_DATA_TYPE_INT: { - Integer val = (Integer) tag.value; - tagDataList.putInt(val); - lengthList.putLong(Integer.BYTES); - break; - } - case TSDBConstants.TSDB_DATA_TYPE_TINYINT: { - Byte val = (Byte) tag.value; - tagDataList.put(val); - lengthList.putLong(Byte.BYTES); - break; - } + ByteBuffer typeList = ByteBuffer.allocate(num); + typeList.order(ByteOrder.LITTLE_ENDIAN); - case TSDBConstants.TSDB_DATA_TYPE_BOOL: { - Boolean val = (Boolean) tag.value; - tagDataList.put((byte) (val ? 1 : 0)); - lengthList.putLong(Byte.BYTES); - break; - } + ByteBuffer lengthList = ByteBuffer.allocate(num * Long.BYTES); + lengthList.order(ByteOrder.LITTLE_ENDIAN); - case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: { - Short val = (Short) tag.value; - tagDataList.putShort(val); - lengthList.putLong(Short.BYTES); + ByteBuffer isNullList = ByteBuffer.allocate(num * Integer.BYTES); + isNullList.order(ByteOrder.LITTLE_ENDIAN); - break; - } + for (int i = 0; i < num; ++i) { + TableTagInfo tag = this.tableTags.get(i); + if (tag.isNull) { + typeList.put((byte) tag.type); + isNullList.putInt(1); + lengthList.putLong(0); + continue; + } - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: - case TSDBConstants.TSDB_DATA_TYPE_BIGINT: { - Long val = (Long) tag.value; - tagDataList.putLong(val == null ? 0 : val); - lengthList.putLong(Long.BYTES); + switch (tag.type) { + case TSDBConstants.TSDB_DATA_TYPE_INT: { + Integer val = (Integer) tag.value; + tagDataList.putInt(val); + lengthList.putLong(Integer.BYTES); + break; + } + case TSDBConstants.TSDB_DATA_TYPE_TINYINT: { + Byte val = (Byte) tag.value; + tagDataList.put(val); + lengthList.putLong(Byte.BYTES); + break; + } - break; - } + case TSDBConstants.TSDB_DATA_TYPE_BOOL: { + Boolean val = (Boolean) tag.value; + tagDataList.put((byte) (val ? 1 : 0)); + lengthList.putLong(Byte.BYTES); + break; + } - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: { - Float val = (Float) tag.value; - tagDataList.putFloat(val == null ? 0 : val); - lengthList.putLong(Float.BYTES); + case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: { + Short val = (Short) tag.value; + tagDataList.putShort(val); + lengthList.putLong(Short.BYTES); - break; - } + break; + } - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { - Double val = (Double) tag.value; - tagDataList.putDouble(val == null ? 0 : val); - lengthList.putLong(Double.BYTES); + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: { + Long val = (Long) tag.value; + tagDataList.putLong(val == null ? 0 : val); + lengthList.putLong(Long.BYTES); - break; - } + break; + } - case TSDBConstants.TSDB_DATA_TYPE_NCHAR: - case TSDBConstants.TSDB_DATA_TYPE_BINARY: { - String charset = TaosGlobalConfig.getCharset(); - String val = (String) tag.value; + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: { + Float val = (Float) tag.value; + tagDataList.putFloat(val == null ? 0 : val); + lengthList.putLong(Float.BYTES); - byte[] b = null; - try { - if (tag.type == TSDBConstants.TSDB_DATA_TYPE_BINARY) { - b = val.getBytes(); - } else { - b = val.getBytes(charset); - } - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } + break; + } - tagDataList.put(b); - lengthList.putLong(b.length); - break; - } + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { + Double val = (Double) tag.value; + tagDataList.putDouble(val == null ? 0 : val); + lengthList.putLong(Double.BYTES); - case TSDBConstants.TSDB_DATA_TYPE_UTINYINT: - case TSDBConstants.TSDB_DATA_TYPE_USMALLINT: - case TSDBConstants.TSDB_DATA_TYPE_UINT: - case TSDBConstants.TSDB_DATA_TYPE_UBIGINT: { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "not support data types"); - } - } - - typeList.put((byte) tag.type); - isNullList.putInt(tag.isNull? 1 : 0); - } + break; + } - connector.setBindTableNameAndTags(this.nativeStmtHandle, this.tableName, this.tableTags.size(), tagDataList, - typeList, lengthList, isNullList); - } - - ColumnInfo colInfo = (ColumnInfo) this.colData.get(0); - if (colInfo == null) { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "column data not bind"); - } - - int rows = colInfo.data.size(); - for (int i = 0; i < numOfCols; ++i) { - ColumnInfo col1 = this.colData.get(i); - if (col1 == null || !col1.isTypeSet()) { + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: + case TSDBConstants.TSDB_DATA_TYPE_BINARY: { + String charset = TaosGlobalConfig.getCharset(); + String val = (String) tag.value; + + byte[] b = null; + try { + if (tag.type == TSDBConstants.TSDB_DATA_TYPE_BINARY) { + b = val.getBytes(); + } else { + b = val.getBytes(charset); + } + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + tagDataList.put(b); + lengthList.putLong(b.length); + break; + } + + case TSDBConstants.TSDB_DATA_TYPE_UTINYINT: + case TSDBConstants.TSDB_DATA_TYPE_USMALLINT: + case TSDBConstants.TSDB_DATA_TYPE_UINT: + case TSDBConstants.TSDB_DATA_TYPE_UBIGINT: { + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "not support data types"); + } + } + + typeList.put((byte) tag.type); + isNullList.putInt(tag.isNull ? 1 : 0); + } + + connector.setBindTableNameAndTags(this.nativeStmtHandle, this.tableName, this.tableTags.size(), tagDataList, + typeList, lengthList, isNullList); + } + + ColumnInfo colInfo = (ColumnInfo) this.colData.get(0); + if (colInfo == null) { + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "column data not bind"); + } + + int rows = colInfo.data.size(); + for (int i = 0; i < numOfCols; ++i) { + ColumnInfo col1 = this.colData.get(i); + if (col1 == null || !col1.isTypeSet()) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "column data not bind"); - } - - if (rows != col1.data.size()) { + } + + if (rows != col1.data.size()) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "the rows in column data not identical"); - } - - ByteBuffer colDataList = ByteBuffer.allocate(rows * col1.bytes); - colDataList.order(ByteOrder.LITTLE_ENDIAN); - + } + + ByteBuffer colDataList = ByteBuffer.allocate(rows * col1.bytes); + colDataList.order(ByteOrder.LITTLE_ENDIAN); + ByteBuffer lengthList = ByteBuffer.allocate(rows * Integer.BYTES); lengthList.order(ByteOrder.LITTLE_ENDIAN); - + ByteBuffer isNullList = ByteBuffer.allocate(rows * Byte.BYTES); isNullList.order(ByteOrder.LITTLE_ENDIAN); - - switch (col1.type) { - case TSDBConstants.TSDB_DATA_TYPE_INT: { - for (int j = 0; j < rows; ++j) { - Integer val = (Integer) col1.data.get(j); - colDataList.putInt(val == null? Integer.MIN_VALUE:val); - isNullList.put((byte) (val == null? 1:0)); - } - break; - } - - case TSDBConstants.TSDB_DATA_TYPE_TINYINT: { - for (int j = 0; j < rows; ++j) { - Byte val = (Byte) col1.data.get(j); - colDataList.put(val == null? 0:val); - isNullList.put((byte) (val == null? 1:0)); - } - break; - } - - case TSDBConstants.TSDB_DATA_TYPE_BOOL: { - for (int j = 0; j < rows; ++j) { - Boolean val = (Boolean) col1.data.get(j); - if (val == null) { - colDataList.put((byte) 0); - } else { - colDataList.put((byte) (val? 1:0)); - } - - isNullList.put((byte) (val == null? 1:0)); - } - break; - } - - case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: { - for (int j = 0; j < rows; ++j) { - Short val = (Short) col1.data.get(j); - colDataList.putShort(val == null? 0:val); - isNullList.put((byte) (val == null? 1:0)); - } - break; - } - - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: - case TSDBConstants.TSDB_DATA_TYPE_BIGINT: { - for (int j = 0; j < rows; ++j) { - Long val = (Long) col1.data.get(j); - colDataList.putLong(val == null? 0:val); - isNullList.put((byte) (val == null? 1:0)); - } - break; - } - - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: { - for (int j = 0; j < rows; ++j) { - Float val = (Float) col1.data.get(j); - colDataList.putFloat(val == null? 0:val); - isNullList.put((byte) (val == null? 1:0)); - } - break; - } - - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { - for (int j = 0; j < rows; ++j) { - Double val = (Double) col1.data.get(j); - colDataList.putDouble(val == null? 0:val); - isNullList.put((byte) (val == null? 1:0)); - } - break; - } - - case TSDBConstants.TSDB_DATA_TYPE_NCHAR: - case TSDBConstants.TSDB_DATA_TYPE_BINARY: { - String charset = TaosGlobalConfig.getCharset(); - for (int j = 0; j < rows; ++j) { - String val = (String) col1.data.get(j); - colDataList.position(j * col1.bytes); // seek to the correct position - if (val != null) { - byte[] b = null; - try { - if (col1.type == TSDBConstants.TSDB_DATA_TYPE_BINARY) { - b = val.getBytes(); - } else { - b = val.getBytes(charset); - } - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - - if (val.length() > col1.bytes) { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "string data too long"); - } - - colDataList.put(b); - lengthList.putInt(b.length); - isNullList.put((byte) 0); - } else { - lengthList.putInt(0); - isNullList.put((byte) 1); - } - } - break; - } - - case TSDBConstants.TSDB_DATA_TYPE_UTINYINT: - case TSDBConstants.TSDB_DATA_TYPE_USMALLINT: - case TSDBConstants.TSDB_DATA_TYPE_UINT: - case TSDBConstants.TSDB_DATA_TYPE_UBIGINT: { - throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "not support data types"); - } - }; - - connector.bindColumnDataArray(this.nativeStmtHandle, colDataList, lengthList, isNullList, col1.type, col1.bytes, rows, i); - } + switch (col1.type) { + case TSDBConstants.TSDB_DATA_TYPE_INT: { + for (int j = 0; j < rows; ++j) { + Integer val = (Integer) col1.data.get(j); + colDataList.putInt(val == null ? Integer.MIN_VALUE : val); + isNullList.put((byte) (val == null ? 1 : 0)); + } + break; + } + + case TSDBConstants.TSDB_DATA_TYPE_TINYINT: { + for (int j = 0; j < rows; ++j) { + Byte val = (Byte) col1.data.get(j); + colDataList.put(val == null ? 0 : val); + isNullList.put((byte) (val == null ? 1 : 0)); + } + break; + } + + case TSDBConstants.TSDB_DATA_TYPE_BOOL: { + for (int j = 0; j < rows; ++j) { + Boolean val = (Boolean) col1.data.get(j); + if (val == null) { + colDataList.put((byte) 0); + } else { + colDataList.put((byte) (val ? 1 : 0)); + } + + isNullList.put((byte) (val == null ? 1 : 0)); + } + break; + } + + case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: { + for (int j = 0; j < rows; ++j) { + Short val = (Short) col1.data.get(j); + colDataList.putShort(val == null ? 0 : val); + isNullList.put((byte) (val == null ? 1 : 0)); + } + break; + } + + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: { + for (int j = 0; j < rows; ++j) { + Long val = (Long) col1.data.get(j); + colDataList.putLong(val == null ? 0 : val); + isNullList.put((byte) (val == null ? 1 : 0)); + } + break; + } + + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: { + for (int j = 0; j < rows; ++j) { + Float val = (Float) col1.data.get(j); + colDataList.putFloat(val == null ? 0 : val); + isNullList.put((byte) (val == null ? 1 : 0)); + } + break; + } + + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: { + for (int j = 0; j < rows; ++j) { + Double val = (Double) col1.data.get(j); + colDataList.putDouble(val == null ? 0 : val); + isNullList.put((byte) (val == null ? 1 : 0)); + } + break; + } + + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: + case TSDBConstants.TSDB_DATA_TYPE_BINARY: { + String charset = TaosGlobalConfig.getCharset(); + for (int j = 0; j < rows; ++j) { + String val = (String) col1.data.get(j); + + colDataList.position(j * col1.bytes); // seek to the correct position + if (val != null) { + byte[] b = null; + try { + if (col1.type == TSDBConstants.TSDB_DATA_TYPE_BINARY) { + b = val.getBytes(); + } else { + b = val.getBytes(charset); + } + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + if (val.length() > col1.bytes) { + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "string data too long"); + } + + colDataList.put(b); + lengthList.putInt(b.length); + isNullList.put((byte) 0); + } else { + lengthList.putInt(0); + isNullList.put((byte) 1); + } + } + break; + } + + case TSDBConstants.TSDB_DATA_TYPE_UTINYINT: + case TSDBConstants.TSDB_DATA_TYPE_USMALLINT: + case TSDBConstants.TSDB_DATA_TYPE_UINT: + case TSDBConstants.TSDB_DATA_TYPE_UBIGINT: { + throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNKNOWN, "not support data types"); + } + } + ; + + connector.bindColumnDataArray(this.nativeStmtHandle, colDataList, lengthList, isNullList, col1.type, col1.bytes, rows, i); + } } - - public void columnDataExecuteBatch() throws SQLException { - TSDBJNIConnector connector = ((TSDBConnection) this.getConnection()).getConnector(); - connector.executeBatch(this.nativeStmtHandle); - this.columnDataClearBatch(); - } - + + public void columnDataExecuteBatch() throws SQLException { + TSDBJNIConnector connector = ((TSDBConnection) this.getConnection()).getConnector(); + connector.executeBatch(this.nativeStmtHandle); + this.columnDataClearBatch(); + } + public void columnDataClearBatch() { - int size = this.colData.size(); - this.colData.clear(); - + int size = this.colData.size(); + this.colData.clear(); + this.colData.addAll(Collections.nCopies(size, null)); this.tableName = null; // clear the table name } - + public void columnDataCloseBatch() throws SQLException { - TSDBJNIConnector connector = ((TSDBConnection) this.getConnection()).getConnector(); - connector.closeBatch(this.nativeStmtHandle); - - this.nativeStmtHandle = 0L; - this.tableName = null; + TSDBJNIConnector connector = ((TSDBConnection) this.getConnection()).getConnector(); + connector.closeBatch(this.nativeStmtHandle); + + this.nativeStmtHandle = 0L; + this.tableName = null; } } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSet.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSet.java index aba29d602b..59a64ad520 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSet.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSet.java @@ -133,9 +133,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { if (this.getBatchFetch()) return this.blockData.getString(columnIndex - 1); - this.lastWasNull = this.rowData.wasNull(columnIndex - 1); + this.lastWasNull = this.rowData.wasNull(columnIndex); if (!lastWasNull) { - res = this.rowData.getString(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); + int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType(); + res = this.rowData.getString(columnIndex, nativeType); } return res; } @@ -147,9 +148,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { if (this.getBatchFetch()) return this.blockData.getBoolean(columnIndex - 1); - this.lastWasNull = this.rowData.wasNull(columnIndex - 1); + this.lastWasNull = this.rowData.wasNull(columnIndex); if (!lastWasNull) { - res = this.rowData.getBoolean(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); + int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType(); + res = this.rowData.getBoolean(columnIndex, nativeType); } return res; } @@ -161,9 +163,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { if (this.getBatchFetch()) return (byte) this.blockData.getInt(columnIndex - 1); - this.lastWasNull = this.rowData.wasNull(columnIndex - 1); + this.lastWasNull = this.rowData.wasNull(columnIndex); if (!lastWasNull) { - res = (byte) this.rowData.getInt(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); + int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType(); + res = (byte) this.rowData.getInt(columnIndex, nativeType); } return res; } @@ -175,9 +178,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { if (this.getBatchFetch()) return (short) this.blockData.getInt(columnIndex - 1); - this.lastWasNull = this.rowData.wasNull(columnIndex - 1); + this.lastWasNull = this.rowData.wasNull(columnIndex); if (!lastWasNull) { - res = (short) this.rowData.getInt(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); + int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType(); + res = (short) this.rowData.getInt(columnIndex, nativeType); } return res; } @@ -189,9 +193,11 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { if (this.getBatchFetch()) return this.blockData.getInt(columnIndex - 1); - this.lastWasNull = this.rowData.wasNull(columnIndex - 1); + + this.lastWasNull = this.rowData.wasNull(columnIndex); if (!lastWasNull) { - res = this.rowData.getInt(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); + int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType(); + res = this.rowData.getInt(columnIndex, nativeType); } return res; } @@ -203,13 +209,15 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { if (this.getBatchFetch()) return this.blockData.getLong(columnIndex - 1); - this.lastWasNull = this.rowData.wasNull(columnIndex - 1); + this.lastWasNull = this.rowData.wasNull(columnIndex); if (!lastWasNull) { - Object value = this.rowData.get(columnIndex - 1); - if (value instanceof Timestamp) + Object value = this.rowData.getObject(columnIndex); + if (value instanceof Timestamp) { res = ((Timestamp) value).getTime(); - else - res = this.rowData.getLong(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); + } else { + int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType(); + res = this.rowData.getLong(columnIndex, nativeType); + } } return res; } @@ -221,9 +229,11 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { if (this.getBatchFetch()) return (float) this.blockData.getDouble(columnIndex - 1); - this.lastWasNull = this.rowData.wasNull(columnIndex - 1); - if (!lastWasNull) - res = this.rowData.getFloat(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); + this.lastWasNull = this.rowData.wasNull(columnIndex); + if (!lastWasNull) { + int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType(); + res = this.rowData.getFloat(columnIndex, nativeType); + } return res; } @@ -235,9 +245,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { if (this.getBatchFetch()) return this.blockData.getDouble(columnIndex - 1); - this.lastWasNull = this.rowData.wasNull(columnIndex - 1); + this.lastWasNull = this.rowData.wasNull(columnIndex); if (!lastWasNull) { - res = this.rowData.getDouble(columnIndex - 1, this.columnMetaDataList.get(columnIndex - 1).getColType()); + int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType(); + res = this.rowData.getDouble(columnIndex, nativeType); } return res; } @@ -245,34 +256,27 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { public byte[] getBytes(int columnIndex) throws SQLException { checkAvailability(columnIndex, this.columnMetaDataList.size()); - Object value = this.rowData.get(columnIndex - 1); + Object value = this.rowData.getObject(columnIndex); if (value == null) return null; - int colType = this.columnMetaDataList.get(columnIndex - 1).getColType(); - switch (colType) { + int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType(); + switch (nativeType) { case TSDBConstants.TSDB_DATA_TYPE_BIGINT: - return Longs.toByteArray((Long) value); + return Longs.toByteArray((long) value); case TSDBConstants.TSDB_DATA_TYPE_INT: return Ints.toByteArray((int) value); case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: - return Shorts.toByteArray((Short) value); + return Shorts.toByteArray((short) value); case TSDBConstants.TSDB_DATA_TYPE_TINYINT: return new byte[]{(byte) value}; + case TSDBConstants.TSDB_DATA_TYPE_BINARY: + return (byte[]) value; + case TSDBConstants.TSDB_DATA_TYPE_BOOL: + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: + default: + return value.toString().getBytes(); } - return value.toString().getBytes(); - } - - @Override - public Date getDate(int columnIndex) throws SQLException { - Timestamp timestamp = getTimestamp(columnIndex); - return timestamp == null ? null : new Date(timestamp.getTime()); - } - - @Override - public Time getTime(int columnIndex) throws SQLException { - Timestamp timestamp = getTimestamp(columnIndex); - return timestamp == null ? null : new Time(timestamp.getTime()); } public Timestamp getTimestamp(int columnIndex) throws SQLException { @@ -282,9 +286,10 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { if (this.getBatchFetch()) return this.blockData.getTimestamp(columnIndex - 1); - this.lastWasNull = this.rowData.wasNull(columnIndex - 1); + this.lastWasNull = this.rowData.wasNull(columnIndex); if (!lastWasNull) { - res = this.rowData.getTimestamp(columnIndex - 1); + int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType(); + res = this.rowData.getTimestamp(columnIndex, nativeType); } return res; } @@ -304,13 +309,9 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { if (this.getBatchFetch()) return this.blockData.get(columnIndex - 1); - this.lastWasNull = this.rowData.wasNull(columnIndex - 1); + this.lastWasNull = this.rowData.wasNull(columnIndex); if (!lastWasNull) { - int colType = this.columnMetaDataList.get(columnIndex - 1).getColType(); - if (colType == TSDBConstants.TSDB_DATA_TYPE_BINARY) - res = ((String) this.rowData.get(columnIndex - 1)).getBytes(); - else - res = this.rowData.get(columnIndex - 1); + res = this.rowData.getObject(columnIndex); } return res; } @@ -318,7 +319,7 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { public int findColumn(String columnLabel) throws SQLException { for (ColumnMetaData colMetaData : this.columnMetaDataList) { if (colMetaData.getColName() != null && colMetaData.getColName().equalsIgnoreCase(columnLabel)) { - return colMetaData.getColIndex() + 1; + return colMetaData.getColIndex(); } } throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE); @@ -329,25 +330,25 @@ public class TSDBResultSet extends AbstractResultSet implements ResultSet { if (this.getBatchFetch()) return new BigDecimal(this.blockData.getLong(columnIndex - 1)); - this.lastWasNull = this.rowData.wasNull(columnIndex - 1); + this.lastWasNull = this.rowData.wasNull(columnIndex); BigDecimal res = null; if (!lastWasNull) { - int colType = this.columnMetaDataList.get(columnIndex - 1).getColType(); - switch (colType) { + int nativeType = this.columnMetaDataList.get(columnIndex - 1).getColType(); + switch (nativeType) { case TSDBConstants.TSDB_DATA_TYPE_TINYINT: case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: case TSDBConstants.TSDB_DATA_TYPE_INT: case TSDBConstants.TSDB_DATA_TYPE_BIGINT: - res = new BigDecimal(Long.valueOf(this.rowData.get(columnIndex - 1).toString())); + res = new BigDecimal(Long.valueOf(this.rowData.getObject(columnIndex).toString())); break; case TSDBConstants.TSDB_DATA_TYPE_FLOAT: case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: - res = new BigDecimal(Double.valueOf(this.rowData.get(columnIndex - 1).toString())); + res = new BigDecimal(Double.valueOf(this.rowData.getObject(columnIndex).toString())); break; case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: - return new BigDecimal(((Timestamp) this.rowData.get(columnIndex - 1)).getTime()); + return new BigDecimal(((Timestamp) this.rowData.getObject(columnIndex)).getTime()); default: - res = new BigDecimal(this.rowData.get(columnIndex - 1).toString()); + res = new BigDecimal(this.rowData.getObject(columnIndex).toString()); } } return res; diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetMetaData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetMetaData.java index e4ac5303d0..48d4247392 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetMetaData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetMetaData.java @@ -113,6 +113,7 @@ public class TSDBResultSetMetaData extends WrapperImpl implements ResultSetMetaD ColumnMetaData columnMetaData = this.colMetaDataList.get(column - 1); switch (columnMetaData.getColType()) { + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: return 5; case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetRowData.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetRowData.java index 618e896a6d..01104440ab 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetRowData.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBResultSetRowData.java @@ -14,6 +14,8 @@ *****************************************************************************/ package com.taosdata.jdbc; +import com.taosdata.jdbc.utils.NullType; + import java.math.BigDecimal; import java.sql.SQLException; import java.sql.Timestamp; @@ -22,11 +24,13 @@ import java.util.ArrayList; import java.util.Collections; public class TSDBResultSetRowData { + private ArrayList data; - private int colSize = 0; + private int colSize; public TSDBResultSetRowData(int colSize) { - this.setColSize(colSize); + this.colSize = colSize; + this.clear(); } public void clear() { @@ -41,68 +45,104 @@ public class TSDBResultSetRowData { } public boolean wasNull(int col) { - return data.get(col) == null; + return data.get(col - 1) == null; } + /** + * $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api + */ + public void setBooleanValue(int col, boolean value) { + setBoolean(col - 1, value); + } + + /** + * !!! this method is invoked by JNI method and the index start from 0 in C implementations + */ public void setBoolean(int col, boolean value) { data.set(col, value); } - public boolean getBoolean(int col, int srcType) throws SQLException { - Object obj = data.get(col); + public boolean getBoolean(int col, int nativeType) throws SQLException { + Object obj = data.get(col - 1); - switch (srcType) { + switch (nativeType) { case TSDBConstants.TSDB_DATA_TYPE_BOOL: return (Boolean) obj; - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: - return ((Float) obj) == 1.0 ? Boolean.TRUE : Boolean.FALSE; - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: - return ((Double) obj) == 1.0 ? Boolean.TRUE : Boolean.FALSE; case TSDBConstants.TSDB_DATA_TYPE_TINYINT: return ((Byte) obj) == 1 ? Boolean.TRUE : Boolean.FALSE; case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: return ((Short) obj) == 1 ? Boolean.TRUE : Boolean.FALSE; case TSDBConstants.TSDB_DATA_TYPE_INT: return ((Integer) obj) == 1 ? Boolean.TRUE : Boolean.FALSE; - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return ((Long) obj) == 1L ? Boolean.TRUE : Boolean.FALSE; + case TSDBConstants.TSDB_DATA_TYPE_BINARY: + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: { + return obj.toString().contains("1"); + } default: return false; } } + /** + * $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api + */ + public void setByteValue(int colIndex, byte value) { + setByte(colIndex - 1, value); + } + + /** + * !!! this method is invoked by JNI method and the index start from 0 in C implementations + */ public void setByte(int col, byte value) { data.set(col, value); } + /** + * $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api + */ + public void setShortValue(int colIndex, short value) { + setShort(colIndex - 1, value); + } + + /** + * !!! this method is invoked by JNI method and the index start from 0 in C implementations + */ public void setShort(int col, short value) { data.set(col, value); } + /** + * $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api + */ + public void setIntValue(int colIndex, int value) { + setInt(colIndex - 1, value); + } + + /** + * !!! this method is invoked by JNI method and the index start from 0 in C implementations + */ public void setInt(int col, int value) { data.set(col, value); } - @SuppressWarnings("deprecation") - public int getInt(int col, int srcType) throws SQLException { - Object obj = data.get(col); + public int getInt(int col, int nativeType) throws SQLException { + Object obj = data.get(col - 1); + if (obj == null) + return NullType.getIntNull(); - switch (srcType) { + switch (nativeType) { case TSDBConstants.TSDB_DATA_TYPE_BOOL: return Boolean.TRUE.equals(obj) ? 1 : 0; - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: - return ((Float) obj).intValue(); - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: - return ((Double) obj).intValue(); case TSDBConstants.TSDB_DATA_TYPE_TINYINT: return (Byte) obj; case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: return (Short) obj; case TSDBConstants.TSDB_DATA_TYPE_INT: return (Integer) obj; - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: case TSDBConstants.TSDB_DATA_TYPE_BIGINT: + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: return ((Long) obj).intValue(); case TSDBConstants.TSDB_DATA_TYPE_NCHAR: case TSDBConstants.TSDB_DATA_TYPE_BINARY: @@ -131,33 +171,46 @@ public class TSDBResultSetRowData { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE); return Long.valueOf(value).intValue(); } + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: + return ((Float) obj).intValue(); + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: + return ((Double) obj).intValue(); + default: + return 0; } - - return 0; } + /** + * $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api + */ + public void setLongValue(int colIndex, long value) { + setLong(colIndex - 1, value); + } + + /** + * !!! this method is invoked by JNI method and the index start from 0 in C implementations + */ public void setLong(int col, long value) { data.set(col, value); } - public long getLong(int col, int srcType) throws SQLException { - Object obj = data.get(col); + public long getLong(int col, int nativeType) throws SQLException { + Object obj = data.get(col - 1); + if (obj == null) { + return NullType.getBigIntNull(); + } - switch (srcType) { + switch (nativeType) { case TSDBConstants.TSDB_DATA_TYPE_BOOL: return Boolean.TRUE.equals(obj) ? 1 : 0; - case TSDBConstants.TSDB_DATA_TYPE_FLOAT: - return ((Float) obj).longValue(); - case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: - return ((Double) obj).longValue(); case TSDBConstants.TSDB_DATA_TYPE_TINYINT: return (Byte) obj; case TSDBConstants.TSDB_DATA_TYPE_SMALLINT: return (Short) obj; case TSDBConstants.TSDB_DATA_TYPE_INT: return (Integer) obj; - case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: case TSDBConstants.TSDB_DATA_TYPE_BIGINT: + case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: return (Long) obj; case TSDBConstants.TSDB_DATA_TYPE_NCHAR: case TSDBConstants.TSDB_DATA_TYPE_BINARY: @@ -186,19 +239,35 @@ public class TSDBResultSetRowData { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_NUMERIC_VALUE_OUT_OF_RANGE); return value; } + case TSDBConstants.TSDB_DATA_TYPE_FLOAT: + return ((Float) obj).longValue(); + case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: + return ((Double) obj).longValue(); + default: + return 0; } - - return 0; } + /** + * $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api + */ + public void setFloatValue(int colIndex, float value) { + setFloat(colIndex - 1, value); + } + + /** + * !!! this method is invoked by JNI method and the index start from 0 in C implementations + */ public void setFloat(int col, float value) { data.set(col, value); } - public float getFloat(int col, int srcType) { - Object obj = data.get(col); + public float getFloat(int col, int nativeType) { + Object obj = data.get(col - 1); + if (obj == null) + return NullType.getFloatNull(); - switch (srcType) { + switch (nativeType) { case TSDBConstants.TSDB_DATA_TYPE_BOOL: return Boolean.TRUE.equals(obj) ? 1 : 0; case TSDBConstants.TSDB_DATA_TYPE_FLOAT: @@ -214,19 +283,31 @@ public class TSDBResultSetRowData { case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return (Long) obj; + default: + return NullType.getFloatNull(); } - - return 0; } + /** + * $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api + */ + public void setDoubleValue(int colIndex, double value) { + setDouble(colIndex - 1, value); + } + + /** + * !!! this method is invoked by JNI method and the index start from 0 in C implementations + */ public void setDouble(int col, double value) { data.set(col, value); } - public double getDouble(int col, int srcType) { - Object obj = data.get(col); + public double getDouble(int col, int nativeType) { + Object obj = data.get(col - 1); + if (obj == null) + return NullType.getDoubleNull(); - switch (srcType) { + switch (nativeType) { case TSDBConstants.TSDB_DATA_TYPE_BOOL: return Boolean.TRUE.equals(obj) ? 1 : 0; case TSDBConstants.TSDB_DATA_TYPE_FLOAT: @@ -242,16 +323,46 @@ public class TSDBResultSetRowData { case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP: case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return (Long) obj; + default: + return NullType.getDoubleNull(); } - - return 0; } + /** + * $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api + */ + public void setStringValue(int colIndex, String value) { + data.set(colIndex - 1, value); + } + + /** + * !!! this method is invoked by JNI method and the index start from 0 in C implementations + */ public void setString(int col, String value) { - data.set(col, value); + // TODO: + // !!!NOTE!!! + // this is very confusing problem which related to JNI-method implementation, + // the JNI method return a String(encoded in UTF) for BINARY value, which means the JNI method will invoke + // this setString(int, String) to handle BINARY value, we need to build a byte[] with default charsetEncoding + data.set(col, value == null ? null : value.getBytes()); } + /** + * $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api + */ + public void setByteArrayValue(int colIndex, byte[] value) { + setByteArray(colIndex - 1, value); + } + + /** + * !!! this method is invoked by JNI method and the index start from 0 in C implementations + */ public void setByteArray(int col, byte[] value) { + // TODO: + // !!!NOTE!!! + // this is very confusing problem which related to JNI-method implementation, + // the JNI method return a byte[] for NCHAR value, which means the JNI method will invoke + // this setByteArr(int, byte[]) to handle NCHAR value, we need to build a String with charsetEncoding by TaosGlobalConfig try { data.set(col, new String(value, TaosGlobalConfig.getCharset())); } catch (Exception e) { @@ -259,47 +370,56 @@ public class TSDBResultSetRowData { } } - /** - * The original type may not be a string type, but will be converted to by calling this method - * - * @param col column index - * @return - */ - public String getString(int col, int srcType) { - switch (srcType) { - case TSDBConstants.TSDB_DATA_TYPE_BINARY: - case TSDBConstants.TSDB_DATA_TYPE_NCHAR: - return (String) data.get(col); + public String getString(int col, int nativeType) { + Object obj = data.get(col - 1); + if (obj == null) + return null; + + switch (nativeType) { case TSDBConstants.TSDB_DATA_TYPE_UTINYINT: { - Byte value = new Byte(String.valueOf(data.get(col))); + Byte value = new Byte(String.valueOf(obj)); if (value >= 0) return value.toString(); return Integer.toString(value & 0xff); } case TSDBConstants.TSDB_DATA_TYPE_USMALLINT: { - Short value = new Short(String.valueOf(data.get(col))); + Short value = new Short(String.valueOf(obj)); if (value >= 0) return value.toString(); return Integer.toString(value & 0xffff); } case TSDBConstants.TSDB_DATA_TYPE_UINT: { - Integer value = new Integer(String.valueOf(data.get(col))); + Integer value = new Integer(String.valueOf(obj)); if (value >= 0) return value.toString(); return Long.toString(value & 0xffffffffl); } case TSDBConstants.TSDB_DATA_TYPE_UBIGINT: { - Long value = new Long(String.valueOf(data.get(col))); + Long value = new Long(String.valueOf(obj)); if (value >= 0) return value.toString(); long lowValue = value & 0x7fffffffffffffffL; return BigDecimal.valueOf(lowValue).add(BigDecimal.valueOf(Long.MAX_VALUE)).add(BigDecimal.valueOf(1)).toString(); } + case TSDBConstants.TSDB_DATA_TYPE_BINARY: + return new String((byte[]) obj); + case TSDBConstants.TSDB_DATA_TYPE_NCHAR: + return (String) obj; default: - return String.valueOf(data.get(col)); + return String.valueOf(obj); } } + /** + * $$$ this method is invoked by databaseMetaDataResultSet and so on which use a index start from 1 in JDBC api + */ + public void setTimestampValue(int colIndex, long value) { + setTimestamp(colIndex - 1, value); + } + + /** + * !!! this method is invoked by JNI method and the index start from 0 in C implementations + */ public void setTimestamp(int col, long ts) { //TODO: this implementation contains logical error // when precision is us the (long ts) is 16 digital number @@ -316,28 +436,20 @@ public class TSDBResultSetRowData { } } - public Timestamp getTimestamp(int col) { - return (Timestamp) data.get(col); + public Timestamp getTimestamp(int col, int nativeType) { + Object obj = data.get(col - 1); + if (obj == null) + return null; + switch (nativeType) { + case TSDBConstants.TSDB_DATA_TYPE_BIGINT: + return new Timestamp((Long) obj); + default: + return (Timestamp) obj; + } } - public Object get(int col) { - return data.get(col); + public Object getObject(int col) { + return data.get(col - 1); } - public int getColSize() { - return colSize; - } - - private void setColSize(int colSize) { - this.colSize = colSize; - this.clear(); - } - - public ArrayList getData() { - return data; - } - - public void setData(ArrayList data) { - this.data = (ArrayList) data.clone(); - } } diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java index d8ba67576d..e1ebc4ab3c 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/TSDBStatement.java @@ -32,14 +32,15 @@ public class TSDBStatement extends AbstractStatement { } public ResultSet executeQuery(String sql) throws SQLException { - // check if closed if (isClosed()) { throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_STATEMENT_CLOSED); } - - //TODO: 如果在executeQuery方法中执行insert语句,那么先执行了SQL,再通过pSql来检查是否为一个insert语句,但这个insert SQL已经执行成功了 - - // execute query + //TODO: + // this is an unreasonable implementation, if the paratemer is a insert statement, + // the JNI connector will execute the sql at first and return a pointer: pSql, + // we use this pSql and invoke the isUpdateQuery(long pSql) method to decide . + // but the insert sql is already executed in database. + //execute query long pSql = this.connection.getConnector().executeQuery(sql); // if pSql is create/insert/update/delete/alter SQL if (this.connection.getConnector().isUpdateQuery(pSql)) { diff --git a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/Utils.java b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/Utils.java index eeb936a1d0..8ab610fec6 100644 --- a/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/Utils.java +++ b/src/connector/jdbc/src/main/java/com/taosdata/jdbc/utils/Utils.java @@ -95,16 +95,7 @@ public class Utils { public static String getNativeSql(String rawSql, Object[] parameters) { // toLowerCase String preparedSql = rawSql.trim().toLowerCase(); - - String[] clause = new String[0]; - if (SqlSyntaxValidator.isInsertSql(preparedSql)) { - // insert or import - clause = new String[]{"values\\s*\\(.*?\\)", "tags\\s*\\(.*?\\)"}; - } - if (SqlSyntaxValidator.isSelectSql(preparedSql)) { - // select - clause = new String[]{"where\\s*.*"}; - } + String[] clause = new String[]{"values\\s*\\(.*?\\)", "tags\\s*\\(.*?\\)", "where\\s*.*"}; Map placeholderPositions = new HashMap<>(); RangeSet clauseRangeSet = TreeRangeSet.create(); findPlaceholderPosition(preparedSql, placeholderPositions); diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBConnectionTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBConnectionTest.java index ca7251bb0e..dc6d0d322a 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBConnectionTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBConnectionTest.java @@ -32,20 +32,34 @@ public class TSDBConnectionTest { } @Test - public void subscribe() { + public void runSubscribe() { try { + // given TSDBConnection unwrap = conn.unwrap(TSDBConnection.class); TSDBSubscribe subscribe = unwrap.subscribe("topic1", "select * from log.log", false); + // when TSDBResultSet rs = subscribe.consume(); ResultSetMetaData metaData = rs.getMetaData(); - for (int count = 0; count < 10 && rs.next(); count++) { - for (int i = 1; i <= metaData.getColumnCount(); i++) { - String value = rs.getString(i); - System.out.print(metaData.getColumnLabel(i) + ":" + value + "\t"); - } - System.out.println(); - } + + // then Assert.assertNotNull(rs); + Assert.assertEquals(4, metaData.getColumnCount()); + Assert.assertEquals("ts", metaData.getColumnLabel(1)); + Assert.assertEquals("level", metaData.getColumnLabel(2)); + Assert.assertEquals("content", metaData.getColumnLabel(3)); + Assert.assertEquals("ipaddr", metaData.getColumnLabel(4)); + rs.next(); + // row 1 + { + Assert.assertNotNull(rs.getTimestamp(1)); + Assert.assertNotNull(rs.getTimestamp("ts")); + Assert.assertNotNull(rs.getByte(2)); + Assert.assertNotNull(rs.getByte("level")); + Assert.assertNotNull(rs.getString(3)); + Assert.assertNotNull(rs.getString("content")); + Assert.assertNotNull(rs.getString(4)); + Assert.assertNotNull(rs.getString("ipaddr")); + } subscribe.close(false); } catch (SQLException e) { e.printStackTrace(); diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBDatabaseMetaDataTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBDatabaseMetaDataTest.java index a7657fa3e5..cb6133015c 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBDatabaseMetaDataTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBDatabaseMetaDataTest.java @@ -7,9 +7,11 @@ import java.util.Properties; public class TSDBDatabaseMetaDataTest { private static final String host = "127.0.0.1"; + private static final String url = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata"; private static Connection connection; private static TSDBDatabaseMetaData metaData; + @Test public void unwrap() throws SQLException { TSDBDatabaseMetaData unwrap = metaData.unwrap(TSDBDatabaseMetaData.class); @@ -33,7 +35,7 @@ public class TSDBDatabaseMetaDataTest { @Test public void getURL() throws SQLException { - Assert.assertEquals("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata", metaData.getURL()); + Assert.assertEquals(url, metaData.getURL()); } @Test @@ -627,17 +629,32 @@ public class TSDBDatabaseMetaDataTest { @Test public void getTables() throws SQLException { - System.out.println("****************************************************"); - ResultSet tables = metaData.getTables("log", "", null, null); - ResultSetMetaData metaData = tables.getMetaData(); - while (tables.next()) { - System.out.print(metaData.getColumnLabel(1) + ":" + tables.getString(1) + "\t"); - System.out.print(metaData.getColumnLabel(3) + ":" + tables.getString(3) + "\t"); - System.out.print(metaData.getColumnLabel(4) + ":" + tables.getString(4) + "\t"); - System.out.print(metaData.getColumnLabel(5) + ":" + tables.getString(5) + "\n"); + ResultSet rs = metaData.getTables("log", "", null, null); + ResultSetMetaData meta = rs.getMetaData(); + Assert.assertNotNull(rs); + rs.next(); + { + // TABLE_CAT + Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1)); + Assert.assertEquals("log", rs.getString(1)); + Assert.assertEquals("log", rs.getString("TABLE_CAT")); + // TABLE_SCHEM + Assert.assertEquals("TABLE_SCHEM", meta.getColumnLabel(2)); + Assert.assertEquals(null, rs.getString(2)); + Assert.assertEquals(null, rs.getString("TABLE_SCHEM")); + // TABLE_NAME + Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3)); + Assert.assertNotNull(rs.getString(3)); + Assert.assertNotNull(rs.getString("TABLE_NAME")); + // TABLE_TYPE + Assert.assertEquals("TABLE_TYPE", meta.getColumnLabel(4)); + Assert.assertEquals("TABLE", rs.getString(4)); + Assert.assertEquals("TABLE", rs.getString("TABLE_TYPE")); + // REMARKS + Assert.assertEquals("REMARKS", meta.getColumnLabel(5)); + Assert.assertEquals("", rs.getString(5)); + Assert.assertEquals("", rs.getString("REMARKS")); } - System.out.println(); - Assert.assertNotNull(tables); } @Test @@ -647,46 +664,130 @@ public class TSDBDatabaseMetaDataTest { @Test public void getCatalogs() throws SQLException { - System.out.println("****************************************************"); - - ResultSet catalogs = metaData.getCatalogs(); - ResultSetMetaData meta = catalogs.getMetaData(); - while (catalogs.next()) { - for (int i = 1; i <= meta.getColumnCount(); i++) { - System.out.print(meta.getColumnLabel(i) + ": " + catalogs.getString(i)); - } - System.out.println(); + ResultSet rs = metaData.getCatalogs(); + ResultSetMetaData meta = rs.getMetaData(); + rs.next(); + { + // TABLE_CAT + Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1)); + Assert.assertNotNull(rs.getString(1)); + Assert.assertNotNull(rs.getString("TABLE_CAT")); } } @Test public void getTableTypes() throws SQLException { - System.out.println("****************************************************"); - ResultSet tableTypes = metaData.getTableTypes(); - while (tableTypes.next()) { - System.out.println(tableTypes.getString("TABLE_TYPE")); + tableTypes.next(); + // tableTypes: table + { + Assert.assertEquals("TABLE", tableTypes.getString(1)); + Assert.assertEquals("TABLE", tableTypes.getString("TABLE_TYPE")); + } + tableTypes.next(); + // tableTypes: stable + { + Assert.assertEquals("STABLE", tableTypes.getString(1)); + Assert.assertEquals("STABLE", tableTypes.getString("TABLE_TYPE")); } - Assert.assertNotNull(metaData.getTableTypes()); } @Test public void getColumns() throws SQLException { - System.out.println("****************************************************"); - + // when ResultSet columns = metaData.getColumns("log", "", "dn", ""); + // then ResultSetMetaData meta = columns.getMetaData(); - while (columns.next()) { - System.out.print(meta.getColumnLabel(1) + ": " + columns.getString(1) + "\t"); - System.out.print(meta.getColumnLabel(3) + ": " + columns.getString(3) + "\t"); - System.out.print(meta.getColumnLabel(4) + ": " + columns.getString(4) + "\t"); - System.out.print(meta.getColumnLabel(5) + ": " + columns.getString(5) + "\t"); - System.out.print(meta.getColumnLabel(6) + ": " + columns.getString(6) + "\t"); - System.out.print(meta.getColumnLabel(7) + ": " + columns.getString(7) + "\t"); - System.out.print(meta.getColumnLabel(9) + ": " + columns.getString(9) + "\t"); - System.out.print(meta.getColumnLabel(10) + ": " + columns.getString(10) + "\t"); - System.out.print(meta.getColumnLabel(11) + ": " + columns.getString(11) + "\n"); - System.out.print(meta.getColumnLabel(12) + ": " + columns.getString(12) + "\n"); + columns.next(); + // column: 1 + { + // TABLE_CAT + Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1)); + Assert.assertEquals("log", columns.getString(1)); + Assert.assertEquals("log", columns.getString("TABLE_CAT")); + // TABLE_NAME + Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3)); + Assert.assertEquals("dn", columns.getString(3)); + Assert.assertEquals("dn", columns.getString("TABLE_NAME")); + // COLUMN_NAME + Assert.assertEquals("COLUMN_NAME", meta.getColumnLabel(4)); + Assert.assertEquals("ts", columns.getString(4)); + Assert.assertEquals("ts", columns.getString("COLUMN_NAME")); + // DATA_TYPE + Assert.assertEquals("DATA_TYPE", meta.getColumnLabel(5)); + Assert.assertEquals(Types.TIMESTAMP, columns.getInt(5)); + Assert.assertEquals(Types.TIMESTAMP, columns.getInt("DATA_TYPE")); + // TYPE_NAME + Assert.assertEquals("TYPE_NAME", meta.getColumnLabel(6)); + Assert.assertEquals("TIMESTAMP", columns.getString(6)); + Assert.assertEquals("TIMESTAMP", columns.getString("TYPE_NAME")); + // COLUMN_SIZE + Assert.assertEquals("COLUMN_SIZE", meta.getColumnLabel(7)); + Assert.assertEquals(26, columns.getInt(7)); + Assert.assertEquals(26, columns.getInt("COLUMN_SIZE")); + // DECIMAL_DIGITS + Assert.assertEquals("DECIMAL_DIGITS", meta.getColumnLabel(9)); + Assert.assertEquals(Integer.MIN_VALUE, columns.getInt(9)); + Assert.assertEquals(Integer.MIN_VALUE, columns.getInt("DECIMAL_DIGITS")); + Assert.assertEquals(null, columns.getString(9)); + Assert.assertEquals(null, columns.getString("DECIMAL_DIGITS")); + // NUM_PREC_RADIX + Assert.assertEquals("NUM_PREC_RADIX", meta.getColumnLabel(10)); + Assert.assertEquals(10, columns.getInt(10)); + Assert.assertEquals(10, columns.getInt("NUM_PREC_RADIX")); + // NULLABLE + Assert.assertEquals("NULLABLE", meta.getColumnLabel(11)); + Assert.assertEquals(DatabaseMetaData.columnNoNulls, columns.getInt(11)); + Assert.assertEquals(DatabaseMetaData.columnNoNulls, columns.getInt("NULLABLE")); + // REMARKS + Assert.assertEquals("REMARKS", meta.getColumnLabel(12)); + Assert.assertEquals(null, columns.getString(12)); + Assert.assertEquals(null, columns.getString("REMARKS")); + } + columns.next(); + // column: 2 + { + // TABLE_CAT + Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1)); + Assert.assertEquals("log", columns.getString(1)); + Assert.assertEquals("log", columns.getString("TABLE_CAT")); + // TABLE_NAME + Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3)); + Assert.assertEquals("dn", columns.getString(3)); + Assert.assertEquals("dn", columns.getString("TABLE_NAME")); + // COLUMN_NAME + Assert.assertEquals("COLUMN_NAME", meta.getColumnLabel(4)); + Assert.assertEquals("cpu_taosd", columns.getString(4)); + Assert.assertEquals("cpu_taosd", columns.getString("COLUMN_NAME")); + // DATA_TYPE + Assert.assertEquals("DATA_TYPE", meta.getColumnLabel(5)); + Assert.assertEquals(Types.FLOAT, columns.getInt(5)); + Assert.assertEquals(Types.FLOAT, columns.getInt("DATA_TYPE")); + // TYPE_NAME + Assert.assertEquals("TYPE_NAME", meta.getColumnLabel(6)); + Assert.assertEquals("FLOAT", columns.getString(6)); + Assert.assertEquals("FLOAT", columns.getString("TYPE_NAME")); + // COLUMN_SIZE + Assert.assertEquals("COLUMN_SIZE", meta.getColumnLabel(7)); + Assert.assertEquals(12, columns.getInt(7)); + Assert.assertEquals(12, columns.getInt("COLUMN_SIZE")); + // DECIMAL_DIGITS + Assert.assertEquals("DECIMAL_DIGITS", meta.getColumnLabel(9)); + Assert.assertEquals(Integer.MIN_VALUE, columns.getInt(9)); + Assert.assertEquals(Integer.MIN_VALUE, columns.getInt("DECIMAL_DIGITS")); + Assert.assertEquals(null, columns.getString(9)); + Assert.assertEquals(null, columns.getString("DECIMAL_DIGITS")); + // NUM_PREC_RADIX + Assert.assertEquals("NUM_PREC_RADIX", meta.getColumnLabel(10)); + Assert.assertEquals(10, columns.getInt(10)); + Assert.assertEquals(10, columns.getInt("NUM_PREC_RADIX")); + // NULLABLE + Assert.assertEquals("NULLABLE", meta.getColumnLabel(11)); + Assert.assertEquals(DatabaseMetaData.columnNullable, columns.getInt(11)); + Assert.assertEquals(DatabaseMetaData.columnNullable, columns.getInt("NULLABLE")); + // REMARKS + Assert.assertEquals("REMARKS", meta.getColumnLabel(12)); + Assert.assertEquals(null, columns.getString(12)); } } @@ -712,17 +813,35 @@ public class TSDBDatabaseMetaDataTest { @Test public void getPrimaryKeys() throws SQLException { - System.out.println("****************************************************"); - ResultSet rs = metaData.getPrimaryKeys("log", "", "dn1"); - while (rs.next()) { - System.out.println("TABLE_NAME: " + rs.getString("TABLE_NAME")); - System.out.println("COLUMN_NAME: " + rs.getString("COLUMN_NAME")); - System.out.println("KEY_SEQ: " + rs.getString("KEY_SEQ")); - System.out.println("PK_NAME: " + rs.getString("PK_NAME")); + ResultSetMetaData meta = rs.getMetaData(); + rs.next(); + { + // TABLE_CAT + Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1)); + Assert.assertEquals("log", rs.getString(1)); + Assert.assertEquals("log", rs.getString("TABLE_CAT")); + // TABLE_SCHEM + Assert.assertEquals("TABLE_SCHEM", meta.getColumnLabel(2)); + Assert.assertEquals(null, rs.getString(2)); + Assert.assertEquals(null, rs.getString("TABLE_SCHEM")); + // TABLE_NAME + Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3)); + Assert.assertEquals("dn1", rs.getString(3)); + Assert.assertEquals("dn1", rs.getString("TABLE_NAME")); + // COLUMN_NAME + Assert.assertEquals("COLUMN_NAME", meta.getColumnLabel(4)); + Assert.assertEquals("ts", rs.getString(4)); + Assert.assertEquals("ts", rs.getString("COLUMN_NAME")); + // KEY_SEQ + Assert.assertEquals("KEY_SEQ", meta.getColumnLabel(5)); + Assert.assertEquals(1, rs.getShort(5)); + Assert.assertEquals(1, rs.getShort("KEY_SEQ")); + // DATA_TYPE + Assert.assertEquals("PK_NAME", meta.getColumnLabel(6)); + Assert.assertEquals("ts", rs.getString(6)); + Assert.assertEquals("ts", rs.getString("PK_NAME")); } - - Assert.assertNotNull(rs); } @Test @@ -847,14 +966,27 @@ public class TSDBDatabaseMetaDataTest { @Test public void getSuperTables() throws SQLException { - System.out.println("****************************************************"); - ResultSet rs = metaData.getSuperTables("log", "", "dn1"); - while (rs.next()) { - System.out.println("TABLE_NAME: " + rs.getString("TABLE_NAME")); - System.out.println("SUPERTABLE_NAME: " + rs.getString("SUPERTABLE_NAME")); + ResultSetMetaData meta = rs.getMetaData(); + rs.next(); + { + // TABLE_CAT + Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1)); + Assert.assertEquals("log", rs.getString(1)); + Assert.assertEquals("log", rs.getString("TABLE_CAT")); + // TABLE_CAT + Assert.assertEquals("TABLE_SCHEM", meta.getColumnLabel(2)); + Assert.assertEquals(null, rs.getString(2)); + Assert.assertEquals(null, rs.getString("TABLE_SCHEM")); + // TABLE_CAT + Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3)); + Assert.assertEquals("dn1", rs.getString(3)); + Assert.assertEquals("dn1", rs.getString("TABLE_NAME")); + // TABLE_CAT + Assert.assertEquals("SUPERTABLE_NAME", meta.getColumnLabel(4)); + Assert.assertEquals("dn", rs.getString(4)); + Assert.assertEquals("dn", rs.getString("SUPERTABLE_NAME")); } - Assert.assertNotNull(rs); } @Test @@ -951,15 +1083,12 @@ public class TSDBDatabaseMetaDataTest { @BeforeClass public static void beforeClass() { try { - Class.forName("com.taosdata.jdbc.TSDBDriver"); Properties properties = new Properties(); properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); - connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata", properties); + connection = DriverManager.getConnection(url, properties); metaData = connection.getMetaData().unwrap(TSDBDatabaseMetaData.class); - } catch (ClassNotFoundException e) { - e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java index 161539962d..66078ef503 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBJNIConnectorTest.java @@ -45,9 +45,9 @@ public class TSDBJNIConnectorTest { rowData = new TSDBResultSetRowData(columnSize); // iterate resultSet for (int i = 0; next(connector, pSql); i++) { - System.out.println("col[" + i + "] size: " + rowData.getColSize()); - rowData.getData().stream().forEach(col -> System.out.print(col + "\t")); - System.out.println(); +// System.out.println("col[" + i + "] size: " + rowData.getColSize()); +// rowData.getData().stream().forEach(col -> System.out.print(col + "\t")); +// System.out.println(); } // close resultSet code = connector.freeResultSet(pSql); diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBParameterMetaDataTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBParameterMetaDataTest.java index 12bcc43917..83caf1bebb 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBParameterMetaDataTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBParameterMetaDataTest.java @@ -54,16 +54,17 @@ public class TSDBParameterMetaDataTest { @Test public void getPrecision() throws SQLException { - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(1)); - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(2)); - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(3)); - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(4)); - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(5)); - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(6)); - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(7)); - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(8)); - Assert.assertEquals(5, parameterMetaData_insert.getPrecision(9)); - Assert.assertEquals(5, parameterMetaData_insert.getPrecision(10)); + //create table weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(64), f9 nchar(64)) + Assert.assertEquals(TSDBConstants.TIMESTAMP_MS_PRECISION, parameterMetaData_insert.getPrecision(1)); + Assert.assertEquals(TSDBConstants.INT_PRECISION, parameterMetaData_insert.getPrecision(2)); + Assert.assertEquals(TSDBConstants.BIGINT_PRECISION, parameterMetaData_insert.getPrecision(3)); + Assert.assertEquals(TSDBConstants.FLOAT_PRECISION, parameterMetaData_insert.getPrecision(4)); + Assert.assertEquals(TSDBConstants.DOUBLE_PRECISION, parameterMetaData_insert.getPrecision(5)); + Assert.assertEquals(TSDBConstants.SMALLINT_PRECISION, parameterMetaData_insert.getPrecision(6)); + Assert.assertEquals(TSDBConstants.TINYINT_PRECISION, parameterMetaData_insert.getPrecision(7)); + Assert.assertEquals(TSDBConstants.BOOLEAN_PRECISION, parameterMetaData_insert.getPrecision(8)); + Assert.assertEquals("hello".getBytes().length, parameterMetaData_insert.getPrecision(9)); + Assert.assertEquals("涛思数据".length(), parameterMetaData_insert.getPrecision(10)); } @Test @@ -71,8 +72,8 @@ public class TSDBParameterMetaDataTest { Assert.assertEquals(0, parameterMetaData_insert.getScale(1)); Assert.assertEquals(0, parameterMetaData_insert.getScale(2)); Assert.assertEquals(0, parameterMetaData_insert.getScale(3)); - Assert.assertEquals(0, parameterMetaData_insert.getScale(4)); - Assert.assertEquals(0, parameterMetaData_insert.getScale(5)); + Assert.assertEquals(31, parameterMetaData_insert.getScale(4)); + Assert.assertEquals(31, parameterMetaData_insert.getScale(5)); Assert.assertEquals(0, parameterMetaData_insert.getScale(6)); Assert.assertEquals(0, parameterMetaData_insert.getScale(7)); Assert.assertEquals(0, parameterMetaData_insert.getScale(8)); @@ -124,10 +125,16 @@ public class TSDBParameterMetaDataTest { @Test public void getParameterMode() throws SQLException { - for (int i = 1; i <= parameterMetaData_insert.getParameterCount(); i++) { - int parameterMode = parameterMetaData_insert.getParameterMode(i); - Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMode); - } + Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(1)); + Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(2)); + Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(3)); + Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(4)); + Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(5)); + Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(6)); + Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(7)); + Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(8)); + Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(9)); + Assert.assertEquals(ParameterMetaData.parameterModeUnknown, parameterMetaData_insert.getParameterMode(10)); } @Test @@ -144,7 +151,6 @@ public class TSDBParameterMetaDataTest { @BeforeClass public static void beforeClass() { try { - Class.forName("com.taosdata.jdbc.TSDBDriver"); conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata"); try (Statement stmt = conn.createStatement()) { stmt.execute("drop database if exists test_pstmt"); @@ -164,7 +170,7 @@ public class TSDBParameterMetaDataTest { pstmt_insert.setObject(7, Byte.MAX_VALUE); pstmt_insert.setObject(8, true); pstmt_insert.setObject(9, "hello".getBytes()); - pstmt_insert.setObject(10, "Hello"); + pstmt_insert.setObject(10, "涛思数据"); parameterMetaData_insert = pstmt_insert.getParameterMetaData(); pstmt_select = conn.prepareStatement(sql_select); @@ -173,7 +179,7 @@ public class TSDBParameterMetaDataTest { pstmt_select.setInt(3, 0); parameterMetaData_select = pstmt_select.getParameterMetaData(); - } catch (ClassNotFoundException | SQLException e) { + } catch (SQLException e) { e.printStackTrace(); } } diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java index 277ca447f5..ab7cf6c8c0 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBPreparedStatementTest.java @@ -1,523 +1,644 @@ package com.taosdata.jdbc; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.*; import java.io.IOException; -import java.io.Serializable; +import java.math.BigDecimal; import java.sql.*; -import java.util.ArrayList; -import java.util.Random; +import java.time.LocalTime; public class TSDBPreparedStatementTest { + private static final String host = "127.0.0.1"; private static Connection conn; private static final String sql_insert = "insert into t1 values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; - private static PreparedStatement pstmt_insert; - private static final String sql_select = "select * from t1 where ts > ? and ts <= ? and f1 >= ?"; - private static PreparedStatement pstmt_select; + private static final String sql_select = "select * from t1 where ts >= ? and ts < ? and f1 >= ?"; + + private PreparedStatement pstmt_insert; + private PreparedStatement pstmt_select; + //create table weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(64), f9 nchar(64)) tags(loc nchar(64)) @Test public void executeQuery() throws SQLException { - long end = System.currentTimeMillis(); - long start = end - 1000 * 60 * 60; + // given + long ts = System.currentTimeMillis(); + pstmt_insert.setTimestamp(1, new Timestamp(ts)); + pstmt_insert.setInt(2, 2); + pstmt_insert.setLong(3, 3l); + pstmt_insert.setFloat(4, 3.14f); + pstmt_insert.setDouble(5, 3.1415); + pstmt_insert.setShort(6, (short) 6); + pstmt_insert.setByte(7, (byte) 7); + pstmt_insert.setBoolean(8, true); + pstmt_insert.setBytes(9, "abc".getBytes()); + pstmt_insert.setString(10, "涛思数据"); + pstmt_insert.executeUpdate(); + long start = ts - 1000 * 60 * 60; + long end = ts + 1000 * 60 * 60; pstmt_select.setTimestamp(1, new Timestamp(start)); pstmt_select.setTimestamp(2, new Timestamp(end)); pstmt_select.setInt(3, 0); + // when ResultSet rs = pstmt_select.executeQuery(); - Assert.assertNotNull(rs); ResultSetMetaData meta = rs.getMetaData(); - while (rs.next()) { - for (int i = 1; i <= meta.getColumnCount(); i++) { - System.out.print(meta.getColumnLabel(i) + ": " + rs.getString(i) + "\t"); - } - System.out.println(); + rs.next(); + + // then + assertMetaData(meta); + { + Assert.assertNotNull(rs); + Assert.assertEquals(ts, rs.getTimestamp(1).getTime()); + Assert.assertEquals(2, rs.getInt(2)); + Assert.assertEquals(2, rs.getInt("f1")); + Assert.assertEquals(3l, rs.getLong(3)); + Assert.assertEquals(3l, rs.getLong("f2")); + Assert.assertEquals(3.14f, rs.getFloat(4), 0.0); + Assert.assertEquals(3.14f, rs.getFloat("f3"), 0.0); + Assert.assertEquals(3.1415, rs.getDouble(5), 0.0); + Assert.assertEquals(3.1415, rs.getDouble("f4"), 0.0); + Assert.assertEquals((short) 6, rs.getShort(6)); + Assert.assertEquals((short) 6, rs.getShort("f5")); + Assert.assertEquals((byte) 7, rs.getByte(7)); + Assert.assertEquals((byte) 7, rs.getByte("f6")); + Assert.assertTrue(rs.getBoolean(8)); + Assert.assertTrue(rs.getBoolean("f7")); + Assert.assertArrayEquals("abc".getBytes(), rs.getBytes(9)); + Assert.assertArrayEquals("abc".getBytes(), rs.getBytes("f8")); + Assert.assertEquals("涛思数据", rs.getString(10)); + Assert.assertEquals("涛思数据", rs.getString("f9")); } } - @Test - public void executeUpdate() throws SQLException { - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - pstmt_insert.setFloat(4, 3.14f); - int result = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, result); + private void assertMetaData(ResultSetMetaData meta) throws SQLException { + Assert.assertEquals(10, meta.getColumnCount()); + Assert.assertEquals("ts", meta.getColumnLabel(1)); + Assert.assertEquals("f1", meta.getColumnLabel(2)); + Assert.assertEquals("f2", meta.getColumnLabel(3)); + Assert.assertEquals("f3", meta.getColumnLabel(4)); + Assert.assertEquals("f4", meta.getColumnLabel(5)); + Assert.assertEquals("f5", meta.getColumnLabel(6)); + Assert.assertEquals("f6", meta.getColumnLabel(7)); + Assert.assertEquals("f7", meta.getColumnLabel(8)); + Assert.assertEquals("f8", meta.getColumnLabel(9)); + Assert.assertEquals("f9", meta.getColumnLabel(10)); } @Test - public void setNull() throws SQLException { - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + public void setNullForTimestamp() throws SQLException { + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setNull(2, Types.INTEGER); int result = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, result); - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + assertAllNullExceptTimestamp(rs, ts); + } + } + + private void assertAllNullExceptTimestamp(ResultSet rs, long ts) throws SQLException { + Assert.assertNotNull(rs); + Assert.assertEquals(ts, rs.getTimestamp(1).getTime()); + Assert.assertEquals(0, rs.getInt(2)); + Assert.assertEquals(0, rs.getInt("f1")); + Assert.assertEquals(0, rs.getLong(3)); + Assert.assertEquals(0, rs.getLong("f2")); + Assert.assertEquals(0, rs.getFloat(4), 0.0); + Assert.assertEquals(0, rs.getFloat("f3"), 0.0); + Assert.assertEquals(0, rs.getDouble(5), 0.0); + Assert.assertEquals(0, rs.getDouble("f4"), 0.0); + Assert.assertEquals(0, rs.getShort(6)); + Assert.assertEquals(0, rs.getShort("f5")); + Assert.assertEquals(0, rs.getByte(7)); + Assert.assertEquals(0, rs.getByte("f6")); + Assert.assertFalse(rs.getBoolean(8)); + Assert.assertFalse(rs.getBoolean("f7")); + Assert.assertNull(rs.getBytes(9)); + Assert.assertNull(rs.getBytes("f8")); + Assert.assertNull(rs.getString(10)); + Assert.assertNull(rs.getString("f9")); + } + + @Test + public void setNullForInteger() throws SQLException { + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setNull(3, Types.BIGINT); - result = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, result); + int result = pstmt_insert.executeUpdate(); - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + assertAllNullExceptTimestamp(rs, ts); + } + } + + @Test + public void setNullForFloat() throws SQLException { + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setNull(4, Types.FLOAT); - result = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, result); + int result = pstmt_insert.executeUpdate(); - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + assertAllNullExceptTimestamp(rs, ts); + } + } + + @Test + public void setNullForDouble() throws SQLException { + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setNull(5, Types.DOUBLE); - result = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, result); + int result = pstmt_insert.executeUpdate(); - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + assertAllNullExceptTimestamp(rs, ts); + } + } + + @Test + public void setNullForSmallInt() throws SQLException { + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setNull(6, Types.SMALLINT); - result = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, result); + int result = pstmt_insert.executeUpdate(); - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + assertAllNullExceptTimestamp(rs, ts); + } + } + + @Test + public void setNullForTinyInt() throws SQLException { + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setNull(7, Types.TINYINT); - result = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, result); + int result = pstmt_insert.executeUpdate(); - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + assertAllNullExceptTimestamp(rs, ts); + } + } + + @Test + public void setNullForBoolean() throws SQLException { + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setNull(8, Types.BOOLEAN); - result = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, result); + int result = pstmt_insert.executeUpdate(); - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + assertAllNullExceptTimestamp(rs, ts); + } + } + + @Test + public void setNullForBinary() throws SQLException { + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setNull(9, Types.BINARY); - result = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, result); + int result = pstmt_insert.executeUpdate(); - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + assertAllNullExceptTimestamp(rs, ts); + } + } + + @Test + public void setNullForNchar() throws SQLException { + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setNull(10, Types.NCHAR); - result = pstmt_insert.executeUpdate(); + int result = pstmt_insert.executeUpdate(); + + // then Assert.assertEquals(1, result); - - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - pstmt_insert.setNull(10, Types.OTHER); - result = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, result); - } - - @Test - public void executeTest() throws SQLException { - Statement stmt = conn.createStatement(); - - int numOfRows = 1000; - - for (int loop = 0; loop < 10; loop++){ - stmt.execute("drop table if exists weather_test"); - stmt.execute("create table weather_test(ts timestamp, f1 nchar(4), f2 float, f3 double, f4 timestamp, f5 int, f6 bool, f7 binary(10))"); - - TSDBPreparedStatement s = (TSDBPreparedStatement) conn.prepareStatement("insert into ? values(?, ?, ?, ?, ?, ?, ?, ?)"); - Random r = new Random(); - s.setTableName("weather_test"); - - ArrayList ts = new ArrayList(); - for(int i = 0; i < numOfRows; i++) { - ts.add(System.currentTimeMillis() + i); - } - s.setTimestamp(0, ts); - - int random = 10 + r.nextInt(5); - ArrayList s2 = new ArrayList(); - for(int i = 0; i < numOfRows; i++) { - if(i % random == 0) { - s2.add(null); - }else{ - s2.add("分支" + i % 4); - } - } - s.setNString(1, s2, 4); - - random = 10 + r.nextInt(5); - ArrayList s3 = new ArrayList(); - for(int i = 0; i < numOfRows; i++) { - if(i % random == 0) { - s3.add(null); - }else{ - s3.add(r.nextFloat()); - } - } - s.setFloat(2, s3); - - random = 10 + r.nextInt(5); - ArrayList s4 = new ArrayList(); - for(int i = 0; i < numOfRows; i++) { - if(i % random == 0) { - s4.add(null); - }else{ - s4.add(r.nextDouble()); - } - } - s.setDouble(3, s4); - - random = 10 + r.nextInt(5); - ArrayList ts2 = new ArrayList(); - for(int i = 0; i < numOfRows; i++) { - if(i % random == 0) { - ts2.add(null); - }else{ - ts2.add(System.currentTimeMillis() + i); - } - } - s.setTimestamp(4, ts2); - - random = 10 + r.nextInt(5); - ArrayList vals = new ArrayList<>(); - for(int i = 0; i < numOfRows; i++) { - if(i % random == 0) { - vals.add(null); - }else{ - vals.add(r.nextInt()); - } - } - s.setInt(5, vals); - - random = 10 + r.nextInt(5); - ArrayList sb = new ArrayList<>(); - for(int i = 0; i < numOfRows; i++) { - if(i % random == 0) { - sb.add(null); - }else{ - sb.add(i % 2 == 0 ? true : false); - } - } - s.setBoolean(6, sb); - - random = 10 + r.nextInt(5); - ArrayList s5 = new ArrayList(); - for(int i = 0; i < numOfRows; i++) { - if(i % random == 0) { - s5.add(null); - }else{ - s5.add("test" + i % 10); - } - } - s.setString(7, s5, 10); - - s.columnDataAddBatch(); - s.columnDataExecuteBatch(); - s.columnDataCloseBatch(); - - String sql = "select * from weather_test"; - PreparedStatement statement = conn.prepareStatement(sql); - ResultSet rs = statement.executeQuery(); - int rows = 0; - while(rs.next()) { - rows++; - } - Assert.assertEquals(numOfRows, rows); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + assertAllNullExceptTimestamp(rs, ts); } } - @Test - public void bindDataSelectColumnTest() throws SQLException { - Statement stmt = conn.createStatement(); - - int numOfRows = 1000; - - for (int loop = 0; loop < 10; loop++){ - stmt.execute("drop table if exists weather_test"); - stmt.execute("create table weather_test(ts timestamp, f1 nchar(4), f2 float, f3 double, f4 timestamp, f5 int, f6 bool, f7 binary(10))"); - - TSDBPreparedStatement s = (TSDBPreparedStatement) conn.prepareStatement("insert into ? (ts, f1, f7) values(?, ?, ?)"); - Random r = new Random(); - s.setTableName("weather_test"); - - ArrayList ts = new ArrayList(); - for(int i = 0; i < numOfRows; i++) { - ts.add(System.currentTimeMillis() + i); - } - s.setTimestamp(0, ts); - - int random = 10 + r.nextInt(5); - ArrayList s2 = new ArrayList(); - for(int i = 0; i < numOfRows; i++) { - if(i % random == 0) { - s2.add(null); - }else{ - s2.add("分支" + i % 4); - } - } - s.setNString(1, s2, 4); - - random = 10 + r.nextInt(5); - ArrayList s3 = new ArrayList(); - for(int i = 0; i < numOfRows; i++) { - if(i % random == 0) { - s3.add(null); - }else{ - s3.add("test" + i % 10); - } - } - s.setString(2, s3, 10); - - s.columnDataAddBatch(); - s.columnDataExecuteBatch(); - s.columnDataCloseBatch(); - - String sql = "select * from weather_test"; - PreparedStatement statement = conn.prepareStatement(sql); - ResultSet rs = statement.executeQuery(); - int rows = 0; - while(rs.next()) { - rows++; - } - Assert.assertEquals(numOfRows, rows); - } - } - - @Test - public void bindDataWithSingleTagTest() throws SQLException { - Statement stmt = conn.createStatement(); - - String types[] = new String[] {"tinyint", "smallint", "int", "bigint", "bool", "float", "double", "binary(10)", "nchar(10)"}; - - for (String type : types) { - stmt.execute("drop table if exists weather_test"); - stmt.execute("create table weather_test(ts timestamp, f1 nchar(10), f2 binary(10)) tags (t " + type + ")"); - - int numOfRows = 1; - - TSDBPreparedStatement s = (TSDBPreparedStatement) conn.prepareStatement("insert into ? using weather_test tags(?) values(?, ?, ?)"); - Random r = new Random(); - s.setTableName("w1"); - - switch(type) { - case "tinyint": - case "smallint": - case "int": - case "bigint": - s.setTagInt(0, 1); - break; - case "float": - s.setTagFloat(0, 1.23f); - break; - case "double": - s.setTagDouble(0, 3.14159265); - break; - case "bool": - s.setTagBoolean(0, true); - break; - case "binary(10)": - s.setTagString(0, "test"); - break; - case "nchar(10)": - s.setTagNString(0, "test"); - break; - default: - break; - } - - - ArrayList ts = new ArrayList(); - for(int i = 0; i < numOfRows; i++) { - ts.add(System.currentTimeMillis() + i); - } - s.setTimestamp(0, ts); - - int random = 10 + r.nextInt(5); - ArrayList s2 = new ArrayList(); - for(int i = 0; i < numOfRows; i++) { - s2.add("分支" + i % 4); - } - s.setNString(1, s2, 10); - - random = 10 + r.nextInt(5); - ArrayList s3 = new ArrayList(); - for(int i = 0; i < numOfRows; i++) { - s3.add("test" + i % 4); - } - s.setString(2, s3, 10); - - s.columnDataAddBatch(); - s.columnDataExecuteBatch(); - s.columnDataCloseBatch(); - - String sql = "select * from weather_test"; - PreparedStatement statement = conn.prepareStatement(sql); - ResultSet rs = statement.executeQuery(); - int rows = 0; - while(rs.next()) { - rows++; - } - Assert.assertEquals(numOfRows, rows); - } - } - - - @Test - public void bindDataWithMultipleTagsTest() throws SQLException { - Statement stmt = conn.createStatement(); - - stmt.execute("drop table if exists weather_test"); - stmt.execute("create table weather_test(ts timestamp, f1 nchar(10), f2 binary(10)) tags (t1 int, t2 binary(10))"); - - int numOfRows = 1; - - TSDBPreparedStatement s = (TSDBPreparedStatement) conn.prepareStatement("insert into ? using weather_test tags(?,?) (ts, f2) values(?, ?)"); - s.setTableName("w2"); - s.setTagInt(0, 1); - s.setTagString(1, "test"); - - - ArrayList ts = new ArrayList(); - for(int i = 0; i < numOfRows; i++) { - ts.add(System.currentTimeMillis() + i); - } - s.setTimestamp(0, ts); - - ArrayList s2 = new ArrayList(); - for(int i = 0; i < numOfRows; i++) { - s2.add("test" + i % 4); - } - s.setString(1, s2, 10); - - s.columnDataAddBatch(); - s.columnDataExecuteBatch(); - s.columnDataCloseBatch(); - - String sql = "select * from weather_test"; - PreparedStatement statement = conn.prepareStatement(sql); - ResultSet rs = statement.executeQuery(); - int rows = 0; - while(rs.next()) { - rows++; - } - Assert.assertEquals(numOfRows, rows); - - } - @Test public void setBoolean() throws SQLException { - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setBoolean(8, true); - int ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); + int result = pstmt_insert.executeUpdate(); + + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + { + Assert.assertNotNull(rs); + Assert.assertEquals(ts, rs.getTimestamp(1).getTime()); + Assert.assertEquals(ts, rs.getTimestamp("ts").getTime()); + Assert.assertTrue(rs.getBoolean(8)); + Assert.assertTrue(rs.getBoolean("f7")); + } + } } @Test public void setByte() throws SQLException { - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setByte(7, (byte) 0x001); - int ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); + int result = pstmt_insert.executeUpdate(); + + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + { + Assert.assertNotNull(rs); + Assert.assertEquals(ts, rs.getTimestamp(1).getTime()); + Assert.assertEquals(ts, rs.getTimestamp("ts").getTime()); + Assert.assertEquals((byte) 0x001, rs.getByte(7)); + Assert.assertEquals((byte) 0x001, rs.getByte("f6")); + } + } } @Test public void setShort() throws SQLException { - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setShort(6, (short) 2); - int ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); + int result = pstmt_insert.executeUpdate(); + + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + { + Assert.assertNotNull(rs); + Assert.assertEquals(ts, rs.getTimestamp(1).getTime()); + Assert.assertEquals(ts, rs.getTimestamp("ts").getTime()); + Assert.assertEquals((short) 2, rs.getByte(6)); + Assert.assertEquals((short) 2, rs.getByte("f5")); + } + } } @Test public void setInt() throws SQLException { - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setInt(2, 10086); - int ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); + int result = pstmt_insert.executeUpdate(); + + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + { + Assert.assertNotNull(rs); + Assert.assertEquals(ts, rs.getTimestamp(1).getTime()); + Assert.assertEquals(ts, rs.getTimestamp("ts").getTime()); + Assert.assertEquals(10086, rs.getInt(2)); + Assert.assertEquals(10086, rs.getInt("f1")); + } + } } @Test public void setLong() throws SQLException { - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setLong(3, Long.MAX_VALUE); - int ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); + int result = pstmt_insert.executeUpdate(); + + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + { + Assert.assertNotNull(rs); + Assert.assertEquals(ts, rs.getTimestamp(1).getTime()); + Assert.assertEquals(ts, rs.getTimestamp("ts").getTime()); + Assert.assertEquals(Long.MAX_VALUE, rs.getLong(3)); + Assert.assertEquals(Long.MAX_VALUE, rs.getLong("f2")); + } + } } @Test public void setFloat() throws SQLException { - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setFloat(4, 3.14f); - int ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); + int result = pstmt_insert.executeUpdate(); + + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + { + Assert.assertNotNull(rs); + Assert.assertEquals(ts, rs.getTimestamp(1).getTime()); + Assert.assertEquals(ts, rs.getTimestamp("ts").getTime()); + Assert.assertEquals(3.14f, rs.getFloat(4), 0.0f); + Assert.assertEquals(3.14f, rs.getFloat("f3"), 0.0f); + } + } } @Test public void setDouble() throws SQLException { - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); pstmt_insert.setDouble(5, 3.14444); - int ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); + int result = pstmt_insert.executeUpdate(); + + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + { + Assert.assertNotNull(rs); + Assert.assertEquals(ts, rs.getTimestamp(1).getTime()); + Assert.assertEquals(ts, rs.getTimestamp("ts").getTime()); + Assert.assertEquals(3.14444, rs.getDouble(5), 0.0); + Assert.assertEquals(3.14444, rs.getDouble("f4"), 0.0); + } + } } - @Test(expected = SQLFeatureNotSupportedException.class) + @Test public void setBigDecimal() throws SQLException { - pstmt_insert.setBigDecimal(1, null); + // given + long ts = System.currentTimeMillis(); + BigDecimal bigDecimal = new BigDecimal(3.14444); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); + pstmt_insert.setBigDecimal(5, bigDecimal); + int result = pstmt_insert.executeUpdate(); + + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + { + Assert.assertNotNull(rs); + Assert.assertEquals(ts, rs.getTimestamp(1).getTime()); + Assert.assertEquals(ts, rs.getTimestamp("ts").getTime()); + Assert.assertEquals(3.14444, rs.getDouble(5), 0.0); + Assert.assertEquals(3.14444, rs.getDouble("f4"), 0.0); + } + } } @Test public void setString() throws SQLException { - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - pstmt_insert.setString(10, "aaaa"); - boolean execute = pstmt_insert.execute(); - Assert.assertFalse(execute); + // given + long ts = System.currentTimeMillis(); + String f9 = "{\"name\": \"john\", \"age\": 10, \"address\": \"192.168.1.100\"}"; - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - pstmt_insert.setString(10, new Person("john", 33, true).toString()); - Assert.assertFalse(pstmt_insert.execute()); + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); + pstmt_insert.setString(10, f9); + int result = pstmt_insert.executeUpdate(); - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - pstmt_insert.setString(10, new Person("john", 33, true).toString().replaceAll("'", "\"")); - Assert.assertFalse(pstmt_insert.execute()); - } - - class Person { - String name; - int age; - boolean sex; - - public Person(String name, int age, boolean sex) { - this.name = name; - this.age = age; - this.sex = sex; - } - - @Override - public String toString() { - return "Person{" + - "name='" + name + '\'' + - ", age=" + age + - ", sex=" + sex + - '}'; + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + { + Assert.assertNotNull(rs); + Assert.assertEquals(ts, rs.getTimestamp(1).getTime()); + Assert.assertEquals(ts, rs.getTimestamp("ts").getTime()); + Assert.assertEquals(f9, rs.getString(10)); + Assert.assertEquals(f9, rs.getString("f9")); + } } } @Test public void setBytes() throws SQLException, IOException { + // given + long ts = System.currentTimeMillis(); + byte[] f8 = "{\"name\": \"john\", \"age\": 10, \"address\": \"192.168.1.100\"}".getBytes(); + + // when pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); + pstmt_insert.setBytes(9, f8); + int result = pstmt_insert.executeUpdate(); -// ByteArrayOutputStream baos = new ByteArrayOutputStream(); -// ObjectOutputStream oos = new ObjectOutputStream(baos); -// oos.writeObject(new Person("john", 33, true)); -// oos.flush(); -// byte[] bytes = baos.toByteArray(); -// pstmt_insert.setBytes(9, bytes); - - pstmt_insert.setBytes(9, new Person("john", 33, true).toString().getBytes()); - int ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + { + Assert.assertNotNull(rs); + Assert.assertEquals(ts, rs.getTimestamp(1).getTime()); + Assert.assertEquals(ts, rs.getTimestamp("ts").getTime()); + Assert.assertArrayEquals(f8, rs.getBytes(9)); + Assert.assertArrayEquals(f8, rs.getBytes("f8")); + } + } } - @Test(expected = SQLFeatureNotSupportedException.class) + @Test public void setDate() throws SQLException { - pstmt_insert.setDate(1, new Date(System.currentTimeMillis())); + // given + long ts = new java.util.Date().getTime(); + + // when + pstmt_insert.setDate(1, new Date(ts)); + int result = pstmt_insert.executeUpdate(); + + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + { + Assert.assertNotNull(rs); + Assert.assertEquals(ts, rs.getTimestamp(1).getTime()); + Assert.assertEquals(ts, rs.getTimestamp("ts").getTime()); + } + } } - @Test(expected = SQLFeatureNotSupportedException.class) + @Test public void setTime() throws SQLException { - pstmt_insert.setTime(1, new Time(System.currentTimeMillis())); + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTime(1, new Time(ts)); + int result = pstmt_insert.executeUpdate(); + + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + { + Assert.assertNotNull(rs); + Assert.assertEquals(ts, rs.getTimestamp(1).getTime()); + Assert.assertEquals(ts, rs.getTimestamp("ts").getTime()); + } + } } @Test public void setTimestamp() throws SQLException { - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - int ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); + int result = pstmt_insert.executeUpdate(); + + // then + Assert.assertEquals(1, result); + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("select * from t1"); + ResultSetMetaData meta = rs.getMetaData(); + assertMetaData(meta); + rs.next(); + { + Assert.assertNotNull(rs); + Assert.assertEquals(ts, rs.getTimestamp(1).getTime()); + Assert.assertEquals(ts, rs.getTimestamp("ts").getTime()); + } + } } @Test(expected = SQLFeatureNotSupportedException.class) @@ -530,72 +651,6 @@ public class TSDBPreparedStatementTest { pstmt_insert.setBinaryStream(1, null); } - @Test - public void clearParameters() throws SQLException { - pstmt_insert.clearParameters(); - } - - @Test - public void setObject() throws SQLException { - pstmt_insert.setObject(1, new Timestamp(System.currentTimeMillis())); - int ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); - - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - pstmt_insert.setObject(2, 111); - ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); - - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - pstmt_insert.setObject(3, Long.MAX_VALUE); - ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); - - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - pstmt_insert.setObject(4, 3.14159265354f); - ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); - - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - pstmt_insert.setObject(5, Double.MAX_VALUE); - ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); - - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - pstmt_insert.setObject(6, Short.MAX_VALUE); - ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); - - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - pstmt_insert.setObject(7, Byte.MAX_VALUE); - ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); - - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - pstmt_insert.setObject(8, true); - ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); - - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - pstmt_insert.setObject(9, "hello".getBytes()); - ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); - - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - pstmt_insert.setObject(10, "Hello"); - ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); - } - - @Test - public void execute() throws SQLException { - pstmt_insert.setTimestamp(1, new Timestamp(System.currentTimeMillis())); - int ret = pstmt_insert.executeUpdate(); - Assert.assertEquals(1, ret); - - executeQuery(); - } - @Test(expected = SQLFeatureNotSupportedException.class) public void setCharacterStream() throws SQLException { pstmt_insert.setCharacterStream(1, null); @@ -621,9 +676,17 @@ public class TSDBPreparedStatementTest { pstmt_insert.setArray(1, null); } - @Test(expected = SQLFeatureNotSupportedException.class) + @Test public void getMetaData() throws SQLException { - pstmt_insert.getMetaData(); + // given + long ts = System.currentTimeMillis(); + + // when + pstmt_insert.setTimestamp(1, new Timestamp(ts)); + ResultSetMetaData metaData = pstmt_insert.getMetaData(); + + // then + Assert.assertNull(metaData); } @Test(expected = SQLFeatureNotSupportedException.class) @@ -633,9 +696,46 @@ public class TSDBPreparedStatementTest { @Test public void getParameterMetaData() throws SQLException { + // given + long ts = System.currentTimeMillis(); + pstmt_insert.setTimestamp(1, new Timestamp(ts)); + pstmt_insert.setInt(2, 2); + pstmt_insert.setLong(3, 3l); + pstmt_insert.setFloat(4, 3.14f); + pstmt_insert.setDouble(5, 3.1415); + pstmt_insert.setShort(6, (short) 6); + pstmt_insert.setByte(7, (byte) 7); + pstmt_insert.setBoolean(8, true); + pstmt_insert.setBytes(9, "abc".getBytes()); + pstmt_insert.setString(10, "涛思数据"); + + // when ParameterMetaData parameterMetaData = pstmt_insert.getParameterMetaData(); + + // then Assert.assertNotNull(parameterMetaData); - //TODO: modify the test case + Assert.assertEquals(10, parameterMetaData.getParameterCount()); + Assert.assertEquals(Types.TIMESTAMP, parameterMetaData.getParameterType(1)); + Assert.assertEquals(Types.INTEGER, parameterMetaData.getParameterType(2)); + Assert.assertEquals(Types.BIGINT, parameterMetaData.getParameterType(3)); + Assert.assertEquals(Types.FLOAT, parameterMetaData.getParameterType(4)); + Assert.assertEquals(Types.DOUBLE, parameterMetaData.getParameterType(5)); + Assert.assertEquals(Types.SMALLINT, parameterMetaData.getParameterType(6)); + Assert.assertEquals(Types.TINYINT, parameterMetaData.getParameterType(7)); + Assert.assertEquals(Types.BOOLEAN, parameterMetaData.getParameterType(8)); + Assert.assertEquals(Types.BINARY, parameterMetaData.getParameterType(9)); + Assert.assertEquals(Types.NCHAR, parameterMetaData.getParameterType(10)); + + Assert.assertEquals("TIMESTAMP", parameterMetaData.getParameterTypeName(1)); + Assert.assertEquals("INT", parameterMetaData.getParameterTypeName(2)); + Assert.assertEquals("BIGINT", parameterMetaData.getParameterTypeName(3)); + Assert.assertEquals("FLOAT", parameterMetaData.getParameterTypeName(4)); + Assert.assertEquals("DOUBLE", parameterMetaData.getParameterTypeName(5)); + Assert.assertEquals("SMALLINT", parameterMetaData.getParameterTypeName(6)); + Assert.assertEquals("TINYINT", parameterMetaData.getParameterTypeName(7)); + Assert.assertEquals("BOOL", parameterMetaData.getParameterTypeName(8)); + Assert.assertEquals("BINARY", parameterMetaData.getParameterTypeName(9)); + Assert.assertEquals("NCHAR", parameterMetaData.getParameterTypeName(10)); } @Test(expected = SQLFeatureNotSupportedException.class) @@ -643,9 +743,9 @@ public class TSDBPreparedStatementTest { pstmt_insert.setRowId(1, null); } - @Test(expected = SQLFeatureNotSupportedException.class) + @Test public void setNString() throws SQLException { - pstmt_insert.setNString(1, null); + setString(); } @Test(expected = SQLFeatureNotSupportedException.class) @@ -663,22 +763,45 @@ public class TSDBPreparedStatementTest { pstmt_insert.setSQLXML(1, null); } + @Before + public void before() { + try { + Statement stmt = conn.createStatement(); + stmt.execute("drop table if exists weather"); + stmt.execute("create table if not exists weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(64), f9 nchar(64)) tags(loc nchar(64))"); + stmt.execute("create table if not exists t1 using weather tags('beijing')"); + stmt.close(); + + pstmt_insert = conn.prepareStatement(sql_insert); + pstmt_select = conn.prepareStatement(sql_select); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @After + public void after() { + try { + if (pstmt_insert != null) + pstmt_insert.close(); + if (pstmt_select != null) + pstmt_select.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + } @BeforeClass public static void beforeClass() { try { - Class.forName("com.taosdata.jdbc.TSDBDriver"); conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata"); try (Statement stmt = conn.createStatement()) { stmt.execute("drop database if exists test_pstmt_jni"); stmt.execute("create database if not exists test_pstmt_jni"); stmt.execute("use test_pstmt_jni"); - stmt.execute("create table weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(64), f9 nchar(64)) tags(loc nchar(64))"); - stmt.execute("create table t1 using weather tags('beijing')"); } - pstmt_insert = conn.prepareStatement(sql_insert); - pstmt_select = conn.prepareStatement(sql_select); - } catch (ClassNotFoundException | SQLException e) { + } catch (SQLException e) { e.printStackTrace(); } } @@ -686,10 +809,6 @@ public class TSDBPreparedStatementTest { @AfterClass public static void afterClass() { try { - if (pstmt_insert != null) - pstmt_insert.close(); - if (pstmt_select != null) - pstmt_select.close(); if (conn != null) conn.close(); } catch (SQLException e) { diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBResultSetTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBResultSetTest.java index f304fd6874..31ee35899c 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBResultSetTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBResultSetTest.java @@ -14,6 +14,7 @@ import java.math.BigDecimal; import java.sql.*; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Arrays; public class TSDBResultSetTest { @@ -133,7 +134,7 @@ public class TSDBResultSetTest { Assert.assertEquals(3.1415926, Double.valueOf(new String(f5)), 0.000000f); byte[] f6 = rs.getBytes("f6"); - Assert.assertEquals("abc", new String(f6)); + Assert.assertTrue(Arrays.equals("abc".getBytes(), f6)); byte[] f7 = rs.getBytes("f7"); Assert.assertEquals((short) 10, Shorts.fromByteArray(f7)); @@ -176,8 +177,7 @@ public class TSDBResultSetTest { rs.getAsciiStream("f1"); } - @SuppressWarnings("deprecation") - @Test(expected = SQLFeatureNotSupportedException.class) + @Test(expected = SQLFeatureNotSupportedException.class) public void getUnicodeStream() throws SQLException { rs.getUnicodeStream("f1"); } @@ -646,7 +646,6 @@ public class TSDBResultSetTest { @BeforeClass public static void beforeClass() { try { - Class.forName("com.taosdata.jdbc.TSDBDriver"); conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata"); stmt = conn.createStatement(); stmt.execute("create database if not exists restful_test"); @@ -656,10 +655,9 @@ public class TSDBResultSetTest { stmt.execute("insert into restful_test.weather values('2021-01-01 00:00:00.000', 1, 100, 3.1415, 3.1415926, 'abc', 10, 10, true, '涛思数据')"); rs = stmt.executeQuery("select * from restful_test.weather"); rs.next(); - } catch (ClassNotFoundException | SQLException e) { + } catch (SQLException e) { e.printStackTrace(); } - } @AfterClass diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBStatementTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBStatementTest.java index 671551c426..51535bc886 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBStatementTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/TSDBStatementTest.java @@ -387,15 +387,12 @@ public class TSDBStatementTest { @BeforeClass public static void beforeClass() { try { - Class.forName("com.taosdata.jdbc.TSDBDriver"); Properties properties = new Properties(); properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); conn = DriverManager.getConnection("jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata", properties); stmt = conn.createStatement(); - } catch (ClassNotFoundException e) { - e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulDatabaseMetaDataTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulDatabaseMetaDataTest.java index a052fbbdcb..85007db0e5 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulDatabaseMetaDataTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulDatabaseMetaDataTest.java @@ -10,6 +10,7 @@ import java.sql.*; import java.util.Properties; public class RestfulDatabaseMetaDataTest { + private static final String host = "127.0.0.1"; private static final String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata"; private static Connection connection; @@ -632,17 +633,32 @@ public class RestfulDatabaseMetaDataTest { @Test public void getTables() throws SQLException { - System.out.println("****************************************************"); - ResultSet tables = metaData.getTables("log", "", null, null); - ResultSetMetaData metaData = tables.getMetaData(); - while (tables.next()) { - System.out.print(metaData.getColumnLabel(1) + ":" + tables.getString(1) + "\t"); - System.out.print(metaData.getColumnLabel(3) + ":" + tables.getString(3) + "\t"); - System.out.print(metaData.getColumnLabel(4) + ":" + tables.getString(4) + "\t"); - System.out.print(metaData.getColumnLabel(5) + ":" + tables.getString(5) + "\n"); + ResultSet rs = metaData.getTables("log", "", null, null); + ResultSetMetaData meta = rs.getMetaData(); + Assert.assertNotNull(rs); + rs.next(); + { + // TABLE_CAT + Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1)); + Assert.assertEquals("log", rs.getString(1)); + Assert.assertEquals("log", rs.getString("TABLE_CAT")); + // TABLE_SCHEM + Assert.assertEquals("TABLE_SCHEM", meta.getColumnLabel(2)); + Assert.assertEquals(null, rs.getString(2)); + Assert.assertEquals(null, rs.getString("TABLE_SCHEM")); + // TABLE_NAME + Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3)); + Assert.assertNotNull(rs.getString(3)); + Assert.assertNotNull(rs.getString("TABLE_NAME")); + // TABLE_TYPE + Assert.assertEquals("TABLE_TYPE", meta.getColumnLabel(4)); + Assert.assertEquals("TABLE", rs.getString(4)); + Assert.assertEquals("TABLE", rs.getString("TABLE_TYPE")); + // REMARKS + Assert.assertEquals("REMARKS", meta.getColumnLabel(5)); + Assert.assertEquals("", rs.getString(5)); + Assert.assertEquals("", rs.getString("REMARKS")); } - System.out.println(); - Assert.assertNotNull(tables); } @Test @@ -652,46 +668,130 @@ public class RestfulDatabaseMetaDataTest { @Test public void getCatalogs() throws SQLException { - System.out.println("****************************************************"); - - ResultSet catalogs = metaData.getCatalogs(); - ResultSetMetaData meta = catalogs.getMetaData(); - while (catalogs.next()) { - for (int i = 1; i <= meta.getColumnCount(); i++) { - System.out.print(meta.getColumnLabel(i) + ": " + catalogs.getString(i)); - } - System.out.println(); + ResultSet rs = metaData.getCatalogs(); + ResultSetMetaData meta = rs.getMetaData(); + rs.next(); + { + // TABLE_CAT + Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1)); + Assert.assertNotNull(rs.getString(1)); + Assert.assertNotNull(rs.getString("TABLE_CAT")); } } @Test public void getTableTypes() throws SQLException { - System.out.println("****************************************************"); - ResultSet tableTypes = metaData.getTableTypes(); - while (tableTypes.next()) { - System.out.println(tableTypes.getString("TABLE_TYPE")); + tableTypes.next(); + // tableTypes: table + { + Assert.assertEquals("TABLE", tableTypes.getString(1)); + Assert.assertEquals("TABLE", tableTypes.getString("TABLE_TYPE")); + } + tableTypes.next(); + // tableTypes: stable + { + Assert.assertEquals("STABLE", tableTypes.getString(1)); + Assert.assertEquals("STABLE", tableTypes.getString("TABLE_TYPE")); } - Assert.assertNotNull(metaData.getTableTypes()); } @Test public void getColumns() throws SQLException { - System.out.println("****************************************************"); - + // when ResultSet columns = metaData.getColumns("log", "", "dn", ""); + // then ResultSetMetaData meta = columns.getMetaData(); - while (columns.next()) { - System.out.print(meta.getColumnLabel(1) + ": " + columns.getString(1) + "\t"); - System.out.print(meta.getColumnLabel(3) + ": " + columns.getString(3) + "\t"); - System.out.print(meta.getColumnLabel(4) + ": " + columns.getString(4) + "\t"); - System.out.print(meta.getColumnLabel(5) + ": " + columns.getString(5) + "\t"); - System.out.print(meta.getColumnLabel(6) + ": " + columns.getString(6) + "\t"); - System.out.print(meta.getColumnLabel(7) + ": " + columns.getString(7) + "\t"); - System.out.print(meta.getColumnLabel(9) + ": " + columns.getString(9) + "\t"); - System.out.print(meta.getColumnLabel(10) + ": " + columns.getString(10) + "\t"); - System.out.print(meta.getColumnLabel(11) + ": " + columns.getString(11) + "\n"); - System.out.print(meta.getColumnLabel(12) + ": " + columns.getString(12) + "\n"); + columns.next(); + // column: 1 + { + // TABLE_CAT + Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1)); + Assert.assertEquals("log", columns.getString(1)); + Assert.assertEquals("log", columns.getString("TABLE_CAT")); + // TABLE_NAME + Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3)); + Assert.assertEquals("dn", columns.getString(3)); + Assert.assertEquals("dn", columns.getString("TABLE_NAME")); + // COLUMN_NAME + Assert.assertEquals("COLUMN_NAME", meta.getColumnLabel(4)); + Assert.assertEquals("ts", columns.getString(4)); + Assert.assertEquals("ts", columns.getString("COLUMN_NAME")); + // DATA_TYPE + Assert.assertEquals("DATA_TYPE", meta.getColumnLabel(5)); + Assert.assertEquals(Types.TIMESTAMP, columns.getInt(5)); + Assert.assertEquals(Types.TIMESTAMP, columns.getInt("DATA_TYPE")); + // TYPE_NAME + Assert.assertEquals("TYPE_NAME", meta.getColumnLabel(6)); + Assert.assertEquals("TIMESTAMP", columns.getString(6)); + Assert.assertEquals("TIMESTAMP", columns.getString("TYPE_NAME")); + // COLUMN_SIZE + Assert.assertEquals("COLUMN_SIZE", meta.getColumnLabel(7)); + Assert.assertEquals(26, columns.getInt(7)); + Assert.assertEquals(26, columns.getInt("COLUMN_SIZE")); + // DECIMAL_DIGITS + Assert.assertEquals("DECIMAL_DIGITS", meta.getColumnLabel(9)); + Assert.assertEquals(Integer.MIN_VALUE, columns.getInt(9)); + Assert.assertEquals(Integer.MIN_VALUE, columns.getInt("DECIMAL_DIGITS")); + Assert.assertEquals(null, columns.getString(9)); + Assert.assertEquals(null, columns.getString("DECIMAL_DIGITS")); + // NUM_PREC_RADIX + Assert.assertEquals("NUM_PREC_RADIX", meta.getColumnLabel(10)); + Assert.assertEquals(10, columns.getInt(10)); + Assert.assertEquals(10, columns.getInt("NUM_PREC_RADIX")); + // NULLABLE + Assert.assertEquals("NULLABLE", meta.getColumnLabel(11)); + Assert.assertEquals(DatabaseMetaData.columnNoNulls, columns.getInt(11)); + Assert.assertEquals(DatabaseMetaData.columnNoNulls, columns.getInt("NULLABLE")); + // REMARKS + Assert.assertEquals("REMARKS", meta.getColumnLabel(12)); + Assert.assertEquals(null, columns.getString(12)); + Assert.assertEquals(null, columns.getString("REMARKS")); + } + columns.next(); + // column: 2 + { + // TABLE_CAT + Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1)); + Assert.assertEquals("log", columns.getString(1)); + Assert.assertEquals("log", columns.getString("TABLE_CAT")); + // TABLE_NAME + Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3)); + Assert.assertEquals("dn", columns.getString(3)); + Assert.assertEquals("dn", columns.getString("TABLE_NAME")); + // COLUMN_NAME + Assert.assertEquals("COLUMN_NAME", meta.getColumnLabel(4)); + Assert.assertEquals("cpu_taosd", columns.getString(4)); + Assert.assertEquals("cpu_taosd", columns.getString("COLUMN_NAME")); + // DATA_TYPE + Assert.assertEquals("DATA_TYPE", meta.getColumnLabel(5)); + Assert.assertEquals(Types.FLOAT, columns.getInt(5)); + Assert.assertEquals(Types.FLOAT, columns.getInt("DATA_TYPE")); + // TYPE_NAME + Assert.assertEquals("TYPE_NAME", meta.getColumnLabel(6)); + Assert.assertEquals("FLOAT", columns.getString(6)); + Assert.assertEquals("FLOAT", columns.getString("TYPE_NAME")); + // COLUMN_SIZE + Assert.assertEquals("COLUMN_SIZE", meta.getColumnLabel(7)); + Assert.assertEquals(12, columns.getInt(7)); + Assert.assertEquals(12, columns.getInt("COLUMN_SIZE")); + // DECIMAL_DIGITS + Assert.assertEquals("DECIMAL_DIGITS", meta.getColumnLabel(9)); + Assert.assertEquals(Integer.MIN_VALUE, columns.getInt(9)); + Assert.assertEquals(Integer.MIN_VALUE, columns.getInt("DECIMAL_DIGITS")); + Assert.assertEquals(null, columns.getString(9)); + Assert.assertEquals(null, columns.getString("DECIMAL_DIGITS")); + // NUM_PREC_RADIX + Assert.assertEquals("NUM_PREC_RADIX", meta.getColumnLabel(10)); + Assert.assertEquals(10, columns.getInt(10)); + Assert.assertEquals(10, columns.getInt("NUM_PREC_RADIX")); + // NULLABLE + Assert.assertEquals("NULLABLE", meta.getColumnLabel(11)); + Assert.assertEquals(DatabaseMetaData.columnNullable, columns.getInt(11)); + Assert.assertEquals(DatabaseMetaData.columnNullable, columns.getInt("NULLABLE")); + // REMARKS + Assert.assertEquals("REMARKS", meta.getColumnLabel(12)); + Assert.assertEquals(null, columns.getString(12)); } } @@ -717,17 +817,35 @@ public class RestfulDatabaseMetaDataTest { @Test public void getPrimaryKeys() throws SQLException { - System.out.println("****************************************************"); - ResultSet rs = metaData.getPrimaryKeys("log", "", "dn1"); - while (rs.next()) { - System.out.println("TABLE_NAME: " + rs.getString("TABLE_NAME")); - System.out.println("COLUMN_NAME: " + rs.getString("COLUMN_NAME")); - System.out.println("KEY_SEQ: " + rs.getString("KEY_SEQ")); - System.out.println("PK_NAME: " + rs.getString("PK_NAME")); + ResultSetMetaData meta = rs.getMetaData(); + rs.next(); + { + // TABLE_CAT + Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1)); + Assert.assertEquals("log", rs.getString(1)); + Assert.assertEquals("log", rs.getString("TABLE_CAT")); + // TABLE_SCHEM + Assert.assertEquals("TABLE_SCHEM", meta.getColumnLabel(2)); + Assert.assertEquals(null, rs.getString(2)); + Assert.assertEquals(null, rs.getString("TABLE_SCHEM")); + // TABLE_NAME + Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3)); + Assert.assertEquals("dn1", rs.getString(3)); + Assert.assertEquals("dn1", rs.getString("TABLE_NAME")); + // COLUMN_NAME + Assert.assertEquals("COLUMN_NAME", meta.getColumnLabel(4)); + Assert.assertEquals("ts", rs.getString(4)); + Assert.assertEquals("ts", rs.getString("COLUMN_NAME")); + // KEY_SEQ + Assert.assertEquals("KEY_SEQ", meta.getColumnLabel(5)); + Assert.assertEquals(1, rs.getShort(5)); + Assert.assertEquals(1, rs.getShort("KEY_SEQ")); + // DATA_TYPE + Assert.assertEquals("PK_NAME", meta.getColumnLabel(6)); + Assert.assertEquals("ts", rs.getString(6)); + Assert.assertEquals("ts", rs.getString("PK_NAME")); } - - Assert.assertNotNull(rs); } @Test @@ -852,14 +970,27 @@ public class RestfulDatabaseMetaDataTest { @Test public void getSuperTables() throws SQLException { - System.out.println("****************************************************"); - ResultSet rs = metaData.getSuperTables("log", "", "dn1"); - while (rs.next()) { - System.out.println("TABLE_NAME: " + rs.getString("TABLE_NAME")); - System.out.println("SUPERTABLE_NAME: " + rs.getString("SUPERTABLE_NAME")); + ResultSetMetaData meta = rs.getMetaData(); + rs.next(); + { + // TABLE_CAT + Assert.assertEquals("TABLE_CAT", meta.getColumnLabel(1)); + Assert.assertEquals("log", rs.getString(1)); + Assert.assertEquals("log", rs.getString("TABLE_CAT")); + // TABLE_CAT + Assert.assertEquals("TABLE_SCHEM", meta.getColumnLabel(2)); + Assert.assertEquals(null, rs.getString(2)); + Assert.assertEquals(null, rs.getString("TABLE_SCHEM")); + // TABLE_CAT + Assert.assertEquals("TABLE_NAME", meta.getColumnLabel(3)); + Assert.assertEquals("dn1", rs.getString(3)); + Assert.assertEquals("dn1", rs.getString("TABLE_NAME")); + // TABLE_CAT + Assert.assertEquals("SUPERTABLE_NAME", meta.getColumnLabel(4)); + Assert.assertEquals("dn", rs.getString(4)); + Assert.assertEquals("dn", rs.getString("SUPERTABLE_NAME")); } - Assert.assertNotNull(rs); } @Test diff --git a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulParameterMetaDataTest.java b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulParameterMetaDataTest.java index 8bb2532ce8..81d7f5b56c 100644 --- a/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulParameterMetaDataTest.java +++ b/src/connector/jdbc/src/test/java/com/taosdata/jdbc/rs/RestfulParameterMetaDataTest.java @@ -54,16 +54,17 @@ public class RestfulParameterMetaDataTest { @Test public void getPrecision() throws SQLException { - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(1)); - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(2)); - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(3)); - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(4)); - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(5)); - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(6)); - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(7)); - Assert.assertEquals(0, parameterMetaData_insert.getPrecision(8)); - Assert.assertEquals(5, parameterMetaData_insert.getPrecision(9)); - Assert.assertEquals(5, parameterMetaData_insert.getPrecision(10)); + //create table weather(ts timestamp, f1 int, f2 bigint, f3 float, f4 double, f5 smallint, f6 tinyint, f7 bool, f8 binary(64), f9 nchar(64)) + Assert.assertEquals(TSDBConstants.TIMESTAMP_MS_PRECISION, parameterMetaData_insert.getPrecision(1)); + Assert.assertEquals(TSDBConstants.INT_PRECISION, parameterMetaData_insert.getPrecision(2)); + Assert.assertEquals(TSDBConstants.BIGINT_PRECISION, parameterMetaData_insert.getPrecision(3)); + Assert.assertEquals(TSDBConstants.FLOAT_PRECISION, parameterMetaData_insert.getPrecision(4)); + Assert.assertEquals(TSDBConstants.DOUBLE_PRECISION, parameterMetaData_insert.getPrecision(5)); + Assert.assertEquals(TSDBConstants.SMALLINT_PRECISION, parameterMetaData_insert.getPrecision(6)); + Assert.assertEquals(TSDBConstants.TINYINT_PRECISION, parameterMetaData_insert.getPrecision(7)); + Assert.assertEquals(TSDBConstants.BOOLEAN_PRECISION, parameterMetaData_insert.getPrecision(8)); + Assert.assertEquals("hello".getBytes().length, parameterMetaData_insert.getPrecision(9)); + Assert.assertEquals("涛思数据".length(), parameterMetaData_insert.getPrecision(10)); } @Test @@ -71,8 +72,8 @@ public class RestfulParameterMetaDataTest { Assert.assertEquals(0, parameterMetaData_insert.getScale(1)); Assert.assertEquals(0, parameterMetaData_insert.getScale(2)); Assert.assertEquals(0, parameterMetaData_insert.getScale(3)); - Assert.assertEquals(0, parameterMetaData_insert.getScale(4)); - Assert.assertEquals(0, parameterMetaData_insert.getScale(5)); + Assert.assertEquals(31, parameterMetaData_insert.getScale(4)); + Assert.assertEquals(31, parameterMetaData_insert.getScale(5)); Assert.assertEquals(0, parameterMetaData_insert.getScale(6)); Assert.assertEquals(0, parameterMetaData_insert.getScale(7)); Assert.assertEquals(0, parameterMetaData_insert.getScale(8)); @@ -164,7 +165,7 @@ public class RestfulParameterMetaDataTest { pstmt_insert.setObject(7, Byte.MAX_VALUE); pstmt_insert.setObject(8, true); pstmt_insert.setObject(9, "hello".getBytes()); - pstmt_insert.setObject(10, "Hello"); + pstmt_insert.setObject(10, "涛思数据"); parameterMetaData_insert = pstmt_insert.getParameterMetaData(); pstmt_select = conn.prepareStatement(sql_select); From db8defa8da74d56ebc25d211800c5424f8ad154c Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 7 Jun 2021 14:18:40 +0800 Subject: [PATCH 52/57] [td-225] fix compiler error. --- src/query/src/qAggMain.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/query/src/qAggMain.c b/src/query/src/qAggMain.c index ced83d539b..cdc1a67c06 100644 --- a/src/query/src/qAggMain.c +++ b/src/query/src/qAggMain.c @@ -3708,13 +3708,13 @@ static void diff_function(SQLFunctionCtx *pCtx) { } if (pCtx->param[1].nType != INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet - *pOutput = pData[i] - pCtx->param[1].d64; // direct previous may be null + *pOutput = pData[i] - pCtx->param[1].dKey; // direct previous may be null *pTimestamp = tsList[i]; pOutput += 1; pTimestamp += 1; } - pCtx->param[1].d64 = pData[i]; + pCtx->param[1].dKey = pData[i]; pCtx->param[1].nType = pCtx->inputType; notNullElems++; } @@ -3730,13 +3730,13 @@ static void diff_function(SQLFunctionCtx *pCtx) { } if (pCtx->param[1].nType != INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet - *pOutput = pData[i] - pCtx->param[1].d64; // direct previous may be null + *pOutput = pData[i] - pCtx->param[1].dKey; // direct previous may be null *pTimestamp = tsList[i]; pOutput += 1; pTimestamp += 1; } - pCtx->param[1].d64 = pData[i]; + pCtx->param[1].dKey = pData[i]; pCtx->param[1].nType = pCtx->inputType; notNullElems++; } From 76638283b130e95be2f5dedde143903286c29a2b Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 7 Jun 2021 14:28:04 +0800 Subject: [PATCH 53/57] [td-225] fix compiler error. --- src/query/src/qAggMain.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/query/src/qAggMain.c b/src/query/src/qAggMain.c index cdc1a67c06..0cb895709c 100644 --- a/src/query/src/qAggMain.c +++ b/src/query/src/qAggMain.c @@ -3730,7 +3730,7 @@ static void diff_function(SQLFunctionCtx *pCtx) { } if (pCtx->param[1].nType != INITIAL_VALUE_NOT_ASSIGNED) { // initial value is not set yet - *pOutput = pData[i] - pCtx->param[1].dKey; // direct previous may be null + *pOutput = (float)(pData[i] - pCtx->param[1].dKey); // direct previous may be null *pTimestamp = tsList[i]; pOutput += 1; pTimestamp += 1; From e275dd32cd94bc6b6011811efdf50e182350659a Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Mon, 7 Jun 2021 15:28:57 +0800 Subject: [PATCH 54/57] [td-225] fix compiler error. --- src/client/src/tscSQLParser.c | 2 +- src/query/src/qAggMain.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 8f915346f1..15a76daab7 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -2161,7 +2161,7 @@ int32_t addExprAndResultField(SSqlCmd* pCmd, SQueryInfo* pQueryInfo, int32_t col case TSDB_FUNC_STDDEV: case TSDB_FUNC_LEASTSQR: { // 1. valid the number of parameters - int32_t numOfParams = (pItem->pNode->pParam == NULL)? 0: taosArrayGetSize(pItem->pNode->pParam); + int32_t numOfParams = (pItem->pNode->pParam == NULL)? 0: (int32_t) taosArrayGetSize(pItem->pNode->pParam); if (pItem->pNode->pParam == NULL || (functionId != TSDB_FUNC_LEASTSQR && functionId != TSDB_FUNC_DERIVATIVE && numOfParams != 1) || ((functionId == TSDB_FUNC_LEASTSQR || functionId == TSDB_FUNC_DERIVATIVE) && numOfParams != 3)) { diff --git a/src/query/src/qAggMain.c b/src/query/src/qAggMain.c index 0cb895709c..62be49c3df 100644 --- a/src/query/src/qAggMain.c +++ b/src/query/src/qAggMain.c @@ -3764,6 +3764,7 @@ static void diff_function(SQLFunctionCtx *pCtx) { } break; } + case TSDB_DATA_TYPE_TINYINT: { int8_t *pData = (int8_t *)data; int8_t *pOutput = (int8_t *)pCtx->pOutput; From 034cd7d8bc7d3c053cae5128c7a5a5ff2731de11 Mon Sep 17 00:00:00 2001 From: liuyq-617 Date: Mon, 7 Jun 2021 09:14:24 +0000 Subject: [PATCH 55/57] [TD-4043]case for session window --- tests/pytest/query/querySession.py | 126 +++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 tests/pytest/query/querySession.py diff --git a/tests/pytest/query/querySession.py b/tests/pytest/query/querySession.py new file mode 100644 index 0000000000..9c8fc747df --- /dev/null +++ b/tests/pytest/query/querySession.py @@ -0,0 +1,126 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import sys +import taos +from util.log import * +from util.cases import * +from util.sql import * + + +class TDTestCase: + def init(self, conn, logSql): + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor()) + + def run(self): + tdSql.prepare() + + print("==============step1") + tdSql.execute( + "create table if not exists st (ts timestamp, tagtype int) tags(dev nchar(50), tag2 binary(16))") + tdSql.execute( + 'CREATE TABLE if not exists dev_001 using st tags("dev_01", "tag_01")') + tdSql.execute( + 'CREATE TABLE if not exists dev_002 using st tags("dev_02", "tag_02")') + + print("==============step2") + + tdSql.execute( + """INSERT INTO dev_001 VALUES('2020-05-13 10:00:00.000', 1)('2020-05-13 10:00:00.005', 2)('2020-05-13 10:00:00.011', 3) + ('2020-05-13 10:00:01.011', 4)('2020-05-13 10:00:01.611', 5)('2020-05-13 10:00:02.612', 6) + ('2020-05-13 10:01:02.612', 7)('2020-05-13 10:02:02.612', 8)('2020-05-13 10:03:02.613', 9) + ('2020-05-13 11:00:00.000', 10)('2020-05-13 12:00:00.000', 11)('2020-05-13 13:00:00.001', 12) + ('2020-05-14 13:00:00.001', 13)('2020-05-15 14:00:00.000', 14)('2020-05-20 10:00:00.000', 15) + ('2020-05-27 10:00:00.001', 16) dev_002 VALUES('2020-05-13 10:00:00.000', 1)('2020-05-13 10:00:00.005', 2)('2020-05-13 10:00:00.009', 3)('2020-05-13 10:00:00.0021', 4) + ('2020-05-13 10:00:00.031', 5)('2020-05-13 10:00:00.036', 6)('2020-05-13 10:00:00.51', 7) + """) + + # session(ts,5a) + tdSql.query("select count(*) from dev_001 session(ts,5a)") + tdSql.checkRows(15) + tdSql.checkData(0, 1, 2) + + + # session(ts,1s) + tdSql.query("select count(*) from dev_001 session(ts,1s)") + tdSql.checkRows(12) + tdSql.checkData(0, 1, 5) + + tdSql.query("select count(*) from dev_001 session(ts,1000a)") + tdSql.checkRows(12) + tdSql.checkData(0, 1, 5) + + # session(ts,1m) + tdSql.query("select count(*) from dev_001 session(ts,1m)") + tdSql.checkRows(9) + tdSql.checkData(0, 1, 8) + + # session(ts,1h) + tdSql.query("select count(*) from dev_001 session(ts,1h)") + tdSql.checkRows(6) + tdSql.checkData(0, 1, 11) + + # session(ts,1d) + tdSql.query("select count(*) from dev_001 session(ts,1d)") + tdSql.checkRows(4) + tdSql.checkData(0, 1, 13) + + # session(ts,1w) + tdSql.query("select count(*) from dev_001 session(ts,1w)") + tdSql.checkRows(2) + tdSql.checkData(0, 1, 15) + + # session with where + tdSql.query("select count(*),first(tagtype),last(tagtype),avg(tagtype),sum(tagtype),min(tagtype),max(tagtype),leastsquares(tagtype, 1, 1),spread(tagtype) from dev_001 where ts <'2020-05-20 0:0:0' session(ts,1d)") + tdSql.checkRows(2) + tdSql.checkData(0, 1, 13) + tdSql.checkData(0, 2, 1) + tdSql.checkData(0, 3, 13) + tdSql.checkData(0, 4, 7) + tdSql.checkData(0, 5, 91) + tdSql.checkData(0, 6, 1) + tdSql.checkData(0, 7, 13) + tdSql.checkData(0, 8, '{slop:1.000000, intercept:0.000000}') + tdSql.checkData(0, 9, 12) + + # tdsql err + tdSql.error("select * from dev_001 session(ts,1w)") + tdSql.error("select count(*) from st session(ts,1w)") + tdSql.error("select count(*) from dev_001 group by tagtype session(ts,1w) ") + tdSql.error("select count(*) from dev_001 session(ts,1n)") + tdSql.error("select count(*) from dev_001 session(ts,1y)") + tdSql.error("select count(*) from dev_001 session(ts,0s)") + tdSql.error("select count(*) from dev_001 session(i,1y)") + tdSql.error("select count(*) from dev_001 session(ts,1d) where ts <'2020-05-20 0:0:0'") + + #test precision us + tdSql.execute("create database test precision 'us'") + tdSql.execute("use test") + tdSql.execute("create table dev_001 (ts timestamp ,i timestamp ,j int)") + tdSql.execute("insert into dev_001 values(1623046993681000,now,1)(1623046993681001,now+1s,2)(1623046993681002,now+2s,3)(1623046993681004,now+5s,4)") + + # session(ts,1u) + tdSql.query("select count(*) from dev_001 session(ts,1u)") + tdSql.checkRows(2) + tdSql.checkData(0, 1, 3) + tdSql.error("select count(*) from dev_001 session(i,1s)") + + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase()) From 83c2ebaa0b54633ea71df66dd61ba72b2e6a0fab Mon Sep 17 00:00:00 2001 From: Shuduo Sang Date: Mon, 7 Jun 2021 17:40:19 +0800 Subject: [PATCH 56/57] [TD-4606]: windows compile argument mistake. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2c33119e34..0e1adcd97c 100644 --- a/README.md +++ b/README.md @@ -131,10 +131,10 @@ cmake .. -DCPUTYPE=mips64 && cmake --build . ### On Windows platform If you use the Visual Studio 2013, please open a command window by executing "cmd.exe". -Please specify "x86_amd64" for 64 bits Windows or specify "x86" is for 32 bits Windows when you execute vcvarsall.bat. +Please specify "amd64" for 64 bits Windows or specify "x86" is for 32 bits Windows when you execute vcvarsall.bat. ```cmd mkdir debug && cd debug -"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" < x86_amd64 | x86 > +"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" < amd64 | x86 > cmake .. -G "NMake Makefiles" nmake ``` From d87cc3b641981bae95047868d44c7f9b0db9c78b Mon Sep 17 00:00:00 2001 From: Ping Xiao Date: Mon, 7 Jun 2021 18:10:42 +0800 Subject: [PATCH 57/57] [TD-4421]: add test case for subquery with filter --- tests/pytest/fulltest.sh | 2 +- tests/pytest/query/subqueryFilter.py | 79 ++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 tests/pytest/query/subqueryFilter.py diff --git a/tests/pytest/fulltest.sh b/tests/pytest/fulltest.sh index be920dc1ba..68d7bbefee 100755 --- a/tests/pytest/fulltest.sh +++ b/tests/pytest/fulltest.sh @@ -229,7 +229,7 @@ python3 ./test.py -f query/queryFilterTswithDateUnit.py python3 ./test.py -f query/queryTscomputWithNow.py python3 ./test.py -f query/computeErrorinWhere.py python3 ./test.py -f query/queryTsisNull.py - +python3 ./test.py -f query/subqueryFilter.py #stream diff --git a/tests/pytest/query/subqueryFilter.py b/tests/pytest/query/subqueryFilter.py new file mode 100644 index 0000000000..1e111360e2 --- /dev/null +++ b/tests/pytest/query/subqueryFilter.py @@ -0,0 +1,79 @@ +################################################################### +# Copyright (c) 2016 by TAOS Technologies, Inc. +# All rights reserved. +# +# This file is proprietary and confidential to TAOS Technologies. +# No part of this file may be reproduced, stored, transmitted, +# disclosed or used in any form or by any means other than as +# expressly provided by the written permission from Jianhui Tao +# +################################################################### + +# -*- coding: utf-8 -*- + +import sys +import taos +from util.log import tdLog +from util.cases import tdCases +from util.sql import tdSql +from util.dnodes import tdDnodes + +class TDTestCase: + def init(self, conn, logSql): + tdLog.debug("start to execute %s" % __file__) + tdSql.init(conn.cursor(), logSql) + + self.ts = 1601481600000 + self.tables = 10 + self.perfix = 'dev' + + def insertData(self): + print("==============step1") + tdSql.execute( + "create table if not exists st (ts timestamp, tagtype int) tags(dev nchar(50))") + + for i in range(self.tables): + tdSql.execute("create table %s%d using st tags(%d)" % (self.perfix, i, i)) + rows = 15 + i + for j in range(rows): + tdSql.execute("insert into %s%d values(%d, %d)" %(self.perfix, i, self.ts + i * 20 * 10000 + j * 10000, j)) + + def run(self): + tdSql.prepare() + + self.insertData() + + tdSql.query("select count(*) val from st group by tbname") + tdSql.checkRows(10) + + tdSql.query("select * from (select count(*) val from st group by tbname)") + tdSql.checkRows(10) + + tdSql.query("select * from (select count(*) val from st group by tbname) a where a.val < 20") + tdSql.checkRows(5) + + tdSql.query("select * from (select count(*) val from st group by tbname) a where a.val > 20") + tdSql.checkRows(4) + + tdSql.query("select * from (select count(*) val from st group by tbname) a where a.val = 20") + tdSql.checkRows(1) + + tdSql.query("select * from (select count(*) val from st group by tbname) a where a.val <= 20") + tdSql.checkRows(6) + + tdSql.query("select * from (select count(*) val from st group by tbname) a where a.val >= 20") + tdSql.checkRows(5) + + tdSql.query("select count(*) from (select first(tagtype) val from st interval(30s)) a where a.val > 20") + tdSql.checkRows(1) + tdSql.checkData(0, 0, 1) + + + + def stop(self): + tdSql.close() + tdLog.success("%s successfully executed" % __file__) + + +tdCases.addWindows(__file__, TDTestCase()) +tdCases.addLinux(__file__, TDTestCase())