Fix comparisons with modulo key (version 2)

This commit is contained in:
kssenii 2021-05-21 16:40:47 +00:00
parent 94b5763dbd
commit fcfec83875
3 changed files with 21 additions and 6 deletions

View File

@ -2,6 +2,7 @@
#include <Functions/IFunction.h> #include <Functions/IFunction.h>
#include <Parsers/ASTIdentifier.h> #include <Parsers/ASTIdentifier.h>
#include <Parsers/ASTFunction.h>
#include <Interpreters/ExpressionActions.h> #include <Interpreters/ExpressionActions.h>
#include <Interpreters/ExpressionAnalyzer.h> #include <Interpreters/ExpressionAnalyzer.h>
#include <Interpreters/TreeRewriter.h> #include <Interpreters/TreeRewriter.h>
@ -89,7 +90,7 @@ KeyDescription KeyDescription::getKeyFromAST(
bool KeyDescription::moduloToModuloLegacyRecursive(ASTPtr node_expr) bool KeyDescription::moduloToModuloLegacyRecursive(ASTPtr node_expr)
{ {
if (!node_expr) if (!node_expr)
return; return false;
auto * function_expr = node_expr->as<ASTFunction>(); auto * function_expr = node_expr->as<ASTFunction>();
bool modulo_in_ast = false; bool modulo_in_ast = false;

View File

@ -71,7 +71,7 @@ struct KeyDescription
KeyDescription & operator=(const KeyDescription & other); KeyDescription & operator=(const KeyDescription & other);
/// Substitute modulo with moduloLegacy. Used in KeyCondition to allow proper comparison with keys. /// 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);
}; };
} }

View File

@ -602,10 +602,17 @@ bool KeyCondition::canConstantBeWrappedByMonotonicFunctions(
auto adjusted_node = node->clone(); auto adjusted_node = node->clone();
KeyDescription::moduloToModuloLegacyRecursive(adjusted_node); KeyDescription::moduloToModuloLegacyRecursive(adjusted_node);
String adjusted_expr_name = adjusted_node->getColumnName();
// Constant expr should use alias names if any // 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(); 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; return false;
/// TODO Nullable index is not yet landed. /// TODO Nullable index is not yet landed.
@ -673,10 +680,17 @@ bool KeyCondition::canConstantBeWrappedByFunctions(
auto adjusted_ast = ast->clone(); auto adjusted_ast = ast->clone();
KeyDescription::moduloToModuloLegacyRecursive(adjusted_ast); KeyDescription::moduloToModuloLegacyRecursive(adjusted_ast);
String adjusted_expr_name = adjusted_ast->getColumnName();
// Constant expr should use alias names if any // 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(); 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; return false;
/// TODO Nullable index is not yet landed. /// TODO Nullable index is not yet landed.