diff --git a/src/Storages/KeyDescription.cpp b/src/Storages/KeyDescription.cpp index 534195dd0f0..264a3ab45be 100644 --- a/src/Storages/KeyDescription.cpp +++ b/src/Storages/KeyDescription.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -89,7 +90,7 @@ KeyDescription KeyDescription::getKeyFromAST( bool KeyDescription::moduloToModuloLegacyRecursive(ASTPtr node_expr) { if (!node_expr) - return; + return false; auto * function_expr = node_expr->as(); bool modulo_in_ast = false; diff --git a/src/Storages/KeyDescription.h b/src/Storages/KeyDescription.h index dda043fbb8b..c99c7ebc5c5 100644 --- a/src/Storages/KeyDescription.h +++ b/src/Storages/KeyDescription.h @@ -71,7 +71,7 @@ struct KeyDescription KeyDescription & operator=(const KeyDescription & other); /// Substitute modulo with moduloLegacy. Used in KeyCondition to allow proper comparison with keys. - static void moduloToModuloLegacyRecursive(ASTPtr node_expr); + static bool moduloToModuloLegacyRecursive(ASTPtr node_expr); }; } diff --git a/src/Storages/MergeTree/KeyCondition.cpp b/src/Storages/MergeTree/KeyCondition.cpp index 1c1b3422b31..1ce64c52f6c 100644 --- a/src/Storages/MergeTree/KeyCondition.cpp +++ b/src/Storages/MergeTree/KeyCondition.cpp @@ -602,10 +602,17 @@ bool KeyCondition::canConstantBeWrappedByMonotonicFunctions( auto adjusted_node = node->clone(); KeyDescription::moduloToModuloLegacyRecursive(adjusted_node); + String adjusted_expr_name = adjusted_node->getColumnName(); // Constant expr should use alias names if any - String expr_name = adjusted_node->getColumnName(); + String passed_expr_name = node->getColumnName(); + String expr_name; + const auto & sample_block = key_expr->getSampleBlock(); - if (!sample_block.has(expr_name)) + if (sample_block.has(passed_expr_name)) + expr_name = passed_expr_name; + else if (sample_block.has(adjusted_expr_name)) + expr_name = adjusted_expr_name; + else return false; /// TODO Nullable index is not yet landed. @@ -673,10 +680,17 @@ bool KeyCondition::canConstantBeWrappedByFunctions( auto adjusted_ast = ast->clone(); KeyDescription::moduloToModuloLegacyRecursive(adjusted_ast); + String adjusted_expr_name = adjusted_ast->getColumnName(); // Constant expr should use alias names if any - String expr_name = adjusted_ast->getColumnName(); + String passed_expr_name = ast->getColumnName(); + String expr_name; + const auto & sample_block = key_expr->getSampleBlock(); - if (!sample_block.has(expr_name)) + if (sample_block.has(passed_expr_name)) + expr_name = passed_expr_name; + else if (sample_block.has(adjusted_expr_name)) + expr_name = adjusted_expr_name; + else return false; /// TODO Nullable index is not yet landed.