mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-24 16:42:05 +00:00
Aggregate function sumMap: improve insertResultInto method and change return type to Tuple(Array(K), Array(V)).
This commit is contained in:
parent
07cd6829d0
commit
e99c969a1c
@ -4,10 +4,10 @@
|
||||
#include <IO/ReadHelpers.h>
|
||||
|
||||
#include <DataTypes/DataTypeArray.h>
|
||||
#include <DataTypes/DataTypesNumber.h>
|
||||
#include <DataTypes/DataTypeTuple.h>
|
||||
|
||||
#include <Columns/ColumnArray.h>
|
||||
#include <Columns/ColumnVector.h>
|
||||
#include <Columns/ColumnTuple.h>
|
||||
|
||||
#include <Core/FieldVisitors.h>
|
||||
#include <AggregateFunctions/IBinaryAggregateFunction.h>
|
||||
@ -43,12 +43,11 @@ struct AggregateFunctionSumMapData
|
||||
* [7,5,3] [5,15,25]
|
||||
* [8,9,10] [20,20,20]
|
||||
* will return:
|
||||
* [[1,2,3,4,5,6,7,8,9,10],[10,10,45,20,35,20,15,30,20,20]]
|
||||
* ([1,2,3,4,5,6,7,8,9,10],[10,10,45,20,35,20,15,30,20,20])
|
||||
*/
|
||||
class AggregateFunctionSumMap final : public IBinaryAggregateFunction<struct AggregateFunctionSumMapData, AggregateFunctionSumMap>
|
||||
{
|
||||
private:
|
||||
DataTypePtr type;
|
||||
DataTypePtr keys_type;
|
||||
DataTypePtr values_type;
|
||||
|
||||
@ -57,7 +56,11 @@ public:
|
||||
|
||||
DataTypePtr getReturnType() const override
|
||||
{
|
||||
return std::make_shared<DataTypeArray>(type);
|
||||
DataTypes types;
|
||||
types.emplace_back(std::make_shared<DataTypeArray>(keys_type));
|
||||
types.emplace_back(std::make_shared<DataTypeArray>(values_type));
|
||||
|
||||
return std::make_shared<DataTypeTuple>(types);
|
||||
}
|
||||
|
||||
void setArgumentsImpl(const DataTypes & arguments)
|
||||
@ -77,8 +80,6 @@ public:
|
||||
throw Exception("Second argument for function " + getName() + " must be an array.",
|
||||
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
||||
values_type = array_type->getNestedType();
|
||||
|
||||
type = arguments.front();
|
||||
}
|
||||
|
||||
void setParameters(const Array & params) override
|
||||
@ -159,25 +160,30 @@ public:
|
||||
|
||||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
||||
{
|
||||
auto & to_array = static_cast<ColumnArray &>(to);
|
||||
auto & to_data = to_array.getData();
|
||||
auto & to_offsets = to_array.getOffsets();
|
||||
auto & to_cols = static_cast<ColumnTuple &>(to).getColumns();
|
||||
|
||||
auto & to_keys_arr = static_cast<ColumnArray &>(*to_cols[0]);
|
||||
auto & to_values_arr = static_cast<ColumnArray &>(*to_cols[1]);
|
||||
|
||||
auto & to_keys_col = to_keys_arr.getData();
|
||||
auto & to_keys_offsets = to_keys_arr.getOffsets();
|
||||
|
||||
auto & to_values_col = to_values_arr.getData();
|
||||
auto & to_values_offsets = to_values_arr.getOffsets();
|
||||
|
||||
const auto & merged_maps = this->data(place).merged_maps;
|
||||
size_t size = merged_maps.size();
|
||||
|
||||
Array keys, values;
|
||||
keys.reserve(size);
|
||||
values.reserve(size);
|
||||
to_keys_col.reserve(size);
|
||||
to_values_col.reserve(size);
|
||||
for (const auto &v : merged_maps)
|
||||
{
|
||||
keys.push_back(v.first);
|
||||
values.push_back(v.second);
|
||||
to_keys_col.insert(v.first);
|
||||
to_values_col.insert(v.second);
|
||||
}
|
||||
|
||||
to_data.insert(keys);
|
||||
to_data.insert(values);
|
||||
to_offsets.push_back((to_offsets.empty() ? 0 : to_offsets.back()) + 2);
|
||||
to_keys_offsets.push_back((to_keys_offsets.empty() ? 0 : to_keys_offsets.back()) + size);
|
||||
to_values_offsets.push_back((to_values_offsets.empty() ? 0 : to_values_offsets.back()) + size);
|
||||
}
|
||||
|
||||
const char * getHeaderFilePath() const override { return __FILE__; }
|
||||
|
@ -2,9 +2,9 @@
|
||||
2000-01-01 2000-01-01 00:00:00 [3,4,5] [10,10,10]
|
||||
2000-01-01 2000-01-01 00:01:00 [4,5,6] [10,10,10]
|
||||
2000-01-01 2000-01-01 00:01:00 [6,7,8] [10,10,10]
|
||||
[[1,2,3,4,5,6,7,8],[10,10,20,20,20,20,10,10]]
|
||||
[[1,2,3,4,5,6,7,8],[10,10,20,20,20,20,10,10]]
|
||||
2000-01-01 00:00:00 [[1,2,3,4,5],[10,10,20,10,10]]
|
||||
2000-01-01 00:01:00 [[4,5,6,7,8],[10,10,20,10,10]]
|
||||
([1,2,3,4,5,6,7,8],[10,10,20,20,20,20,10,10])
|
||||
([1,2,3,4,5,6,7,8],[10,10,20,20,20,20,10,10])
|
||||
2000-01-01 00:00:00 ([1,2,3,4,5],[10,10,20,10,10])
|
||||
2000-01-01 00:01:00 ([4,5,6,7,8],[10,10,20,10,10])
|
||||
2000-01-01 00:00:00 [1,2,3,4,5] [10,10,20,10,10]
|
||||
2000-01-01 00:01:00 [4,5,6,7,8] [10,10,20,10,10]
|
||||
|
@ -7,6 +7,6 @@ SELECT * FROM test.sum_map;
|
||||
SELECT sumMap(statusMap.status, statusMap.requests) FROM test.sum_map;
|
||||
SELECT sumMapMerge(s) FROM (SELECT sumMapState(statusMap.status, statusMap.requests) AS s FROM test.sum_map);
|
||||
SELECT timeslot, sumMap(statusMap.status, statusMap.requests) FROM test.sum_map GROUP BY timeslot;
|
||||
SELECT timeslot, sumMap(statusMap.status, statusMap.requests)[1], sumMap(statusMap.status, statusMap.requests)[2] FROM test.sum_map GROUP BY timeslot;
|
||||
SELECT timeslot, sumMap(statusMap.status, statusMap.requests).1, sumMap(statusMap.status, statusMap.requests).2 FROM test.sum_map GROUP BY timeslot;
|
||||
|
||||
DROP TABLE test.sum_map;
|
||||
|
@ -48,7 +48,7 @@ sumMap(key, value)
|
||||
------
|
||||
Performs summation of array 'value' by corresponding keys of array 'key'.
|
||||
Number of elements in 'key' and 'value' arrays should be the same for each row, on which summation is being performed.
|
||||
Returns array of two arrays - sorted keys and values, summed up by corresponding keys.
|
||||
Returns a tuple of two arrays - sorted keys and values, summed up by corresponding keys.
|
||||
|
||||
Example:
|
||||
|
||||
@ -76,8 +76,8 @@ GROUP BY timeslot
|
||||
.. code-block:: text
|
||||
|
||||
┌────────────timeslot─┬─sumMap(statusMap.status, statusMap.requests)─┐
|
||||
│ 2000-01-01 00:00:00 │ [[1,2,3,4,5],[10,10,20,10,10]] │
|
||||
│ 2000-01-01 00:01:00 │ [[4,5,6,7,8],[10,10,20,10,10]] │
|
||||
│ 2000-01-01 00:00:00 │ ([1,2,3,4,5],[10,10,20,10,10]) │
|
||||
│ 2000-01-01 00:01:00 │ ([4,5,6,7,8],[10,10,20,10,10]) │
|
||||
└─────────────────────┴──────────────────────────────────────────────┘
|
||||
|
||||
avg(x)
|
||||
|
@ -48,7 +48,7 @@ sumMap(key, value)
|
||||
------
|
||||
Производит суммирование массива 'value' по соотвествующим ключам заданным в массиве 'key'.
|
||||
Количество элементов в 'key' и 'value' должно быть одинаковым для каждой строки, для которой происходит суммирование.
|
||||
Возвращает массив из двух массивов - ключи в отсортированном порядке и значения, просуммированные по соотвествующим ключам.
|
||||
Возвращает кортеж из двух массивов - ключи в отсортированном порядке и значения, просуммированные по соотвествующим ключам.
|
||||
|
||||
Пример:
|
||||
|
||||
@ -76,8 +76,8 @@ GROUP BY timeslot
|
||||
.. code-block:: text
|
||||
|
||||
┌────────────timeslot─┬─sumMap(statusMap.status, statusMap.requests)─┐
|
||||
│ 2000-01-01 00:00:00 │ [[1,2,3,4,5],[10,10,20,10,10]] │
|
||||
│ 2000-01-01 00:01:00 │ [[4,5,6,7,8],[10,10,20,10,10]] │
|
||||
│ 2000-01-01 00:00:00 │ ([1,2,3,4,5],[10,10,20,10,10]) │
|
||||
│ 2000-01-01 00:01:00 │ ([4,5,6,7,8],[10,10,20,10,10]) │
|
||||
└─────────────────────┴──────────────────────────────────────────────┘
|
||||
|
||||
avg(x)
|
||||
|
Loading…
Reference in New Issue
Block a user