Skip to content

Commit

Permalink
Support USING method when creating indexes. (#731)
Browse files Browse the repository at this point in the history
* fix: create index using function

* fix: code style

Co-authored-by: yangjiaxin <yangjiaxin@qianxin.com>
  • Loading branch information
step-baby and yangjiaxin01 committed Nov 30, 2022
1 parent 7101e00 commit 3163597
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/ast/mod.rs
Expand Up @@ -1192,6 +1192,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 @@ -2115,18 +2116,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 @@ -2749,12 +2749,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() {
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 @@ -5162,9 +5162,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

0 comments on commit 3163597

Please sign in to comment.