2014-08-18 05:45:41 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
2014-08-18 05:45:41 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnVector.h>
|
|
|
|
#include <Columns/ColumnString.h>
|
2017-12-20 07:36:30 +00:00
|
|
|
#include <DataTypes/IDataType.h>
|
2017-12-20 21:43:54 +00:00
|
|
|
#include <common/StringRef.h>
|
2014-08-18 05:45:41 +00:00
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
/** Aggregate functions that store one of passed values.
|
2017-03-09 00:56:38 +00:00
|
|
|
* For example: min, max, any, anyLast.
|
2014-08-18 05:45:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2017-03-09 00:56:38 +00:00
|
|
|
/// For numeric values.
|
2014-08-18 05:45:41 +00:00
|
|
|
template <typename T>
|
|
|
|
struct SingleValueDataFixed
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
private:
|
2018-09-13 14:59:03 +00:00
|
|
|
using Self = SingleValueDataFixed;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
bool has_value = false; /// We need to remember if at least one value has been passed. This is necessary for AggregateFunctionIf.
|
2017-04-01 07:20:54 +00:00
|
|
|
T value;
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
void write(WriteBuffer & buf, const IDataType & /*data_type*/) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
writeBinary(has(), buf);
|
|
|
|
if (has())
|
|
|
|
writeBinary(value, buf);
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
void read(ReadBuffer & buf, const IDataType & /*data_type*/, Arena *)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
readBinary(has_value, buf);
|
|
|
|
if (has())
|
|
|
|
readBinary(value, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
void change(const IColumn & column, size_t row_num, Arena *)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
has_value = true;
|
|
|
|
value = static_cast<const ColumnVector<T> &>(column).getData()[row_num];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Assuming to.has()
|
2017-12-26 17:58:59 +00:00
|
|
|
void change(const Self & to, Arena *)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
has_value = true;
|
|
|
|
value = to.value;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeFirstTime(const IColumn & column, size_t row_num, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!has())
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(column, row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeFirstTime(const Self & to, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!has() && to.has())
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(to, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeEveryTime(const IColumn & column, size_t row_num, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(column, row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeEveryTime(const Self & to, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (to.has())
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(to, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfLess(const IColumn & column, size_t row_num, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!has() || static_cast<const ColumnVector<T> &>(column).getData()[row_num] < value)
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(column, row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfLess(const Self & to, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (to.has() && (!has() || to.value < value))
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(to, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfGreater(const IColumn & column, size_t row_num, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!has() || static_cast<const ColumnVector<T> &>(column).getData()[row_num] > value)
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(column, row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfGreater(const Self & to, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (to.has() && (!has() || to.value > value))
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(to, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-07-21 06:35:58 +00:00
|
|
|
/** For strings. Short strings are stored in the object itself, and long strings are allocated separately.
|
2017-03-09 00:56:38 +00:00
|
|
|
* NOTE It could also be suitable for arrays of numbers.
|
2014-08-18 05:45:41 +00:00
|
|
|
*/
|
2018-01-09 18:47:20 +00:00
|
|
|
struct SingleValueDataString
|
2014-08-18 05:45:41 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
using Self = SingleValueDataString;
|
|
|
|
|
|
|
|
Int32 size = -1; /// -1 indicates that there is no value.
|
2017-12-26 17:58:59 +00:00
|
|
|
Int32 capacity = 0; /// power of two or zero
|
2018-01-09 18:47:20 +00:00
|
|
|
char * large_data;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr Int32 AUTOMATIC_STORAGE_SIZE = 64;
|
2017-12-26 17:58:59 +00:00
|
|
|
static constexpr Int32 MAX_SMALL_STRING_SIZE = AUTOMATIC_STORAGE_SIZE - sizeof(size) - sizeof(capacity) - sizeof(large_data);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
private:
|
|
|
|
char small_data[MAX_SMALL_STRING_SIZE]; /// Including the terminating zero.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
void write(WriteBuffer & buf, const IDataType & /*data_type*/) const
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
writeBinary(size, buf);
|
|
|
|
if (has())
|
|
|
|
buf.write(getData(), size);
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
void read(ReadBuffer & buf, const IDataType & /*data_type*/, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
Int32 rhs_size;
|
|
|
|
readBinary(rhs_size, buf);
|
|
|
|
|
|
|
|
if (rhs_size >= 0)
|
|
|
|
{
|
|
|
|
if (rhs_size <= MAX_SMALL_STRING_SIZE)
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
/// Don't free large_data here.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
size = rhs_size;
|
|
|
|
|
|
|
|
if (size > 0)
|
|
|
|
buf.read(small_data, size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
if (capacity < rhs_size)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
capacity = static_cast<UInt32>(roundUpToPowerOfTwoOrZero(rhs_size));
|
|
|
|
/// Don't free large_data here.
|
|
|
|
large_data = arena->alloc(capacity);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size = rhs_size;
|
|
|
|
buf.read(large_data, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
/// Don't free large_data here.
|
2017-04-01 07:20:54 +00:00
|
|
|
size = rhs_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Assuming to.has()
|
2017-12-26 17:58:59 +00:00
|
|
|
void changeImpl(StringRef value, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
Int32 value_size = value.size;
|
|
|
|
|
|
|
|
if (value_size <= MAX_SMALL_STRING_SIZE)
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
/// Don't free large_data here.
|
2017-04-01 07:20:54 +00:00
|
|
|
size = value_size;
|
|
|
|
|
|
|
|
if (size > 0)
|
|
|
|
memcpy(small_data, value.data, size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
if (capacity < value_size)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
/// Don't free large_data here.
|
|
|
|
capacity = roundUpToPowerOfTwoOrZero(value_size);
|
|
|
|
large_data = arena->alloc(capacity);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size = value_size;
|
|
|
|
memcpy(large_data, value.data, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
void change(const IColumn & column, size_t row_num, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
changeImpl(static_cast<const ColumnString &>(column).getDataAtWithTerminatingZero(row_num), arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
void change(const Self & to, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
changeImpl(to.getStringRef(), arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeFirstTime(const IColumn & column, size_t row_num, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!has())
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(column, row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeFirstTime(const Self & to, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!has() && to.has())
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(to, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeEveryTime(const IColumn & column, size_t row_num, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(column, row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeEveryTime(const Self & to, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (to.has())
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(to, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfLess(const IColumn & column, size_t row_num, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!has() || static_cast<const ColumnString &>(column).getDataAtWithTerminatingZero(row_num) < getStringRef())
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(column, row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfLess(const Self & to, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (to.has() && (!has() || to.getStringRef() < getStringRef()))
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(to, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfGreater(const IColumn & column, size_t row_num, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!has() || static_cast<const ColumnString &>(column).getDataAtWithTerminatingZero(row_num) > getStringRef())
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(column, row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfGreater(const Self & to, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (to.has() && (!has() || to.getStringRef() > getStringRef()))
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(to, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2017-04-01 07:20:54 +00:00
|
|
|
sizeof(SingleValueDataString) == SingleValueDataString::AUTOMATIC_STORAGE_SIZE,
|
|
|
|
"Incorrect size of SingleValueDataString struct");
|
2015-11-26 21:31:06 +00:00
|
|
|
|
2014-08-18 05:45:41 +00:00
|
|
|
|
2017-03-09 00:56:38 +00:00
|
|
|
/// For any other value types.
|
2014-08-18 05:45:41 +00:00
|
|
|
struct SingleValueDataGeneric
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
using Self = SingleValueDataGeneric;
|
|
|
|
|
|
|
|
Field value;
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
void read(ReadBuffer & buf, const IDataType & data_type, Arena *)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
bool is_not_null;
|
|
|
|
readBinary(is_not_null, buf);
|
|
|
|
|
|
|
|
if (is_not_null)
|
|
|
|
data_type.deserializeBinary(value, buf);
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
void change(const IColumn & column, size_t row_num, Arena *)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
column.get(row_num, value);
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
void change(const Self & to, Arena *)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
value = to.value;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeFirstTime(const IColumn & column, size_t row_num, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!has())
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(column, row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeFirstTime(const Self & to, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!has() && to.has())
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(to, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeEveryTime(const IColumn & column, size_t row_num, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(column, row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeEveryTime(const Self & to, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (to.has())
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(to, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfLess(const IColumn & column, size_t row_num, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!has())
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(column, row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Field new_value;
|
|
|
|
column.get(row_num, new_value);
|
|
|
|
if (new_value < value)
|
|
|
|
{
|
|
|
|
value = new_value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfLess(const Self & to, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (to.has() && (!has() || to.value < value))
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(to, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfGreater(const IColumn & column, size_t row_num, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (!has())
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(column, row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Field new_value;
|
|
|
|
column.get(row_num, new_value);
|
|
|
|
if (new_value > value)
|
|
|
|
{
|
|
|
|
value = new_value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfGreater(const Self & to, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (to.has() && (!has() || to.value > value))
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
change(to, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-03-09 00:56:38 +00:00
|
|
|
/** What is the difference between the aggregate functions min, max, any, anyLast
|
|
|
|
* (the condition that the stored value is replaced by a new one,
|
|
|
|
* as well as, of course, the name).
|
2014-08-18 05:45:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
struct AggregateFunctionMinData : Data
|
|
|
|
{
|
2018-09-13 14:59:03 +00:00
|
|
|
using Self = AggregateFunctionMinData;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfBetter(const IColumn & column, size_t row_num, Arena * arena) { return this->changeIfLess(column, row_num, arena); }
|
|
|
|
bool changeIfBetter(const Self & to, Arena * arena) { return this->changeIfLess(to, arena); }
|
2014-08-18 05:45:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static const char * name() { return "min"; }
|
2014-08-18 05:45:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
struct AggregateFunctionMaxData : Data
|
|
|
|
{
|
2018-09-13 14:59:03 +00:00
|
|
|
using Self = AggregateFunctionMaxData;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfBetter(const IColumn & column, size_t row_num, Arena * arena) { return this->changeIfGreater(column, row_num, arena); }
|
|
|
|
bool changeIfBetter(const Self & to, Arena * arena) { return this->changeIfGreater(to, arena); }
|
2014-08-18 05:45:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static const char * name() { return "max"; }
|
2014-08-18 05:45:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
struct AggregateFunctionAnyData : Data
|
|
|
|
{
|
2018-09-13 14:59:03 +00:00
|
|
|
using Self = AggregateFunctionAnyData;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfBetter(const IColumn & column, size_t row_num, Arena * arena) { return this->changeFirstTime(column, row_num, arena); }
|
|
|
|
bool changeIfBetter(const Self & to, Arena * arena) { return this->changeFirstTime(to, arena); }
|
2014-08-18 05:45:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static const char * name() { return "any"; }
|
2014-08-18 05:45:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
struct AggregateFunctionAnyLastData : Data
|
|
|
|
{
|
2018-09-13 14:59:03 +00:00
|
|
|
using Self = AggregateFunctionAnyLastData;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfBetter(const IColumn & column, size_t row_num, Arena * arena) { return this->changeEveryTime(column, row_num, arena); }
|
|
|
|
bool changeIfBetter(const Self & to, Arena * arena) { return this->changeEveryTime(to, arena); }
|
2014-08-18 05:45:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static const char * name() { return "anyLast"; }
|
2014-08-18 05:45:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
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.
|
2018-10-13 14:33:43 +00:00
|
|
|
* Otherwise, selects some arbitrary value.
|
2016-06-26 12:48:04 +00:00
|
|
|
* http://www.cs.umd.edu/~samir/498/karp.pdf
|
|
|
|
*/
|
|
|
|
template <typename Data>
|
|
|
|
struct AggregateFunctionAnyHeavyData : Data
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t counter = 0;
|
|
|
|
|
2018-09-13 14:59:03 +00:00
|
|
|
using Self = AggregateFunctionAnyHeavyData;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfBetter(const IColumn & column, size_t row_num, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (this->isEqualTo(column, row_num))
|
|
|
|
{
|
|
|
|
++counter;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (counter == 0)
|
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
this->change(column, row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
++counter;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
--counter;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
bool changeIfBetter(const Self & to, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
if (this->isEqualTo(to))
|
|
|
|
{
|
|
|
|
counter += to.counter;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-27 19:51:13 +00:00
|
|
|
if ((!this->has() && to.has()) || counter < to.counter)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
this->change(to, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
counter -= to.counter;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write(WriteBuffer & buf, const IDataType & data_type) const
|
|
|
|
{
|
|
|
|
Data::write(buf, data_type);
|
|
|
|
writeBinary(counter, buf);
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
void read(ReadBuffer & buf, const IDataType & data_type, Arena * arena)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
Data::read(buf, data_type, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
readBinary(counter, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char * name() { return "anyHeavy"; }
|
2016-06-26 12:48:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-05-18 14:15:44 +00:00
|
|
|
template <typename Data, bool AllocatesMemoryInArena>
|
|
|
|
class AggregateFunctionsSingleValue final : public IAggregateFunctionDataHelper<Data, AggregateFunctionsSingleValue<Data, AllocatesMemoryInArena>>
|
2014-08-18 05:45:41 +00:00
|
|
|
{
|
|
|
|
private:
|
2019-02-11 19:26:32 +00:00
|
|
|
DataTypePtr & type;
|
2014-08-18 05:45:41 +00:00
|
|
|
|
|
|
|
public:
|
2019-02-11 19:26:32 +00:00
|
|
|
AggregateFunctionsSingleValue(const DataTypePtr & type)
|
2019-05-18 14:15:44 +00:00
|
|
|
: IAggregateFunctionDataHelper<Data, AggregateFunctionsSingleValue<Data, AllocatesMemoryInArena>>({type}, {})
|
2019-02-12 09:31:20 +00:00
|
|
|
, type(this->argument_types[0])
|
2017-12-20 07:36:30 +00:00
|
|
|
{
|
2017-12-20 21:43:54 +00:00
|
|
|
if (StringRef(Data::name()) == StringRef("min")
|
|
|
|
|| StringRef(Data::name()) == StringRef("max"))
|
|
|
|
{
|
|
|
|
if (!type->isComparable())
|
|
|
|
throw Exception("Illegal type " + type->getName() + " of argument of aggregate function " + getName()
|
|
|
|
+ " because the values of that data type are not comparable", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
}
|
2017-12-20 07:36:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override { return Data::name(); }
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
this->data(place).changeIfBetter(*columns[0], row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
this->data(place).changeIfBetter(this->data(rhs), arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
|
|
|
{
|
|
|
|
this->data(place).write(buf, *type.get());
|
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena * arena) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
this->data(place).read(buf, *type.get(), arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 14:15:44 +00:00
|
|
|
bool allocatesMemoryInArena() const override
|
|
|
|
{
|
|
|
|
return AllocatesMemoryInArena;
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
|
|
|
{
|
|
|
|
this->data(place).insertResultInto(to);
|
|
|
|
}
|
2017-09-17 20:22:39 +00:00
|
|
|
|
|
|
|
const char * getHeaderFilePath() const override { return __FILE__; }
|
2014-08-18 05:45:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|