2011-11-07 01:15:37 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2013-12-17 13:49:21 +00:00
|
|
|
|
#include <Poco/Net/DNS.h>
|
2016-02-09 17:06:50 +00:00
|
|
|
|
#include <common/ClickHouseRevision.h>
|
2013-12-17 13:49:21 +00:00
|
|
|
|
|
2015-06-03 13:11:59 +00:00
|
|
|
|
#include <DB/Core/Defines.h>
|
2015-10-12 07:05:54 +00:00
|
|
|
|
#include <DB/Core/FieldVisitors.h>
|
2012-08-26 11:14:52 +00:00
|
|
|
|
#include <DB/IO/WriteBufferFromString.h>
|
2011-11-07 01:15:37 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
|
|
|
|
#include <DB/DataTypes/DataTypeString.h>
|
|
|
|
|
#include <DB/DataTypes/DataTypeFixedString.h>
|
|
|
|
|
#include <DB/DataTypes/DataTypeDate.h>
|
|
|
|
|
#include <DB/DataTypes/DataTypeDateTime.h>
|
2012-08-23 18:43:57 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypeTuple.h>
|
2012-08-26 11:14:52 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypeArray.h>
|
2015-06-06 00:28:37 +00:00
|
|
|
|
#include <DB/DataTypes/DataTypeAggregateFunction.h>
|
2011-11-07 01:15:37 +00:00
|
|
|
|
#include <DB/Columns/ColumnString.h>
|
|
|
|
|
#include <DB/Columns/ColumnFixedString.h>
|
|
|
|
|
#include <DB/Columns/ColumnConst.h>
|
|
|
|
|
#include <DB/Columns/ColumnVector.h>
|
2012-08-23 18:05:31 +00:00
|
|
|
|
#include <DB/Columns/ColumnSet.h>
|
2012-08-23 18:43:57 +00:00
|
|
|
|
#include <DB/Columns/ColumnTuple.h>
|
2012-08-26 11:14:52 +00:00
|
|
|
|
#include <DB/Columns/ColumnArray.h>
|
2015-06-06 00:28:37 +00:00
|
|
|
|
#include <DB/Columns/ColumnAggregateFunction.h>
|
2014-10-25 20:27:37 +00:00
|
|
|
|
#include <DB/Common/UnicodeBar.h>
|
2011-11-07 01:15:37 +00:00
|
|
|
|
#include <DB/Functions/IFunction.h>
|
2015-05-04 17:52:19 +00:00
|
|
|
|
#include <DB/Interpreters/ExpressionActions.h>
|
2015-10-05 00:33:43 +00:00
|
|
|
|
#include <ext/range.hpp>
|
2011-11-07 01:15:37 +00:00
|
|
|
|
|
2015-06-03 13:11:59 +00:00
|
|
|
|
#include <cmath>
|
2011-11-07 01:15:37 +00:00
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
|
namespace ErrorCodes
|
|
|
|
|
{
|
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
|
extern const int ILLEGAL_INDEX;
|
|
|
|
|
extern const int FUNCTION_IS_SPECIAL;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-07 01:15:37 +00:00
|
|
|
|
/** Вспомогательные функции:
|
|
|
|
|
*
|
2013-03-04 10:38:44 +00:00
|
|
|
|
* visibleWidth(x) - вычисляет приблизительную ширину при выводе значения в текстовом (tab-separated) виде на консоль.
|
2014-06-26 00:58:14 +00:00
|
|
|
|
*
|
2013-03-04 10:38:44 +00:00
|
|
|
|
* toTypeName(x) - получить имя типа
|
|
|
|
|
* blockSize() - получить размер блока
|
|
|
|
|
* materialize(x) - материализовать константу
|
2012-08-26 02:08:18 +00:00
|
|
|
|
* ignore(...) - функция, принимающая любые аргументы, и всегда возвращающая 0.
|
2013-03-04 10:38:44 +00:00
|
|
|
|
* sleep(seconds) - спит указанное количество секунд каждый блок.
|
2014-06-26 00:58:14 +00:00
|
|
|
|
*
|
2013-03-04 10:38:44 +00:00
|
|
|
|
* in(x, set) - функция для вычисления оператора IN
|
|
|
|
|
* notIn(x, set) - и NOT IN.
|
2012-08-23 18:05:31 +00:00
|
|
|
|
*
|
|
|
|
|
* tuple(x, y, ...) - функция, позволяющая сгруппировать несколько столбцов
|
2012-08-26 02:08:18 +00:00
|
|
|
|
* tupleElement(tuple, n) - функция, позволяющая достать столбец из tuple.
|
2012-08-27 05:13:14 +00:00
|
|
|
|
*
|
2013-03-04 10:38:44 +00:00
|
|
|
|
* arrayJoin(arr) - особая функция - выполнить её напрямую нельзя;
|
2012-08-27 05:13:14 +00:00
|
|
|
|
* используется только чтобы получить тип результата соответствующего выражения.
|
2013-09-18 20:06:49 +00:00
|
|
|
|
*
|
2015-06-19 02:34:47 +00:00
|
|
|
|
* replicate(x, arr) - создаёт массив такого же размера как arr, все элементы которого равны x;
|
|
|
|
|
* например: replicate(1, ['a', 'b', 'c']) = [1, 1, 1].
|
2013-09-18 20:06:49 +00:00
|
|
|
|
*
|
2013-03-18 10:49:31 +00:00
|
|
|
|
* sleep(n) - спит n секунд каждый блок.
|
2014-08-09 01:51:03 +00:00
|
|
|
|
*
|
|
|
|
|
* bar(x, min, max, width) - рисует полосу из количества символов, пропорционального (x - min) и равного width при x == max.
|
2015-06-03 13:20:19 +00:00
|
|
|
|
*
|
|
|
|
|
* version() - возвращает текущую версию сервера в строке.
|
2015-06-06 00:28:37 +00:00
|
|
|
|
*
|
|
|
|
|
* finalizeAggregation(agg_state) - по состоянию агрегации получить результат.
|
|
|
|
|
*
|
|
|
|
|
* runningAccumulate(agg_state) - принимает состояния агрегатной функции и возвращает столбец со значениями,
|
|
|
|
|
* являющимися результатом накопления этих состояний для множества строк блока, от первой до текущей строки.
|
2011-11-07 01:15:37 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline UInt64 stringWidth(const UInt8 * pos, const UInt8 * end)
|
|
|
|
|
{
|
|
|
|
|
UInt64 res = 0;
|
|
|
|
|
for (; pos < end; ++pos)
|
|
|
|
|
{
|
|
|
|
|
if (*pos == '\b' || *pos == '\f' || *pos == '\n' || *pos == '\r' || *pos == '\t' || *pos == '\0' || *pos == '\'' || *pos == '\\')
|
|
|
|
|
++res;
|
|
|
|
|
if (*pos <= 0x7F || *pos >= 0xC0)
|
|
|
|
|
++res;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-26 19:00:13 +00:00
|
|
|
|
static inline void stringWidthConstant(const String & data, UInt64 & res)
|
2011-11-07 01:15:37 +00:00
|
|
|
|
{
|
|
|
|
|
res = stringWidth(reinterpret_cast<const UInt8 *>(data.data()), reinterpret_cast<const UInt8 *>(data.data()) + data.size());
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-23 15:28:21 +00:00
|
|
|
|
class FunctionCurrentDatabase : public IFunction
|
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
const String db_name;
|
2014-09-23 15:28:21 +00:00
|
|
|
|
|
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = "currentDatabase";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionCurrentDatabase>(context.getCurrentDatabase()); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
|
|
|
|
|
explicit FunctionCurrentDatabase(const String & db_name) : db_name{db_name} {}
|
2014-09-23 15:28:21 +00:00
|
|
|
|
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override {
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2014-09-23 15:28:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-11 23:36:45 +00:00
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
2014-09-23 15:28:21 +00:00
|
|
|
|
{
|
|
|
|
|
if (arguments.size() != 0)
|
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
|
+ toString(arguments.size()) + ", should be 0.",
|
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeString>();
|
2014-09-23 15:28:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-07 04:56:19 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, const size_t result) override
|
2014-09-23 15:28:21 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
block.getByPosition(result).column = std::make_shared<ColumnConstString>(
|
|
|
|
|
block.rowsInFirstColumn(), db_name);
|
2014-09-23 15:28:21 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-29 00:33:56 +00:00
|
|
|
|
|
2013-12-17 13:49:21 +00:00
|
|
|
|
/// Получить имя хоста. (Оно - константа, вычисляется один раз за весь запрос.)
|
|
|
|
|
class FunctionHostName : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = "hostName";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionHostName>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
|
2013-12-17 13:49:21 +00:00
|
|
|
|
/// Получить имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override
|
2013-12-17 13:49:21 +00:00
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2013-12-17 13:49:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
2013-12-17 13:49:21 +00:00
|
|
|
|
{
|
|
|
|
|
if (arguments.size() != 0)
|
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
|
+ toString(arguments.size()) + ", should be 0.",
|
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeString>();
|
2013-12-17 13:49:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-10-13 14:35:40 +00:00
|
|
|
|
/** Выполнить функцию над блоком. convertToFullColumn вызывается для того, чтобы в случае
|
|
|
|
|
* распределенного выполнения запроса каждый сервер возвращал свое имя хоста. */
|
2015-10-11 23:36:45 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2013-12-17 13:49:21 +00:00
|
|
|
|
{
|
2013-12-19 19:01:08 +00:00
|
|
|
|
block.getByPosition(result).column = ColumnConstString(
|
2013-12-17 13:49:21 +00:00
|
|
|
|
block.rowsInFirstColumn(),
|
2013-12-19 19:01:08 +00:00
|
|
|
|
Poco::Net::DNS::hostName()).convertToFullColumn();
|
2013-12-17 13:49:21 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2011-11-07 01:15:37 +00:00
|
|
|
|
|
2015-05-29 00:33:56 +00:00
|
|
|
|
|
2011-11-07 01:15:37 +00:00
|
|
|
|
class FunctionVisibleWidth : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = "visibleWidth";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionVisibleWidth>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
|
2011-11-07 01:15:37 +00:00
|
|
|
|
/// Получить имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override
|
2011-11-07 01:15:37 +00:00
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2011-11-07 01:15:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
2011-11-07 01:15:37 +00:00
|
|
|
|
{
|
|
|
|
|
if (arguments.size() != 1)
|
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
2013-06-21 20:34:19 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 1.",
|
2011-11-07 01:15:37 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeUInt64>();
|
2011-11-07 01:15:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override;
|
2011-11-07 01:15:37 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-07-15 23:13:08 +00:00
|
|
|
|
|
|
|
|
|
class FunctionToTypeName : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = "toTypeName";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionToTypeName>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
|
2012-07-15 23:13:08 +00:00
|
|
|
|
/// Получить имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override
|
2012-07-15 23:13:08 +00:00
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2012-07-15 23:13:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
2012-07-15 23:13:08 +00:00
|
|
|
|
{
|
2012-08-03 19:40:08 +00:00
|
|
|
|
if (arguments.size() != 1)
|
2012-07-15 23:13:08 +00:00
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
2013-06-21 20:34:19 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 1.",
|
2012-07-15 23:13:08 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeString>();
|
2012-07-15 23:13:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2012-07-15 23:13:08 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
block.getByPosition(result).column = std::make_shared<ColumnConstString>(block.rowsInFirstColumn(), block.getByPosition(arguments[0]).type->getName());
|
2012-07-15 23:13:08 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-08-03 19:40:08 +00:00
|
|
|
|
|
|
|
|
|
class FunctionBlockSize : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = "blockSize";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionBlockSize>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
|
2012-08-03 19:40:08 +00:00
|
|
|
|
/// Получить имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override
|
2012-08-03 19:40:08 +00:00
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2012-08-03 19:40:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
2012-08-03 19:40:08 +00:00
|
|
|
|
{
|
|
|
|
|
if (!arguments.empty())
|
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
2013-06-21 20:34:19 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 0.",
|
2012-08-03 19:40:08 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeUInt64>();
|
2012-08-03 19:40:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2012-08-03 19:40:08 +00:00
|
|
|
|
{
|
2013-06-08 20:19:29 +00:00
|
|
|
|
size_t size = block.rowsInFirstColumn();
|
2012-08-03 19:52:43 +00:00
|
|
|
|
block.getByPosition(result).column = ColumnConstUInt64(size, size).convertToFullColumn();
|
2012-08-03 19:40:08 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2016-02-23 03:20:48 +00:00
|
|
|
|
class FunctionRowNumberInBlock : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "rowNumberInBlock";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionRowNumberInBlock>(); }
|
2016-02-23 03:20:48 +00:00
|
|
|
|
|
|
|
|
|
/// Получить имя функции.
|
|
|
|
|
String getName() const override
|
|
|
|
|
{
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
|
|
|
|
{
|
|
|
|
|
if (!arguments.empty())
|
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
|
+ toString(arguments.size()) + ", should be 0.",
|
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeUInt64>();
|
2016-02-23 03:20:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
|
|
|
|
{
|
|
|
|
|
size_t size = block.rowsInFirstColumn();
|
2016-05-28 05:31:36 +00:00
|
|
|
|
auto column = std::make_shared<ColumnUInt64>();
|
2016-02-23 03:20:48 +00:00
|
|
|
|
block.getByPosition(result).column = column;
|
|
|
|
|
auto & data = column->getData();
|
|
|
|
|
data.resize(size);
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
data[i] = i;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2016-05-17 18:25:47 +00:00
|
|
|
|
/** Инкрементальный номер блока среди вызовов этой функции. */
|
|
|
|
|
class FunctionBlockNumber : public IFunction
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
std::atomic<size_t> block_number {0};
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "blockNumber";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionBlockNumber>(); }
|
2016-05-17 18:25:47 +00:00
|
|
|
|
|
|
|
|
|
/// Получить имя функции.
|
|
|
|
|
String getName() const override
|
|
|
|
|
{
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
|
|
|
|
{
|
|
|
|
|
if (!arguments.empty())
|
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
|
+ toString(arguments.size()) + ", should be 0.",
|
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeUInt64>();
|
2016-05-17 18:25:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
|
|
|
|
{
|
|
|
|
|
size_t current_block_number = block_number++;
|
|
|
|
|
block.getByPosition(result).column = ColumnConstUInt64(block.rowsInFirstColumn(), current_block_number).convertToFullColumn();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2013-03-04 10:38:44 +00:00
|
|
|
|
class FunctionSleep : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = "sleep";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionSleep>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
|
2013-03-04 10:38:44 +00:00
|
|
|
|
/// Получить имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override
|
2013-03-04 10:38:44 +00:00
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2013-03-04 10:38:44 +00:00
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-03-04 10:38:44 +00:00
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
2013-03-04 10:38:44 +00:00
|
|
|
|
{
|
|
|
|
|
if (arguments.size() != 1)
|
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
2013-06-21 20:34:19 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 1.",
|
2013-03-04 10:38:44 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
|
if (!typeid_cast<const DataTypeFloat64 *>(&*arguments[0]) &&
|
|
|
|
|
!typeid_cast<const DataTypeFloat32 *>(&*arguments[0]) &&
|
|
|
|
|
!typeid_cast<const DataTypeUInt64 *>(&*arguments[0]) &&
|
|
|
|
|
!typeid_cast<const DataTypeUInt32 *>(&*arguments[0]) &&
|
|
|
|
|
!typeid_cast<const DataTypeUInt16 *>(&*arguments[0]) &&
|
|
|
|
|
!typeid_cast<const DataTypeUInt8 *>(&*arguments[0]))
|
2013-03-04 10:38:44 +00:00
|
|
|
|
throw Exception("Illegal type " + arguments[0]->getName() + " of argument of function " + getName() + ", expected Float64",
|
|
|
|
|
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<DataTypeUInt8>();
|
2013-03-04 10:38:44 +00:00
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-03-04 10:38:44 +00:00
|
|
|
|
/// Выполнить функцию над блоком.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2013-03-04 10:38:44 +00:00
|
|
|
|
{
|
2013-03-07 18:19:22 +00:00
|
|
|
|
IColumn * col = &*block.getByPosition(arguments[0]).column;
|
|
|
|
|
double seconds;
|
2013-06-10 13:42:05 +00:00
|
|
|
|
size_t size = col->size();
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
|
if (ColumnConst<Float64> * column = typeid_cast<ColumnConst<Float64> *>(col))
|
2013-06-10 13:42:05 +00:00
|
|
|
|
seconds = column->getData();
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
|
else if (ColumnConst<Float32> * column = typeid_cast<ColumnConst<Float32> *>(col))
|
2013-06-10 13:42:05 +00:00
|
|
|
|
seconds = static_cast<double>(column->getData());
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
|
else if (ColumnConst<UInt64> * column = typeid_cast<ColumnConst<UInt64> *>(col))
|
2013-06-10 13:42:05 +00:00
|
|
|
|
seconds = static_cast<double>(column->getData());
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
|
else if (ColumnConst<UInt32> * column = typeid_cast<ColumnConst<UInt32> *>(col))
|
2013-06-10 13:42:05 +00:00
|
|
|
|
seconds = static_cast<double>(column->getData());
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
|
else if (ColumnConst<UInt16> * column = typeid_cast<ColumnConst<UInt16> *>(col))
|
2013-06-10 13:42:05 +00:00
|
|
|
|
seconds = static_cast<double>(column->getData());
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
|
else if (ColumnConst<UInt8> * column = typeid_cast<ColumnConst<UInt8> *>(col))
|
2013-06-10 13:42:05 +00:00
|
|
|
|
seconds = static_cast<double>(column->getData());
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-03-07 18:19:22 +00:00
|
|
|
|
else
|
2013-03-04 10:38:44 +00:00
|
|
|
|
throw Exception("The argument of function " + getName() + " must be constant.", ErrorCodes::ILLEGAL_COLUMN);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-06-10 13:42:05 +00:00
|
|
|
|
/// Не спим, если блок пустой.
|
|
|
|
|
if (size > 0)
|
|
|
|
|
usleep(static_cast<unsigned>(seconds * 1e6));
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-03-07 18:19:22 +00:00
|
|
|
|
block.getByPosition(result).column = ColumnConst<UInt8>(size, 0).convertToFullColumn();
|
2013-03-04 10:38:44 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-08-03 19:40:08 +00:00
|
|
|
|
class FunctionMaterialize : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = "materialize";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionMaterialize>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
|
2012-08-03 19:40:08 +00:00
|
|
|
|
/// Получить имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override
|
2012-08-03 19:40:08 +00:00
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2012-08-03 19:40:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
2012-08-03 19:40:08 +00:00
|
|
|
|
{
|
|
|
|
|
if (arguments.size() != 1)
|
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
2013-06-21 20:34:19 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 1.",
|
2012-08-03 19:40:08 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
return arguments[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2012-08-03 19:40:08 +00:00
|
|
|
|
{
|
2015-10-29 00:17:50 +00:00
|
|
|
|
const auto & src = block.getByPosition(arguments[0]).column;
|
|
|
|
|
if (auto converted = src->convertToFullColumnIfConst())
|
|
|
|
|
block.getByPosition(result).column = converted;
|
|
|
|
|
else
|
|
|
|
|
block.getByPosition(result).column = src;
|
2012-08-03 19:40:08 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
template <bool negative, bool global> struct FunctionInName;
|
|
|
|
|
template <> struct FunctionInName<false, false> { static constexpr auto name = "in"; };
|
|
|
|
|
template <> struct FunctionInName<false, true> { static constexpr auto name = "globalIn"; };
|
|
|
|
|
template <> struct FunctionInName<true, false> { static constexpr auto name = "notIn"; };
|
|
|
|
|
template <> struct FunctionInName<true, true> { static constexpr auto name = "globalNotIn"; };
|
2012-08-23 18:05:31 +00:00
|
|
|
|
|
2014-11-12 17:23:26 +00:00
|
|
|
|
template <bool negative, bool global>
|
2012-08-23 18:05:31 +00:00
|
|
|
|
class FunctionIn : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = FunctionInName<negative, global>::name;
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionIn>(); }
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2012-08-23 18:05:31 +00:00
|
|
|
|
/// Получить имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override
|
2012-08-23 18:05:31 +00:00
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2012-08-23 18:05:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
2012-08-23 18:05:31 +00:00
|
|
|
|
{
|
|
|
|
|
if (arguments.size() != 2)
|
2012-08-23 19:38:45 +00:00
|
|
|
|
throw Exception("Number of arguments for function '" + getName() + "' doesn't match: passed "
|
2013-06-21 20:34:19 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 2.",
|
2012-08-23 18:05:31 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
2012-08-23 18:05:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2012-08-23 18:05:31 +00:00
|
|
|
|
{
|
|
|
|
|
/// Второй аргумент - обязательно ColumnSet.
|
2013-06-26 16:31:49 +00:00
|
|
|
|
ColumnPtr column_set_ptr = block.getByPosition(arguments[1]).column;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
const ColumnSet * column_set = typeid_cast<const ColumnSet *>(&*column_set_ptr);
|
2012-08-23 18:05:31 +00:00
|
|
|
|
if (!column_set)
|
2013-06-26 16:31:49 +00:00
|
|
|
|
throw Exception("Second argument for function '" + getName() + "' must be Set; found " + column_set_ptr->getName(),
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
2012-08-23 18:05:31 +00:00
|
|
|
|
|
2015-10-08 03:41:11 +00:00
|
|
|
|
Block block_of_key_columns;
|
2012-08-23 18:05:31 +00:00
|
|
|
|
|
2012-08-23 18:43:57 +00:00
|
|
|
|
/// Первый аргумент может быть tuple или одиночным столбцом.
|
2014-06-26 00:58:14 +00:00
|
|
|
|
const ColumnTuple * tuple = typeid_cast<const ColumnTuple *>(&*block.getByPosition(arguments[0]).column);
|
2012-08-23 18:43:57 +00:00
|
|
|
|
if (tuple)
|
2015-10-08 03:41:11 +00:00
|
|
|
|
block_of_key_columns = tuple->getData();
|
2012-08-23 18:43:57 +00:00
|
|
|
|
else
|
2015-10-08 03:41:11 +00:00
|
|
|
|
block_of_key_columns.insert(block.getByPosition(arguments[0]));
|
2012-08-23 18:43:57 +00:00
|
|
|
|
|
2015-10-08 03:41:11 +00:00
|
|
|
|
block.getByPosition(result).column = column_set->getData()->execute(block_of_key_columns, negative);
|
2012-08-23 18:43:57 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FunctionTuple : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = "tuple";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionTuple>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
|
2012-08-23 18:43:57 +00:00
|
|
|
|
/// Получить имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override
|
2012-08-23 18:43:57 +00:00
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2012-08-23 18:43:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
2012-08-23 18:43:57 +00:00
|
|
|
|
{
|
2015-09-11 20:58:44 +00:00
|
|
|
|
if (arguments.size() < 1)
|
|
|
|
|
throw Exception("Function " + getName() + " requires at least one argument.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeTuple>(arguments);
|
2012-08-23 18:43:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2012-08-23 18:43:57 +00:00
|
|
|
|
{
|
|
|
|
|
Block tuple_block;
|
|
|
|
|
|
|
|
|
|
for (ColumnNumbers::const_iterator it = arguments.begin(); it != arguments.end(); ++it)
|
|
|
|
|
tuple_block.insert(block.getByPosition(*it));
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
block.getByPosition(result).column = std::make_shared<ColumnTuple>(tuple_block);
|
2012-08-23 18:05:31 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-08-26 02:08:18 +00:00
|
|
|
|
|
|
|
|
|
class FunctionTupleElement : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = "tupleElement";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionTupleElement>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
|
2012-08-26 02:08:18 +00:00
|
|
|
|
/// Получить имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override
|
2012-08-26 02:08:18 +00:00
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2012-08-26 02:08:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-17 01:27:35 +00:00
|
|
|
|
void getReturnTypeAndPrerequisites(const ColumnsWithTypeAndName & arguments,
|
2013-06-04 13:47:18 +00:00
|
|
|
|
DataTypePtr & out_return_type,
|
2015-10-11 23:36:45 +00:00
|
|
|
|
ExpressionActions::Actions & out_prerequisites) override
|
2013-06-04 13:34:46 +00:00
|
|
|
|
{
|
|
|
|
|
if (arguments.size() != 2)
|
2014-08-09 01:51:03 +00:00
|
|
|
|
throw Exception("Function " + getName() + " requires exactly two arguments: tuple and element index.",
|
2013-06-04 13:34:46 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
|
const ColumnConstUInt8 * index_col = typeid_cast<const ColumnConstUInt8 *>(&*arguments[1].column);
|
2013-06-04 13:34:46 +00:00
|
|
|
|
if (!index_col)
|
2014-08-09 01:51:03 +00:00
|
|
|
|
throw Exception("Second argument to " + getName() + " must be a constant UInt8", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-06-04 13:34:46 +00:00
|
|
|
|
size_t index = index_col->getData();
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
|
|
|
|
const DataTypeTuple * tuple = typeid_cast<const DataTypeTuple *>(&*arguments[0].type);
|
2013-06-04 13:34:46 +00:00
|
|
|
|
if (!tuple)
|
2014-08-09 01:51:03 +00:00
|
|
|
|
throw Exception("First argument for function " + getName() + " must be tuple.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-06-04 13:34:46 +00:00
|
|
|
|
if (index == 0)
|
|
|
|
|
throw Exception("Indices in tuples are 1-based.", ErrorCodes::ILLEGAL_INDEX);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-06-04 13:34:46 +00:00
|
|
|
|
const DataTypes & elems = tuple->getElements();
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-06-04 13:34:46 +00:00
|
|
|
|
if (index > elems.size())
|
|
|
|
|
throw Exception("Index for tuple element is out of range.", ErrorCodes::ILLEGAL_INDEX);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2013-06-04 13:34:46 +00:00
|
|
|
|
out_return_type = elems[index - 1]->clone();
|
|
|
|
|
}
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2012-08-26 02:08:18 +00:00
|
|
|
|
/// Выполнить функцию над блоком.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2012-08-26 02:08:18 +00:00
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
const ColumnTuple * tuple_col = typeid_cast<const ColumnTuple *>(&*block.getByPosition(arguments[0]).column);
|
|
|
|
|
const ColumnConstUInt8 * index_col = typeid_cast<const ColumnConstUInt8 *>(&*block.getByPosition(arguments[1]).column);
|
2012-08-26 02:08:18 +00:00
|
|
|
|
|
|
|
|
|
if (!tuple_col)
|
2014-08-09 01:51:03 +00:00
|
|
|
|
throw Exception("First argument for function " + getName() + " must be tuple.", ErrorCodes::ILLEGAL_COLUMN);
|
2012-08-26 02:08:18 +00:00
|
|
|
|
|
|
|
|
|
if (!index_col)
|
2014-08-09 01:51:03 +00:00
|
|
|
|
throw Exception("Second argument for function " + getName() + " must be UInt8 constant literal.", ErrorCodes::ILLEGAL_COLUMN);
|
2012-08-26 02:08:18 +00:00
|
|
|
|
|
|
|
|
|
size_t index = index_col->getData();
|
|
|
|
|
if (index == 0)
|
|
|
|
|
throw Exception("Indices in tuples is 1-based.", ErrorCodes::ILLEGAL_INDEX);
|
|
|
|
|
|
|
|
|
|
const Block & tuple_block = tuple_col->getData();
|
|
|
|
|
|
|
|
|
|
if (index > tuple_block.columns())
|
|
|
|
|
throw Exception("Index for tuple element is out of range.", ErrorCodes::ILLEGAL_INDEX);
|
|
|
|
|
|
|
|
|
|
block.getByPosition(result).column = tuple_block.getByPosition(index - 1).column;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FunctionIgnore : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = "ignore";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionIgnore>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
|
2016-04-15 23:10:29 +00:00
|
|
|
|
String getName() const override { return name; }
|
2016-05-28 07:48:40 +00:00
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override { return std::make_shared<DataTypeUInt8>(); }
|
2012-08-26 02:08:18 +00:00
|
|
|
|
|
2016-04-15 23:10:29 +00:00
|
|
|
|
/// Выполнить функцию над блоком.
|
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2012-08-26 02:08:18 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
block.getByPosition(result).column = std::make_shared<ColumnConstUInt8>(block.rowsInFirstColumn(), 0);
|
2012-08-26 02:08:18 +00:00
|
|
|
|
}
|
2016-04-15 23:10:29 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Функция indexHint принимает любое количество любых аргументов и всегда возвращает единицу.
|
|
|
|
|
*
|
|
|
|
|
* Эта функция имеет особый смысл (см. ExpressionAnalyzer, PKCondition):
|
|
|
|
|
* - расположенные внутри неё выражения не вычисляются;
|
|
|
|
|
* - но при анализе индекса (выбора диапазонов для чтения), эта функция воспринимается так же,
|
|
|
|
|
* как если бы вместо её применения было бы само выражение.
|
|
|
|
|
*
|
|
|
|
|
* Пример: WHERE something AND indexHint(CounterID = 34)
|
|
|
|
|
* - не читать и не вычислять CounterID = 34, но выбрать диапазоны, в которых выражение CounterID = 34 может быть истинным.
|
|
|
|
|
*
|
|
|
|
|
* Функция может использоваться в отладочных целях, а также для (скрытых от пользователя) преобразований запроса.
|
|
|
|
|
*/
|
|
|
|
|
class FunctionIndexHint : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "indexHint";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionIndexHint>(); }
|
2016-04-15 23:10:29 +00:00
|
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
2016-05-28 07:48:40 +00:00
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override { return std::make_shared<DataTypeUInt8>(); }
|
2012-08-26 02:08:18 +00:00
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2012-08-26 02:08:18 +00:00
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
block.getByPosition(result).column = std::make_shared<ColumnConstUInt8>(block.rowsInFirstColumn(), 1);
|
2012-08-26 02:08:18 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2012-08-27 05:13:14 +00:00
|
|
|
|
|
2015-07-13 21:27:08 +00:00
|
|
|
|
class FunctionIdentity : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "identity";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionIdentity>(); }
|
2015-07-13 21:27:08 +00:00
|
|
|
|
|
|
|
|
|
/// Получить имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override
|
2015-07-13 21:27:08 +00:00
|
|
|
|
{
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
2015-07-13 21:27:08 +00:00
|
|
|
|
{
|
|
|
|
|
if (arguments.size() != 1)
|
|
|
|
|
throw Exception("Function " + getName() + " requires exactly one argument.",
|
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
return arguments.front()->clone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2015-07-13 21:27:08 +00:00
|
|
|
|
{
|
|
|
|
|
block.getByPosition(result).column = block.getByPosition(arguments.front()).column;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2012-08-27 05:13:14 +00:00
|
|
|
|
class FunctionArrayJoin : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = "arrayJoin";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionArrayJoin>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
|
|
|
|
|
|
2012-08-27 05:13:14 +00:00
|
|
|
|
/// Получить имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override
|
2012-08-27 05:13:14 +00:00
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2012-08-27 05:13:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
2012-08-27 05:13:14 +00:00
|
|
|
|
{
|
|
|
|
|
if (arguments.size() != 1)
|
2014-08-09 01:51:03 +00:00
|
|
|
|
throw Exception("Function " + getName() + " requires exactly one argument.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2012-08-27 05:13:14 +00:00
|
|
|
|
|
2014-06-26 00:58:14 +00:00
|
|
|
|
const DataTypeArray * arr = typeid_cast<const DataTypeArray *>(&*arguments[0]);
|
2012-08-27 05:13:14 +00:00
|
|
|
|
if (!arr)
|
2014-08-09 01:51:03 +00:00
|
|
|
|
throw Exception("Argument for function " + getName() + " must be Array.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
|
2012-08-27 05:13:14 +00:00
|
|
|
|
return arr->getNestedType()->clone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2012-08-27 05:13:14 +00:00
|
|
|
|
{
|
2014-08-09 01:51:03 +00:00
|
|
|
|
throw Exception("Function " + getName() + " must not be executed directly.", ErrorCodes::FUNCTION_IS_SPECIAL);
|
2012-08-27 05:13:14 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2013-09-18 20:06:49 +00:00
|
|
|
|
|
2015-06-19 02:34:47 +00:00
|
|
|
|
/** Создаёт массив, размножая столбец (первый аргумент) по количеству элементов в массиве (втором аргументе).
|
2013-09-18 20:06:49 +00:00
|
|
|
|
* Используется только в качестве prerequisites для функций высшего порядка.
|
|
|
|
|
*/
|
2013-06-04 14:59:05 +00:00
|
|
|
|
class FunctionReplicate : public IFunction
|
|
|
|
|
{
|
2015-06-19 02:34:47 +00:00
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = "replicate";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionReplicate>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
|
2013-06-04 14:59:05 +00:00
|
|
|
|
/// Получить имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override
|
2013-06-04 14:59:05 +00:00
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2013-06-04 14:59:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить типы результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
2013-06-04 14:59:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (arguments.size() != 2)
|
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
2013-06-21 20:34:19 +00:00
|
|
|
|
+ toString(arguments.size()) + ", should be 2.",
|
2013-06-04 14:59:05 +00:00
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
2014-06-26 00:58:14 +00:00
|
|
|
|
const DataTypeArray * array_type = typeid_cast<const DataTypeArray *>(&*arguments[1]);
|
2013-06-04 14:59:05 +00:00
|
|
|
|
if (!array_type)
|
|
|
|
|
throw Exception("Second argument for function " + getName() + " must be array.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeArray>(arguments[0]->clone());
|
2013-06-04 14:59:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2013-06-04 14:59:05 +00:00
|
|
|
|
{
|
|
|
|
|
ColumnPtr first_column = block.getByPosition(arguments[0]).column;
|
2013-09-18 20:06:49 +00:00
|
|
|
|
|
2014-06-26 00:58:14 +00:00
|
|
|
|
ColumnArray * array_column = typeid_cast<ColumnArray *>(&*block.getByPosition(arguments[1]).column);
|
2013-06-04 14:59:05 +00:00
|
|
|
|
ColumnPtr temp_column;
|
2013-09-18 20:06:49 +00:00
|
|
|
|
|
2013-06-04 14:59:05 +00:00
|
|
|
|
if (!array_column)
|
|
|
|
|
{
|
2014-06-26 00:58:14 +00:00
|
|
|
|
ColumnConstArray * const_array_column = typeid_cast<ColumnConstArray *>(&*block.getByPosition(arguments[1]).column);
|
2013-06-04 14:59:05 +00:00
|
|
|
|
if (!const_array_column)
|
|
|
|
|
throw Exception("Unexpected column for replicate", ErrorCodes::ILLEGAL_COLUMN);
|
2013-06-05 09:56:47 +00:00
|
|
|
|
temp_column = const_array_column->convertToFullColumn();
|
2014-06-26 00:58:14 +00:00
|
|
|
|
array_column = typeid_cast<ColumnArray *>(&*temp_column);
|
2013-06-04 14:59:05 +00:00
|
|
|
|
}
|
2013-09-18 22:40:09 +00:00
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
block.getByPosition(result).column = std::make_shared<ColumnArray>(
|
2015-06-19 02:34:47 +00:00
|
|
|
|
first_column->replicate(array_column->getOffsets()),
|
|
|
|
|
array_column->getOffsetsColumn());
|
2013-06-04 14:59:05 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-09 01:51:03 +00:00
|
|
|
|
|
|
|
|
|
class FunctionBar : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-11-12 17:23:26 +00:00
|
|
|
|
static constexpr auto name = "bar";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionBar>(); }
|
2014-11-12 17:23:26 +00:00
|
|
|
|
|
2014-08-09 01:51:03 +00:00
|
|
|
|
/// Получить имя функции.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
String getName() const override
|
2014-08-09 01:51:03 +00:00
|
|
|
|
{
|
2014-11-12 17:23:26 +00:00
|
|
|
|
return name;
|
2014-08-09 01:51:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
2014-08-09 01:51:03 +00:00
|
|
|
|
{
|
|
|
|
|
if (arguments.size() != 3 && arguments.size() != 4)
|
|
|
|
|
throw Exception("Function " + getName() + " requires from 3 or 4 parameters: value, min_value, max_value, [max_width_of_bar = 80]. Passed "
|
|
|
|
|
+ toString(arguments.size()) + ".",
|
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
if (!arguments[0]->isNumeric() || !arguments[1]->isNumeric() || !arguments[2]->isNumeric()
|
|
|
|
|
|| (arguments.size() == 4 && !arguments[3]->isNumeric()))
|
|
|
|
|
throw Exception("All arguments for function " + getName() + " must be numeric.", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeString>();
|
2014-08-09 01:51:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Выполнить функцию над блоком.
|
2015-10-11 23:36:45 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2014-08-09 01:51:03 +00:00
|
|
|
|
{
|
|
|
|
|
Int64 min = extractConstant<Int64>(block, arguments, 1, "Second"); /// Уровень значения, при котором полоска имеет нулевую длину.
|
|
|
|
|
Int64 max = extractConstant<Int64>(block, arguments, 2, "Third"); /// Уровень значения, при котором полоска имеет максимальную длину.
|
|
|
|
|
|
|
|
|
|
/// Максимальная ширина полоски в символах, по-умолчанию.
|
|
|
|
|
Float64 max_width = arguments.size() == 4
|
|
|
|
|
? extractConstant<Float64>(block, arguments, 3, "Fourth")
|
|
|
|
|
: 80;
|
|
|
|
|
|
|
|
|
|
if (max_width < 1)
|
|
|
|
|
throw Exception("Max_width argument must be >= 1.", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
|
|
|
|
|
|
if (max_width > 1000)
|
|
|
|
|
throw Exception("Too large max_width.", ErrorCodes::ARGUMENT_OUT_OF_BOUND);
|
|
|
|
|
|
|
|
|
|
const auto & src = *block.getByPosition(arguments[0]).column;
|
|
|
|
|
|
|
|
|
|
if (src.isConst())
|
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
auto res_column = std::make_shared<ColumnConstString>(block.rowsInFirstColumn(), "");
|
2014-08-09 01:51:03 +00:00
|
|
|
|
block.getByPosition(result).column = res_column;
|
|
|
|
|
|
|
|
|
|
if ( executeConstNumber<UInt8> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeConstNumber<UInt16> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeConstNumber<UInt32> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeConstNumber<UInt64> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeConstNumber<Int8> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeConstNumber<Int16> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeConstNumber<Int32> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeConstNumber<Int64> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeConstNumber<Float32> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeConstNumber<Float64> (src, *res_column, min, max, max_width))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
|
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
auto res_column = std::make_shared<ColumnString>();
|
2014-08-09 01:51:03 +00:00
|
|
|
|
block.getByPosition(result).column = res_column;
|
|
|
|
|
|
|
|
|
|
if ( executeNumber<UInt8> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeNumber<UInt16> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeNumber<UInt32> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeNumber<UInt64> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeNumber<Int8> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeNumber<Int16> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeNumber<Int32> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeNumber<Int64> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeNumber<Float32> (src, *res_column, min, max, max_width)
|
|
|
|
|
|| executeNumber<Float64> (src, *res_column, min, max, max_width))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
throw Exception("Illegal column " + block.getByPosition(arguments[0]).column->getName()
|
|
|
|
|
+ " of argument of function " + getName(),
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
template <typename T>
|
|
|
|
|
T extractConstant(Block & block, const ColumnNumbers & arguments, size_t argument_pos, const char * which_argument) const
|
|
|
|
|
{
|
|
|
|
|
const auto & column = *block.getByPosition(arguments[argument_pos]).column;
|
|
|
|
|
|
|
|
|
|
if (!column.isConst())
|
|
|
|
|
throw Exception(which_argument + String(" argument for function ") + getName() + " must be constant.", ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
|
|
return apply_visitor(FieldVisitorConvertToNumber<T>(), column[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2016-04-15 00:33:21 +00:00
|
|
|
|
static void fill(const PaddedPODArray<T> & src, ColumnString::Chars_t & dst_chars, ColumnString::Offsets_t & dst_offsets,
|
2014-08-09 01:51:03 +00:00
|
|
|
|
Int64 min, Int64 max, Float64 max_width)
|
|
|
|
|
{
|
|
|
|
|
size_t size = src.size();
|
|
|
|
|
size_t current_offset = 0;
|
|
|
|
|
|
|
|
|
|
dst_offsets.resize(size);
|
2014-10-25 20:27:37 +00:00
|
|
|
|
dst_chars.reserve(size * (UnicodeBar::getWidthInBytes(max_width) + 1)); /// строки 0-terminated.
|
2014-08-09 01:51:03 +00:00
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
|
{
|
2014-10-25 20:27:37 +00:00
|
|
|
|
Float64 width = UnicodeBar::getWidth(src[i], min, max, max_width);
|
|
|
|
|
size_t next_size = current_offset + UnicodeBar::getWidthInBytes(width) + 1;
|
2014-08-09 01:51:03 +00:00
|
|
|
|
dst_chars.resize(next_size);
|
2014-10-25 20:27:37 +00:00
|
|
|
|
UnicodeBar::render(width, reinterpret_cast<char *>(&dst_chars[current_offset]));
|
2014-08-09 01:51:03 +00:00
|
|
|
|
current_offset = next_size;
|
|
|
|
|
dst_offsets[i] = current_offset;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
static void fill(T src, String & dst_chars,
|
|
|
|
|
Int64 min, Int64 max, Float64 max_width)
|
|
|
|
|
{
|
2014-10-25 20:27:37 +00:00
|
|
|
|
Float64 width = UnicodeBar::getWidth(src, min, max, max_width);
|
|
|
|
|
dst_chars.resize(UnicodeBar::getWidthInBytes(width));
|
|
|
|
|
UnicodeBar::render(width, &dst_chars[0]);
|
2014-08-09 01:51:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
static bool executeNumber(const IColumn & src, ColumnString & dst, Int64 min, Int64 max, Float64 max_width)
|
|
|
|
|
{
|
|
|
|
|
if (const ColumnVector<T> * col = typeid_cast<const ColumnVector<T> *>(&src))
|
|
|
|
|
{
|
|
|
|
|
fill(col->getData(), dst.getChars(), dst.getOffsets(), min, max, max_width);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
static bool executeConstNumber(const IColumn & src, ColumnConstString & dst, Int64 min, Int64 max, Float64 max_width)
|
|
|
|
|
{
|
|
|
|
|
if (const ColumnConst<T> * col = typeid_cast<const ColumnConst<T> *>(&src))
|
|
|
|
|
{
|
|
|
|
|
fill(col->getData(), dst.getData(), min, max, max_width);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-04-03 13:30:00 +00:00
|
|
|
|
|
|
|
|
|
template <typename Impl>
|
|
|
|
|
class FunctionNumericPredicate : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = Impl::name;
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionNumericPredicate>(); }
|
2015-04-03 13:30:00 +00:00
|
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
|
|
|
|
{
|
|
|
|
|
const auto args_size = arguments.size();
|
|
|
|
|
if (args_size != 1)
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Number of arguments for function " + getName() + " doesn't match: passed " +
|
|
|
|
|
toString(args_size) + ", should be 1",
|
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const auto arg = arguments.front().get();
|
|
|
|
|
if (!typeid_cast<const DataTypeUInt8 *>(arg) &&
|
|
|
|
|
!typeid_cast<const DataTypeUInt16 *>(arg) &&
|
|
|
|
|
!typeid_cast<const DataTypeUInt32 *>(arg) &&
|
|
|
|
|
!typeid_cast<const DataTypeUInt64 *>(arg) &&
|
|
|
|
|
!typeid_cast<const DataTypeInt8 *>(arg) &&
|
|
|
|
|
!typeid_cast<const DataTypeInt16 *>(arg) &&
|
|
|
|
|
!typeid_cast<const DataTypeInt32 *>(arg) &&
|
|
|
|
|
!typeid_cast<const DataTypeInt64 *>(arg) &&
|
|
|
|
|
!typeid_cast<const DataTypeFloat32 *>(arg) &&
|
|
|
|
|
!typeid_cast<const DataTypeFloat64 *>(arg))
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Argument for function " + getName() + " must be numeric",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT
|
|
|
|
|
};
|
|
|
|
|
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
2015-04-03 13:30:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, const size_t result) override
|
|
|
|
|
{
|
|
|
|
|
const auto in = block.getByPosition(arguments.front()).column.get();
|
|
|
|
|
|
|
|
|
|
if (!execute<UInt8>(block, in, result) &&
|
|
|
|
|
!execute<UInt16>(block, in, result) &&
|
|
|
|
|
!execute<UInt32>(block, in, result) &&
|
|
|
|
|
!execute<UInt64>(block, in, result) &&
|
|
|
|
|
!execute<Int8>(block, in, result) &&
|
|
|
|
|
!execute<Int16>(block, in, result) &&
|
|
|
|
|
!execute<Int32>(block, in, result) &&
|
|
|
|
|
!execute<Int64>(block, in, result) &&
|
|
|
|
|
!execute<Float32>(block, in, result) &&
|
|
|
|
|
!execute<Float64>(block, in, result))
|
|
|
|
|
throw Exception{
|
|
|
|
|
"Illegal column " + in->getName() + " of first argument of function " + getName(),
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2015-11-16 03:28:10 +00:00
|
|
|
|
bool execute(Block & block, const IColumn * in_untyped, const size_t result)
|
2015-04-03 13:30:00 +00:00
|
|
|
|
{
|
|
|
|
|
if (const auto in = typeid_cast<const ColumnVector<T> *>(in_untyped))
|
|
|
|
|
{
|
|
|
|
|
const auto size = in->size();
|
|
|
|
|
|
2016-05-28 05:31:36 +00:00
|
|
|
|
const auto out = std::make_shared<ColumnUInt8>(size);
|
2015-04-03 13:30:00 +00:00
|
|
|
|
block.getByPosition(result).column = out;
|
|
|
|
|
|
|
|
|
|
const auto & in_data = in->getData();
|
|
|
|
|
auto & out_data = out->getData();
|
|
|
|
|
|
|
|
|
|
for (const auto i : ext::range(0, size))
|
|
|
|
|
out_data[i] = Impl::execute(in_data[i]);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (const auto in = typeid_cast<const ColumnConst<T> *>(in_untyped))
|
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
block.getByPosition(result).column = std::make_shared<ColumnConstUInt8>(
|
2015-04-03 13:30:00 +00:00
|
|
|
|
in->size(),
|
2016-05-28 05:31:36 +00:00
|
|
|
|
Impl::execute(in->getData()));
|
2015-04-03 13:30:00 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct IsFiniteImpl
|
|
|
|
|
{
|
|
|
|
|
static constexpr auto name = "isFinite";
|
|
|
|
|
template <typename T> static bool execute(const T t) { return std::isfinite(t); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct IsInfiniteImpl
|
|
|
|
|
{
|
|
|
|
|
static constexpr auto name = "isInfinite";
|
|
|
|
|
template <typename T> static bool execute(const T t) { return std::isinf(t); }
|
|
|
|
|
};
|
|
|
|
|
|
2015-04-03 14:52:33 +00:00
|
|
|
|
struct IsNaNImpl
|
2015-04-03 13:30:00 +00:00
|
|
|
|
{
|
2015-04-03 14:52:33 +00:00
|
|
|
|
static constexpr auto name = "isNaN";
|
2015-04-03 13:30:00 +00:00
|
|
|
|
template <typename T> static bool execute(const T t) { return std::isnan(t); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using FunctionIsFinite = FunctionNumericPredicate<IsFiniteImpl>;
|
|
|
|
|
using FunctionIsInfinite = FunctionNumericPredicate<IsInfiniteImpl>;
|
2015-04-03 14:52:33 +00:00
|
|
|
|
using FunctionIsNaN = FunctionNumericPredicate<IsNaNImpl>;
|
2015-04-03 13:30:00 +00:00
|
|
|
|
|
2015-06-06 00:28:37 +00:00
|
|
|
|
|
2015-06-03 13:11:59 +00:00
|
|
|
|
class FunctionVersion : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "version";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionVersion>(); }
|
2015-06-03 13:11:59 +00:00
|
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
|
|
|
|
{
|
|
|
|
|
if (!arguments.empty())
|
|
|
|
|
throw Exception("Function " + getName() + " must be called without arguments", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeString>();
|
2015-06-03 13:11:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
|
|
|
|
{
|
|
|
|
|
static const std::string version = getVersion();
|
2016-05-28 05:31:36 +00:00
|
|
|
|
block.getByPosition(result).column = std::make_shared<ColumnConstString>(block.rowsInFirstColumn(), version);
|
2015-06-03 13:11:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::string getVersion() const
|
|
|
|
|
{
|
|
|
|
|
std::ostringstream os;
|
2016-02-09 17:06:50 +00:00
|
|
|
|
os << DBMS_VERSION_MAJOR << "." << DBMS_VERSION_MINOR << "." << ClickHouseRevision::get();
|
2015-06-03 13:11:59 +00:00
|
|
|
|
return os.str();
|
|
|
|
|
}
|
|
|
|
|
};
|
2015-04-28 03:38:41 +00:00
|
|
|
|
|
2015-06-06 00:28:37 +00:00
|
|
|
|
|
2016-04-09 07:47:08 +00:00
|
|
|
|
class FunctionUptime : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "uptime";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionUptime>(context.getUptimeSeconds()); }
|
2016-04-09 07:47:08 +00:00
|
|
|
|
|
|
|
|
|
FunctionUptime(time_t uptime_) : uptime(uptime_) {}
|
|
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
|
|
|
|
{
|
|
|
|
|
if (!arguments.empty())
|
|
|
|
|
throw Exception("Function " + getName() + " must be called without arguments", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2016-05-28 07:48:40 +00:00
|
|
|
|
return std::make_shared<DataTypeUInt32>();
|
2016-04-09 07:47:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
|
|
|
|
{
|
2016-05-28 05:31:36 +00:00
|
|
|
|
block.getByPosition(result).column = std::make_shared<ColumnConstUInt32>(block.rowsInFirstColumn(), uptime);
|
2016-04-09 07:47:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
time_t uptime;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2015-06-06 00:28:37 +00:00
|
|
|
|
/** Весьма необычная функция.
|
|
|
|
|
* Принимает состояние агрегатной функции (например runningAccumulate(uniqState(UserID))),
|
|
|
|
|
* и для каждой строки блока, возвращает результат агрегатной функции по объединению состояний от всех предыдущих строк блока и текущей строки.
|
|
|
|
|
*
|
|
|
|
|
* То есть, функция зависит от разбиения данных на блоки и от порядка строк в блоке.
|
|
|
|
|
*/
|
|
|
|
|
class FunctionRunningAccumulate : public IFunction
|
|
|
|
|
{
|
2015-08-26 11:27:45 +00:00
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "runningAccumulate";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionRunningAccumulate>(); }
|
2015-08-26 11:27:45 +00:00
|
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
2015-06-06 00:28:37 +00:00
|
|
|
|
{
|
2015-08-26 11:27:45 +00:00
|
|
|
|
if (arguments.size() != 1)
|
|
|
|
|
throw Exception("Function " + getName() + " requires exactly one argument.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
const DataTypeAggregateFunction * type = typeid_cast<const DataTypeAggregateFunction *>(&*arguments[0]);
|
|
|
|
|
if (!type)
|
|
|
|
|
throw Exception("Argument for function " + getName() + " must have type AggregateFunction - state of aggregate function.",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
|
|
return type->getReturnType()->clone();
|
2015-06-06 00:28:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-26 11:27:45 +00:00
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
2015-06-06 00:28:37 +00:00
|
|
|
|
{
|
|
|
|
|
const ColumnAggregateFunction * column_with_states = typeid_cast<const ColumnAggregateFunction *>(&*block.getByPosition(arguments.at(0)).column);
|
|
|
|
|
if (!column_with_states)
|
|
|
|
|
throw Exception(
|
|
|
|
|
"Illegal column " + block.getByPosition(arguments.at(0)).column->getName() + " of first argument of function " + getName(),
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
|
|
AggregateFunctionPtr aggregate_function_ptr = column_with_states->getAggregateFunction();
|
|
|
|
|
const IAggregateFunction & agg_func = *aggregate_function_ptr;
|
|
|
|
|
|
|
|
|
|
auto deleter = [&agg_func] (char * ptr) { agg_func.destroy(ptr); free(ptr); };
|
|
|
|
|
std::unique_ptr<char, decltype(deleter)> place { reinterpret_cast<char *>(malloc(agg_func.sizeOfData())), deleter };
|
|
|
|
|
|
|
|
|
|
agg_func.create(place.get()); /// Немного не exception-safe. Если здесь выкинется исключение, то зря вызовется destroy.
|
|
|
|
|
|
|
|
|
|
ColumnPtr result_column_ptr = agg_func.getReturnType()->createColumn();
|
|
|
|
|
block.getByPosition(result).column = result_column_ptr;
|
|
|
|
|
IColumn & result_column = *result_column_ptr;
|
|
|
|
|
result_column.reserve(column_with_states->size());
|
|
|
|
|
|
|
|
|
|
const auto & states = column_with_states->getData();
|
|
|
|
|
for (const auto & state_to_add : states)
|
|
|
|
|
{
|
|
|
|
|
agg_func.merge(place.get(), state_to_add);
|
|
|
|
|
agg_func.insertResultInto(place.get(), result_column);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-25 16:34:28 +00:00
|
|
|
|
};
|
2015-06-06 00:28:37 +00:00
|
|
|
|
|
2015-08-26 11:27:45 +00:00
|
|
|
|
|
2015-06-06 00:28:37 +00:00
|
|
|
|
/** Принимает состояние агрегатной функции. Возвращает результат агрегации.
|
|
|
|
|
*/
|
|
|
|
|
class FunctionFinalizeAggregation : public IFunction
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static constexpr auto name = "finalizeAggregation";
|
2016-05-28 15:42:22 +00:00
|
|
|
|
static FunctionPtr create(const Context & context) { return std::make_shared<FunctionFinalizeAggregation>(); }
|
2015-06-06 00:28:37 +00:00
|
|
|
|
|
|
|
|
|
String getName() const override { return name; }
|
|
|
|
|
|
|
|
|
|
DataTypePtr getReturnType(const DataTypes & arguments) const override
|
|
|
|
|
{
|
|
|
|
|
if (arguments.size() != 1)
|
|
|
|
|
throw Exception("Function " + getName() + " requires exactly one argument.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
|
|
const DataTypeAggregateFunction * type = typeid_cast<const DataTypeAggregateFunction *>(&*arguments[0]);
|
|
|
|
|
if (!type)
|
|
|
|
|
throw Exception("Argument for function " + getName() + " must have type AggregateFunction - state of aggregate function.",
|
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
|
|
|
|
|
|
return type->getReturnType()->clone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void execute(Block & block, const ColumnNumbers & arguments, size_t result) override
|
|
|
|
|
{
|
|
|
|
|
ColumnAggregateFunction * column_with_states = typeid_cast<ColumnAggregateFunction *>(&*block.getByPosition(arguments.at(0)).column);
|
|
|
|
|
if (!column_with_states)
|
|
|
|
|
throw Exception(
|
|
|
|
|
"Illegal column " + block.getByPosition(arguments.at(0)).column->getName() + " of first argument of function " + getName(),
|
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
|
|
block.getByPosition(result).column = column_with_states->convertToValues();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2011-11-07 01:15:37 +00:00
|
|
|
|
}
|