SQL Parser
https://blog.csdn.net/w1047667241/article/details/123110220
alibaba druid
经过不断迭代,已经解决了很多 hive解析的bug,比如 2020年的create tablebug
支持的db type 多,impala ,hive ,oracle 等等都支持 。
缺点就是捆绑销售,1个jar 包高大全的 datasource 全家桶。我们只是想要 parser 而已。
老版本的 sql 解析会有bug,但是新版本的已经解决。测试了 一些复杂 语句,都能正确 解析。
对比 hive 本身的 parser ast 的操作,还需要自己分析 token,实在是太那个了。
二者对比参考代码
/**
* hive-sql-parser versus druid-sql-parser
*/
@Test
public void testDruidSqlParser() throws ParseException {
String sql = "FROM (SELECT p.datekey datekey, p.userid userid, c.clienttype FROM detail.usersequence_client c JOIN fact.orderpayment p ON p.orderid = c.orderid JOIN default.user du ON du.userid = p.userid WHERE p.datekey = 20131118 ) base INSERT OVERWRITE TABLE `test`.`customer_kpi` SELECT base.datekey, base.clienttype, count(distinct base.userid) buyer_count GROUP BY base.datekey, base.clienttype";
final SQLStatementParser hive = SQLParserUtils.createSQLStatementParser(sql, DbType.hive);
final SQLStatement statement = hive.parseStatement();
System.out.println(statement);
// as you can see , using this parseDriver will cause an error
ParseDriver pd = new ParseDriver();
ASTNode ast = pd.parse(sql);
System.out.println(ast.dump());
}
/**
* @return 解析树
*/
public static List<SQLStatement> parseStatements(String sql, String dbType) {
try {
return SQLUtils.parseStatements(sql, dbType);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RuntimeException("SQL格式错误");
}
}
/*
* sql格式化
*/
public static void format(String sql, String dbType) {
String sqlFormat = SQLUtils.format(sql, dbType);
if (sql.equals(sqlFormat)) {
throw new RuntimeException("SQL格式错误");
}
}
[Spark] https://spark.apache.org/docs/1.3.1/api/java/org/apache/spark/sql/SparkSQLParser.html
ChatGPT 生成case
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.12</artifactId>
<version>3.1.2</version>
</dependency>
import org.apache.spark.sql.catalyst.AbstractSparkSQLParser;
import org.apache.spark.sql.catalyst.analysis.SimpleAnalyzer;
import org.apache.spark.sql.catalyst.parser.SqlParser;
import org.apache.spark.sql.catalyst.parser.SqlParserBase;
import org.apache.spark.sql.catalyst.symbols.Column;
import org.apache.spark.sql.catalyst.symbols.Symbol;
import java.util.List;
public class Main {
public static void main(String[] args) {
String sql = "SELECT a.id, b.name FROM table1 a JOIN table2 b ON a.id = b.id WHERE a.status = 'active'";
// 创建 AbstractSparkSQLParser 实例
AbstractSparkSQLParser parser = new AbstractSparkSQLParser(sql);
// 解析 SQL 查询
SqlParser sqlParser = parser.parseQuery();
// 获取 SELECT 字段
List<Symbol> selectList = sqlParser.getSelectList();
// 打印 SELECT 字段
for (Symbol symbol : selectList) {
if (symbol instanceof Column) {
System.out.println(((Column) symbol).getColumnName());
}
}
// 获取 FROM 表
List<Symbol> fromList = sqlParser.getFromList();
// 打印 FROM 表
for (Symbol symbol : fromList) {
System.out.println(symbol.getName());
}
}
}