You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "guan404ming (via GitHub)" <gi...@apache.org> on 2026/02/09 04:52:47 UTC

[PR] PostgreSQL: Support PRIMARY KEY/UNIQUE USING INDEX [datafusion-sqlparser-rs]

guan404ming opened a new pull request, #2213:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2213

   ## Why
   
   PostgreSQL allows promoting an existing unique index to a PRIMARY KEY or UNIQUE table constraint via ALTER TABLE, which was
   not supported by the parser.
   
   ## How
   
   - Add ConstraintUsingIndex variant to TableConstraint with display, span, and serialization support
   - Parse USING INDEX index_name after PRIMARY KEY or UNIQUE keywords in constraint parsing
   - Add tests for named/unnamed constraints and deferrable characteristics


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] PostgreSQL: Support PRIMARY KEY/UNIQUE USING INDEX [datafusion-sqlparser-rs]

Posted by "iffyio (via GitHub)" <gi...@apache.org>.
iffyio merged PR #2213:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2213


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] PostgreSQL: Support PRIMARY KEY/UNIQUE USING INDEX [datafusion-sqlparser-rs]

Posted by "iffyio (via GitHub)" <gi...@apache.org>.
iffyio commented on code in PR #2213:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2213#discussion_r2821301988


##########
src/parser/mod.rs:
##########
@@ -9375,6 +9391,22 @@ impl<'a> Parser<'a> {
                 // after `PRIMARY` always stay `KEY`
                 self.expect_keyword_is(Keyword::KEY)?;
 
+                // PostgreSQL: PRIMARY KEY USING INDEX index_name
+                // https://www.postgresql.org/docs/current/sql-altertable.html
+                if self.parse_keywords(&[Keyword::USING, Keyword::INDEX]) {
+                    let index_name = self.parse_identifier()?;
+                    let characteristics = self.parse_constraint_characteristics()?;
+                    return Ok(Some(
+                        ConstraintUsingIndex {
+                            name,
+                            is_primary_key: true,
+                            index_name,
+                            characteristics,
+                        }
+                        .into(),
+                    ));
+                }

Review Comment:
   can we pull this out into a e.g. `parse_constraint_using_index()` function so that it can be reused across both cases?
   
   Also repr wise, I think it would be clearer to have both cases as unique variants on the `TableConstraint` enum vs using the `is_primary_key` bool to differentiate. i.e.
   ```rust
   TableConstraint::UniqueUsingIndex(ConstraintUsingIndex)
   TableConstraint::PrimaryKeyUsingIndex(ConstraintUsingIndex)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] PostgreSQL: Support PRIMARY KEY/UNIQUE USING INDEX [datafusion-sqlparser-rs]

Posted by "guan404ming (via GitHub)" <gi...@apache.org>.
guan404ming commented on code in PR #2213:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2213#discussion_r2822498279


##########
src/parser/mod.rs:
##########
@@ -9375,6 +9391,22 @@ impl<'a> Parser<'a> {
                 // after `PRIMARY` always stay `KEY`
                 self.expect_keyword_is(Keyword::KEY)?;
 
+                // PostgreSQL: PRIMARY KEY USING INDEX index_name
+                // https://www.postgresql.org/docs/current/sql-altertable.html
+                if self.parse_keywords(&[Keyword::USING, Keyword::INDEX]) {
+                    let index_name = self.parse_identifier()?;
+                    let characteristics = self.parse_constraint_characteristics()?;
+                    return Ok(Some(
+                        ConstraintUsingIndex {
+                            name,
+                            is_primary_key: true,
+                            index_name,
+                            characteristics,
+                        }
+                        .into(),
+                    ));
+                }

Review Comment:
   Yes, thanks for the nice suggestions. I just updated with 
   
   1. Split enum variant: TableConstraint::ConstraintUsingIndex into PrimaryKeyUsingIndex and UniqueUsingIndex, removing the is_primary_key bool from the struct.                                                                                                                                                      
   2. Extracted helper: Created parse_constraint_using_index() to deduplicate the parsing logic used by both UNIQUE USING INDEX and PRIMARY KEY USING INDEX.  



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


Re: [PR] PostgreSQL: Support PRIMARY KEY/UNIQUE USING INDEX [datafusion-sqlparser-rs]

Posted by "guan404ming (via GitHub)" <gi...@apache.org>.
guan404ming commented on code in PR #2213:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2213#discussion_r2822498279


##########
src/parser/mod.rs:
##########
@@ -9375,6 +9391,22 @@ impl<'a> Parser<'a> {
                 // after `PRIMARY` always stay `KEY`
                 self.expect_keyword_is(Keyword::KEY)?;
 
+                // PostgreSQL: PRIMARY KEY USING INDEX index_name
+                // https://www.postgresql.org/docs/current/sql-altertable.html
+                if self.parse_keywords(&[Keyword::USING, Keyword::INDEX]) {
+                    let index_name = self.parse_identifier()?;
+                    let characteristics = self.parse_constraint_characteristics()?;
+                    return Ok(Some(
+                        ConstraintUsingIndex {
+                            name,
+                            is_primary_key: true,
+                            index_name,
+                            characteristics,
+                        }
+                        .into(),
+                    ));
+                }

Review Comment:
   Yes, thanks for the nice suggestions. I just updated with 
   
   1. Split enum variant: `TableConstraint::ConstraintUsingIndex` into `PrimaryKeyUsingIndex` and `UniqueUsingIndex`, removing the `is_primary_key bool` from the struct.                                                                                                                                                      
   2. Extracted helper: Created `parse_constraint_using_index()` to deduplicate the parsing logic used by both `UNIQUE USING INDEX` and `PRIMARY KEY USING INDEX`.  



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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