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

fix group by json tag format [change json type with qid return logic]

上级 8f0788e9
......@@ -396,6 +396,7 @@ void getJsonTagValueAll(void* data, void* dst, int16_t bytes);
int8_t jsonType2DbType(double data, int jsonType);
void* getJsonTagValue(STable* pTable, char* key, int32_t keyLen, int16_t* colId);
void getJsonKey(SStrToken *t0);
char* cloneCurrentDBName(SSqlObj* pSql);
......
......@@ -1910,14 +1910,26 @@ static void addProjectQueryCol(SQueryInfo* pQueryInfo, int32_t startPos, SColumn
SSchema* pSchema = tscGetTableColumnSchema(pTableMeta, pIndex->columnIndex);
if (pItem->aliasName){
tstrncpy(pExpr->base.aliasName, pItem->aliasName, sizeof(pExpr->base.aliasName));
}else {
if (pSchema->type == TSDB_DATA_TYPE_JSON && pItem->pNode->tokenId == TK_ARROW) {
tstrncpy(pExpr->base.aliasName, pItem->pNode->exprToken.z,
(pItem->pNode->exprToken.n + 1) < sizeof(pExpr->base.aliasName) ? (pItem->pNode->exprToken.n + 1) : sizeof(pExpr->base.aliasName));
} else {
char oriAliasName[TSDB_COL_NAME_LEN + TSDB_TABLE_NAME_LEN + TSDB_MAX_JSON_KEY_LEN + 4 + 1] = {0};
void* oriAliasNamePtr = &oriAliasName;
if (pSchema->type == TSDB_DATA_TYPE_JSON && pItem->pNode->tokenId == TK_ARROW) {
char keyMd5[TSDB_MAX_JSON_KEY_MD5_LEN + 1] = {0};
if (pItem->aliasName){
jsonKeyMd5(pItem->aliasName, strlen(pItem->aliasName), keyMd5);
*(char**)oriAliasNamePtr = pItem->aliasName;
}else{
jsonKeyMd5(pItem->pNode->exprToken.z, pItem->pNode->exprToken.n, keyMd5);
tstrncpy(*(char**)oriAliasNamePtr, pItem->pNode->exprToken.z,
pItem->pNode->exprToken.n + 1 < sizeof(oriAliasName) ? pItem->pNode->exprToken.n + 1 : sizeof(oriAliasName));
}
tstrncpy(pExpr->base.aliasName, keyMd5, sizeof(pExpr->base.aliasName));
} else {
if (pItem->aliasName){
tstrncpy(pExpr->base.aliasName, pItem->aliasName, sizeof(pExpr->base.aliasName));
*(char**)oriAliasNamePtr = pItem->aliasName;
}else{
tstrncpy(pExpr->base.aliasName, pSchema->name, sizeof(pExpr->base.aliasName));
*(char**)oriAliasNamePtr = pSchema->name;
}
}
......@@ -1930,7 +1942,7 @@ static void addProjectQueryCol(SQueryInfo* pQueryInfo, int32_t startPos, SColumn
ids.num = 0;
}
insertResultField(pQueryInfo, startPos, &ids, pExpr->base.resBytes, (int8_t)pExpr->base.resType, pExpr->base.aliasName, pExpr);
insertResultField(pQueryInfo, startPos, &ids, pExpr->base.resBytes, (int8_t)pExpr->base.resType, *(char**)oriAliasNamePtr, pExpr);
}
static void addPrimaryTsColIntoResult(SQueryInfo* pQueryInfo, SSqlCmd* pCmd) {
......@@ -2183,6 +2195,11 @@ int32_t insertResultField(SQueryInfo* pQueryInfo, int32_t outputIndex, SColumnLi
TAOS_FIELD f = tscCreateField(type, fieldName, bytes);
SInternalField* pInfo = tscFieldInfoInsert(&pQueryInfo->fieldsInfo, outputIndex, &f);
if (type == TSDB_DATA_TYPE_JSON){
char keyMd5[TSDB_MAX_JSON_KEY_MD5_LEN + 1] = {0};
jsonKeyMd5(fieldName, strlen(fieldName), keyMd5);
strncpy(pInfo->fieldJson.name, keyMd5, sizeof(pInfo->fieldJson.name));
}
pInfo->pExpr = pSqlExpr;
return TSDB_CODE_SUCCESS;
......@@ -3822,7 +3839,8 @@ int32_t validateGroupbyNode(SQueryInfo* pQueryInfo, SArray* pList, SSqlCmd* pCmd
SColIndex colIndex = { .colIndex = relIndex, .flag = TSDB_COL_TAG, .colId = pSchema->colId, };
if(pItem->isJsonExp) {
tstrncpy(colIndex.name, pItem->jsonExp->pRight->value.pz, tListLen(colIndex.name));
tstrncpy(colIndex.name, pItem->jsonExp->exprToken.z,
pItem->jsonExp->exprToken.n + 1 > tListLen(colIndex.name) ? tListLen(colIndex.name) : pItem->jsonExp->exprToken.n + 1);
}else{
tstrncpy(colIndex.name, pSchema->name, tListLen(colIndex.name));
}
......@@ -7281,7 +7299,7 @@ static bool tagColumnInGroupby(SGroupbyExpr* pGroupbyExpr, int16_t columnId, int
for (int32_t j = 0; j < pGroupbyExpr->numOfGroupCols; ++j) {
SColIndex* pColIndex = taosArrayGet(pGroupbyExpr->columnInfo, j);
if (type == TSDB_DATA_TYPE_JSON){
if (type == TSDB_DATA_TYPE_JSON && name != NULL){
if (columnId == pColIndex->colId && strncmp(pColIndex->name, name, tListLen(pColIndex->name)) == 0 && TSDB_COL_IS_TAG(pColIndex->flag )) {
return true;
}
......@@ -7502,20 +7520,24 @@ static int32_t doAddGroupbyColumnsOnDemand(SSqlCmd* pCmd, SQueryInfo* pQueryInfo
SExprInfo* pExpr = tscExprInsert(pQueryInfo, pos, f, &index, s->type, s->bytes, getNewResColId(pCmd), s->bytes, true);
// if json->key is more than TSDB_COL_NAME_LEN + TSDB_DB_NAME_LEN, truncature it, maybe case error, can encode name by md5.
if(s->type == TSDB_DATA_TYPE_JSON){
tVariantCreateFromBinary(&(pExpr->base.param[pExpr->base.numOfParams]), pColIndex->name,
strlen(pColIndex->name), TSDB_DATA_TYPE_BINARY);
SStrToken t0 = {.z = pColIndex->name};
getJsonKey(&t0);
tVariantCreateFromBinary(&(pExpr->base.param[pExpr->base.numOfParams]), t0.z,
t0.n, TSDB_DATA_TYPE_BINARY);
pExpr->base.numOfParams++;
}
char keyMd5[TSDB_MAX_JSON_KEY_MD5_LEN + 1] = {0};
jsonKeyMd5(pColIndex->name, strlen(pColIndex->name), keyMd5);
memset(pExpr->base.aliasName, 0, sizeof(pExpr->base.aliasName));
tstrncpy(pExpr->base.aliasName, pColIndex->name, sizeof(pExpr->base.aliasName));
tstrncpy(pExpr->base.token, pColIndex->name, sizeof(pExpr->base.aliasName));
tstrncpy(pExpr->base.aliasName, keyMd5, sizeof(pExpr->base.aliasName));
tstrncpy(pExpr->base.token, keyMd5, sizeof(pExpr->base.token));
pExpr->base.colInfo.flag = TSDB_COL_TAG;
// NOTE: tag column does not add to source column list
SColumnList ids = createColumnList(1, 0, pColIndex->colIndex);
insertResultField(pQueryInfo, pos, &ids, s->bytes, (int8_t)s->type, s->name, pExpr);
insertResultField(pQueryInfo, pos, &ids, s->bytes, (int8_t)s->type, pColIndex->name, pExpr);
} else {
// if this query is "group by" normal column, time window query is not allowed
if (isTimeWindowQuery(pQueryInfo)) {
......
......@@ -2657,7 +2657,7 @@ static void updateFieldForJson(SSqlObj *pSql, SQueryTableRsp *pQueryAttr){
if (pField->field.type == TSDB_DATA_TYPE_JSON) {
pField->fieldJson.type = TSDB_DATA_TYPE_JSON;
for (int k = 0; k < pQueryAttr->tJsonSchLen; ++k) {
if (strncmp(pField->field.name, pQueryAttr->tagJsonSchema[k].name, TSDB_MAX_JSON_KEY_LEN) == 0) {
if (strncmp(pField->fieldJson.name, pQueryAttr->tagJsonSchema[k].name, TSDB_MAX_JSON_KEY_LEN) == 0) {
pField->fieldJson.type = pQueryAttr->tagJsonSchema[k].type;
pField->fieldJson.bytes = TYPE_BYTES[pField->field.type];
tscDebug("0x%" PRIx64 " change json type %s:%s to %d", pSql->self, pField->field.name, pQueryAttr->tagJsonSchema[k].name,
......
......@@ -5681,3 +5681,17 @@ void* getJsonTagValue(STable* pTable, char* key, int32_t keyLen, int16_t* retCol
}
return NULL;
}
void getJsonKey(SStrToken *t0){
while(true){
t0->n = tGetToken(t0->z, &t0->type);
if (t0->type == TK_STRING){
t0->z++;
t0->n -= 2;
break;
}else if (t0->type == TK_ILLEGAL){
assert(0);
}
t0->z += t0->n;
}
}
......@@ -16,6 +16,6 @@ IF (HEADER_GTEST_INCLUDE_DIR AND (LIB_GTEST_STATIC_DIR OR LIB_GTEST_SHARED_DIR))
AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} SOURCE_LIST)
ADD_EXECUTABLE(cliTest ${SOURCE_LIST})
ADD_EXECUTABLE(cliTest ${SOURCE_LIST} jsonTagTest.cpp)
TARGET_LINK_LIBRARIES(cliTest taos cJson tutil common gtest pthread)
ENDIF()
//
// Created by mingming wanng on 2021/11/24.
//
#include <gtest/gtest.h>
#include <inttypes.h>
#include "tscUtil.h"
/* test set config function */
TEST(testCase, json_test1) {
SStrToken t0 = {.z = "jtag->'location'"};
getJsonKey(&t0);
ASSERT_EQ(t0.n, 8);
t0.z[8] = 0;
ASSERT_STREQ(t0.z, "location");
}
\ No newline at end of file
......@@ -405,7 +405,7 @@ typedef struct SColIndex {
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
uint16_t flag; // denote if it is a tag or a normal column
char name[TSDB_COL_NAME_LEN + TSDB_TABLE_NAME_LEN + 1];
char name[TSDB_COL_NAME_LEN + TSDB_TABLE_NAME_LEN + TSDB_MAX_JSON_KEY_LEN + 4 + 1];
} SColIndex;
typedef struct SColumnFilterInfo {
......
......@@ -194,7 +194,7 @@ class TDTestCase:
tdSql.checkData(0, 0, "{\"k1\":\"\",\"k2\":true,\"k3\":false,\"k4\":55}")
tdSql.query("select jtag->'k2' from db_json_tag_test.jsons1_10")
tdSql.checkData(0, 0, "true")
tdSql.checkData(0, 0, True)
tdSql.query("select jtag from db_json_tag_test.jsons1 where jtag->'k1'=''")
tdSql.checkRows(1)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册