diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index da08ee8d389..6e7259a98e3 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -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() { diff --git a/src/Interpreters/InterpreterSelectQuery.h b/src/Interpreters/InterpreterSelectQuery.h index 761eea8e1b8..f679051532c 100644 --- a/src/Interpreters/InterpreterSelectQuery.h +++ b/src/Interpreters/InterpreterSelectQuery.h @@ -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 { diff --git a/tests/queries/0_stateless/02420_force_select_final_setting.reference b/tests/queries/0_stateless/02420_force_select_final_setting.reference index 36b8905289b..ad1a0bc1567 100644 --- a/tests/queries/0_stateless/02420_force_select_final_setting.reference +++ b/tests/queries/0_stateless/02420_force_select_final_setting.reference @@ -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 diff --git a/tests/queries/0_stateless/02420_force_select_final_setting.sql b/tests/queries/0_stateless/02420_force_select_final_setting.sql index ae925a5dd14..7dd7eb05b12 100644 --- a/tests/queries/0_stateless/02420_force_select_final_setting.sql +++ b/tests/queries/0_stateless/02420_force_select_final_setting.sql @@ -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;