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

fix bugs and add test case

上级 59dea347
...@@ -738,36 +738,12 @@ int32_t tagValCompar(const void* p1, const void* p2) { ...@@ -738,36 +738,12 @@ int32_t tagValCompar(const void* p1, const void* p2) {
const STidTags* t2 = (const STidTags*) varDataVal(p2); const STidTags* t2 = (const STidTags*) varDataVal(p2);
if (t1->padding == TSDB_DATA_TYPE_JSON){ if (t1->padding == TSDB_DATA_TYPE_JSON){
bool f1IsNull = (*t1->tag == TSDB_DATA_TYPE_JSON && isNull(t1->tag + CHAR_BYTES, TSDB_DATA_TYPE_JSON)); bool canReturn = true;
bool f2IsNull = (*t1->tag == TSDB_DATA_TYPE_JSON && isNull(t1->tag + CHAR_BYTES, TSDB_DATA_TYPE_JSON)); int32_t result = jsonCompareUnit(t1->tag, t2->tag, &canReturn);
if(f1IsNull && f2IsNull){ if(canReturn) return result;
return 0;
}else if(f1IsNull && !f2IsNull){
return -1;
}else if(!f1IsNull && f2IsNull){
return 1;
}else {
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 && !(IS_NUMERIC_TYPE(*t1->tag) && IS_NUMERIC_TYPE(*t2->tag))) {
return 1;
}
if(*t1->tag == TSDB_DATA_TYPE_BIGINT && *t2->tag == TSDB_DATA_TYPE_DOUBLE){
DEFAULT_COMP(GET_INT64_VAL(t1->tag + CHAR_BYTES), GET_DOUBLE_VAL(t2->tag + CHAR_BYTES));
}else if(*t1->tag == TSDB_DATA_TYPE_DOUBLE && *t2->tag == TSDB_DATA_TYPE_BIGINT){
DEFAULT_COMP(GET_DOUBLE_VAL(t1->tag + CHAR_BYTES), GET_INT64_VAL(t2->tag + CHAR_BYTES));
}
__compar_fn_t func = getComparFunc(t1->tag[0], 0); __compar_fn_t func = getComparFunc(t1->tag[0], 0);
return func(t1->tag + CHAR_BYTES, t2->tag + CHAR_BYTES); return func(t1->tag + CHAR_BYTES, t2->tag + CHAR_BYTES);
}
} }
__compar_fn_t func = getComparFunc(t1->padding, 0); __compar_fn_t func = getComparFunc(t1->padding, 0);
return func(t1->tag, t2->tag); return func(t1->tag, t2->tag);
......
...@@ -400,7 +400,7 @@ typedef struct SColIndex { ...@@ -400,7 +400,7 @@ typedef struct SColIndex {
int16_t colId; // column id int16_t colId; // column id
int16_t colIndex; // column index in colList if it is a normal column or index in tagColList if a tag int16_t colIndex; // column index in colList if it is a normal column or index in tagColList if a tag
uint16_t flag; // denote if it is a tag or a normal column uint16_t flag; // denote if it is a tag or a normal column
char name[TSDB_COL_NAME_LEN + TSDB_TABLE_NAME_LEN + TSDB_MAX_JSON_KEY_LEN + 4 + 1]; char name[TSDB_COL_NAME_LEN + TSDB_TABLE_NAME_LEN + TSDB_MAX_JSON_KEY_LEN + 4 + 1]; // 4 meams ->'' for json tag
} SColIndex; } SColIndex;
typedef struct SColumnFilterInfo { typedef struct SColumnFilterInfo {
......
...@@ -368,37 +368,12 @@ static int32_t tsCompareFunc(TSKEY k1, TSKEY k2, int32_t order) { ...@@ -368,37 +368,12 @@ static int32_t tsCompareFunc(TSKEY k1, TSKEY k2, int32_t order) {
int32_t columnValueAscendingComparator(char *f1, char *f2, int32_t type, int32_t bytes) { int32_t columnValueAscendingComparator(char *f1, char *f2, int32_t type, int32_t bytes) {
if (type == TSDB_DATA_TYPE_JSON){ if (type == TSDB_DATA_TYPE_JSON){
bool f1IsNull = (*f1 == TSDB_DATA_TYPE_JSON && isNull(f1 + CHAR_BYTES, TSDB_DATA_TYPE_JSON)); bool canReturn = true;
bool f2IsNull = (*f2 == TSDB_DATA_TYPE_JSON && isNull(f2 + CHAR_BYTES, TSDB_DATA_TYPE_JSON)); int32_t result = jsonCompareUnit(f1, f2, &canReturn);
if(f1IsNull && f2IsNull){ if(canReturn) return result;
return 0; type = *f1;
}else if(f1IsNull && !f2IsNull){ f1 += CHAR_BYTES;
return -1; f2 += CHAR_BYTES;
}else if(!f1IsNull && f2IsNull){
return 1;
}else{
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 && !(IS_NUMERIC_TYPE(*f1) && IS_NUMERIC_TYPE(*f2))) {
return 1;
}
if(*f1 == TSDB_DATA_TYPE_BIGINT && *f2 == TSDB_DATA_TYPE_DOUBLE){
DEFAULT_COMP(GET_INT64_VAL(f1 + CHAR_BYTES), GET_DOUBLE_VAL(f2 + CHAR_BYTES));
}else if(*f1 == TSDB_DATA_TYPE_DOUBLE && *f2 == TSDB_DATA_TYPE_BIGINT){
DEFAULT_COMP(GET_DOUBLE_VAL(f1 + CHAR_BYTES), GET_INT64_VAL(f2 + CHAR_BYTES));
}
type = *f1;
f1 += CHAR_BYTES;
f2 += CHAR_BYTES;
}
} }
switch (type) { switch (type) {
case TSDB_DATA_TYPE_INT: DEFAULT_COMP(GET_INT32_VAL(f1), GET_INT32_VAL(f2)); case TSDB_DATA_TYPE_INT: DEFAULT_COMP(GET_INT32_VAL(f1), GET_INT32_VAL(f2));
......
...@@ -90,6 +90,7 @@ int32_t compareFindItemInSet(const void *pLeft, const void* pRight); ...@@ -90,6 +90,7 @@ int32_t compareFindItemInSet(const void *pLeft, const void* pRight);
int32_t compareWStrPatternComp(const void* pLeft, const void* pRight); int32_t compareWStrPatternComp(const void* pLeft, const void* pRight);
int32_t compareStrContainJson(const void* pLeft, const void* pRight); int32_t compareStrContainJson(const void* pLeft, const void* pRight);
int32_t compareJsonVal(const void* pLeft, const void* pRight); int32_t compareJsonVal(const void* pLeft, const void* pRight);
int32_t jsonCompareUnit(const char* f1, const char* f2, bool* canReturn);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -615,38 +615,47 @@ __compar_fn_t getKeyComparFunc(int32_t keyType, int32_t order) { ...@@ -615,38 +615,47 @@ __compar_fn_t getKeyComparFunc(int32_t keyType, int32_t order) {
return comparFn; return comparFn;
} }
int32_t doCompare(const char* f1, const char* f2, int32_t type, size_t size) { int32_t jsonCompareUnit(const char* f1, const char* f2, bool* canReturn){
if (type == TSDB_DATA_TYPE_JSON){ *canReturn = true;
bool f1IsNull = (*f1 == TSDB_DATA_TYPE_JSON && isNull(f1 + CHAR_BYTES, TSDB_DATA_TYPE_JSON)); bool f1IsNull = (*f1 == TSDB_DATA_TYPE_JSON && isNull(f1 + CHAR_BYTES, TSDB_DATA_TYPE_JSON));
bool f2IsNull = (*f2 == TSDB_DATA_TYPE_JSON && isNull(f2 + CHAR_BYTES, TSDB_DATA_TYPE_JSON)); bool f2IsNull = (*f2 == TSDB_DATA_TYPE_JSON && isNull(f2 + CHAR_BYTES, TSDB_DATA_TYPE_JSON));
if(f1IsNull && f2IsNull){ if(f1IsNull && f2IsNull){
return 0;
}else if(f1IsNull && !f2IsNull){
return -1;
}else if(!f1IsNull && f2IsNull){
return 1;
}else{
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; return 0;
}else if(f1IsNull && !f2IsNull){ }else if(f1IsJsonNull && !f2IsJsonNull){
return -1; return -1;
}else if(!f1IsNull && f2IsNull){ }else if(!f1IsJsonNull && f2IsJsonNull) {
return 1; return 1;
}else{
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 && !(IS_NUMERIC_TYPE(*f1) && IS_NUMERIC_TYPE(*f2))) {
return 1;
}
if(*f1 == TSDB_DATA_TYPE_BIGINT && *f2 == TSDB_DATA_TYPE_DOUBLE){
DEFAULT_COMP(GET_INT64_VAL(f1 + CHAR_BYTES), GET_DOUBLE_VAL(f2 + CHAR_BYTES));
}else if(*f1 == TSDB_DATA_TYPE_DOUBLE && *f2 == TSDB_DATA_TYPE_BIGINT){
DEFAULT_COMP(GET_DOUBLE_VAL(f1 + CHAR_BYTES), GET_INT64_VAL(f2 + CHAR_BYTES));
}
type = *f1;
f1 += CHAR_BYTES;
f2 += CHAR_BYTES;
} }
if(*f1 != *f2 && !(IS_NUMERIC_TYPE(*f1) && IS_NUMERIC_TYPE(*f2))) {
return *f1 > *f2 ? 1 : -1;
}
if(*f1 == TSDB_DATA_TYPE_BIGINT && *f2 == TSDB_DATA_TYPE_DOUBLE){
DEFAULT_COMP(GET_INT64_VAL(f1 + CHAR_BYTES), GET_DOUBLE_VAL(f2 + CHAR_BYTES));
}else if(*f1 == TSDB_DATA_TYPE_DOUBLE && *f2 == TSDB_DATA_TYPE_BIGINT){
DEFAULT_COMP(GET_DOUBLE_VAL(f1 + CHAR_BYTES), GET_INT64_VAL(f2 + CHAR_BYTES));
}
*canReturn = false;
return 0; // meaningless
}
}
int32_t doCompare(const char* f1, const char* f2, int32_t type, size_t size) {
if (type == TSDB_DATA_TYPE_JSON){
bool canReturn = true;
int32_t result = jsonCompareUnit(f1, f2, &canReturn);
if(canReturn) return result;
type = *f1;
f1 += CHAR_BYTES;
f2 += CHAR_BYTES;
} }
switch (type) { switch (type) {
case TSDB_DATA_TYPE_INT: DEFAULT_COMP(GET_INT32_VAL(f1), GET_INT32_VAL(f2)); case TSDB_DATA_TYPE_INT: DEFAULT_COMP(GET_INT32_VAL(f1), GET_INT32_VAL(f2));
......
...@@ -319,7 +319,7 @@ class TDTestCase: ...@@ -319,7 +319,7 @@ class TDTestCase:
# test join # test join
tdSql.execute("create table if not exists jsons2(ts timestamp, dataInt int, dataBool bool, dataStr nchar(50), dataStrBin binary(150)) tags(jtag json)") tdSql.execute("create table if not exists jsons2(ts timestamp, dataInt int, dataBool bool, dataStr nchar(50), dataStrBin binary(150)) tags(jtag json)")
tdSql.execute("insert into jsons2_1 using jsons2 tags('{\"tag1\":\"fff\",\"tag2\":5, \"tag3\":true}') values(1591060618000, 2, false, 'json2', '你是2')") tdSql.execute("insert into jsons2_1 using jsons2 tags('{\"tag1\":\"fff\",\"tag2\":5, \"tag3\":true}') values(1591060618000, 2, false, 'json2', '你是2')")
tdSql.execute("insert into jsons2_2 using jsons2 tags('{\"tag1\":5,\"tag2\":\"beijing\"}') values (1591060628000, 2, true, 'json2', 'sss')") tdSql.execute("insert into jsons2_2 using jsons2 tags('{\"tag1\":5,\"tag2\":null}') values (1591060628000, 2, true, 'json2', 'sss')")
tdSql.execute("create table if not exists jsons3(ts timestamp, dataInt int, dataBool bool, dataStr nchar(50), dataStrBin binary(150)) tags(jtag json)") tdSql.execute("create table if not exists jsons3(ts timestamp, dataInt int, dataBool bool, dataStr nchar(50), dataStrBin binary(150)) tags(jtag json)")
tdSql.execute("insert into jsons3_1 using jsons3 tags('{\"tag1\":\"fff\",\"tag2\":5, \"tag3\":true}') values(1591060618000, 3, false, 'json3', '你是3')") tdSql.execute("insert into jsons3_1 using jsons3 tags('{\"tag1\":\"fff\",\"tag2\":5, \"tag3\":true}') values(1591060618000, 3, false, 'json3', '你是3')")
...@@ -335,30 +335,24 @@ class TDTestCase: ...@@ -335,30 +335,24 @@ class TDTestCase:
cname_list.append("a.jtag->'tag3'") cname_list.append("a.jtag->'tag3'")
tdSql.checkColNameList(res, cname_list) tdSql.checkColNameList(res, cname_list)
# test group by & order by string # test group by & order by json tag
#tdSql.query("select avg(dataint),count(*) from jsons1 group by jtag->'tag1' order by jtag->'tag1' desc") tdSql.query("select count(*) from jsons1 group by jtag->'tag1' order by jtag->'tag1' desc")
#tdSql.checkData(1, 0, 2.5) #tdSql.checkData(1, 0, 2.5)
#tdSql.checkData(1, 1, 2) #tdSql.checkData(1, 1, 2)
#tdSql.checkData(1, 2, "\"beijing\"") #tdSql.checkData(1, 2, "\"beijing\"")
#tdSql.checkData(2, 2, None) #tdSql.checkData(2, 2, None)
# test stddev with group by json tag sting # test stddev with group by json tag
#tdSql.query("select stddev(dataint) from jsons1 group by jtag->'tag1'") tdSql.query("select stddev(dataint) from jsons1 group by jtag->'tag1'")
#tdSql.checkData(0, 1, None) #tdSql.checkData(0, 1, None)
#tdSql.checkData(1, 0, 0.5) #tdSql.checkData(1, 0, 0.5)
#tdSql.checkData(2, 0, 0) #tdSql.checkData(2, 0, 0)
#tdSql.query("select stddev(dataint) from jsons1 group by jtag->'tagint'") res = tdSql.getColNameList("select stddev(dataint) from jsons1 group by jsons1.jtag->'tag1'")
#tdSql.checkData(0, 0, 1.16619037896906) cname_list = []
#tdSql.checkData(0, 1, None) cname_list.append("stddev(dataint)")
#tdSql.checkData(1, 0, 0) cname_list.append("jsons1.jtag->'tag1'")
#tdSql.checkData(2, 1, 2) tdSql.checkColNameList(res, cname_list)
#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)
# subquery with json tag # subquery with json tag
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册