Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] support trino's show schemas function #50586

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import com.starrocks.sql.ast.SelectListItem;
import com.starrocks.sql.ast.SelectRelation;
import com.starrocks.sql.ast.SetQualifier;
import com.starrocks.sql.ast.ShowDbStmt;
import com.starrocks.sql.ast.StatementBase;
import com.starrocks.sql.ast.SubqueryRelation;
import com.starrocks.sql.ast.TableFunctionRelation;
Expand Down Expand Up @@ -162,6 +163,7 @@
import io.trino.sql.tree.RowDataType;
import io.trino.sql.tree.SearchedCaseExpression;
import io.trino.sql.tree.SetOperation;
import io.trino.sql.tree.ShowSchemas;
import io.trino.sql.tree.SimpleCaseExpression;
import io.trino.sql.tree.SimpleGroupBy;
import io.trino.sql.tree.SingleColumn;
Expand Down Expand Up @@ -739,6 +741,21 @@ protected ParseNode visitFunctionCall(FunctionCall node, ParseTreeContext contex

}

@Override
protected ParseNode visitShowSchemas(ShowSchemas node, ParseTreeContext context) {
String catalog = null;
if (!node.getCatalog().isEmpty()) {
catalog = node.getCatalog().map(Identifier::getValue).get();
}

if (!node.getLikePattern().isEmpty()) {
String likePattern = node.getLikePattern().get();
return new ShowDbStmt(likePattern, null, catalog, null);
} else {
return new ShowDbStmt(null, null, catalog, null);
}
}

private static AnalyticWindow.Type getFrameType(WindowFrame.Type type) {
switch (type) {
case RANGE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.trino.sql.tree.Explain;
import io.trino.sql.tree.ExplainAnalyze;
import io.trino.sql.tree.Query;
import io.trino.sql.tree.ShowSchemas;
import io.trino.sql.tree.Statement;

import java.time.format.DateTimeParseException;
Expand All @@ -40,7 +41,7 @@ public static StatementBase toStatement(String query, long sqlMode) {
String trimmedQuery = query.trim();
Statement statement = TrinoParser.parse(trimmedQuery);
if (statement instanceof Query || statement instanceof Explain || statement instanceof ExplainAnalyze
|| statement instanceof CreateTableAsSelect) {
|| statement instanceof CreateTableAsSelect || statement instanceof ShowSchemas) {
return (StatementBase) statement.accept(new AstBuilder(sqlMode), new ParseTreeContext());
} else {
throw trinoParserUnsupportedException("Unsupported statement type: " + statement.getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ setCatalogStatement

showDatabasesStatement
: SHOW DATABASES ((FROM | IN) catalog=qualifiedName)? ((LIKE pattern=string) | (WHERE expression))?
| SHOW SCHEMAS ((LIKE pattern=string) | (WHERE expression))?
| SHOW SCHEMAS (FROM catalog=qualifiedName)? ((LIKE pattern=string) | (WHERE expression))?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you convert show shemas to show databases using trino parser, there is no need to modify starrocks parser

;

alterDbQuotaStatement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ public void testShowSchemas() throws Exception {
ShowDbStmt showDbStmt = (ShowDbStmt) UtFrameUtils.parseStmtWithNewParser(showSQL, ctx);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

package com.starrocks.connector.parser.trino;

import com.starrocks.common.util.UUIDUtil;
import com.starrocks.sql.ast.ShowDbStmt;
import com.starrocks.utframe.UtFrameUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -1228,4 +1231,13 @@ public void testCastArrayDataType() throws Exception {
String sql = "select cast(ARRAY[1] as array(int))";
assertPlanContains(sql, "CAST([1] AS ARRAY<INT>)");
}

@Test
public void testShowSchemasFroCatalog() throws Exception {
connectContext.setExecutionId(UUIDUtil.toTUniqueId(UUIDUtil.genUUID()));
String showSQL = "show schemas from default_catalog";
ShowDbStmt showDbStmt = (ShowDbStmt) UtFrameUtils.parseStmtWithNewParser(showSQL, connectContext);
Assert.assertNotNull(showDbStmt);
Assert.assertEquals(1, showDbStmt.getMetaData().getColumnCount());
}
}
Loading