WITH TIES: number of rows is optional and defaults to one
authorAlvaro Herrera
Mon, 18 May 2020 23:28:46 +0000 (19:28 -0400)
committerAlvaro Herrera
Mon, 18 May 2020 23:28:46 +0000 (19:28 -0400)
FETCH FIRST .. ONLY implements this correctly, but we missed to include
it for FETCH FIRST .. WITH TIES in commit 357889eb17bb.

Author: Vik Fearing
Discussion: https://postgr.es/m/6aa690ef-551d-e24f-2690-c38c2442947c@postgresfriends.org

src/backend/parser/gram.y
src/test/regress/expected/limit.out
src/test/regress/sql/limit.sql

index 3c78f2d1b51f2f17c9b2c24f42ce4e1a492a4ab5..a24b30f06f1cdc1f2de2aaddfd733393b8e14aba 100644 (file)
@@ -11816,6 +11816,14 @@ limit_clause:
                    n->limitOption = LIMIT_OPTION_COUNT;
                    $$ = n;
                }
+           | FETCH first_or_next row_or_rows WITH TIES
+               {
+                   SelectLimit *n = (SelectLimit *) palloc(sizeof(SelectLimit));
+                   n->limitOffset = NULL;
+                   n->limitCount = makeIntConst(1, -1);
+                   n->limitOption = LIMIT_OPTION_WITH_TIES;
+                   $$ = n;
+               }
        ;
 
 offset_clause:
index a4e175855c39a6ca6f72a107b511967782d50044..e6f6809fbee8a0b031986c4db8c0d09fec6eef51 100644 (file)
@@ -576,6 +576,23 @@ SELECT  thousand
         0
 (10 rows)
 
+SELECT  thousand
+       FROM onek WHERE thousand < 5
+       ORDER BY thousand FETCH FIRST ROWS WITH TIES;
+ thousand 
+----------
+        0
+        0
+        0
+        0
+        0
+        0
+        0
+        0
+        0
+        0
+(10 rows)
+
 SELECT  thousand
        FROM onek WHERE thousand < 5
        ORDER BY thousand FETCH FIRST 1 ROW WITH TIES;
index afce5019b2664367e71b90f0c8b3db6cbad48836..d2d4ef132df2f0a1e75dbdb8c3dd281b855dc852 100644 (file)
@@ -161,6 +161,10 @@ SELECT  thousand
        FROM onek WHERE thousand < 5
        ORDER BY thousand FETCH FIRST 2 ROW WITH TIES;
 
+SELECT  thousand
+       FROM onek WHERE thousand < 5
+       ORDER BY thousand FETCH FIRST ROWS WITH TIES;
+
 SELECT  thousand
        FROM onek WHERE thousand < 5
        ORDER BY thousand FETCH FIRST 1 ROW WITH TIES;
@@ -168,6 +172,7 @@ SELECT  thousand
 SELECT  thousand
        FROM onek WHERE thousand < 5
        ORDER BY thousand FETCH FIRST 2 ROW ONLY;
+
 -- should fail
 SELECT ''::text AS two, unique1, unique2, stringu1
        FROM onek WHERE unique1 > 50