diff --git a/src/Interpreters/evaluateConstantExpression.cpp b/src/Interpreters/evaluateConstantExpression.cpp index 6c08d481acf..c05118b7c6a 100644 --- a/src/Interpreters/evaluateConstantExpression.cpp +++ b/src/Interpreters/evaluateConstantExpression.cpp @@ -18,6 +18,7 @@ #include #include #include +#include namespace DB { @@ -339,6 +340,7 @@ std::optional evaluateExpressionOverConstantCondition(const ASTPtr & nod if (const auto * fn = node->as()) { + std::unordered_map always_false_map; const auto dnf = analyzeFunction(fn, target_expr, limit); if (dnf.empty() || !limit) @@ -388,8 +390,19 @@ std::optional evaluateExpressionOverConstantCondition(const ASTPtr & nod Field prev_value = assert_cast(*prev.column).getField(); Field curr_value = assert_cast(*elem.column).getField(); - if (prev_value != curr_value) - return Blocks{}; + if (!always_false_map.count(elem.name)) + { + always_false_map[elem.name] = prev_value != curr_value; + } + else + { + auto & always_false = always_false_map[elem.name]; + /// If at least one of conjunct is not always false, we should preserve this. + if (always_false) + { + always_false = prev_value != curr_value; + } + } } } } @@ -417,6 +430,11 @@ std::optional evaluateExpressionOverConstantCondition(const ASTPtr & nod return {}; } } + + bool any_always_false = std::any_of(always_false_map.begin(), always_false_map.end(), [](const auto & v) { return v.second; }); + if (any_always_false) + return Blocks{}; + } else if (const auto * literal = node->as()) {