Skip to content

Commit

Permalink
[Refactor](dialect) Add sql dialect converter plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
wangxiangyu committed Dec 25, 2023
1 parent f8b62c5 commit 3a5766d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private List<StatementBase> parseSQLWithDialect(String sql,
DialectConverterPluginMgr pluginMgr = Env.getCurrentEnv().getSqlDialectPluginMgr();
List<DialectConverterPlugin> plugins = pluginMgr.getDialectConverterPlugins(sqlDialect);
for (DialectConverterPlugin plugin : plugins) {
LOG.debug("Use plugin {} for dialect {}", plugin.getClass().getSimpleName(), sqlDialect);
LOG.debug("Use plugin {} for dialect {} with sql: {}", plugin.getClass().getSimpleName(), sqlDialect, sql);
try {
List<StatementBase> statementBases = plugin.parseSqlWithDialect(sql, sessionVariable);
if (CollectionUtils.isNotEmpty(statementBases)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ public void testSqlConvert() {
String expectedSql = "select * from t1 where `k1` = 1";

String targetURL = "http://127.0.0.1:" + port + "/api/v1/convert";
String res = SQLDialectUtils.convertSql(targetURL, originSql);
String res = HttpDialectUtils.convertSql(targetURL, originSql);
Assert.assertEquals(originSql, res);
// test presto
server.setResponse("{\"version\": \"v1\", \"data\": \"" + expectedSql + "\", \"code\": 0, \"message\": \"\"}");
res = SQLDialectUtils.convertSql(targetURL, originSql);
res = HttpDialectUtils.convertSql(targetURL, originSql);
Assert.assertEquals(expectedSql, res);
// test response version error
server.setResponse("{\"version\": \"v2\", \"data\": \"" + expectedSql + "\", \"code\": 0, \"message\": \"\"}");
res = SQLDialectUtils.convertSql(targetURL, originSql);
res = HttpDialectUtils.convertSql(targetURL, originSql);
Assert.assertEquals(originSql, res);
// 7. test response code error
server.setResponse(
"{\"version\": \"v1\", \"data\": \"" + expectedSql + "\", \"code\": 400, \"message\": \"\"}");
res = SQLDialectUtils.convertSql(targetURL, originSql);
res = HttpDialectUtils.convertSql(targetURL, originSql);
Assert.assertEquals(originSql, res);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.apache.doris.analysis.StatementBase;
import org.apache.doris.nereids.StatementContext;
import org.apache.doris.nereids.exceptions.UnsupportedDialectException;
import org.apache.doris.nereids.glue.LogicalPlanAdapter;
import org.apache.doris.nereids.parser.Dialect;
import org.apache.doris.nereids.parser.ParserContext;
Expand Down Expand Up @@ -50,6 +49,7 @@ public class TrinoParser {
* Parse with trino syntax, return null if parse failed
*/
public static @Nullable List<StatementBase> parse(String sql, SessionVariable sessionVariable) {
LOG.debug("Start parse sql : {}", sql);
final List<StatementBase> logicalPlans = new ArrayList<>();
try {
io.trino.sql.parser.StatementSplitter splitter = new io.trino.sql.parser.StatementSplitter(sql);
Expand All @@ -61,10 +61,11 @@ public class TrinoParser {
? null : new LogicalPlanAdapter((LogicalPlan) parsedPlan, statementContext));
}
if (logicalPlans.isEmpty() || logicalPlans.stream().anyMatch(Objects::isNull)) {
LOG.debug("LogicalPlans is empty or any plan is null");
return null;
}
return logicalPlans;
} catch (io.trino.sql.parser.ParsingException | UnsupportedDialectException e) {
} catch (Throwable e) {
LOG.debug("Failed to parse logical plan from trino, sql is :{}", sql, e);
return null;
}
Expand Down

0 comments on commit 3a5766d

Please sign in to comment.