From 234367b0382a46e5a023594df7a779983770cfde Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Tue, 14 Jul 2015 00:27:08 +0300 Subject: [PATCH] dbms: added function identity [#METR-17251]. --- .../DB/Functions/FunctionsMiscellaneous.h | 30 +++++++++++++++++++ dbms/src/Functions/FunctionsMiscellaneous.cpp | 1 + .../0_stateless/00194_identity.reference | 1 + .../queries/0_stateless/00194_identity.sql | 1 + 4 files changed, 33 insertions(+) create mode 100644 dbms/tests/queries/0_stateless/00194_identity.reference create mode 100644 dbms/tests/queries/0_stateless/00194_identity.sql diff --git a/dbms/include/DB/Functions/FunctionsMiscellaneous.h b/dbms/include/DB/Functions/FunctionsMiscellaneous.h index 06320362501..ae606f53182 100644 --- a/dbms/include/DB/Functions/FunctionsMiscellaneous.h +++ b/dbms/include/DB/Functions/FunctionsMiscellaneous.h @@ -534,6 +534,36 @@ public: }; +class FunctionIdentity : public IFunction +{ +public: + static constexpr auto name = "identity"; + static IFunction * create(const Context & context) { return new FunctionIdentity; } + + /// Получить имя функции. + String getName() const + { + return name; + } + + /// Получить тип результата по типам аргументов. Если функция неприменима для данных аргументов - кинуть исключение. + DataTypePtr getReturnType(const DataTypes & arguments) const + { + if (arguments.size() != 1) + throw Exception("Function " + getName() + " requires exactly one argument.", + ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); + + return arguments.front()->clone(); + } + + /// Выполнить функцию над блоком. + void execute(Block & block, const ColumnNumbers & arguments, size_t result) + { + block.getByPosition(result).column = block.getByPosition(arguments.front()).column; + } +}; + + class FunctionArrayJoin : public IFunction { public: diff --git a/dbms/src/Functions/FunctionsMiscellaneous.cpp b/dbms/src/Functions/FunctionsMiscellaneous.cpp index e2a478933c1..08b51f8ba5f 100644 --- a/dbms/src/Functions/FunctionsMiscellaneous.cpp +++ b/dbms/src/Functions/FunctionsMiscellaneous.cpp @@ -325,6 +325,7 @@ void registerFunctionsMiscellaneous(FunctionFactory & factory) factory.registerFunction(); factory.registerFunction(); factory.registerFunction(); + factory.registerFunction(); factory.registerFunction(); factory.registerFunction(); factory.registerFunction(); diff --git a/dbms/tests/queries/0_stateless/00194_identity.reference b/dbms/tests/queries/0_stateless/00194_identity.reference new file mode 100644 index 00000000000..72749c905a3 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00194_identity.reference @@ -0,0 +1 @@ +1 1 1 diff --git a/dbms/tests/queries/0_stateless/00194_identity.sql b/dbms/tests/queries/0_stateless/00194_identity.sql new file mode 100644 index 00000000000..cb2e7264b16 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00194_identity.sql @@ -0,0 +1 @@ +SELECT identity(1 AS a) AS b, a, b;