From 00662c03fbce23d0a6cff88d934f16a49bcbbde8 Mon Sep 17 00:00:00 2001 From: Michael Victor Zink Date: Mon, 7 Jul 2025 11:03:35 -0700 Subject: [PATCH] Display `CREATE INDEX` column list separated by commas with spaces The column list in `CREATE INDEX` now matches the style used elsewhere, e.g. in `TableConstraint`, which is to use spaces after commas. ```sql -- before: CREATE INDEX idx_name ON table_name (column1,column2,column3); -- after: CREATE INDEX idx_name ON table_name (column1, column2, column3); ``` When `CreateIndex` was added, there was no explanation for the lack of spaces, so I assume it was just author preference. But standard style in all documentation I've seen is to use spaces after commas (including [MSSQL]'s documentation of `INCLUDE`, which copied the no-spaces style when added). [MSSQL]: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-index-transact-sql?view=sql-server-ver17#i-create-an-index-with-included-non-key-columns --- src/ast/dml.rs | 4 ++-- tests/sqlparser_common.rs | 4 ++-- tests/sqlparser_postgres.rs | 20 ++++++++++---------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/ast/dml.rs b/src/ast/dml.rs index e179f5d7..134b0553 100644 --- a/src/ast/dml.rs +++ b/src/ast/dml.rs @@ -106,9 +106,9 @@ impl Display for CreateIndex { if let Some(value) = &self.using { write!(f, " USING {value} ")?; } - write!(f, "({})", display_separated(&self.columns, ","))?; + write!(f, "({})", display_comma_separated(&self.columns))?; if !self.include.is_empty() { - write!(f, " INCLUDE ({})", display_separated(&self.include, ","))?; + write!(f, " INCLUDE ({})", display_comma_separated(&self.include))?; } if let Some(value) = self.nulls_distinct { if value { diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index a64733d6..b1fd85f9 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -9175,7 +9175,7 @@ fn ensure_multiple_dialects_are_tested() { #[test] fn parse_create_index() { - let sql = "CREATE UNIQUE INDEX IF NOT EXISTS idx_name ON test(name,age DESC)"; + let sql = "CREATE UNIQUE INDEX IF NOT EXISTS idx_name ON test(name, age DESC)"; let indexed_columns: Vec = vec![ IndexColumn { operator_class: None, @@ -9221,7 +9221,7 @@ fn parse_create_index() { #[test] fn test_create_index_with_using_function() { - let sql = "CREATE UNIQUE INDEX IF NOT EXISTS idx_name ON test USING BTREE (name,age DESC)"; + let sql = "CREATE UNIQUE INDEX IF NOT EXISTS idx_name ON test USING BTREE (name, age DESC)"; let indexed_columns: Vec = vec![ IndexColumn { operator_class: None, diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 6c00b1f1..6fed27d1 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -2486,7 +2486,7 @@ fn parse_array_multi_subscript() { #[test] fn parse_create_index() { - let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2)"; + let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1, col2)"; match pg().verified_stmt(sql) { Statement::CreateIndex(CreateIndex { name: Some(ObjectName(name)), @@ -2517,7 +2517,7 @@ fn parse_create_index() { #[test] fn parse_create_anonymous_index() { - let sql = "CREATE INDEX ON my_table(col1,col2)"; + let sql = "CREATE INDEX ON my_table(col1, col2)"; match pg().verified_stmt(sql) { Statement::CreateIndex(CreateIndex { name, @@ -2577,7 +2577,7 @@ fn parse_create_indices_with_operator_classes() { .unwrap_or_default() ); let multi_column_sql_statement = format!( - "CREATE INDEX the_index_name ON users USING {expected_index_type} (column_name,concat_users_name(first_name, last_name){})", + "CREATE INDEX the_index_name ON users USING {expected_index_type} (column_name, concat_users_name(first_name, last_name){})", expected_operator_class.as_ref().map(|oc| format!(" {oc}")) .unwrap_or_default() ); @@ -2698,7 +2698,7 @@ fn parse_create_indices_with_operator_classes() { #[test] fn parse_create_bloom() { let sql = - "CREATE INDEX bloomidx ON tbloom USING BLOOM (i1,i2,i3) WITH (length = 80, col1 = 2, col2 = 2, col3 = 4)"; + "CREATE INDEX bloomidx ON tbloom USING BLOOM (i1, i2, i3) WITH (length = 80, col1 = 2, col2 = 2, col3 = 4)"; match pg().verified_stmt(sql) { Statement::CreateIndex(CreateIndex { name: Some(ObjectName(name)), @@ -2813,7 +2813,7 @@ fn parse_create_table_with_empty_inherits_fails() { #[test] fn parse_create_index_concurrently() { - let sql = "CREATE INDEX CONCURRENTLY IF NOT EXISTS my_index ON my_table(col1,col2)"; + let sql = "CREATE INDEX CONCURRENTLY IF NOT EXISTS my_index ON my_table(col1, col2)"; match pg().verified_stmt(sql) { Statement::CreateIndex(CreateIndex { name: Some(ObjectName(name)), @@ -2844,7 +2844,7 @@ fn parse_create_index_concurrently() { #[test] fn parse_create_index_with_predicate() { - let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) WHERE col3 IS NULL"; + let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1, col2) WHERE col3 IS NULL"; match pg().verified_stmt(sql) { Statement::CreateIndex(CreateIndex { name: Some(ObjectName(name)), @@ -2875,7 +2875,7 @@ fn parse_create_index_with_predicate() { #[test] fn parse_create_index_with_include() { - let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) INCLUDE (col3)"; + let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1, col2) INCLUDE (col3, col4)"; match pg().verified_stmt(sql) { Statement::CreateIndex(CreateIndex { name: Some(ObjectName(name)), @@ -2897,7 +2897,7 @@ fn parse_create_index_with_include() { assert!(!concurrently); assert!(if_not_exists); assert_eq_vec(&["col1", "col2"], &columns); - assert_eq_vec(&["col3"], &include); + assert_eq_vec(&["col3", "col4"], &include); assert!(with.is_empty()); } _ => unreachable!(), @@ -2906,7 +2906,7 @@ fn parse_create_index_with_include() { #[test] fn parse_create_index_with_nulls_distinct() { - let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) NULLS NOT DISTINCT"; + let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1, col2) NULLS NOT DISTINCT"; match pg().verified_stmt(sql) { Statement::CreateIndex(CreateIndex { name: Some(ObjectName(name)), @@ -2935,7 +2935,7 @@ fn parse_create_index_with_nulls_distinct() { _ => unreachable!(), } - let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1,col2) NULLS DISTINCT"; + let sql = "CREATE INDEX IF NOT EXISTS my_index ON my_table(col1, col2) NULLS DISTINCT"; match pg().verified_stmt(sql) { Statement::CreateIndex(CreateIndex { name: Some(ObjectName(name)),