From f6483f4369946cf8ff9621bfb6200a82ed111265 Mon Sep 17 00:00:00 2001 From: chenxiaobin19 <1025221611@qq.com> Date: Tue, 20 Dec 2022 15:14:42 +0800 Subject: [PATCH] support siblings by + limit --- src/common/backend/parser/gram.y | 9 ++-- src/test/regress/expected/sw_siblings.out | 57 +++++++++++++++++++++++ src/test/regress/sql/sw_siblings.sql | 25 ++++++++++ 3 files changed, 88 insertions(+), 3 deletions(-) diff --git a/src/common/backend/parser/gram.y b/src/common/backend/parser/gram.y index 16848a3a2..c6d7ccece 100644 --- a/src/common/backend/parser/gram.y +++ b/src/common/backend/parser/gram.y @@ -21171,9 +21171,12 @@ select_with_parens: */ select_no_parens: simple_select { $$ = $1; } - | select_clause siblings_clause + | select_clause siblings_clause opt_select_limit { SelectStmt* stmt = (SelectStmt *) $1; + insertSelectOptions((SelectStmt *) $1, NIL, NIL, + (Node*)list_nth($3, 0), (Node*)list_nth($3, 1), NULL, + yyscanner); StartWithClause* swc = (StartWithClause*) stmt->startWithClause; if (swc == NULL) { ereport(errstate, @@ -21185,11 +21188,11 @@ select_no_parens: } $$ = $1; } - | select_clause siblings_clause sort_clause + | select_clause siblings_clause sort_clause opt_select_limit { SelectStmt* stmt = (SelectStmt *) $1; insertSelectOptions((SelectStmt *) $1, $3, NIL, - NULL, NULL, NULL, + (Node*)list_nth($4, 0), (Node*)list_nth($4, 1), NULL, yyscanner); StartWithClause* swc = (StartWithClause*) stmt->startWithClause; if (swc == NULL) { diff --git a/src/test/regress/expected/sw_siblings.out b/src/test/regress/expected/sw_siblings.out index 14d955a4f..8915a635a 100644 --- a/src/test/regress/expected/sw_siblings.out +++ b/src/test/regress/expected/sw_siblings.out @@ -496,3 +496,60 @@ ORDER SIBLINGS BY id desc, name, name_desc desc; -> WorkTable Scan on tmp_reuslt (18 rows) +-- test siblings by + limit +SELECT *, LEVEL, connect_by_isleaf, connect_by_iscycle, connect_by_root(name_desc), SYS_CONNECT_BY_PATH(name, '@') cpath +FROM test_area +START WITH name = '中国' +CONNECT BY prior id = fatherid +ORDER SIBLINGS BY 1 limit 5; + id | name | fatherid | name_desc | level | connect_by_isleaf | connect_by_iscycle | connect_by_root | cpath +----+--------+----------+-----------+-------+-------------------+--------------------+-----------------+---------------------------- + 1 | 中国 | 0 | China | 1 | 0 | 0 | China | @中国 + 2 | 湖南省 | 1 | Hunan | 2 | 0 | 0 | China | @中国@湖南省 + 11 | 长沙市 | 2 | Changsha | 3 | 1 | 0 | China | @中国@湖南省@长沙市 + 13 | 衡阳市 | 2 | Hengyang | 3 | 0 | 0 | China | @中国@湖南省@衡阳市 + 14 | 耒阳市 | 13 | Leiyang | 4 | 1 | 0 | China | @中国@湖南省@衡阳市@耒阳市 +(5 rows) + +SELECT *, LEVEL, connect_by_isleaf, connect_by_iscycle, connect_by_root(name_desc), SYS_CONNECT_BY_PATH(name, '@') cpath +FROM test_area +START WITH name = '中国' +CONNECT BY prior id = fatherid +ORDER SIBLINGS BY 1 limit 5 offset 5; + id | name | fatherid | name_desc | level | connect_by_isleaf | connect_by_iscycle | connect_by_root | cpath +----+--------+----------+-----------+-------+-------------------+--------------------+-----------------+---------------------------- + 18 | 常宁市 | 13 | Changning | 4 | 1 | 0 | China | @中国@湖南省@衡阳市@常宁市 + 19 | 祁东县 | 13 | Qidong | 4 | 1 | 0 | China | @中国@湖南省@衡阳市@祁东县 + 20 | 祁南县 | 13 | Qinan | 4 | 1 | 0 | China | @中国@湖南省@衡阳市@祁南县 + 21 | 祁西县 | 13 | Qixi | 4 | 1 | 0 | China | @中国@湖南省@衡阳市@祁西县 + 22 | 祁北县 | 13 | Qibei | 4 | 1 | 0 | China | @中国@湖南省@衡阳市@祁北县 +(5 rows) + +SELECT *, LEVEL, connect_by_isleaf, connect_by_iscycle, connect_by_root(name_desc), SYS_CONNECT_BY_PATH(name, '@') cpath +FROM test_area +START WITH name = '中国' +CONNECT BY prior id = fatherid +ORDER SIBLINGS BY 1 order by name limit 5; + id | name | fatherid | name_desc | level | connect_by_isleaf | connect_by_iscycle | connect_by_root | cpath +----+--------+----------+-----------+-------+-------------------+--------------------+-----------------+---------------------------- + 1 | 中国 | 0 | China | 1 | 0 | 0 | China | @中国 + 12 | 南山区 | 10 | Nanshan | 4 | 1 | 0 | China | @中国@广东省@深圳市@南山区 + 17 | 宝安区 | 10 | Baoan | 4 | 1 | 0 | China | @中国@广东省@深圳市@宝安区 + 7 | 山东省 | 1 | Shandong | 2 | 1 | 0 | China | @中国@山东省 + 18 | 常宁市 | 13 | Changning | 4 | 1 | 0 | China | @中国@湖南省@衡阳市@常宁市 +(5 rows) + +SELECT *, LEVEL, connect_by_isleaf, connect_by_iscycle, connect_by_root(name_desc), SYS_CONNECT_BY_PATH(name, '@') cpath +FROM test_area +START WITH name = '中国' +CONNECT BY prior id = fatherid +ORDER SIBLINGS BY 1 order by name limit 5 offset 5; + id | name | fatherid | name_desc | level | connect_by_isleaf | connect_by_iscycle | connect_by_root | cpath +----+--------+----------+-----------+-------+-------------------+--------------------+-----------------+-------------- + 3 | 广东省 | 1 | Guangdong | 2 | 0 | 0 | China | @中国@广东省 + 9 | 江苏省 | 1 | Jiangsu | 2 | 1 | 0 | China | @中国@江苏省 + 5 | 河北省 | 1 | Hebei | 2 | 1 | 0 | China | @中国@河北省 + 6 | 河南省 | 1 | Henan | 2 | 1 | 0 | China | @中国@河南省 + 4 | 海南省 | 1 | Hainan | 2 | 1 | 0 | China | @中国@海南省 +(5 rows) + diff --git a/src/test/regress/sql/sw_siblings.sql b/src/test/regress/sql/sw_siblings.sql index 764892c5b..bbb2190b5 100644 --- a/src/test/regress/sql/sw_siblings.sql +++ b/src/test/regress/sql/sw_siblings.sql @@ -167,3 +167,28 @@ SELECT * FROM test_area START WITH name = '中国' CONNECT BY prior id = fatherid ORDER SIBLINGS BY id desc, name, name_desc desc; + +-- test siblings by + limit +SELECT *, LEVEL, connect_by_isleaf, connect_by_iscycle, connect_by_root(name_desc), SYS_CONNECT_BY_PATH(name, '@') cpath +FROM test_area +START WITH name = '中国' +CONNECT BY prior id = fatherid +ORDER SIBLINGS BY 1 limit 5; + +SELECT *, LEVEL, connect_by_isleaf, connect_by_iscycle, connect_by_root(name_desc), SYS_CONNECT_BY_PATH(name, '@') cpath +FROM test_area +START WITH name = '中国' +CONNECT BY prior id = fatherid +ORDER SIBLINGS BY 1 limit 5 offset 5; + +SELECT *, LEVEL, connect_by_isleaf, connect_by_iscycle, connect_by_root(name_desc), SYS_CONNECT_BY_PATH(name, '@') cpath +FROM test_area +START WITH name = '中国' +CONNECT BY prior id = fatherid +ORDER SIBLINGS BY 1 order by name limit 5; + +SELECT *, LEVEL, connect_by_isleaf, connect_by_iscycle, connect_by_root(name_desc), SYS_CONNECT_BY_PATH(name, '@') cpath +FROM test_area +START WITH name = '中国' +CONNECT BY prior id = fatherid +ORDER SIBLINGS BY 1 order by name limit 5 offset 5;