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

TD-6129<feature> add tag-> where logic

上级 36e4677e
...@@ -378,10 +378,10 @@ char* cloneCurrentDBName(SSqlObj* pSql); ...@@ -378,10 +378,10 @@ char* cloneCurrentDBName(SSqlObj* pSql);
int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, int16_t startColId); int parseJsontoTagData(char* json, SKVRowBuilder* kvRowBuilder, char* errMsg, int16_t startColId);
char* parseTagDatatoJson(void *p); char* parseTagDatatoJson(void *p);
void findTagValue(void* data, char* key, int32_t keyLen, char* out, int16_t len); void findTagValue(STable* data, char* key, int32_t keyLen, char* out, int16_t len);
int8_t jsonType2DbType(double data, int jsonType); int8_t jsonType2DbType(double data, int jsonType);
void* getJsonTagValue(STable* pTable, char* key); void* getJsonTagValue(STable* pTable, char* key, int32_t keyLen);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -725,23 +725,19 @@ static void setResRawPtrImpl(SSqlRes* pRes, SInternalField* pInfo, int32_t i, bo ...@@ -725,23 +725,19 @@ static void setResRawPtrImpl(SSqlRes* pRes, SInternalField* pInfo, int32_t i, bo
char* p = pRes->urow[i]; char* p = pRes->urow[i];
for (int32_t k = 0; k < pRes->numOfRows; ++k) { for (int32_t k = 0; k < pRes->numOfRows; ++k) {
char* dst = pRes->buffer[i] + k * pInfo->field.bytes; char* dst = pRes->buffer[i] + k * pInfo->field.bytes;
char* realData = p + CHAR_BYTES;
if (isNull(p, TSDB_DATA_TYPE_NCHAR)) {
memcpy(dst, p, varDataTLen(p));
} else {
if (*p == SELECT_ALL_JSON_TAG){ if (*p == SELECT_ALL_JSON_TAG){
char* json = parseTagDatatoJson(p+1); char* json = parseTagDatatoJson(realData);
if(json) { if(json) {
memcpy(varDataVal(dst), json, strlen(json)); memcpy(varDataVal(dst), json, strlen(json));
varDataSetLen(dst, strlen(json)); varDataSetLen(dst, strlen(json));
tfree(json); tfree(json);
} }
}else if (*p == SELECT_ELEMENT_JSON_TAG){ }else if (*p == SELECT_ELEMENT_JSON_TAG){
memcpy(dst, p+1, varDataTLen(p+1)); memcpy(dst, realData, varDataTLen(realData));
}else{ }else{
tscError("construct json error"); tscError("construct json error");
} }
}
p += pInfo->field.bytes; p += pInfo->field.bytes;
} }
...@@ -5178,62 +5174,42 @@ char* cloneCurrentDBName(SSqlObj* pSql) { ...@@ -5178,62 +5174,42 @@ char* cloneCurrentDBName(SSqlObj* pSql) {
return p; return p;
} }
void findTagValue(void* data, char* key, int32_t keyLen, char* out, int16_t len){ void findTagValue(STable* data, char* key, int32_t keyLen, char* out, int16_t len){
int16_t nCols = kvRowNCols(data); void* result = getJsonTagValue(data, key, keyLen);
if (result == NULL){ // json key no result
bool found = false; return;
for (int k = 1; k < nCols; ++k) {
SColIdx* pColIdx = kvRowColIdxAt(data, k);
void* result = kvRowColVal(data, pColIdx);
if (k % 2 != 0) { // json key
if (JSON_TYPE_BINARY){
if (keyLen != varDataLen(result)) continue;
if (memcmp(varDataVal(result), key, keyLen) != 0) continue;
} else if(JSON_TYPE_NCHAR){
char tagJsonKey[TSDB_MAX_TAGS_LEN] = {0};
int32_t length = taosUcs4ToMbs(varDataVal(result), varDataLen(result), tagJsonKey);
if (length == 0) {
tscError("charset:%s to %s. val:%s convert json key failed.", DEFAULT_UNICODE_ENCODEC, tsCharset,
(char*)result);
continue;
}
if (keyLen != length) continue;
if (strncmp(key, tagJsonKey, keyLen) != 0) continue;
} }
found = true;
} else { // json value
if (!found) continue;
char* realData = POINTER_SHIFT(result, CHAR_BYTES); char* realData = POINTER_SHIFT(result, CHAR_BYTES);
if (*(char*)result == cJSON_String) {
if (JSON_TYPE_BINARY){ if (*(char*)result == TSDB_DATA_TYPE_BINARY){
assert(varDataLen(realData) <= len); assert(varDataLen(realData) <= len);
memcpy(varDataVal(out), varDataVal(realData), varDataLen(realData)); memcpy(varDataVal(out), varDataVal(realData), varDataLen(realData));
varDataSetLen(out, varDataLen(realData)); varDataSetLen(out, varDataLen(realData));
} else if(JSON_TYPE_NCHAR) { } else if(*(char*)result == TSDB_DATA_TYPE_NCHAR) {
char tagJsonValue[TSDB_MAX_TAGS_LEN] = {0}; char tagJsonValue[TSDB_MAX_TAGS_LEN] = {0};
int32_t length = taosUcs4ToMbs(varDataVal(realData), int32_t length = taosUcs4ToMbs(varDataVal(realData),
varDataLen(realData), tagJsonValue); varDataLen(realData), tagJsonValue);
if (length == 0) { if (length == 0) {
tscError("charset:%s to %s. val:%s convert json value failed.", DEFAULT_UNICODE_ENCODEC, tsCharset, tscError("charset:%s to %s. val:%s convert json value failed.", DEFAULT_UNICODE_ENCODEC, tsCharset,
(char*)result); (char*)result);
} else { return;
}
assert(length <= len); assert(length <= len);
varDataSetLen(out, length); varDataSetLen(out, length);
memcpy(varDataVal(out), tagJsonValue, length); memcpy(varDataVal(out), tagJsonValue, length);
} }else if (*(char*)result == TSDB_DATA_TYPE_DOUBLE) {
}
} else if (*(char*)result == cJSON_Number) {
double jsonVd = *(double*)(realData); double jsonVd = *(double*)(realData);
sprintf(varDataVal(out), "%.9lf", jsonVd); sprintf(varDataVal(out), "%.9lf", jsonVd);
assert(strlen(varDataVal(out)) <= len); assert(strlen(varDataVal(out)) <= len);
varDataSetLen(out, strlen(varDataVal(out))); varDataSetLen(out, strlen(varDataVal(out)));
} else { }else if (*(char*)result == TSDB_DATA_TYPE_BIGINT) {
tscError("unsupportted json value"); int64_t jsonVd = *(int64_t*)(realData);
} sprintf(varDataVal(out), "%" PRId64, jsonVd);
break; assert(strlen(varDataVal(out)) <= len);
varDataSetLen(out, strlen(varDataVal(out)));
} }
else {
tscError("unsupportted json value");
} }
} }
...@@ -5407,17 +5383,17 @@ int8_t jsonType2DbType(double data, int jsonType){ ...@@ -5407,17 +5383,17 @@ int8_t jsonType2DbType(double data, int jsonType){
return TSDB_DATA_TYPE_NULL; return TSDB_DATA_TYPE_NULL;
} }
void* getJsonTagValue(STable* pTable, char* key){ void* getJsonTagValue(STable* pTable, char* key, int32_t keyLen){
int32_t outLen = 0; int32_t outLen = 0;
if(JSON_TYPE_NCHAR){ if(JSON_TYPE_NCHAR){
char tagKey[256] = {0}; char tagKey[256] = {0};
if (!taosMbsToUcs4(key, strlen(key), tagKey, 256, &outLen)) { if (!taosMbsToUcs4(key, keyLen, tagKey, 256, &outLen)) {
tscError("json key to ucs4 error:%s|%s", strerror(errno), key); tscError("json key to ucs4 error:%s|%s", strerror(errno), key);
return NULL; return NULL;
} }
key = tagKey; key = tagKey;
}else{ }else{
outLen = strlen(key); outLen = keyLen;
} }
if(TABLE_TYPE(pTable) == TSDB_CHILD_TABLE){ if(TABLE_TYPE(pTable) == TSDB_CHILD_TABLE){
STable* superTable= pTable->pSuper; STable* superTable= pTable->pSuper;
......
...@@ -7186,23 +7186,24 @@ static SSDataBlock* doTagScan(void* param, bool* newgroup) { ...@@ -7186,23 +7186,24 @@ static SSDataBlock* doTagScan(void* param, bool* newgroup) {
if (pExprInfo[j].base.colInfo.colId == TSDB_TBNAME_COLUMN_INDEX) { if (pExprInfo[j].base.colInfo.colId == TSDB_TBNAME_COLUMN_INDEX) {
data = tsdbGetTableName(item->pTable); data = tsdbGetTableName(item->pTable);
} else { } else {
data = tsdbGetTableTagVal(item->pTable, pExprInfo[j].base.colInfo.colId, type, bytes);
if(type == TSDB_DATA_TYPE_JSON){ if(type == TSDB_DATA_TYPE_JSON){
if(pExprInfo[j].base.numOfParams > 0){ // tag-> operation if(pExprInfo[j].base.numOfParams > 0){ // tag-> operation
tagJsonElementData = calloc(bytes, 1); tagJsonElementData = calloc(bytes, 1);
findTagValue(data, pExprInfo[j].base.param[0].pz, pExprInfo[j].base.param[0].nLen, tagJsonElementData, bytes); findTagValue(item->pTable, pExprInfo[j].base.param[0].pz, pExprInfo[j].base.param[0].nLen, tagJsonElementData, bytes);
*dst = SELECT_ELEMENT_JSON_TAG; // select tag->element *dst = SELECT_ELEMENT_JSON_TAG; // select tag->element
dst++; dst++;
assert(varDataTLen(tagJsonElementData) < bytes); assert(varDataTLen(tagJsonElementData) < bytes);
doSetTagValueToResultBuf(dst, tagJsonElementData, type, bytes - 1); doSetTagValueToResultBuf(dst, tagJsonElementData, type, bytes - CHAR_BYTES);
tfree(tagJsonElementData); tfree(tagJsonElementData);
}else{ }else{
*dst = SELECT_ALL_JSON_TAG; // select tag *dst = SELECT_ALL_JSON_TAG; // select tag
dst++; dst++;
assert(kvRowLen(data) < bytes); assert(kvRowLen(data) < bytes);
doSetTagValueToResultBuf(dst, data, type, bytes - 1); doSetTagValueToResultBuf(dst, data, type, bytes - CHAR_BYTES);
} }
continue; continue;
}else{
data = tsdbGetTableTagVal(item->pTable, pExprInfo[j].base.colInfo.colId, type, bytes);
} }
} }
doSetTagValueToResultBuf(dst, data, type, bytes); doSetTagValueToResultBuf(dst, data, type, bytes);
......
...@@ -3180,7 +3180,7 @@ int filterJsonTypeConvert(SFilterInfo* info) { ...@@ -3180,7 +3180,7 @@ int filterJsonTypeConvert(SFilterInfo* info) {
SSchema* schema = info->fields[FLD_TYPE_COLUMN].fields[i].desc; SSchema* schema = info->fields[FLD_TYPE_COLUMN].fields[i].desc;
if(schema->type == TSDB_DATA_TYPE_JSON){ if(schema->type == TSDB_DATA_TYPE_JSON){
void* data = getJsonTagValue(info->pTable, schema->name); void* data = getJsonTagValue(info->pTable, schema->name, strlen(schema->name));
if(data == NULL) return TSDB_CODE_QRY_JSON_KEY_NOT_EXIST; if(data == NULL) return TSDB_CODE_QRY_JSON_KEY_NOT_EXIST;
int8_t type = *(char*)data; int8_t type = *(char*)data;
assert(type > TSDB_DATA_TYPE_NULL && type < TSDB_DATA_TYPE_JSON); assert(type > TSDB_DATA_TYPE_NULL && type < TSDB_DATA_TYPE_JSON);
......
...@@ -4075,7 +4075,7 @@ static FORCE_INLINE int32_t tsdbGetJsonTagDataFromId(void *param, int32_t id, ch ...@@ -4075,7 +4075,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 {
void* jsonData = getJsonTagValue(pTable, name); void* jsonData = getJsonTagValue(pTable, name, strlen(name));
if (jsonData != NULL) jsonData += CHAR_BYTES; // jump type if (jsonData != NULL) jsonData += CHAR_BYTES; // jump type
*data = jsonData; *data = jsonData;
} }
......
...@@ -45,6 +45,11 @@ class TDTestCase: ...@@ -45,6 +45,11 @@ class TDTestCase:
tdSql.query("select jtag from db_json_tag_test.jsons1_1") tdSql.query("select jtag from db_json_tag_test.jsons1_1")
tdSql.checkRows(1) tdSql.checkRows(1)
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.checkRows(1)
print("==============step3") print("==============step3")
tdLog.info("alter stable add tag") tdLog.info("alter stable add tag")
tdSql.error("ALTER STABLE db_json_tag_test.jsons1 add tag tag2 nchar(20)") tdSql.error("ALTER STABLE db_json_tag_test.jsons1 add tag tag2 nchar(20)")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册