Skip to content

Commit 3163597

Browse files
Support USING method when creating indexes. (#731)
* fix: create index using function * fix: code style Co-authored-by: yangjiaxin <yangjiaxin@qianxin.com>
1 parent 7101e00 commit 3163597

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

src/ast/mod.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,7 @@ pub enum Statement {
11921192
/// index name
11931193
name: ObjectName,
11941194
table_name: ObjectName,
1195+
using: Option<Ident>,
11951196
columns: Vec<OrderByExpr>,
11961197
unique: bool,
11971198
if_not_exists: bool,
@@ -2115,18 +2116,24 @@ impl fmt::Display for Statement {
21152116
Statement::CreateIndex {
21162117
name,
21172118
table_name,
2119+
using,
21182120
columns,
21192121
unique,
21202122
if_not_exists,
2121-
} => write!(
2122-
f,
2123-
"CREATE {unique}INDEX {if_not_exists}{name} ON {table_name}({columns})",
2124-
unique = if *unique { "UNIQUE " } else { "" },
2125-
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
2126-
name = name,
2127-
table_name = table_name,
2128-
columns = display_separated(columns, ",")
2129-
),
2123+
} => {
2124+
write!(
2125+
f,
2126+
"CREATE {unique}INDEX {if_not_exists}{name} ON {table_name}",
2127+
unique = if *unique { "UNIQUE " } else { "" },
2128+
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
2129+
name = name,
2130+
table_name = table_name
2131+
)?;
2132+
if let Some(value) = using {
2133+
write!(f, " USING {} ", value)?;
2134+
}
2135+
write!(f, "({})", display_separated(columns, ","))
2136+
}
21302137
Statement::CreateRole {
21312138
names,
21322139
if_not_exists,

src/parser.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2749,12 +2749,18 @@ impl<'a> Parser<'a> {
27492749
let index_name = self.parse_object_name()?;
27502750
self.expect_keyword(Keyword::ON)?;
27512751
let table_name = self.parse_object_name()?;
2752+
let using = if self.expect_keyword(Keyword::USING).is_ok() {
2753+
Some(self.parse_identifier()?)
2754+
} else {
2755+
None
2756+
};
27522757
self.expect_token(&Token::LParen)?;
27532758
let columns = self.parse_comma_separated(Parser::parse_order_by_expr)?;
27542759
self.expect_token(&Token::RParen)?;
27552760
Ok(Statement::CreateIndex {
27562761
name: index_name,
27572762
table_name,
2763+
using,
27582764
columns,
27592765
unique,
27602766
if_not_exists,

tests/sqlparser_common.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5162,9 +5162,45 @@ fn parse_create_index() {
51625162
columns,
51635163
unique,
51645164
if_not_exists,
5165+
..
5166+
} => {
5167+
assert_eq!("idx_name", name.to_string());
5168+
assert_eq!("test", table_name.to_string());
5169+
assert_eq!(indexed_columns, columns);
5170+
assert!(unique);
5171+
assert!(if_not_exists)
5172+
}
5173+
_ => unreachable!(),
5174+
}
5175+
}
5176+
5177+
#[test]
5178+
fn test_create_index_with_using_function() {
5179+
let sql = "CREATE UNIQUE INDEX IF NOT EXISTS idx_name ON test USING btree (name,age DESC)";
5180+
let indexed_columns = vec![
5181+
OrderByExpr {
5182+
expr: Expr::Identifier(Ident::new("name")),
5183+
asc: None,
5184+
nulls_first: None,
5185+
},
5186+
OrderByExpr {
5187+
expr: Expr::Identifier(Ident::new("age")),
5188+
asc: Some(false),
5189+
nulls_first: None,
5190+
},
5191+
];
5192+
match verified_stmt(sql) {
5193+
Statement::CreateIndex {
5194+
name,
5195+
table_name,
5196+
using,
5197+
columns,
5198+
unique,
5199+
if_not_exists,
51655200
} => {
51665201
assert_eq!("idx_name", name.to_string());
51675202
assert_eq!("test", table_name.to_string());
5203+
assert_eq!("btree", using.unwrap().to_string());
51685204
assert_eq!(indexed_columns, columns);
51695205
assert!(unique);
51705206
assert!(if_not_exists)

0 commit comments

Comments
 (0)