apply final only on storages that support it

This commit is contained in:
Arthur Passos 2022-12-05 10:18:44 -03:00
parent 252575ee23
commit 3239516521
4 changed files with 25 additions and 3 deletions

View File

@ -499,9 +499,7 @@ InterpreterSelectQuery::InterpreterSelectQuery(
query_info.additional_filter_ast = parseAdditionalFilterConditionForTable(
settings.additional_table_filters, joined_tables.tablesWithColumns().front().table, *context);
// query.tables() is required because not all queries have tables in it, it could be a function.
if (context->getSettingsRef().force_select_final && !query.final() && query.tables())
if (forceSelectFinalOnSelectQuery(query))
{
query.setFinal();
}
@ -2916,6 +2914,15 @@ void InterpreterSelectQuery::ignoreWithTotals()
getSelectQuery().group_by_with_totals = false;
}
bool InterpreterSelectQuery::forceSelectFinalOnSelectQuery(ASTSelectQuery & query)
{
// query.tables() is required because not all queries have tables in it, it could be a function.
auto isFinalSupported = storage && storage->supportsFinal() && query.tables();
auto isForceSelectFinalSettingOn = context->getSettingsRef().force_select_final;
auto isQueryAlreadyFinal = query.final();
return isForceSelectFinalSettingOn && !isQueryAlreadyFinal && isFinalSupported;
}
void InterpreterSelectQuery::initSettings()
{

View File

@ -194,6 +194,7 @@ private:
void executeDistinct(QueryPlan & query_plan, bool before_order, Names columns, bool pre_distinct);
void executeExtremes(QueryPlan & query_plan);
void executeSubqueriesInSetsAndJoins(QueryPlan & query_plan);
bool forceSelectFinalOnSelectQuery(ASTSelectQuery & select_query);
enum class Modificator
{

View File

@ -26,3 +26,10 @@ set force_select_final = 1;
-- expected output is 1 because force_select_final == 1
select count() from lhs inner join rhs on lhs.x = rhs.x;
1
-- regular non final table
set force_select_final = 1;
create table if not exists regular_mt_table (x String) engine=MergeTree() ORDER BY x;
insert into regular_mt_table values ('abc');
-- expected output is 1, it should silently ignore final modifier
select count() from regular_mt_table;
1

View File

@ -31,3 +31,10 @@ select count() from lhs inner join rhs on lhs.x = rhs.x;
set force_select_final = 1;
-- expected output is 1 because force_select_final == 1
select count() from lhs inner join rhs on lhs.x = rhs.x;
-- regular non final table
set force_select_final = 1;
create table if not exists regular_mt_table (x String) engine=MergeTree() ORDER BY x;
insert into regular_mt_table values ('abc');
-- expected output is 1, it should silently ignore final modifier
select count() from regular_mt_table;