提交 de52faff 编写于 作者: wmmhello's avatar wmmhello

enable different value type of the same key in json

上级 3d868b5b
......@@ -747,7 +747,18 @@ int32_t tagValCompar(const void* p1, const void* p2) {
}else if(!f1IsNull && f2IsNull){
return 1;
}else {
assert(*t1->tag == *t1->tag);
bool f1IsJsonNull = (*t1->tag == TSDB_DATA_TYPE_BINARY && *(uint32_t*)(t1->tag + CHAR_BYTES) == TSDB_DATA_JSON_null);
bool f2IsJsonNull = (*t2->tag == TSDB_DATA_TYPE_BINARY && *(uint32_t*)(t2->tag + CHAR_BYTES) == TSDB_DATA_JSON_null);
if(f1IsJsonNull && f2IsJsonNull){
return 0;
}else if(f1IsJsonNull && !f2IsJsonNull){
return -1;
}else if(!f1IsJsonNull && f2IsJsonNull) {
return 1;
}
if(*t1->tag != *t2->tag) {
return 1;
}
__compar_fn_t func = getComparFunc(t1->tag[0], 0);
return func(t1->tag + CHAR_BYTES, t2->tag + CHAR_BYTES);
}
......
......@@ -5602,7 +5602,6 @@ int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, in
}
char *jsonKey = item->string;
if(strtrim(jsonKey) == 0) continue;
if(!isValidateTag(jsonKey)){
tscError("json key not validate");
retCode = tscSQLSyntaxErrMsg(errMsg, "json key not validate", NULL);
......@@ -5629,7 +5628,6 @@ int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, in
if(item->type == cJSON_String){ // add json value format: type|data
char *jsonValue = item->valuestring;
strtrim(jsonValue);
outLen = 0;
char tagVal[TSDB_MAX_JSON_TAGS_LEN] = {0};
*tagVal = jsonType2DbType(0, item->type); // type
......
......@@ -74,6 +74,7 @@ extern const int32_t TYPE_BYTES[16];
#define TSDB_DATA_JSON_NULL 0xFFFFFFFF
#define TSDB_DATA_JSON_null 0xFFFFFFFE
#define TSDB_DATA_JSON_NOT_NULL 0x01
#define TSDB_DATA_JSON_CAN_NOT_COMPARE 0x7FFFFFFF
#define TSDB_DATA_UTINYINT_NULL 0xFF
#define TSDB_DATA_USMALLINT_NULL 0xFFFF
......
......@@ -377,7 +377,18 @@ int32_t columnValueAscendingComparator(char *f1, char *f2, int32_t type, int32_t
}else if(!f1IsNull && f2IsNull){
return 1;
}else{
assert(*f1 == *f2);
bool f1IsJsonNull = (*f1 == TSDB_DATA_TYPE_BINARY && *(uint32_t*)(f1 + CHAR_BYTES) == TSDB_DATA_JSON_null);
bool f2IsJsonNull = (*f2 == TSDB_DATA_TYPE_BINARY && *(uint32_t*)(f1 + CHAR_BYTES) == TSDB_DATA_JSON_null);
if(f1IsJsonNull && f2IsJsonNull){
return 0;
}else if(f1IsJsonNull && !f2IsJsonNull){
return -1;
}else if(!f1IsJsonNull && f2IsJsonNull) {
return 1;
}
if(*f1 != *f2) {
return 1;
}
type = *f1;
f1 += CHAR_BYTES;
f2 += CHAR_BYTES;
......
......@@ -1910,6 +1910,8 @@ int32_t filterInitValFieldData(SFilterInfo *info) {
bool filterDoCompare(__compar_fn_t func, uint8_t optr, void *left, void *right) {
int32_t ret = func(left, right);
if(ret == TSDB_DATA_JSON_CAN_NOT_COMPARE) return false;
switch (optr) {
case TSDB_RELATION_EQUAL: {
return ret == 0;
......
......@@ -220,17 +220,21 @@ int32_t compareLenPrefixedWStrDesc(const void* pLeft, const void* pRight) {
}
int32_t compareJsonVal(const void *pLeft, const void *pRight) {
if(*(char*)pLeft == TSDB_DATA_TYPE_BINARY){ // json null
return -1;
}
const tVariant* right = pRight;
if(right->nType != *(char*)pLeft) return -1;
if(right->nType != *(char*)pLeft && !(IS_NUMERIC_TYPE(right->nType) && IS_NUMERIC_TYPE(*(char*)pLeft)))
return TSDB_DATA_JSON_CAN_NOT_COMPARE;
uint8_t type = *(char*)pLeft;
char* realData = POINTER_SHIFT(pLeft, CHAR_BYTES);
if(type == TSDB_DATA_TYPE_BOOL) {
DEFAULT_COMP(GET_INT8_VAL(realData), right->i64);
}else if(type == TSDB_DATA_TYPE_BIGINT){
DEFAULT_COMP(GET_INT64_VAL(realData), right->i64);
DEFAULT_COMP(GET_INT64_VAL(realData), (right->nType == TSDB_DATA_TYPE_BIGINT) ? right->i64 : right->dKey);
}else if(type == TSDB_DATA_TYPE_DOUBLE){
DEFAULT_DOUBLE_COMP(GET_DOUBLE_VAL(realData), right->dKey);
DEFAULT_DOUBLE_COMP(GET_DOUBLE_VAL(realData), (right->nType == TSDB_DATA_TYPE_DOUBLE) ? right->dKey : right->i64);
}else if(type == TSDB_DATA_TYPE_NCHAR){
if (varDataLen(realData) != right->nLen) {
return varDataLen(realData) > right->nLen ? 1 : -1;
......@@ -243,7 +247,6 @@ int32_t compareJsonVal(const void *pLeft, const void *pRight) {
}else{
assert(0);
}
return 0;
}
/*
......@@ -624,7 +627,18 @@ int32_t doCompare(const char* f1, const char* f2, int32_t type, size_t size) {
}else if(!f1IsNull && f2IsNull){
return 1;
}else{
assert(*f1 == *f2);
bool f1IsJsonNull = (*f1 == TSDB_DATA_TYPE_BINARY && *(uint32_t*)(f1 + CHAR_BYTES) == TSDB_DATA_JSON_null);
bool f2IsJsonNull = (*f2 == TSDB_DATA_TYPE_BINARY && *(uint32_t*)(f1 + CHAR_BYTES) == TSDB_DATA_JSON_null);
if(f1IsJsonNull && f2IsJsonNull){
return 0;
}else if(f1IsJsonNull && !f2IsJsonNull){
return -1;
}else if(!f1IsJsonNull && f2IsJsonNull) {
return 1;
}
if(*f1 != *f2) {
return 1;
}
type = *f1;
f1 += CHAR_BYTES;
f2 += CHAR_BYTES;
......
......@@ -528,7 +528,7 @@ bool isValidateTag(char *input) {
if (!input) return false;
int len = strlen(input);
if (len == 0) return false;
for (int i = 1; i < len; ++i) {
for (int i = 0; i < len; ++i) {
if (isprint(input[i]) == 0) return false;
}
return true;
......
......@@ -27,244 +27,330 @@ class TDTestCase:
def run(self):
tdSql.prepare()
print("==============step1")
tdLog.info("create database and table")
tdSql.execute("create database db_json_tag_test")
tdSql.execute("create table if not exists db_json_tag_test.jsons1(ts timestamp, dataInt int, dataBool bool, dataStr nchar(50)) tags(jtag json)")
tdSql.execute("CREATE TABLE if not exists db_json_tag_test.jsons1_1 using db_json_tag_test.jsons1 tags('{\"loc\":\"fff\",\"id\":5}')")
tdSql.execute("insert into db_json_tag_test.jsons1_2 using db_json_tag_test.jsons1 tags('{\"num\":5,\"location\":\"beijing\"}') values (1591060628000, 2, true, 'json2')")
tdSql.execute("insert into db_json_tag_test.jsons1_1 values(1591060638000, 1, false, 'json1')")
tdSql.execute("insert into db_json_tag_test.jsons1_3 using db_json_tag_test.jsons1 tags('{\"num\":34,\"location\":\"beijing\",\"level\":\"l1\"}') values (1591060668000, 3, false 'json3')")
tdSql.execute("insert into db_json_tag_test.jsons1_4 using db_json_tag_test.jsons1 tags('{\"class\":55,\"location\":\"shanghai\",\"name\":\"name4\"}') values (1591060728000, 4, true, 'json4')")
print("============== STEP 1 ===== prepare data & validate json string")
tdSql.execute("create table if not exists jsons1(ts timestamp, dataInt int, dataBool bool, dataStr nchar(50), dataStrBin binary(150)) tags(jtag json)")
tdSql.execute("insert into jsons1_1 using jsons1 tags('{\"tag1\":\"fff\",\"tag2\":5, \"tag3\":true}') values(1591060618000, 1, false, 'json1', '你是') (1591060608000, 23, true, '等等', 'json')")
tdSql.execute("insert into jsons1_2 using jsons1 tags('{\"tag1\":5,\"tag2\":\"beijing\"}') values (1591060628000, 2, true, 'json2', 'sss')")
tdSql.execute("insert into jsons1_3 using jsons1 tags('{\"tag1\":false,\"tag2\":\"beijing\"}') values (1591060668000, 3, false, 'json3', 'efwe')")
tdSql.execute("insert into jsons1_4 using jsons1 tags('{\"tag1\":null,\"tag2\":\"shanghai\",\"tag3\":\"hello\"}') values (1591060728000, 4, true, 'json4', '323sd')")
tdSql.execute("insert into jsons1_5 using jsons1 tags('{\"tag1\":1.232, \"tag2\":null}') values(1591060928000, 1, false, '你就会', 'ewe')")
tdSql.execute("insert into jsons1_6 using jsons1 tags('{\"tag1\":11,\"tag2\":\"\",\"tag2\":null}') values(1591061628000, 11, false, '你就会','')")
tdSql.execute("insert into jsons1_7 using jsons1 tags('{\"tag1\":\"收到货\",\"tag2\":\"\",\"tag3\":null}') values(1591062628000, 2, NULL, '你就会', 'dws')")
# test duplicate key using the first one. elimate empty key
tdSql.execute("CREATE TABLE if not exists jsons1_8 using jsons1 tags('{\"tag1\":null, \"tag1\":true, \"tag1\":45, \"1tag$\":2, \" \":90}')")
# test empty json string, save as jtag is NULL
tdSql.execute("insert into jsons1_9 using jsons1 tags('\t') values (1591062328000, 24, NULL, '你就会', '2sdw')")
tdSql.execute("CREATE TABLE if not exists jsons1_10 using jsons1 tags('')")
tdSql.execute("CREATE TABLE if not exists jsons1_11 using jsons1 tags(' ')")
tdSql.execute("CREATE TABLE if not exists jsons1_12 using jsons1 tags('{}')")
tdSql.execute("CREATE TABLE if not exists jsons1_13 using jsons1 tags('null')")
# test invalidate json
tdSql.error("CREATE TABLE if not exists jsons1_14 using jsons1 tags('\"efwewf\"')")
tdSql.error("CREATE TABLE if not exists jsons1_14 using jsons1 tags('3333')")
tdSql.error("CREATE TABLE if not exists jsons1_14 using jsons1 tags('33.33')")
tdSql.error("CREATE TABLE if not exists jsons1_14 using jsons1 tags('false')")
tdSql.error("CREATE TABLE if not exists jsons1_14 using jsons1 tags('[1,true]')")
tdSql.error("CREATE TABLE if not exists jsons1_14 using jsons1 tags('{222}')")
tdSql.error("CREATE TABLE if not exists jsons1_14 using jsons1 tags('{\"fe\"}')")
# test invalidate json key, key must can be printed assic char
tdSql.error("CREATE TABLE if not exists jsons1_14 using jsons1 tags('{\"tag1\":[1,true]}')")
tdSql.error("CREATE TABLE if not exists jsons1_14 using jsons1 tags('{\"tag1\":{}}')")
tdSql.error("CREATE TABLE if not exists jsons1_14 using jsons1 tags('{\"。loc\":\"fff\"}')")
tdSql.error("CREATE TABLE if not exists jsons1_14 using jsons1 tags('{\"\":\"fff\"}')")
tdSql.error("CREATE TABLE if not exists jsons1_14 using jsons1 tags('{\"\t\":\"fff\"}')")
tdSql.error("CREATE TABLE if not exists jsons1_14 using jsons1 tags('{\"试试\":\"fff\"}')")
print("============== STEP 2 ===== alter table json tag")
tdSql.error("ALTER STABLE jsons1 add tag tag2 nchar(20)")
tdSql.error("ALTER STABLE jsons1 drop tag jtag")
tdSql.error("ALTER TABLE jsons1_1 SET TAG jtag=4")
tdSql.execute("ALTER TABLE jsons1_1 SET TAG jtag='{\"tag1\":\"femail\",\"tag2\":35,\"tag3\":true}'")
print("============== STEP 3 ===== query table")
# test error syntax
tdSql.error("select * from jsons1 where jtag->tag1='beijing'")
tdSql.error("select * from jsons1 where jtag->'location'")
tdSql.error("select * from jsons1 where jtag->''")
tdSql.error("select * from jsons1 where jtag->''=9")
tdSql.error("select -> from jsons1")
tdSql.error("select ? from jsons1")
tdSql.error("select * from jsons1 where ?")
tdSql.error("select * from jsons1 where jtag->")
tdSql.error("select jtag->location from jsons1")
tdSql.error("select jtag?location from jsons1")
tdSql.error("select * from jsons1 where jtag?location")
tdSql.error("select * from jsons1 where jtag?''")
tdSql.error("select * from jsons1 where jtag?'location'='beijing'")
# test select normal column
tdSql.query("select dataint from jsons1")
tdSql.checkRows(9)
tdSql.checkData(1, 0, 1)
print("==============step2")
tdLog.info("alter stable add tag")
tdSql.error("ALTER STABLE db_json_tag_test.jsons1 add tag tag2 nchar(20)")
# test select json tag
tdSql.query("select * from jsons1")
tdSql.checkRows(9)
tdSql.query("select jtag from jsons1")
tdSql.checkRows(13)
tdSql.query("select jtag from jsons1 where jtag is null")
tdSql.checkRows(5)
tdSql.query("select jtag from jsons1 where jtag is not null")
tdSql.checkRows(8)
# test #line 41
tdSql.query("select jtag from jsons1_8")
tdSql.checkData(0, 0, '{"tag1":null,"1tag$":2," ":90}')
# test #line 72
tdSql.query("select jtag from jsons1_1")
tdSql.checkData(0, 0, '{"tag1":"femail","tag2":35,"tag3":true}')
# test jtag is NULL
tdSql.query("select jtag from jsons1_9")
tdSql.checkData(0, 0, None)
tdSql.error("ALTER STABLE db_json_tag_test.jsons1 drop tag jtag")
tdSql.error("ALTER TABLE db_json_tag_test.jsons1_1 SET TAG jtag=4")
tdSql.execute("ALTER TABLE db_json_tag_test.jsons1_1 SET TAG jtag='{\"sex\":\"femail\",\"age\":35, \"isKey\":true}'")
tdSql.query("select jtag from db_json_tag_test.jsons1_1")
tdSql.checkData(0, 0, "{\"sex\":\"femail\",\"age\":35,\"isKey\":true}")
# test select json tag->'key', value is string
tdSql.query("select jtag->'tag1' from jsons1_1")
tdSql.checkData(0, 0, '"femail"')
tdSql.query("select jtag->'tag2' from jsons1_6")
tdSql.checkData(0, 0, '""')
# test select json tag->'key', value is int
tdSql.query("select jtag->'tag2' from jsons1_1")
tdSql.checkData(0, 0, 35)
# test select json tag->'key', value is bool
tdSql.query("select jtag->'tag3' from jsons1_1")
tdSql.checkData(0, 0, "true")
# test select json tag->'key', value is null
tdSql.query("select jtag->'tag1' from jsons1_4")
tdSql.checkData(0, 0, "null")
# test select json tag->'key', value is double
tdSql.query("select jtag->'tag1' from jsons1_5")
tdSql.checkData(0, 0, "1.232000000")
# test select json tag->'key', key is not exist
tdSql.query("select jtag->'tag10' from jsons1_4")
tdSql.checkData(0, 0, None)
print("==============step3")
tdLog.info("select table")
tdSql.query("select jtag->'tag1' from jsons1")
tdSql.checkRows(13)
# test header name
res = tdSql.getColNameList("select jtag->'tag1' from jsons1")
cname_list = []
cname_list.append("jtag->'tag1'")
tdSql.checkColNameList(res, cname_list)
tdSql.query("select * from db_json_tag_test.jsons1")
tdSql.checkRows(4)
# error test
#tdSql.error("select * from db_json_tag_test.jsons1 where jtag->'location'=4")
tdSql.error("select * from db_json_tag_test.jsons1 where jtag->location='beijing'")
tdSql.error("select * from db_json_tag_test.jsons1 where jtag->'location'")
tdSql.error("select * from db_json_tag_test.jsons1 where jtag->''")
tdSql.error("select * from db_json_tag_test.jsons1 where jtag->''=9")
tdSql.error("select jtag->location from db_json_tag_test.jsons1")
tdSql.error("select jtag?location from db_json_tag_test.jsons1")
tdSql.error("select * from db_json_tag_test.jsons1 where jtag?location")
tdSql.error("select * from db_json_tag_test.jsons1 where jtag?''")
tdSql.error("select * from db_json_tag_test.jsons1 where jtag?'location'='beijing'")
# test select condition
tdSql.query("select jtag->'location' from db_json_tag_test.jsons1_2")
tdSql.checkData(0, 0, "\"beijing\"")
tdSql.query("select jtag->'location' from db_json_tag_test.jsons1")
tdSql.checkRows(4)
tdSql.query("select jtag from db_json_tag_test.jsons1_1")
# test where with json tag
tdSql.error("select * from jsons1_1 where jtag is not null")
# where json value is string
tdSql.query("select * from jsons1 where jtag->'tag2'='beijing'")
tdSql.checkRows(2)
tdSql.query("select dataint,tbname,jtag->'tag1',jtag from jsons1 where jtag->'tag2'='beijing'")
tdSql.checkData(0, 0, 2)
tdSql.checkData(0, 1, 'jsons1_2')
tdSql.checkData(0, 2, 5)
tdSql.checkData(0, 3, '{"tag1":5,"tag2":"beijing"}')
tdSql.checkData(1, 0, 3)
tdSql.checkData(1, 1, 'jsons1_3')
tdSql.checkData(1, 2, 'false')
tdSql.query("select * from jsons1 where jtag->'tag1'='beijing'")
tdSql.checkRows(0)
tdSql.query("select * from jsons1 where jtag->'tag1'='收到货'")
tdSql.checkRows(1)
# where json value is int
tdSql.query("select * from jsons1 where jtag->'tag1'=5")
tdSql.checkRows(1)
tdSql.checkData(0, 1, 2)
tdSql.query("select * from jsons1 where jtag->'tag1'=10")
tdSql.checkRows(0)
# where json value is double
tdSql.query("select * from jsons1 where jtag->'tag1'=1.232")
tdSql.checkRows(1)
# where json value is null
tdSql.query("select * from jsons1 where jtag->'tag1'=null")
tdSql.checkRows(1)
# where json value is not exist
tdSql.query("select * from jsons1 where jtag->'tag1' is null")
tdSql.checkData(0, 0, 'jsons1_9')
tdSql.checkRows(1)
tdSql.query("select * from jsons1 where jtag->'tag4' is null")
tdSql.checkRows(9)
tdSql.query("select * from jsons1 where jtag->'tag3' is not null")
tdSql.checkRows(4)
# test json string value
tdSql.query("select * from db_json_tag_test.jsons1 where jtag->'location'='beijing'")
tdSql.checkRows(2)
tdSql.query("select * from db_json_tag_test.jsons1 where jtag->'location'!='beijing'")
# test json tag in where condition with and/or/?
tdSql.query("select * from jsons1 where jtag->'location'!='beijing'")
tdSql.checkRows(1)
tdSql.query("select jtag->'num' from db_json_tag_test.jsons1 where jtag->'level'='l1'")
tdSql.query("select jtag->'num' from jsons1 where jtag->'level'='l1'")
tdSql.checkData(0, 0, 34)
# test json number value
tdSql.query("select *,tbname from db_json_tag_test.jsons1 where jtag->'class'>5 and jtag->'class'<9")
tdSql.query("select *,tbname from jsons1 where jtag->'class'>5 and jtag->'class'<9")
tdSql.checkRows(0)
tdSql.query("select *,tbname from db_json_tag_test.jsons1 where jtag->'class'>5 and jtag->'class'<92")
tdSql.query("select *,tbname from jsons1 where jtag->'class'>5 and jtag->'class'<92")
tdSql.checkRows(1)
# test where condition
tdSql.query("select * from db_json_tag_test.jsons1 where jtag?'sex' or jtag?'num'")
tdSql.query("select * from jsons1 where jtag?'sex' or jtag?'num'")
tdSql.checkRows(3)
tdSql.query("select * from db_json_tag_test.jsons1 where jtag?'sex' or jtag?'numww'")
tdSql.query("select * from jsons1 where jtag?'sex' or jtag?'numww'")
tdSql.checkRows(1)
tdSql.query("select * from db_json_tag_test.jsons1 where jtag?'sex' and jtag?'num'")
tdSql.query("select * from jsons1 where jtag?'sex' and jtag?'num'")
tdSql.checkRows(0)
tdSql.query("select jtag->'sex' from db_json_tag_test.jsons1 where jtag?'sex' or jtag?'num'")
tdSql.query("select jtag->'sex' from jsons1 where jtag?'sex' or jtag?'num'")
tdSql.checkRows(3)
tdSql.query("select *,tbname from db_json_tag_test.jsons1 where jtag->'location'='beijing'")
tdSql.query("select *,tbname from jsons1 where jtag->'location'='beijing'")
tdSql.checkRows(2)
tdSql.query("select *,tbname from db_json_tag_test.jsons1 where jtag->'num'=5 or jtag?'sex'")
tdSql.query("select *,tbname from jsons1 where jtag->'num'=5 or jtag?'sex'")
tdSql.checkRows(2)
tdSql.query("select *,tbname from db_json_tag_test.jsons1 where jtag->'num'=5")
tdSql.query("select *,tbname from jsons1 where jtag->'num'=5")
tdSql.checkRows(1)
# test with tbname
tdSql.query("select * from db_json_tag_test.jsons1 where tbname = 'jsons1_1'")
tdSql.query("select * from jsons1 where tbname = 'jsons1_1'")
tdSql.checkRows(1)
tdSql.query("select * from db_json_tag_test.jsons1 where tbname = 'jsons1_1' or jtag?'num'")
tdSql.query("select * from jsons1 where tbname = 'jsons1_1' or jtag?'num'")
tdSql.checkRows(3)
tdSql.query("select * from db_json_tag_test.jsons1 where tbname = 'jsons1_1' and jtag?'num'")
tdSql.query("select * from jsons1 where tbname = 'jsons1_1' and jtag?'num'")
tdSql.checkRows(0)
tdSql.query("select * from db_json_tag_test.jsons1 where tbname = 'jsons1_1' or jtag->'num'=5")
tdSql.query("select * from jsons1 where tbname = 'jsons1_1' or jtag->'num'=5")
tdSql.checkRows(2)
# test where condition like
tdSql.query("select *,tbname from db_json_tag_test.jsons1 where jtag->'location' like 'bei%'")
tdSql.query("select *,tbname from jsons1 where jtag->'location' like 'bei%'")
tdSql.checkRows(2)
tdSql.query("select *,tbname from db_json_tag_test.jsons1 where jtag->'location' like 'bei%' and jtag->'location'='beijin'")
tdSql.query("select *,tbname from jsons1 where jtag->'location' like 'bei%' and jtag->'location'='beijin'")
tdSql.checkRows(0)
tdSql.query("select *,tbname from db_json_tag_test.jsons1 where jtag->'location' like 'bei%' or jtag->'location'='beijin'")
tdSql.query("select *,tbname from jsons1 where jtag->'location' like 'bei%' or jtag->'location'='beijin'")
tdSql.checkRows(2)
tdSql.query("select *,tbname from db_json_tag_test.jsons1 where jtag->'location' like 'bei%' and jtag->'num'=34")
tdSql.query("select *,tbname from jsons1 where jtag->'location' like 'bei%' and jtag->'num'=34")
tdSql.checkRows(1)
tdSql.query("select *,tbname from db_json_tag_test.jsons1 where (jtag->'location' like 'bei%' or jtag->'num'=34) and jtag->'class'=55")
tdSql.query("select *,tbname from jsons1 where (jtag->'location' like 'bei%' or jtag->'num'=34) and jtag->'class'=55")
tdSql.checkRows(0)
tdSql.query("select * from db_json_tag_test.jsons1 where jtag->'num' like '5%'")
tdSql.query("select * from jsons1 where jtag->'num' like '5%'")
tdSql.checkRows(0)
# test where condition in
tdSql.error("select * from db_json_tag_test.jsons1 where jtag->'location' in ('beijing')")
tdSql.error("select * from db_json_tag_test.jsons1 where jtag->'num' in ('5',34)")
tdSql.error("select * from jsons1 where jtag->'location' in ('beijing')")
tdSql.error("select * from jsons1 where jtag->'num' in ('5',34)")
# test where condition match
tdSql.query("select * from db_json_tag_test.jsons1 where jtag->'location' match 'jin$'")
tdSql.query("select * from jsons1 where jtag->'location' match 'jin$'")
tdSql.checkRows(0)
tdSql.query("select * from db_json_tag_test.jsons1 where jtag->'location' match 'jin'")
tdSql.query("select * from jsons1 where jtag->'location' match 'jin'")
tdSql.checkRows(2)
tdSql.query("select * from db_json_tag_test.jsons1 where datastr match 'json' and jtag->'location' match 'jin'")
tdSql.query("select * from jsons1 where datastr match 'json' and jtag->'location' match 'jin'")
tdSql.checkRows(2)
tdSql.query("select * from db_json_tag_test.jsons1 where jtag->'num' match '5'")
tdSql.query("select * from jsons1 where jtag->'num' match '5'")
tdSql.checkRows(0)
# test json string parse
tdSql.error("CREATE TABLE if not exists db_json_tag_test.jsons1_5 using db_json_tag_test.jsons1 tags('efwewf')")
tdSql.execute("CREATE TABLE if not exists db_json_tag_test.jsons1_5 using db_json_tag_test.jsons1 tags('\t')")
tdSql.execute("CREATE TABLE if not exists db_json_tag_test.jsons1_6 using db_json_tag_test.jsons1 tags('')")
tdSql.query("select jtag from db_json_tag_test.jsons1_6")
tdSql.query("select jtag from jsons1_6")
tdSql.checkData(0, 0, None)
tdSql.execute("CREATE TABLE if not exists db_json_tag_test.jsons1_7 using db_json_tag_test.jsons1 tags('{}')")
tdSql.query("select jtag from db_json_tag_test.jsons1_7")
tdSql.query("select jtag from jsons1_7")
tdSql.checkData(0, 0, None)
tdSql.execute("CREATE TABLE if not exists db_json_tag_test.jsons1_8 using db_json_tag_test.jsons1 tags('null')")
tdSql.query("select jtag from db_json_tag_test.jsons1_8")
tdSql.query("select jtag from jsons1_8")
tdSql.checkData(0, 0, None)
tdSql.execute("CREATE TABLE if not exists db_json_tag_test.jsons1_9 using db_json_tag_test.jsons1 tags('{\"\":4, \"time\":null}')")
tdSql.query("select jtag from db_json_tag_test.jsons1_9")
tdSql.checkData(0, 0, "{\"time\":null}")
tdSql.execute("CREATE TABLE if not exists db_json_tag_test.jsons1_10 using db_json_tag_test.jsons1 tags('{\"k1\":\"\",\"k1\":\"v1\",\"k2\":true,\"k3\":false,\"k4\":55}')")
tdSql.query("select jtag from db_json_tag_test.jsons1_10")
tdSql.query("select jtag from jsons1_9")
tdSql.checkData(0, 0, "{\"time\":null}")
tdSql.query("select jtag from jsons1_10")
tdSql.checkData(0, 0, "{\"k1\":\"\",\"k2\":true,\"k3\":false,\"k4\":55}")
tdSql.query("select jtag->'k2' from db_json_tag_test.jsons1_10")
tdSql.query("select jtag->'k2' from jsons1_10")
tdSql.checkData(0, 0, "true")
tdSql.query("select jtag from db_json_tag_test.jsons1 where jtag->'k1'=''")
tdSql.query("select jtag from jsons1 where jtag->'k1'=''")
tdSql.checkRows(1)
tdSql.query("select jtag from db_json_tag_test.jsons1 where jtag->'k2'=true")
tdSql.query("select jtag from jsons1 where jtag->'k2'=true")
tdSql.checkRows(1)
tdSql.query("select jtag from db_json_tag_test.jsons1 where jtag is null")
tdSql.query("select jtag from jsons1 where jtag is null")
tdSql.checkRows(4)
tdSql.query("select jtag from db_json_tag_test.jsons1 where jtag is not null")
tdSql.query("select jtag from jsons1 where jtag is not null")
tdSql.checkRows(6)
tdSql.query("select * from db_json_tag_test.jsons1 where jtag->'location' is not null")
tdSql.query("select * from jsons1 where jtag->'location' is not null")
tdSql.checkRows(3)
tdSql.query("select tbname,jtag from db_json_tag_test.jsons1 where jtag->'location' is null")
tdSql.query("select tbname,jtag from jsons1 where jtag->'location' is null")
tdSql.checkRows(7)
tdSql.query("select * from db_json_tag_test.jsons1 where jtag->'num' is not null")
tdSql.query("select * from jsons1 where jtag->'num' is not null")
tdSql.checkRows(2)
tdSql.query("select * from db_json_tag_test.jsons1 where jtag->'location'='null'")
tdSql.query("select * from jsons1 where jtag->'location'='null'")
tdSql.checkRows(0)
tdSql.query("select * from db_json_tag_test.jsons1 where jtag->'num'='null'")
tdSql.query("select * from jsons1 where jtag->'num'='null'")
tdSql.checkRows(0)
# test distinct
tdSql.query("select distinct jtag from db_json_tag_test.jsons1")
tdSql.query("select distinct jtag from jsons1")
tdSql.checkRows(7)
tdSql.query("select distinct jtag->'location' from db_json_tag_test.jsons1")
tdSql.query("select distinct jtag->'location' from jsons1")
tdSql.checkRows(3)
# test chinese
tdSql.execute("CREATE TABLE if not exists db_json_tag_test.jsons1_11 using db_json_tag_test.jsons1 tags('{\"k1\":\"中国\",\"k5\":\"是是是\"}')")
tdSql.query("select tbname,jtag from db_json_tag_test.jsons1 where jtag->'k1' match '中'")
tdSql.query("select tbname,jtag from jsons1 where jtag->'k1' match '中'")
tdSql.checkRows(1)
tdSql.query("select tbname,jtag from db_json_tag_test.jsons1 where jtag->'k1'='中国'")
tdSql.query("select tbname,jtag from jsons1 where jtag->'k1'='中国'")
tdSql.checkRows(1)
#test dumplicate key with normal colomn
tdSql.execute("INSERT INTO db_json_tag_test.jsons1_12 using db_json_tag_test.jsons1 tags('{\"tbname\":\"tt\",\"databool\":true,\"dataStr\":\"是是是\"}') values(1591060828000, 4, false, \"你就会\")")
tdSql.execute("INSERT INTO jsons1_12 using jsons1 tags('{\"tbname\":\"tt\",\"databool\":true,\"dataStr\":\"是是是\"}') values(1591060828000, 4, false, \"你就会\")")
tdSql.query("select *,tbname,jtag from db_json_tag_test.jsons1 where jtag->'dataStr' match '是'")
tdSql.query("select *,tbname,jtag from jsons1 where jtag->'dataStr' match '是'")
tdSql.checkRows(1)
tdSql.query("select tbname,jtag->'tbname' from db_json_tag_test.jsons1 where jtag->'tbname'='tt'")
tdSql.query("select tbname,jtag->'tbname' from jsons1 where jtag->'tbname'='tt'")
tdSql.checkRows(1)
tdSql.query("select *,tbname,jtag from db_json_tag_test.jsons1 where dataBool=true")
tdSql.query("select *,tbname,jtag from jsons1 where dataBool=true")
tdSql.checkRows(2)
# test error
tdSql.error("CREATE TABLE if not exists db_json_tag_test.jsons1_13 using db_json_tag_test.jsons1 tags(3333)")
tdSql.execute("CREATE TABLE if not exists db_json_tag_test.jsons1_13 using db_json_tag_test.jsons1 tags('{\"1loc\":\"fff\",\";id\":5}')")
tdSql.error("CREATE TABLE if not exists db_json_tag_test.jsons1_13 using db_json_tag_test.jsons1 tags('{\"。loc\":\"fff\",\"fsd\":5}')")
tdSql.error("CREATE TABLE if not exists db_json_tag_test.jsons1_13 using db_json_tag_test.jsons1 tags('{\"试试\":\"fff\",\";id\":5}')")
tdSql.error("insert into db_json_tag_test.jsons1_13 using db_json_tag_test.jsons1 tags(3)")
# test join
tdSql.execute("create table if not exists db_json_tag_test.jsons2(ts timestamp, dataInt int, dataBool bool, dataStr nchar(50)) tags(jtag json)")
tdSql.execute("create table if not exists db_json_tag_test.jsons3(ts timestamp, dataInt int, dataBool bool, dataStr nchar(50)) tags(jtag json)")
tdSql.execute("CREATE TABLE if not exists db_json_tag_test.jsons2_1 using db_json_tag_test.jsons2 tags('{\"loc\":\"fff\",\"id\":5}')")
tdSql.execute("insert into db_json_tag_test.jsons3_1 using db_json_tag_test.jsons3 tags('{\"loc\":\"fff\",\"num\":5,\"location\":\"beijing\"}') values ('2020-04-18 15:00:00.000', 2, true, 'json2')")
tdSql.execute("insert into db_json_tag_test.jsons2_1 values('2020-04-18 15:00:00.000', 1, false, 'json1')")
tdSql.query("select 'sss',33,a.jtag->'loc' from db_json_tag_test.jsons2 a,db_json_tag_test.jsons3 b where a.ts=b.ts and a.jtag->'loc'=b.jtag->'loc'")
tdSql.query("select 'sss',33,a.jtag->'loc' from jsons2 a,jsons3 b where a.ts=b.ts and a.jtag->'loc'=b.jtag->'loc'")
tdSql.checkData(0, 0, "sss")
tdSql.checkData(0, 2, "\"fff\"")
res = tdSql.getColNameList("select 'sss',33,a.jtag->'loc' from db_json_tag_test.jsons2 a,db_json_tag_test.jsons3 b where a.ts=b.ts and a.jtag->'loc'=b.jtag->'loc'")
res = tdSql.getColNameList("select 'sss',33,a.jtag->'loc' from jsons2 a,jsons3 b where a.ts=b.ts and a.jtag->'loc'=b.jtag->'loc'")
cname_list = []
cname_list.append("sss")
cname_list.append("33")
......@@ -272,76 +358,76 @@ class TDTestCase:
tdSql.checkColNameList(res, cname_list)
# test group by & order by string
tdSql.query("select avg(dataint),count(*) from db_json_tag_test.jsons1 group by jtag->'location' order by jtag->'location' desc")
tdSql.query("select avg(dataint),count(*) from jsons1 group by jtag->'location' order by jtag->'location' desc")
tdSql.checkData(1, 0, 2.5)
tdSql.checkData(1, 1, 2)
tdSql.checkData(1, 2, "\"beijing\"")
tdSql.checkData(2, 2, None)
# test group by & order by int
tdSql.execute("INSERT INTO db_json_tag_test.jsons1_20 using db_json_tag_test.jsons1 tags('{\"tagint\":1}') values(1591060928000, 1, false, \"你就会\")")
tdSql.execute("INSERT INTO db_json_tag_test.jsons1_21 using db_json_tag_test.jsons1 tags('{\"tagint\":11}') values(1591061628000, 11, false, \"你就会\")")
tdSql.execute("INSERT INTO db_json_tag_test.jsons1_22 using db_json_tag_test.jsons1 tags('{\"tagint\":2}') values(1591062628000, 2, false, \"你就会\")")
tdSql.query("select avg(dataint),count(*) from db_json_tag_test.jsons1 group by jtag->'tagint' order by jtag->'tagint' desc")
tdSql.query("select avg(dataint),count(*) from jsons1 group by jtag->'tagint' order by jtag->'tagint' desc")
tdSql.checkData(0, 0, 11)
tdSql.checkData(0, 2, 11)
tdSql.checkData(1, 2, 2)
tdSql.checkData(3, 2, None)
tdSql.checkData(3, 1, 5)
tdSql.query("select avg(dataint),count(*) from db_json_tag_test.jsons1 group by jtag->'tagint' order by jtag->'tagint'")
tdSql.query("select avg(dataint),count(*) from jsons1 group by jtag->'tagint' order by jtag->'tagint'")
tdSql.checkData(0, 1, 5)
tdSql.checkData(0, 2, None)
tdSql.checkData(1, 2, 1)
tdSql.checkData(3, 2, 11)
# test stddev with group by json tag sting
tdSql.query("select stddev(dataint) from db_json_tag_test.jsons1 group by jtag->'location'")
tdSql.query("select stddev(dataint) from jsons1 group by jtag->'location'")
tdSql.checkData(0, 1, None)
tdSql.checkData(1, 0, 0.5)
tdSql.checkData(2, 0, 0)
tdSql.query("select stddev(dataint) from db_json_tag_test.jsons1 group by jtag->'tagint'")
tdSql.query("select stddev(dataint) from jsons1 group by jtag->'tagint'")
tdSql.checkData(0, 0, 1.16619037896906)
tdSql.checkData(0, 1, None)
tdSql.checkData(1, 0, 0)
tdSql.checkData(2, 1, 2)
res = tdSql.getColNameList("select stddev(dataint) from db_json_tag_test.jsons1 group by jsons1.jtag->'tagint'")
res = tdSql.getColNameList("select stddev(dataint) from jsons1 group by jsons1.jtag->'tagint'")
cname_list = []
cname_list.append("stddev(dataint)")
cname_list.append("jsons1.jtag->'tagint'")
tdSql.checkColNameList(res, cname_list)
# test json->'key'=null
tdSql.execute("insert into db_json_tag_test.jsons1_9 values('2020-04-17 15:20:00.000', 5, false, 'json19')")
tdSql.query("select * from db_json_tag_test.jsons1")
tdSql.query("select * from jsons1")
tdSql.checkRows(9)
tdSql.query("select * from db_json_tag_test.jsons1 where jtag->'time' is null")
tdSql.query("select * from jsons1 where jtag->'time' is null")
tdSql.checkRows(8)
tdSql.query("select * from db_json_tag_test.jsons1 where jtag->'time'=null")
tdSql.query("select * from jsons1 where jtag->'time'=null")
tdSql.checkRows(1)
# subquery with json tag
tdSql.query("select * from (select jtag, dataint from db_json_tag_test.jsons1)")
tdSql.query("select * from (select jtag, dataint from jsons1)")
tdSql.checkRows(9)
tdSql.checkData(1, 1, 2)
tdSql.checkData(6, 0, "{\"tagint\":1}")
tdSql.query("select jtag->'age' from (select jtag->'age', dataint from db_json_tag_test.jsons1)")
tdSql.query("select jtag->'age' from (select jtag->'age', dataint from jsons1)")
tdSql.checkRows(9)
tdSql.checkData(0, 0, 35)
tdSql.checkData(2, 0, None)
res = tdSql.getColNameList("select jtag->'age' from (select jtag->'age', dataint from db_json_tag_test.jsons1)")
res = tdSql.getColNameList("select jtag->'age' from (select jtag->'age', dataint from jsons1)")
cname_list = []
cname_list.append("jtag->'age'")
tdSql.checkColNameList(res, cname_list)
tdSql.query("select ts,tbname,jtag->'location' from (select jtag->'location',tbname,ts from db_json_tag_test.jsons1 order by ts)")
tdSql.query("select ts,tbname,jtag->'location' from (select jtag->'location',tbname,ts from jsons1 order by ts)")
tdSql.checkRows(9)
tdSql.checkData(1, 1, "jsons1_2")
tdSql.checkData(3, 2, "\"beijing\"")
# test different type of json value
tdSql.query("select avg(dataint),count(*) from jsons1 group by jtag->'tagint' order by jtag->'tagint' desc")
def stop(self):
tdSql.close()
tdLog.success("%s successfully executed" % __file__)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册