You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/09/24 21:20:05 UTC

(datafusion-sqlparser-rs) branch gh-readonly-queue/main/pr-2569-712de6e96d87523ddc8be24a98331042ed5fb747 created (now 88ffee641)

This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a change to branch gh-readonly-queue/main/pr-2569-712de6e96d87523ddc8be24a98331042ed5fb747
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git


      at 88ffee641 ClickHouse: Support ALTER TABLE ... MODIFY ORDER BY (#2569)

This branch includes the following new commits:

     new 88ffee641 ClickHouse: Support ALTER TABLE ... MODIFY ORDER BY (#2569)

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org


(datafusion-sqlparser-rs) 01/01: ClickHouse: Support ALTER TABLE ... MODIFY ORDER BY (#2569)

Posted by gi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-2569-712de6e96d87523ddc8be24a98331042ed5fb747
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git

commit 88ffee641a2192f17b44fec658463e2963233ec3
Author: Jordi Orihuela <jo...@gmail.com>
AuthorDate: Thu Sep 24 21:19:40 2026 +0000

    ClickHouse: Support ALTER TABLE ... MODIFY ORDER BY (#2569)
    
    Co-authored-by: Luca Cappelletti <ca...@gmail.com>
---
 src/ast/ddl.rs                | 11 +++++++++++
 src/ast/spans.rs              | 18 ++++++++++++++++++
 src/parser/mod.rs             | 32 +++++++++++++++++++++-----------
 tests/sqlparser_clickhouse.rs | 39 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 89 insertions(+), 11 deletions(-)

diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs
index 66f2cad3e..b8e917822 100644
--- a/src/ast/ddl.rs
+++ b/src/ast/ddl.rs
@@ -412,6 +412,14 @@ pub enum AlterTableOperation {
         /// MySQL-specific column position (`FIRST`/`AFTER`).
         column_position: Option<MySQLColumnPosition>,
     },
+    /// `MODIFY ORDER BY <expr>`
+    ///
+    /// Note: this is a ClickHouse-specific operation.
+    /// Please refer to [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/alter/order-by)
+    ModifyOrderBy {
+        /// The new sorting key.
+        order_by: OneOrManyWithParens<Expr>,
+    },
     /// `RENAME CONSTRAINT <old_constraint_name> TO <new_constraint_name>`
     ///
     /// Note: this is a PostgreSQL-specific operation.
@@ -963,6 +971,9 @@ impl fmt::Display for AlterTableOperation {
 
                 Ok(())
             }
+            AlterTableOperation::ModifyOrderBy { order_by } => {
+                write!(f, "MODIFY ORDER BY {order_by}")
+            }
             AlterTableOperation::RenameConstraint { old_name, new_name } => {
                 write!(f, "RENAME CONSTRAINT {old_name} TO {new_name}")
             }
diff --git a/src/ast/spans.rs b/src/ast/spans.rs
index 7acbd7d0b..9fa9e61ef 100644
--- a/src/ast/spans.rs
+++ b/src/ast/spans.rs
@@ -1220,6 +1220,9 @@ impl Spanned for AlterTableOperation {
             } => {
                 union_spans(core::iter::once(col_name.span).chain(options.iter().map(|i| i.span())))
             }
+            AlterTableOperation::ModifyOrderBy { order_by } => {
+                union_spans(order_by.iter().map(|e| e.span()))
+            }
             AlterTableOperation::RenameConstraint { old_name, new_name } => {
                 old_name.span.union(&new_name.span)
             }
@@ -3163,4 +3166,19 @@ WHERE id = 1
             Span::new(Location::new(2, 8), Location::new(4, 52))
         );
     }
+
+    #[test]
+    fn test_alter_table_modify_order_by_span() {
+        let dialect = &crate::dialect::ClickHouseDialect {};
+        let sql = "ALTER TABLE t MODIFY ORDER BY (a, b.c)";
+        let test = SpanTest::new(dialect, sql);
+        let r = Parser::parse_sql(dialect, sql).unwrap();
+        match &r[0] {
+            Statement::AlterTable(alter) => {
+                let op_span = alter.operations[0].span();
+                assert_eq!(test.get_source(op_span), "a, b.c");
+            }
+            stmt => panic!("expected ALTER TABLE; got {stmt:?}"),
+        }
+    }
 }
diff --git a/src/parser/mod.rs b/src/parser/mod.rs
index a4ee28763..6fd25b9eb 100644
--- a/src/parser/mod.rs
+++ b/src/parser/mod.rs
@@ -8836,17 +8836,7 @@ impl<'a> Parser<'a> {
         };
 
         let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
-            if self.consume_token(&Token::LParen) {
-                let columns = if self.peek_token_ref().token != Token::RParen {
-                    self.parse_comma_separated(|p| p.parse_expr())?
-                } else {
-                    vec![]
-                };
-                self.expect_token(&Token::RParen)?;
-                Some(OneOrManyWithParens::Many(columns))
-            } else {
-                Some(OneOrManyWithParens::One(self.parse_expr()?))
-            }
+            Some(self.parse_sorting_key()?)
         } else {
             None
         };
@@ -10680,6 +10670,22 @@ impl<'a> Parser<'a> {
         Ok(AlterTableOperation::AlterSortKey { columns })
     }
 
+    /// Parse the expression(s) following ClickHouse `ORDER BY`: either a single
+    /// expression or a possibly empty parenthesized list.
+    fn parse_sorting_key(&mut self) -> Result<OneOrManyWithParens<Expr>, ParserError> {
+        if self.consume_token(&Token::LParen) {
+            let columns = if self.peek_token_ref().token != Token::RParen {
+                self.parse_comma_separated(|p| p.parse_expr())?
+            } else {
+                vec![]
+            };
+            self.expect_token(&Token::RParen)?;
+            Ok(OneOrManyWithParens::Many(columns))
+        } else {
+            Ok(OneOrManyWithParens::One(self.parse_expr()?))
+        }
+    }
+
     /// Parse a single `ALTER TABLE` operation and return an `AlterTableOperation`.
     pub fn parse_alter_table_operation(&mut self) -> Result<AlterTableOperation, ParserError> {
         let operation = if self.parse_keyword(Keyword::ADD) {
@@ -10940,6 +10946,10 @@ impl<'a> Parser<'a> {
                 options,
                 column_position,
             }
+        } else if self.parse_keywords(&[Keyword::MODIFY, Keyword::ORDER, Keyword::BY]) {
+            AlterTableOperation::ModifyOrderBy {
+                order_by: self.parse_sorting_key()?,
+            }
         } else if self.parse_keyword(Keyword::MODIFY) {
             let _ = self.parse_keyword(Keyword::COLUMN); // [ COLUMN ]
             let col_name = self.parse_identifier()?;
diff --git a/tests/sqlparser_clickhouse.rs b/tests/sqlparser_clickhouse.rs
index 2d55ae058..7cf30cce9 100644
--- a/tests/sqlparser_clickhouse.rs
+++ b/tests/sqlparser_clickhouse.rs
@@ -1917,6 +1917,45 @@ fn parse_alter_table_column_position() {
         .is_err());
 }
 
+#[test]
+fn parse_alter_table_modify_order_by() {
+    clickhouse().verified_stmt("ALTER TABLE events MODIFY ORDER BY (region, user.id)");
+    clickhouse_and_generic().verified_stmt("ALTER TABLE events MODIFY ORDER BY region");
+    clickhouse_and_generic().verified_stmt("ALTER TABLE events MODIFY ORDER BY ()");
+    clickhouse_and_generic().verified_stmt("ALTER TABLE events MODIFY ORDER BY tuple()");
+    clickhouse_and_generic()
+        .verified_stmt("ALTER TABLE events ON CLUSTER c MODIFY ORDER BY (a, toDate(ts))");
+    clickhouse_and_generic()
+        .verified_stmt("ALTER TABLE events ADD COLUMN b UInt8, MODIFY ORDER BY (a, b)");
+    // A column named `order` is still handled by MODIFY COLUMN.
+    clickhouse_and_generic().verified_stmt("ALTER TABLE events MODIFY COLUMN `order` UInt8");
+
+    match clickhouse_and_generic().verified_stmt("ALTER TABLE events MODIFY ORDER BY (a)") {
+        Statement::AlterTable(AlterTable { operations, .. }) => {
+            assert_eq!(
+                operations,
+                vec![AlterTableOperation::ModifyOrderBy {
+                    order_by: OneOrManyWithParens::Many(vec![Expr::Identifier(Ident::new("a"))]),
+                }]
+            );
+        }
+        _ => unreachable!(),
+    }
+
+    for sql in [
+        "ALTER TABLE events MODIFY ORDER BY",
+        "ALTER TABLE events MODIFY ORDER BY (a",
+        "ALTER TABLE events MODIFY ORDER BY (,)",
+        "ALTER TABLE events MODIFY ORDER BY (a) b",
+        "CREATE TABLE events (a UInt8) ENGINE = MergeTree ORDER BY (a",
+    ] {
+        assert!(
+            clickhouse_and_generic().parse_sql_statements(sql).is_err(),
+            "{sql}"
+        );
+    }
+}
+
 fn clickhouse() -> TestedDialects {
     TestedDialects::new(vec![Box::new(ClickHouseDialect {})])
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org