diff --git a/dbms/src/Analyzers/TypeAndConstantInference.cpp b/dbms/src/Analyzers/TypeAndConstantInference.cpp index f800d4b3b18..3516b9fb316 100644 --- a/dbms/src/Analyzers/TypeAndConstantInference.cpp +++ b/dbms/src/Analyzers/TypeAndConstantInference.cpp @@ -128,15 +128,15 @@ void processFunction(const String & column_name, ASTPtr & ast, TypeAndConstantIn { ASTFunction * function = static_cast(ast.get()); - /// Special case for lambda functions. Lambda function has special return type "Expression". - /// We first create info with Expression of unspecified arguments, and will specify them later. + /// Special case for lambda functions. Lambda function has special return type "Function". + /// We first create info with Function of unspecified arguments, and will specify them later. if (function->name == "lambda") { size_t number_of_lambda_parameters = AnalyzeLambdas::extractLambdaParameters(function->arguments->children.at(0)).size(); TypeAndConstantInference::ExpressionInfo expression_info; expression_info.node = ast; - expression_info.data_type = std::make_unique(DataTypes(number_of_lambda_parameters)); + expression_info.data_type = std::make_unique(DataTypes(number_of_lambda_parameters)); info.emplace(column_name, std::move(expression_info)); return; } diff --git a/dbms/src/Columns/ColumnExpression.cpp b/dbms/src/Columns/ColumnExpression.cpp deleted file mode 100644 index 526d7a5734a..00000000000 --- a/dbms/src/Columns/ColumnExpression.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include - - -namespace DB -{ - -ColumnExpression::ColumnExpression( - size_t s_, const ExpressionActionsPtr & expression_, const NamesAndTypesList & arguments_, const DataTypePtr & return_type_, const String & return_name_) - : expression(expression_), arguments(arguments_), return_type(return_type_), return_name(return_name_) -{ - s = s_; -} - -MutableColumnPtr ColumnExpression::cloneDummy(size_t s_) const -{ - return ColumnExpression::create(s_, expression, arguments, return_type, return_name); -} - -const ExpressionActionsPtr & ColumnExpression::getExpression() const { return expression; } -const DataTypePtr & ColumnExpression::getReturnType() const { return return_type; } -const std::string & ColumnExpression::getReturnName() const { return return_name; } -const NamesAndTypesList & ColumnExpression::getArguments() const { return arguments; } - -} diff --git a/dbms/src/Columns/ColumnExpression.h b/dbms/src/Columns/ColumnExpression.h deleted file mode 100644 index 9810060d197..00000000000 --- a/dbms/src/Columns/ColumnExpression.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include -#include - - -namespace DB -{ - -class ExpressionActions; - -/** A column containing a lambda expression. - * Behaves like a constant-column. Contains an expression, but not input or output data. - */ -class ColumnExpression final : public COWPtrHelper -{ -private: - friend class COWPtrHelper; - - using ExpressionActionsPtr = std::shared_ptr; - - ColumnExpression(size_t s_, const ExpressionActionsPtr & expression_, const NamesAndTypesList & arguments_, const DataTypePtr & return_type_, const String & return_name_); - ColumnExpression(const ColumnExpression &) = default; - -public: - const char * getFamilyName() const override { return "Expression"; } - MutableColumnPtr cloneDummy(size_t s_) const override; - - const ExpressionActionsPtr & getExpression() const; - const DataTypePtr & getReturnType() const; - const std::string & getReturnName() const; - const NamesAndTypesList & getArguments() const; - -private: - ExpressionActionsPtr expression; - NamesAndTypesList arguments; - DataTypePtr return_type; - std::string return_name; -}; - -} diff --git a/dbms/src/DataTypes/DataTypeExpression.cpp b/dbms/src/DataTypes/DataTypeExpression.cpp deleted file mode 100644 index 924bfdef62d..00000000000 --- a/dbms/src/DataTypes/DataTypeExpression.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include - - -namespace DB -{ - -std::string DataTypeExpression::getName() const -{ - std::string res = "Expression("; - if (argument_types.size() > 1) - res += "("; - for (size_t i = 0; i < argument_types.size(); ++i) - { - if (i > 0) - res += ", "; - const DataTypePtr & type = argument_types[i]; - res += type ? type->getName() : "?"; - } - if (argument_types.size() > 1) - res += ")"; - res += " -> "; - res += return_type ? return_type->getName() : "?"; - res += ")"; - return res; -} - -bool DataTypeExpression::equals(const IDataType & rhs) const -{ - return typeid(rhs) == typeid(*this) && getName() == rhs.getName(); -} - -} diff --git a/dbms/src/DataTypes/DataTypeExpression.h b/dbms/src/DataTypes/DataTypeExpression.h deleted file mode 100644 index 605148b855d..00000000000 --- a/dbms/src/DataTypes/DataTypeExpression.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include - - -namespace DB -{ - -/** Special data type, representing lambda expression. - */ -class DataTypeExpression final : public IDataTypeDummy -{ -private: - DataTypes argument_types; - DataTypePtr return_type; - -public: - static constexpr bool is_parametric = true; - bool isParametric() const override { return true; } - - /// Some types could be still unknown. - DataTypeExpression(const DataTypes & argument_types_ = DataTypes(), const DataTypePtr & return_type_ = nullptr) - : argument_types(argument_types_), return_type(return_type_) {} - - std::string getName() const override; - const char * getFamilyName() const override { return "Expression"; } - - const DataTypes & getArgumentTypes() const - { - return argument_types; - } - - const DataTypePtr & getReturnType() const - { - return return_type; - } - - bool equals(const IDataType & rhs) const override; -}; - -} diff --git a/dbms/src/Functions/FunctionsHigherOrder.h b/dbms/src/Functions/FunctionsHigherOrder.h index 6a8992acb44..5f9e570a47a 100644 --- a/dbms/src/Functions/FunctionsHigherOrder.h +++ b/dbms/src/Functions/FunctionsHigherOrder.h @@ -1,13 +1,11 @@ #pragma once #include -#include #include #include #include #include #include -#include #include #include #include diff --git a/dbms/src/Functions/FunctionsMiscellaneous.h b/dbms/src/Functions/FunctionsMiscellaneous.h index 4ce49ffb515..aecf9210906 100644 --- a/dbms/src/Functions/FunctionsMiscellaneous.h +++ b/dbms/src/Functions/FunctionsMiscellaneous.h @@ -10,32 +10,7 @@ namespace DB { -/** Creates an array, multiplying the column (the first argument) by the number of elements in the array (the second argument). - * Used only as prerequisites for higher-order functions. - */ -class FunctionReplicate : public IFunction -{ -public: - static constexpr auto name = "replicate"; - static FunctionPtr create(const Context & context); - - String getName() const override - { - return name; - } - - size_t getNumberOfArguments() const override - { - return 2; - } - - bool useDefaultImplementationForNulls() const override { return false; } - - DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override; - - void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result) override; -}; - +/// Executes expression. Uses for lambda functions implementation. Isn't can't be created from factory. class FunctionExpression : public IFunctionBase, public IPreparedFunction, public std::enable_shared_from_this { @@ -64,6 +39,7 @@ public: for (size_t i = 0; i < arguments.size(); ++i) { const auto & argument = block.getByPosition(arguments[i]); + /// Replace column name with value from argument_names. expr_block.insert({argument.column, argument.type, argument_names[i]}); } @@ -80,6 +56,10 @@ private: std::string return_name; }; +/// Captures columns which are used bu lambda function but not in argument list. +/// Returns ColumnFunction with captured columns. +/// For lambda(x, x + y) x is in lambda_arguments, y is in captured arguments, expression_actions is 'x + y'. +/// execute(y) returns ColumnFunction(FunctionExpression(x + y), y) with type Function(x) -> function_return_type. class FunctionCapture : public IFunctionBase, public IPreparedFunction, public FunctionBuilderImpl, public std::enable_shared_from_this { @@ -91,6 +71,7 @@ public: , function_return_type(function_return_type), expression_return_name(expression_return_name) { const auto & all_arguments = expression_actions->getRequiredColumnsWithTypes(); + std::unordered_map arguments_map; for (const auto & arg : all_arguments) arguments_map[arg.name] = arg.type; diff --git a/dbms/src/Interpreters/ExpressionAnalyzer.cpp b/dbms/src/Interpreters/ExpressionAnalyzer.cpp index 5be1c9d771a..1223bfaa47e 100644 --- a/dbms/src/Interpreters/ExpressionAnalyzer.cpp +++ b/dbms/src/Interpreters/ExpressionAnalyzer.cpp @@ -17,12 +17,10 @@ #include #include #include -#include #include #include #include -#include #include #include