Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
1aeb15cb
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
1aeb15cb
编写于
7月 04, 2023
作者:
D
dapan1121
提交者:
GitHub
7月 04, 2023
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #21944 from taosdata/fix/TD-25098
fix: remove order logic in diff function
上级
589636f2
bf3a1991
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
68 addition
and
75 deletion
+68
-75
source/libs/function/src/builtinsimpl.c
source/libs/function/src/builtinsimpl.c
+33
-74
tests/system-test/2-query/diff.py
tests/system-test/2-query/diff.py
+35
-1
未找到文件。
source/libs/function/src/builtinsimpl.c
浏览文件 @
1aeb15cb
...
...
@@ -2702,13 +2702,12 @@ static int32_t doSetPrevVal(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
}
static
int32_t
doHandleDiff
(
SDiffInfo
*
pDiffInfo
,
int32_t
type
,
const
char
*
pv
,
SColumnInfoData
*
pOutput
,
int32_t
pos
,
int32_t
order
,
int64_t
ts
)
{
int32_t
factor
=
(
order
==
TSDB_ORDER_ASC
)
?
1
:
-
1
;
int64_t
ts
)
{
pDiffInfo
->
prevTs
=
ts
;
switch
(
type
)
{
case
TSDB_DATA_TYPE_INT
:
{
int32_t
v
=
*
(
int32_t
*
)
pv
;
int64_t
delta
=
factor
*
(
v
-
pDiffInfo
->
prev
.
i64
)
;
// direct previous may be null
int64_t
delta
=
v
-
pDiffInfo
->
prev
.
i64
;
// direct previous may be null
if
(
delta
<
0
&&
pDiffInfo
->
ignoreNegative
)
{
colDataSetNull_f_s
(
pOutput
,
pos
);
}
else
{
...
...
@@ -2721,7 +2720,7 @@ static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
case
TSDB_DATA_TYPE_BOOL
:
case
TSDB_DATA_TYPE_TINYINT
:
{
int8_t
v
=
*
(
int8_t
*
)
pv
;
int64_t
delta
=
factor
*
(
v
-
pDiffInfo
->
prev
.
i64
)
;
// direct previous may be null
int64_t
delta
=
v
-
pDiffInfo
->
prev
.
i64
;
// direct previous may be null
if
(
delta
<
0
&&
pDiffInfo
->
ignoreNegative
)
{
colDataSetNull_f_s
(
pOutput
,
pos
);
}
else
{
...
...
@@ -2732,7 +2731,7 @@ static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
}
case
TSDB_DATA_TYPE_SMALLINT
:
{
int16_t
v
=
*
(
int16_t
*
)
pv
;
int64_t
delta
=
factor
*
(
v
-
pDiffInfo
->
prev
.
i64
)
;
// direct previous may be null
int64_t
delta
=
v
-
pDiffInfo
->
prev
.
i64
;
// direct previous may be null
if
(
delta
<
0
&&
pDiffInfo
->
ignoreNegative
)
{
colDataSetNull_f_s
(
pOutput
,
pos
);
}
else
{
...
...
@@ -2744,7 +2743,7 @@ static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
case
TSDB_DATA_TYPE_TIMESTAMP
:
case
TSDB_DATA_TYPE_BIGINT
:
{
int64_t
v
=
*
(
int64_t
*
)
pv
;
int64_t
delta
=
factor
*
(
v
-
pDiffInfo
->
prev
.
i64
)
;
// direct previous may be null
int64_t
delta
=
v
-
pDiffInfo
->
prev
.
i64
;
// direct previous may be null
if
(
delta
<
0
&&
pDiffInfo
->
ignoreNegative
)
{
colDataSetNull_f_s
(
pOutput
,
pos
);
}
else
{
...
...
@@ -2755,7 +2754,7 @@ static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
}
case
TSDB_DATA_TYPE_FLOAT
:
{
float
v
=
*
(
float
*
)
pv
;
double
delta
=
factor
*
(
v
-
pDiffInfo
->
prev
.
d64
);
// direct previous may be null
double
delta
=
v
-
pDiffInfo
->
prev
.
d64
;
// direct previous may be null
if
((
delta
<
0
&&
pDiffInfo
->
ignoreNegative
)
||
isinf
(
delta
)
||
isnan
(
delta
))
{
// check for overflow
colDataSetNull_f_s
(
pOutput
,
pos
);
}
else
{
...
...
@@ -2766,7 +2765,7 @@ static int32_t doHandleDiff(SDiffInfo* pDiffInfo, int32_t type, const char* pv,
}
case
TSDB_DATA_TYPE_DOUBLE
:
{
double
v
=
*
(
double
*
)
pv
;
double
delta
=
factor
*
(
v
-
pDiffInfo
->
prev
.
d64
);
// direct previous may be null
double
delta
=
v
-
pDiffInfo
->
prev
.
d64
;
// direct previous may be null
if
((
delta
<
0
&&
pDiffInfo
->
ignoreNegative
)
||
isinf
(
delta
)
||
isnan
(
delta
))
{
// check for overflow
colDataSetNull_f_s
(
pOutput
,
pos
);
}
else
{
...
...
@@ -2797,82 +2796,42 @@ int32_t diffFunction(SqlFunctionCtx* pCtx) {
SColumnInfoData
*
pOutput
=
(
SColumnInfoData
*
)
pCtx
->
pOutput
;
if
(
pCtx
->
order
==
TSDB_ORDER_ASC
)
{
for
(
int32_t
i
=
pInput
->
startRowIndex
;
i
<
pInput
->
numOfRows
+
pInput
->
startRowIndex
;
i
+=
1
)
{
int32_t
pos
=
startOffset
+
numOfElems
;
for
(
int32_t
i
=
pInput
->
startRowIndex
;
i
<
pInput
->
numOfRows
+
pInput
->
startRowIndex
;
i
+=
1
)
{
int32_t
pos
=
startOffset
+
numOfElems
;
if
(
colDataIsNull_f
(
pInputCol
->
nullbitmap
,
i
))
{
if
(
pDiffInfo
->
includeNull
)
{
colDataSetNull_f_s
(
pOutput
,
pos
);
if
(
colDataIsNull_f
(
pInputCol
->
nullbitmap
,
i
))
{
if
(
pDiffInfo
->
includeNull
)
{
colDataSetNull_f_s
(
pOutput
,
pos
);
numOfElems
+=
1
;
}
continue
;
numOfElems
+=
1
;
}
continue
;
}
char
*
pv
=
colDataGetData
(
pInputCol
,
i
);
char
*
pv
=
colDataGetData
(
pInputCol
,
i
);
if
(
pDiffInfo
->
hasPrev
)
{
if
(
tsList
[
i
]
==
pDiffInfo
->
prevTs
)
{
return
TSDB_CODE_FUNC_DUP_TIMESTAMP
;
}
int32_t
code
=
doHandleDiff
(
pDiffInfo
,
pInputCol
->
info
.
type
,
pv
,
pOutput
,
pos
,
pCtx
->
order
,
tsList
[
i
]);
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
return
code
;
}
// handle selectivity
if
(
pCtx
->
subsidiaries
.
num
>
0
)
{
appendSelectivityValue
(
pCtx
,
i
,
pos
);
}
numOfElems
++
;
}
else
{
int32_t
code
=
doSetPrevVal
(
pDiffInfo
,
pInputCol
->
info
.
type
,
pv
,
tsList
[
i
]);
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
return
code
;
}
if
(
pDiffInfo
->
hasPrev
)
{
if
(
tsList
[
i
]
==
pDiffInfo
->
prevTs
)
{
return
TSDB_CODE_FUNC_DUP_TIMESTAMP
;
}
pDiffInfo
->
hasPrev
=
true
;
}
}
else
{
for
(
int32_t
i
=
pInput
->
startRowIndex
;
i
<
pInput
->
numOfRows
+
pInput
->
startRowIndex
;
i
+=
1
)
{
int32_t
pos
=
startOffset
+
numOfElems
;
if
(
colDataIsNull_f
(
pInputCol
->
nullbitmap
,
i
))
{
if
(
pDiffInfo
->
includeNull
)
{
colDataSetNull_f_s
(
pOutput
,
pos
);
numOfElems
+=
1
;
}
continue
;
int32_t
code
=
doHandleDiff
(
pDiffInfo
,
pInputCol
->
info
.
type
,
pv
,
pOutput
,
pos
,
tsList
[
i
]);
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
return
code
;
}
char
*
pv
=
colDataGetData
(
pInputCol
,
i
);
// there is a row of previous data block to be handled in the first place.
if
(
pDiffInfo
->
hasPrev
)
{
if
(
tsList
[
i
]
==
pDiffInfo
->
prevTs
)
{
return
TSDB_CODE_FUNC_DUP_TIMESTAMP
;
}
int32_t
code
=
doHandleDiff
(
pDiffInfo
,
pInputCol
->
info
.
type
,
pv
,
pOutput
,
pos
,
pCtx
->
order
,
tsList
[
i
]);
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
return
code
;
}
// handle selectivity
if
(
pCtx
->
subsidiaries
.
num
>
0
)
{
appendSelectivityValue
(
pCtx
,
i
,
pos
);
}
numOfElems
++
;
}
else
{
int32_t
code
=
doSetPrevVal
(
pDiffInfo
,
pInputCol
->
info
.
type
,
pv
,
tsList
[
i
]);
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
return
code
;
}
// handle selectivity
if
(
pCtx
->
subsidiaries
.
num
>
0
)
{
appendSelectivityValue
(
pCtx
,
i
,
pos
);
}
pDiffInfo
->
hasPrev
=
true
;
numOfElems
++
;
}
else
{
int32_t
code
=
doSetPrevVal
(
pDiffInfo
,
pInputCol
->
info
.
type
,
pv
,
tsList
[
i
]);
if
(
code
!=
TSDB_CODE_SUCCESS
)
{
return
code
;
}
}
pDiffInfo
->
hasPrev
=
true
;
}
pResInfo
->
numOfRes
=
numOfElems
;
...
...
tests/system-test/2-query/diff.py
浏览文件 @
1aeb15cb
...
...
@@ -23,7 +23,7 @@ class TDTestCase:
tdSql
.
execute
(
f
"create table
{
dbname
}
.ntb(ts timestamp,c1 int,c2 double,c3 float)"
)
tdSql
.
execute
(
f
"insert into
{
dbname
}
.ntb values(now,1,1.0,10.5)(now+1s,10,-100.0,5.1)(now+10s
,-1,15.1,5.0)"
)
f
"insert into
{
dbname
}
.ntb values('2023-01-01 00:00:01',1,1.0,10.5)('2023-01-01 00:00:02',10,-100.0,5.1)('2023-01-01 00:00:03'
,-1,15.1,5.0)"
)
tdSql
.
query
(
f
"select diff(c1,0) from
{
dbname
}
.ntb"
)
tdSql
.
checkRows
(
2
)
...
...
@@ -233,6 +233,40 @@ class TDTestCase:
tdSql
.
checkRows
(
19
)
tdSql
.
checkData
(
0
,
0
,
None
)
# TD-25098
tdSql
.
query
(
f
"select ts, diff(c1) from
{
dbname
}
.ntb order by ts"
)
tdSql
.
checkRows
(
2
)
tdSql
.
checkData
(
0
,
0
,
'2023-01-01 00:00:02.000'
)
tdSql
.
checkData
(
1
,
0
,
'2023-01-01 00:00:03.000'
)
tdSql
.
checkData
(
0
,
1
,
9
)
tdSql
.
checkData
(
1
,
1
,
-
11
)
tdSql
.
query
(
f
"select ts, diff(c1) from
{
dbname
}
.ntb order by ts desc"
)
tdSql
.
checkRows
(
2
)
tdSql
.
checkData
(
0
,
0
,
'2023-01-01 00:00:03.000'
)
tdSql
.
checkData
(
1
,
0
,
'2023-01-01 00:00:02.000'
)
tdSql
.
checkData
(
0
,
1
,
-
11
)
tdSql
.
checkData
(
1
,
1
,
9
)
tdSql
.
query
(
f
"select ts, diff(c1) from (select * from
{
dbname
}
.ntb order by ts)"
)
tdSql
.
checkRows
(
2
)
tdSql
.
checkData
(
0
,
0
,
'2023-01-01 00:00:02.000'
)
tdSql
.
checkData
(
1
,
0
,
'2023-01-01 00:00:03.000'
)
tdSql
.
checkData
(
0
,
1
,
9
)
tdSql
.
checkData
(
1
,
1
,
-
11
)
tdSql
.
query
(
f
"select ts, diff(c1) from (select * from
{
dbname
}
.ntb order by ts desc)"
)
tdSql
.
checkRows
(
2
)
tdSql
.
checkData
(
0
,
0
,
'2023-01-01 00:00:02.000'
)
tdSql
.
checkData
(
1
,
0
,
'2023-01-01 00:00:01.000'
)
tdSql
.
checkData
(
0
,
1
,
11
)
tdSql
.
checkData
(
1
,
1
,
-
9
)
def
stop
(
self
):
tdSql
.
close
()
tdLog
.
success
(
"%s successfully executed"
%
__file__
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录