Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[USING method] is supported when creating indexes. #731

Merged
merged 2 commits into from Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/ast/mod.rs
Expand Up @@ -1157,6 +1157,7 @@ pub enum Statement {
/// index name
name: ObjectName,
table_name: ObjectName,
using: Option<Ident>,
columns: Vec<OrderByExpr>,
unique: bool,
if_not_exists: bool,
Expand Down Expand Up @@ -2075,18 +2076,24 @@ impl fmt::Display for Statement {
Statement::CreateIndex {
name,
table_name,
using,
columns,
unique,
if_not_exists,
} => write!(
f,
"CREATE {unique}INDEX {if_not_exists}{name} ON {table_name}({columns})",
unique = if *unique { "UNIQUE " } else { "" },
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
name = name,
table_name = table_name,
columns = display_separated(columns, ",")
),
} => {
write!(
f,
"CREATE {unique}INDEX {if_not_exists}{name} ON {table_name}",
unique = if *unique { "UNIQUE " } else { "" },
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
name = name,
table_name = table_name
)?;
if let Some(value) = using {
write!(f, " USING {} ", value)?;
}
write!(f, "({})", display_separated(columns, ","))
}
Statement::CreateRole {
names,
if_not_exists,
Expand Down
6 changes: 6 additions & 0 deletions src/parser.rs
Expand Up @@ -2695,12 +2695,18 @@ impl<'a> Parser<'a> {
let index_name = self.parse_object_name()?;
self.expect_keyword(Keyword::ON)?;
let table_name = self.parse_object_name()?;
let using = if self.expect_keyword(Keyword::USING).is_ok() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can avoid is_ok: here #731

Some(self.parse_identifier()?)
} else {
None
};
self.expect_token(&Token::LParen)?;
let columns = self.parse_comma_separated(Parser::parse_order_by_expr)?;
self.expect_token(&Token::RParen)?;
Ok(Statement::CreateIndex {
name: index_name,
table_name,
using,
columns,
unique,
if_not_exists,
Expand Down
36 changes: 36 additions & 0 deletions tests/sqlparser_common.rs
Expand Up @@ -5052,9 +5052,45 @@ fn parse_create_index() {
columns,
unique,
if_not_exists,
..
} => {
assert_eq!("idx_name", name.to_string());
assert_eq!("test", table_name.to_string());
assert_eq!(indexed_columns, columns);
assert!(unique);
assert!(if_not_exists)
}
_ => unreachable!(),
}
}

#[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 indexed_columns = vec![
OrderByExpr {
expr: Expr::Identifier(Ident::new("name")),
asc: None,
nulls_first: None,
},
OrderByExpr {
expr: Expr::Identifier(Ident::new("age")),
asc: Some(false),
nulls_first: None,
},
];
match verified_stmt(sql) {
Statement::CreateIndex {
name,
table_name,
using,
columns,
unique,
if_not_exists,
} => {
assert_eq!("idx_name", name.to_string());
assert_eq!("test", table_name.to_string());
assert_eq!("btree", using.unwrap().to_string());
assert_eq!(indexed_columns, columns);
assert!(unique);
assert!(if_not_exists)
Expand Down