Skip to content

Commit

Permalink
[opt](nereids) when table.rows > topn.limit+topn.offset, do not gener…
Browse files Browse the repository at this point in the history
…ate topn-filter (#40480)

## Proposed changes

Issue Number: close #xxx

<!--Describe your changes.-->
  • Loading branch information
englefly authored Sep 11, 2024
1 parent 73bf91a commit 042bfce
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,12 @@ public Boolean visitPhysicalNestedLoopJoin(PhysicalNestedLoopJoin<? extends Plan
public Boolean visitPhysicalRelation(PhysicalRelation relation, PushDownContext ctx) {
if (supportPhysicalRelations(relation)
&& relation.getOutputSet().containsAll(ctx.probeExpr.getInputSlots())) {
topnFilterContext.addTopnFilter(ctx.topn, relation, ctx.probeExpr);
return true;
// in ut, relation.getStats() may return null
if (relation.getStats() == null
|| relation.getStats().getRowCount() > ctx.topn.getLimit() + ctx.topn.getOffset()) {
topnFilterContext.addTopnFilter(ctx.topn, relation, ctx.probeExpr);
return true;
}
}
return false;
}
Expand Down
40 changes: 40 additions & 0 deletions regression-test/suites/nereids_tpch_p0/tpch/topn_filter.groovy
Original file line number Diff line number Diff line change
@@ -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")
}

}
26 changes: 14 additions & 12 deletions regression-test/suites/point_query_p0/test_rowstore.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SMALLINT(6), STRING> NULL
) ENGINE=OLAP
Expand All @@ -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 """
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<array<char(50)>> NULL
) ENGINE=OLAP
Expand All @@ -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 """
Expand Down

0 comments on commit 042bfce

Please sign in to comment.