Skip to content

Commit

Permalink
fix failed to parse table ddl when create fulltext index with parser
Browse files Browse the repository at this point in the history
  • Loading branch information
PeachThinking committed Jan 10, 2025
1 parent 942f328 commit f0c2c0a
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,21 @@ private void parseDdlToSetIndexInfo(String ddl, List<DBTableIndex> indexList) {
fillWarning(indexList, DBObjectType.INDEX, "table ddl is blank, can not set index range by parse ddl");
return;
}
ParseSqlResult result = SqlParser.parseMysql(ddl);
if (CollectionUtils.isEmpty(result.getIndexes())) {
fillWarning(indexList, DBObjectType.INDEX, "parse index DDL failed");
} else {
indexList.forEach(index -> result.getIndexes().forEach(dbIndex -> {
if (StringUtils.equals(index.getName(), dbIndex.getName())) {
index.setGlobal("GLOBAL".equalsIgnoreCase(dbIndex.getRange().name()));
index.setColumnGroups(dbIndex.getColumnGroups());
}
}));
try {
ParseSqlResult result = SqlParser.parseMysql(ddl);
if (CollectionUtils.isEmpty(result.getIndexes())) {
fillWarning(indexList, DBObjectType.INDEX, "parse index DDL failed");
} else {
indexList.forEach(index -> result.getIndexes().forEach(dbIndex -> {
if (StringUtils.equals(index.getName(), dbIndex.getName())) {
index.setGlobal("GLOBAL".equalsIgnoreCase(dbIndex.getRange().name()));
index.setColumnGroups(dbIndex.getColumnGroups());
}
}));
}
} catch (Exception e) {
fillWarning(indexList, DBObjectType.INDEX, "failed to set index info by parse ddl");
log.warn("failed to set index info by parse ddl:{}", ddl, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public IndexOptions visitOpt_index_options(Opt_index_optionsContext ctx) {
} else if (option.CTXCAT() != null) {
indexOptions.setCtxcat(getReference(option));
} else if (option.WITH() != null && option.PARSER() != null) {
indexOptions.setWithParser(option.STRING_VALUE().getText());
indexOptions.setWithParser(option.index_with_parser().getText());
} else if (option.WITH() != null && option.ROWID() != null) {
indexOptions.setWithRowId(true);
} else if (option.WITH() != null && option.vec_index_params() != null) {
Expand Down
12 changes: 12 additions & 0 deletions libs/ob-sql-parser/src/main/resources/obmysql/sql/OBLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,18 @@ ROWID
: R O W I D
;

SPACE
: S P A C E
;

NGRAM
: N G R A M
;

BENG
: B E N G
;

DEFAULT
: ( D E F A U L T )
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1861,13 +1861,17 @@ index_option
| COMMENT STRING_VALUE
| (STORING|CTXCAT) LeftParen column_name_list RightParen
| WITH ROWID
| WITH PARSER STRING_VALUE
| WITH PARSER index_with_parser
| WITH LeftParen vec_index_params RightParen
| index_using_algorithm
| visibility_option
| parallel_option
;

index_with_parser
: SPACE | NGRAM | BENG
;

index_using_algorithm
: USING (BTREE|HASH)
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ public void generate_columnDefCheckConstraint1_generateSuccees() {
@Test
public void generate_indexBtree_succeed() {
StatementFactory<TableElement> factory = new MySQLTableElementFactory(
getTableElementContext("index idx_name using btree (col, col1) global with parser 'aaaa'"));
getTableElementContext("index idx_name using btree (col, col1) global with parser ngram"));
OutOfLineIndex actual = (OutOfLineIndex) factory.generate();

SortColumn s1 = new SortColumn(new ColumnReference(null, null, "col"));
Expand All @@ -702,7 +702,7 @@ public void generate_indexBtree_succeed() {
IndexOptions indexOptions = new IndexOptions();
indexOptions.setUsingBtree(true);
indexOptions.setGlobal(true);
indexOptions.setWithParser("'aaaa'");
indexOptions.setWithParser("ngram");
expect.setIndexOptions(indexOptions);
Assert.assertEquals(expect, actual);
}
Expand All @@ -711,7 +711,7 @@ public void generate_indexBtree_succeed() {
public void generate_exprIndexBtree_succeed() {
StatementFactory<TableElement> factory = new MySQLTableElementFactory(
getTableElementContext(
"index idx_name using btree ((CASE a WHEN 1 THEN 11 WHEN 2 THEN 22 ELSE 33 END)) global with parser 'aaaa'"));
"index idx_name using btree ((CASE a WHEN 1 THEN 11 WHEN 2 THEN 22 ELSE 33 END)) global with parser beng"));
OutOfLineIndex actual = (OutOfLineIndex) factory.generate();

List<WhenClause> whenClauses = new ArrayList<>();
Expand All @@ -725,7 +725,7 @@ public void generate_exprIndexBtree_succeed() {
IndexOptions indexOptions = new IndexOptions();
indexOptions.setUsingBtree(true);
indexOptions.setGlobal(true);
indexOptions.setWithParser("'aaaa'");
indexOptions.setWithParser("beng");
expect.setIndexOptions(indexOptions);
Assert.assertEquals(expect, actual);
}
Expand Down Expand Up @@ -911,7 +911,7 @@ public void generate_indexPrimaryKeyNoyAlgorithmAndComment_succeed() {
public void generate_uniqueIndexColumnAscId_succeed() {
StatementFactory<TableElement> factory = new MySQLTableElementFactory(
getTableElementContext(
"unique index idx_name using btree (col asc id 16, col1) global with parser 'aaaa'"));
"unique index idx_name using btree (col asc id 16, col1) global with parser space"));
TableElement actual = factory.generate();

SortColumn s1 = new SortColumn(new ColumnReference(null, null, "col"));
Expand All @@ -922,7 +922,7 @@ public void generate_uniqueIndexColumnAscId_succeed() {
IndexOptions indexOptions = new IndexOptions();
indexOptions.setUsingBtree(true);
indexOptions.setGlobal(true);
indexOptions.setWithParser("'aaaa'");
indexOptions.setWithParser("space");
state.setIndexOptions(indexOptions);
OutOfLineConstraint expect = new OutOfLineConstraint(state, Arrays.asList(s1, s2));
expect.setUniqueKey(true);
Expand Down Expand Up @@ -977,7 +977,7 @@ public void generate_uniqueIndexAutoPartition_succeed() {
@Test
public void generate_uniqueIndexColumnAscIdNoIndexOps_succeed() {
StatementFactory<TableElement> factory = new MySQLTableElementFactory(
getTableElementContext("unique index idx_name (col asc id 16, col1) global with parser 'aaaa'"));
getTableElementContext("unique index idx_name (col asc id 16, col1) global with parser space"));
TableElement actual = factory.generate();

SortColumn s1 = new SortColumn(new ColumnReference(null, null, "col"));
Expand All @@ -987,7 +987,7 @@ public void generate_uniqueIndexColumnAscIdNoIndexOps_succeed() {
ConstraintState state = new ConstraintState();
IndexOptions indexOptions = new IndexOptions();
indexOptions.setGlobal(true);
indexOptions.setWithParser("'aaaa'");
indexOptions.setWithParser("space");
state.setIndexOptions(indexOptions);
OutOfLineConstraint expect = new OutOfLineConstraint(state, Arrays.asList(s1, s2));
expect.setUniqueKey(true);
Expand Down Expand Up @@ -1174,6 +1174,22 @@ public void generate_outOfLineIndexVectorIndex_succeed() {
Assert.assertEquals(expected, actual);
}

@Test
public void parseMysql_createFullTextIndex_getWithParserSucceed(){
StatementFactory<TableElement> factory = new MySQLTableElementFactory(
getTableElementContext("FULLTEXT KEY idx1 (name) WITH PARSER space"));
TableElement actual = factory.generate();

OutOfLineIndex expected = new OutOfLineIndex("idx1",
Collections.singletonList(new SortColumn(new ColumnReference(null, null, "name"))));
expected.setFullText(true);
IndexOptions indexOptions = new IndexOptions();
indexOptions.setWithParser("space");
expected.setIndexOptions(indexOptions);

Assert.assertEquals(expected, actual);
}

private Table_elementContext getTableElementContext(String str) {
OBLexer lexer = new OBLexer(CharStreams.fromString(str));
CommonTokenStream tokens = new CommonTokenStream(lexer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public void generate_columnDefForeignConstraintDeleteCascade_generateSuccees() {
public void generate_columnDefForeignConstraintDeleteSetNull_generateSuccees() {
StatementFactory<TableElement> factory = new OracleTableElementFactory(
getTableElementContext(
"tb.col varchar2(64) references a.b(a,b,c) on delete set null rely using index with parser 'aaa' using btree enable novalidate"));
"tb.col varchar2(64) references a.b(a,b,c) on delete set null rely using index with parser space using btree enable novalidate"));
ColumnDefinition actual = (ColumnDefinition) factory.generate();

DataType dataType = new CharacterType("varchar2", new BigDecimal("64"));
Expand All @@ -334,7 +334,7 @@ public void generate_columnDefForeignConstraintDeleteSetNull_generateSuccees() {
state.setRely(true);
state.setUsingIndexFlag(true);
IndexOptions indexOptions = new IndexOptions();
indexOptions.setWithParser("'aaa'");
indexOptions.setWithParser("space");
indexOptions.setUsingBtree(true);
state.setIndexOptions(indexOptions);
state.setEnable(true);
Expand Down Expand Up @@ -815,7 +815,7 @@ public void generate_indexPrimaryKeyNoyAlgorithmAndComment_succeed() {
public void generate_uniqueIndexColumnAscId_succeed() {
StatementFactory<TableElement> factory = new OracleTableElementFactory(
getTableElementContext(
"unique (col asc id 16, col1) using index using btree global with parser 'aaaa'"));
"unique (col asc id 16, col1) using index using btree global with parser space"));
OutOfLineConstraint actual = (OutOfLineConstraint) factory.generate();

SortColumn s1 = new SortColumn(new RelationReference("col", null));
Expand All @@ -826,7 +826,7 @@ public void generate_uniqueIndexColumnAscId_succeed() {
IndexOptions indexOptions = new IndexOptions();
indexOptions.setUsingBtree(true);
indexOptions.setGlobal(true);
indexOptions.setWithParser("'aaaa'");
indexOptions.setWithParser("space");
state.setIndexOptions(indexOptions);
state.setUsingIndexFlag(true);
OutOfLineConstraint expect = new OutOfLineConstraint(state, Arrays.asList(s1, s2));
Expand All @@ -845,7 +845,7 @@ public void generate_uniqueIndexWithoutUsingIndex_wrong() {
public void generate_foreignKey_succeed() {
StatementFactory<TableElement> factory = new OracleTableElementFactory(
getTableElementContext(
"constraint fk_name foreign key (col, col1) references tb_name (col2, col3) on delete cascade using index using btree global with parser 'aaaa'"));
"constraint fk_name foreign key (col, col1) references tb_name (col2, col3) on delete cascade using index using btree global with parser space"));
TableElement actual = factory.generate();

SortColumn s1 = new SortColumn(new ColumnReference(null, null, "col"));
Expand All @@ -858,7 +858,7 @@ public void generate_foreignKey_succeed() {
IndexOptions indexOptions = new IndexOptions();
indexOptions.setUsingBtree(true);
indexOptions.setGlobal(true);
indexOptions.setWithParser("'aaaa'");
indexOptions.setWithParser("space");
state.setIndexOptions(indexOptions);
state.setUsingIndexFlag(true);
OutOfLineForeignConstraint expect = new OutOfLineForeignConstraint(state, Arrays.asList(s1, s2), reference);
Expand All @@ -870,7 +870,7 @@ public void generate_foreignKey_succeed() {
public void generate_checkKey_succeed() {
StatementFactory<TableElement> factory = new OracleTableElementFactory(
getTableElementContext(
"constraint ck_name check(col > 12) using index using btree global with parser 'aaaa'"));
"constraint ck_name check(col > 12) using index using btree global with parser space"));
TableElement actual = factory.generate();

RelationReference r = new RelationReference("col", null);
Expand All @@ -880,7 +880,7 @@ public void generate_checkKey_succeed() {
IndexOptions indexOptions = new IndexOptions();
indexOptions.setUsingBtree(true);
indexOptions.setGlobal(true);
indexOptions.setWithParser("'aaaa'");
indexOptions.setWithParser("space");
state.setIndexOptions(indexOptions);
state.setUsingIndexFlag(true);
OutOfLineCheckConstraint expect = new OutOfLineCheckConstraint(state, checkExpr);
Expand Down

0 comments on commit f0c2c0a

Please sign in to comment.