From b01f54ade1af9627aa09628851950cd45a70b9a4 Mon Sep 17 00:00:00 2001 From: tai Date: Tue, 23 Jul 2019 14:18:44 +0800 Subject: [PATCH] ISSUE-5695: support push down predicate to final subquery --- dbms/src/Core/Settings.h | 3 +- .../PredicateExpressionsOptimizer.cpp | 5 +- .../PredicateExpressionsOptimizer.h | 4 ++ .../00974_final_predicate_push_down.reference | 50 +++++++++++++++++++ .../00974_final_predicate_push_down.sql | 17 +++++++ 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 dbms/tests/queries/0_stateless/00974_final_predicate_push_down.reference create mode 100644 dbms/tests/queries/0_stateless/00974_final_predicate_push_down.sql diff --git a/dbms/src/Core/Settings.h b/dbms/src/Core/Settings.h index 4df07df60f6..e55e58e9d28 100644 --- a/dbms/src/Core/Settings.h +++ b/dbms/src/Core/Settings.h @@ -336,7 +336,8 @@ struct Settings : public SettingsCollection \ /** Obsolete settings that do nothing but left for compatibility reasons. Remove each one after half a year of obsolescence. */ \ \ - M(SettingBool, allow_experimental_low_cardinality_type, true, "Obsolete setting, does nothing. Will be removed after 2019-08-13") + M(SettingBool, allow_experimental_low_cardinality_type, true, "Obsolete setting, does nothing. Will be removed after 2019-08-13") \ + M(SettingBool, allow_push_predicate_to_final_subquery, 1, "Allow push predicate to final subquery.") DECLARE_SETTINGS_COLLECTION(LIST_OF_SETTINGS) diff --git a/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp b/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp index b6e6545e475..5e0d11212dc 100644 --- a/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp +++ b/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp @@ -138,7 +138,10 @@ bool PredicateExpressionsOptimizer::allowPushDown( const std::vector & dependencies, OptimizeKind & optimize_kind) { - if (!subquery || subquery->final() || subquery->limitBy() || subquery->limitLength() || subquery->with()) + if (!subquery || + (!settings.allow_push_predicate_to_final_subquery && subquery->final()) || + subquery->limitBy() || subquery->limitLength() || + subquery->with()) return false; else { diff --git a/dbms/src/Interpreters/PredicateExpressionsOptimizer.h b/dbms/src/Interpreters/PredicateExpressionsOptimizer.h index b7e3cd430fc..89d2bfcfbf0 100644 --- a/dbms/src/Interpreters/PredicateExpressionsOptimizer.h +++ b/dbms/src/Interpreters/PredicateExpressionsOptimizer.h @@ -37,6 +37,9 @@ class PredicateExpressionsOptimizer const UInt64 max_expanded_ast_elements; const String count_distinct_implementation; + /// for final PredicatePushOptimizer + const bool allow_push_predicate_to_final_subquery; + /// for PredicateExpressionsOptimizer const bool enable_optimize_predicate_expression; const bool join_use_nulls; @@ -46,6 +49,7 @@ class PredicateExpressionsOptimizer : max_ast_depth(settings.max_ast_depth), max_expanded_ast_elements(settings.max_expanded_ast_elements), count_distinct_implementation(settings.count_distinct_implementation), + allow_push_predicate_to_final_subquery(settings.allow_push_predicate_to_final_subquery), enable_optimize_predicate_expression(settings.enable_optimize_predicate_expression), join_use_nulls(settings.join_use_nulls) {} diff --git a/dbms/tests/queries/0_stateless/00974_final_predicate_push_down.reference b/dbms/tests/queries/0_stateless/00974_final_predicate_push_down.reference new file mode 100644 index 00000000000..55b5763f76b --- /dev/null +++ b/dbms/tests/queries/0_stateless/00974_final_predicate_push_down.reference @@ -0,0 +1,50 @@ +{ + "meta": + [ + { + "name": "COUNT()", + "type": "UInt64" + } + ], + + "data": + [ + { + "COUNT()": "1" + } + ], + + "rows": 1, + + "statistics": + { + "elapsed": 0.000780393, + "rows_read": 4, + "bytes_read": 16 + } +} +{ + "meta": + [ + { + "name": "COUNT()", + "type": "UInt64" + } + ], + + "data": + [ + { + "COUNT()": "1" + } + ], + + "rows": 1, + + "statistics": + { + "elapsed": 0.000687364, + "rows_read": 2, + "bytes_read": 8 + } +} diff --git a/dbms/tests/queries/0_stateless/00974_final_predicate_push_down.sql b/dbms/tests/queries/0_stateless/00974_final_predicate_push_down.sql new file mode 100644 index 00000000000..76b39b09acb --- /dev/null +++ b/dbms/tests/queries/0_stateless/00974_final_predicate_push_down.sql @@ -0,0 +1,17 @@ +DROP TABLE IF EXISTS test_00974; + +CREATE TABLE test_00974 +( + date Date, + x Int32, + ver UInt64 +) +ENGINE = ReplacingMergeTree(date, x, 4096); + +INSERT INTO test_00974 VALUES ('2019-07-23', 1, 1), ('2019-07-23', 1, 2); +INSERT INTO test_00974 VALUES ('2019-07-23', 2, 1), ('2019-07-23', 2, 2); + +SELECT COUNT() FROM (SELECT * FROM test_00974 FINAL) where x = 1 SETTINGS allow_push_predicate_to_final_subquery = 0 FORMAT JSON ; +SELECT COUNT() FROM (SELECT * FROM test_00974 FINAL) where x = 1 SETTINGS allow_push_predicate_to_final_subquery = 1 FORMAT JSON ; + +DROP TABLE test_00974;