Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
500ee304
T
TDengine
项目概览
taosdata
/
TDengine
大约 2 年 前同步成功
通知
1192
Star
22018
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看板
提交
500ee304
编写于
12月 15, 2022
作者:
A
Alex Duan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(query): avg sum over range replace with macro
上级
74cf7764
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
52 addition
and
102 deletion
+52
-102
source/libs/function/src/detail/tavgfunction.c
source/libs/function/src/detail/tavgfunction.c
+52
-102
未找到文件。
source/libs/function/src/detail/tavgfunction.c
浏览文件 @
500ee304
...
@@ -41,6 +41,31 @@
...
@@ -41,6 +41,31 @@
} \
} \
} while (0)
} while (0)
// define signed number sum with check overflow
#define CHECK_OVERFLOW_SUM_SIGNED(out, val) \
if (out->sum.overflow) { \
out->sum.dsum += val; \
} else if (out->sum.isum > 0 && val > 0 && INT64_MAX - out->sum.isum <= val || \
out->sum.isum < 0 && val < 0 && INT64_MIN - out->sum.isum >= val) { \
double dsum = (double)out->sum.isum; \
out->sum.overflow = true; \
out->sum.dsum = dsum + val; \
} else { \
out->sum.isum += val; \
}
// define unsigned number sum with check overflow
#define CHECK_OVERFLOW_SUM_UNSIGNED (out, val) \
if (out->sum.overflow) { \
out->sum.dsum += val; \
} else if (UINT64_MAX - out->sum.usum <= val) { \
double dsum = (double)out->sum.usum; \
out->sum.overflow = true; \
out->sum.dsum = dsum + val; \
} else { \
out->sum.usum += val; \
}
typedef
struct
SAvgRes
{
typedef
struct
SAvgRes
{
double
result
;
double
result
;
SSumRes
sum
;
SSumRes
sum
;
...
@@ -344,7 +369,7 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
...
@@ -344,7 +369,7 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
numOfElems
+=
1
;
numOfElems
+=
1
;
pRes
->
count
+=
1
;
pRes
->
count
+=
1
;
pRes
->
sum
.
isum
+=
plist
[
i
];
CHECK_OVERFLOW_SUM_SIGNED
(
pRes
,
plist
[
i
])
}
}
break
;
break
;
...
@@ -359,7 +384,7 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
...
@@ -359,7 +384,7 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
numOfElems
+=
1
;
numOfElems
+=
1
;
pRes
->
count
+=
1
;
pRes
->
count
+=
1
;
pRes
->
sum
.
isum
+=
plist
[
i
];
CHECK_OVERFLOW_SUM_SIGNED
(
pRes
,
plist
[
i
])
}
}
break
;
break
;
}
}
...
@@ -373,16 +398,7 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
...
@@ -373,16 +398,7 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
numOfElems
+=
1
;
numOfElems
+=
1
;
pRes
->
count
+=
1
;
pRes
->
count
+=
1
;
CHECK_OVERFLOW_SUM_SIGNED
(
pRes
,
plist
[
i
])
// check overflow for int
if
(
pRes
->
sum
.
isum
>
0
&&
plist
[
i
]
>
0
&&
INT64_MAX
-
pRes
->
sum
.
isum
<=
plist
[
i
]
||
pRes
->
sum
.
isum
<
0
&&
plist
[
i
]
<
0
&&
INT64_MIN
-
pRes
->
sum
.
isum
>=
plist
[
i
]
)
{
double
dsum
=
(
double
)
pRes
->
sum
.
isum
;
pRes
->
sum
.
overflow
=
true
;
pRes
->
sum
.
dsum
=
dsum
+
plist
[
i
];
}
else
{
pRes
->
sum
.
isum
+=
plist
[
i
];
}
}
}
break
;
break
;
...
@@ -397,16 +413,7 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
...
@@ -397,16 +413,7 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
numOfElems
+=
1
;
numOfElems
+=
1
;
pRes
->
count
+=
1
;
pRes
->
count
+=
1
;
CHECK_OVERFLOW_SUM_SIGNED
(
pRes
,
plist
[
i
])
// check overflow
if
(
pRes
->
sum
.
isum
>
0
&&
plist
[
i
]
>
0
&&
INT64_MAX
-
pRes
->
sum
.
isum
<=
plist
[
i
]
||
pRes
->
sum
.
isum
<
0
&&
plist
[
i
]
<
0
&&
INT64_MIN
-
pRes
->
sum
.
isum
>=
plist
[
i
]
)
{
double
dsum
=
(
double
)
pAvgRes
->
sum
.
isum
;
pRes
->
sum
.
overflow
=
true
;
pRes
->
sum
.
dsum
=
dsum
+
plist
[
i
];
}
else
{
pRes
->
sum
.
isum
+=
plist
[
i
];
}
}
}
break
;
break
;
}
}
...
@@ -420,7 +427,7 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
...
@@ -420,7 +427,7 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
numOfElems
+=
1
;
numOfElems
+=
1
;
pRes
->
count
+=
1
;
pRes
->
count
+=
1
;
pRes
->
sum
.
usum
+=
plist
[
i
];
CHECK_OVERFLOW_SUM_UNSIGNED
(
pRes
,
plist
[
i
])
}
}
break
;
break
;
...
@@ -435,7 +442,7 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
...
@@ -435,7 +442,7 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
numOfElems
+=
1
;
numOfElems
+=
1
;
pRes
->
count
+=
1
;
pRes
->
count
+=
1
;
pRes
->
sum
.
usum
+=
plist
[
i
];
CHECK_OVERFLOW_SUM_UNSIGNED
(
pRes
,
plist
[
i
])
}
}
break
;
break
;
}
}
...
@@ -449,16 +456,7 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
...
@@ -449,16 +456,7 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
numOfElems
+=
1
;
numOfElems
+=
1
;
pRes
->
count
+=
1
;
pRes
->
count
+=
1
;
CHECK_OVERFLOW_SUM_UNSIGNED
(
pRes
,
plist
[
i
])
// check overflow
if
(
UINT64_MAX
-
pRes
->
sum
.
usum
<=
plist
[
i
]
)
{
double
dsum
=
(
double
)
pRes
->
sum
.
isum
;
pRes
->
sum
.
overflow
=
true
;
pRes
->
sum
.
dsum
=
dsum
+
plist
[
i
];
}
else
{
pRes
->
sum
.
usum
+=
(
uint32_t
)
plist
[
i
];
}
}
}
break
;
break
;
...
@@ -473,15 +471,8 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
...
@@ -473,15 +471,8 @@ static int32_t doAddNumericVector(SColumnInfoData* pCol, int32_t type, SInputCol
numOfElems
+=
1
;
numOfElems
+=
1
;
pRes
->
count
+=
1
;
pRes
->
count
+=
1
;
CHECK_OVERFLOW_SUM_UNSIGNED
(
pRes
,
plist
[
i
])
// check overflow
if
(
UINT64_MAX
-
pRes
->
sum
.
usum
<=
plist
[
i
]
)
{
double
dsum
=
(
double
)
pRes
->
sum
.
isum
;
pRes
->
sum
.
overflow
=
true
;
pRes
->
sum
.
dsum
=
dsum
+
plist
[
i
];
}
else
{
pRes
->
sum
.
usum
+=
(
uint64_t
)
plist
[
i
];
}
}
}
break
;
break
;
}
}
...
@@ -563,8 +554,9 @@ int32_t avgFunction(SqlFunctionCtx* pCtx) {
...
@@ -563,8 +554,9 @@ int32_t avgFunction(SqlFunctionCtx* pCtx) {
for
(
int32_t
i
=
pInput
->
startRowIndex
;
i
<
pInput
->
numOfRows
+
pInput
->
startRowIndex
;
++
i
)
{
for
(
int32_t
i
=
pInput
->
startRowIndex
;
i
<
pInput
->
numOfRows
+
pInput
->
startRowIndex
;
++
i
)
{
if
(
type
==
TSDB_DATA_TYPE_TINYINT
)
{
if
(
type
==
TSDB_DATA_TYPE_TINYINT
)
{
pAvgRes
->
sum
.
isum
+=
plist
[
i
];
pAvgRes
->
sum
.
isum
+=
plist
[
i
];
CHECK_OVERFLOW_SUM_SIGNED
(
pAvgRes
,
plist
[
i
])
}
else
{
}
else
{
pAvgRes
->
sum
.
usum
+=
(
uint8_t
)
plist
[
i
];
CHECK_OVERFLOW_SUM_UNSIGNED
(
pAvgRes
,
(
uint8_t
)
plist
[
i
])
}
}
}
}
}
}
...
@@ -581,9 +573,9 @@ int32_t avgFunction(SqlFunctionCtx* pCtx) {
...
@@ -581,9 +573,9 @@ int32_t avgFunction(SqlFunctionCtx* pCtx) {
}
else
{
}
else
{
for
(
int32_t
i
=
pInput
->
startRowIndex
;
i
<
pInput
->
numOfRows
+
pInput
->
startRowIndex
;
++
i
)
{
for
(
int32_t
i
=
pInput
->
startRowIndex
;
i
<
pInput
->
numOfRows
+
pInput
->
startRowIndex
;
++
i
)
{
if
(
type
==
TSDB_DATA_TYPE_SMALLINT
)
{
if
(
type
==
TSDB_DATA_TYPE_SMALLINT
)
{
pAvgRes
->
sum
.
isum
+=
plist
[
i
];
CHECK_OVERFLOW_SUM_SIGNED
(
pAvgRes
,
plist
[
i
])
}
else
{
}
else
{
pAvgRes
->
sum
.
usum
+=
(
uint16_t
)
plist
[
i
];
CHECK_OVERFLOW_SUM_UNSIGNED
(
pAvgRes
,
(
uint16_t
)
plist
[
i
])
}
}
}
}
}
}
...
@@ -600,24 +592,9 @@ int32_t avgFunction(SqlFunctionCtx* pCtx) {
...
@@ -600,24 +592,9 @@ int32_t avgFunction(SqlFunctionCtx* pCtx) {
}
else
{
}
else
{
for
(
int32_t
i
=
pInput
->
startRowIndex
;
i
<
pInput
->
numOfRows
+
pInput
->
startRowIndex
;
++
i
)
{
for
(
int32_t
i
=
pInput
->
startRowIndex
;
i
<
pInput
->
numOfRows
+
pInput
->
startRowIndex
;
++
i
)
{
if
(
type
==
TSDB_DATA_TYPE_INT
)
{
if
(
type
==
TSDB_DATA_TYPE_INT
)
{
// check overflow for int
CHECK_OVERFLOW_SUM_SIGNED
(
pAvgRes
,
plist
[
i
])
if
(
pAvgRes
->
sum
.
isum
>
0
&&
plist
[
i
]
>
0
&&
INT64_MAX
-
pAvgRes
->
sum
.
isum
<=
plist
[
i
]
||
pAvgRes
->
sum
.
isum
<
0
&&
plist
[
i
]
<
0
&&
INT64_MIN
-
pAvgRes
->
sum
.
isum
>=
plist
[
i
]
)
{
double
dsum
=
(
double
)
pAvgRes
->
sum
.
isum
;
pAvgRes
->
sum
.
overflow
=
true
;
pAvgRes
->
sum
.
dsum
=
dsum
+
plist
[
i
];
}
else
{
pAvgRes
->
sum
.
isum
+=
plist
[
i
];
}
}
else
{
}
else
{
// check overflow for uint
CHECK_OVERFLOW_SUM_UNSIGNED
(
pAvgRes
,
(
uint32_t
)
plist
[
i
])
if
(
UINT64_MAX
-
pAvgRes
->
sum
.
usum
<=
plist
[
i
]
)
{
double
dsum
=
(
double
)
pAvgRes
->
sum
.
isum
;
pAvgRes
->
sum
.
overflow
=
true
;
pAvgRes
->
sum
.
dsum
=
dsum
+
plist
[
i
];
}
else
{
pAvgRes
->
sum
.
usum
+=
(
uint32_t
)
plist
[
i
];
}
}
}
}
}
}
}
...
@@ -634,24 +611,9 @@ int32_t avgFunction(SqlFunctionCtx* pCtx) {
...
@@ -634,24 +611,9 @@ int32_t avgFunction(SqlFunctionCtx* pCtx) {
}
else
{
}
else
{
for
(
int32_t
i
=
pInput
->
startRowIndex
;
i
<
pInput
->
numOfRows
+
pInput
->
startRowIndex
;
++
i
)
{
for
(
int32_t
i
=
pInput
->
startRowIndex
;
i
<
pInput
->
numOfRows
+
pInput
->
startRowIndex
;
++
i
)
{
if
(
type
==
TSDB_DATA_TYPE_BIGINT
)
{
if
(
type
==
TSDB_DATA_TYPE_BIGINT
)
{
// check overflow for int
CHECK_OVERFLOW_SUM_SIGNED
(
pAvgRes
,
plist
[
i
])
if
(
pAvgRes
->
sum
.
isum
>
0
&&
plist
[
i
]
>
0
&&
INT64_MAX
-
pAvgRes
->
sum
.
isum
<=
plist
[
i
]
||
\
}
else
{
pAvgRes
->
sum
.
isum
<
0
&&
plist
[
i
]
<
0
&&
INT64_MIN
-
pAvgRes
->
sum
.
isum
>=
plist
[
i
]
)
{
CHECK_OVERFLOW_SUM_UNSIGNED
(
pAvgRes
,
(
uint64_t
)
plist
[
i
])
double
dsum
=
(
double
)
pAvgRes
->
sum
.
isum
;
pAvgRes
->
sum
.
overflow
=
true
;
pAvgRes
->
sum
.
dsum
=
dsum
+
plist
[
i
];
}
else
{
pAvgRes
->
sum
.
isum
+=
plist
[
i
];
}
}
else
{
// check overflow for uint
if
(
UINT64_MAX
-
pAvgRes
->
sum
.
usum
<=
plist
[
i
]
)
{
double
dsum
=
(
double
)
pAvgRes
->
sum
.
isum
;
pAvgRes
->
sum
.
overflow
=
true
;
pAvgRes
->
sum
.
dsum
=
dsum
+
plist
[
i
];
}
else
{
pAvgRes
->
sum
.
usum
+=
(
uint64_t
)
plist
[
i
];
}
}
}
}
}
}
}
...
@@ -697,6 +659,8 @@ _over:
...
@@ -697,6 +659,8 @@ _over:
return
TSDB_CODE_SUCCESS
;
return
TSDB_CODE_SUCCESS
;
}
}
static
void
avgTransferInfo
(
SAvgRes
*
pInput
,
SAvgRes
*
pOutput
)
{
static
void
avgTransferInfo
(
SAvgRes
*
pInput
,
SAvgRes
*
pOutput
)
{
if
(
IS_NULL_TYPE
(
pInput
->
type
))
{
if
(
IS_NULL_TYPE
(
pInput
->
type
))
{
return
;
return
;
...
@@ -704,26 +668,9 @@ static void avgTransferInfo(SAvgRes* pInput, SAvgRes* pOutput) {
...
@@ -704,26 +668,9 @@ static void avgTransferInfo(SAvgRes* pInput, SAvgRes* pOutput) {
pOutput
->
type
=
pInput
->
type
;
pOutput
->
type
=
pInput
->
type
;
if
(
IS_SIGNED_NUMERIC_TYPE
(
pOutput
->
type
))
{
if
(
IS_SIGNED_NUMERIC_TYPE
(
pOutput
->
type
))
{
int64_t
val
=
pInput
->
sum
.
isum
;
CHECK_OVERFLOW_SUM_SIGNED
(
pOutput
,
pInput
->
sum
.
isum
)
// check overflow
if
(
pOutput
->
sum
.
isum
>
0
&&
val
>
0
&&
INT64_MAX
-
pOutput
->
sum
.
isum
<=
val
||
pOutput
->
sum
.
isum
<
0
&&
val
<
0
&&
INT64_MIN
-
pOutput
->
sum
.
isum
>=
val
)
{
double
dsum
=
(
double
)
pOutput
->
sum
.
isum
;
pOutput
->
sum
.
overflow
=
true
;
pOutput
->
sum
.
dsum
=
dsum
+
val
;
}
else
{
pOutput
->
sum
.
isum
+=
val
;
}
}
else
if
(
IS_UNSIGNED_NUMERIC_TYPE
(
pOutput
->
type
))
{
}
else
if
(
IS_UNSIGNED_NUMERIC_TYPE
(
pOutput
->
type
))
{
// check overflow
CHECK_OVERFLOW_SUM_UNSIGNED
(
pOutput
,
pInput
->
sum
.
usum
)
uint64_t
val
=
pInput
->
sum
.
usum
;
if
(
UINT64_MAX
-
pOutput
->
sum
.
usum
<=
val
)
{
double
dsum
=
(
double
)
pOutput
->
sum
.
isum
;
pOutput
->
sum
.
overflow
=
true
;
pOutput
->
sum
.
dsum
=
dsum
+
val
;
}
else
{
pOutput
->
sum
.
usum
+=
val
;
}
}
else
{
}
else
{
pOutput
->
sum
.
dsum
+=
pInput
->
sum
.
dsum
;
pOutput
->
sum
.
dsum
+=
pInput
->
sum
.
dsum
;
}
}
...
@@ -823,9 +770,9 @@ int32_t avgCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
...
@@ -823,9 +770,9 @@ int32_t avgCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) {
int16_t
type
=
pDBuf
->
type
==
TSDB_DATA_TYPE_NULL
?
pSBuf
->
type
:
pDBuf
->
type
;
int16_t
type
=
pDBuf
->
type
==
TSDB_DATA_TYPE_NULL
?
pSBuf
->
type
:
pDBuf
->
type
;
if
(
IS_SIGNED_NUMERIC_TYPE
(
type
))
{
if
(
IS_SIGNED_NUMERIC_TYPE
(
type
))
{
pDBuf
->
sum
.
isum
+=
pSBuf
->
sum
.
isum
;
CHECK_OVERFLOW_SUM_SIGNED
(
pDBuf
,
pSBuf
->
sum
.
isum
)
}
else
if
(
IS_UNSIGNED_NUMERIC_TYPE
(
type
))
{
}
else
if
(
IS_UNSIGNED_NUMERIC_TYPE
(
type
))
{
pDBuf
->
sum
.
usum
+=
pSBuf
->
sum
.
usum
;
CHECK_OVERFLOW_SUM_UNSIGNED
(
pDBuf
,
pSBuf
->
sum
.
usum
)
}
else
{
}
else
{
pDBuf
->
sum
.
dsum
+=
pSBuf
->
sum
.
dsum
;
pDBuf
->
sum
.
dsum
+=
pSBuf
->
sum
.
dsum
;
}
}
...
@@ -841,7 +788,10 @@ int32_t avgFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
...
@@ -841,7 +788,10 @@ int32_t avgFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
int32_t
type
=
pRes
->
type
;
int32_t
type
=
pRes
->
type
;
if
(
pRes
->
count
>
0
)
{
if
(
pRes
->
count
>
0
)
{
if
(
IS_SIGNED_NUMERIC_TYPE
(
type
))
{
if
(
pRes
->
sum
.
overflow
)
{
// overflow flag set , use dsum
pRes
->
result
=
pRes
->
sum
.
dsum
/
((
double
)
pRes
->
count
);
}
else
if
(
IS_SIGNED_NUMERIC_TYPE
(
type
))
{
pRes
->
result
=
pRes
->
sum
.
isum
/
((
double
)
pRes
->
count
);
pRes
->
result
=
pRes
->
sum
.
isum
/
((
double
)
pRes
->
count
);
}
else
if
(
IS_UNSIGNED_NUMERIC_TYPE
(
type
))
{
}
else
if
(
IS_UNSIGNED_NUMERIC_TYPE
(
type
))
{
pRes
->
result
=
pRes
->
sum
.
usum
/
((
double
)
pRes
->
count
);
pRes
->
result
=
pRes
->
sum
.
usum
/
((
double
)
pRes
->
count
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录