Skip to content

Commit fdf46ee

Browse files
authored
Merge pull request ClickHouse#100110 from hanfei1991/hanfei/skip-estimator
Skip estimator if it is not a CNF
2 parents 8fd15c5 + caa5f03 commit fdf46ee

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

src/Interpreters/InterpreterSelectQuery.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -841,9 +841,13 @@ InterpreterSelectQuery::InterpreterSelectQuery(
841841
parts_for_estimator = *parts;
842842
}
843843

844-
/// Just attempting to read statistics files on disk can increase query latencies
845-
/// First check the in-memory metadata if statistics are present at all
846-
auto estimator = storage_snapshot->metadata->hasStatistics()
844+
/// Just attempting to read statistics files on disk can increase query latencies.
845+
/// First check the in-memory metadata if statistics are present at all.
846+
/// Also, statistics are only used to reorder conditions, so skip if there is just one.
847+
const auto * where_function = query.where()->as<ASTFunction>();
848+
const bool has_multiple_conditions = where_function && where_function->name == "and";
849+
const bool has_statistics = storage_snapshot->metadata->hasStatistics();
850+
auto estimator = (has_statistics && has_multiple_conditions)
847851
? storage->getConditionSelectivityEstimator(parts_for_estimator, queried_columns, context)
848852
: nullptr;
849853

src/Processors/QueryPlan/Optimizations/optimizePrewhere.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,15 @@ void optimizePrewhere(QueryPlan::Node & parent_node, const bool remove_unused_co
181181

182182
const auto & queried_columns = source_step_with_filter->requiredSourceColumns();
183183

184+
/// Statistics are only used to reorder conditions, so skip if there is just one.
185+
const auto & filter_root_node = filter_step->getExpression().findInOutputs(filter_step->getFilterColumnName());
186+
const bool has_multiple_conditions = filter_root_node.type == ActionsDAG::ActionType::FUNCTION
187+
&& filter_root_node.function_base && filter_root_node.function_base->getName() == "and";
188+
184189
MergeTreeWhereOptimizer where_optimizer{
185190
std::move(column_compressed_sizes),
186191
storage_snapshot,
187-
read_from_merge_tree_step ? read_from_merge_tree_step->getConditionSelectivityEstimator(queried_columns) : nullptr,
192+
(has_multiple_conditions && read_from_merge_tree_step) ? read_from_merge_tree_step->getConditionSelectivityEstimator(queried_columns) : nullptr,
188193
queried_columns,
189194
storage.supportedPrewhereColumns(),
190195
getLogger("QueryPlanOptimizePrewhere")};

tests/parallel_replicas_blacklist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@
310310
00443_optimize_final_vertical_merge
311311
01660_join_or_any
312312
01801_s3_cluster
313+
03707_statistics_cache
313314

314315
Produces different result:
315316
03658_joined_block_split_single_row_bytes
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
1
2-
1
2+
0
33
1

tests/queries/0_stateless/03707_statistics_cache.sql

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
-- Tags: no-fasttest
22

3-
SET allow_statistics = 1;
4-
SET use_statistics = 1;
5-
SET log_queries = 1;
6-
SET log_query_settings = 1;
7-
SET mutations_sync = 2;
8-
SET max_execution_time = 60;
93
SET optimize_move_to_prewhere = 1, query_plan_optimize_prewhere = 1;
4+
SET enable_analyzer = 1;
5+
SET use_statistics = 1;
6+
107
-- Statistics loading happens inside optimizePrewhere(), which requires
118
-- the Filter→ReadFromMergeTree pattern. query_plan_merge_expressions = 0
129
-- inserts an intermediate Expression step that breaks this pattern.
1310
SET query_plan_merge_expressions = 1;
1411

15-
-- test rely on local execution, - force parallel replicas to genearate local plan
16-
SET parallel_replicas_local_plan=1;
17-
1812
DROP TABLE IF EXISTS sc_core SYNC;
1913

2014
CREATE TABLE sc_core

0 commit comments

Comments
 (0)