Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,45 @@ private Set<Expression> collectExpressionsToBePushedDown(List<NamedExpression> e
inputSlots.stream()
).distinct();
}

// for this sql:
// select
// SUBSTR(orderdate,1,10) AS dt,
// ROW_NUMBER() OVER(PARTITION BY orderdate ORDER BY orderid DESC) AS rn
// from lineorders
// having dt = '2025-01-01'
//
// we not push down the `dt` slot under LogicalWindow, but push down [orderdate, orderid]
// to the bottom projects, because if we push down `dt`, the plan tree will be:
//
// LogicalFilter(substr(dt#3, 1, 10) = '2025-01-01')
// |
// LogicalWindow(rowNumber(partition by orderdate#2, order by orderid#1))
// |
// LogicalProject(orderid#1, orderdate#2, substr(orderdate#1, 1, 10) as dt#3)
//
// and can not push down filter by `PushDownFilterThroughWindow`, causing inefficiency,
// because dt#3 in LogicalFilter not contains in the partition key in LogicalWindow: [orderdate#2].
//
// so we only push down orderdate in the LogicalFilter, not push down `dt`:
//
// LogicalFilter(substr(orderdate#2, 1, 10) = '2025-01-01')
// |
// LogicalWindow(rowNumber(partition by orderdate#2, order by orderid#1))
// |
// LogicalProject(orderid#1, orderdate#2)
//
// and then, `PushDownFilterThroughWindow` found the LogicalFilter's `orderdate#2` contains
// in the LogicalWindow's partition key: [orderdate#2], and can push down filter to:
//
// LogicalWindow(rowNumber(partition by orderdate#2, order by orderid#1))
// |
// LogicalProject(orderid#1, orderdate#2)
// |
// LogicalFilter(substr(orderdate#2, 1, 10) = '2025-01-01')
if (expression instanceof Alias) {
return expression.getInputSlots().stream();
}
return ImmutableList.of(expression).stream();
})
.collect(ImmutableSet.toImmutableSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.doris.nereids.rules.rewrite;

import org.apache.doris.nereids.rules.RuleType;
import org.apache.doris.nereids.trees.expressions.Alias;
import org.apache.doris.nereids.trees.expressions.EqualTo;
import org.apache.doris.nereids.trees.expressions.Expression;
Expand All @@ -35,14 +36,15 @@
import org.apache.doris.nereids.util.PlanChecker;
import org.apache.doris.nereids.util.PlanConstructor;
import org.apache.doris.qe.ConnectContext;
import org.apache.doris.utframe.TestWithFeService;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test;

import java.util.List;

class PushDownFilterThroughWindowTest implements MemoPatternMatchSupported {
class PushDownFilterThroughWindowTest extends TestWithFeService implements MemoPatternMatchSupported {
private final LogicalOlapScan scan = new LogicalOlapScan(StatementScopeIdGenerator.newRelationId(),
PlanConstructor.student,
ImmutableList.of(""));
Expand Down Expand Up @@ -89,4 +91,64 @@ void pushDownFilterThroughWindowTest() {
)
);
}

@Test
public void testPushDownFilter() throws Exception {
String db = "test";
createDatabase(db);
useDatabase(db);
createTable("CREATE TABLE lineorders (\n"
+ "orderdate varchar(100) NOT NULL,\n"
+ "orderid int NOT NULL,\n"
+ "country_id int NOT NULL,\n"
+ "vender_id int NOT NULL,\n"
+ "ordernum int NOT NULL,\n"
+ "ordemoney int NOT NULL\n"
+ ") ENGINE=OLAP\n"
+ "DUPLICATE KEY(orderdate, orderid, country_id)\n"
+ "COMMENT 'OLAP'\n"
+ "PARTITION BY LIST(orderdate)\n"
+ "(PARTITION p1992 VALUES IN (\"0-2020\"),\n"
+ "PARTITION p1993 VALUES IN (\"0-2021\"),\n"
+ "PARTITION p1994 VALUES IN (\"0-2022\"),\n"
+ "PARTITION p1995 VALUES IN (\"0-2023\"),\n"
+ "PARTITION p1996 VALUES IN (\"0-2024\"),\n"
+ "PARTITION p1997 VALUES IN (\"0-2025\"))\n"
+ "DISTRIBUTED BY HASH(orderid) BUCKETS 48\n"
+ "PROPERTIES (\n"
+ "\"replication_allocation\" = \"tag.location.default: 1\"\n"
+ ")");

connectContext.getSessionVariable()
.setDisableNereidsRules(
RuleType.OLAP_SCAN_PARTITION_PRUNE.name() + "," + RuleType.PRUNE_EMPTY_PARTITION.name());

PlanChecker.from(connectContext)
.analyze("select * from ( \n"
+ " select \n"
+ " orderid,\n"
+ " orderdate,\n"
+ " country_id,\n"
+ " ordernum,\n"
+ " ordemoney,\n"
+ " SUBSTR(lineorders.orderdate,3,4) AS dt,\n"
+ " ROW_NUMBER() OVER(PARTITION BY lineorders.orderid,lineorders.orderdate ORDER BY lineorders.country_id DESC) AS rn\n"
+ " from lineorders\n"
+ ") a \n"
+ "where SUBSTR(a.dt, 1, 4) = SUBSTR(curdate(), 1, 4)")
.rewrite()
.matchesFromRoot(
logicalResultSink(
logicalProject(
logicalWindow(
logicalProject(
logicalFilter(
logicalOlapScan()
)
)
)
)
)
);
}
}
31 changes: 15 additions & 16 deletions regression-test/data/nereids_hint_tpcds_p0/shape/query51.out
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ PhysicalResultSink
--PhysicalTopN[MERGE_SORT]
----PhysicalDistribute[DistributionSpecGather]
------PhysicalTopN[LOCAL_SORT]
--------filter((web_cumulative > store_cumulative))
----------PhysicalWindow
------------PhysicalQuickSort[LOCAL_SORT]
--------------PhysicalDistribute[DistributionSpecHash]
----------------PhysicalProject
------------------hashJoin[FULL_OUTER_JOIN colocated] hashCondition=((web.d_date = store.d_date) and (web.item_sk = store.item_sk)) otherCondition=()
--------------------PhysicalProject
----------------------PhysicalWindow
------------------------PhysicalQuickSort[LOCAL_SORT]
--------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------PhysicalProject
--------PhysicalProject
----------filter((web_cumulative > store_cumulative))
------------PhysicalWindow
--------------PhysicalQuickSort[LOCAL_SORT]
----------------PhysicalDistribute[DistributionSpecHash]
------------------PhysicalProject
--------------------hashJoin[FULL_OUTER_JOIN colocated] hashCondition=((web.d_date = store.d_date) and (web.item_sk = store.item_sk)) otherCondition=()
----------------------PhysicalProject
------------------------PhysicalWindow
--------------------------PhysicalQuickSort[LOCAL_SORT]
----------------------------PhysicalDistribute[DistributionSpecHash]
------------------------------hashAgg[GLOBAL]
--------------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------------hashAgg[LOCAL]
Expand All @@ -25,11 +25,10 @@ PhysicalResultSink
----------------------------------------PhysicalProject
------------------------------------------filter((date_dim.d_month_seq <= 1223) and (date_dim.d_month_seq >= 1212))
--------------------------------------------PhysicalOlapScan[date_dim]
--------------------PhysicalProject
----------------------PhysicalWindow
------------------------PhysicalQuickSort[LOCAL_SORT]
--------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------PhysicalProject
----------------------PhysicalProject
------------------------PhysicalWindow
--------------------------PhysicalQuickSort[LOCAL_SORT]
----------------------------PhysicalDistribute[DistributionSpecHash]
------------------------------hashAgg[GLOBAL]
--------------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------------hashAgg[LOCAL]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ PhysicalCteAnchor ( cteId=CTEId#0 )
-- !cte_filter_pushdown_3 --
PhysicalResultSink
--hashJoin[INNER_JOIN] hashCondition=((k3 = dd.k3)) otherCondition=()
----filter((tmp2.k3 = 0))
----filter((tmp.k3 = 0))
------PhysicalWindow
--------PhysicalQuickSort[LOCAL_SORT]
----------filter((tmp.k1 = 1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ PhysicalCteAnchor ( cteId=CTEId#0 )
-- !cte_filter_pushdown_3 --
PhysicalResultSink
--hashJoin[INNER_JOIN] hashCondition=((k3 = dd.k3)) otherCondition=()
----filter((tmp2.k3 = 0))
----filter((tmp.k3 = 0))
------PhysicalWindow
--------PhysicalQuickSort[LOCAL_SORT]
----------filter((tmp.k1 = 1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ PhysicalResultSink
--PhysicalTopN[MERGE_SORT]
----PhysicalDistribute[DistributionSpecGather]
------PhysicalTopN[LOCAL_SORT]
--------filter((web_cumulative > store_cumulative))
----------PhysicalWindow
------------PhysicalQuickSort[LOCAL_SORT]
--------------PhysicalDistribute[DistributionSpecHash]
----------------PhysicalProject
------------------hashJoin[FULL_OUTER_JOIN colocated] hashCondition=((web.d_date = store.d_date) and (web.item_sk = store.item_sk)) otherCondition=()
--------------------PhysicalProject
----------------------PhysicalWindow
------------------------PhysicalQuickSort[LOCAL_SORT]
--------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------PhysicalProject
--------PhysicalProject
----------filter((web_cumulative > store_cumulative))
------------PhysicalWindow
--------------PhysicalQuickSort[LOCAL_SORT]
----------------PhysicalDistribute[DistributionSpecHash]
------------------PhysicalProject
--------------------hashJoin[FULL_OUTER_JOIN colocated] hashCondition=((web.d_date = store.d_date) and (web.item_sk = store.item_sk)) otherCondition=()
----------------------PhysicalProject
------------------------PhysicalWindow
--------------------------PhysicalQuickSort[LOCAL_SORT]
----------------------------PhysicalDistribute[DistributionSpecHash]
------------------------------hashAgg[GLOBAL]
--------------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------------hashAgg[LOCAL]
Expand All @@ -25,11 +25,10 @@ PhysicalResultSink
----------------------------------------PhysicalProject
------------------------------------------filter((date_dim.d_month_seq <= 1223) and (date_dim.d_month_seq >= 1212))
--------------------------------------------PhysicalOlapScan[date_dim]
--------------------PhysicalProject
----------------------PhysicalWindow
------------------------PhysicalQuickSort[LOCAL_SORT]
--------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------PhysicalProject
----------------------PhysicalProject
------------------------PhysicalWindow
--------------------------PhysicalQuickSort[LOCAL_SORT]
----------------------------PhysicalDistribute[DistributionSpecHash]
------------------------------hashAgg[GLOBAL]
--------------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------------hashAgg[LOCAL]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ PhysicalResultSink
--PhysicalTopN[MERGE_SORT]
----PhysicalDistribute[DistributionSpecGather]
------PhysicalTopN[LOCAL_SORT]
--------filter((web_cumulative > store_cumulative))
----------PhysicalWindow
------------PhysicalQuickSort[LOCAL_SORT]
--------------PhysicalDistribute[DistributionSpecHash]
----------------PhysicalProject
------------------hashJoin[FULL_OUTER_JOIN colocated] hashCondition=((web.d_date = store.d_date) and (web.item_sk = store.item_sk)) otherCondition=()
--------------------PhysicalProject
----------------------PhysicalWindow
------------------------PhysicalQuickSort[LOCAL_SORT]
--------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------PhysicalProject
--------PhysicalProject
----------filter((web_cumulative > store_cumulative))
------------PhysicalWindow
--------------PhysicalQuickSort[LOCAL_SORT]
----------------PhysicalDistribute[DistributionSpecHash]
------------------PhysicalProject
--------------------hashJoin[FULL_OUTER_JOIN colocated] hashCondition=((web.d_date = store.d_date) and (web.item_sk = store.item_sk)) otherCondition=()
----------------------PhysicalProject
------------------------PhysicalWindow
--------------------------PhysicalQuickSort[LOCAL_SORT]
----------------------------PhysicalDistribute[DistributionSpecHash]
------------------------------hashAgg[GLOBAL]
--------------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------------hashAgg[LOCAL]
Expand All @@ -25,11 +25,10 @@ PhysicalResultSink
----------------------------------------PhysicalProject
------------------------------------------filter((date_dim.d_month_seq <= 1227) and (date_dim.d_month_seq >= 1216))
--------------------------------------------PhysicalOlapScan[date_dim]
--------------------PhysicalProject
----------------------PhysicalWindow
------------------------PhysicalQuickSort[LOCAL_SORT]
--------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------PhysicalProject
----------------------PhysicalProject
------------------------PhysicalWindow
--------------------------PhysicalQuickSort[LOCAL_SORT]
----------------------------PhysicalDistribute[DistributionSpecHash]
------------------------------hashAgg[GLOBAL]
--------------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------------hashAgg[LOCAL]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ PhysicalResultSink
--PhysicalTopN[MERGE_SORT]
----PhysicalDistribute[DistributionSpecGather]
------PhysicalTopN[LOCAL_SORT]
--------filter((web_cumulative > store_cumulative))
----------PhysicalWindow
------------PhysicalQuickSort[LOCAL_SORT]
--------------PhysicalDistribute[DistributionSpecHash]
----------------PhysicalProject
------------------hashJoin[FULL_OUTER_JOIN colocated] hashCondition=((web.d_date = store.d_date) and (web.item_sk = store.item_sk)) otherCondition=()
--------------------PhysicalProject
----------------------PhysicalWindow
------------------------PhysicalQuickSort[LOCAL_SORT]
--------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------PhysicalProject
--------PhysicalProject
----------filter((web_cumulative > store_cumulative))
------------PhysicalWindow
--------------PhysicalQuickSort[LOCAL_SORT]
----------------PhysicalDistribute[DistributionSpecHash]
------------------PhysicalProject
--------------------hashJoin[FULL_OUTER_JOIN colocated] hashCondition=((web.d_date = store.d_date) and (web.item_sk = store.item_sk)) otherCondition=()
----------------------PhysicalProject
------------------------PhysicalWindow
--------------------------PhysicalQuickSort[LOCAL_SORT]
----------------------------PhysicalDistribute[DistributionSpecHash]
------------------------------hashAgg[GLOBAL]
--------------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------------hashAgg[LOCAL]
Expand All @@ -25,11 +25,10 @@ PhysicalResultSink
----------------------------------------PhysicalProject
------------------------------------------filter((date_dim.d_month_seq <= 1227) and (date_dim.d_month_seq >= 1216))
--------------------------------------------PhysicalOlapScan[date_dim]
--------------------PhysicalProject
----------------------PhysicalWindow
------------------------PhysicalQuickSort[LOCAL_SORT]
--------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------PhysicalProject
----------------------PhysicalProject
------------------------PhysicalWindow
--------------------------PhysicalQuickSort[LOCAL_SORT]
----------------------------PhysicalDistribute[DistributionSpecHash]
------------------------------hashAgg[GLOBAL]
--------------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------------hashAgg[LOCAL]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ PhysicalResultSink
--PhysicalTopN[MERGE_SORT]
----PhysicalDistribute[DistributionSpecGather]
------PhysicalTopN[LOCAL_SORT]
--------filter((web_cumulative > store_cumulative))
----------PhysicalWindow
------------PhysicalQuickSort[LOCAL_SORT]
--------------PhysicalDistribute[DistributionSpecHash]
----------------PhysicalProject
------------------hashJoin[FULL_OUTER_JOIN colocated] hashCondition=((web.d_date = store.d_date) and (web.item_sk = store.item_sk)) otherCondition=()
--------------------PhysicalProject
----------------------PhysicalWindow
------------------------PhysicalQuickSort[LOCAL_SORT]
--------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------PhysicalProject
--------PhysicalProject
----------filter((web_cumulative > store_cumulative))
------------PhysicalWindow
--------------PhysicalQuickSort[LOCAL_SORT]
----------------PhysicalDistribute[DistributionSpecHash]
------------------PhysicalProject
--------------------hashJoin[FULL_OUTER_JOIN colocated] hashCondition=((web.d_date = store.d_date) and (web.item_sk = store.item_sk)) otherCondition=()
----------------------PhysicalProject
------------------------PhysicalWindow
--------------------------PhysicalQuickSort[LOCAL_SORT]
----------------------------PhysicalDistribute[DistributionSpecHash]
------------------------------hashAgg[GLOBAL]
--------------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------------hashAgg[LOCAL]
Expand All @@ -25,11 +25,10 @@ PhysicalResultSink
----------------------------------------PhysicalProject
------------------------------------------filter((date_dim.d_month_seq <= 1227) and (date_dim.d_month_seq >= 1216))
--------------------------------------------PhysicalOlapScan[date_dim]
--------------------PhysicalProject
----------------------PhysicalWindow
------------------------PhysicalQuickSort[LOCAL_SORT]
--------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------PhysicalProject
----------------------PhysicalProject
------------------------PhysicalWindow
--------------------------PhysicalQuickSort[LOCAL_SORT]
----------------------------PhysicalDistribute[DistributionSpecHash]
------------------------------hashAgg[GLOBAL]
--------------------------------PhysicalDistribute[DistributionSpecHash]
----------------------------------hashAgg[LOCAL]
Expand Down
Loading
Loading