You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "zzzdong (via GitHub)" <gi...@apache.org> on 2025/02/20 04:15:07 UTC
[PR] Add support column prefix index for MySQL [datafusion-sqlparser-rs]
zzzdong opened a new pull request, #1732:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/1732
This pull request add support for column prefixe and functional indexes in MySQL's `CREATE TABLE` and `ALTER TABLE` statements.
```sql
ALTER TABLE `tbl_1` ADD KEY `idx_1` (col_1(10) DESC, (LOWER(col_2)));
```
May fix parts of #302 .
--
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] Add support column prefix index for MySQL [datafusion-sqlparser-rs]
Posted by "zzzdong (via GitHub)" <gi...@apache.org>.
zzzdong commented on code in PR #1732:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/1732#discussion_r1962930972
##########
src/ast/mod.rs:
##########
@@ -8591,6 +8591,61 @@ pub enum CopyIntoSnowflakeKind {
Location,
}
+/// Index Field
+///
+/// This structure used here [`MySQL` CREATE INDEX][1], [`PostgreSQL` CREATE INDEX][2], [`MySQL` CREATE TABLE][3].
+///
+/// [1]: https://dev.mysql.com/doc/refman/8.3/en/create-index.html
+/// [2]: https://www.postgresql.org/docs/17/sql-createindex.html
+/// [3]: https://dev.mysql.com/doc/refman/8.3/en/create-table.html
+
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub struct IndexField {
+ pub expr: IndexExpr,
+ /// Optional `ASC` or `DESC`
+ pub asc: Option<bool>,
+}
+
+impl fmt::Display for IndexField {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{}", self.expr)?;
+ match self.asc {
+ Some(true) => write!(f, " ASC")?,
+ Some(false) => write!(f, " DESC")?,
+ None => (),
+ }
+ Ok(())
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub enum IndexExpr {
+ Column(Ident),
+ /// Mysql specific syntax
+ ///
+ /// See [Mysql documentation](https://dev.mysql.com/doc/refman/8.3/en/create-index.html)
+ /// for more details.
+ ColumnPrefix {
+ column: Ident,
+ length: u64,
+ },
+ Functional(Box<Expr>),
+}
Review Comment:
I agree that using OrderByExpr could be a good approach. However, I am somewhat concerned that users might find it challenging to distinguish between the different `Expr` when working with it.
--
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] Add support column prefix index for MySQL [datafusion-sqlparser-rs]
Posted by "zzzdong (via GitHub)" <gi...@apache.org>.
zzzdong commented on code in PR #1732:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/1732#discussion_r1966963134
##########
src/ast/mod.rs:
##########
@@ -8591,6 +8591,61 @@ pub enum CopyIntoSnowflakeKind {
Location,
}
+/// Index Field
+///
+/// This structure used here [`MySQL` CREATE INDEX][1], [`PostgreSQL` CREATE INDEX][2], [`MySQL` CREATE TABLE][3].
+///
+/// [1]: https://dev.mysql.com/doc/refman/8.3/en/create-index.html
+/// [2]: https://www.postgresql.org/docs/17/sql-createindex.html
+/// [3]: https://dev.mysql.com/doc/refman/8.3/en/create-table.html
+
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub struct IndexField {
+ pub expr: IndexExpr,
+ /// Optional `ASC` or `DESC`
+ pub asc: Option<bool>,
+}
+
+impl fmt::Display for IndexField {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{}", self.expr)?;
+ match self.asc {
+ Some(true) => write!(f, " ASC")?,
+ Some(false) => write!(f, " DESC")?,
+ None => (),
+ }
+ Ok(())
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub enum IndexExpr {
+ Column(Ident),
+ /// Mysql specific syntax
+ ///
+ /// See [Mysql documentation](https://dev.mysql.com/doc/refman/8.3/en/create-index.html)
+ /// for more details.
+ ColumnPrefix {
+ column: Ident,
+ length: u64,
+ },
+ Functional(Box<Expr>),
+}
Review Comment:
That's right. I believe adding only ColumnPrefix will suffice. We can extend the Expr enum as follows:
```rust
enum Expr {
ColumnPrefix { column: Ident, length: u64 }
}
```
--
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] Add support column prefix index for MySQL [datafusion-sqlparser-rs]
Posted by "iffyio (via GitHub)" <gi...@apache.org>.
iffyio commented on code in PR #1732:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/1732#discussion_r1966449581
##########
src/ast/mod.rs:
##########
@@ -8591,6 +8591,61 @@ pub enum CopyIntoSnowflakeKind {
Location,
}
+/// Index Field
+///
+/// This structure used here [`MySQL` CREATE INDEX][1], [`PostgreSQL` CREATE INDEX][2], [`MySQL` CREATE TABLE][3].
+///
+/// [1]: https://dev.mysql.com/doc/refman/8.3/en/create-index.html
+/// [2]: https://www.postgresql.org/docs/17/sql-createindex.html
+/// [3]: https://dev.mysql.com/doc/refman/8.3/en/create-table.html
+
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub struct IndexField {
+ pub expr: IndexExpr,
+ /// Optional `ASC` or `DESC`
+ pub asc: Option<bool>,
+}
+
+impl fmt::Display for IndexField {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{}", self.expr)?;
+ match self.asc {
+ Some(true) => write!(f, " ASC")?,
+ Some(false) => write!(f, " DESC")?,
+ None => (),
+ }
+ Ok(())
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub enum IndexExpr {
+ Column(Ident),
+ /// Mysql specific syntax
+ ///
+ /// See [Mysql documentation](https://dev.mysql.com/doc/refman/8.3/en/create-index.html)
+ /// for more details.
+ ColumnPrefix {
+ column: Ident,
+ length: u64,
+ },
+ Functional(Box<Expr>),
+}
Review Comment:
The different Expr I imagine are encoded by the Expr variants, I think it would be preferrable to using a custom repr here which looks to require duplicating a lot of Expr in order to be comprehensive
--
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] Add support column prefix index for MySQL [datafusion-sqlparser-rs]
Posted by "iffyio (via GitHub)" <gi...@apache.org>.
iffyio commented on code in PR #1732:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/1732#discussion_r1967152710
##########
src/ast/mod.rs:
##########
@@ -8591,6 +8591,61 @@ pub enum CopyIntoSnowflakeKind {
Location,
}
+/// Index Field
+///
+/// This structure used here [`MySQL` CREATE INDEX][1], [`PostgreSQL` CREATE INDEX][2], [`MySQL` CREATE TABLE][3].
+///
+/// [1]: https://dev.mysql.com/doc/refman/8.3/en/create-index.html
+/// [2]: https://www.postgresql.org/docs/17/sql-createindex.html
+/// [3]: https://dev.mysql.com/doc/refman/8.3/en/create-table.html
+
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub struct IndexField {
+ pub expr: IndexExpr,
+ /// Optional `ASC` or `DESC`
+ pub asc: Option<bool>,
+}
+
+impl fmt::Display for IndexField {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{}", self.expr)?;
+ match self.asc {
+ Some(true) => write!(f, " ASC")?,
+ Some(false) => write!(f, " DESC")?,
+ None => (),
+ }
+ Ok(())
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub enum IndexExpr {
+ Column(Ident),
+ /// Mysql specific syntax
+ ///
+ /// See [Mysql documentation](https://dev.mysql.com/doc/refman/8.3/en/create-index.html)
+ /// for more details.
+ ColumnPrefix {
+ column: Ident,
+ length: u64,
+ },
+ Functional(Box<Expr>),
+}
Review Comment:
Hmm it looks like `ColumnPrefix` is equivalent to a function call with one argument? that can already be expressed by an `Expr` so that it doesn't seem like that should be necessary?
--
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] Add support column prefix index for MySQL [datafusion-sqlparser-rs]
Posted by "zzzdong (via GitHub)" <gi...@apache.org>.
zzzdong closed pull request #1732: Add support column prefix index for MySQL
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/1732
--
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] Add support column prefix index for MySQL [datafusion-sqlparser-rs]
Posted by "iffyio (via GitHub)" <gi...@apache.org>.
iffyio commented on code in PR #1732:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/1732#discussion_r1962898767
##########
src/ast/mod.rs:
##########
@@ -8591,6 +8591,61 @@ pub enum CopyIntoSnowflakeKind {
Location,
}
+/// Index Field
+///
+/// This structure used here [`MySQL` CREATE INDEX][1], [`PostgreSQL` CREATE INDEX][2], [`MySQL` CREATE TABLE][3].
+///
+/// [1]: https://dev.mysql.com/doc/refman/8.3/en/create-index.html
+/// [2]: https://www.postgresql.org/docs/17/sql-createindex.html
+/// [3]: https://dev.mysql.com/doc/refman/8.3/en/create-table.html
+
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub struct IndexField {
+ pub expr: IndexExpr,
+ /// Optional `ASC` or `DESC`
+ pub asc: Option<bool>,
+}
+
+impl fmt::Display for IndexField {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{}", self.expr)?;
+ match self.asc {
+ Some(true) => write!(f, " ASC")?,
+ Some(false) => write!(f, " DESC")?,
+ None => (),
+ }
+ Ok(())
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub enum IndexExpr {
+ Column(Ident),
+ /// Mysql specific syntax
+ ///
+ /// See [Mysql documentation](https://dev.mysql.com/doc/refman/8.3/en/create-index.html)
+ /// for more details.
+ ColumnPrefix {
+ column: Ident,
+ length: u64,
+ },
+ Functional(Box<Expr>),
+}
Review Comment:
would it be possible to replace this with an `Expr` instead? The docs seems to suggest that arbitrary expressions are allowed, from the docs
```sql
CREATE TABLE t1 (col1 INT, col2 INT, INDEX func_index ((ABS(col1))));
CREATE INDEX idx1 ON t1 ((col1 + col2));
CREATE INDEX idx2 ON t1 ((col1 + col2), (col1 - col2), col1);
ALTER TABLE t1 ADD INDEX ((col1 * 40) DESC);
```
this would let us reuse existing code and representation, maybe even replace `IndexField` with `OrderByExpr`
--
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] Add support column prefix index for MySQL [datafusion-sqlparser-rs]
Posted by "iffyio (via GitHub)" <gi...@apache.org>.
iffyio commented on code in PR #1732:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/1732#discussion_r1969053806
##########
src/parser/mod.rs:
##########
@@ -7646,6 +7646,39 @@ impl<'a> Parser<'a> {
}
}
+ pub fn parse_index_exprs(&mut self) -> Result<Vec<IndexExpr>, ParserError> {
+ self.parse_parenthesized(|p| p.parse_comma_separated(Parser::parse_index_expr))
+ }
+
+ pub fn parse_index_expr(&mut self) -> Result<IndexExpr, ParserError> {
+ let expr = self.parse_expr()?;
+ let collation = if self.parse_keyword(Keyword::COLLATE) {
+ Some(self.parse_object_name(false)?)
+ } else {
+ None
+ };
+
+ let (operator_class, order_options) = if self.peek_keyword(Keyword::ASC)
+ || self.peek_keyword(Keyword::DESC)
+ || self.peek_keyword(Keyword::NULLS)
+ {
+ let order_options = self.parse_order_by_options()?;
+ (None, order_options)
+ } else {
+ let operator_class = self.maybe_parse(|p| p.parse_expr())?;
Review Comment:
hmm I couldn't see `operator_class` being used in the introduced tests or documented so its unclear what the syntax is?
##########
src/parser/mod.rs:
##########
@@ -7646,6 +7646,39 @@ impl<'a> Parser<'a> {
}
}
+ pub fn parse_index_exprs(&mut self) -> Result<Vec<IndexExpr>, ParserError> {
+ self.parse_parenthesized(|p| p.parse_comma_separated(Parser::parse_index_expr))
+ }
+
+ pub fn parse_index_expr(&mut self) -> Result<IndexExpr, ParserError> {
Review Comment:
Since this is flagged as a public function can we add a short description mentioning what it does?
##########
src/parser/mod.rs:
##########
@@ -7646,6 +7646,39 @@ impl<'a> Parser<'a> {
}
}
+ pub fn parse_index_exprs(&mut self) -> Result<Vec<IndexExpr>, ParserError> {
+ self.parse_parenthesized(|p| p.parse_comma_separated(Parser::parse_index_expr))
+ }
+
+ pub fn parse_index_expr(&mut self) -> Result<IndexExpr, ParserError> {
+ let expr = self.parse_expr()?;
+ let collation = if self.parse_keyword(Keyword::COLLATE) {
+ Some(self.parse_object_name(false)?)
+ } else {
+ None
+ };
+
+ let (operator_class, order_options) = if self.peek_keyword(Keyword::ASC)
+ || self.peek_keyword(Keyword::DESC)
+ || self.peek_keyword(Keyword::NULLS)
Review Comment:
can we introduce a test case that covers the `ASC/DESC` and `nulls_first` syntax?
##########
src/parser/mod.rs:
##########
@@ -14688,6 +14721,8 @@ impl Word {
#[cfg(test)]
mod tests {
+ use std::vec;
+
Review Comment:
hmm is this needed?
##########
src/parser/mod.rs:
##########
@@ -7646,6 +7646,39 @@ impl<'a> Parser<'a> {
}
}
+ pub fn parse_index_exprs(&mut self) -> Result<Vec<IndexExpr>, ParserError> {
+ self.parse_parenthesized(|p| p.parse_comma_separated(Parser::parse_index_expr))
+ }
+
+ pub fn parse_index_expr(&mut self) -> Result<IndexExpr, ParserError> {
+ let expr = self.parse_expr()?;
+ let collation = if self.parse_keyword(Keyword::COLLATE) {
+ Some(self.parse_object_name(false)?)
+ } else {
+ None
+ };
+
+ let (operator_class, order_options) = if self.peek_keyword(Keyword::ASC)
+ || self.peek_keyword(Keyword::DESC)
+ || self.peek_keyword(Keyword::NULLS)
Review Comment:
Instead of using `peek`, we can probably use `self.maybe_parse()` so that we don't need to maintain this list of keywords in sync with the `parse_order_by_options` function e.g.
```rust
let (operator_class, order_options) = if let Some(order_options ) = self.maybe_parse(|p| p.parse_order_by_options())? {
(None, order_options)
} else {
let operator_class = self.maybe_parse(|p| p.parse_expr())?;
let order_options = self.parse_order_by_options()?;
(operator_class, order_options)
};
```
--
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] Add support column prefix index for MySQL [datafusion-sqlparser-rs]
Posted by "zzzdong (via GitHub)" <gi...@apache.org>.
zzzdong commented on PR #1732:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/1732#issuecomment-2680992482
Considering the work on the index parsing in #1707, this will be closed.
--
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