diff --git a/libs/db-browser/src/main/java/com/oceanbase/tools/dbbrowser/schema/mysql/OBMySQLSchemaAccessor.java b/libs/db-browser/src/main/java/com/oceanbase/tools/dbbrowser/schema/mysql/OBMySQLSchemaAccessor.java index a9b9f9a4e1..bc5720e892 100644 --- a/libs/db-browser/src/main/java/com/oceanbase/tools/dbbrowser/schema/mysql/OBMySQLSchemaAccessor.java +++ b/libs/db-browser/src/main/java/com/oceanbase/tools/dbbrowser/schema/mysql/OBMySQLSchemaAccessor.java @@ -289,16 +289,21 @@ private void parseDdlToSetIndexInfo(String ddl, List 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); } } diff --git a/libs/ob-sql-parser/src/main/java/com/oceanbase/tools/sqlparser/adapter/mysql/MySQLIndexOptionsFactory.java b/libs/ob-sql-parser/src/main/java/com/oceanbase/tools/sqlparser/adapter/mysql/MySQLIndexOptionsFactory.java index 2888e4a780..0dae7c8e03 100644 --- a/libs/ob-sql-parser/src/main/java/com/oceanbase/tools/sqlparser/adapter/mysql/MySQLIndexOptionsFactory.java +++ b/libs/ob-sql-parser/src/main/java/com/oceanbase/tools/sqlparser/adapter/mysql/MySQLIndexOptionsFactory.java @@ -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) { diff --git a/libs/ob-sql-parser/src/main/resources/obmysql/sql/OBLexer.g4 b/libs/ob-sql-parser/src/main/resources/obmysql/sql/OBLexer.g4 index 2647278a61..872c6e5dd0 100644 --- a/libs/ob-sql-parser/src/main/resources/obmysql/sql/OBLexer.g4 +++ b/libs/ob-sql-parser/src/main/resources/obmysql/sql/OBLexer.g4 @@ -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 ) ; diff --git a/libs/ob-sql-parser/src/main/resources/obmysql/sql/OBParser.g4 b/libs/ob-sql-parser/src/main/resources/obmysql/sql/OBParser.g4 index 472619c695..a659aafcd6 100644 --- a/libs/ob-sql-parser/src/main/resources/obmysql/sql/OBParser.g4 +++ b/libs/ob-sql-parser/src/main/resources/obmysql/sql/OBParser.g4 @@ -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) ; diff --git a/libs/ob-sql-parser/src/test/java/com/oceanbase/tools/sqlparser/adapter/MySQLTableElementFactoryTest.java b/libs/ob-sql-parser/src/test/java/com/oceanbase/tools/sqlparser/adapter/MySQLTableElementFactoryTest.java index aae65c06fd..9d09d55526 100644 --- a/libs/ob-sql-parser/src/test/java/com/oceanbase/tools/sqlparser/adapter/MySQLTableElementFactoryTest.java +++ b/libs/ob-sql-parser/src/test/java/com/oceanbase/tools/sqlparser/adapter/MySQLTableElementFactoryTest.java @@ -693,7 +693,7 @@ public void generate_columnDefCheckConstraint1_generateSuccees() { @Test public void generate_indexBtree_succeed() { StatementFactory 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")); @@ -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); } @@ -711,7 +711,7 @@ public void generate_indexBtree_succeed() { public void generate_exprIndexBtree_succeed() { StatementFactory 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 whenClauses = new ArrayList<>(); @@ -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); } @@ -911,7 +911,7 @@ public void generate_indexPrimaryKeyNoyAlgorithmAndComment_succeed() { public void generate_uniqueIndexColumnAscId_succeed() { StatementFactory 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")); @@ -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); @@ -977,7 +977,7 @@ public void generate_uniqueIndexAutoPartition_succeed() { @Test public void generate_uniqueIndexColumnAscIdNoIndexOps_succeed() { StatementFactory 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")); @@ -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); @@ -1174,6 +1174,22 @@ public void generate_outOfLineIndexVectorIndex_succeed() { Assert.assertEquals(expected, actual); } + @Test + public void parseMysql_createFullTextIndex_getWithParserSucceed(){ + StatementFactory 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); diff --git a/libs/ob-sql-parser/src/test/java/com/oceanbase/tools/sqlparser/adapter/OracleTableElementFactoryTest.java b/libs/ob-sql-parser/src/test/java/com/oceanbase/tools/sqlparser/adapter/OracleTableElementFactoryTest.java index b1829da72f..c2a757462b 100644 --- a/libs/ob-sql-parser/src/test/java/com/oceanbase/tools/sqlparser/adapter/OracleTableElementFactoryTest.java +++ b/libs/ob-sql-parser/src/test/java/com/oceanbase/tools/sqlparser/adapter/OracleTableElementFactoryTest.java @@ -324,7 +324,7 @@ public void generate_columnDefForeignConstraintDeleteCascade_generateSuccees() { public void generate_columnDefForeignConstraintDeleteSetNull_generateSuccees() { StatementFactory 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")); @@ -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); @@ -815,7 +815,7 @@ public void generate_indexPrimaryKeyNoyAlgorithmAndComment_succeed() { public void generate_uniqueIndexColumnAscId_succeed() { StatementFactory 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)); @@ -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)); @@ -845,7 +845,7 @@ public void generate_uniqueIndexWithoutUsingIndex_wrong() { public void generate_foreignKey_succeed() { StatementFactory 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")); @@ -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); @@ -870,7 +870,7 @@ public void generate_foreignKey_succeed() { public void generate_checkKey_succeed() { StatementFactory 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); @@ -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);