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

change json output format

上级 9060898a
...@@ -2494,7 +2494,7 @@ int32_t tscHandleFirstRoundStableQuery(SSqlObj *pSql) { ...@@ -2494,7 +2494,7 @@ int32_t tscHandleFirstRoundStableQuery(SSqlObj *pSql) {
SExprInfo* p = tscAddFuncInSelectClause(pNewQueryInfo, index++, TSDB_FUNC_TAG, &colIndex, schema, TSDB_COL_TAG, getNewResColId(pCmd)); SExprInfo* p = tscAddFuncInSelectClause(pNewQueryInfo, index++, TSDB_FUNC_TAG, &colIndex, schema, TSDB_COL_TAG, getNewResColId(pCmd));
if (schema->type == TSDB_DATA_TYPE_JSON){ if (schema->type == TSDB_DATA_TYPE_JSON){
p->base.numOfParams = pExpr->base.numOfParams; p->base.numOfParams = pExpr->base.numOfParams;
p->base.param[0] = pExpr->base.param[0]; tVariantAssign(&p->base.param[0], &pExpr->base.param[0]);
} }
p->base.resColId = pExpr->base.resColId; p->base.resColId = pExpr->base.resColId;
} else if (pExpr->base.functionId == TSDB_FUNC_PRJ) { } else if (pExpr->base.functionId == TSDB_FUNC_PRJ) {
...@@ -3687,7 +3687,14 @@ TAOS_ROW doSetResultRowData(SSqlObj *pSql) { ...@@ -3687,7 +3687,14 @@ TAOS_ROW doSetResultRowData(SSqlObj *pSql) {
int32_t type = pInfo->field.type; int32_t type = pInfo->field.type;
int32_t bytes = pInfo->field.bytes; int32_t bytes = pInfo->field.bytes;
if (!IS_VAR_DATA_TYPE(type) && type != TSDB_DATA_TYPE_JSON) { if (type == TSDB_DATA_TYPE_JSON){
char* p = pRes->urow[i];
if (*p == TSDB_DATA_TYPE_NCHAR && isNull(POINTER_SHIFT(p, CHAR_BYTES), type)) {
pRes->tsrow[j] = NULL;
}else{
pRes->tsrow[j] = pRes->urow[i];
}
}else if (!IS_VAR_DATA_TYPE(type)) {
pRes->tsrow[j] = isNull(pRes->urow[i], type) ? NULL : pRes->urow[i]; pRes->tsrow[j] = isNull(pRes->urow[i], type) ? NULL : pRes->urow[i];
} else { } else {
pRes->tsrow[j] = isNull(pRes->urow[i], type) ? NULL : varDataVal(pRes->urow[i]); pRes->tsrow[j] = isNull(pRes->urow[i], type) ? NULL : varDataVal(pRes->urow[i]);
......
...@@ -750,7 +750,7 @@ static void setResRawPtrImpl(SSqlRes* pRes, SInternalField* pInfo, int32_t i, bo ...@@ -750,7 +750,7 @@ static void setResRawPtrImpl(SSqlRes* pRes, SInternalField* pInfo, int32_t i, bo
p += pInfo->field.bytes; p += pInfo->field.bytes;
} }
memcpy(pRes->urow[i], pRes->buffer[i], pInfo->field.bytes * pRes->numOfRows); memcpy(pRes->urow[i], pRes->buffer[i], pInfo->field.bytes * pRes->numOfRows);
}else if (pInfo->field.type == TSDB_DATA_TYPE_JSON) { }else if (convertNchar && pInfo->field.type == TSDB_DATA_TYPE_JSON && *(char*)(pRes->urow[i]) == TSDB_DATA_TYPE_NCHAR) {
// convert unicode to native code in a temporary buffer extra one byte for terminated symbol // convert unicode to native code in a temporary buffer extra one byte for terminated symbol
char* buffer = realloc(pRes->buffer[i], pInfo->field.bytes * pRes->numOfRows); char* buffer = realloc(pRes->buffer[i], pInfo->field.bytes * pRes->numOfRows);
if (buffer == NULL) return; if (buffer == NULL) return;
...@@ -761,34 +761,14 @@ static void setResRawPtrImpl(SSqlRes* pRes, SInternalField* pInfo, int32_t i, bo ...@@ -761,34 +761,14 @@ 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 type = *p;
char* realData = p + CHAR_BYTES; char* realData = p + CHAR_BYTES;
if (type == TSDB_DATA_TYPE_NCHAR && isNull(realData, TSDB_DATA_TYPE_NCHAR)) {
memcpy(dst, realData, varDataTLen(realData)); *dst = *p;
} else if (type == TSDB_DATA_TYPE_BINARY) { dst += CHAR_BYTES;
assert(*(uint32_t*)varDataVal(realData) == TSDB_DATA_JSON_null); // json null value int32_t length = taosUcs4ToMbs(varDataVal(realData), varDataLen(realData), varDataVal(dst));
assert(varDataLen(realData) == INT_BYTES); varDataSetLen(dst, length);
sprintf(varDataVal(dst), "%s", "null"); if (length == 0) {
varDataSetLen(dst, strlen(varDataVal(dst))); tscError("charset:%s to %s. val:%s convert failed.", DEFAULT_UNICODE_ENCODEC, tsCharset, (char*)p);
}else if (type == TSDB_DATA_TYPE_NCHAR) {
int32_t length = taosUcs4ToMbs(varDataVal(realData), varDataLen(realData), varDataVal(dst));
varDataSetLen(dst, length);
if (length == 0) {
tscError("charset:%s to %s. val:%s convert failed.", DEFAULT_UNICODE_ENCODEC, tsCharset, (char*)p);
}
}else if (type == TSDB_DATA_TYPE_DOUBLE) {
double jsonVd = *(double*)(realData);
sprintf(varDataVal(dst), "%.9lf", jsonVd);
varDataSetLen(dst, strlen(varDataVal(dst)));
}else if (type == TSDB_DATA_TYPE_BIGINT) {
int64_t jsonVd = *(int64_t*)(realData);
sprintf(varDataVal(dst), "%" PRId64, jsonVd);
varDataSetLen(dst, strlen(varDataVal(dst)));
}else if (type == TSDB_DATA_TYPE_BOOL) {
sprintf(varDataVal(dst), "%s", (*((char *)realData) == 1) ? "true" : "false");
varDataSetLen(dst, strlen(varDataVal(dst)));
}else {
assert(0);
} }
p += pInfo->field.bytes; p += pInfo->field.bytes;
......
...@@ -470,12 +470,22 @@ static char* formatTimestamp(char* buf, int64_t val, int precision) { ...@@ -470,12 +470,22 @@ static char* formatTimestamp(char* buf, int64_t val, int precision) {
} }
static void dumpFieldToFile(FILE* fp, const char* val, TAOS_FIELD* field, int32_t length, int precision) { static void dumpFieldToFile(FILE* fp, char* val, TAOS_FIELD* field, int32_t length, int precision) {
if (val == NULL) { if (val == NULL) {
fprintf(fp, "%s", TSDB_DATA_NULL_STR); fprintf(fp, "%s", TSDB_DATA_NULL_STR);
return; return;
} }
uint8_t type = field->type;
if (type == TSDB_DATA_TYPE_JSON){
char* p = val;
type = *p;
val += CHAR_BYTES;
if(type == TSDB_DATA_TYPE_NCHAR) {
length = varDataLen(val);
}
}
char buf[TSDB_MAX_BYTES_PER_ROW]; char buf[TSDB_MAX_BYTES_PER_ROW];
switch (field->type) { switch (field->type) {
case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_BOOL:
...@@ -556,7 +566,7 @@ static int dumpResultToFile(const char* fname, TAOS_RES* tres) { ...@@ -556,7 +566,7 @@ static int dumpResultToFile(const char* fname, TAOS_RES* tres) {
if (i > 0) { if (i > 0) {
fputc(',', fp); fputc(',', fp);
} }
dumpFieldToFile(fp, (const char*)row[i], fields +i, length[i], precision); dumpFieldToFile(fp, row[i], fields +i, length[i], precision);
} }
fputc('\n', fp); fputc('\n', fp);
...@@ -635,7 +645,7 @@ static void shellPrintNChar(const char *str, int length, int width) { ...@@ -635,7 +645,7 @@ static void shellPrintNChar(const char *str, int length, int width) {
} }
static void printField(const char* val, TAOS_FIELD* field, int width, int32_t length, int precision) { static void printField(char* val, TAOS_FIELD* field, int width, int32_t length, int precision) {
if (val == NULL) { if (val == NULL) {
int w = width; int w = width;
if (field->type < TSDB_DATA_TYPE_TINYINT || field->type > TSDB_DATA_TYPE_DOUBLE) { if (field->type < TSDB_DATA_TYPE_TINYINT || field->type > TSDB_DATA_TYPE_DOUBLE) {
...@@ -648,8 +658,18 @@ static void printField(const char* val, TAOS_FIELD* field, int width, int32_t le ...@@ -648,8 +658,18 @@ static void printField(const char* val, TAOS_FIELD* field, int width, int32_t le
return; return;
} }
uint8_t type = field->type;
if (type == TSDB_DATA_TYPE_JSON){
char* p = val;
type = *p;
val += CHAR_BYTES;
if(type == TSDB_DATA_TYPE_NCHAR) {
length = varDataLen(val);
}
}
char buf[TSDB_MAX_BYTES_PER_ROW]; char buf[TSDB_MAX_BYTES_PER_ROW];
switch (field->type) { switch (type) {
case TSDB_DATA_TYPE_BOOL: case TSDB_DATA_TYPE_BOOL:
printf("%*s", width, ((((int32_t)(*((char *)val))) == 1) ? "true" : "false")); printf("%*s", width, ((((int32_t)(*((char *)val))) == 1) ? "true" : "false"));
break; break;
...@@ -685,7 +705,6 @@ static void printField(const char* val, TAOS_FIELD* field, int width, int32_t le ...@@ -685,7 +705,6 @@ static void printField(const char* val, TAOS_FIELD* field, int width, int32_t le
break; break;
case TSDB_DATA_TYPE_BINARY: case TSDB_DATA_TYPE_BINARY:
case TSDB_DATA_TYPE_NCHAR: case TSDB_DATA_TYPE_NCHAR:
case TSDB_DATA_TYPE_JSON:
shellPrintNChar(val, length, width); shellPrintNChar(val, length, width);
break; break;
case TSDB_DATA_TYPE_TIMESTAMP: case TSDB_DATA_TYPE_TIMESTAMP:
...@@ -747,7 +766,7 @@ static int verticalPrintResult(TAOS_RES* tres) { ...@@ -747,7 +766,7 @@ static int verticalPrintResult(TAOS_RES* tres) {
int padding = (int)(maxColNameLen - strlen(field->name)); int padding = (int)(maxColNameLen - strlen(field->name));
printf("%*.s%s: ", padding, " ", field->name); printf("%*.s%s: ", padding, " ", field->name);
printField((const char*)row[i], field, 0, length[i], precision); printField(row[i], field, 0, length[i], precision);
putchar('\n'); putchar('\n');
} }
} else if (showMore) { } else if (showMore) {
...@@ -877,7 +896,7 @@ static int horizontalPrintResult(TAOS_RES* tres) { ...@@ -877,7 +896,7 @@ static int horizontalPrintResult(TAOS_RES* tres) {
if (numOfRows < resShowMaxNum) { if (numOfRows < resShowMaxNum) {
for (int i = 0; i < num_fields; i++) { for (int i = 0; i < num_fields; i++) {
putchar(' '); putchar(' ');
printField((const char*)row[i], fields + i, width[i], length[i], precision); printField(row[i], fields + i, width[i], length[i], precision);
putchar(' '); putchar(' ');
putchar('|'); putchar('|');
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册