Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
570b36cb
T
TDengine
项目概览
taosdata
/
TDengine
大约 1 年 前同步成功
通知
1184
Star
22015
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
570b36cb
编写于
6月 25, 2022
作者:
G
Ganlin Zhao
提交者:
GitHub
6月 25, 2022
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #14232 from taosdata/enh/cast_function
enh(query): enhance cast function to support more types
上级
54fb69ba
52bfe2e7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
206 addition
and
35 deletion
+206
-35
source/libs/function/src/builtins.c
source/libs/function/src/builtins.c
+11
-10
source/libs/scalar/src/sclfunc.c
source/libs/scalar/src/sclfunc.c
+174
-4
tests/system-test/2-query/cast.py
tests/system-test/2-query/cast.py
+21
-21
未找到文件。
source/libs/function/src/builtins.c
浏览文件 @
570b36cb
...
@@ -1220,18 +1220,19 @@ static int32_t translateSubstr(SFunctionNode* pFunc, char* pErrBuf, int32_t len)
...
@@ -1220,18 +1220,19 @@ static int32_t translateSubstr(SFunctionNode* pFunc, char* pErrBuf, int32_t len)
static
int32_t
translateCast
(
SFunctionNode
*
pFunc
,
char
*
pErrBuf
,
int32_t
len
)
{
static
int32_t
translateCast
(
SFunctionNode
*
pFunc
,
char
*
pErrBuf
,
int32_t
len
)
{
// The number of parameters has been limited by the syntax definition
// The number of parameters has been limited by the syntax definition
uint8_t
para1Type
=
((
SExprNode
*
)
nodesListGetNode
(
pFunc
->
pParameterList
,
0
))
->
resType
.
type
;
//uint8_t para1Type = ((SExprNode*)nodesListGetNode(pFunc->pParameterList, 0))->resType.type;
// The function return type has been set during syntax parsing
// The function return type has been set during syntax parsing
uint8_t
para2Type
=
pFunc
->
node
.
resType
.
type
;
uint8_t
para2Type
=
pFunc
->
node
.
resType
.
type
;
if
(
para2Type
!=
TSDB_DATA_TYPE_BIGINT
&&
para2Type
!=
TSDB_DATA_TYPE_UBIGINT
&&
//
if (para2Type != TSDB_DATA_TYPE_BIGINT && para2Type != TSDB_DATA_TYPE_UBIGINT &&
para2Type
!=
TSDB_DATA_TYPE_VARCHAR
&&
para2Type
!=
TSDB_DATA_TYPE_NCHAR
&&
//
para2Type != TSDB_DATA_TYPE_VARCHAR && para2Type != TSDB_DATA_TYPE_NCHAR &&
para2Type
!=
TSDB_DATA_TYPE_TIMESTAMP
)
{
//
para2Type != TSDB_DATA_TYPE_TIMESTAMP) {
return
invaildFuncParaTypeErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
//
return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
}
//
}
if
((
para2Type
==
TSDB_DATA_TYPE_TIMESTAMP
&&
IS_VAR_DATA_TYPE
(
para1Type
))
||
//
if ((para2Type == TSDB_DATA_TYPE_TIMESTAMP && IS_VAR_DATA_TYPE(para1Type)) ||
(
para2Type
==
TSDB_DATA_TYPE_BINARY
&&
para1Type
==
TSDB_DATA_TYPE_NCHAR
))
{
//
(para2Type == TSDB_DATA_TYPE_BINARY && para1Type == TSDB_DATA_TYPE_NCHAR)) {
return
invaildFuncParaTypeErrMsg
(
pErrBuf
,
len
,
pFunc
->
functionName
);
//
return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
}
//
}
int32_t
para2Bytes
=
pFunc
->
node
.
resType
.
bytes
;
int32_t
para2Bytes
=
pFunc
->
node
.
resType
.
bytes
;
if
(
IS_VAR_DATA_TYPE
(
para2Type
))
{
if
(
IS_VAR_DATA_TYPE
(
para2Type
))
{
...
...
source/libs/scalar/src/sclfunc.c
浏览文件 @
570b36cb
...
@@ -735,6 +735,60 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp
...
@@ -735,6 +735,60 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp
char
*
input
=
colDataGetData
(
pInput
[
0
].
columnData
,
i
);
char
*
input
=
colDataGetData
(
pInput
[
0
].
columnData
,
i
);
switch
(
outputType
)
{
switch
(
outputType
)
{
case
TSDB_DATA_TYPE_TINYINT
:
{
if
(
inputType
==
TSDB_DATA_TYPE_BINARY
)
{
*
(
int8_t
*
)
output
=
taosStr2Int8
(
varDataVal
(
input
),
NULL
,
10
);
}
else
if
(
inputType
==
TSDB_DATA_TYPE_NCHAR
)
{
char
*
newBuf
=
taosMemoryCalloc
(
1
,
inputLen
);
int32_t
len
=
taosUcs4ToMbs
((
TdUcs4
*
)
varDataVal
(
input
),
varDataLen
(
input
),
newBuf
);
if
(
len
<
0
)
{
taosMemoryFree
(
newBuf
);
return
TSDB_CODE_FAILED
;
}
newBuf
[
len
]
=
0
;
*
(
int8_t
*
)
output
=
taosStr2Int8
(
newBuf
,
NULL
,
10
);
taosMemoryFree
(
newBuf
);
}
else
{
GET_TYPED_DATA
(
*
(
int8_t
*
)
output
,
int8_t
,
inputType
,
input
);
}
break
;
}
case
TSDB_DATA_TYPE_SMALLINT
:
{
if
(
inputType
==
TSDB_DATA_TYPE_BINARY
)
{
*
(
int16_t
*
)
output
=
taosStr2Int16
(
varDataVal
(
input
),
NULL
,
10
);
}
else
if
(
inputType
==
TSDB_DATA_TYPE_NCHAR
)
{
char
*
newBuf
=
taosMemoryCalloc
(
1
,
inputLen
);
int32_t
len
=
taosUcs4ToMbs
((
TdUcs4
*
)
varDataVal
(
input
),
varDataLen
(
input
),
newBuf
);
if
(
len
<
0
)
{
taosMemoryFree
(
newBuf
);
return
TSDB_CODE_FAILED
;
}
newBuf
[
len
]
=
0
;
*
(
int16_t
*
)
output
=
taosStr2Int16
(
newBuf
,
NULL
,
10
);
taosMemoryFree
(
newBuf
);
}
else
{
GET_TYPED_DATA
(
*
(
int16_t
*
)
output
,
int16_t
,
inputType
,
input
);
}
break
;
}
case
TSDB_DATA_TYPE_INT
:
{
if
(
inputType
==
TSDB_DATA_TYPE_BINARY
)
{
*
(
int32_t
*
)
output
=
taosStr2Int32
(
varDataVal
(
input
),
NULL
,
10
);
}
else
if
(
inputType
==
TSDB_DATA_TYPE_NCHAR
)
{
char
*
newBuf
=
taosMemoryCalloc
(
1
,
inputLen
);
int32_t
len
=
taosUcs4ToMbs
((
TdUcs4
*
)
varDataVal
(
input
),
varDataLen
(
input
),
newBuf
);
if
(
len
<
0
)
{
taosMemoryFree
(
newBuf
);
return
TSDB_CODE_FAILED
;
}
newBuf
[
len
]
=
0
;
*
(
int32_t
*
)
output
=
taosStr2Int32
(
newBuf
,
NULL
,
10
);
taosMemoryFree
(
newBuf
);
}
else
{
GET_TYPED_DATA
(
*
(
int32_t
*
)
output
,
int32_t
,
inputType
,
input
);
}
break
;
}
case
TSDB_DATA_TYPE_BIGINT
:
{
case
TSDB_DATA_TYPE_BIGINT
:
{
if
(
inputType
==
TSDB_DATA_TYPE_BINARY
)
{
if
(
inputType
==
TSDB_DATA_TYPE_BINARY
)
{
*
(
int64_t
*
)
output
=
taosStr2Int64
(
varDataVal
(
input
),
NULL
,
10
);
*
(
int64_t
*
)
output
=
taosStr2Int64
(
varDataVal
(
input
),
NULL
,
10
);
...
@@ -753,6 +807,60 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp
...
@@ -753,6 +807,60 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp
}
}
break
;
break
;
}
}
case
TSDB_DATA_TYPE_UTINYINT
:
{
if
(
inputType
==
TSDB_DATA_TYPE_BINARY
)
{
*
(
uint8_t
*
)
output
=
taosStr2UInt8
(
varDataVal
(
input
),
NULL
,
10
);
}
else
if
(
inputType
==
TSDB_DATA_TYPE_NCHAR
)
{
char
*
newBuf
=
taosMemoryCalloc
(
1
,
inputLen
);
int32_t
len
=
taosUcs4ToMbs
((
TdUcs4
*
)
varDataVal
(
input
),
varDataLen
(
input
),
newBuf
);
if
(
len
<
0
)
{
taosMemoryFree
(
newBuf
);
return
TSDB_CODE_FAILED
;
}
newBuf
[
len
]
=
0
;
*
(
uint8_t
*
)
output
=
taosStr2UInt8
(
newBuf
,
NULL
,
10
);
taosMemoryFree
(
newBuf
);
}
else
{
GET_TYPED_DATA
(
*
(
uint8_t
*
)
output
,
uint8_t
,
inputType
,
input
);
}
break
;
}
case
TSDB_DATA_TYPE_USMALLINT
:
{
if
(
inputType
==
TSDB_DATA_TYPE_BINARY
)
{
*
(
uint16_t
*
)
output
=
taosStr2UInt16
(
varDataVal
(
input
),
NULL
,
10
);
}
else
if
(
inputType
==
TSDB_DATA_TYPE_NCHAR
)
{
char
*
newBuf
=
taosMemoryCalloc
(
1
,
inputLen
);
int32_t
len
=
taosUcs4ToMbs
((
TdUcs4
*
)
varDataVal
(
input
),
varDataLen
(
input
),
newBuf
);
if
(
len
<
0
)
{
taosMemoryFree
(
newBuf
);
return
TSDB_CODE_FAILED
;
}
newBuf
[
len
]
=
0
;
*
(
uint16_t
*
)
output
=
taosStr2UInt16
(
newBuf
,
NULL
,
10
);
taosMemoryFree
(
newBuf
);
}
else
{
GET_TYPED_DATA
(
*
(
uint16_t
*
)
output
,
uint16_t
,
inputType
,
input
);
}
break
;
}
case
TSDB_DATA_TYPE_UINT
:
{
if
(
inputType
==
TSDB_DATA_TYPE_BINARY
)
{
*
(
uint32_t
*
)
output
=
taosStr2UInt32
(
varDataVal
(
input
),
NULL
,
10
);
}
else
if
(
inputType
==
TSDB_DATA_TYPE_NCHAR
)
{
char
*
newBuf
=
taosMemoryCalloc
(
1
,
inputLen
);
int32_t
len
=
taosUcs4ToMbs
((
TdUcs4
*
)
varDataVal
(
input
),
varDataLen
(
input
),
newBuf
);
if
(
len
<
0
)
{
taosMemoryFree
(
newBuf
);
return
TSDB_CODE_FAILED
;
}
newBuf
[
len
]
=
0
;
*
(
uint32_t
*
)
output
=
taosStr2UInt32
(
newBuf
,
NULL
,
10
);
taosMemoryFree
(
newBuf
);
}
else
{
GET_TYPED_DATA
(
*
(
uint32_t
*
)
output
,
uint32_t
,
inputType
,
input
);
}
break
;
}
case
TSDB_DATA_TYPE_UBIGINT
:
{
case
TSDB_DATA_TYPE_UBIGINT
:
{
if
(
inputType
==
TSDB_DATA_TYPE_BINARY
)
{
if
(
inputType
==
TSDB_DATA_TYPE_BINARY
)
{
*
(
uint64_t
*
)
output
=
taosStr2UInt64
(
varDataVal
(
input
),
NULL
,
10
);
*
(
uint64_t
*
)
output
=
taosStr2UInt64
(
varDataVal
(
input
),
NULL
,
10
);
...
@@ -771,10 +879,64 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp
...
@@ -771,10 +879,64 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp
}
}
break
;
break
;
}
}
case
TSDB_DATA_TYPE_FLOAT
:
{
if
(
inputType
==
TSDB_DATA_TYPE_BINARY
)
{
*
(
float
*
)
output
=
taosStr2Float
(
varDataVal
(
input
),
NULL
);
}
else
if
(
inputType
==
TSDB_DATA_TYPE_NCHAR
)
{
char
*
newBuf
=
taosMemoryCalloc
(
1
,
inputLen
);
int32_t
len
=
taosUcs4ToMbs
((
TdUcs4
*
)
varDataVal
(
input
),
varDataLen
(
input
),
newBuf
);
if
(
len
<
0
)
{
taosMemoryFree
(
newBuf
);
return
TSDB_CODE_FAILED
;
}
newBuf
[
len
]
=
0
;
*
(
float
*
)
output
=
taosStr2Float
(
newBuf
,
NULL
);
taosMemoryFree
(
newBuf
);
}
else
{
GET_TYPED_DATA
(
*
(
float
*
)
output
,
float
,
inputType
,
input
);
}
break
;
}
case
TSDB_DATA_TYPE_DOUBLE
:
{
if
(
inputType
==
TSDB_DATA_TYPE_BINARY
)
{
*
(
double
*
)
output
=
taosStr2Double
(
varDataVal
(
input
),
NULL
);
}
else
if
(
inputType
==
TSDB_DATA_TYPE_NCHAR
)
{
char
*
newBuf
=
taosMemoryCalloc
(
1
,
inputLen
);
int32_t
len
=
taosUcs4ToMbs
((
TdUcs4
*
)
varDataVal
(
input
),
varDataLen
(
input
),
newBuf
);
if
(
len
<
0
)
{
taosMemoryFree
(
newBuf
);
return
TSDB_CODE_FAILED
;
}
newBuf
[
len
]
=
0
;
*
(
double
*
)
output
=
taosStr2Double
(
newBuf
,
NULL
);
taosMemoryFree
(
newBuf
);
}
else
{
GET_TYPED_DATA
(
*
(
double
*
)
output
,
double
,
inputType
,
input
);
}
break
;
}
case
TSDB_DATA_TYPE_BOOL
:
{
if
(
inputType
==
TSDB_DATA_TYPE_BINARY
)
{
*
(
bool
*
)
output
=
taosStr2Int8
(
varDataVal
(
input
),
NULL
,
10
);
}
else
if
(
inputType
==
TSDB_DATA_TYPE_NCHAR
)
{
char
*
newBuf
=
taosMemoryCalloc
(
1
,
inputLen
);
int32_t
len
=
taosUcs4ToMbs
((
TdUcs4
*
)
varDataVal
(
input
),
varDataLen
(
input
),
newBuf
);
if
(
len
<
0
)
{
taosMemoryFree
(
newBuf
);
return
TSDB_CODE_FAILED
;
}
newBuf
[
len
]
=
0
;
*
(
bool
*
)
output
=
taosStr2Int8
(
newBuf
,
NULL
,
10
);
taosMemoryFree
(
newBuf
);
}
else
{
GET_TYPED_DATA
(
*
(
bool
*
)
output
,
bool
,
inputType
,
input
);
}
break
;
}
case
TSDB_DATA_TYPE_TIMESTAMP
:
{
case
TSDB_DATA_TYPE_TIMESTAMP
:
{
if
(
inputType
==
TSDB_DATA_TYPE_BINARY
||
inputType
==
TSDB_DATA_TYPE_NCHAR
)
{
if
(
inputType
==
TSDB_DATA_TYPE_BINARY
||
inputType
==
TSDB_DATA_TYPE_NCHAR
)
{
//
not support
//
convert to 0
return
TSDB_CODE_FAILED
;
*
(
int64_t
*
)
output
=
0
;
}
else
{
}
else
{
GET_TYPED_DATA
(
*
(
int64_t
*
)
output
,
int64_t
,
inputType
,
input
);
GET_TYPED_DATA
(
*
(
int64_t
*
)
output
,
int64_t
,
inputType
,
input
);
}
}
...
@@ -789,8 +951,16 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp
...
@@ -789,8 +951,16 @@ int32_t castFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutp
len
=
sprintf
(
varDataVal
(
output
),
"%.*s"
,
len
,
varDataVal
(
input
));
len
=
sprintf
(
varDataVal
(
output
),
"%.*s"
,
len
,
varDataVal
(
input
));
varDataSetLen
(
output
,
len
);
varDataSetLen
(
output
,
len
);
}
else
if
(
inputType
==
TSDB_DATA_TYPE_NCHAR
)
{
}
else
if
(
inputType
==
TSDB_DATA_TYPE_NCHAR
)
{
//not support
char
*
newBuf
=
taosMemoryCalloc
(
1
,
inputLen
);
return
TSDB_CODE_FAILED
;
int32_t
len
=
taosUcs4ToMbs
((
TdUcs4
*
)
varDataVal
(
input
),
varDataLen
(
input
),
newBuf
);
if
(
len
<
0
)
{
taosMemoryFree
(
newBuf
);
return
TSDB_CODE_FAILED
;
}
len
=
TMIN
(
len
,
outputLen
-
VARSTR_HEADER_SIZE
);
memcpy
(
varDataVal
(
output
),
newBuf
,
len
);
varDataSetLen
(
output
,
len
);
taosMemoryFree
(
newBuf
);
}
else
{
}
else
{
char
tmp
[
400
]
=
{
0
};
char
tmp
[
400
]
=
{
0
};
NUM_TO_STRING
(
inputType
,
input
,
sizeof
(
tmp
),
tmp
);
NUM_TO_STRING
(
inputType
,
input
,
sizeof
(
tmp
),
tmp
);
...
...
tests/system-test/2-query/cast.py
浏览文件 @
570b36cb
...
@@ -630,27 +630,27 @@ class TDTestCase:
...
@@ -630,27 +630,27 @@ class TDTestCase:
(
tdSql
.
checkData
(
i
,
0
,
'12'
)
for
i
in
range
(
tdSql
.
queryRows
)
)
(
tdSql
.
checkData
(
i
,
0
,
'12'
)
for
i
in
range
(
tdSql
.
queryRows
)
)
tdLog
.
printNoPrefix
(
"==========step40: error cast condition, should return error "
)
tdLog
.
printNoPrefix
(
"==========step40: error cast condition, should return error "
)
tdSql
.
error
(
"select cast(c1 as int) as b from ct4"
)
#
tdSql.error("select cast(c1 as int) as b from ct4")
tdSql
.
error
(
"select cast(c1 as bool) as b from ct4"
)
#
tdSql.error("select cast(c1 as bool) as b from ct4")
tdSql
.
error
(
"select cast(c1 as tinyint) as b from ct4"
)
#
tdSql.error("select cast(c1 as tinyint) as b from ct4")
tdSql
.
error
(
"select cast(c1 as smallint) as b from ct4"
)
#
tdSql.error("select cast(c1 as smallint) as b from ct4")
tdSql
.
error
(
"select cast(c1 as float) as b from ct4"
)
#
tdSql.error("select cast(c1 as float) as b from ct4")
tdSql
.
error
(
"select cast(c1 as double) as b from ct4"
)
#
tdSql.error("select cast(c1 as double) as b from ct4")
tdSql
.
error
(
"select cast(c1 as tinyint unsigned) as b from ct4"
)
#
tdSql.error("select cast(c1 as tinyint unsigned) as b from ct4")
tdSql
.
error
(
"select cast(c1 as smallint unsigned) as b from ct4"
)
#
tdSql.error("select cast(c1 as smallint unsigned) as b from ct4")
tdSql
.
error
(
"select cast(c1 as int unsigned) as b from ct4"
)
#
tdSql.error("select cast(c1 as int unsigned) as b from ct4")
tdSql
.
error
(
"select cast(c2 as int) as b from ct4"
)
#
tdSql.error("select cast(c2 as int) as b from ct4")
tdSql
.
error
(
"select cast(c3 as bool) as b from ct4"
)
#
tdSql.error("select cast(c3 as bool) as b from ct4")
tdSql
.
error
(
"select cast(c4 as tinyint) as b from ct4"
)
#
tdSql.error("select cast(c4 as tinyint) as b from ct4")
tdSql
.
error
(
"select cast(c5 as smallint) as b from ct4"
)
#
tdSql.error("select cast(c5 as smallint) as b from ct4")
tdSql
.
error
(
"select cast(c6 as float) as b from ct4"
)
#
tdSql.error("select cast(c6 as float) as b from ct4")
tdSql
.
error
(
"select cast(c7 as double) as b from ct4"
)
#
tdSql.error("select cast(c7 as double) as b from ct4")
tdSql
.
error
(
"select cast(c8 as tinyint unsigned) as b from ct4"
)
#
tdSql.error("select cast(c8 as tinyint unsigned) as b from ct4")
tdSql
.
error
(
"select cast(c8 as timestamp ) as b from ct4"
)
#
tdSql.error("select cast(c8 as timestamp ) as b from ct4")
tdSql
.
error
(
"select cast(c9 as timestamp ) as b from ct4"
)
#
tdSql.error("select cast(c9 as timestamp ) as b from ct4")
tdSql
.
error
(
"select cast(c9 as binary(64) ) as b from ct4"
)
#
tdSql.error("select cast(c9 as binary(64) ) as b from ct4")
pass
pass
def
run
(
self
):
def
run
(
self
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录