Skip to content

Commit

Permalink
[Feature] Support translate Trino query to StarRocks Query (backport #…
Browse files Browse the repository at this point in the history
…54185) (#54632)

Co-authored-by: Youngwb <[email protected]>
  • Loading branch information
mergify[bot] and Youngwb authored Jan 3, 2025
1 parent 767dab5 commit 54c2632
Show file tree
Hide file tree
Showing 14 changed files with 354 additions and 30 deletions.
8 changes: 8 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/qe/StmtExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@
import com.starrocks.sql.ast.UseDbStmt;
import com.starrocks.sql.ast.UserVariable;
import com.starrocks.sql.ast.feedback.PlanAdvisorStmt;
import com.starrocks.sql.ast.translate.TranslateStmt;
import com.starrocks.sql.ast.warehouse.SetWarehouseStmt;
import com.starrocks.sql.common.AuditEncryptionChecker;
import com.starrocks.sql.common.DmlException;
Expand Down Expand Up @@ -786,6 +787,8 @@ public void execute() throws Exception {
handleDelBackendBlackListStmt();
} else if (parsedStmt instanceof PlanAdvisorStmt) {
handlePlanAdvisorStmt();
} else if (parsedStmt instanceof TranslateStmt) {
handleTranslateStmt();
} else {
context.getState().setError("Do not support this query.");
}
Expand Down Expand Up @@ -1797,6 +1800,11 @@ private void handleSetWarehouseStmt() throws AnalysisException {
context.getState().setOk();
}

private void handleTranslateStmt() throws IOException {
ShowResultSet resultSet = TranslateExecutor.execute((TranslateStmt) parsedStmt);
sendShowResult(resultSet);
}

private void sendMetaData(ShowResultSetMetaData metaData) throws IOException {
// sends how many columns
serializer.reset();
Expand Down
44 changes: 44 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/qe/TranslateExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.starrocks.qe;

import com.starrocks.catalog.Column;
import com.starrocks.catalog.Type;
import com.starrocks.sql.analyzer.AstToSQLBuilder;
import com.starrocks.sql.ast.StatementBase;
import com.starrocks.sql.ast.translate.TranslateStmt;
import com.starrocks.sql.parser.SqlParser;

import java.util.ArrayList;
import java.util.List;

public class TranslateExecutor {
private static final ShowResultSetMetaData COLUMN_META =
ShowResultSetMetaData.builder().addColumn(new Column("Translated SQL", Type.STRING)).build();

public static ShowResultSet execute(TranslateStmt stmt) {
String dialect = stmt.getDialect();
String translateSQL = stmt.getTranslateSQL();
SessionVariable sessionVariable = ConnectContext.getSessionVariableOrDefault();
sessionVariable.setSqlDialect(dialect);
List<StatementBase> statementBases = SqlParser.parse(translateSQL, sessionVariable);

List<List<String>> resultRows = new ArrayList<>();
for (StatementBase statementBase : statementBases) {
resultRows.add(List.of(AstToSQLBuilder.toSQL(statementBase)));
}
return new ShowResultSet(COLUMN_META, resultRows);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
import com.starrocks.sql.ast.pipe.DescPipeStmt;
import com.starrocks.sql.ast.pipe.DropPipeStmt;
import com.starrocks.sql.ast.pipe.ShowPipeStmt;
import com.starrocks.sql.ast.translate.TranslateStmt;
import com.starrocks.sql.ast.warehouse.CreateWarehouseStmt;
import com.starrocks.sql.ast.warehouse.DropWarehouseStmt;
import com.starrocks.sql.ast.warehouse.ResumeWarehouseStmt;
Expand Down Expand Up @@ -1086,5 +1087,12 @@ public Void visitSetWarehouseStatement(SetWarehouseStmt statement, ConnectContex
public Void visitShowNodesStatement(ShowNodesStmt statement, ConnectContext context) {
return null;
}

// ---------------------------------------- Translate Statement --------------------------------------------------
@Override
public Void visitTranslateStatement(TranslateStmt statement, ConnectContext context) {
TranslateAnalyzer.analyze(statement, context);
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.starrocks.sql.analyzer;

import com.starrocks.qe.ConnectContext;
import com.starrocks.sql.ast.AstVisitor;
import com.starrocks.sql.ast.StatementBase;
import com.starrocks.sql.ast.translate.TranslateStmt;

public class TranslateAnalyzer {
public static void analyze(StatementBase statement, ConnectContext context) {
new TranslateAnalyzer.TranslateAnalyzerVisitor().analyze(statement, context);
}

static class TranslateAnalyzerVisitor implements AstVisitor<Void, ConnectContext> {
public void analyze(StatementBase statement, ConnectContext session) {
visit(statement, session);
}

@Override
public Void visitTranslateStatement(TranslateStmt statement, ConnectContext context) {
String dialect = statement.getDialect();
if (!dialect.equalsIgnoreCase("trino")) {
throw new SemanticException(String.format("Unsupported dialect: %s, only support trino dialect now", dialect));
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import com.starrocks.sql.ast.pipe.DropPipeStmt;
import com.starrocks.sql.ast.pipe.PipeName;
import com.starrocks.sql.ast.pipe.ShowPipeStmt;
import com.starrocks.sql.ast.translate.TranslateStmt;
import com.starrocks.sql.ast.warehouse.CreateWarehouseStmt;
import com.starrocks.sql.ast.warehouse.DropWarehouseStmt;
import com.starrocks.sql.ast.warehouse.ResumeWarehouseStmt;
Expand Down Expand Up @@ -1491,6 +1492,11 @@ default R visitShowPlanAdvisorStatement(ShowPlanAdvisorStmt statement, C context
return visitStatement(statement, context);
}

// ---------------------------------------- Translate Statement --------------------------------------------------
default R visitTranslateStatement(TranslateStmt statement, C context) {
return visitStatement(statement, context);
}

// ------------------------------------------- AST -----------------------------------------------------------------

default R visitLimitElement(LimitElement node, C context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.starrocks.sql.ast.translate;

import com.starrocks.analysis.RedirectStatus;
import com.starrocks.sql.ast.AstVisitor;
import com.starrocks.sql.ast.StatementBase;
import com.starrocks.sql.parser.NodePosition;

public class TranslateStmt extends StatementBase {
private final String dialect;
private final String translateSQL;

public TranslateStmt(NodePosition pos, String dialect, String translateSQL) {
super(pos);
this.translateSQL = translateSQL;
this.dialect = dialect;
}

@Override
public RedirectStatus getRedirectStatus() {
return RedirectStatus.NO_FORWARD;
}

@Override
public <R, C> R accept(AstVisitor<R, C> visitor, C context) {
return visitor.visitTranslateStatement(this, context);
}

public String getDialect() {
return dialect;
}

public String getTranslateSQL() {
return translateSQL;
}
}
Loading

0 comments on commit 54c2632

Please sign in to comment.