mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-27 01:51:59 +00:00
dbms: improved performance of finalizing aggregate functions [#CONV-2944].
This commit is contained in:
parent
6e34a0331d
commit
43825cdf95
@ -69,9 +69,9 @@ public:
|
||||
d.value = tmp;
|
||||
}
|
||||
|
||||
Field getResult(ConstAggregateDataPtr place) const
|
||||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
||||
{
|
||||
return data(place).value;
|
||||
to.insert(data(place).value);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -56,9 +56,9 @@ public:
|
||||
type->deserializeBinary(data(place).value, buf);
|
||||
}
|
||||
|
||||
Field getResult(ConstAggregateDataPtr place) const
|
||||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
||||
{
|
||||
return data(place).value;
|
||||
to.insert(data(place).value);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -70,9 +70,10 @@ public:
|
||||
this->data(place).count += tmp_count;
|
||||
}
|
||||
|
||||
Field getResult(ConstAggregateDataPtr place) const
|
||||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
||||
{
|
||||
return static_cast<Float64>(this->data(place).sum) / this->data(place).count;
|
||||
static_cast<ColumnFloat64 &>(to).getData().push_back(
|
||||
static_cast<Float64>(this->data(place).sum) / this->data(place).count);
|
||||
}
|
||||
};
|
||||
|
||||
@ -134,9 +135,10 @@ public:
|
||||
this->data(place).count += tmp_count;
|
||||
}
|
||||
|
||||
Field getResult(ConstAggregateDataPtr place) const
|
||||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
||||
{
|
||||
return static_cast<Float64>(this->data(place).sum) / this->data(place).count;
|
||||
static_cast<ColumnFloat64 &>(to).getData().push_back(
|
||||
static_cast<Float64>(this->data(place).sum) / this->data(place).count);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -53,9 +53,9 @@ public:
|
||||
data(place).count += tmp;
|
||||
}
|
||||
|
||||
Field getResult(ConstAggregateDataPtr place) const
|
||||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
||||
{
|
||||
return data(place).count;
|
||||
static_cast<ColumnUInt64 &>(to).getData().push_back(data(place).count);
|
||||
}
|
||||
|
||||
/// Для оптимизации
|
||||
|
@ -74,9 +74,9 @@ public:
|
||||
type->deserializeBinary(value[i], buf);
|
||||
}
|
||||
|
||||
Field getResult(ConstAggregateDataPtr place) const
|
||||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
||||
{
|
||||
return data(place).value;
|
||||
to.insert(data(place).value);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -10,6 +10,8 @@
|
||||
|
||||
#include <DB/AggregateFunctions/IUnaryAggregateFunction.h>
|
||||
|
||||
#include <DB/Columns/ColumnArray.h>
|
||||
|
||||
|
||||
namespace DB
|
||||
{
|
||||
@ -86,14 +88,15 @@ public:
|
||||
this->data(place).sample.merge(tmp_sample);
|
||||
}
|
||||
|
||||
Field getResult(ConstAggregateDataPtr place) const
|
||||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
||||
{
|
||||
/// Sample может отсортироваться при получении квантиля, но в этом контексте можно не считать это нарушением константности.
|
||||
Sample & sample = const_cast<Sample &>(this->data(place).sample);
|
||||
|
||||
if (returns_float)
|
||||
return Float64(const_cast<Sample &>(this->data(place).sample).quantileInterpolated(level));
|
||||
static_cast<ColumnFloat64 &>(to).getData().push_back(sample.quantileInterpolated(level));
|
||||
else
|
||||
return typename NearestFieldType<ArgumentFieldType>::Type(const_cast<Sample &>(this->data(place).sample).quantileInterpolated(level));
|
||||
static_cast<ColumnVector<ArgumentFieldType> &>(to).getData().push_back(sample.quantileInterpolated(level));
|
||||
}
|
||||
};
|
||||
|
||||
@ -164,23 +167,31 @@ public:
|
||||
this->data(place).sample.merge(tmp_sample);
|
||||
}
|
||||
|
||||
Field getResult(ConstAggregateDataPtr place) const
|
||||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
||||
{
|
||||
size_t size = levels.size();
|
||||
Field res = Array(levels.size());
|
||||
Array & arr = get<Array &>(res);
|
||||
|
||||
/// Sample может отсортироваться при получении квантиля, но в этом контексте можно не считать это нарушением константности.
|
||||
Sample & sample = const_cast<Sample &>(this->data(place).sample);
|
||||
|
||||
if (returns_float)
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
arr[i] = Float64(sample.quantileInterpolated(levels[i]));
|
||||
else
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
arr[i] = typename NearestFieldType<ArgumentFieldType>::Type(sample.quantileInterpolated(levels[i]));
|
||||
ColumnArray & arr_to = static_cast<ColumnArray &>(to);
|
||||
ColumnArray::Offsets_t & offsets_to = arr_to.getOffsets();
|
||||
|
||||
return res;
|
||||
size_t size = levels.size();
|
||||
offsets_to.push_back((offsets_to.size() == 0 ? 0 : offsets_to.back()) + size);
|
||||
|
||||
if (returns_float)
|
||||
{
|
||||
ColumnFloat64::Container_t & data_to = static_cast<ColumnFloat64 &>(arr_to.getData()).getData();
|
||||
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
data_to.push_back(sample.quantileInterpolated(levels[i]));
|
||||
}
|
||||
else
|
||||
{
|
||||
typename ColumnVector<ArgumentFieldType>::Container_t & data_to = static_cast<ColumnVector<ArgumentFieldType> &>(arr_to.getData()).getData();
|
||||
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
data_to.push_back(sample.quantileInterpolated(levels[i]));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -62,9 +62,9 @@ public:
|
||||
this->data(place).sum += tmp;
|
||||
}
|
||||
|
||||
Field getResult(ConstAggregateDataPtr place) const
|
||||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
||||
{
|
||||
return this->data(place).sum;
|
||||
static_cast<ColumnVector<typename NearestFieldType<T>::Type> &>(to).getData().push_back(this->data(place).sum);
|
||||
}
|
||||
};
|
||||
|
||||
@ -115,9 +115,9 @@ public:
|
||||
this->data(place).sum += tmp;
|
||||
}
|
||||
|
||||
Field getResult(ConstAggregateDataPtr place) const
|
||||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
||||
{
|
||||
return this->data(place).sum;
|
||||
static_cast<ColumnVector<typename NearestFieldType<T>::Type> &>(to).getData().push_back(this->data(place).sum);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -11,6 +11,8 @@
|
||||
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
||||
#include <DB/DataTypes/DataTypeString.h>
|
||||
|
||||
#include <DB/Columns/ColumnString.h>
|
||||
|
||||
#include <DB/AggregateFunctions/IUnaryAggregateFunction.h>
|
||||
|
||||
|
||||
@ -90,9 +92,9 @@ public:
|
||||
data(place).set.merge(tmp_set);
|
||||
}
|
||||
|
||||
Field getResult(ConstAggregateDataPtr place) const
|
||||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
||||
{
|
||||
return data(place).set.size();
|
||||
static_cast<ColumnUInt64 &>(to).getData().push_back(data(place).set.size());
|
||||
}
|
||||
};
|
||||
|
||||
@ -120,12 +122,13 @@ public:
|
||||
return new DataTypeString;
|
||||
}
|
||||
|
||||
Field getResult(ConstAggregateDataPtr place) const
|
||||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
||||
{
|
||||
Field res = String();
|
||||
WriteBufferFromString wb(get<String &>(res));
|
||||
String res;
|
||||
WriteBufferFromString wb(res);
|
||||
this->data(place).set.writeText(wb);
|
||||
return res;
|
||||
|
||||
static_cast<ColumnString &>(to).insertDataWithTerminatingZero(res.data(), res.size() + 1);
|
||||
}
|
||||
};
|
||||
|
||||
@ -176,9 +179,9 @@ public:
|
||||
data(place).set.merge(tmp_set);
|
||||
}
|
||||
|
||||
Field getResult(ConstAggregateDataPtr place) const
|
||||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
||||
{
|
||||
return data(place).set.size();
|
||||
static_cast<ColumnUInt64 &>(to).getData().push_back(data(place).set.size());
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -99,9 +99,9 @@ public:
|
||||
type->deserializeBinary(d.value, buf);
|
||||
}
|
||||
|
||||
Field getResult(ConstAggregateDataPtr place) const
|
||||
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
||||
{
|
||||
return data(place).value;
|
||||
to.insert(data(place).value);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -79,8 +79,8 @@ public:
|
||||
/// Десериализовать состояние и объединить своё состояние с ним.
|
||||
virtual void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const = 0;
|
||||
|
||||
/// Получить результат
|
||||
virtual Field getResult(ConstAggregateDataPtr place) const = 0;
|
||||
/// Вставить результат в столбец.
|
||||
virtual void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const = 0;
|
||||
};
|
||||
|
||||
|
||||
|
@ -56,13 +56,12 @@ protected:
|
||||
ColumnAggregateFunction::Container_t & data = col->getData();
|
||||
IAggregateFunction * func = col->getFunction();
|
||||
column.type = func->getReturnType();
|
||||
ColumnPtr finalized_column = column.type->createColumn();
|
||||
finalized_column->reserve(rows);
|
||||
column.column = column.type->createColumn();
|
||||
IColumn & finalized_column = *column.column;
|
||||
finalized_column.reserve(rows);
|
||||
|
||||
for (size_t j = 0; j < rows; ++j)
|
||||
finalized_column->insert(func->getResult(data[j]));
|
||||
|
||||
column.column = finalized_column;
|
||||
func->insertResultInto(data[j], finalized_column);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user