CREATE TABLE with_ties_table (a INT, b INT);
SELECT create_distributed_table('with_ties_table', 'a');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

INSERT INTO with_ties_table VALUES (10, 20), (11, 21), (12, 22), (12, 22), (12, 22), (12, 23), (14, 24);
-- test ordering by distribution column
SELECT a FROM with_ties_table ORDER BY a OFFSET 1 FETCH FIRST 2 ROWS WITH TIES;
 a
---------------------------------------------------------------------
 11
 12
 12
 12
 12
(5 rows)

-- test ordering by non-distribution column
SELECT b FROM with_ties_table ORDER BY b OFFSET 1 FETCH FIRST 2 ROWS WITH TIES;
 b
---------------------------------------------------------------------
 21
 22
 22
 22
(4 rows)

-- test ordering by distribution column filtering one shard
SELECT a FROM with_ties_table WHERE a=12 ORDER BY a OFFSET 1 FETCH FIRST 1 ROWS WITH TIES;
 a
---------------------------------------------------------------------
 12
 12
 12
(3 rows)

-- test ordering by non-distribution column filtering one shard
SELECT b FROM with_ties_table WHERE a=12 ORDER BY b OFFSET 1 FETCH FIRST 1 ROWS WITH TIES;
 b
---------------------------------------------------------------------
 22
 22
(2 rows)

-- test INSERT SELECTs into local table
CREATE TABLE with_ties_table_2 (a INT, b INT);
-- test ordering by distribution column
INSERT INTO with_ties_table_2 SELECT a, b FROM with_ties_table ORDER BY a OFFSET 1 FETCH FIRST 2 ROWS WITH TIES;
SELECT * FROM with_ties_table_2 ORDER BY a, b;
 a  | b
---------------------------------------------------------------------
 11 | 21
 12 | 22
 12 | 22
 12 | 22
 12 | 23
(5 rows)

TRUNCATE with_ties_table_2;
-- test ordering by non-distribution column
INSERT INTO with_ties_table_2 SELECT a, b FROM with_ties_table ORDER BY b OFFSET 1 FETCH FIRST 2 ROWS WITH TIES;
SELECT * FROM with_ties_table_2 ORDER BY a, b;
 a  | b
---------------------------------------------------------------------
 11 | 21
 12 | 22
 12 | 22
 12 | 22
(4 rows)

TRUNCATE with_ties_table_2;
-- test ordering by distribution column filtering one shard
INSERT INTO with_ties_table_2 SELECT a FROM with_ties_table WHERE a=12 ORDER BY a OFFSET 1 FETCH FIRST 1 ROWS WITH TIES;
SELECT * FROM with_ties_table_2 ORDER BY a, b;
 a  | b
---------------------------------------------------------------------
 12 |
 12 |
 12 |
(3 rows)

TRUNCATE with_ties_table_2;
-- test ordering by non-distribution column filtering one shard
INSERT INTO with_ties_table_2 SELECT a, b FROM with_ties_table WHERE a=12 ORDER BY b OFFSET 1 FETCH FIRST 1 ROWS WITH TIES;
SELECT * FROM with_ties_table_2 ORDER BY a, b;
 a  | b
---------------------------------------------------------------------
 12 | 22
 12 | 22
(2 rows)

TRUNCATE with_ties_table_2;
-- test INSERT SELECTs into distributed table
SELECT create_distributed_table('with_ties_table_2', 'a');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

-- test ordering by distribution column
INSERT INTO with_ties_table_2 SELECT a, b FROM with_ties_table ORDER BY a OFFSET 1 FETCH FIRST 2 ROWS WITH TIES;
SELECT * FROM with_ties_table_2 ORDER BY a, b;
 a  | b
---------------------------------------------------------------------
 11 | 21
 12 | 22
 12 | 22
 12 | 22
 12 | 23
(5 rows)

TRUNCATE with_ties_table_2;
-- test ordering by non-distribution column
INSERT INTO with_ties_table_2 SELECT a, b FROM with_ties_table ORDER BY b OFFSET 1 FETCH FIRST 2 ROWS WITH TIES;
SELECT * FROM with_ties_table_2 ORDER BY a, b;
 a  | b
---------------------------------------------------------------------
 11 | 21
 12 | 22
 12 | 22
 12 | 22
(4 rows)

TRUNCATE with_ties_table_2;
-- test ordering by distribution column filtering one shard
INSERT INTO with_ties_table_2 SELECT a FROM with_ties_table WHERE a=12 ORDER BY a OFFSET 1 FETCH FIRST 1 ROWS WITH TIES;
SELECT * FROM with_ties_table_2 ORDER BY a, b;
 a  | b
---------------------------------------------------------------------
 12 |
 12 |
 12 |
(3 rows)

TRUNCATE with_ties_table_2;
-- test ordering by non-distribution column filtering one shard
INSERT INTO with_ties_table_2 SELECT a, b FROM with_ties_table WHERE a=12 ORDER BY b OFFSET 1 FETCH FIRST 1 ROWS WITH TIES;
SELECT * FROM with_ties_table_2 ORDER BY a, b;
 a  | b
---------------------------------------------------------------------
 12 | 22
 12 | 22
(2 rows)

TRUNCATE with_ties_table_2;
-- test INSERT SELECTs into distributed table with a different distribution column
SELECT undistribute_table('with_ties_table_2');
NOTICE:  creating a new table for public.with_ties_table_2
NOTICE:  moving the data of public.with_ties_table_2
NOTICE:  dropping the old public.with_ties_table_2
NOTICE:  renaming the new table to public.with_ties_table_2
 undistribute_table
---------------------------------------------------------------------

(1 row)

SELECT create_distributed_table('with_ties_table_2', 'b');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

-- test ordering by distribution column
INSERT INTO with_ties_table_2 SELECT a, b FROM with_ties_table ORDER BY a OFFSET 1 FETCH FIRST 2 ROWS WITH TIES;
SELECT * FROM with_ties_table_2 ORDER BY a, b;
 a  | b
---------------------------------------------------------------------
 11 | 21
 12 | 22
 12 | 22
 12 | 22
 12 | 23
(5 rows)

TRUNCATE with_ties_table_2;
-- test ordering by non-distribution column
INSERT INTO with_ties_table_2 SELECT a, b FROM with_ties_table ORDER BY b OFFSET 1 FETCH FIRST 2 ROWS WITH TIES;
SELECT * FROM with_ties_table_2 ORDER BY a, b;
 a  | b
---------------------------------------------------------------------
 11 | 21
 12 | 22
 12 | 22
 12 | 22
(4 rows)

TRUNCATE with_ties_table_2;
-- test ordering by distribution column filtering one shard
-- selecting actual b column makes this test flaky but we have tp select something for dist column
INSERT INTO with_ties_table_2 SELECT a, 1 FROM with_ties_table WHERE a=12 ORDER BY a OFFSET 1 FETCH FIRST 1 ROWS WITH TIES;
SELECT * FROM with_ties_table_2 ORDER BY a, b;
 a  | b
---------------------------------------------------------------------
 12 | 1
 12 | 1
 12 | 1
(3 rows)

TRUNCATE with_ties_table_2;
-- test ordering by non-distribution column filtering one shard
INSERT INTO with_ties_table_2 SELECT a, b FROM with_ties_table WHERE a=12 ORDER BY b OFFSET 1 FETCH FIRST 1 ROWS WITH TIES;
SELECT * FROM with_ties_table_2 ORDER BY a, b;
 a  | b
---------------------------------------------------------------------
 12 | 22
 12 | 22
(2 rows)

TRUNCATE with_ties_table_2;
-- test ordering by multiple columns
SELECT a, b FROM with_ties_table ORDER BY a, b OFFSET 1 FETCH FIRST 2 ROWS WITH TIES;
 a  | b
---------------------------------------------------------------------
 11 | 21
 12 | 22
 12 | 22
 12 | 22
(4 rows)

-- test ordering by multiple columns filtering one shard
SELECT a, b FROM with_ties_table WHERE a=12 ORDER BY a, b OFFSET 1 FETCH FIRST 1 ROWS WITH TIES;
 a  | b
---------------------------------------------------------------------
 12 | 22
 12 | 22
(2 rows)

-- test without ties
-- test ordering by distribution column
SELECT a FROM with_ties_table ORDER BY a OFFSET 1 FETCH FIRST 2 ROWS ONLY;
 a
---------------------------------------------------------------------
 11
 12
(2 rows)

-- test ordering by non-distribution column
SELECT b FROM with_ties_table ORDER BY b OFFSET 1 FETCH FIRST 2 ROWS ONLY;
 b
---------------------------------------------------------------------
 21
 22
(2 rows)

-- test ordering by distribution column filtering one shard
SELECT a FROM with_ties_table WHERE a=12 ORDER BY a OFFSET 1 FETCH FIRST 1 ROWS ONLY;
 a
---------------------------------------------------------------------
 12
(1 row)

-- test ordering by non-distribution column filtering one shard
SELECT b FROM with_ties_table WHERE a=12 ORDER BY b OFFSET 1 FETCH FIRST 1 ROWS ONLY;
 b
---------------------------------------------------------------------
 22
(1 row)

--test other syntaxes
SELECT a FROM with_ties_table ORDER BY a OFFSET 1 FETCH NEXT 2 ROW WITH TIES;
 a
---------------------------------------------------------------------
 11
 12
 12
 12
 12
(5 rows)

SELECT a FROM with_ties_table ORDER BY a OFFSET 2 FETCH NEXT ROW WITH TIES;
 a
---------------------------------------------------------------------
 12
 12
 12
 12
(4 rows)

