2012-12-26 16:27:46 +00:00
|
|
|
|
#include <DB/Functions/IFunction.h>
|
|
|
|
|
#include <DB/Columns/ColumnString.h>
|
2014-08-22 00:57:20 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypeString.h>
|
2012-12-26 16:27:46 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
|
|
|
|
#include <DB/IO/WriteBufferFromVector.h>
|
|
|
|
|
#include <DB/IO/WriteBufferFromString.h>
|
2015-09-20 02:03:12 +00:00
|
|
|
|
#include <DB/Common/formatReadable.h>
|
2012-12-26 16:27:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Функция для необычного преобразования в строку:
|
2014-06-26 00:58:14 +00:00
|
|
|
|
*
|
2012-12-26 16:27:46 +00:00
|
|
|
|
* bitmaskToList - принимает целое число - битовую маску, возвращает строку из степеней двойки через запятую.
|
|
|
|
|
* например, bitmaskToList(50) = '2,16,32'
|
2015-09-20 02:03:12 +00:00
|
|
|
|
*
|
|
|
|
|
* formatReadableSize - выводит переданный размер в байтах в виде 123.45 GiB.
|
2012-12-26 16:27:46 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
class FunctionBitmaskToList : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = "bitmaskToList";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionBitmaskToList>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
|
2012-12-26 16:27:46 +00:00
|
|
|
|
/// Получить основное имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
virtual String getName() const override
|
2012-12-26 16:27:46 +00:00
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2012-12-26 16:27:46 +00:00
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2016-12-29 19:38:10 +00:00
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
2017-01-05 16:34:05 +00:00
|
|
|
|
bool isInjective(const Block &) override { return true; }
|
2016-12-29 19:38:10 +00:00
|
|
|
|
|
2012-12-26 16:27:46 +00:00
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2016-07-06 09:47:55 +00:00
|
|
|
|
virtual DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2012-12-26 16:27:46 +00:00
|
|
|
|
{
|
|
|
|
|
const IDataType * type = &*arguments[0];
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
|
if (!typeid_cast<const DataTypeUInt8 *>(type) &&
|
|
|
|
|
!typeid_cast<const DataTypeUInt16 *>(type) &&
|
|
|
|
|
!typeid_cast<const DataTypeUInt32 *>(type) &&
|
|
|
|
|
!typeid_cast<const DataTypeUInt64 *>(type) &&
|
|
|
|
|
!typeid_cast<const DataTypeInt8 *>(type) &&
|
|
|
|
|
!typeid_cast<const DataTypeInt16 *>(type) &&
|
|
|
|
|
!typeid_cast<const DataTypeInt32 *>(type) &&
|
|
|
|
|
!typeid_cast<const DataTypeInt64 *>(type))
|
2012-12-26 17:13:23 +00:00
|
|
|
|
throw Exception("Cannot format " + type->getName() + " as bitmask string", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeString>();
|
2012-12-26 16:27:46 +00:00
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2015-09-20 02:03:12 +00:00
|
|
|
|
/// Выполнить функцию над блоком.
|
2016-07-06 09:47:55 +00:00
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2015-09-20 02:03:12 +00:00
|
|
|
|
{
|
|
|
|
|
if (!( executeType<UInt8>(block, arguments, result)
|
|
|
|
|
|| executeType<UInt16>(block, arguments, result)
|
|
|
|
|
|| executeType<UInt32>(block, arguments, result)
|
|
|
|
|
|| executeType<UInt64>(block, arguments, result)
|
|
|
|
|
|| executeType<Int8>(block, arguments, result)
|
|
|
|
|
|| executeType<Int16>(block, arguments, result)
|
|
|
|
|
|| executeType<Int32>(block, arguments, result)
|
|
|
|
|
|| executeType<Int64>(block, arguments, result)))
|
2017-01-02 20:12:12 +00:00
|
|
|
|
throw Exception("Illegal column " + block.safeGetByPosition(arguments[0]).column->getName()
|
2015-09-20 02:03:12 +00:00
|
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
template <typename T>
|
2012-12-26 16:27:46 +00:00
|
|
|
|
inline static void writeBitmask(T x, WriteBuffer & out)
|
|
|
|
|
{
|
|
|
|
|
bool first = true;
|
2013-03-07 18:11:30 +00:00
|
|
|
|
while (x)
|
2012-12-26 16:27:46 +00:00
|
|
|
|
{
|
2013-03-07 18:11:30 +00:00
|
|
|
|
T y = (x & (x - 1));
|
|
|
|
|
T bit = x ^ y;
|
|
|
|
|
x = y;
|
|
|
|
|
if (!first)
|
|
|
|
|
out.write(",", 1);
|
|
|
|
|
first = false;
|
|
|
|
|
writeIntText(bit, out);
|
2012-12-26 16:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2015-09-20 02:03:12 +00:00
|
|
|
|
template <typename T>
|
2012-12-26 16:27:46 +00:00
|
|
|
|
bool executeType(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
|
if (const ColumnVector<T> * col_from = typeid_cast<const ColumnVector<T> *>(block.safeGetByPosition(arguments[0]).column.get()))
|
2012-12-26 16:27:46 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
auto col_to = std::make_shared<ColumnString>();
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = col_to;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2012-12-26 16:27:46 +00:00
|
|
|
|
const typename ColumnVector<T>::Container_t & vec_from = col_from->getData();
|
2013-05-05 15:25:25 +00:00
|
|
|
|
ColumnString::Chars_t & data_to = col_to->getChars();
|
2012-12-26 16:27:46 +00:00
|
|
|
|
ColumnString::Offsets_t & offsets_to = col_to->getOffsets();
|
|
|
|
|
size_t size = vec_from.size();
|
|
|
|
|
data_to.resize(size * 2);
|
|
|
|
|
offsets_to.resize(size);
|
|
|
|
|
|
2013-09-15 05:51:43 +00:00
|
|
|
|
WriteBufferFromVector<ColumnString::Chars_t> buf_to(data_to);
|
2012-12-26 16:27:46 +00:00
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
{
|
|
|
|
|
writeBitmask<T>(vec_from[i], buf_to);
|
|
|
|
|
writeChar(0, buf_to);
|
|
|
|
|
offsets_to[i] = buf_to.count();
|
|
|
|
|
}
|
|
|
|
|
data_to.resize(buf_to.count());
|
|
|
|
|
}
|
2017-01-02 20:12:12 +00:00
|
|
|
|
else if (const ColumnConst<T> * col_from = typeid_cast<const ColumnConst<T> *>(block.safeGetByPosition(arguments[0]).column.get()))
|
2012-12-26 16:27:46 +00:00
|
|
|
|
{
|
|
|
|
|
std::string res;
|
|
|
|
|
{
|
|
|
|
|
WriteBufferFromString buf(res);
|
|
|
|
|
writeBitmask<T>(col_from->getData(), buf);
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = std::make_shared<ColumnConstString>(col_from->size(), res);
|
2012-12-26 16:27:46 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2012-12-26 16:27:46 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2015-09-20 02:03:12 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FunctionFormatReadableSize : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "formatReadableSize";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionFormatReadableSize>(); }
|
2015-09-20 02:03:12 +00:00
|
|
|
|
|
|
|
|
|
/// Получить основное имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
virtual String getName() const override
|
2015-09-20 02:03:12 +00:00
|
|
|
|
{
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-29 19:38:10 +00:00
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
|
2015-09-20 02:03:12 +00:00
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2016-07-06 09:47:55 +00:00
|
|
|
|
virtual DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
2015-09-20 02:03:12 +00:00
|
|
|
|
{
|
|
|
|
|
const IDataType & type = *arguments[0];
|
|
|
|
|
|
|
|
|
|
if (!type.behavesAsNumber())
|
|
|
|
|
throw Exception("Cannot format " + type.getName() + " as size in bytes", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeString>();
|
2015-09-20 02:03:12 +00:00
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2012-12-26 16:27:46 +00:00
|
|
|
|
/// Выполнить функцию над блоком.
|
2016-07-06 09:47:55 +00:00
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2012-12-26 16:27:46 +00:00
|
|
|
|
{
|
|
|
|
|
if (!( executeType<UInt8>(block, arguments, result)
|
|
|
|
|
|| executeType<UInt16>(block, arguments, result)
|
|
|
|
|
|| executeType<UInt32>(block, arguments, result)
|
|
|
|
|
|| executeType<UInt64>(block, arguments, result)
|
|
|
|
|
|| executeType<Int8>(block, arguments, result)
|
|
|
|
|
|| executeType<Int16>(block, arguments, result)
|
|
|
|
|
|| executeType<Int32>(block, arguments, result)
|
2015-09-20 02:03:12 +00:00
|
|
|
|
|| executeType<Int64>(block, arguments, result)
|
|
|
|
|
|| executeType<Float32>(block, arguments, result)
|
|
|
|
|
|| executeType<Float64>(block, arguments, result)))
|
2017-01-02 20:12:12 +00:00
|
|
|
|
throw Exception("Illegal column " + block.safeGetByPosition(arguments[0]).column->getName()
|
2015-09-20 02:03:12 +00:00
|
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2012-12-26 16:27:46 +00:00
|
|
|
|
}
|
2015-09-20 02:03:12 +00:00
|
|
|
|
|
2012-12-26 16:27:46 +00:00
|
|
|
|
private:
|
2015-09-20 02:03:12 +00:00
|
|
|
|
template <typename T>
|
|
|
|
|
bool executeType(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
|
if (const ColumnVector<T> * col_from = typeid_cast<const ColumnVector<T> *>(block.safeGetByPosition(arguments[0]).column.get()))
|
2015-09-20 02:03:12 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
auto col_to = std::make_shared<ColumnString>();
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = col_to;
|
2015-09-20 02:03:12 +00:00
|
|
|
|
|
|
|
|
|
const typename ColumnVector<T>::Container_t & vec_from = col_from->getData();
|
|
|
|
|
ColumnString::Chars_t & data_to = col_to->getChars();
|
|
|
|
|
ColumnString::Offsets_t & offsets_to = col_to->getOffsets();
|
|
|
|
|
size_t size = vec_from.size();
|
|
|
|
|
data_to.resize(size * 2);
|
|
|
|
|
offsets_to.resize(size);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2015-09-20 02:03:12 +00:00
|
|
|
|
WriteBufferFromVector<ColumnString::Chars_t> buf_to(data_to);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
{
|
|
|
|
|
formatReadableSizeWithBinarySuffix(vec_from[i], buf_to);
|
|
|
|
|
writeChar(0, buf_to);
|
|
|
|
|
offsets_to[i] = buf_to.count();
|
|
|
|
|
}
|
|
|
|
|
data_to.resize(buf_to.count());
|
|
|
|
|
}
|
2017-01-02 20:12:12 +00:00
|
|
|
|
else if (const ColumnConst<T> * col_from = typeid_cast<const ColumnConst<T> *>(block.safeGetByPosition(arguments[0]).column.get()))
|
2015-09-20 02:03:12 +00:00
|
|
|
|
{
|
2017-01-02 20:12:12 +00:00
|
|
|
|
block.safeGetByPosition(result).column = std::make_shared<ColumnConstString>(col_from->size(), formatReadableSizeWithBinarySuffix(col_from->getData()));
|
2015-09-20 02:03:12 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-12-26 16:27:46 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|