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

TD-6129<feature> support null true false for json tag

上级 cd8b081b
...@@ -4433,11 +4433,11 @@ static int32_t validateJsonTagExpr(tSqlExpr* pExpr, char* msgBuf) { ...@@ -4433,11 +4433,11 @@ static int32_t validateJsonTagExpr(tSqlExpr* pExpr, char* msgBuf) {
if (pRight != NULL && pRight->value.nLen >= TSDB_COL_NAME_LEN) if (pRight != NULL && pRight->value.nLen >= TSDB_COL_NAME_LEN)
return invalidOperationMsg(msgBuf, msg2); return invalidOperationMsg(msgBuf, msg2);
} else { } else {
if (pLeft != NULL && (pLeft->tokenId == TK_ID)) { if (pLeft != NULL && pLeft->tokenId == TK_ID && pExpr->tokenId != TK_NULL) {
return invalidOperationMsg(msgBuf, msg1); return invalidOperationMsg(msgBuf, msg1);
} }
if (pLeft != NULL && (pLeft->tokenId == TK_ARROW)) { if (pLeft != NULL && pLeft->tokenId == TK_ARROW) {
if (pLeft->pRight && !IS_VAR_DATA_TYPE(pLeft->pRight->value.nType)) if (pLeft->pRight && !IS_VAR_DATA_TYPE(pLeft->pRight->value.nType))
return invalidOperationMsg(msgBuf, msg3); return invalidOperationMsg(msgBuf, msg3);
if (pLeft->pRight && pLeft->pRight->value.nLen >= TSDB_COL_NAME_LEN) if (pLeft->pRight && pLeft->pRight->value.nLen >= TSDB_COL_NAME_LEN)
......
...@@ -5198,8 +5198,6 @@ void findTagValue(STable* data, char* key, int32_t keyLen, char** out, int* len) ...@@ -5198,8 +5198,6 @@ void findTagValue(STable* data, char* key, int32_t keyLen, char** out, int* len)
*len = LONG_BYTES + CHAR_BYTES; *len = LONG_BYTES + CHAR_BYTES;
}else if (*(char*)result == TSDB_DATA_TYPE_BOOL) { }else if (*(char*)result == TSDB_DATA_TYPE_BOOL) {
*len = CHAR_BYTES + CHAR_BYTES; *len = CHAR_BYTES + CHAR_BYTES;
}else if (*(char*)result == TSDB_DATA_TYPE_NULL) {
*len = CHAR_BYTES + CHAR_BYTES;
}else { }else {
tscError("unsupportted json value"); tscError("unsupportted json value");
return; return;
...@@ -5228,9 +5226,8 @@ void parseTagValue2Dst(char* result, char* dst){ ...@@ -5228,9 +5226,8 @@ void parseTagValue2Dst(char* result, char* dst){
sprintf(varDataVal(dst), "%" PRId64, jsonVd); sprintf(varDataVal(dst), "%" PRId64, jsonVd);
varDataSetLen(dst, strlen(varDataVal(dst))); varDataSetLen(dst, strlen(varDataVal(dst)));
}else if (*(char*)result == TSDB_DATA_TYPE_BOOL) { }else if (*(char*)result == TSDB_DATA_TYPE_BOOL) {
sprintf(varDataVal(dst), "%s", (*((char *)realData) == 1) ? "true" : "false");
}else if (*(char*)result == TSDB_DATA_TYPE_NULL) { varDataSetLen(dst, strlen(varDataVal(dst)));
} }
} }
...@@ -5243,20 +5240,21 @@ char* parseTagDatatoJson(void *p){ ...@@ -5243,20 +5240,21 @@ char* parseTagDatatoJson(void *p){
} }
int16_t nCols = kvRowNCols(p); int16_t nCols = kvRowNCols(p);
ASSERT(nCols%2 == 0); ASSERT(nCols%2 == 1);
char tagJsonKey[TSDB_MAX_JSON_KEY_LEN + 1] = {0}; char tagJsonKey[TSDB_MAX_JSON_KEY_LEN + 1] = {0};
for (int j = 0; j < nCols; ++j) { for (int j = 0; j < nCols; ++j) {
SColIdx * pColIdx = kvRowColIdxAt(p, j); SColIdx * pColIdx = kvRowColIdxAt(p, j);
void* val = (kvRowColVal(p, pColIdx)); void* val = (kvRowColVal(p, pColIdx));
if (j == 0){ if (j == 0){
int8_t jsonVal = *(int8_t*)val; int8_t jsonPlaceHolder = *(int8_t*)val;
ASSERT(jsonVal == TSDB_DATA_JSON_PLACEHOLDER); ASSERT(jsonPlaceHolder == TSDB_DATA_JSON_PLACEHOLDER);
continue; continue;
} }else if(j == 1){
if (j == 1 && *(uint8_t*)val == TSDB_DATA_JSON_NULL){ uint8_t jsonNULL = *(uint8_t*)(varDataVal(val));
ASSERT(jsonNULL == TSDB_DATA_JSON_NULL);
}else if (j == 2 && *(uint8_t*)val == TSDB_DATA_JSON_NULL){
goto end; goto end;
} }else if (j%2 == 1) { // json key encode by binary
if (j%2 == 0) { // json key encode by binary
ASSERT(varDataLen(val) <= TSDB_MAX_JSON_KEY_LEN); ASSERT(varDataLen(val) <= TSDB_MAX_JSON_KEY_LEN);
memset(tagJsonKey, 0, sizeof(tagJsonKey)); memset(tagJsonKey, 0, sizeof(tagJsonKey));
memcpy(tagJsonKey, varDataVal(val), varDataLen(val)); memcpy(tagJsonKey, varDataVal(val), varDataLen(val));
...@@ -5307,14 +5305,18 @@ end: ...@@ -5307,14 +5305,18 @@ end:
} }
int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, int16_t startColId){ int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, int16_t startColId){
uint8_t jsonNULL = TSDB_DATA_JSON_NULL;
int jsonIndex = startColId + 1;
char nullTypeKey[VARSTR_HEADER_SIZE + CHAR_BYTES] = {0};
varDataSetLen(nullTypeKey, CHAR_BYTES);
*(uint8_t*)(varDataVal(nullTypeKey)) = jsonNULL;
tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_JSON, nullTypeKey, false); // add json null type
if (strtrim(json) == 0 || strcasecmp(json, "null") == 0){ if (strtrim(json) == 0 || strcasecmp(json, "null") == 0){
uint8_t typeVal = TSDB_DATA_JSON_NULL; tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_JSON, &jsonNULL, false); // add json null value
tdAddColToKVRow(kvRowBuilder, startColId++, TSDB_DATA_TYPE_JSON, &typeVal, false); // add json type
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
int jsonIndex = ++startColId; int8_t jsonNotNull = TSDB_DATA_JSON_NOT_NULL;
int8_t typeVal = TSDB_DATA_JSON_OBJECT; tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_JSON, &jsonNotNull, false); // add json type
tdAddColToKVRow(kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_JSON, &typeVal, false); // add json type
cJSON *root = cJSON_Parse(json); cJSON *root = cJSON_Parse(json);
if (root == NULL){ if (root == NULL){
...@@ -5399,9 +5401,8 @@ int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, in ...@@ -5399,9 +5401,8 @@ int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, in
} }
} }
if(taosHashGetSize(keyHash) == 0){ if(taosHashGetSize(keyHash) == 0){ // set json NULL true
typeVal = TSDB_DATA_JSON_NULL; memcpy(POINTER_SHIFT(kvRowBuilder->buf, sizeof(nullTypeKey)), &jsonNULL, CHAR_BYTES);
memcpy(kvRowBuilder->buf, &typeVal, CHAR_BYTES);
} }
end: end:
......
...@@ -510,11 +510,15 @@ void setNullN(void *val, int32_t type, int32_t bytes, int32_t numOfElems) { ...@@ -510,11 +510,15 @@ void setNullN(void *val, int32_t type, int32_t bytes, int32_t numOfElems) {
break; break;
case TSDB_DATA_TYPE_NCHAR: case TSDB_DATA_TYPE_NCHAR:
case TSDB_DATA_TYPE_BINARY: case TSDB_DATA_TYPE_BINARY:
case TSDB_DATA_TYPE_JSON:
for (int32_t i = 0; i < numOfElems; ++i) { for (int32_t i = 0; i < numOfElems; ++i) {
setVardataNull(POINTER_SHIFT(val, i * bytes), type); setVardataNull(POINTER_SHIFT(val, i * bytes), type);
} }
break; break;
case TSDB_DATA_TYPE_JSON:
for (int32_t i = 0; i < numOfElems; ++i) {
*(uint8_t *)(POINTER_SHIFT(val, i * tDataTypes[type].bytes)) = TSDB_DATA_JSON_NULL;
}
break;
default: { default: {
for (int32_t i = 0; i < numOfElems; ++i) { for (int32_t i = 0; i < numOfElems; ++i) {
*(uint32_t *)(POINTER_SHIFT(val, i * tDataTypes[TSDB_DATA_TYPE_INT].bytes)) = TSDB_DATA_INT_NULL; *(uint32_t *)(POINTER_SHIFT(val, i * tDataTypes[TSDB_DATA_TYPE_INT].bytes)) = TSDB_DATA_INT_NULL;
......
...@@ -73,7 +73,7 @@ extern const int32_t TYPE_BYTES[16]; ...@@ -73,7 +73,7 @@ extern const int32_t TYPE_BYTES[16];
#define TSDB_DATA_BINARY_NULL 0xFF #define TSDB_DATA_BINARY_NULL 0xFF
#define TSDB_DATA_JSON_PLACEHOLDER 0x7F #define TSDB_DATA_JSON_PLACEHOLDER 0x7F
#define TSDB_DATA_JSON_NULL 0xFF #define TSDB_DATA_JSON_NULL 0xFF
#define TSDB_DATA_JSON_OBJECT 0x01 #define TSDB_DATA_JSON_NOT_NULL 0x01
#define TSDB_DATA_UTINYINT_NULL 0xFF #define TSDB_DATA_UTINYINT_NULL 0xFF
#define TSDB_DATA_USMALLINT_NULL 0xFFFF #define TSDB_DATA_USMALLINT_NULL 0xFFFF
......
...@@ -160,7 +160,7 @@ static FORCE_INLINE bool isNull(const void *val, int32_t type) { ...@@ -160,7 +160,7 @@ static FORCE_INLINE bool isNull(const void *val, int32_t type) {
case TSDB_DATA_TYPE_DOUBLE: case TSDB_DATA_TYPE_DOUBLE:
return *(uint64_t *)val == TSDB_DATA_DOUBLE_NULL; return *(uint64_t *)val == TSDB_DATA_DOUBLE_NULL;
case TSDB_DATA_TYPE_JSON: case TSDB_DATA_TYPE_JSON:
return varDataLen(val) == sizeof(int8_t) && *(uint8_t *) varDataVal(val) == TSDB_DATA_JSON_NULL; return *(uint8_t *)val == TSDB_DATA_JSON_NULL;
case TSDB_DATA_TYPE_NCHAR: case TSDB_DATA_TYPE_NCHAR:
return varDataLen(val) == sizeof(int32_t) && *(uint32_t*) varDataVal(val) == TSDB_DATA_NCHAR_NULL; return varDataLen(val) == sizeof(int32_t) && *(uint32_t*) varDataVal(val) == TSDB_DATA_NCHAR_NULL;
case TSDB_DATA_TYPE_BINARY: case TSDB_DATA_TYPE_BINARY:
......
...@@ -1100,22 +1100,26 @@ static int tsdbAddTableIntoIndex(STsdbMeta *pMeta, STable *pTable, bool refSuper ...@@ -1100,22 +1100,26 @@ static int tsdbAddTableIntoIndex(STsdbMeta *pMeta, STable *pTable, bool refSuper
if(pSTable->tagSchema->columns[0].type == TSDB_DATA_TYPE_JSON){ if(pSTable->tagSchema->columns[0].type == TSDB_DATA_TYPE_JSON){
ASSERT(pSTable->tagSchema->numOfCols == 1); ASSERT(pSTable->tagSchema->numOfCols == 1);
int16_t nCols = kvRowNCols(pTable->tagVal); int16_t nCols = kvRowNCols(pTable->tagVal);
ASSERT(nCols%2 == 0); ASSERT(nCols%2 == 1);
void* jsonTypeKey = NULL;
for (int j = 0; j < nCols; ++j) { for (int j = 0; j < nCols; ++j) {
if (j != 0 && j != 1 && j%2 != 0) continue; // jump value if (j != 0 && j != 2 && j%2 == 0) continue; // jump value
SColIdx * pColIdx = kvRowColIdxAt(pTable->tagVal, j); SColIdx * pColIdx = kvRowColIdxAt(pTable->tagVal, j);
void* val = (kvRowColVal(pTable->tagVal, pColIdx)); void* val = (kvRowColVal(pTable->tagVal, pColIdx));
if (j == 0){ // json value is the first if (j == 0){ // json value is the first
int8_t jsonVal = *(int8_t*)val; int8_t jsonPlaceHolder = *(int8_t*)val;
ASSERT(jsonVal == TSDB_DATA_JSON_PLACEHOLDER); ASSERT(jsonPlaceHolder == TSDB_DATA_JSON_PLACEHOLDER);
continue; continue;
} }
if (j == 1){
char nullData[VARSTR_HEADER_SIZE + CHAR_BYTES] = {0}; uint8_t jsonNULL = *(uint8_t*)(varDataVal(val));
if( j == 1 && *(uint8_t*)val == TSDB_DATA_JSON_NULL){ // for null json data, add index to jsonKeyMap ASSERT(jsonNULL == TSDB_DATA_JSON_NULL);
varDataSetLen(nullData, CHAR_BYTES); jsonTypeKey = val;
*(uint8_t*)(varDataVal(nullData)) = TSDB_DATA_JSON_NULL; continue;
val = nullData; }
if (j == 2){
if(*(uint8_t*)val == TSDB_DATA_JSON_NOT_NULL) continue;
else val = jsonTypeKey;
} }
char keyMd5[TSDB_MAX_JSON_KEY_MD5_LEN] = {0}; char keyMd5[TSDB_MAX_JSON_KEY_MD5_LEN] = {0};
...@@ -1180,16 +1184,27 @@ static int tsdbRemoveTableFromIndex(STsdbMeta *pMeta, STable *pTable) { ...@@ -1180,16 +1184,27 @@ static int tsdbRemoveTableFromIndex(STsdbMeta *pMeta, STable *pTable) {
if(pSTable->tagSchema->columns[0].type == TSDB_DATA_TYPE_JSON){ if(pSTable->tagSchema->columns[0].type == TSDB_DATA_TYPE_JSON){
ASSERT(pSTable->tagSchema->numOfCols == 1); ASSERT(pSTable->tagSchema->numOfCols == 1);
int16_t nCols = kvRowNCols(pTable->tagVal); int16_t nCols = kvRowNCols(pTable->tagVal);
ASSERT(nCols%2 == 0); ASSERT(nCols%2 == 1);
void* jsonTypeKey = NULL;
for (int j = 0; j < nCols; ++j) { for (int j = 0; j < nCols; ++j) {
if (j != 0 && j%2 != 0) continue; // jump value if (j != 0 && j != 2 && j%2 == 0) continue; // jump value
SColIdx * pColIdx = kvRowColIdxAt(pTable->tagVal, j); SColIdx * pColIdx = kvRowColIdxAt(pTable->tagVal, j);
void* val = (kvRowColVal(pTable->tagVal, pColIdx)); void* val = (kvRowColVal(pTable->tagVal, pColIdx));
if (j == 0){ // json value is the first if (j == 0){ // json value is the first
int8_t jsonVal = *(int8_t*)val; int8_t jsonPlaceHolder = *(int8_t*)val;
ASSERT(jsonVal == TSDB_DATA_JSON_PLACEHOLDER); ASSERT(jsonPlaceHolder == TSDB_DATA_JSON_PLACEHOLDER);
continue; continue;
} }
if (j == 1){
uint8_t jsonNULL = *(uint8_t*)(varDataVal(val));
ASSERT(jsonNULL == TSDB_DATA_JSON_NULL);
jsonTypeKey = val;
continue;
}
if (j == 2){
if(*(uint8_t*)val == TSDB_DATA_JSON_NOT_NULL) continue;
else val = jsonTypeKey;
}
SArray** tablist = (SArray **)taosHashGet(pSTable->jsonKeyMap, varDataVal(val) ,varDataLen(val)); SArray** tablist = (SArray **)taosHashGet(pSTable->jsonKeyMap, varDataVal(val) ,varDataLen(val));
if(tablist == NULL) { if(tablist == NULL) {
......
...@@ -4087,13 +4087,7 @@ static FORCE_INLINE int32_t tsdbGetJsonTagDataFromId(void *param, int32_t id, ch ...@@ -4087,13 +4087,7 @@ static FORCE_INLINE int32_t tsdbGetJsonTagDataFromId(void *param, int32_t id, ch
if (id == TSDB_TBNAME_COLUMN_INDEX) { if (id == TSDB_TBNAME_COLUMN_INDEX) {
*data = TABLE_NAME(pTable); *data = TABLE_NAME(pTable);
} else { } else {
int16_t colId = 0; void* jsonData = getJsonTagValue(pTable, name, TSDB_MAX_JSON_KEY_MD5_LEN, NULL);
void* jsonData = getJsonTagValue(pTable, name, TSDB_MAX_JSON_KEY_MD5_LEN, &colId);
if(colId == id + 1){ // if find json tag is NULL
if(jsonData != NULL) *data = NULL;
else *data = pTable;
return TSDB_CODE_SUCCESS;
}
// jsonData == NULL for ? operation // jsonData == NULL for ? operation
if(jsonData != NULL) jsonData += CHAR_BYTES; // jump type if(jsonData != NULL) jsonData += CHAR_BYTES; // jump type
*data = jsonData; *data = jsonData;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册