Skip to content

Commit

Permalink
Support super sql parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
lingo-xp committed Jan 3, 2025
1 parent c83288e commit ff13951
Show file tree
Hide file tree
Showing 23 changed files with 295 additions and 59 deletions.
4 changes: 3 additions & 1 deletion core/src/main/java/com/alibaba/druid/sql/SQLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerOutputVisitor;
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor;
import com.alibaba.druid.sql.dialect.starrocks.visitor.StarRocksOutputVisitor;
import com.alibaba.druid.sql.dialect.supersql.visitor.SuperSqlOutputVisitor;
import com.alibaba.druid.sql.dialect.teradata.visitor.TDOutputVisitor;
import com.alibaba.druid.sql.parser.*;
import com.alibaba.druid.sql.repository.SchemaRepository;
Expand Down Expand Up @@ -568,8 +569,9 @@ public static SQLASTOutputVisitor createFormatOutputVisitor(
return new SparkOutputVisitor(out);
case presto:
case trino:
case supersql:
return new PrestoOutputVisitor(out);
case supersql:
return new SuperSqlOutputVisitor(out);
case athena:
return new AthenaOutputVisitor(out);
case clickhouse:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.alibaba.druid.sql.dialect.athena.parser;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
Expand All @@ -16,14 +15,8 @@
import com.alibaba.druid.util.FnvHash;

public class AthenaCreateTableParser extends PrestoCreateTableParser {
public AthenaCreateTableParser(String sql) {
super(sql);
this.dbType = DbType.athena;
}

public AthenaCreateTableParser(SQLExprParser exprParser) {
super(exprParser);
this.dbType = DbType.athena;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

public class AthenaExprParser extends PrestoExprParser {
public AthenaExprParser(String sql, SQLParserFeature... features) {
super(sql, features);
this.dbType = DbType.athena;
this(new AthenaLexer(sql, features));
this.lexer.nextToken();
}

public AthenaExprParser(Lexer lexer) {
super(lexer);
this.dbType = DbType.athena;
super(lexer, DbType.athena);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

public class AthenaLexer extends PrestoLexer {
public AthenaLexer(String input, SQLParserFeature... features) {
super(input, features);
this.dbType = DbType.athena;
super(input, DbType.athena, features);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
package com.alibaba.druid.sql.dialect.athena.parser;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.dialect.presto.parser.PrestoSelectParser;
import com.alibaba.druid.sql.parser.SQLExprParser;
import com.alibaba.druid.sql.parser.SQLSelectListCache;

public class AthenaSelectParser extends PrestoSelectParser {
public AthenaSelectParser(SQLExprParser exprParser) {
super(exprParser);
this.dbType = DbType.athena;
}

public AthenaSelectParser(SQLExprParser exprParser, SQLSelectListCache selectListCache) {
super(exprParser, selectListCache);
this.dbType = DbType.athena;
}

public AthenaSelectParser(String sql) {
super(sql);
this.dbType = DbType.athena;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
package com.alibaba.druid.sql.dialect.athena.parser;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.dialect.presto.parser.PrestoStatementParser;
import com.alibaba.druid.sql.parser.Lexer;
import com.alibaba.druid.sql.parser.SQLCreateTableParser;
import com.alibaba.druid.sql.parser.SQLParserFeature;

public class AthenaStatementParser extends PrestoStatementParser {
public AthenaStatementParser(String sql) {
super(sql);
this.dbType = DbType.athena;
}

public AthenaStatementParser(String sql, SQLParserFeature... features) {
super(sql, features);
this.dbType = DbType.athena;
}

public AthenaStatementParser(Lexer lexer) {
super(lexer);
this.dbType = DbType.athena;
super(new AthenaExprParser(sql, features));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@

public class AthenaOutputVisitor extends PrestoOutputVisitor implements AthenaASTVisitor {
public AthenaOutputVisitor(StringBuilder appender) {
super(appender);
dbType = DbType.athena;
super(appender, DbType.athena);
}

public AthenaOutputVisitor(StringBuilder appender, boolean parameterized) {
super(appender, parameterized);
dbType = DbType.athena;
super(appender, DbType.athena, parameterized);
}

protected void printCreateTable(SQLCreateTableStatement x, boolean printSelect) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,16 @@ public PrestoExprParser(String sql, SQLParserFeature... features) {
this.lexer.nextToken();
}

public PrestoExprParser(Lexer lexer) {
super(lexer);
dbType = DbType.presto;
public PrestoExprParser(Lexer lexer, DbType dbType) {
super(lexer, dbType);
this.aggregateFunctions = AGGREGATE_FUNCTIONS;
this.aggregateFunctionHashCodes = AGGREGATE_FUNCTIONS_CODES;
}

public PrestoExprParser(Lexer lexer) {
this(lexer, DbType.presto);
}

@Override
protected SQLColumnDefinition parseColumnSpecific(SQLColumnDefinition column) {
if (lexer.token() == Token.WITH) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,16 @@ protected Keywords loadKeywords() {
return new Keywords(map);
}

public PrestoLexer(String input, SQLParserFeature... features) {
super(input);
this.dbType = DbType.presto;
public PrestoLexer(String input, DbType dbType, SQLParserFeature... features) {
super(input, dbType);
for (SQLParserFeature feature : features) {
config(feature, true);
}
}

public PrestoLexer(String input, SQLParserFeature... features) {
this(input, DbType.presto, features);
}
@Override
protected void initDialectFeature() {
super.initDialectFeature();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.alibaba.druid.sql.parser.Lexer;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.SQLCreateTableParser;
import com.alibaba.druid.sql.parser.SQLExprParser;
import com.alibaba.druid.sql.parser.SQLParserFeature;
import com.alibaba.druid.sql.parser.SQLSelectParser;
import com.alibaba.druid.sql.parser.SQLStatementParser;
Expand All @@ -66,10 +67,13 @@ public class PrestoStatementParser extends SQLStatementParser {
public PrestoStatementParser(String sql) {
super(new PrestoExprParser(sql));
}
public PrestoStatementParser(String sql, SQLParserFeature... features) {
public PrestoStatementParser(String sql, SQLParserFeature... features) {
super(new PrestoExprParser(sql, features));
}

public PrestoStatementParser(SQLExprParser exprParser) {
super(exprParser);
}
public PrestoStatementParser(Lexer lexer) {
super(new PrestoExprParser(lexer));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,19 @@
*/
public class PrestoOutputVisitor extends SQLASTOutputVisitor implements PrestoASTVisitor {
public PrestoOutputVisitor(StringBuilder appender) {
super(appender, DbType.presto);
this(appender, DbType.presto);
}

public PrestoOutputVisitor(StringBuilder appender, DbType dbType) {
super(appender, dbType);
}

public PrestoOutputVisitor(StringBuilder appender, boolean parameterized) {
super(appender, DbType.presto, parameterized);
this(appender, DbType.presto, parameterized);
}

public PrestoOutputVisitor(StringBuilder appender, DbType dbType, boolean parameterized) {
super(appender, dbType, parameterized);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.alibaba.druid.sql.dialect.supersql.ast;

import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.dialect.supersql.visitor.SuperSqlASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

public interface SuperSqlObject extends SQLObject {
void accept0(SuperSqlASTVisitor visitor);

@Override
default void accept(SQLASTVisitor visitor) {
if (visitor instanceof SuperSqlASTVisitor) {
accept0((SuperSqlASTVisitor) visitor);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.alibaba.druid.sql.dialect.supersql.parser;

import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.dialect.presto.parser.PrestoCreateTableParser;
import com.alibaba.druid.sql.parser.SQLExprParser;
import com.alibaba.druid.util.FnvHash;

public class SuperSqlCreateTableParser extends PrestoCreateTableParser {
public SuperSqlCreateTableParser(SQLExprParser exprParser) {
super(exprParser);
}

@Override
protected void createTableBefore(SQLCreateTableStatement stmt) {
acceptIdentifier(FnvHash.Constants.EXTERNAL);
stmt.setExternal(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.alibaba.druid.sql.dialect.supersql.parser;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.dialect.presto.parser.PrestoExprParser;
import com.alibaba.druid.sql.parser.Lexer;
import com.alibaba.druid.sql.parser.SQLParserFeature;

public class SuperSqlExprParser extends PrestoExprParser {
public SuperSqlExprParser(String sql, SQLParserFeature... features) {
this(new SuperSqlLexer(sql, features));
this.lexer.nextToken();
}

public SuperSqlExprParser(Lexer lexer) {
super(lexer, DbType.supersql);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.alibaba.druid.sql.dialect.supersql.parser;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.dialect.presto.parser.PrestoLexer;
import com.alibaba.druid.sql.parser.Keywords;
import com.alibaba.druid.sql.parser.SQLParserFeature;
import com.alibaba.druid.sql.parser.Token;

import java.util.HashMap;
import java.util.Map;

public class SuperSqlLexer extends PrestoLexer {
@Override
protected Keywords loadKeywords() {
Map<String, Token> map = new HashMap<String, Token>();
map.putAll(Keywords.DEFAULT_KEYWORDS.getKeywords());
map.put("FETCH", Token.FETCH);
map.put("FIRST", Token.FIRST);
map.put("ONLY", Token.ONLY);
map.put("OPTIMIZE", Token.OPTIMIZE);
map.put("OF", Token.OF);
map.put("CONCAT", Token.CONCAT);
map.put("CONTINUE", Token.CONTINUE);
map.put("IDENTITY", Token.IDENTITY);
map.put("MERGE", Token.MERGE);
map.put("USING", Token.USING);
map.put("MATCHED", Token.MATCHED);
map.put("UPSERT", Token.UPSERT);
map.put("IF", Token.IF);
map.put("OVERWRITE", Token.OVERWRITE);
map.put("PARTITION", Token.PARTITION);

return new Keywords(map);
}
public SuperSqlLexer(String input, SQLParserFeature... features) {
super(input, features);
this.dbType = DbType.supersql;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.alibaba.druid.sql.dialect.supersql.parser;

import com.alibaba.druid.sql.dialect.presto.parser.PrestoSelectParser;
import com.alibaba.druid.sql.parser.SQLExprParser;
import com.alibaba.druid.sql.parser.SQLSelectListCache;

public class SuperSqlSelectParser extends PrestoSelectParser {
public SuperSqlSelectParser(SQLExprParser exprParser, SQLSelectListCache selectListCache) {
super(exprParser, selectListCache);
}

@Override
protected SQLExprParser createExprParser() {
return new SuperSqlExprParser(this.lexer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.alibaba.druid.sql.dialect.supersql.parser;

import com.alibaba.druid.sql.dialect.presto.parser.PrestoStatementParser;
import com.alibaba.druid.sql.parser.SQLCreateTableParser;
import com.alibaba.druid.sql.parser.SQLParserFeature;

public class SuperSqlStatementParser extends PrestoStatementParser {
public SuperSqlStatementParser(String sql, SQLParserFeature... features) {
super(new SuperSqlExprParser(sql, features));
}

@Override
public SuperSqlSelectParser createSQLSelectParser() {
return new SuperSqlSelectParser(this.exprParser, selectListCache);
}

@Override
public SQLCreateTableParser getSQLCreateTableParser() {
return new SuperSqlCreateTableParser(this.exprParser);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.alibaba.druid.sql.dialect.supersql.visitor;

import com.alibaba.druid.sql.dialect.presto.visitor.PrestoASTVisitor;

public interface SuperSqlASTVisitor extends PrestoASTVisitor {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.alibaba.druid.sql.dialect.supersql.visitor;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.dialect.presto.visitor.PrestoOutputVisitor;

public class SuperSqlOutputVisitor extends PrestoOutputVisitor implements SuperSqlASTVisitor {
public SuperSqlOutputVisitor(StringBuilder appender) {
super(appender, DbType.supersql);
}

public SuperSqlOutputVisitor(StringBuilder appender, boolean parameterized) {
super(appender, DbType.supersql, parameterized);
}
}
6 changes: 3 additions & 3 deletions core/src/main/java/com/alibaba/druid/sql/parser/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ public Lexer(String input) {
this(input, (CommentHandler) null);
}

// public Lexer(String input, DbType dbType){
// this(input, (CommentHandler) null, dbType);
// }
public Lexer(String input, DbType dbType) {
this(input, null, dbType);
}

public Lexer(String input, CommentHandler commentHandler) {
this(input, true);
Expand Down
Loading

0 comments on commit ff13951

Please sign in to comment.