improved sql parser

This commit is contained in:
高铁
2021-05-04 13:56:56 +08:00
parent 80e3b9246e
commit 1990f03438
7 changed files with 115 additions and 13 deletions
@@ -422,15 +422,16 @@ public class SQLUtils {
visitor.println();
}
}
{
List<String> comments = stmt.getBeforeCommentsDirect();
if (comments != null){
for(String comment : comments) {
visitor.printComment(comment);
visitor.println();
}
}
}
// {
// List<String> comments = stmt.getBeforeCommentsDirect();
// if (comments != null){
// for(String comment : comments) {
// visitor.printComment(comment);
// visitor.println();
// }
// }
// }
stmt.accept(visitor);
if (i == size - 1) {
@@ -57,6 +57,18 @@ public class OdpsOutputVisitor extends HiveOutputVisitor implements OdpsASTVisit
}
public boolean visit(OdpsCreateTableStatement x) {
List<SQLCommentHint> headHints = x.getHeadHintsDirect();
if (headHints != null) {
for (SQLCommentHint hint : headHints) {
visit((SQLCommentHint) hint);
println();
}
}
if (x.hasBeforeComment()) {
printlnComments(x.getBeforeCommentsDirect());
}
print0(ucase ? "CREATE " : "create ");
if (x.isExternal()) {
@@ -569,6 +581,18 @@ public class OdpsOutputVisitor extends HiveOutputVisitor implements OdpsASTVisit
@Override
public boolean visit(OdpsReadStatement x) {
List<SQLCommentHint> headHints = x.getHeadHintsDirect();
if (headHints != null) {
for (SQLCommentHint hint : headHints) {
visit((SQLCommentHint) hint);
println();
}
}
if (x.hasBeforeComment()) {
printlnComments(x.getBeforeCommentsDirect());
}
print0(ucase ? "READ " : "read ");
x.getTable().accept(this);
@@ -636,6 +660,18 @@ public class OdpsOutputVisitor extends HiveOutputVisitor implements OdpsASTVisit
@Override
public boolean visit(OdpsShowGrantsStmt x) {
List<SQLCommentHint> headHints = x.getHeadHintsDirect();
if (headHints != null) {
for (SQLCommentHint hint : headHints) {
visit((SQLCommentHint) hint);
println();
}
}
if (x.hasBeforeComment()) {
printlnComments(x.getBeforeCommentsDirect());
}
if (x.isLabel()) {
print0(ucase ? "SHOW LABEL GRANTS" : "show label grants");
@@ -852,6 +888,18 @@ public class OdpsOutputVisitor extends HiveOutputVisitor implements OdpsASTVisit
@Override
public boolean visit(OdpsCountStatement x) {
List<SQLCommentHint> headHints = x.getHeadHintsDirect();
if (headHints != null) {
for (SQLCommentHint hint : headHints) {
visit((SQLCommentHint) hint);
println();
}
}
if (x.hasBeforeComment()) {
printlnComments(x.getBeforeCommentsDirect());
}
print0(ucase ? "COUNT " : "count ");
x.getTable().accept(this);
@@ -3078,6 +3078,10 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
}
}
if (x.hasBeforeComment()) {
printlnComments(x.getBeforeCommentsDirect());
}
print0(ucase ? "DROP " : "drop ");
List<SQLCommentHint> hints = x.getHints();
if (hints != null) {
@@ -3130,6 +3134,18 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
}
public boolean visit(SQLDropViewStatement x) {
List<SQLCommentHint> headHints = x.getHeadHintsDirect();
if (headHints != null) {
for (SQLCommentHint hint : headHints) {
visit((SQLCommentHint) hint);
println();
}
}
if (x.hasBeforeComment()) {
printlnComments(x.getBeforeCommentsDirect());
}
print0(ucase ? "DROP VIEW " : "drop view ");
if (x.isIfExists()) {
@@ -3886,6 +3902,16 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
@Override
public boolean visit(SQLSetStatement x) {
if(x.getHeadHintsDirect() != null) {
for (SQLCommentHint hint : x.getHeadHintsDirect()) {
hint.accept(this);
}
}
if (x.hasBeforeComment()) {
printlnComments(x.getBeforeCommentsDirect());
}
boolean printSet = x.getAttribute("parser.set") == Boolean.TRUE || !JdbcUtils.isOracleDbType(dbType);
if (printSet) {
print0(ucase ? "SET " : "set ");
@@ -5938,6 +5964,18 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
@Override
public boolean visit(SQLDropFunctionStatement x) {
List<SQLCommentHint> headHints = x.getHeadHintsDirect();
if (headHints != null) {
for (SQLCommentHint hint : headHints) {
visit((SQLCommentHint) hint);
println();
}
}
if (x.hasBeforeComment()) {
printlnComments(x.getBeforeCommentsDirect());
}
if (x.isTemporary()) {
print0(ucase ? "DROP TEMPORARY FUNCTION " : "drop temporary function ");
} else {
@@ -72,6 +72,10 @@ public class ClickHouseResourceTest extends OracleTest {
exec_test("bvt/parser/clickhouse-8.txt");
}
public void test_9() throws Exception {
exec_test("bvt/parser/clickhouse-9.txt");
}
public void exec_test(String resource) throws Exception {
System.out.println(resource);
InputStream is = null;
@@ -34,7 +34,6 @@ public class HiveCreateTableTest_38_dla extends OracleTest {
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, DbType.hive, SQLParserFeature.KeepComments);
assertEquals("-- {\"n_nationkey\":0,\"n_name\":\"ALGERIA\",\"n_regionkey\":0,\"n_comment\":\" haggle. carefully final deposits detect slyly agai\"}\n" +
"-- {\"n_nationkey\":0,\"n_name\":\"ALGERIA\",\"n_regionkey\":0,\"n_comment\":\" haggle. carefully final deposits detect slyly agai\"}\n" +
"CREATE DATABASE ddl_json_string\n" +
"WITH DBPROPERTIES (\n" +
"\tLOCATION = 'oss://oss-cn-hangzhou-for-openanalytics/datasets/tpch/1x/json_string/',\n" +
@@ -32,9 +32,6 @@ public class HiveCreateTableTest_39_dla extends OracleTest {
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, DbType.hive, SQLParserFeature.KeepComments);
assertEquals("--\n" +
"-- Sample on record\n" +
"-- {\"c_custkey\":1,\"c_name\":\"Customer#000000001\",\"c_address\":\"IVhzIApeRb ot,c,E\",\"c_nationkey\":15,\"c_phone\":\"25-989-741-2988\",\"c_acctbal\":711.56,\"c_mktsegment\":\"BUILDING\",\"c_comment\":\"to theeven, regular platelets. regular, ironic epitaphs nag e\"}\n" +
"--\n" +
"-- Sample on record\n" +
"-- {\"c_custkey\":1,\"c_name\":\"Customer#000000001\",\"c_address\":\"IVhzIApeRb ot,c,E\",\"c_nationkey\":15,\"c_phone\":\"25-989-741-2988\",\"c_acctbal\":711.56,\"c_mktsegment\":\"BUILDING\",\"c_comment\":\"to theeven, regular platelets. regular, ironic epitaphs nag e\"}\n" +
"CREATE EXTERNAL TABLE IF NOT EXISTS `mengdou_test`.`ddl_customer_json` (\n" +
@@ -0,0 +1,15 @@
SELECT asset_id, statistics_year, statistics_month, rent_rate, average_rent
FROM dw_asset_operating_summary
WHERE rent_rate IS NOT NULL AND average_rent IS NOT NULL AND is_del = toInt8(0)
GROUP BY asset_id, statistics_year, statistics_month, rent_rate, average_rent
ORDER BY asset_id, statistics_year DESC, statistics_month DESC
LIMIT 1 BY asset_id
---------------------------
SELECT asset_id, statistics_year, statistics_month, rent_rate, average_rent
FROM dw_asset_operating_summary
WHERE rent_rate IS NOT NULL
AND average_rent IS NOT NULL
AND is_del = toInt8(0)
GROUP BY asset_id, statistics_year, statistics_month, rent_rate, average_rent
ORDER BY asset_id, statistics_year DESC, statistics_month DESC
LIMIT 1 BY asset_id