Disable logical expression optimizer for expression with aliases. Never use it with optimize_min_equality_disjunction_chain_length=0

This commit is contained in:
Nikolai Kochetov 2023-03-10 20:14:35 +00:00
parent 6a086ab926
commit aaecc233b2
3 changed files with 9 additions and 3 deletions

View File

@ -119,7 +119,7 @@ void LogicalExpressionsOptimizer::collectDisjunctiveEqualityChains()
bool found_chain = false; bool found_chain = false;
auto * function = to_node->as<ASTFunction>(); auto * function = to_node->as<ASTFunction>();
if (function && function->name == "or" && function->children.size() == 1) if (function && function->alias.empty() && function->name == "or" && function->children.size() == 1)
{ {
const auto * expression_list = function->children[0]->as<ASTExpressionList>(); const auto * expression_list = function->children[0]->as<ASTExpressionList>();
if (expression_list) if (expression_list)
@ -128,14 +128,14 @@ void LogicalExpressionsOptimizer::collectDisjunctiveEqualityChains()
for (const auto & child : expression_list->children) for (const auto & child : expression_list->children)
{ {
auto * equals = child->as<ASTFunction>(); auto * equals = child->as<ASTFunction>();
if (equals && equals->name == "equals" && equals->children.size() == 1) if (equals && equals->alias.empty() && equals->name == "equals" && equals->children.size() == 1)
{ {
const auto * equals_expression_list = equals->children[0]->as<ASTExpressionList>(); const auto * equals_expression_list = equals->children[0]->as<ASTExpressionList>();
if (equals_expression_list && equals_expression_list->children.size() == 2) if (equals_expression_list && equals_expression_list->children.size() == 2)
{ {
/// Equality expr = xN. /// Equality expr = xN.
const auto * literal = equals_expression_list->children[1]->as<ASTLiteral>(); const auto * literal = equals_expression_list->children[1]->as<ASTLiteral>();
if (literal) if (literal && literal->alias.empty())
{ {
auto expr_lhs = equals_expression_list->children[0]->getTreeHash(); auto expr_lhs = equals_expression_list->children[0]->getTreeHash();
OrWithExpression or_with_expression{function, expr_lhs, function->tryGetAlias()}; OrWithExpression or_with_expression{function, expr_lhs, function->tryGetAlias()};
@ -230,6 +230,9 @@ bool LogicalExpressionsOptimizer::mayOptimizeDisjunctiveEqualityChain(const Disj
const auto & equalities = chain.second; const auto & equalities = chain.second;
const auto & equality_functions = equalities.functions; const auto & equality_functions = equalities.functions;
if (settings.optimize_min_equality_disjunction_chain_length == 0)
return false;
/// For LowCardinality column, the dict is usually smaller and the index is relatively large. /// For LowCardinality column, the dict is usually smaller and the index is relatively large.
/// In most cases, merging OR-chain as IN is better than converting each LowCardinality into full column individually. /// In most cases, merging OR-chain as IN is better than converting each LowCardinality into full column individually.
/// For non-LowCardinality, we need to eliminate too short chains. /// For non-LowCardinality, we need to eliminate too short chains.

View File

@ -0,0 +1,2 @@
create table test_local (id UInt32, path LowCardinality(String)) engine = MergeTree order by id;
WITH ((position(path, '/a') > 0) AND (NOT (position(path, 'a') > 0))) OR (path = '/b') OR (path = '/b/') as alias1 SELECT max(alias1) FROM remote('127.0.0.{1,2}', currentDatabase(), test_local) WHERE (id = 299386662);