diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/TopnFilterPushDownVisitor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/TopnFilterPushDownVisitor.java index a6ec5a70353c96..2d8989d9147c9d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/TopnFilterPushDownVisitor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/TopnFilterPushDownVisitor.java @@ -218,8 +218,12 @@ public Boolean visitPhysicalNestedLoopJoin(PhysicalNestedLoopJoin ctx.topn.getLimit() + ctx.topn.getOffset()) { + topnFilterContext.addTopnFilter(ctx.topn, relation, ctx.probeExpr); + return true; + } } return false; } diff --git a/regression-test/suites/nereids_tpch_p0/tpch/topn_filter.groovy b/regression-test/suites/nereids_tpch_p0/tpch/topn_filter.groovy new file mode 100644 index 00000000000000..cb752332cd51fc --- /dev/null +++ b/regression-test/suites/nereids_tpch_p0/tpch/topn_filter.groovy @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +suite("topn_filter") { + String db = context.config.getDbNameByFile(new File(context.file.parent)) + sql "use ${db}" + sql "set topn_opt_limit_threshold=1024" + sql "analyze table nation with sync" + // limit + offset > table.rows, do not generate topn filter + explain{ + sql "select * from nation order by n_nationkey limit 40;" + notContains ("TOPN OPT:") + } + explain{ + sql "select * from nation order by n_nationkey limit 4 offset 21;" + notContains ("TOPN OPT:") + } + + explain{ + sql "select * from nation order by n_nationkey limit 4 offset 20;" + contains ("TOPN OPT:1") + } + +} \ No newline at end of file diff --git a/regression-test/suites/point_query_p0/test_rowstore.groovy b/regression-test/suites/point_query_p0/test_rowstore.groovy index 5ab2f3e47e270e..8d7a28415c237c 100644 --- a/regression-test/suites/point_query_p0/test_rowstore.groovy +++ b/regression-test/suites/point_query_p0/test_rowstore.groovy @@ -283,11 +283,10 @@ suite("test_rowstore", "p0,nonConcurrent") { """ sql "select /*+ SET_VAR(enable_nereids_planner=true)*/ * from table_with_column_group where k1 = 1" - def tableName = "rs_query" - sql """DROP TABLE IF EXISTS ${tableName}""" + sql """DROP TABLE IF EXISTS rs_query""" sql "set enable_decimal256 = true" sql """ - CREATE TABLE IF NOT EXISTS ${tableName} ( + CREATE TABLE IF NOT EXISTS rs_query ( `k1` int(11) NULL COMMENT "", `v1` text NULL COMMENT "", `v2` DECIMAL(50, 18) NULL COMMENT "" @@ -303,26 +302,29 @@ suite("test_rowstore", "p0,nonConcurrent") { ) """ - sql """insert into ${tableName} values (1, 'abc', 1111919.12345678919)""" + sql """ + insert into rs_query values (1, 'abc', 1111919.12345678919), (2, 'abc', 1111919.12345678919); + analyze table rs_query with sync; + """ explain { - sql("select * from ${tableName} order by k1 limit 1") + sql("select * from rs_query order by k1 limit 1") contains "TOPN OPT" } - qt_sql """select * from ${tableName} order by k1 limit 1""" + qt_sql """select * from rs_query order by k1 limit 1""" sql """ - ALTER table ${tableName} ADD COLUMN new_column1 INT default "123"; + ALTER table rs_query ADD COLUMN new_column1 INT default "123"; """ - qt_sql """select * from ${tableName} where k1 = 1""" + qt_sql """select * from rs_query where k1 = 1""" sql """ - ALTER table ${tableName} ADD COLUMN new_column2 DATETIMEV2(3) DEFAULT "1970-01-01 00:00:00.111"; + ALTER table rs_query ADD COLUMN new_column2 DATETIMEV2(3) DEFAULT "1970-01-01 00:00:00.111"; """ sleep(1000) - qt_sql """select * from ${tableName} where k1 = 1""" + qt_sql """select * from rs_query where k1 = 1""" - sql """insert into ${tableName} values (2, 'def', 1111919.12345678919, 456, NULL)""" - qt_sql """select * from ${tableName} where k1 = 2""" + sql """insert into rs_query values (2, 'def', 1111919.12345678919, 456, NULL)""" + qt_sql """select * from rs_query where k1 = 2""" sql "set global enable_short_circuit_query_access_column_store = false" test { diff --git a/regression-test/suites/query_p0/limit/test_map_select_with_limit.groovy b/regression-test/suites/query_p0/limit/test_map_select_with_limit.groovy index 5a37b7b9bc1888..090706960556b9 100644 --- a/regression-test/suites/query_p0/limit/test_map_select_with_limit.groovy +++ b/regression-test/suites/query_p0/limit/test_map_select_with_limit.groovy @@ -16,11 +16,8 @@ // under the License. suite("test_map_select_with_limit", "query") { - // define a sql table - def testTable = "test_map_select_with_limit" - sql """ - CREATE TABLE IF NOT EXISTS ${testTable} ( + CREATE TABLE IF NOT EXISTS test_map_select_with_limit ( `k1` INT(11) NULL, `k2` MAP NULL ) ENGINE=OLAP @@ -35,15 +32,18 @@ suite("test_map_select_with_limit", "query") { ) """ // prepare data - sql """ INSERT INTO ${testTable} VALUES (100, {1: "amory", 2: "is", 3: "better"}) """ + sql """ + INSERT INTO test_map_select_with_limit VALUES (100, {1: "amory", 2: "is", 3: "better"}), (101, {1: "amory", 2: "is", 3: "better"}); + analyze table test_map_select_with_limit with sync; + """ // set topn_opt_limit_threshold = 1024 to make sure _internal_service to be request with proto request sql """ set topn_opt_limit_threshold = 1024 """ explain{ - sql("select * from ${testTable} order by k1 limit 1") + sql("select * from test_map_select_with_limit order by k1 limit 1") contains "TOPN" } - qt_select """ select * from ${testTable} order by k1 limit 1 """ -} + qt_select """ select * from test_map_select_with_limit order by k1 limit 1 """ +} \ No newline at end of file diff --git a/regression-test/suites/query_p0/test_array_orderby_limit.groovy b/regression-test/suites/query_p0/test_array_orderby_limit.groovy index 2fa3bb9c5ed838..a1de7219f9a16b 100644 --- a/regression-test/suites/query_p0/test_array_orderby_limit.groovy +++ b/regression-test/suites/query_p0/test_array_orderby_limit.groovy @@ -20,7 +20,7 @@ suite("test_array_char_orderby", "query") { def testTable = "test_array_char_orderby" sql """ - CREATE TABLE IF NOT EXISTS ${testTable} ( + CREATE TABLE IF NOT EXISTS test_array_char_orderby ( `k1` INT(11) NULL, `k2` array> NULL ) ENGINE=OLAP @@ -35,16 +35,17 @@ suite("test_array_char_orderby", "query") { ) """ // prepare data - sql """ INSERT INTO ${testTable} VALUES (100, [['abc']]) """ + sql """ INSERT INTO test_array_char_orderby VALUES (100, [['abc']]), (200, [['xyz']]) """ + sql "analyze table test_array_char_orderby with sync" // set topn_opt_limit_threshold = 1024 to make sure _internal_service to be request with proto request sql """ set topn_opt_limit_threshold = 1024 """ explain{ - sql("select * from ${testTable} order by k1 limit 1") + sql("select * from test_array_char_orderby order by k1 limit 1") contains "TOPN" } - qt_select """ select * from ${testTable} order by k1 limit 1 """ + qt_select """ select * from test_array_char_orderby order by k1 limit 1 """ sql "DROP TABLE IF EXISTS unpart_tbl_parquet_struct_3;" sql """