Merge pull request #11818 from ClickHouse/fix-bad-code-optimizations

Fix another 10% of bad code.
This commit is contained in:
alexey-milovidov 2020-06-20 16:49:23 +03:00 committed by GitHub
commit f9372bfbbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 31 deletions

View File

@ -158,7 +158,7 @@ struct Settings : public SettingsCollection<Settings>
M(SettingInt64, os_thread_priority, 0, "If non zero - set corresponding 'nice' value for query processing threads. Can be used to adjust query priority for OS scheduler.", 0) \
\
M(SettingBool, log_queries, 1, "Log requests and write the log to the system table.", 0) \
M(SettingLogQueriesType, log_queries_min_type, QueryLogElementType::QUERY_START, "query_log minimal type to log, possible values (from low to high): QUERY_START, QUERY_FINISH, EXCEPTION_BEFORE_START, EXCEPTION_WHILE_PROCESSING.", 0) \
M(SettingLogQueriesType, log_queries_min_type, QueryLogElementType::QUERY_START, "Minimal type in query_log to log, possible values (from low to high): QUERY_START, QUERY_FINISH, EXCEPTION_BEFORE_START, EXCEPTION_WHILE_PROCESSING.", 0) \
M(SettingUInt64, log_queries_cut_to_length, 100000, "If query length is greater than specified threshold (in bytes), then cut query when writing to query log. Also limit length of printed query in ordinary text log.", 0) \
\
M(SettingDistributedProductMode, distributed_product_mode, DistributedProductMode::DENY, "How are distributed subqueries performed inside IN or JOIN sections?", IMPORTANT) \
@ -360,7 +360,7 @@ struct Settings : public SettingsCollection<Settings>
M(SettingBool, enable_scalar_subquery_optimization, true, "If it is set to true, prevent scalar subqueries from (de)serializing large scalar values and possibly avoid running the same subquery more than once.", 0) \
M(SettingBool, optimize_trivial_count_query, true, "Process trivial 'SELECT count() FROM table' query from metadata.", 0) \
M(SettingUInt64, mutations_sync, 0, "Wait for synchronous execution of ALTER TABLE UPDATE/DELETE queries (mutations). 0 - execute asynchronously. 1 - wait current server. 2 - wait all replicas if they exist.", 0) \
M(SettingBool, optimize_any_input, true, "removal of any operations from Any", 0) \
M(SettingBool, optimize_move_functions_out_of_any, true, "Move functions out of aggregate functions 'any', 'anyLast'.", 0) \
M(SettingBool, optimize_arithmetic_operations_in_aggregate_functions, true, "Move arithmetic operations out of aggregation functions", 0) \
M(SettingBool, optimize_duplicate_order_by_and_distinct, true, "Remove duplicate ORDER BY and DISTINCT if it's possible", 0) \
M(SettingBool, optimize_if_chain_to_miltiif, false, "Replace if(cond1, then1, if(cond2, ...)) chains to multiIf. Currently it's not beneficial for numeric types.", 0) \

View File

@ -394,11 +394,8 @@ GroupByKeysInfo getGroupByKeysInfo(ASTs & group_keys)
}
///eliminate functions of other GROUP BY keys
void optimizeGroupByFunctionKeys(ASTSelectQuery * select_query, bool optimize_group_by_function_keys)
void optimizeGroupByFunctionKeys(ASTSelectQuery * select_query)
{
if (!optimize_group_by_function_keys)
return;
if (!select_query->groupBy())
return;
@ -446,11 +443,8 @@ void optimizeGroupByFunctionKeys(ASTSelectQuery * select_query, bool optimize_gr
}
/// Eliminates min/max/any-aggregators of functions of GROUP BY keys
void optimizeAggregateFunctionsOfGroupByKeys(ASTSelectQuery * select_query, bool optimize_aggregators_of_group_by_keys)
void optimizeAggregateFunctionsOfGroupByKeys(ASTSelectQuery * select_query)
{
if (!optimize_aggregators_of_group_by_keys)
return;
if (!select_query->groupBy())
return;
@ -493,16 +487,13 @@ void optimizeOrderBy(const ASTSelectQuery * select_query)
}
/// Optimize duplicate ORDER BY and DISTINCT
void optimizeDuplicateOrderByAndDistinct(ASTPtr & query, bool optimize_duplicate_order_by_and_distinct, const Context & context)
{
if (optimize_duplicate_order_by_and_distinct)
void optimizeDuplicateOrderByAndDistinct(ASTPtr & query, const Context & context)
{
DuplicateOrderByVisitor::Data order_by_data{context, false};
DuplicateOrderByVisitor(order_by_data).visit(query);
DuplicateDistinctVisitor::Data distinct_data{};
DuplicateDistinctVisitor(distinct_data).visit(query);
}
}
/// Remove duplicate items from LIMIT BY.
void optimizeLimitBy(const ASTSelectQuery * select_query)
@ -574,15 +565,12 @@ void optimizeArithmeticOperationsInAgr(ASTPtr & query, bool optimize_arithmetic_
}
}
void optimizeAnyInput(ASTPtr & query, bool optimize_any_input)
{
if (optimize_any_input)
void optimizeAnyInput(ASTPtr & query)
{
/// Removing arithmetic operations from functions
AnyInputVisitor::Data data = {};
AnyInputVisitor(data).visit(query);
}
}
void getArrayJoinedColumns(ASTPtr & query, SyntaxAnalyzerResult & result, const ASTSelectQuery * select_query,
const NamesAndTypesList & source_columns, const NameSet & source_columns_set)
@ -973,19 +961,23 @@ SyntaxAnalyzerResultPtr SyntaxAnalyzer::analyzeSelect(
optimizeGroupBy(select_query, source_columns_set, context);
/// GROUP BY functions of other keys elimination.
optimizeGroupByFunctionKeys(select_query, settings.optimize_group_by_function_keys);
if (settings.optimize_group_by_function_keys)
optimizeGroupByFunctionKeys(select_query);
///Move all operations out of any function
optimizeAnyInput(query, settings.optimize_any_input);
if (settings.optimize_move_functions_out_of_any)
optimizeAnyInput(query);
/// Eliminate min/max/any aggregators of functions of GROUP BY keys
optimizeAggregateFunctionsOfGroupByKeys(select_query, settings.optimize_aggregators_of_group_by_keys);
if (settings.optimize_aggregators_of_group_by_keys)
optimizeAggregateFunctionsOfGroupByKeys(select_query);
/// Remove duplicate items from ORDER BY.
optimizeOrderBy(select_query);
/// Remove duplicate ORDER BY and DISTINCT from subqueries.
optimizeDuplicateOrderByAndDistinct(query, settings.optimize_duplicate_order_by_and_distinct, context);
if (settings.optimize_duplicate_order_by_and_distinct)
optimizeDuplicateOrderByAndDistinct(query, context);
/// Remove duplicated elements from LIMIT BY clause.
optimizeLimitBy(select_query);

View File

@ -1,6 +1,6 @@
set optimize_aggregators_of_group_by_keys = 1;
set enable_debug_queries = 1;
set optimize_any_input = 0;
set optimize_move_functions_out_of_any = 0;
SELECT min(number % 2) AS a, max(number % 3) AS b FROM numbers(10000000) GROUP BY number % 2, number % 3 ORDER BY a, b;
SELECT any(number % 2) AS a, anyLast(number % 3) AS b FROM numbers(10000000) GROUP BY number % 2, number % 3 ORDER BY a, b;

View File

@ -1,4 +1,4 @@
SET optimize_any_input=1;
SET optimize_move_functions_out_of_any=1;
SET enable_debug_queries=1;
SELECT any(number + number * 2) FROM numbers(3, 10);
ANALYZE SELECT any(number + number * 2) FROM numbers(3, 10);

View File

@ -0,0 +1 @@
Settings description should start with capital letter

View File

@ -0,0 +1,2 @@
SELECT 'Settings description should start with capital letter';
SELECT name, description FROM system.settings WHERE substring(description, 1, 1) != upper(substring(description, 1, 1));