From 7033a56ff2bc7b9bc19b40066ffc2bba8a7a67ea Mon Sep 17 00:00:00 2001 From: Smita Kulkarni Date: Mon, 3 Oct 2022 16:42:44 +0200 Subject: [PATCH] Moved QueryParameterVisitor to Parsers & EXPLAIN SYNTAX test fix - 40907 Parameterized views as table functions --- src/Interpreters/ActionsVisitor.cpp | 2 +- src/Interpreters/ExpressionAnalyzer.cpp | 6 ++++++ src/Parsers/ASTSelectQuery.cpp | 5 ++++- src/Parsers/ASTSelectQuery.h | 1 - src/Parsers/ASTSelectWithUnionQuery.cpp | 2 ++ src/Parsers/ASTTablesInSelectQuery.cpp | 10 ++++++++++ .../QueryParameterVisitor.cpp | 2 +- src/{Interpreters => Parsers}/QueryParameterVisitor.h | 0 src/Server/HTTPHandler.cpp | 2 +- 9 files changed, 25 insertions(+), 5 deletions(-) rename src/{Interpreters => Parsers}/QueryParameterVisitor.cpp (96%) rename src/{Interpreters => Parsers}/QueryParameterVisitor.h (100%) diff --git a/src/Interpreters/ActionsVisitor.cpp b/src/Interpreters/ActionsVisitor.cpp index 148d8e4d30b..c694f6007fc 100644 --- a/src/Interpreters/ActionsVisitor.cpp +++ b/src/Interpreters/ActionsVisitor.cpp @@ -54,7 +54,7 @@ #include #include #include -#include +#include namespace DB diff --git a/src/Interpreters/ExpressionAnalyzer.cpp b/src/Interpreters/ExpressionAnalyzer.cpp index b49df1b1fe7..4efb08c414f 100644 --- a/src/Interpreters/ExpressionAnalyzer.cpp +++ b/src/Interpreters/ExpressionAnalyzer.cpp @@ -1286,6 +1286,8 @@ bool SelectQueryExpressionAnalyzer::appendWhere(ExpressionActionsChain & chain, getRootActions(select_query->where(), only_types, step.actions()); + //For creating parameterized view, query parameters are allowed in select + //As select will be stored without substituting query parameters, we don't want to evaluate the where expression if (select_query->allow_query_parameters && select_query->hasQueryParameters()) return true; @@ -1906,6 +1908,8 @@ ExpressionAnalysisResult::ExpressionAnalysisResult( before_where, ExpressionActionsSettings::fromSettings(context->getSettingsRef())).execute(before_where_sample); + //For creating parameterized view, query parameters are allowed in select + //As select will be stored without substituting query parameters, we don't want to evaluate the where expression bool has_query_parameters = query.allow_query_parameters && query.hasQueryParameters(); if (!has_query_parameters) { @@ -2075,6 +2079,8 @@ void ExpressionAnalysisResult::finalize( ssize_t & having_step_num, const ASTSelectQuery & query) { + //For creating parameterized view, query parameters are allowed in select + //As select will be stored without substituting query parameters, we don't want to evaluate the expressions/steps if (query.allow_query_parameters && query.hasQueryParameters()) return; diff --git a/src/Parsers/ASTSelectQuery.cpp b/src/Parsers/ASTSelectQuery.cpp index aa4ff96e050..c2ca04eaa13 100644 --- a/src/Parsers/ASTSelectQuery.cpp +++ b/src/Parsers/ASTSelectQuery.cpp @@ -9,7 +9,7 @@ #include #include #include -#include +#include #include @@ -41,6 +41,9 @@ ASTPtr ASTSelectQuery::clone() const for (const auto & child : children) res->children.push_back(child->clone()); + res->allow_query_parameters = allow_query_parameters; + res->has_query_parameters = has_query_parameters; + return res; } diff --git a/src/Parsers/ASTSelectQuery.h b/src/Parsers/ASTSelectQuery.h index b3f29009df6..dae718aa040 100644 --- a/src/Parsers/ASTSelectQuery.h +++ b/src/Parsers/ASTSelectQuery.h @@ -2,7 +2,6 @@ #include #include -#include namespace DB { diff --git a/src/Parsers/ASTSelectWithUnionQuery.cpp b/src/Parsers/ASTSelectWithUnionQuery.cpp index 50e929a6f46..792fffe5f12 100644 --- a/src/Parsers/ASTSelectWithUnionQuery.cpp +++ b/src/Parsers/ASTSelectWithUnionQuery.cpp @@ -23,6 +23,8 @@ ASTPtr ASTSelectWithUnionQuery::clone() const res->list_of_modes = list_of_modes; res->set_of_modes = set_of_modes; + res->has_query_parameters = has_query_parameters; + cloneOutputOptions(*res); return res; } diff --git a/src/Parsers/ASTTablesInSelectQuery.cpp b/src/Parsers/ASTTablesInSelectQuery.cpp index 3f687f76c86..85db26efcd2 100644 --- a/src/Parsers/ASTTablesInSelectQuery.cpp +++ b/src/Parsers/ASTTablesInSelectQuery.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace DB @@ -112,6 +113,15 @@ void ASTTableExpression::formatImpl(const FormatSettings & settings, FormatState settings.ostr << " "; database_and_table_name->formatImpl(settings, state, frame); } + //In case of table function view, table_function is preferred over subquery for EXPLAIN SYNTAX + else if (table_function && table_function->as() && table_function->as()->name=="view") + { + settings.ostr << " "; + table_function->formatImpl(settings, state, frame); + + } + //For parameterized view, subquery is preferred over table_function for EXPLAIN SYNTAX + //we cannot remove the table function part, as its needed for query substitution else if (subquery) { settings.ostr << settings.nl_or_ws << indent_str; diff --git a/src/Interpreters/QueryParameterVisitor.cpp b/src/Parsers/QueryParameterVisitor.cpp similarity index 96% rename from src/Interpreters/QueryParameterVisitor.cpp rename to src/Parsers/QueryParameterVisitor.cpp index 491c05ac3d2..14750845034 100644 --- a/src/Interpreters/QueryParameterVisitor.cpp +++ b/src/Parsers/QueryParameterVisitor.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/src/Interpreters/QueryParameterVisitor.h b/src/Parsers/QueryParameterVisitor.h similarity index 100% rename from src/Interpreters/QueryParameterVisitor.h rename to src/Parsers/QueryParameterVisitor.h diff --git a/src/Server/HTTPHandler.cpp b/src/Server/HTTPHandler.cpp index 8886a77c9b5..45d4bd824f2 100644 --- a/src/Server/HTTPHandler.cpp +++ b/src/Server/HTTPHandler.cpp @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include