2017-05-23 18:24:43 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2024-01-09 06:33:48 +00:00
|
|
|
#include <Interpreters/InterpreterFactory.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterSetQuery.h>
|
2023-03-24 02:44:52 +00:00
|
|
|
#include <Parsers/ASTSelectQuery.h>
|
2022-08-12 12:28:35 +00:00
|
|
|
#include <Parsers/ASTSetQuery.h>
|
2023-03-10 15:24:47 +00:00
|
|
|
#include <Parsers/ASTCreateQuery.h>
|
|
|
|
#include <Parsers/ASTExplainQuery.h>
|
|
|
|
#include <Parsers/ASTInsertQuery.h>
|
|
|
|
#include <Parsers/ASTQueryWithOutput.h>
|
|
|
|
#include <Parsers/ASTSelectWithUnionQuery.h>
|
2016-12-12 07:24:56 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
BlockIO InterpreterSetQuery::execute()
|
|
|
|
{
|
2019-03-15 16:14:13 +00:00
|
|
|
const auto & ast = query_ptr->as<ASTSetQuery &>();
|
2023-07-30 22:09:03 +00:00
|
|
|
getContext()->checkSettingsConstraints(ast.changes, SettingSource::QUERY);
|
2022-08-12 12:28:35 +00:00
|
|
|
auto session_context = getContext()->getSessionContext();
|
|
|
|
session_context->applySettingsChanges(ast.changes);
|
2024-02-26 01:11:51 +00:00
|
|
|
session_context->addQueryParameters(NameToNameMap{ast.query_parameters.begin(), ast.query_parameters.end()});
|
2022-10-11 18:25:28 +00:00
|
|
|
session_context->resetSettingsToDefaultValue(ast.default_settings);
|
2017-06-07 12:54:35 +00:00
|
|
|
return {};
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 12:54:35 +00:00
|
|
|
|
2023-07-11 15:23:15 +00:00
|
|
|
void InterpreterSetQuery::executeForCurrentContext(bool ignore_setting_constraints)
|
2017-06-07 12:54:35 +00:00
|
|
|
{
|
2019-03-15 16:14:13 +00:00
|
|
|
const auto & ast = query_ptr->as<ASTSetQuery &>();
|
2023-07-11 15:23:15 +00:00
|
|
|
if (!ignore_setting_constraints)
|
2023-07-30 22:09:03 +00:00
|
|
|
getContext()->checkSettingsConstraints(ast.changes, SettingSource::QUERY);
|
2021-04-10 23:33:54 +00:00
|
|
|
getContext()->applySettingsChanges(ast.changes);
|
2022-10-11 18:25:28 +00:00
|
|
|
getContext()->resetSettingsToDefaultValue(ast.default_settings);
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-10 15:24:47 +00:00
|
|
|
static void applySettingsFromSelectWithUnion(const ASTSelectWithUnionQuery & select_with_union, ContextMutablePtr context)
|
|
|
|
{
|
|
|
|
const ASTs & children = select_with_union.list_of_selects->children;
|
|
|
|
if (children.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// We might have an arbitrarily complex UNION tree, so just give
|
|
|
|
// up if the last first-order child is not a plain SELECT.
|
|
|
|
// It is flattened later, when we process UNION ALL/DISTINCT.
|
|
|
|
const auto * last_select = children.back()->as<ASTSelectQuery>();
|
|
|
|
if (last_select && last_select->settings())
|
|
|
|
{
|
|
|
|
InterpreterSetQuery(last_select->settings(), context).executeForCurrentContext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterpreterSetQuery::applySettingsFromQuery(const ASTPtr & ast, ContextMutablePtr context_)
|
|
|
|
{
|
2023-03-12 11:13:44 +00:00
|
|
|
if (!ast)
|
|
|
|
return;
|
|
|
|
|
2023-03-10 15:24:47 +00:00
|
|
|
if (const auto * select_query = ast->as<ASTSelectQuery>())
|
|
|
|
{
|
|
|
|
if (auto new_settings = select_query->settings())
|
|
|
|
InterpreterSetQuery(new_settings, context_).executeForCurrentContext();
|
|
|
|
}
|
|
|
|
else if (const auto * select_with_union_query = ast->as<ASTSelectWithUnionQuery>())
|
|
|
|
{
|
|
|
|
applySettingsFromSelectWithUnion(*select_with_union_query, context_);
|
|
|
|
}
|
|
|
|
else if (const auto * explain_query = ast->as<ASTExplainQuery>())
|
|
|
|
{
|
2023-07-24 12:57:05 +00:00
|
|
|
if (explain_query->settings_ast)
|
|
|
|
InterpreterSetQuery(explain_query->settings_ast, context_).executeForCurrentContext();
|
|
|
|
|
2023-03-10 15:24:47 +00:00
|
|
|
applySettingsFromQuery(explain_query->getExplainedQuery(), context_);
|
|
|
|
}
|
|
|
|
else if (const auto * query_with_output = dynamic_cast<const ASTQueryWithOutput *>(ast.get()))
|
|
|
|
{
|
|
|
|
if (query_with_output->settings_ast)
|
|
|
|
InterpreterSetQuery(query_with_output->settings_ast, context_).executeForCurrentContext();
|
|
|
|
|
|
|
|
if (const auto * create_query = ast->as<ASTCreateQuery>())
|
|
|
|
{
|
|
|
|
if (create_query->select)
|
|
|
|
{
|
|
|
|
applySettingsFromSelectWithUnion(create_query->select->as<ASTSelectWithUnionQuery &>(), context_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (auto * insert_query = ast->as<ASTInsertQuery>())
|
|
|
|
{
|
|
|
|
context_->setInsertFormat(insert_query->format);
|
|
|
|
if (insert_query->settings_ast)
|
|
|
|
InterpreterSetQuery(insert_query->settings_ast, context_).executeForCurrentContext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 06:33:48 +00:00
|
|
|
void registerInterpreterSetQuery(InterpreterFactory & factory)
|
|
|
|
{
|
|
|
|
auto create_fn = [] (const InterpreterFactory::Arguments & args)
|
|
|
|
{
|
|
|
|
return std::make_unique<InterpreterSetQuery>(args.query, args.context);
|
|
|
|
};
|
|
|
|
factory.registerInterpreter("InterpreterSetQuery", create_fn);
|
|
|
|
}
|
2016-12-12 07:24:56 +00:00
|
|
|
}
|