Skip to content

Commit 43a114c

Browse files
merge test cases for USING in CREATE INDEX
1 parent c6a3910 commit 43a114c

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

src/ast/ddl.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,6 +2361,8 @@ pub struct CreateIndex {
23612361
pub name: Option<ObjectName>,
23622362
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
23632363
pub table_name: ObjectName,
2364+
/// Index type used in the statement. Can also be found inside [`CreateIndex::index_options`]
2365+
/// depending on the position of the option within the statement.
23642366
pub using: Option<IndexType>,
23652367
pub columns: Vec<IndexColumn>,
23662368
pub unique: bool,

src/parser/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7123,6 +7123,13 @@ impl<'a> Parser<'a> {
71237123
// parse it anyway (as we do inside `ALTER TABLE` and `CREATE TABLE` parsing).
71247124
let index_options = self.parse_index_options()?;
71257125

7126+
if index_options
7127+
.iter()
7128+
.any(|opt| matches!(opt, IndexOption::Using(_)))
7129+
{
7130+
using = None;
7131+
};
7132+
71267133
// MySQL allows `ALGORITHM` and `LOCK` options. Unlike in `ALTER TABLE`, they need not be comma separated.
71277134
let mut alter_options = Vec::new();
71287135
while self

tests/sqlparser_common.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17248,10 +17248,10 @@ fn parse_invisible_column() {
1724817248
}
1724917249

1725017250
#[test]
17251-
fn parse_create_index_using_before_on() {
17251+
fn parse_create_index_different_using_positions() {
1725217252
let sql = "CREATE INDEX idx_name USING BTREE ON table_name (col1)";
17253-
// Can't use `verified_stmt` here as the USING will be placed after the `ON` clause
17254-
match all_dialects().parse_sql_statements(sql).unwrap()[0].clone() {
17253+
let expected = "CREATE INDEX idx_name ON table_name USING BTREE (col1)";
17254+
match all_dialects().one_statement_parses_to(sql, expected) {
1725517255
Statement::CreateIndex(CreateIndex {
1725617256
name,
1725717257
table_name,
@@ -17268,24 +17268,23 @@ fn parse_create_index_using_before_on() {
1726817268
}
1726917269
_ => unreachable!(),
1727017270
}
17271-
}
1727217271

17273-
#[test]
17274-
fn parse_create_index_using_multiple_clauses() {
17275-
let sql = "CREATE INDEX idx_name USING BTREE ON table_name USING HASH (col1)";
17276-
// Can't use `verified_stmt` here as the first USING will be ignored
17277-
match all_dialects().parse_sql_statements(sql).unwrap()[0].clone() {
17272+
let sql = "CREATE INDEX idx_name USING BTREE ON table_name (col1) USING HASH";
17273+
let expected = "CREATE INDEX idx_name ON table_name(col1) USING HASH";
17274+
match all_dialects().one_statement_parses_to(sql, expected) {
1727817275
Statement::CreateIndex(CreateIndex {
1727917276
name,
1728017277
table_name,
17281-
using,
1728217278
columns,
17279+
index_options,
1728317280
..
1728417281
}) => {
1728517282
assert_eq!(name.unwrap().to_string(), "idx_name");
1728617283
assert_eq!(table_name.to_string(), "table_name");
17287-
assert_eq!(using, Some(IndexType::Hash));
1728817284
assert_eq!(columns.len(), 1);
17285+
assert!(index_options
17286+
.iter()
17287+
.any(|o| o == &IndexOption::Using(IndexType::Hash)));
1728917288
}
1729017289
_ => unreachable!(),
1729117290
}

0 commit comments

Comments
 (0)