-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Support translate Trino query to StarRocks Query (backport #…
…54185) (#54632) Co-authored-by: Youngwb <[email protected]>
- Loading branch information
1 parent
767dab5
commit 54c2632
Showing
14 changed files
with
354 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
fe/fe-core/src/main/java/com/starrocks/qe/TranslateExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
fe/fe-core/src/main/java/com/starrocks/sql/analyzer/TranslateAnalyzer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
fe/fe-core/src/main/java/com/starrocks/sql/ast/translate/TranslateStmt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.