zhuqi-lucas commented on code in PR #23824:
URL: https://github.com/apache/datafusion/pull/23824#discussion_r3733128825


##########
datafusion/sqllogictest/test_files/row_number_to_aggregate.slt:
##########
@@ -0,0 +1,111 @@
+# 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.
+
+# Tests for the `optimizer.enable_row_number_to_aggregate` rule
+# (ReplaceFilterTop1): a "top-1 per group" ROW_NUMBER() filter is rewritten
+# into a first_value aggregate.
+
+statement ok
+CREATE TABLE t (p INT, o INT, payload VARCHAR) AS VALUES
+  (1, 10, 'a'),
+  (1, 20, 'b'),
+  (2, 5,  'c'),
+  (2, 30, 'd');
+
+# ---------------------------------------------------------------------------
+# Rule disabled (the default): the plan keeps the Window + Filter shape.
+# ---------------------------------------------------------------------------
+
+statement ok
+set datafusion.optimizer.enable_row_number_to_aggregate = false;
+
+query TT
+EXPLAIN SELECT * FROM (
+  SELECT *, ROW_NUMBER() OVER (PARTITION BY p ORDER BY o DESC) AS rn
+  FROM t
+) WHERE rn = 1;
+----
+logical_plan
+01)Projection: t.p, t.o, t.payload, row_number() PARTITION BY [t.p] ORDER BY 
[t.o DESC NULLS FIRST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS rn
+02)--Filter: row_number() PARTITION BY [t.p] ORDER BY [t.o DESC NULLS FIRST] 
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW = UInt64(1)
+03)----WindowAggr: windowExpr=[[row_number() PARTITION BY [t.p] ORDER BY [t.o 
DESC NULLS FIRST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+04)------TableScan: t projection=[p, o, payload]
+physical_plan
+01)ProjectionExec: expr=[p@0 as p, o@1 as o, payload@2 as payload, 
row_number() PARTITION BY [t.p] ORDER BY [t.o DESC NULLS FIRST] RANGE BETWEEN 
UNBOUNDED PRECEDING AND CURRENT ROW@3 as rn]
+02)--FilterExec: row_number() PARTITION BY [t.p] ORDER BY [t.o DESC NULLS 
FIRST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@3 = 1
+03)----BoundedWindowAggExec: wdw=[row_number() PARTITION BY [t.p] ORDER BY 
[t.o DESC NULLS FIRST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field 
{ "row_number() PARTITION BY [t.p] ORDER BY [t.o DESC NULLS FIRST] RANGE 
BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": UInt64 }, frame: RANGE BETWEEN 
UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+04)------SortExec: expr=[p@0 ASC NULLS LAST, o@1 DESC], 
preserve_partitioning=[false]
+05)--------DataSourceExec: partitions=1, partition_sizes=[1]
+
+# ---------------------------------------------------------------------------
+# Rule enabled: the Filter(row_number() = 1) collapses into an Aggregate.
+# ---------------------------------------------------------------------------
+
+statement ok
+set datafusion.optimizer.enable_row_number_to_aggregate = true;
+
+query TT
+EXPLAIN SELECT * FROM (
+  SELECT *, ROW_NUMBER() OVER (PARTITION BY p ORDER BY o DESC) AS rn
+  FROM t
+) WHERE rn = 1;

Review Comment:
   What about more slt test cases, for example which is not rn = 1, we will not 
affect those cases?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to