support siblings by + limit

This commit is contained in:
chenxiaobin19
2022-12-20 15:14:42 +08:00
parent c950de5f36
commit f6483f4369
3 changed files with 88 additions and 3 deletions

View File

@ -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) {

View File

@ -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)

View File

@ -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;