Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2dot5
ClickHouse
提交
85396fc8
C
ClickHouse
项目概览
2dot5
/
ClickHouse
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
ClickHouse
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
85396fc8
编写于
2月 10, 2021
作者:
M
Maksim Kita
提交者:
GitHub
2月 10, 2021
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #20057 from rf/rf/add-deltasum
Add `deltaSum` aggregate function, docs & test
上级
9179fc8d
14c7f38a
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
218 addition
and
0 deletion
+218
-0
docs/en/sql-reference/aggregate-functions/reference/deltasum.md
...n/sql-reference/aggregate-functions/reference/deltasum.md
+19
-0
src/AggregateFunctions/AggregateFunctionDeltaSum.cpp
src/AggregateFunctions/AggregateFunctionDeltaSum.cpp
+49
-0
src/AggregateFunctions/AggregateFunctionDeltaSum.h
src/AggregateFunctions/AggregateFunctionDeltaSum.h
+129
-0
src/AggregateFunctions/registerAggregateFunctions.cpp
src/AggregateFunctions/registerAggregateFunctions.cpp
+2
-0
src/AggregateFunctions/ya.make
src/AggregateFunctions/ya.make
+1
-0
tests/queries/0_stateless/01700_deltasum.reference
tests/queries/0_stateless/01700_deltasum.reference
+9
-0
tests/queries/0_stateless/01700_deltasum.sql
tests/queries/0_stateless/01700_deltasum.sql
+9
-0
未找到文件。
docs/en/sql-reference/aggregate-functions/reference/deltasum.md
0 → 100644
浏览文件 @
85396fc8
---
toc_priority
:
141
---
# deltaSum {#agg_functions-deltasum}
Syntax:
`deltaSum(value)`
Adds the differences between consecutive rows. If the difference is negative, it is ignored.
`value`
must be some integer or floating point type.
Example:
```
sql
select
deltaSum
(
arrayJoin
([
1
,
2
,
3
]));
-- => 2
select
deltaSum
(
arrayJoin
([
1
,
2
,
3
,
0
,
3
,
4
,
2
,
3
]));
-- => 7
select
deltaSum
(
arrayJoin
([
2
.
25
,
3
,
4
.
5
]));
-- => 2.25
```
src/AggregateFunctions/AggregateFunctionDeltaSum.cpp
0 → 100644
浏览文件 @
85396fc8
#include <AggregateFunctions/AggregateFunctionDeltaSum.h>
#include <AggregateFunctions/AggregateFunctionFactory.h>
#include <AggregateFunctions/FactoryHelpers.h>
#include <AggregateFunctions/Helpers.h>
namespace
DB
{
namespace
ErrorCodes
{
extern
const
int
NUMBER_OF_ARGUMENTS_DOESNT_MATCH
;
extern
const
int
ILLEGAL_TYPE_OF_ARGUMENT
;
}
namespace
{
AggregateFunctionPtr
createAggregateFunctionDeltaSum
(
const
String
&
name
,
const
DataTypes
&
arguments
,
const
Array
&
params
)
{
assertNoParameters
(
name
,
params
);
if
(
arguments
.
size
()
!=
1
)
throw
Exception
(
"Incorrect number of arguments for aggregate function "
+
name
,
ErrorCodes
::
NUMBER_OF_ARGUMENTS_DOESNT_MATCH
);
DataTypePtr
data_type
=
arguments
[
0
];
if
(
isInteger
(
data_type
)
||
isFloat
(
data_type
))
return
AggregateFunctionPtr
(
createWithNumericType
<
AggregationFunctionDeltaSum
>
(
*
data_type
,
arguments
,
params
));
else
throw
Exception
(
"Illegal type "
+
arguments
[
0
]
->
getName
()
+
" of argument for aggregate function "
+
name
,
ErrorCodes
::
ILLEGAL_TYPE_OF_ARGUMENT
);
}
}
void
registerAggregateFunctionDeltaSum
(
AggregateFunctionFactory
&
factory
)
{
AggregateFunctionProperties
properties
=
{
.
returns_default_when_only_null
=
true
,
.
is_order_dependent
=
true
};
factory
.
registerFunction
(
"deltaSum"
,
{
createAggregateFunctionDeltaSum
,
properties
});
}
}
src/AggregateFunctions/AggregateFunctionDeltaSum.h
0 → 100644
浏览文件 @
85396fc8
#pragma once
#include <type_traits>
#include <experimental/type_traits>
#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>
#include <Columns/ColumnVector.h>
#include <DataTypes/DataTypesDecimal.h>
#include <DataTypes/DataTypesNumber.h>
#include <AggregateFunctions/IAggregateFunction.h>
namespace
DB
{
template
<
typename
T
>
struct
AggregationFunctionDeltaSumData
{
T
sum
=
0
;
T
last
=
0
;
T
first
=
0
;
bool
seen_last
=
false
;
bool
seen_first
=
false
;
};
template
<
typename
T
>
class
AggregationFunctionDeltaSum
final
:
public
IAggregateFunctionDataHelper
<
AggregationFunctionDeltaSumData
<
T
>
,
AggregationFunctionDeltaSum
<
T
>>
{
public:
AggregationFunctionDeltaSum
(
const
DataTypes
&
arguments
,
const
Array
&
params
)
:
IAggregateFunctionDataHelper
<
AggregationFunctionDeltaSumData
<
T
>
,
AggregationFunctionDeltaSum
<
T
>>
{
arguments
,
params
}
{}
AggregationFunctionDeltaSum
()
:
IAggregateFunctionDataHelper
<
AggregationFunctionDeltaSumData
<
T
>
,
AggregationFunctionDeltaSum
<
T
>>
{}
{}
String
getName
()
const
override
{
return
"deltaSum"
;
}
DataTypePtr
getReturnType
()
const
override
{
return
std
::
make_shared
<
DataTypeNumber
<
T
>>
();
}
void
NO_SANITIZE_UNDEFINED
ALWAYS_INLINE
add
(
AggregateDataPtr
place
,
const
IColumn
**
columns
,
size_t
row_num
,
Arena
*
)
const
override
{
auto
value
=
assert_cast
<
const
ColumnVector
<
T
>
&>
(
*
columns
[
0
]).
getData
()[
row_num
];
if
((
this
->
data
(
place
).
last
<
value
)
&&
this
->
data
(
place
).
seen_last
)
{
this
->
data
(
place
).
sum
+=
(
value
-
this
->
data
(
place
).
last
);
}
this
->
data
(
place
).
last
=
value
;
this
->
data
(
place
).
seen_last
=
true
;
if
(
!
this
->
data
(
place
).
seen_first
)
{
this
->
data
(
place
).
first
=
value
;
this
->
data
(
place
).
seen_first
=
true
;
}
}
void
NO_SANITIZE_UNDEFINED
ALWAYS_INLINE
merge
(
AggregateDataPtr
place
,
ConstAggregateDataPtr
rhs
,
Arena
*
)
const
override
{
auto
place_data
=
&
this
->
data
(
place
);
auto
rhs_data
=
&
this
->
data
(
rhs
);
if
((
place_data
->
last
<
rhs_data
->
first
)
&&
place_data
->
seen_last
&&
rhs_data
->
seen_first
)
{
// If the lhs last number seen is less than the first number the rhs saw, the lhs is before
// the rhs, for example [0, 2] [4, 7]. So we want to add the deltasums, but also add the
// difference between lhs last number and rhs first number (the 2 and 4). Then we want to
// take last value from the rhs, so first and last become 0 and 7.
place_data
->
sum
+=
rhs_data
->
sum
+
(
rhs_data
->
first
-
place_data
->
last
);
place_data
->
last
=
rhs_data
->
last
;
}
else
if
((
rhs_data
->
last
<
place_data
->
first
&&
rhs_data
->
seen_last
&&
place_data
->
seen_first
))
{
// In the opposite scenario, the lhs comes after the rhs, e.g. [4, 6] [1, 2]. Since we
// assume the input interval states are sorted by time, we assume this is a counter
// reset, and therefore do *not* add the difference between our first value and the
// rhs last value.
place_data
->
sum
+=
rhs_data
->
sum
;
place_data
->
first
=
rhs_data
->
first
;
}
else
if
(
rhs_data
->
seen_first
)
{
// If we're here then the lhs is an empty state and the rhs does have some state, so
// we'll just take that state.
place_data
->
first
=
rhs_data
->
first
;
place_data
->
seen_first
=
rhs_data
->
seen_first
;
place_data
->
last
=
rhs_data
->
last
;
place_data
->
seen_last
=
rhs_data
->
seen_last
;
place_data
->
sum
=
rhs_data
->
sum
;
}
// Otherwise lhs either has data or is uninitialized, so we don't need to modify its values.
}
void
serialize
(
ConstAggregateDataPtr
place
,
WriteBuffer
&
buf
)
const
override
{
writeIntBinary
(
this
->
data
(
place
).
sum
,
buf
);
writeIntBinary
(
this
->
data
(
place
).
first
,
buf
);
writeIntBinary
(
this
->
data
(
place
).
last
,
buf
);
writePODBinary
<
bool
>
(
this
->
data
(
place
).
seen_first
,
buf
);
writePODBinary
<
bool
>
(
this
->
data
(
place
).
seen_last
,
buf
);
}
void
deserialize
(
AggregateDataPtr
place
,
ReadBuffer
&
buf
,
Arena
*
)
const
override
{
readIntBinary
(
this
->
data
(
place
).
sum
,
buf
);
readIntBinary
(
this
->
data
(
place
).
first
,
buf
);
readIntBinary
(
this
->
data
(
place
).
last
,
buf
);
readPODBinary
<
bool
>
(
this
->
data
(
place
).
seen_first
,
buf
);
readPODBinary
<
bool
>
(
this
->
data
(
place
).
seen_last
,
buf
);
}
void
insertResultInto
(
AggregateDataPtr
place
,
IColumn
&
to
,
Arena
*
)
const
override
{
assert_cast
<
ColumnVector
<
T
>
&>
(
to
).
getData
().
push_back
(
this
->
data
(
place
).
sum
);
}
};
}
src/AggregateFunctions/registerAggregateFunctions.cpp
浏览文件 @
85396fc8
...
@@ -11,6 +11,7 @@ class AggregateFunctionFactory;
...
@@ -11,6 +11,7 @@ class AggregateFunctionFactory;
void
registerAggregateFunctionAvg
(
AggregateFunctionFactory
&
);
void
registerAggregateFunctionAvg
(
AggregateFunctionFactory
&
);
void
registerAggregateFunctionAvgWeighted
(
AggregateFunctionFactory
&
);
void
registerAggregateFunctionAvgWeighted
(
AggregateFunctionFactory
&
);
void
registerAggregateFunctionCount
(
AggregateFunctionFactory
&
);
void
registerAggregateFunctionCount
(
AggregateFunctionFactory
&
);
void
registerAggregateFunctionDeltaSum
(
AggregateFunctionFactory
&
);
void
registerAggregateFunctionGroupArray
(
AggregateFunctionFactory
&
);
void
registerAggregateFunctionGroupArray
(
AggregateFunctionFactory
&
);
void
registerAggregateFunctionGroupUniqArray
(
AggregateFunctionFactory
&
);
void
registerAggregateFunctionGroupUniqArray
(
AggregateFunctionFactory
&
);
void
registerAggregateFunctionGroupArrayInsertAt
(
AggregateFunctionFactory
&
);
void
registerAggregateFunctionGroupArrayInsertAt
(
AggregateFunctionFactory
&
);
...
@@ -66,6 +67,7 @@ void registerAggregateFunctions()
...
@@ -66,6 +67,7 @@ void registerAggregateFunctions()
registerAggregateFunctionAvg
(
factory
);
registerAggregateFunctionAvg
(
factory
);
registerAggregateFunctionAvgWeighted
(
factory
);
registerAggregateFunctionAvgWeighted
(
factory
);
registerAggregateFunctionCount
(
factory
);
registerAggregateFunctionCount
(
factory
);
registerAggregateFunctionDeltaSum
(
factory
);
registerAggregateFunctionGroupArray
(
factory
);
registerAggregateFunctionGroupArray
(
factory
);
registerAggregateFunctionGroupUniqArray
(
factory
);
registerAggregateFunctionGroupUniqArray
(
factory
);
registerAggregateFunctionGroupArrayInsertAt
(
factory
);
registerAggregateFunctionGroupArrayInsertAt
(
factory
);
...
...
src/AggregateFunctions/ya.make
浏览文件 @
85396fc8
...
@@ -19,6 +19,7 @@ SRCS(
...
@@ -19,6 +19,7 @@ SRCS(
AggregateFunctionCategoricalInformationValue.cpp
AggregateFunctionCategoricalInformationValue.cpp
AggregateFunctionCombinatorFactory.cpp
AggregateFunctionCombinatorFactory.cpp
AggregateFunctionCount.cpp
AggregateFunctionCount.cpp
AggregateFunctionDeltaSum.cpp
AggregateFunctionDistinct.cpp
AggregateFunctionDistinct.cpp
AggregateFunctionEntropy.cpp
AggregateFunctionEntropy.cpp
AggregateFunctionFactory.cpp
AggregateFunctionFactory.cpp
...
...
tests/queries/0_stateless/01700_deltasum.reference
0 → 100644
浏览文件 @
85396fc8
2
6
7
7
7
5
2
2.25
6.5
tests/queries/0_stateless/01700_deltasum.sql
0 → 100644
浏览文件 @
85396fc8
select
deltaSum
(
arrayJoin
([
1
,
2
,
3
]));
select
deltaSum
(
arrayJoin
([
1
,
2
,
3
,
0
,
3
,
4
]));
select
deltaSum
(
arrayJoin
([
1
,
2
,
3
,
0
,
3
,
4
,
2
,
3
]));
select
deltaSum
(
arrayJoin
([
1
,
2
,
3
,
0
,
3
,
3
,
3
,
3
,
3
,
4
,
2
,
3
]));
select
deltaSum
(
arrayJoin
([
1
,
2
,
3
,
0
,
0
,
0
,
0
,
3
,
3
,
3
,
3
,
3
,
4
,
2
,
3
]));
select
deltaSumMerge
(
rows
)
from
(
select
deltaSumState
(
arrayJoin
([
0
,
1
]))
as
rows
union
all
select
deltaSumState
(
arrayJoin
([
4
,
5
]))
as
rows
);
select
deltaSumMerge
(
rows
)
from
(
select
deltaSumState
(
arrayJoin
([
4
,
5
]))
as
rows
union
all
select
deltaSumState
(
arrayJoin
([
0
,
1
]))
as
rows
);
select
deltaSum
(
arrayJoin
([
2
.
25
,
3
,
4
.
5
]));
select
deltaSumMerge
(
rows
)
from
(
select
deltaSumState
(
arrayJoin
([
0
.
1
,
0
.
3
,
0
.
5
]))
as
rows
union
all
select
deltaSumState
(
arrayJoin
([
4
.
1
,
5
.
1
,
6
.
6
]))
as
rows
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录