Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
085ace59
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
085ace59
编写于
7月 01, 2022
作者:
wmmhello
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:to_json() function
上级
77fcf17a
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
109 addition
and
44 deletion
+109
-44
include/common/tdataformat.h
include/common/tdataformat.h
+1
-0
source/common/src/tvariant.c
source/common/src/tvariant.c
+5
-2
source/libs/command/src/command.c
source/libs/command/src/command.c
+1
-1
source/libs/function/src/builtins.c
source/libs/function/src/builtins.c
+1
-1
source/libs/nodes/src/nodesCloneFuncs.c
source/libs/nodes/src/nodesCloneFuncs.c
+10
-1
source/libs/nodes/src/nodesCodeFuncs.c
source/libs/nodes/src/nodesCodeFuncs.c
+37
-2
source/libs/nodes/src/nodesUtilFuncs.c
source/libs/nodes/src/nodesUtilFuncs.c
+5
-0
source/libs/parser/inc/parUtil.h
source/libs/parser/inc/parUtil.h
+0
-1
source/libs/parser/src/parUtil.c
source/libs/parser/src/parUtil.c
+2
-1
source/libs/qcom/src/queryUtil.c
source/libs/qcom/src/queryUtil.c
+12
-5
source/libs/scalar/src/scalar.c
source/libs/scalar/src/scalar.c
+5
-1
source/libs/scalar/src/sclfunc.c
source/libs/scalar/src/sclfunc.c
+13
-25
source/os/src/osString.c
source/os/src/osString.c
+4
-4
tests/system-test/2-query/json_tag.py
tests/system-test/2-query/json_tag.py
+13
-0
未找到文件。
include/common/tdataformat.h
浏览文件 @
085ace59
...
...
@@ -78,6 +78,7 @@ int32_t tEncodeTag(SEncoder *pEncoder, const STag *pTag);
int32_t
tDecodeTag
(
SDecoder
*
pDecoder
,
STag
**
ppTag
);
int32_t
tTagToValArray
(
const
STag
*
pTag
,
SArray
**
ppArray
);
void
debugPrintSTag
(
STag
*
pTag
,
const
char
*
tag
,
int32_t
ln
);
// TODO: remove
int32_t
parseJsontoTagData
(
const
char
*
json
,
SArray
*
pTagVals
,
STag
**
ppTag
,
void
*
pMsgBuf
);
// STRUCT =================
struct
STColumn
{
...
...
source/common/src/tvariant.c
浏览文件 @
085ace59
...
...
@@ -155,7 +155,8 @@ void taosVariantCreateFromBinary(SVariant *pVar, const char *pz, size_t len, uin
void
taosVariantDestroy
(
SVariant
*
pVar
)
{
if
(
pVar
==
NULL
)
return
;
if
(
pVar
->
nType
==
TSDB_DATA_TYPE_BINARY
||
pVar
->
nType
==
TSDB_DATA_TYPE_NCHAR
)
{
if
(
pVar
->
nType
==
TSDB_DATA_TYPE_BINARY
||
pVar
->
nType
==
TSDB_DATA_TYPE_NCHAR
||
pVar
->
nType
==
TSDB_DATA_TYPE_JSON
)
{
taosMemoryFreeClear
(
pVar
->
pz
);
pVar
->
nLen
=
0
;
}
...
...
@@ -184,7 +185,8 @@ void taosVariantAssign(SVariant *pDst, const SVariant *pSrc) {
if
(
pSrc
==
NULL
||
pDst
==
NULL
)
return
;
pDst
->
nType
=
pSrc
->
nType
;
if
(
pSrc
->
nType
==
TSDB_DATA_TYPE_BINARY
||
pSrc
->
nType
==
TSDB_DATA_TYPE_NCHAR
)
{
if
(
pSrc
->
nType
==
TSDB_DATA_TYPE_BINARY
||
pSrc
->
nType
==
TSDB_DATA_TYPE_NCHAR
||
pSrc
->
nType
==
TSDB_DATA_TYPE_JSON
)
{
int32_t
len
=
pSrc
->
nLen
+
TSDB_NCHAR_SIZE
;
char
*
p
=
taosMemoryRealloc
(
pDst
->
pz
,
len
);
assert
(
p
);
...
...
@@ -976,6 +978,7 @@ char *taosVariantGet(SVariant *pVar, int32_t type) {
case
TSDB_DATA_TYPE_FLOAT
:
return
(
char
*
)
&
pVar
->
d
;
case
TSDB_DATA_TYPE_BINARY
:
case
TSDB_DATA_TYPE_JSON
:
return
(
char
*
)
pVar
->
pz
;
case
TSDB_DATA_TYPE_NCHAR
:
return
(
char
*
)
pVar
->
ucs4
;
...
...
source/libs/command/src/command.c
浏览文件 @
085ace59
...
...
@@ -297,7 +297,7 @@ int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg) {
SArray
*
pTagVals
=
NULL
;
STag
*
pTag
=
(
STag
*
)
pCfg
->
pTags
;
if
(
pCfg
->
pTags
&&
pTag
->
flags
&
TD_TAG_JSON
)
{
if
(
pCfg
->
pTags
&&
tTagIsJson
(
pTag
)
)
{
char
*
pJson
=
parseTagDatatoJson
(
pTag
);
if
(
pJson
)
{
*
len
+=
sprintf
(
buf
+
VARSTR_HEADER_SIZE
+
*
len
,
"%s"
,
pJson
);
...
...
source/libs/function/src/builtins.c
浏览文件 @
085ace59
...
...
@@ -1543,7 +1543,7 @@ static int32_t translateToJson(SFunctionNode* pFunc, char* pErrBuf, int32_t len)
return
invaildFuncParaTypeErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
}
pFunc
->
node
.
resType
=
(
SDataType
){.
bytes
=
tDataTypes
[
TSDB_DATA_TYPE_
BINARY
].
bytes
,
.
type
=
TSDB_DATA_TYPE_BINARY
};
pFunc
->
node
.
resType
=
(
SDataType
){.
bytes
=
tDataTypes
[
TSDB_DATA_TYPE_
JSON
].
bytes
,
.
type
=
TSDB_DATA_TYPE_JSON
};
return
TSDB_CODE_SUCCESS
;
}
...
...
source/libs/nodes/src/nodesCloneFuncs.c
浏览文件 @
085ace59
...
...
@@ -18,6 +18,7 @@
#include "querynodes.h"
#include "taos.h"
#include "taoserror.h"
#include "tdatablock.h"
#define COPY_SCALAR_FIELD(fldname) \
do { \
...
...
@@ -164,7 +165,15 @@ static int32_t valueNodeCopy(const SValueNode* pSrc, SValueNode* pDst) {
memcpy
(
pDst
->
datum
.
p
,
pSrc
->
datum
.
p
,
len
);
break
;
}
case
TSDB_DATA_TYPE_JSON
:
case
TSDB_DATA_TYPE_JSON
:{
int32_t
len
=
getJsonValueLen
(
pSrc
->
datum
.
p
);
pDst
->
datum
.
p
=
taosMemoryCalloc
(
1
,
len
);
if
(
NULL
==
pDst
->
datum
.
p
)
{
return
TSDB_CODE_OUT_OF_MEMORY
;
}
memcpy
(
pDst
->
datum
.
p
,
pSrc
->
datum
.
p
,
len
);
break
;
}
case
TSDB_DATA_TYPE_DECIMAL
:
case
TSDB_DATA_TYPE_BLOB
:
case
TSDB_DATA_TYPE_MEDIUMBLOB
:
...
...
source/libs/nodes/src/nodesCodeFuncs.c
浏览文件 @
085ace59
...
...
@@ -20,6 +20,7 @@
#include "querynodes.h"
#include "taoserror.h"
#include "tjson.h"
#include "tdatablock.h"
static
int32_t
nodeToJson
(
const
void
*
pObj
,
SJson
*
pJson
);
static
int32_t
jsonToNode
(
const
SJson
*
pJson
,
void
*
pObj
);
...
...
@@ -2638,7 +2639,18 @@ static int32_t datumToJson(const void* pObj, SJson* pJson) {
case
TSDB_DATA_TYPE_VARBINARY
:
code
=
tjsonAddStringToObject
(
pJson
,
jkValueDatum
,
varDataVal
(
pNode
->
datum
.
p
));
break
;
case
TSDB_DATA_TYPE_JSON
:
case
TSDB_DATA_TYPE_JSON
:{
int32_t
len
=
getJsonValueLen
(
pNode
->
datum
.
p
);
char
*
buf
=
taosMemoryCalloc
(
len
*
2
+
1
,
sizeof
(
char
));
code
=
taosHexEncode
(
pNode
->
datum
.
p
,
buf
,
len
);
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
taosMemoryFree
(
buf
);
return
TSDB_CODE_TSC_INVALID_VALUE
;
}
code
=
tjsonAddStringToObject
(
pJson
,
jkValueDatum
,
buf
);
taosMemoryFree
(
buf
);
break
;
}
case
TSDB_DATA_TYPE_DECIMAL
:
case
TSDB_DATA_TYPE_BLOB
:
// todo
...
...
@@ -2761,7 +2773,30 @@ static int32_t jsonToDatum(const SJson* pJson, void* pObj) {
}
break
;
}
case
TSDB_DATA_TYPE_JSON
:
case
TSDB_DATA_TYPE_JSON
:{
pNode
->
datum
.
p
=
taosMemoryCalloc
(
1
,
pNode
->
node
.
resType
.
bytes
);
if
(
NULL
==
pNode
->
datum
.
p
)
{
code
=
TSDB_CODE_OUT_OF_MEMORY
;
break
;
}
char
*
buf
=
taosMemoryCalloc
(
1
,
pNode
->
node
.
resType
.
bytes
*
2
+
1
);
if
(
NULL
==
buf
)
{
code
=
TSDB_CODE_OUT_OF_MEMORY
;
break
;
}
code
=
tjsonGetStringValue
(
pJson
,
jkValueDatum
,
buf
);
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
taosMemoryFree
(
buf
);
break
;
}
code
=
taosHexDecode
(
buf
,
pNode
->
datum
.
p
,
pNode
->
node
.
resType
.
bytes
);
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
taosMemoryFree
(
buf
);
break
;
}
taosMemoryFree
(
buf
);
break
;
}
case
TSDB_DATA_TYPE_DECIMAL
:
case
TSDB_DATA_TYPE_BLOB
:
// todo
...
...
source/libs/nodes/src/nodesUtilFuncs.c
浏览文件 @
085ace59
...
...
@@ -20,6 +20,7 @@
#include "taos.h"
#include "taoserror.h"
#include "thash.h"
#include "tdatablock.h"
static
SNode
*
makeNode
(
ENodeType
type
,
size_t
size
)
{
SNode
*
p
=
taosMemoryCalloc
(
1
,
size
);
...
...
@@ -1675,6 +1676,10 @@ void nodesValueNodeToVariant(const SValueNode* pNode, SVariant* pVal) {
pVal
->
pz
[
pVal
->
nLen
+
VARSTR_HEADER_SIZE
]
=
0
;
break
;
case
TSDB_DATA_TYPE_JSON
:
pVal
->
nLen
=
getJsonValueLen
(
pNode
->
datum
.
p
);
pVal
->
pz
=
taosMemoryMalloc
(
pVal
->
nLen
);
memcpy
(
pVal
->
pz
,
pNode
->
datum
.
p
,
pVal
->
nLen
);
break
;
case
TSDB_DATA_TYPE_DECIMAL
:
case
TSDB_DATA_TYPE_BLOB
:
// todo
...
...
source/libs/parser/inc/parUtil.h
浏览文件 @
085ace59
...
...
@@ -62,7 +62,6 @@ int32_t getNumOfColumns(const STableMeta* pTableMeta);
int32_t
getNumOfTags
(
const
STableMeta
*
pTableMeta
);
STableComInfo
getTableInfo
(
const
STableMeta
*
pTableMeta
);
STableMeta
*
tableMetaDup
(
const
STableMeta
*
pTableMeta
);
int32_t
parseJsontoTagData
(
const
char
*
json
,
SArray
*
pTagVals
,
STag
**
ppTag
,
SMsgBuf
*
pMsgBuf
);
int32_t
trimString
(
const
char
*
src
,
int32_t
len
,
char
*
dst
,
int32_t
dlen
);
...
...
source/libs/parser/src/parUtil.c
浏览文件 @
085ace59
...
...
@@ -219,6 +219,7 @@ int32_t buildInvalidOperationMsg(SMsgBuf* pBuf, const char* msg) {
}
int32_t
buildSyntaxErrMsg
(
SMsgBuf
*
pBuf
,
const
char
*
additionalInfo
,
const
char
*
sourceStr
)
{
if
(
pBuf
==
NULL
)
return
TSDB_CODE_TSC_SQL_SYNTAX_ERROR
;
const
char
*
msgFormat1
=
"syntax error near
\'
%s
\'
"
;
const
char
*
msgFormat2
=
"syntax error near
\'
%s
\'
(%s)"
;
const
char
*
msgFormat3
=
"%s"
;
...
...
@@ -346,7 +347,7 @@ static bool isValidateTag(char* input) {
return
true
;
}
int32_t
parseJsontoTagData
(
const
char
*
json
,
SArray
*
pTagVals
,
STag
**
ppTag
,
SMsgBuf
*
pMsgBuf
)
{
int32_t
parseJsontoTagData
(
const
char
*
json
,
SArray
*
pTagVals
,
STag
**
ppTag
,
void
*
pMsgBuf
)
{
int32_t
retCode
=
TSDB_CODE_SUCCESS
;
cJSON
*
root
=
NULL
;
SHashObj
*
keyHash
=
NULL
;
...
...
source/libs/qcom/src/queryUtil.c
浏览文件 @
085ace59
...
...
@@ -304,18 +304,21 @@ int32_t dataConverToStr(char* str, int type, void* buf, int32_t bufSize, int32_t
char
*
parseTagDatatoJson
(
void
*
p
)
{
char
*
string
=
NULL
;
cJSON
*
json
=
cJSON_CreateObject
();
if
(
json
==
NULL
)
{
goto
end
;
}
SArray
*
pTagVals
=
NULL
;
cJSON
*
json
=
NULL
;
if
(
tTagToValArray
((
const
STag
*
)
p
,
&
pTagVals
)
!=
0
)
{
goto
end
;
}
int16_t
nCols
=
taosArrayGetSize
(
pTagVals
);
if
(
nCols
==
0
)
{
goto
end
;
}
char
tagJsonKey
[
256
]
=
{
0
};
json
=
cJSON_CreateObject
();
if
(
json
==
NULL
)
{
goto
end
;
}
for
(
int
j
=
0
;
j
<
nCols
;
++
j
)
{
STagVal
*
pTagVal
=
(
STagVal
*
)
taosArrayGet
(
pTagVals
,
j
);
// json key encode by binary
...
...
@@ -373,6 +376,10 @@ char* parseTagDatatoJson(void* p) {
string
=
cJSON_PrintUnformatted
(
json
);
end:
cJSON_Delete
(
json
);
taosArrayDestroy
(
pTagVals
);
if
(
string
==
NULL
){
string
=
strdup
(
TSDB_DATA_NULL_STR_L
);
}
return
string
;
}
...
...
source/libs/scalar/src/scalar.c
浏览文件 @
085ace59
...
...
@@ -695,7 +695,11 @@ EDealRes sclRewriteFunction(SNode** pNode, SScalarCtx *ctx) {
res
->
node
.
resType
.
scale
=
output
.
columnData
->
info
.
scale
;
res
->
node
.
resType
.
precision
=
output
.
columnData
->
info
.
precision
;
int32_t
type
=
output
.
columnData
->
info
.
type
;
if
(
IS_VAR_DATA_TYPE
(
type
))
{
if
(
type
==
TSDB_DATA_TYPE_JSON
){
int32_t
len
=
getJsonValueLen
(
output
.
columnData
->
pData
);
res
->
datum
.
p
=
taosMemoryCalloc
(
len
,
1
);
memcpy
(
res
->
datum
.
p
,
output
.
columnData
->
pData
,
len
);
}
else
if
(
IS_VAR_DATA_TYPE
(
type
))
{
res
->
datum
.
p
=
taosMemoryCalloc
(
res
->
node
.
resType
.
bytes
+
VARSTR_HEADER_SIZE
+
1
,
1
);
memcpy
(
res
->
datum
.
p
,
output
.
columnData
->
pData
,
varDataTLen
(
output
.
columnData
->
pData
));
}
else
{
...
...
source/libs/scalar/src/sclfunc.c
浏览文件 @
085ace59
...
...
@@ -1152,42 +1152,30 @@ int32_t toJsonFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOu
char
tmp
[
TSDB_MAX_JSON_TAG_LEN
]
=
{
0
};
for
(
int32_t
i
=
0
;
i
<
pInput
[
0
].
numOfRows
;
++
i
)
{
if
(
colDataIsNull_s
(
pInput
[
0
].
columnData
,
i
))
{
colDataAppendNULL
(
pOutput
->
columnData
,
i
);
continue
;
}
char
*
input
=
pInput
[
0
].
columnData
->
pData
+
pInput
[
0
].
columnData
->
varmeta
.
offset
[
i
];
SArray
*
pTagVals
=
taosArrayInit
(
8
,
sizeof
(
STagVal
));
STag
*
pTag
=
NULL
;
if
(
type
==
TSDB_DATA_TYPE_NCHAR
){
if
(
varDataTLen
(
input
)
>
TSDB_MAX_JSON_TAG_LEN
){
colDataAppendNULL
(
pOutput
->
columnData
,
i
);
continue
;
}
int32_t
len
=
taosUcs4ToMbs
((
TdUcs4
*
)
varDataVal
(
input
),
varDataLen
(
input
),
tmp
);
if
(
len
<
0
)
{
colDataAppendNULL
(
pOutput
->
columnData
,
i
);
continue
;
}
tmp
[
len
]
=
0
;
if
(
colDataIsNull_s
(
pInput
[
0
].
columnData
,
i
))
{
tTagNew
(
pTagVals
,
1
,
true
,
&
pTag
);
}
else
{
char
*
input
=
pInput
[
0
].
columnData
->
pData
+
pInput
[
0
].
columnData
->
varmeta
.
offset
[
i
];
if
(
varDataLen
(
input
)
>
(
TSDB_MAX_JSON_TAG_LEN
-
VARSTR_HEADER_SIZE
)
/
TSDB_NCHAR_SIZE
){
colDataAppendNULL
(
pOutput
->
columnData
,
i
);
continue
;
taosArrayDestroy
(
pTagVals
);
return
TSDB_CODE_FAILED
;
}
memcpy
(
tmp
,
varDataVal
(
input
),
varDataLen
(
input
));
tmp
[
varDataLen
(
input
)]
=
0
;
if
(
parseJsontoTagData
(
tmp
,
pTagVals
,
&
pTag
,
NULL
)){
tTagNew
(
pTagVals
,
1
,
true
,
&
pTag
);
}
}
if
(
!
tjsonValidateJson
(
tmp
)){
colDataAppendNULL
(
pOutput
->
columnData
,
i
);
continue
;
}
colDataAppend
(
pOutput
->
columnData
,
i
,
input
,
false
);
colDataAppend
(
pOutput
->
columnData
,
i
,
(
const
char
*
)
pTag
,
false
);
tTagFree
(
pTag
);
taosArrayDestroy
(
pTagVals
);
}
pOutput
->
numOfRows
=
pInput
->
numOfRows
;
return
TSDB_CODE_SUCCESS
;
}
...
...
source/os/src/osString.c
浏览文件 @
085ace59
...
...
@@ -202,7 +202,7 @@ int32_t taosHexEncode(const char *src, char *dst, int32_t len) {
}
for
(
int32_t
i
=
0
;
i
<
len
;
++
i
)
{
sprintf
(
dst
+
i
*
2
,
"%02x"
,
src
[
i
]
&
0xff
);
sprintf
(
dst
+
i
*
2
,
"%02x"
,
src
[
i
]);
}
return
0
;
...
...
@@ -213,10 +213,10 @@ int32_t taosHexDecode(const char *src, char *dst, int32_t len) {
return
-
1
;
}
uint
16
_t
hn
,
ln
,
out
;
uint
8
_t
hn
,
ln
,
out
;
for
(
int
i
=
0
,
j
=
0
;
i
<
len
*
2
;
i
+=
2
,
++
j
)
{
hn
=
src
[
i
]
>
'9'
?
src
[
i
]
-
'
A
'
+
10
:
src
[
i
]
-
'0'
;
ln
=
src
[
i
+
1
]
>
'9'
?
src
[
i
+
1
]
-
'
A
'
+
10
:
src
[
i
+
1
]
-
'0'
;
hn
=
src
[
i
]
>
'9'
?
src
[
i
]
-
'
a
'
+
10
:
src
[
i
]
-
'0'
;
ln
=
src
[
i
+
1
]
>
'9'
?
src
[
i
+
1
]
-
'
a
'
+
10
:
src
[
i
+
1
]
-
'0'
;
out
=
(
hn
<<
4
)
|
ln
;
memcpy
(
dst
+
j
,
&
out
,
1
);
...
...
tests/system-test/2-query/json_tag.py
浏览文件 @
085ace59
...
...
@@ -684,6 +684,19 @@ class TDTestCase:
tdSql
.
query
(
"select ELAPSED(ts,1h) from jsons1 where jtag->'tag1'>1;"
)
tdSql
.
checkRows
(
1
)
# to_json()
tdSql
.
query
(
"select to_json('{
\"
abc
\"
:123}') from jsons1_1"
)
tdSql
.
checkRows
(
2
)
tdSql
.
checkData
(
0
,
0
,
'{"abc":123}'
)
tdSql
.
checkData
(
1
,
0
,
'{"abc":123}'
)
tdSql
.
query
(
"select to_json('null') from jsons1_1"
)
tdSql
.
checkRows
(
2
)
tdSql
.
checkData
(
0
,
0
,
'null'
)
tdSql
.
checkData
(
1
,
0
,
'null'
)
tdSql
.
query
(
"select to_json('{
\"
key
\"
}') from jsons1_1"
)
tdSql
.
checkRows
(
2
)
tdSql
.
checkData
(
0
,
0
,
'null'
)
tdSql
.
checkData
(
1
,
0
,
'null'
)
#test TD-12077
tdSql
.
execute
(
"insert into jsons1_16 using jsons1 tags('{
\"
tag1
\"
:
\"
收到货
\"
,
\"
tag2
\"
:
\"\"
,
\"
tag3
\"
:-2.111}') values(1591062628000, 2, NULL, '你就会', 'dws')"
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录