2017-04-01 09:19:00 +00:00
|
|
|
#include <Functions/IFunction.h>
|
2017-07-21 06:35:58 +00:00
|
|
|
#include <Functions/FunctionHelpers.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
|
|
|
#include <Columns/ColumnVector.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <IO/WriteBufferFromVector.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Common/formatReadable.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2019-02-09 19:44:27 +00:00
|
|
|
#include <type_traits>
|
2012-12-26 16:27:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-06-13 02:06:53 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-13 22:19:04 +00:00
|
|
|
/** Function for an unusual conversion to a string:
|
2017-04-01 07:20:54 +00:00
|
|
|
*
|
2017-05-13 22:19:04 +00:00
|
|
|
* bitmaskToList - takes an integer - a bitmask, returns a string of degrees of 2 separated by a comma.
|
|
|
|
* for example, bitmaskToList(50) = '2,16,32'
|
2017-04-01 07:20:54 +00:00
|
|
|
*
|
2017-05-13 22:19:04 +00:00
|
|
|
* formatReadableSize - prints the transferred size in bytes in form `123.45 GiB`.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
2012-12-26 16:27:46 +00:00
|
|
|
|
|
|
|
class FunctionBitmaskToList : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "bitmaskToList";
|
2017-12-02 02:47:12 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionBitmaskToList>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
bool isInjective(const Block &) override { return true; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
2018-09-07 14:37:26 +00:00
|
|
|
const DataTypePtr & type = arguments[0];
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!isInteger(type))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Cannot format " + type->getName() + " as bitmask string", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2018-04-24 07:16:39 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-11-24 01:48:06 +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-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-06-13 02:06:53 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2015-09-20 02:03:12 +00:00
|
|
|
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename T>
|
|
|
|
inline static void writeBitmask(T x, WriteBuffer & out)
|
|
|
|
{
|
2019-02-09 21:03:42 +00:00
|
|
|
using UnsignedT = std::make_unsigned_t<T>;
|
|
|
|
UnsignedT u_x = x;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool first = true;
|
2019-02-09 21:03:42 +00:00
|
|
|
while (u_x)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2019-02-09 21:03:42 +00:00
|
|
|
UnsignedT y = u_x & (u_x - 1);
|
|
|
|
UnsignedT bit = u_x ^ y;
|
|
|
|
u_x = y;
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!first)
|
2019-02-09 19:44:27 +00:00
|
|
|
writeChar(',', out);
|
2017-04-01 07:20:54 +00:00
|
|
|
first = false;
|
2019-02-09 21:03:42 +00:00
|
|
|
writeIntText(T(bit), out);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool executeType(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const ColumnVector<T> * col_from = checkAndGetColumn<ColumnVector<T>>(block.getByPosition(arguments[0]).column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_to = ColumnString::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
const typename ColumnVector<T>::Container & vec_from = col_from->getData();
|
2018-11-25 00:08:50 +00:00
|
|
|
ColumnString::Chars & data_to = col_to->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offsets & offsets_to = col_to->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = vec_from.size();
|
|
|
|
data_to.resize(size * 2);
|
|
|
|
offsets_to.resize(size);
|
|
|
|
|
2018-11-25 00:08:50 +00:00
|
|
|
WriteBufferFromVector<ColumnString::Chars> buf_to(data_to);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
2019-02-09 21:03:42 +00:00
|
|
|
writeBitmask<T>(vec_from[i], buf_to);
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar(0, buf_to);
|
|
|
|
offsets_to[i] = buf_to.count();
|
|
|
|
}
|
2017-12-16 05:21:04 +00:00
|
|
|
|
2018-11-30 15:36:41 +00:00
|
|
|
buf_to.finish();
|
2017-12-16 05:21:04 +00:00
|
|
|
block.getByPosition(result).column = std::move(col_to);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-09-20 02:03:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class FunctionFormatReadableSize : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto name = "formatReadableSize";
|
2017-12-02 02:47:12 +00:00
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionFormatReadableSize>(); }
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 1; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
|
|
|
|
{
|
|
|
|
const IDataType & type = *arguments[0];
|
|
|
|
|
2018-09-07 14:37:26 +00:00
|
|
|
if (!isNumber(type))
|
2017-04-01 07:20:54 +00:00
|
|
|
throw Exception("Cannot format " + type.getName() + " as size in bytes", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
|
2017-07-23 08:40:43 +00:00
|
|
|
bool useDefaultImplementationForConstants() const override { return true; }
|
|
|
|
|
2018-04-24 07:16:39 +00:00
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2018-11-24 01:48:06 +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)
|
|
|
|
|| executeType<Float32>(block, arguments, result)
|
|
|
|
|| executeType<Float64>(block, arguments, result)))
|
2017-07-21 06:35:58 +00:00
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
2017-04-01 07:20:54 +00:00
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
}
|
2015-09-20 02:03:12 +00:00
|
|
|
|
2012-12-26 16:27:46 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename T>
|
|
|
|
bool executeType(Block & block, const ColumnNumbers & arguments, size_t result)
|
|
|
|
{
|
2017-07-21 06:35:58 +00:00
|
|
|
if (const ColumnVector<T> * col_from = checkAndGetColumn<ColumnVector<T>>(block.getByPosition(arguments[0]).column.get()))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-14 01:43:19 +00:00
|
|
|
auto col_to = ColumnString::create();
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-15 21:32:25 +00:00
|
|
|
const typename ColumnVector<T>::Container & vec_from = col_from->getData();
|
2018-11-25 00:08:50 +00:00
|
|
|
ColumnString::Chars & data_to = col_to->getChars();
|
2017-12-15 21:32:25 +00:00
|
|
|
ColumnString::Offsets & offsets_to = col_to->getOffsets();
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t size = vec_from.size();
|
|
|
|
data_to.resize(size * 2);
|
|
|
|
offsets_to.resize(size);
|
|
|
|
|
2018-11-25 00:08:50 +00:00
|
|
|
WriteBufferFromVector<ColumnString::Chars> buf_to(data_to);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
2019-01-07 19:56:53 +00:00
|
|
|
formatReadableSizeWithBinarySuffix(static_cast<double>(vec_from[i]), buf_to);
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar(0, buf_to);
|
|
|
|
offsets_to[i] = buf_to.count();
|
|
|
|
}
|
2017-12-16 05:21:04 +00:00
|
|
|
|
2018-11-30 15:36:41 +00:00
|
|
|
buf_to.finish();
|
2017-12-16 05:21:04 +00:00
|
|
|
block.getByPosition(result).column = std::move(col_to);
|
|
|
|
return true;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 05:21:04 +00:00
|
|
|
return false;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2012-12-26 16:27:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|