fix crash with multiif and constant condition and nullable arguments

This commit is contained in:
Anton Popov 2023-05-22 23:31:50 +00:00
parent afc823032c
commit f8905acb46
6 changed files with 15 additions and 11 deletions

View File

@ -440,7 +440,6 @@ InterpreterSelectQuery::InterpreterSelectQuery(
if (has_input || !joined_tables.resolveTables())
joined_tables.makeFakeTable(storage, metadata_snapshot, source_header);
if (context->getCurrentTransaction() && context->getSettingsRef().throw_on_unsupported_query_inside_transaction)
{
if (storage)

View File

@ -703,8 +703,11 @@ void optimizeOrLikeChain(ASTPtr & query)
}
void TreeOptimizer::optimizeIf(ASTPtr & query, Aliases & aliases, bool if_chain_to_multiif)
void TreeOptimizer::optimizeIf(ASTPtr & query, Aliases & aliases, bool if_chain_to_multiif, bool multiif_to_if)
{
if (multiif_to_if)
optimizeMultiIfToIf(query);
/// Optimize if with constant condition after constants was substituted instead of scalar subqueries.
OptimizeIfWithConstantConditionVisitor(aliases).visit(query);
@ -791,9 +794,6 @@ void TreeOptimizer::apply(ASTPtr & query, TreeRewriterResult & result,
if (settings.optimize_normalize_count_variants)
optimizeCountConstantAndSumOne(query, context);
if (settings.optimize_multiif_to_if)
optimizeMultiIfToIf(query);
if (settings.optimize_rewrite_sum_if_to_count_if)
optimizeSumIfFunctions(query);

View File

@ -23,7 +23,7 @@ public:
const std::vector<TableWithColumnNamesAndTypes> & tables_with_columns,
ContextPtr context);
static void optimizeIf(ASTPtr & query, Aliases & aliases, bool if_chain_to_multiif);
static void optimizeIf(ASTPtr & query, Aliases & aliases, bool if_chain_to_multiif, bool multiif_to_if);
static void optimizeCountConstantAndSumOne(ASTPtr & query, ContextPtr context);
static void optimizeGroupByFunctionKeys(ASTSelectQuery * select_query);
};

View File

@ -1230,11 +1230,14 @@ TreeRewriterResultPtr TreeRewriter::analyzeSelect(
/// Push the predicate expression down to subqueries. The optimization should be applied to both initial and secondary queries.
result.rewrite_subqueries = PredicateExpressionsOptimizer(getContext(), tables_with_columns, settings).optimize(*select_query);
TreeOptimizer::optimizeIf(query, result.aliases, settings.optimize_if_chain_to_multiif);
/// Only apply AST optimization for initial queries.
const bool ast_optimizations_allowed =
getContext()->getClientInfo().query_kind != ClientInfo::QueryKind::SECONDARY_QUERY
&& !select_options.ignore_ast_optimizations;
bool optimize_multiif_to_if = ast_optimizations_allowed && settings.optimize_multiif_to_if;
TreeOptimizer::optimizeIf(query, result.aliases, settings.optimize_if_chain_to_multiif, optimize_multiif_to_if);
/// Only apply AST optimization for initial queries.
const bool ast_optimizations_allowed
= getContext()->getClientInfo().query_kind != ClientInfo::QueryKind::SECONDARY_QUERY && !select_options.ignore_ast_optimizations;
if (ast_optimizations_allowed)
TreeOptimizer::apply(query, result, tables_with_columns, getContext());
@ -1341,7 +1344,7 @@ TreeRewriterResultPtr TreeRewriter::analyze(
if (settings.legacy_column_name_of_tuple_literal)
markTupleLiteralsAsLegacy(query);
TreeOptimizer::optimizeIf(query, result.aliases, settings.optimize_if_chain_to_multiif);
TreeOptimizer::optimizeIf(query, result.aliases, settings.optimize_if_chain_to_multiif, false);
if (allow_aggregations)
{

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1 @@
SELECT sum(A) FROM (SELECT multiIf(1, 1, NULL) as A);