[Refactor] Refactor USE CATALOG, SET CATALOG, and DROP CATALOG statements to sql-parser (#63616)

This commit is contained in:
Harbor Liu 2025-09-28 11:54:33 +08:00 committed by GitHub
parent 7d64a5c43e
commit 2754e0d05d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 50 additions and 76 deletions

View File

@ -36,10 +36,6 @@ import static com.starrocks.server.CatalogMgr.ResourceMappingCatalog.isResourceM
import static com.starrocks.sql.ast.CreateCatalogStmt.TYPE;
public class CatalogAnalyzer {
private static final String CATALOG = "CATALOG";
private static final String WHITESPACE = "\\s+";
private static final Set<String> NOT_SUPPORT_ALTER_PROPERTIES = Sets.newHashSet(
"type"
);
@ -99,18 +95,7 @@ public class CatalogAnalyzer {
@Override
public Void visitUseCatalogStatement(UseCatalogStmt statement, ConnectContext context) {
if (Strings.isNullOrEmpty(statement.getCatalogParts())) {
throw new SemanticException("You have an error in your SQL. The correct syntax is: USE 'CATALOG catalog_name'.");
}
String[] splitParts = statement.getCatalogParts().split(WHITESPACE);
if (!splitParts[0].equalsIgnoreCase(CATALOG) || splitParts.length != 2) {
throw new SemanticException("You have an error in your SQL. The correct syntax is: USE 'CATALOG catalog_name'.");
}
FeNameFormat.checkCatalogName(splitParts[1]);
statement.setCatalogName(splitParts[1]);
FeNameFormat.checkCatalogName(statement.getCatalogName());
return null;
}

View File

@ -303,23 +303,10 @@ public interface AstVisitorExtendInterface<R, C> extends AstVisitor<R, C> {
return visitDDLStatement(statement, context);
}
default R visitDropCatalogStatement(DropCatalogStmt statement, C context) {
return visitDDLStatement(statement, context);
}
default R visitShowCreateExternalCatalogStatement(ShowCreateExternalCatalogStmt statement, C context) {
return visitShowStatement(statement, context);
}
default R visitUseCatalogStatement(UseCatalogStmt statement, C context) {
return visitStatement(statement, context);
}
default R visitSetCatalogStatement(SetCatalogStmt statement, C context) {
return visitStatement(statement, context);
}
default R visitAlterCatalogStatement(AlterCatalogStmt statement, C context) {
return visitDDLStatement(statement, context);
}

View File

@ -676,14 +676,29 @@ public class AstBuilder extends com.starrocks.sql.parser.StarRocksBaseVisitor<Pa
@Override
public ParseNode visitUseCatalogStatement(com.starrocks.sql.parser.StarRocksParser.UseCatalogStatementContext context) {
StringLiteral literal = (StringLiteral) visit(context.string());
return new UseCatalogStmt(literal.getValue(), createPos(context));
String catalogParts = literal.getValue();
// Parse and validate catalog name from the string literal
if (catalogParts == null || catalogParts.trim().isEmpty()) {
throw new ParsingException("You have an error in your SQL. The correct syntax is: USE 'CATALOG catalog_name'.",
createPos(context));
}
String[] splitParts = catalogParts.split("\\s+");
if (splitParts.length != 2 || !splitParts[0].equalsIgnoreCase("CATALOG")) {
throw new ParsingException("You have an error in your SQL. The correct syntax is: USE 'CATALOG catalog_name'.",
createPos(context));
}
String catalogName = normalizeName(splitParts[1]);
return new UseCatalogStmt(catalogName, createPos(context));
}
@Override
public ParseNode visitSetCatalogStatement(com.starrocks.sql.parser.StarRocksParser.SetCatalogStatementContext context) {
Identifier identifier = (Identifier) visit(context.identifierOrString());
String catalogName = identifier.getValue();
return new SetCatalogStmt(catalogName, createPos(context));
return new SetCatalogStmt(normalizeName(catalogName), createPos(context));
}
@Override
@ -2368,7 +2383,7 @@ public class AstBuilder extends com.starrocks.sql.parser.StarRocksBaseVisitor<Pa
Identifier identifier = (Identifier) visit(context.catalogName);
boolean ifExists = context.IF() != null;
String catalogName = identifier.getValue();
return new DropCatalogStmt(catalogName, ifExists, createPos(context));
return new DropCatalogStmt(normalizeName(catalogName), ifExists, createPos(context));
}
@Override
@ -6755,9 +6770,9 @@ public class AstBuilder extends com.starrocks.sql.parser.StarRocksBaseVisitor<Pa
}
@Override
public ParseNode visitGrantRoleToGroup(StarRocksParser.GrantRoleToGroupContext context) {
public ParseNode visitGrantRoleToGroup(com.starrocks.sql.parser.StarRocksParser.GrantRoleToGroupContext context) {
List<String> roleNameList = new ArrayList<>();
for (StarRocksParser.IdentifierOrStringContext oneContext : context.identifierOrStringList()
for (com.starrocks.sql.parser.StarRocksParser.IdentifierOrStringContext oneContext : context.identifierOrStringList()
.identifierOrString()) {
roleNameList.add(((Identifier) visit(oneContext)).getValue());
}
@ -6790,9 +6805,9 @@ public class AstBuilder extends com.starrocks.sql.parser.StarRocksBaseVisitor<Pa
}
@Override
public ParseNode visitRevokeRoleFromGroup(StarRocksParser.RevokeRoleFromGroupContext context) {
public ParseNode visitRevokeRoleFromGroup(com.starrocks.sql.parser.StarRocksParser.RevokeRoleFromGroupContext context) {
List<String> roleNameList = new ArrayList<>();
for (StarRocksParser.IdentifierOrStringContext oneContext : context.identifierOrStringList()
for (com.starrocks.sql.parser.StarRocksParser.IdentifierOrStringContext oneContext : context.identifierOrStringList()
.identifierOrString()) {
roleNameList.add(((Identifier) visit(oneContext)).getValue());
}

View File

@ -90,14 +90,12 @@ public class UseCatalogStmtTest {
Assertions.assertEquals("default_catalog", ctx.getCurrentCatalog());
executor = new StmtExecutor(ctx, SqlParser.parseSingleStatement(
"use 'xxx default_catalog'", ctx.getSessionVariable().getSqlMode()));
executor.execute();
Assertions.assertSame(ctx.getState().getStateType(), QueryState.MysqlStateType.ERR);
Assertions.assertThrows(com.starrocks.sql.parser.ParsingException.class, () ->
SqlParser.parseSingleStatement(
"use 'xxx default_catalog'", ctx.getSessionVariable().getSqlMode()));
executor = new StmtExecutor(ctx, SqlParser.parseSingleStatement(
"use 'catalog default_catalog xxx'", ctx.getSessionVariable().getSqlMode()));
executor.execute();
Assertions.assertSame(ctx.getState().getStateType(), QueryState.MysqlStateType.ERR);
Assertions.assertThrows(com.starrocks.sql.parser.ParsingException.class, () ->
SqlParser.parseSingleStatement(
"use 'catalog default_catalog xxx'", ctx.getSessionVariable().getSqlMode()));
}
}

View File

@ -315,7 +315,7 @@ public class RedirectStatusTest {
@Test
public void testUseCatalogStmt() {
UseCatalogStmt stmt = new UseCatalogStmt("test_catalog");
UseCatalogStmt stmt = new UseCatalogStmt("test_catalog", NodePosition.ZERO);
Assertions.assertEquals(RedirectStatus.NO_FORWARD, RedirectStatus.getRedirectStatus(stmt));
}

View File

@ -440,6 +440,18 @@ public interface AstVisitor<R, C> {
return visitStatement(statement, context);
}
default R visitUseCatalogStatement(UseCatalogStmt statement, C context) {
return visitStatement(statement, context);
}
default R visitSetCatalogStatement(SetCatalogStmt statement, C context) {
return visitStatement(statement, context);
}
default R visitDropCatalogStatement(DropCatalogStmt statement, C context) {
return visitDDLStatement(statement, context);
}
default R visitCreateDbStatement(CreateDbStmt statement, C context) {
return visitDDLStatement(statement, context);
}

View File

@ -17,7 +17,6 @@ package com.starrocks.sql.ast;
import com.starrocks.sql.parser.NodePosition;
import static com.starrocks.common.util.Util.normalizeName;
// ToDo(zhuodong): to support internal catalog in the future
public class DropCatalogStmt extends DdlStmt {
@ -36,7 +35,7 @@ public class DropCatalogStmt extends DdlStmt {
public DropCatalogStmt(String name, boolean ifExists, NodePosition pos) {
super(pos);
this.name = normalizeName(name);
this.name = name;
this.ifExists = ifExists;
}
@ -50,7 +49,7 @@ public class DropCatalogStmt extends DdlStmt {
@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
return ((AstVisitorExtendInterface<R, C>) visitor).visitDropCatalogStatement(this, context);
return visitor.visitDropCatalogStatement(this, context);
}
@Override

View File

@ -12,13 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.starrocks.sql.ast;
import com.starrocks.sql.parser.NodePosition;
import static com.starrocks.common.util.Util.normalizeName;
/*
Set catalog specified by catalog name
@ -30,16 +27,15 @@ public class SetCatalogStmt extends StatementBase {
public SetCatalogStmt(String catalogName, NodePosition pos) {
super(pos);
this.catalogName = normalizeName(catalogName);
this.catalogName = catalogName;
}
public String getCatalogName() {
return catalogName;
}
@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
return ((AstVisitorExtendInterface<R, C>) visitor).visitSetCatalogStatement(this, context);
return visitor.visitSetCatalogStatement(this, context);
}
}

View File

@ -12,13 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.starrocks.sql.ast;
import com.starrocks.sql.parser.NodePosition;
import static com.starrocks.common.util.Util.normalizeName;
/*
Use catalog specified by catalog name
@ -36,34 +33,19 @@ import static com.starrocks.common.util.Util.normalizeName;
use "CATALOG hive_metastore_catalog"
*/
public class UseCatalogStmt extends StatementBase {
private final String catalogParts;
private final String catalogName;
private String catalogName;
public UseCatalogStmt(String catalogParts) {
this(catalogParts, NodePosition.ZERO);
}
public UseCatalogStmt(String catalogParts, NodePosition pos) {
public UseCatalogStmt(String catalogName, NodePosition pos) {
super(pos);
this.catalogParts = normalizeName(catalogParts);
this.catalogName = normalizeName(catalogParts);
}
public String getCatalogParts() {
return catalogParts;
this.catalogName = catalogName;
}
public String getCatalogName() {
return catalogName;
}
public void setCatalogName(String catalogName) {
this.catalogName = normalizeName(catalogName);
}
@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
return ((AstVisitorExtendInterface<R, C>) visitor).visitUseCatalogStatement(this, context);
return visitor.visitUseCatalogStatement(this, context);
}
}