Changeset: 658d416e8fc9 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/658d416e8fc9
Added Files:
        sql/test/BugTracker-2026/Tests/8108-rownumber-topn-equality.test
Modified Files:
        sql/server/rel_optimize_sel.c
Branch: default
Log Message:

optimize not only select row_number <(=) n but also row_number() = n, solving 
issue #8108.


diffs (87 lines):

diff --git a/sql/server/rel_optimize_sel.c b/sql/server/rel_optimize_sel.c
--- a/sql/server/rel_optimize_sel.c
+++ b/sql/server/rel_optimize_sel.c
@@ -3517,7 +3517,7 @@ rel_push_select_down(visitor *v, sql_rel
                                for (n = exps->h; n; n = n->next) {
                                        sql_exp *e = n->data;
 
-                                       if (e->type == e_cmp && (e->flag == 
cmp_lt || e->flag == cmp_lte) && exp_is_atom(e->r) && !e->f) { /* simple limit 
*/
+                                       if (e->type == e_cmp && (e->flag == 
cmp_lt || e->flag == cmp_lte || e->flag == cmp_equal) && exp_is_atom(e->r) && 
!e->f) { /* simple limit */
                                                sql_exp *ranke = 
rel_find_exp(r, e->l);
 
                                                if (ranke && ranke->type == 
e_func) {
diff --git a/sql/test/BugTracker-2026/Tests/8108-rownumber-topn-equality.test 
b/sql/test/BugTracker-2026/Tests/8108-rownumber-topn-equality.test
new file mode 100644
--- /dev/null
+++ b/sql/test/BugTracker-2026/Tests/8108-rownumber-topn-equality.test
@@ -0,0 +1,70 @@
+statement ok
+CREATE TABLE t (
+    partition_key INTEGER NOT NULL,
+    value_col BIGINT NOT NULL
+)
+
+statement ok rowcount 20000000
+INSERT INTO t
+SELECT CAST(value % 10 AS INTEGER),
+       CAST((value * 1103515245 + 12345) % 2147483647 AS BIGINT)
+FROM sys.generate_series(1, 20000001)
+
+query T
+EXPLAIN
+SELECT COUNT(*) AS selected_rows, SUM(value_col) AS value_sum
+FROM (
+    SELECT partition_key, value_col,
+           ROW_NUMBER() OVER (
+               PARTITION BY partition_key ORDER BY value_col
+           ) AS rn
+    FROM t
+) AS ranked
+WHERE rn = 1
+----
+project (
+| group by (
+| | project (
+| | | select (
+| | | | project (
+| | | | | grouped top N (
+| | | | | | project (
+| | | | | | | table("sys"."t") [ "t"."partition_key", "t"."value_col" ]
+| | | | | | ) [ "t"."partition_key", "t"."value_col" ] [ "t"."partition_key", 
"t"."value_col" ]
+| | | | | ) [ int(31) "1" ]
+| | | | ) [ "t"."value_col", "sys"."row_number"(varchar["sys"."star"()], 
"sys"."diff"("t"."partition_key"), "sys"."diff"("t"."value_col")) as 
"ranked"."rn" ]
+| | | ) [ ("ranked"."rn") = (int(31) "1") ]
+| | ) [ "t"."value_col" as "ranked"."value_col" ]
+| ) [  ] [ "sys"."count"() as "%1"."%1", "sys"."sum" no nil 
("ranked"."value_col") as "%2"."%2" ]
+) [ "%1"."%1" as "selected_rows", "%2"."%2" as "value_sum" ]
+
+query T
+EXPLAIN
+SELECT COUNT(*) AS selected_rows, SUM(value_col) AS value_sum
+FROM (
+    SELECT partition_key, value_col,
+           ROW_NUMBER() OVER (
+               PARTITION BY partition_key ORDER BY value_col
+           ) AS rn
+    FROM t
+) AS ranked
+WHERE rn <= 1
+----
+project (
+| group by (
+| | project (
+| | | select (
+| | | | project (
+| | | | | grouped top N (
+| | | | | | project (
+| | | | | | | table("sys"."t") [ "t"."partition_key", "t"."value_col" ]
+| | | | | | ) [ "t"."partition_key", "t"."value_col" ] [ "t"."partition_key", 
"t"."value_col" ]
+| | | | | ) [ int(31) "1" ]
+| | | | ) [ "t"."value_col", "sys"."row_number"(varchar["sys"."star"()], 
"sys"."diff"("t"."partition_key"), "sys"."diff"("t"."value_col")) as 
"ranked"."rn" ]
+| | | ) [ ("ranked"."rn") <= (int(31) "1") ]
+| | ) [ "t"."value_col" as "ranked"."value_col" ]
+| ) [  ] [ "sys"."count"() as "%1"."%1", "sys"."sum" no nil 
("ranked"."value_col") as "%2"."%2" ]
+) [ "%1"."%1" as "selected_rows", "%2"."%2" as "value_sum" ]
+
+statement ok
+drop table t
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to