diff --git a/dbms/src/Storages/StorageMergeTree.cpp b/dbms/src/Storages/StorageMergeTree.cpp index 1650520200f..8598017da6d 100644 --- a/dbms/src/Storages/StorageMergeTree.cpp +++ b/dbms/src/Storages/StorageMergeTree.cpp @@ -130,9 +130,8 @@ BlockInputStreams StorageMergeTree::read( auto & select = typeid_cast(*query); /// Try transferring some condition from WHERE to PREWHERE if enabled and viable - if (settings.optimize_move_to_prewhere) - if (select.where_expression && !select.prewhere_expression) - MergeTreeWhereOptimizer{query, context, data, column_names, log}; + if (settings.optimize_move_to_prewhere && select.where_expression && !select.prewhere_expression && !select.final) + MergeTreeWhereOptimizer{query, context, data, column_names, log}; return reader.read(column_names, query, context, settings, processed_stage, max_block_size, threads, nullptr, 0); } diff --git a/dbms/src/Storages/StorageReplicatedMergeTree.cpp b/dbms/src/Storages/StorageReplicatedMergeTree.cpp index 2d4dc56f542..b9dcce59b08 100644 --- a/dbms/src/Storages/StorageReplicatedMergeTree.cpp +++ b/dbms/src/Storages/StorageReplicatedMergeTree.cpp @@ -2089,9 +2089,8 @@ BlockInputStreams StorageReplicatedMergeTree::read( auto & select = typeid_cast(*query); /// Try transferring some condition from WHERE to PREWHERE if enabled and viable - if (settings.optimize_move_to_prewhere) - if (select.where_expression && !select.prewhere_expression) - MergeTreeWhereOptimizer{query, context, data, real_column_names, log}; + if (settings.optimize_move_to_prewhere && select.where_expression && !select.prewhere_expression && !select.final) + MergeTreeWhereOptimizer{query, context, data, real_column_names, log}; Block virtual_columns_block; auto column = std::make_shared(2); diff --git a/dbms/tests/queries/0_stateless/00330_view_subqueries.reference b/dbms/tests/queries/0_stateless/00330_view_subqueries.reference new file mode 100644 index 00000000000..d7211690bb4 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00330_view_subqueries.reference @@ -0,0 +1,11 @@ +1 +0 +1 +4 +9 +16 +25 +36 +49 +64 +81 diff --git a/dbms/tests/queries/0_stateless/00330_view_subqueries.sql b/dbms/tests/queries/0_stateless/00330_view_subqueries.sql new file mode 100644 index 00000000000..9a6cca0a7e3 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00330_view_subqueries.sql @@ -0,0 +1,11 @@ +DROP TABLE IF EXISTS test.v1; +DROP TABLE IF EXISTS test.v2; + +CREATE VIEW test.v1 AS SELECT 1 FROM (SELECT 1); +SELECT * FROM test.v1; + +CREATE VIEW test.v2 AS SELECT number * number FROM (SELECT number FROM system.numbers LIMIT 10); +SELECT * FROM test.v2; + +DROP TABLE test.v1; +DROP TABLE test.v2; diff --git a/dbms/tests/queries/0_stateless/00331_final_and_prewhere.reference b/dbms/tests/queries/0_stateless/00331_final_and_prewhere.reference new file mode 100644 index 00000000000..b5ee2ec8465 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00331_final_and_prewhere.reference @@ -0,0 +1,5 @@ +2016-06-02 1 version 0 0 +2016-06-02 1 version 1 1 +2016-06-02 2 version 1 1 +2016-06-02 1 version 1 1 +2016-06-02 2 version 1 1 diff --git a/dbms/tests/queries/0_stateless/00331_final_and_prewhere.sql b/dbms/tests/queries/0_stateless/00331_final_and_prewhere.sql new file mode 100644 index 00000000000..5e1acb2d0e2 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00331_final_and_prewhere.sql @@ -0,0 +1,12 @@ +DROP TABLE IF EXISTS test.replace; + +CREATE TABLE test.replace ( EventDate Date, Id UInt64, Data String, Version UInt32) ENGINE = ReplacingMergeTree(EventDate, Id, 8192, Version); +INSERT INTO test.replace VALUES ('2016-06-02', 1, 'version 1', 1); +INSERT INTO test.replace VALUES ('2016-06-02', 2, 'version 1', 1); +INSERT INTO test.replace VALUES ('2016-06-02', 1, 'version 0', 0); + +SELECT * FROM test.replace ORDER BY Id, Version; +SELECT * FROM test.replace FINAL ORDER BY Id, Version; +SELECT * FROM test.replace FINAL WHERE Version = 0 ORDER BY Id, Version; + +DROP TABLE test.replace;