This commit is contained in:
Arthur Passos 2023-02-01 14:29:59 -03:00
parent a8489fbef3
commit 7e7b1829be
3 changed files with 77 additions and 4 deletions

View File

@ -0,0 +1,37 @@
#include "AutoFinalOnQueryVisitor.h"
#include <Storages/IStorage.h>
#include <Interpreters/Context.h>
namespace DB
{
void AutoFinalOnQuery::visit(ASTPtr & query, Data & data)
{
if (auto * select_query = query->as<ASTSelectQuery>())
visit(*select_query, data.storage, data.context);
}
void AutoFinalOnQuery::visit(ASTSelectQuery & query, StoragePtr storage, ContextPtr context)
{
if (autoFinalOnQuery(query, storage, context))
{
query.setFinal();
}
}
bool AutoFinalOnQuery::needChildVisit(ASTPtr &, const ASTPtr &)
{
return true;
}
bool AutoFinalOnQuery::autoFinalOnQuery(ASTSelectQuery & query, StoragePtr storage, ContextPtr context)
{
// query.tables() is required because not all queries have tables in it, it could be a function.
bool is_auto_final_setting_on = context->getSettingsRef().final;
bool is_final_supported = storage && storage->supportsFinal() && !storage->isRemote() && query.tables();
bool is_query_already_final = query.final();
return is_auto_final_setting_on && !is_query_already_final && is_final_supported;
}
}

View File

@ -0,0 +1,31 @@
#pragma once
#include <Parsers/ASTSelectQuery.h>
#include <Interpreters/Context_fwd.h>
#include <Storages/IStorage_fwd.h>
#include <Interpreters/InDepthNodeVisitor.h>
namespace DB
{
class AutoFinalOnQuery
{
public:
struct Data
{
StoragePtr storage;
ContextPtr context;
};
static bool needChildVisit(ASTPtr &, const ASTPtr &);
static void visit(ASTPtr & query, Data & data);
private:
static void visit(ASTSelectQuery & select, StoragePtr storage, ContextPtr context);
static bool autoFinalOnQuery(ASTSelectQuery & select_query, StoragePtr storage, ContextPtr context);
};
using AutoFinalOnQueryVisitor = InDepthNodeVisitor<AutoFinalOnQuery, true>;
}

View File

@ -38,6 +38,7 @@
#include <Interpreters/QueryLog.h> #include <Interpreters/QueryLog.h>
#include <Interpreters/replaceAliasColumnsInQuery.h> #include <Interpreters/replaceAliasColumnsInQuery.h>
#include <Interpreters/RewriteCountDistinctVisitor.h> #include <Interpreters/RewriteCountDistinctVisitor.h>
#include <Interpreters/AutoFinalOnQueryVisitor.h>
#include <QueryPipeline/Pipe.h> #include <QueryPipeline/Pipe.h>
#include <Processors/QueryPlan/AggregatingStep.h> #include <Processors/QueryPlan/AggregatingStep.h>
@ -503,10 +504,14 @@ InterpreterSelectQuery::InterpreterSelectQuery(
query_info.additional_filter_ast = parseAdditionalFilterConditionForTable( query_info.additional_filter_ast = parseAdditionalFilterConditionForTable(
settings.additional_table_filters, joined_tables.tablesWithColumns().front().table, *context); settings.additional_table_filters, joined_tables.tablesWithColumns().front().table, *context);
if (autoFinalOnQuery(query)) AutoFinalOnQuery::Data abc{storage, context};
{
query.setFinal(); AutoFinalOnQueryVisitor(abc).visit(query_ptr);
}
// if (autoFinalOnQuery(query))
// {
// query.setFinal();
// }
auto analyze = [&] (bool try_move_to_prewhere) auto analyze = [&] (bool try_move_to_prewhere)
{ {