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

TD-6129<feature> add json tag

上级 b8693f13
......@@ -32,6 +32,7 @@
#include "ttoken.h"
#include "tdataformat.h"
#include "cJSON.h"
enum {
TSDB_USE_SERVER_TS = 0,
......@@ -387,6 +388,10 @@ int32_t tsParseOneColumn(SSchema *pSchema, SStrToken *pToken, char *payload, cha
}
break;
case TSDB_DATA_TYPE_JSON:
*((int8_t *)payload) = -1;
break;
case TSDB_DATA_TYPE_TIMESTAMP: {
if (pToken->type == TK_NULL) {
if (primaryKey) {
......@@ -1067,6 +1072,67 @@ static int32_t tscCheckIfCreateTable(char **sqlstr, SSqlObj *pSql, char** boundC
tdAddColToKVRow(&kvRowBuilder, pSchema->colId, pSchema->type, tagVal);
}
// encode json tag string
if(spd.numOfBound == 1 && pTagSchema[spd.boundedColumns[0]].type == TSDB_DATA_TYPE_JSON){
const char* msg1 = "json parse error";
char tmp = sToken.z[sToken.n];
sToken.z[sToken.n] = 0;
cJSON *root = cJSON_Parse(sToken.z);
if (root == NULL){
tscError("json parse error");
return false;
}
int size = cJSON_GetArraySize(root);
if(!cJSON_IsObject(root) || size == 0){
tscError("json error invalide value");
}
int jsonIndex = 0;
for(int i = 0; i < size; i++) {
cJSON* item = cJSON_GetArrayItem(root, i);
if (!item) {
tscError("json inner error:%d", i);
continue;
}
char tagVal[TSDB_MAX_TAGS_LEN];
int32_t output = 0;
if (!taosMbsToUcs4(item->string, strlen(item->string), varDataVal(tagVal), pSchema->bytes - VARSTR_HEADER_SIZE, &output)) {
tscError("json string error:%s|%s", strerror(errno), item->string);
tdDestroyKVRowBuilder(&kvRowBuilder);
tscDestroyBoundColumnInfo(&spd);
return tscSQLSyntaxErrMsg(pInsertParam->msg, "serizelize json error", NULL);
}
varDataSetLen(tagVal, output);
tdAddColToKVRow(&kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_NCHAR, tagVal);
if(item->type == cJSON_String){
output = 0;
if (!taosMbsToUcs4(item->valuestring, strlen(item->valuestring), varDataVal(tagVal), pSchema->bytes - VARSTR_HEADER_SIZE, &output)) {
tscError("json string error:%s|%s", strerror(errno), item->string);
tdDestroyKVRowBuilder(&kvRowBuilder);
tscDestroyBoundColumnInfo(&spd);
return tscSQLSyntaxErrMsg(pInsertParam->msg, "serizelize json error", NULL);
}
varDataSetLen(tagVal, output);
tdAddColToKVRow(&kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_NCHAR, tagVal);
}else if(item->type == cJSON_Number){
*((double *)tagVal) = item->valuedouble;
tdAddColToKVRow(&kvRowBuilder, jsonIndex++, TSDB_DATA_TYPE_BIGINT, tagVal);
}else{
tdDestroyKVRowBuilder(&kvRowBuilder);
tscDestroyBoundColumnInfo(&spd);
return tscSQLSyntaxErrMsg(pInsertParam->msg, "invalidate json value", NULL);
}
}
cJSON_Delete(root);
sToken.z[sToken.n] = tmp;
}
tscDestroyBoundColumnInfo(&spd);
SKVRow row = tdGetKVRowFromBuilder(&kvRowBuilder);
......
......@@ -41,6 +41,7 @@
#include "qScript.h"
#include "ttype.h"
#include "qFilter.h"
#include "cJSON.h"
#define DEFAULT_PRIMARY_TIMESTAMP_COL_NAME "_c0"
......@@ -1476,6 +1477,34 @@ static bool validateTableColumnInfo(SArray* pFieldList, SSqlCmd* pCmd) {
return true;
}
static bool validataTagJson(char *json){
const char* msg1 = "json parse error";
cJSON *root = cJSON_Parse(json);
if (root == NULL){
tscError("json parse error : %s", json);
return false;
}
int size = cJSON_GetArraySize(root);
if(!cJSON_IsObject(root) || size == 0)
}
for(int i = 0; i < size; i++) {
cJSON* item = cJSON_GetArrayItem(root, i);
if (!item) {
item->string,
}
}
cJSON_Delete(root);
}
if (numOfTags == 1) {
TAOS_FIELD* p = taosArrayGet(pTagsList, 0);
if (p->type == TSDB_DATA_TYPE_JSON && validataTagJson(p->)) {
invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg5);
return false;
}
}
static bool validateTagParams(SArray* pTagsList, SArray* pFieldList, SSqlCmd* pCmd) {
assert(pTagsList != NULL);
......@@ -1487,6 +1516,7 @@ static bool validateTagParams(SArray* pTagsList, SArray* pFieldList, SSqlCmd* pC
const char* msg5 = "invalid data type in tags";
const char* msg6 = "invalid tag name";
const char* msg7 = "invalid binary/nchar tag length";
const char* msg8 = "only support one tag if include json type";
// number of fields at least 1
size_t numOfTags = taosArrayGetSize(pTagsList);
......@@ -1502,6 +1532,11 @@ static bool validateTagParams(SArray* pTagsList, SArray* pFieldList, SSqlCmd* pC
return false;
}
if (p->type == TSDB_DATA_TYPE_JSON && numOfTags != 1) {
invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg8);
return false;
}
if ((p->type == TSDB_DATA_TYPE_BINARY && p->bytes <= 0) ||
(p->type == TSDB_DATA_TYPE_NCHAR && p->bytes <= 0)) {
invalidOperationMsg(tscGetErrorMsgPayload(pCmd), msg7);
......
......@@ -18,7 +18,7 @@
#include "ttokendef.h"
#include "tscompression.h"
const int32_t TYPE_BYTES[15] = {
const int32_t TYPE_BYTES[16] = {
-1, // TSDB_DATA_TYPE_NULL
sizeof(int8_t), // TSDB_DATA_TYPE_BOOL
sizeof(int8_t), // TSDB_DATA_TYPE_TINYINT
......@@ -34,6 +34,7 @@ const int32_t TYPE_BYTES[15] = {
sizeof(uint16_t), // TSDB_DATA_TYPE_USMALLINT
sizeof(uint32_t), // TSDB_DATA_TYPE_UINT
sizeof(uint64_t), // TSDB_DATA_TYPE_UBIGINT
sizeof(VarDataOffsetT), // TSDB_DATA_TYPE_JSON
};
#define DO_STATICS(__sum, __min, __max, __minIndex, __maxIndex, _list, _index) \
......@@ -367,7 +368,7 @@ static void getStatics_nchr(const void *pData, int32_t numOfRow, int64_t *min, i
*maxIndex = 0;
}
tDataTypeDescriptor tDataTypes[15] = {
tDataTypeDescriptor tDataTypes[16] = {
{TSDB_DATA_TYPE_NULL, 6, 1, "NOTYPE", 0, 0, NULL, NULL, NULL},
{TSDB_DATA_TYPE_BOOL, 4, CHAR_BYTES, "BOOL", false, true, tsCompressBool, tsDecompressBool, getStatics_bool},
{TSDB_DATA_TYPE_TINYINT, 7, CHAR_BYTES, "TINYINT", INT8_MIN, INT8_MAX, tsCompressTinyint, tsDecompressTinyint, getStatics_i8},
......@@ -383,6 +384,7 @@ tDataTypeDescriptor tDataTypes[15] = {
{TSDB_DATA_TYPE_USMALLINT, 17, SHORT_BYTES, "SMALLINT UNSIGNED", 0, UINT16_MAX, tsCompressSmallint, tsDecompressSmallint, getStatics_u16},
{TSDB_DATA_TYPE_UINT, 12, INT_BYTES, "INT UNSIGNED", 0, UINT32_MAX, tsCompressInt, tsDecompressInt, getStatics_u32},
{TSDB_DATA_TYPE_UBIGINT, 15, LONG_BYTES, "BIGINT UNSIGNED", 0, UINT64_MAX, tsCompressBigint, tsDecompressBigint, getStatics_u64},
{TSDB_DATA_TYPE_JSON, 4, CHAR_BYTES, "JSON", INT8_MIN, INT8_MAX, tsCompressTinyint, tsDecompressTinyint, getStatics_i8},
};
char tTokenTypeSwitcher[13] = {
......@@ -428,7 +430,7 @@ FORCE_INLINE void* getDataMax(int32_t type) {
bool isValidDataType(int32_t type) {
return type >= TSDB_DATA_TYPE_NULL && type <= TSDB_DATA_TYPE_UBIGINT;
return type >= TSDB_DATA_TYPE_NULL && type <= TSDB_DATA_TYPE_JSON;
}
void setVardataNull(void* val, int32_t type) {
......
......@@ -46,6 +46,7 @@ typedef void **TAOS_ROW;
#define TSDB_DATA_TYPE_USMALLINT 12 // 2 bytes
#define TSDB_DATA_TYPE_UINT 13 // 4 bytes
#define TSDB_DATA_TYPE_UBIGINT 14 // 8 bytes
#define TSDB_DATA_TYPE_JSON 15
typedef enum {
TSDB_OPTION_LOCALE,
......
......@@ -39,7 +39,7 @@ extern "C" {
#define TSKEY_INITIAL_VAL INT64_MIN
// Bytes for each type.
extern const int32_t TYPE_BYTES[15];
extern const int32_t TYPE_BYTES[16];
// TODO: replace and remove code below
#define CHAR_BYTES sizeof(char)
......
......@@ -192,7 +192,7 @@ typedef struct tDataTypeDescriptor {
int16_t *minindex, int16_t *maxindex, int16_t *numofnull);
} tDataTypeDescriptor;
extern tDataTypeDescriptor tDataTypes[15];
extern tDataTypeDescriptor tDataTypes[16];
bool isValidDataType(int32_t type);
......
......@@ -21,6 +21,11 @@
#include "tulog.h"
#include "taoserror.h"
bool isInteger(double x){
int truncated = (int)x;
return (x == truncated);
}
int32_t strdequote(char *z) {
if (z == NULL) {
return 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册