2014-08-18 05:45:41 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
|
|
|
|
#include <DB/IO/ReadHelpers.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Columns/ColumnVector.h>
|
|
|
|
|
#include <DB/Columns/ColumnString.h>
|
2015-11-10 20:48:49 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypeAggregateFunction.h>
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/AggregateFunctions/IUnaryAggregateFunction.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Агрегатные функции, запоминающие одно какое-либо переданное значение.
|
|
|
|
|
* Например, min, max, any, anyLast.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Для числовых значений.
|
|
|
|
|
template <typename T>
|
|
|
|
|
struct SingleValueDataFixed
|
|
|
|
|
{
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using Self = SingleValueDataFixed<T>;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
|
|
|
|
bool has_value = false; /// Надо запомнить, было ли передано хотя бы одно значение. Это нужно для AggregateFunctionIf.
|
|
|
|
|
T value;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool has() const
|
|
|
|
|
{
|
|
|
|
|
return has_value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void insertResultInto(IColumn & to) const
|
|
|
|
|
{
|
|
|
|
|
if (has())
|
|
|
|
|
static_cast<ColumnVector<T> &>(to).getData().push_back(value);
|
|
|
|
|
else
|
|
|
|
|
static_cast<ColumnVector<T> &>(to).insertDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void write(WriteBuffer & buf, const IDataType & data_type) const
|
|
|
|
|
{
|
|
|
|
|
writeBinary(has(), buf);
|
|
|
|
|
if (has())
|
|
|
|
|
writeBinary(value, buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void read(ReadBuffer & buf, const IDataType & data_type)
|
|
|
|
|
{
|
|
|
|
|
readBinary(has_value, buf);
|
|
|
|
|
if (has())
|
|
|
|
|
readBinary(value, buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void change(const IColumn & column, size_t row_num)
|
|
|
|
|
{
|
|
|
|
|
has_value = true;
|
|
|
|
|
value = static_cast<const ColumnVector<T> &>(column).getData()[row_num];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void change(const Self & to)
|
|
|
|
|
{
|
|
|
|
|
has_value = true;
|
|
|
|
|
value = to.value;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeFirstTime(const IColumn & column, size_t row_num)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
if (!has())
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(column, row_num);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeFirstTime(const Self & to)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
if (!has())
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(to);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfLess(const IColumn & column, size_t row_num)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
if (!has() || static_cast<const ColumnVector<T> &>(column).getData()[row_num] < value)
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(column, row_num);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfLess(const Self & to)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
2014-12-02 00:19:11 +00:00
|
|
|
|
if (to.has() && (!has() || to.value < value))
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(to);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfGreater(const IColumn & column, size_t row_num)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
if (!has() || static_cast<const ColumnVector<T> &>(column).getData()[row_num] > value)
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(column, row_num);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfGreater(const Self & to)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
2014-12-02 00:19:11 +00:00
|
|
|
|
if (to.has() && (!has() || to.value > value))
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(to);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
2016-06-26 12:48:04 +00:00
|
|
|
|
|
|
|
|
|
bool isEqualTo(const Self & to) const
|
|
|
|
|
{
|
|
|
|
|
return has() && to.value == value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isEqualTo(const IColumn & column, size_t row_num) const
|
|
|
|
|
{
|
|
|
|
|
return has() && static_cast<const ColumnVector<T> &>(column).getData()[row_num] == value;
|
|
|
|
|
}
|
2014-08-18 05:45:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Для строк. Короткие строки хранятся в самой структуре, а длинные выделяются отдельно.
|
|
|
|
|
* NOTE Могло бы подойти также для массивов чисел.
|
|
|
|
|
*/
|
2015-11-26 21:31:06 +00:00
|
|
|
|
struct __attribute__((__packed__, __aligned__(1))) SingleValueDataString
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using Self = SingleValueDataString;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
|
|
|
|
Int32 size = -1; /// -1 обозначает, что значения нет.
|
|
|
|
|
|
|
|
|
|
static constexpr Int32 AUTOMATIC_STORAGE_SIZE = 64;
|
|
|
|
|
static constexpr Int32 MAX_SMALL_STRING_SIZE = AUTOMATIC_STORAGE_SIZE - sizeof(size);
|
|
|
|
|
|
2015-11-26 21:31:06 +00:00
|
|
|
|
union __attribute__((__packed__, __aligned__(1)))
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
char small_data[MAX_SMALL_STRING_SIZE]; /// Включая завершающий ноль.
|
2015-11-26 21:31:06 +00:00
|
|
|
|
char * __attribute__((__packed__, __aligned__(1))) large_data;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
~SingleValueDataString()
|
|
|
|
|
{
|
|
|
|
|
if (size > MAX_SMALL_STRING_SIZE)
|
|
|
|
|
free(large_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool has() const
|
|
|
|
|
{
|
|
|
|
|
return size >= 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char * getData() const
|
|
|
|
|
{
|
|
|
|
|
return size <= MAX_SMALL_STRING_SIZE ? small_data : large_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringRef getStringRef() const
|
|
|
|
|
{
|
|
|
|
|
return StringRef(getData(), size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void insertResultInto(IColumn & to) const
|
|
|
|
|
{
|
|
|
|
|
if (has())
|
|
|
|
|
static_cast<ColumnString &>(to).insertDataWithTerminatingZero(getData(), size);
|
|
|
|
|
else
|
|
|
|
|
static_cast<ColumnString &>(to).insertDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void write(WriteBuffer & buf, const IDataType & data_type) const
|
|
|
|
|
{
|
|
|
|
|
writeBinary(size, buf);
|
|
|
|
|
if (has())
|
|
|
|
|
buf.write(getData(), size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void read(ReadBuffer & buf, const IDataType & data_type)
|
|
|
|
|
{
|
|
|
|
|
Int32 rhs_size;
|
|
|
|
|
readBinary(rhs_size, buf);
|
|
|
|
|
|
|
|
|
|
if (rhs_size >= 0)
|
|
|
|
|
{
|
|
|
|
|
if (rhs_size <= MAX_SMALL_STRING_SIZE)
|
|
|
|
|
{
|
|
|
|
|
if (size > MAX_SMALL_STRING_SIZE)
|
|
|
|
|
free(large_data);
|
|
|
|
|
|
|
|
|
|
size = rhs_size;
|
2014-08-18 06:56:09 +00:00
|
|
|
|
|
|
|
|
|
if (size > 0)
|
|
|
|
|
buf.read(small_data, size);
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (size < rhs_size)
|
|
|
|
|
{
|
|
|
|
|
if (size > MAX_SMALL_STRING_SIZE)
|
|
|
|
|
free(large_data);
|
|
|
|
|
|
|
|
|
|
large_data = reinterpret_cast<char *>(malloc(rhs_size));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size = rhs_size;
|
|
|
|
|
buf.read(large_data, size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (size > MAX_SMALL_STRING_SIZE)
|
|
|
|
|
free(large_data);
|
|
|
|
|
size = rhs_size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void changeImpl(StringRef value)
|
|
|
|
|
{
|
|
|
|
|
Int32 value_size = value.size;
|
|
|
|
|
|
|
|
|
|
if (value_size <= MAX_SMALL_STRING_SIZE)
|
|
|
|
|
{
|
|
|
|
|
if (size > MAX_SMALL_STRING_SIZE)
|
|
|
|
|
free(large_data);
|
|
|
|
|
|
|
|
|
|
size = value_size;
|
2014-08-18 06:56:09 +00:00
|
|
|
|
|
|
|
|
|
if (size > 0)
|
|
|
|
|
memcpy(small_data, value.data, size);
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (size < value_size)
|
|
|
|
|
{
|
|
|
|
|
if (size > MAX_SMALL_STRING_SIZE)
|
|
|
|
|
free(large_data);
|
|
|
|
|
|
|
|
|
|
large_data = reinterpret_cast<char *>(malloc(value.size));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size = value_size;
|
|
|
|
|
memcpy(large_data, value.data, size);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void change(const IColumn & column, size_t row_num)
|
|
|
|
|
{
|
|
|
|
|
changeImpl(static_cast<const ColumnString &>(column).getDataAtWithTerminatingZero(row_num));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void change(const Self & to)
|
|
|
|
|
{
|
|
|
|
|
changeImpl(to.getStringRef());
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeFirstTime(const IColumn & column, size_t row_num)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
if (!has())
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(column, row_num);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeFirstTime(const Self & to)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
if (!has())
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(to);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfLess(const IColumn & column, size_t row_num)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
if (!has() || static_cast<const ColumnString &>(column).getDataAtWithTerminatingZero(row_num) < getStringRef())
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(column, row_num);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfLess(const Self & to)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
2014-12-02 00:19:11 +00:00
|
|
|
|
if (to.has() && (!has() || to.getStringRef() < getStringRef()))
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(to);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfGreater(const IColumn & column, size_t row_num)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
if (!has() || static_cast<const ColumnString &>(column).getDataAtWithTerminatingZero(row_num) > getStringRef())
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(column, row_num);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfGreater(const Self & to)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
2014-12-02 00:19:11 +00:00
|
|
|
|
if (to.has() && (!has() || to.getStringRef() > getStringRef()))
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(to);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
2016-06-26 12:48:04 +00:00
|
|
|
|
|
|
|
|
|
bool isEqualTo(const Self & to) const
|
|
|
|
|
{
|
|
|
|
|
return has() && to.getStringRef() == getStringRef();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isEqualTo(const IColumn & column, size_t row_num) const
|
|
|
|
|
{
|
|
|
|
|
return has() && static_cast<const ColumnString &>(column).getDataAtWithTerminatingZero(row_num) == getStringRef();
|
|
|
|
|
}
|
2014-08-18 05:45:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
2015-11-26 21:31:06 +00:00
|
|
|
|
static_assert(
|
|
|
|
|
sizeof(SingleValueDataString) == SingleValueDataString::AUTOMATIC_STORAGE_SIZE,
|
|
|
|
|
"Incorrect size of SingleValueDataString struct");
|
|
|
|
|
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
|
|
|
|
/// Для любых других типов значений.
|
|
|
|
|
struct SingleValueDataGeneric
|
|
|
|
|
{
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using Self = SingleValueDataGeneric;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
|
|
|
|
Field value;
|
|
|
|
|
|
|
|
|
|
bool has() const
|
|
|
|
|
{
|
|
|
|
|
return !value.isNull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void insertResultInto(IColumn & to) const
|
|
|
|
|
{
|
|
|
|
|
if (has())
|
|
|
|
|
to.insert(value);
|
|
|
|
|
else
|
|
|
|
|
to.insertDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void write(WriteBuffer & buf, const IDataType & data_type) const
|
|
|
|
|
{
|
|
|
|
|
if (!value.isNull())
|
|
|
|
|
{
|
|
|
|
|
writeBinary(true, buf);
|
|
|
|
|
data_type.serializeBinary(value, buf);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
writeBinary(false, buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void read(ReadBuffer & buf, const IDataType & data_type)
|
|
|
|
|
{
|
|
|
|
|
bool is_not_null;
|
|
|
|
|
readBinary(is_not_null, buf);
|
|
|
|
|
|
|
|
|
|
if (is_not_null)
|
|
|
|
|
data_type.deserializeBinary(value, buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void change(const IColumn & column, size_t row_num)
|
|
|
|
|
{
|
|
|
|
|
column.get(row_num, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void change(const Self & to)
|
|
|
|
|
{
|
|
|
|
|
value = to.value;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeFirstTime(const IColumn & column, size_t row_num)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
if (!has())
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(column, row_num);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeFirstTime(const Self & to)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
if (!has())
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(to);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfLess(const IColumn & column, size_t row_num)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
if (!has())
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(column, row_num);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-08-18 05:45:41 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Field new_value;
|
|
|
|
|
column.get(row_num, new_value);
|
|
|
|
|
if (new_value < value)
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
value = new_value;
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfLess(const Self & to)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
2014-12-02 00:19:11 +00:00
|
|
|
|
if (to.has() && (!has() || to.value < value))
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(to);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfGreater(const IColumn & column, size_t row_num)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
if (!has())
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(column, row_num);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-08-18 05:45:41 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Field new_value;
|
|
|
|
|
column.get(row_num, new_value);
|
|
|
|
|
if (new_value > value)
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
value = new_value;
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfGreater(const Self & to)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
2014-12-02 00:19:11 +00:00
|
|
|
|
if (to.has() && (!has() || to.value > value))
|
2015-03-01 01:06:49 +00:00
|
|
|
|
{
|
2014-08-18 05:45:41 +00:00
|
|
|
|
change(to);
|
2015-03-01 01:06:49 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
2016-06-26 12:48:04 +00:00
|
|
|
|
|
|
|
|
|
bool isEqualTo(const IColumn & column, size_t row_num) const
|
|
|
|
|
{
|
|
|
|
|
return has() && value == column[row_num];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isEqualTo(const Self & to) const
|
|
|
|
|
{
|
|
|
|
|
return has() && to.value == value;
|
|
|
|
|
}
|
2014-08-18 05:45:41 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** То, чем отличаются друг от другая агрегатные функции min, max, any, anyLast
|
|
|
|
|
* (условием, при котором сохранённое значение заменяется на новое,
|
|
|
|
|
* а также, конечно, именем).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
|
struct AggregateFunctionMinData : Data
|
|
|
|
|
{
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using Self = AggregateFunctionMinData<Data>;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfBetter(const IColumn & column, size_t row_num) { return this->changeIfLess(column, row_num); }
|
|
|
|
|
bool changeIfBetter(const Self & to) { return this->changeIfLess(to); }
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
|
|
|
|
static const char * name() { return "min"; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
|
struct AggregateFunctionMaxData : Data
|
|
|
|
|
{
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using Self = AggregateFunctionMaxData<Data>;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfBetter(const IColumn & column, size_t row_num) { return this->changeIfGreater(column, row_num); }
|
|
|
|
|
bool changeIfBetter(const Self & to) { return this->changeIfGreater(to); }
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
|
|
|
|
static const char * name() { return "max"; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
|
struct AggregateFunctionAnyData : Data
|
|
|
|
|
{
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using Self = AggregateFunctionAnyData<Data>;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfBetter(const IColumn & column, size_t row_num) { return this->changeFirstTime(column, row_num); }
|
|
|
|
|
bool changeIfBetter(const Self & to) { return this->changeFirstTime(to); }
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
|
|
|
|
static const char * name() { return "any"; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
|
struct AggregateFunctionAnyLastData : Data
|
|
|
|
|
{
|
2016-05-28 10:35:44 +00:00
|
|
|
|
using Self = AggregateFunctionAnyLastData<Data>;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
bool changeIfBetter(const IColumn & column, size_t row_num) { this->change(column, row_num); return true; }
|
|
|
|
|
bool changeIfBetter(const Self & to) { this->change(to); return true; }
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
|
|
|
|
static const char * name() { return "anyLast"; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2016-06-26 12:48:04 +00:00
|
|
|
|
/** Implement 'heavy hitters' algorithm.
|
2016-06-28 18:00:04 +00:00
|
|
|
|
* Selects most frequent value if its frequency is more than 50% in each thread of execution.
|
2016-06-26 12:48:04 +00:00
|
|
|
|
* Otherwise, selects some arbitary value.
|
|
|
|
|
* http://www.cs.umd.edu/~samir/498/karp.pdf
|
|
|
|
|
*/
|
|
|
|
|
template <typename Data>
|
|
|
|
|
struct AggregateFunctionAnyHeavyData : Data
|
|
|
|
|
{
|
|
|
|
|
size_t counter = 0;
|
|
|
|
|
|
|
|
|
|
using Self = AggregateFunctionAnyHeavyData<Data>;
|
|
|
|
|
|
|
|
|
|
bool changeIfBetter(const IColumn & column, size_t row_num)
|
|
|
|
|
{
|
|
|
|
|
if (this->isEqualTo(column, row_num))
|
|
|
|
|
{
|
|
|
|
|
++counter;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (counter == 0)
|
|
|
|
|
{
|
|
|
|
|
this->change(column, row_num);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
--counter;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool changeIfBetter(const Self & to)
|
|
|
|
|
{
|
|
|
|
|
if (this->isEqualTo(to))
|
|
|
|
|
{
|
|
|
|
|
counter += to.counter;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (counter < to.counter)
|
|
|
|
|
{
|
|
|
|
|
this->change(to);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
counter -= to.counter;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const char * name() { return "anyHeavy"; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2014-08-18 05:45:41 +00:00
|
|
|
|
template <typename Data>
|
|
|
|
|
class AggregateFunctionsSingleValue final : public IUnaryAggregateFunction<Data, AggregateFunctionsSingleValue<Data> >
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
DataTypePtr type;
|
|
|
|
|
|
|
|
|
|
public:
|
2015-11-11 02:04:23 +00:00
|
|
|
|
String getName() const override { return Data::name(); }
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
DataTypePtr getReturnType() const override
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
return type;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-15 08:31:08 +00:00
|
|
|
|
void setArgument(const DataTypePtr & argument)
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
type = argument;
|
2015-11-10 20:48:49 +00:00
|
|
|
|
|
|
|
|
|
if (typeid_cast<const DataTypeAggregateFunction *>(type.get()))
|
|
|
|
|
throw Exception("Illegal type " + type->getName() + " of argument of aggregate function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2015-11-15 06:23:44 +00:00
|
|
|
|
void addImpl(AggregateDataPtr place, const IColumn & column, size_t row_num) const
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
this->data(place).changeIfBetter(column, row_num);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const override
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
this->data(place).changeIfBetter(this->data(rhs));
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
this->data(place).write(buf, *type.get());
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-12 04:01:03 +00:00
|
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf) const override
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
2016-03-12 04:01:03 +00:00
|
|
|
|
this->data(place).read(buf, *type.get());
|
2014-08-18 05:45:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 02:04:23 +00:00
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
2014-08-18 05:45:41 +00:00
|
|
|
|
{
|
|
|
|
|
this->data(place).insertResultInto(to);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|