Added test.

This commit is contained in:
Nikolai Kochetov 2021-06-07 21:24:32 +03:00
parent 0d2a839ca4
commit c4832fd3c0
3 changed files with 41 additions and 21 deletions

View File

@ -500,17 +500,10 @@ static std::pair<Field, DataTypePtr> applyFunctionForFieldOfUnknownType(
const Field & arg_value) const Field & arg_value)
{ {
ColumnsWithTypeAndName arguments{{ arg_type->createColumnConst(1, arg_value), arg_type, "x" }}; 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(); DataTypePtr return_type = func->getResultType();
auto col = func->execute(arguments, return_type, 1); auto col = func->execute(arguments, return_type, 1);
// std::cerr << ">>>>> got " << ColumnWithTypeAndName(col, return_type, "").dumpStructure() << std::endl;
Field result = (*col)[0]; Field result = (*col)[0];
return {std::move(result), std::move(return_type)}; return {std::move(result), std::move(return_type)};
@ -602,12 +595,7 @@ bool KeyCondition::canConstantBeWrapped(const ASTPtr & node, const String & expr
{ {
NameSet names; NameSet names;
for (const auto & action : key_expr->getActions()) for (const auto & action : key_expr->getActions())
{
names.insert(action.node->result_name); 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. /// sample_block from key_expr cannot contain modulo and moduloLegacy at the same time.
/// For partition key it is always moduloLegacy. /// For partition key it is always moduloLegacy.
@ -637,8 +625,6 @@ bool KeyCondition::canConstantBeWrappedByMonotonicFunctions(
Field & out_value [[maybe_unused]], Field & out_value [[maybe_unused]],
DataTypePtr & out_type [[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 // Constant expr should use alias names if any
String passed_expr_name = node->getColumnNameWithoutAlias(); 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). /// 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. /// Let's assume that any two comparable types are castable to each other.
auto const_type = cur_node->result_type; 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_column = out_type->createColumnConst(1, out_value);
auto const_value = (*castColumn({const_column, out_type, ""}, const_type))[0]; auto const_value = (*castColumn({const_column, out_type, ""}, const_type))[0];
@ -746,10 +731,8 @@ bool KeyCondition::canConstantBeWrappedByMonotonicFunctions(
bool KeyCondition::canConstantBeWrappedByFunctions( bool KeyCondition::canConstantBeWrappedByFunctions(
const ASTPtr & ast, size_t & out_key_column_num, DataTypePtr & out_key_column_type, Field & out_value, DataTypePtr & out_type) 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 // Constant expr should use alias names if any
String passed_expr_name = ast->getColumnName(); String passed_expr_name = ast->getColumnNameWithoutAlias();
String expr_name; String expr_name;
if (!canConstantBeWrapped(ast, passed_expr_name, expr_name)) if (!canConstantBeWrapped(ast, passed_expr_name, expr_name))
return false; return false;
@ -809,7 +792,6 @@ bool KeyCondition::canConstantBeWrappedByFunctions(
{ {
/// This CAST is the same as in canConstantBeWrappedByMonotonicFunctions (see comment). /// This CAST is the same as in canConstantBeWrappedByMonotonicFunctions (see comment).
auto const_type = cur_node->result_type; 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_column = out_type->createColumnConst(1, out_value);
auto const_value = (*castColumn({const_column, out_type, ""}, const_type))[0]; auto const_value = (*castColumn({const_column, out_type, ""}, const_type))[0];
@ -821,8 +803,6 @@ bool KeyCondition::canConstantBeWrappedByFunctions(
if (func->type != ActionsDAG::ActionType::FUNCTION) if (func->type != ActionsDAG::ActionType::FUNCTION)
continue; continue;
// std::cerr << ".. chain func " << func->function_base->getName() << ' ' << func->function_builder->getName() << std::endl;
if (func->children.size() == 1) if (func->children.size() == 1)
{ {
std::tie(const_value, const_type) = applyFunctionForFieldOfUnknownType(func->function_base, const_type, const_value); std::tie(const_value, const_type) = applyFunctionForFieldOfUnknownType(func->function_base, const_type, const_value);

View File

@ -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

View File

@ -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;