Oracle: Support ORDER SIBLINGS BY in hierarchical queries (#3091)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/fuzz/fuzz_seeds/oracle_order_siblings_by.sql b/fuzz/fuzz_seeds/oracle_order_siblings_by.sql
new file mode 100644
index 0000000..d4fce3c
--- /dev/null
+++ b/fuzz/fuzz_seeds/oracle_order_siblings_by.sql
@@ -0,0 +1 @@
+SELECT id FROM customers START WITH id = 1 CONNECT BY PRIOR id = id - 1 ORDER SIBLINGS BY id DESC NULLS LAST
diff --git a/src/ast/query.rs b/src/ast/query.rs
index 61b3351..ec45cf1 100644
--- a/src/ast/query.rs
+++ b/src/ast/query.rs
@@ -2987,11 +2987,21 @@
 
     /// Optional `INTERPOLATE` clause (ClickHouse extension).
     pub interpolate: Option<Interpolate>,
+
+    /// `true` for `ORDER SIBLINGS BY`, which orders the rows of a hierarchical
+    /// query among siblings while preserving the hierarchy.
+    ///
+    /// [Oracle](https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/SELECT.html)
+    pub siblings: bool,
 }
 
 impl fmt::Display for OrderBy {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "ORDER BY")?;
+        if self.siblings {
+            write!(f, "ORDER SIBLINGS BY")?;
+        } else {
+            write!(f, "ORDER BY")?;
+        }
         match &self.kind {
             OrderByKind::Expressions(exprs) => {
                 write!(f, " {}", display_comma_separated(exprs))?;
diff --git a/src/dialect/generic.rs b/src/dialect/generic.rs
index 3231b15..9d58cfd 100644
--- a/src/dialect/generic.rs
+++ b/src/dialect/generic.rs
@@ -77,6 +77,10 @@
         true
     }
 
+    fn supports_order_siblings_by(&self) -> bool {
+        true
+    }
+
     fn supports_match_recognize(&self) -> bool {
         true
     }
diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs
index 9240b1b..020479f 100644
--- a/src/dialect/mod.rs
+++ b/src/dialect/mod.rs
@@ -526,6 +526,15 @@
         false
     }
 
+    /// Returns true if the dialect supports `ORDER SIBLINGS BY` in hierarchical
+    /// (`CONNECT BY`) queries, e.g.
+    /// `SELECT id FROM t START WITH id = 1 CONNECT BY PRIOR id = parent ORDER SIBLINGS BY id`.
+    ///
+    /// [Oracle](https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/SELECT.html)
+    fn supports_order_siblings_by(&self) -> bool {
+        false
+    }
+
     /// Returns true if the dialect supports `EXECUTE IMMEDIATE` statements.
     fn supports_execute_immediate(&self) -> bool {
         false
diff --git a/src/dialect/oracle.rs b/src/dialect/oracle.rs
index fc46821..2214977 100644
--- a/src/dialect/oracle.rs
+++ b/src/dialect/oracle.rs
@@ -66,6 +66,11 @@
         true
     }
 
+    /// See <https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/SELECT.html>
+    fn supports_order_siblings_by(&self) -> bool {
+        true
+    }
+
     fn supports_execute_immediate(&self) -> bool {
         true
     }
diff --git a/src/keywords.rs b/src/keywords.rs
index bd83c50..f599f44 100644
--- a/src/keywords.rs
+++ b/src/keywords.rs
@@ -1010,6 +1010,7 @@
     SHARED,
     SHARING,
     SHOW,
+    SIBLINGS,
     SIGNED,
     SIMILAR,
     SIMPLE,
diff --git a/src/parser/mod.rs b/src/parser/mod.rs
index f1f34f5..e6a48a2 100644
--- a/src/parser/mod.rs
+++ b/src/parser/mod.rs
@@ -14966,13 +14966,16 @@
 
     /// Parse an optional `ORDER BY` clause, returning `Some(OrderBy)` when present.
     pub fn parse_optional_order_by(&mut self) -> Result<Option<OrderBy>, ParserError> {
-        if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
+        let siblings = self.dialect.supports_order_siblings_by()
+            && self.parse_keywords(&[Keyword::ORDER, Keyword::SIBLINGS, Keyword::BY]);
+        if siblings || self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
             let order_by =
                 if self.dialect.supports_order_by_all() && self.parse_keyword(Keyword::ALL) {
                     let order_by_options = self.parse_order_by_options()?;
                     OrderBy {
                         kind: OrderByKind::All(order_by_options),
                         interpolate: None,
+                        siblings,
                     }
                 } else {
                     let exprs = self.parse_comma_separated(Parser::parse_order_by_expr)?;
@@ -14984,6 +14987,7 @@
                     OrderBy {
                         kind: OrderByKind::Expressions(exprs),
                         interpolate,
+                        siblings,
                     }
                 };
             Ok(Some(order_by))
diff --git a/tests/sqlparser_bigquery.rs b/tests/sqlparser_bigquery.rs
index 30fcdba..b15a5ec 100644
--- a/tests/sqlparser_bigquery.rs
+++ b/tests/sqlparser_bigquery.rs
@@ -2788,6 +2788,7 @@
                         with_fill: None,
                     },]),
                     interpolate: None,
+                    siblings: false,
                 }),
                 limit_clause: Some(LimitClause::LimitOffset {
                     limit: Some(Expr::Value(number("10").with_empty_span())),
@@ -2894,6 +2895,7 @@
                         with_fill: None,
                     },]),
                     interpolate: None,
+                    siblings: false,
                 }),
                 limit_clause: Some(LimitClause::LimitOffset {
                     limit: Some(Expr::Value(number("10").with_empty_span())),
diff --git a/tests/sqlparser_clickhouse.rs b/tests/sqlparser_clickhouse.rs
index d567324..50e15c2 100644
--- a/tests/sqlparser_clickhouse.rs
+++ b/tests/sqlparser_clickhouse.rs
@@ -345,6 +345,7 @@
                                 with_fill: None,
                             }]),
                             interpolate: None,
+                            siblings: false,
                         }),
                     }
                 }
@@ -1209,7 +1210,8 @@
                         right: Box::new(Expr::value(number("1"))),
                     }),
                 }])
-            })
+            }),
+            siblings: false,
         },
         select.order_by.expect("ORDER BY expected")
     );
diff --git a/tests/sqlparser_oracle.rs b/tests/sqlparser_oracle.rs
index 772f34f..0c910ac 100644
--- a/tests/sqlparser_oracle.rs
+++ b/tests/sqlparser_oracle.rs
@@ -445,6 +445,47 @@
 }
 
 #[test]
+fn parse_order_siblings_by() {
+    let query = oracle().verified_query(
+        "SELECT id FROM customers START WITH id = 1 CONNECT BY PRIOR id = parent_id \
+         ORDER SIBLINGS BY id DESC NULLS LAST, name",
+    );
+    assert!(query.order_by.expect("ORDER SIBLINGS BY expected").siblings);
+    assert!(
+        !oracle()
+            .verified_query("SELECT id FROM t ORDER BY id")
+            .order_by
+            .expect("ORDER BY expected")
+            .siblings
+    );
+
+    oracle().verified_stmt(
+        "SELECT q.id FROM (SELECT id FROM t START WITH id = 1 CONNECT BY PRIOR id = parent_id \
+         ORDER SIBLINGS BY id) AS q ORDER BY q.id",
+    );
+    oracle().verified_stmt(
+        "SELECT id FROM t CONNECT BY PRIOR id = parent_id ORDER SIBLINGS BY id FETCH FIRST 5 ROWS ONLY",
+    );
+
+    // `siblings` is still an ordinary identifier
+    oracle().verified_stmt("SELECT siblings FROM t ORDER BY siblings");
+
+    assert_eq!(
+        oracle().parse_sql_statements("SELECT id FROM t ORDER SIBLINGS BY"),
+        Err(ParserError::ParserError(
+            "Expected: an expression, found: EOF".to_string()
+        ))
+    );
+    assert_eq!(
+        all_dialects_where(|d| !d.supports_order_siblings_by())
+            .parse_sql_statements("SELECT id FROM t ORDER SIBLINGS BY id"),
+        Err(ParserError::ParserError(
+            "Expected: end of statement, found: ORDER".to_string()
+        ))
+    );
+}
+
+#[test]
 fn test_connect_by() {
     let oracle_dialect = oracle();