diff --git a/src/Storages/MergeTree/KeyCondition.cpp b/src/Storages/MergeTree/KeyCondition.cpp index afbaab25687..04eae5c04f5 100644 --- a/src/Storages/MergeTree/KeyCondition.cpp +++ b/src/Storages/MergeTree/KeyCondition.cpp @@ -500,17 +500,10 @@ static std::pair applyFunctionForFieldOfUnknownType( const Field & arg_value) { ColumnsWithTypeAndName arguments{{ arg_type->createColumnConst(1, arg_value), arg_type, "x" }}; - - // std::cerr << ">>>>> applaying func " << func->getName() << " to column " << arguments[0].dumpStructure() << std::endl; - - //FunctionBasePtr func_base = func->build(arguments); - DataTypePtr return_type = func->getResultType(); auto col = func->execute(arguments, return_type, 1); - // std::cerr << ">>>>> got " << ColumnWithTypeAndName(col, return_type, "").dumpStructure() << std::endl; - Field result = (*col)[0]; return {std::move(result), std::move(return_type)}; @@ -602,12 +595,7 @@ bool KeyCondition::canConstantBeWrapped(const ASTPtr & node, const String & expr { NameSet names; for (const auto & action : key_expr->getActions()) - { names.insert(action.node->result_name); - // std::cerr << "-- added " << action.node->result_name << std::endl; - } - - // std::cerr << key_expr->getSampleBlock().dumpStructure() << std::endl; /// sample_block from key_expr cannot contain modulo and moduloLegacy at the same time. /// For partition key it is always moduloLegacy. @@ -637,8 +625,6 @@ bool KeyCondition::canConstantBeWrappedByMonotonicFunctions( Field & out_value [[maybe_unused]], DataTypePtr & out_type [[maybe_unused]]) { - // std::cerr << "=========== canConstantBeWrappedByMonotonicFunctions for " << node->getColumnName() << std::endl; - // std::cerr << key_expr->dumpActions() << std::endl; // Constant expr should use alias names if any String passed_expr_name = node->getColumnNameWithoutAlias(); @@ -714,7 +700,6 @@ bool KeyCondition::canConstantBeWrappedByMonotonicFunctions( /// However, looks like this case newer happenes (I could not find such). /// Let's assume that any two comparable types are castable to each other. auto const_type = cur_node->result_type; - // std::cerr << "==== Using type (mon) for expr " << expr_name << " " << const_type->getName() << std::endl; auto const_column = out_type->createColumnConst(1, out_value); auto const_value = (*castColumn({const_column, out_type, ""}, const_type))[0]; @@ -746,10 +731,8 @@ bool KeyCondition::canConstantBeWrappedByMonotonicFunctions( bool KeyCondition::canConstantBeWrappedByFunctions( const ASTPtr & ast, size_t & out_key_column_num, DataTypePtr & out_key_column_type, Field & out_value, DataTypePtr & out_type) { - - // std::cerr << "=========== canConstantBeWrappedByMonotonicFunctions for " << ast->getColumnName() << std::endl; // Constant expr should use alias names if any - String passed_expr_name = ast->getColumnName(); + String passed_expr_name = ast->getColumnNameWithoutAlias(); String expr_name; if (!canConstantBeWrapped(ast, passed_expr_name, expr_name)) return false; @@ -809,7 +792,6 @@ bool KeyCondition::canConstantBeWrappedByFunctions( { /// This CAST is the same as in canConstantBeWrappedByMonotonicFunctions (see comment). auto const_type = cur_node->result_type; - // std::cerr << "==== Using type for expr " << expr_name << " " << const_type->getName() << std::endl; auto const_column = out_type->createColumnConst(1, out_value); auto const_value = (*castColumn({const_column, out_type, ""}, const_type))[0]; @@ -821,8 +803,6 @@ bool KeyCondition::canConstantBeWrappedByFunctions( if (func->type != ActionsDAG::ActionType::FUNCTION) continue; - // std::cerr << ".. chain func " << func->function_base->getName() << ' ' << func->function_builder->getName() << std::endl; - if (func->children.size() == 1) { std::tie(const_value, const_type) = applyFunctionForFieldOfUnknownType(func->function_base, const_type, const_value); diff --git a/tests/queries/0_stateless/01889_key_condition_function_chains.reference b/tests/queries/0_stateless/01889_key_condition_function_chains.reference new file mode 100644 index 00000000000..79185ae7f93 --- /dev/null +++ b/tests/queries/0_stateless/01889_key_condition_function_chains.reference @@ -0,0 +1,7 @@ +2020-02-02 01:01:01 +2020-02-02 01:01:01 +2020-02-02 01:01:01 +2020-02-02 01:01:01 +1 1 +1 1 +1 1 diff --git a/tests/queries/0_stateless/01889_key_condition_function_chains.sql b/tests/queries/0_stateless/01889_key_condition_function_chains.sql new file mode 100644 index 00000000000..5afececf1af --- /dev/null +++ b/tests/queries/0_stateless/01889_key_condition_function_chains.sql @@ -0,0 +1,33 @@ +set force_primary_key=1; + +drop table if exists tab; +create table tab (t DateTime) engine = MergeTree order by toStartOfDay(t); +insert into tab values ('2020-02-02 01:01:01'); +select t from tab where t > '2020-01-01 01:01:01'; +with t as s select t from tab where s > '2020-01-01 01:01:01'; + +drop table if exists tab; +create table tab (t DateTime) engine = MergeTree order by toStartOfDay(t + 1); +insert into tab values ('2020-02-02 01:01:01'); +select t from tab where t + 1 > '2020-01-01 01:01:01'; +with t + 1 as s select t from tab where s > '2020-01-01 01:01:01'; + + +set force_primary_key = 0; +set force_index_by_date=1; + +drop table if exists tab; +create table tab (x Int32, y Int32) engine = MergeTree partition by x + y order by tuple(); +insert into tab values (1, 1), (2, 2); +select x, y from tab where (x + y) = 2; +with x + y as s select x, y from tab where s = 2; +-- with x as s select x, y from tab where s + y = 2; + +drop table if exists tab; +create table tab (x Int32, y Int32) engine = MergeTree partition by ((x + y) + 1) * 2 order by tuple(); +insert into tab values (1, 1), (2, 2); +select x, y from tab where (x + y) + 1 = 3; +-- with x + y as s select x, y from tab where s + 1 = 3; + +set force_index_by_date=0; +drop table if exists tab;