mirror of
https://github.com/tencentmusic/supersonic.git
synced 2026-04-30 04:54:25 +08:00
Compare commits
1 Commits
d9db455dab
...
f764236657
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f764236657 |
@@ -2,11 +2,19 @@ package com.tencent.supersonic.common.calcite;
|
|||||||
|
|
||||||
import com.tencent.supersonic.common.pojo.enums.EngineType;
|
import com.tencent.supersonic.common.pojo.enums.EngineType;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.sf.jsqlparser.expression.Alias;
|
import org.apache.calcite.sql.SqlIdentifier;
|
||||||
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
import org.apache.calcite.sql.SqlLiteral;
|
||||||
import net.sf.jsqlparser.statement.select.ParenthesedSelect;
|
import org.apache.calcite.sql.SqlNode;
|
||||||
import net.sf.jsqlparser.statement.select.Select;
|
import org.apache.calcite.sql.SqlNodeList;
|
||||||
import net.sf.jsqlparser.statement.select.WithItem;
|
import org.apache.calcite.sql.SqlOrderBy;
|
||||||
|
import org.apache.calcite.sql.SqlSelect;
|
||||||
|
import org.apache.calcite.sql.SqlWith;
|
||||||
|
import org.apache.calcite.sql.SqlWithItem;
|
||||||
|
import org.apache.calcite.sql.SqlWriterConfig;
|
||||||
|
import org.apache.calcite.sql.parser.SqlParseException;
|
||||||
|
import org.apache.calcite.sql.parser.SqlParser;
|
||||||
|
import org.apache.calcite.sql.parser.SqlParserPos;
|
||||||
|
import org.apache.calcite.sql.pretty.SqlPrettyWriter;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -14,37 +22,85 @@ import java.util.List;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class SqlMergeWithUtils {
|
public class SqlMergeWithUtils {
|
||||||
public static String mergeWith(EngineType engineType, String sql, List<String> parentSqlList,
|
public static String mergeWith(EngineType engineType, String sql, List<String> parentSqlList,
|
||||||
List<String> parentWithNameList) throws Exception {
|
List<String> parentWithNameList) throws SqlParseException {
|
||||||
|
SqlParser.Config parserConfig = Configuration.getParserConfig(engineType);
|
||||||
|
|
||||||
Select selectStatement = (Select) CCJSqlParserUtil.parse(sql);
|
// Parse the main SQL statement
|
||||||
List<WithItem> withItemList = new ArrayList<>();
|
SqlParser parser = SqlParser.create(sql, parserConfig);
|
||||||
|
SqlNode sqlNode1 = parser.parseQuery();
|
||||||
|
|
||||||
|
// List to hold all WITH items
|
||||||
|
List<SqlNode> withItemList = new ArrayList<>();
|
||||||
|
|
||||||
|
// Iterate over each parentSql and parentWithName pair
|
||||||
for (int i = 0; i < parentSqlList.size(); i++) {
|
for (int i = 0; i < parentSqlList.size(); i++) {
|
||||||
String parentSql = parentSqlList.get(i);
|
String parentSql = parentSqlList.get(i);
|
||||||
String parentWithName = parentWithNameList.get(i);
|
String parentWithName = parentWithNameList.get(i);
|
||||||
|
|
||||||
Select parentSelect = (Select) CCJSqlParserUtil.parse(parentSql);
|
// Parse the parent SQL statement
|
||||||
ParenthesedSelect select = new ParenthesedSelect();
|
parser = SqlParser.create(parentSql, parserConfig);
|
||||||
select.setSelect(parentSelect);
|
SqlNode sqlNode2 = parser.parseQuery();
|
||||||
|
|
||||||
// Create a new WITH item for parentWithName without quotes
|
// Create a new WITH item for parentWithName without quotes
|
||||||
WithItem withItem = new WithItem();
|
SqlWithItem withItem = new SqlWithItem(SqlParserPos.ZERO,
|
||||||
withItem.setAlias(new Alias(parentWithName));
|
new SqlIdentifier(parentWithName, SqlParserPos.ZERO), null, sqlNode2,
|
||||||
withItem.setSelect(select);
|
SqlLiteral.createBoolean(false, SqlParserPos.ZERO));
|
||||||
|
|
||||||
// Add the new WITH item to the list
|
// Add the new WITH item to the list
|
||||||
withItemList.add(withItem);
|
withItemList.add(withItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract existing WITH items from mainSelectBody if it has any
|
// Check if the main SQL node contains an ORDER BY or LIMIT clause
|
||||||
if (selectStatement.getWithItemsList() != null) {
|
SqlNode limitNode = null;
|
||||||
withItemList.addAll(selectStatement.getWithItemsList());
|
SqlNodeList orderByList = null;
|
||||||
|
if (sqlNode1 instanceof SqlOrderBy) {
|
||||||
|
SqlOrderBy sqlOrderBy = (SqlOrderBy) sqlNode1;
|
||||||
|
limitNode = sqlOrderBy.fetch;
|
||||||
|
orderByList = sqlOrderBy.orderList;
|
||||||
|
sqlNode1 = sqlOrderBy.query;
|
||||||
|
} else if (sqlNode1 instanceof SqlSelect) {
|
||||||
|
SqlSelect sqlSelect = (SqlSelect) sqlNode1;
|
||||||
|
limitNode = sqlSelect.getFetch();
|
||||||
|
sqlSelect.setFetch(null);
|
||||||
|
sqlNode1 = sqlSelect;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the new WITH items list to the main select body
|
// Extract existing WITH items from sqlNode1 if it is a SqlWith
|
||||||
selectStatement.setWithItemsList(withItemList);
|
if (sqlNode1 instanceof SqlWith) {
|
||||||
|
SqlWith sqlWith = (SqlWith) sqlNode1;
|
||||||
|
withItemList.addAll(sqlWith.withList.getList());
|
||||||
|
sqlNode1 = sqlWith.body;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new SqlWith node
|
||||||
|
SqlWith finalSqlNode = new SqlWith(SqlParserPos.ZERO,
|
||||||
|
new SqlNodeList(withItemList, SqlParserPos.ZERO), sqlNode1);
|
||||||
|
|
||||||
|
// If there was an ORDER BY or LIMIT clause, wrap the finalSqlNode in a SqlOrderBy
|
||||||
|
SqlNode resultNode = finalSqlNode;
|
||||||
|
if (orderByList != null || limitNode != null) {
|
||||||
|
resultNode = new SqlOrderBy(SqlParserPos.ZERO, finalSqlNode,
|
||||||
|
orderByList != null ? orderByList : SqlNodeList.EMPTY, null, limitNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Custom SqlPrettyWriter configuration to avoid quoting identifiers
|
||||||
|
SqlWriterConfig config = Configuration.getSqlWriterConfig(engineType);
|
||||||
// Pretty print the final SQL
|
// Pretty print the final SQL
|
||||||
return selectStatement.toString();
|
SqlPrettyWriter writer = new SqlPrettyWriter(config);
|
||||||
|
return writer.format(resultNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean hasWith(EngineType engineType, String sql) throws SqlParseException {
|
||||||
|
SqlParser.Config parserConfig = Configuration.getParserConfig(engineType);
|
||||||
|
SqlParser parser = SqlParser.create(sql, parserConfig);
|
||||||
|
SqlNode sqlNode = parser.parseQuery();
|
||||||
|
SqlNode sqlSelect = sqlNode;
|
||||||
|
if (sqlNode instanceof SqlOrderBy) {
|
||||||
|
SqlOrderBy sqlOrderBy = (SqlOrderBy) sqlNode;
|
||||||
|
sqlSelect = sqlOrderBy.query;
|
||||||
|
} else if (sqlNode instanceof SqlSelect) {
|
||||||
|
sqlSelect = (SqlSelect) sqlNode;
|
||||||
|
}
|
||||||
|
return sqlSelect instanceof SqlWith;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,8 +91,7 @@ public class FiledFilterReplaceVisitor extends ExpressionVisitorAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ExpressionList<?> leftFunctionParams = leftFunction.getParameters();
|
ExpressionList<?> leftFunctionParams = leftFunction.getParameters();
|
||||||
if (CollectionUtils.isEmpty(leftFunctionParams)
|
if (CollectionUtils.isEmpty(leftFunctionParams)) {
|
||||||
|| !(leftFunctionParams.get(0) instanceof Column)) {
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -989,15 +989,6 @@ public class SqlSelectHelper {
|
|||||||
for (SelectItem selectItem : selectItems) {
|
for (SelectItem selectItem : selectItems) {
|
||||||
selectItem.accept(visitor);
|
selectItem.accept(visitor);
|
||||||
}
|
}
|
||||||
if (plainSelect.getHaving() != null) {
|
|
||||||
plainSelect.getHaving().accept(visitor);
|
|
||||||
}
|
|
||||||
if (!CollectionUtils.isEmpty(plainSelect.getOrderByElements())) {
|
|
||||||
for (OrderByElement orderByElement : plainSelect.getOrderByElements()) {
|
|
||||||
orderByElement.getExpression().accept(visitor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return !visitor.getFunctionNames().isEmpty();
|
return !visitor.getFunctionNames().isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.tencent.supersonic.common.calcite;
|
|||||||
|
|
||||||
import com.tencent.supersonic.common.pojo.enums.EngineType;
|
import com.tencent.supersonic.common.pojo.enums.EngineType;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.calcite.sql.parser.SqlParseException;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@@ -11,7 +12,7 @@ import java.util.Collections;
|
|||||||
class SqlWithMergerTest {
|
class SqlWithMergerTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test1() throws Exception {
|
void test1() throws SqlParseException {
|
||||||
String sql1 = "WITH DepartmentVisits AS (\n" + " SELECT department, SUM(pv) AS 总访问次数\n"
|
String sql1 = "WITH DepartmentVisits AS (\n" + " SELECT department, SUM(pv) AS 总访问次数\n"
|
||||||
+ " FROM t_1\n"
|
+ " FROM t_1\n"
|
||||||
+ " WHERE sys_imp_date >= '2024-09-01' AND sys_imp_date <= '2024-09-29'\n"
|
+ " WHERE sys_imp_date >= '2024-09-01' AND sys_imp_date <= '2024-09-29'\n"
|
||||||
@@ -37,7 +38,7 @@ class SqlWithMergerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test2() throws Exception {
|
void test2() throws SqlParseException {
|
||||||
|
|
||||||
String sql1 =
|
String sql1 =
|
||||||
"WITH DepartmentVisits AS (SELECT department, SUM(pv) AS 总访问次数 FROM t_1 WHERE sys_imp_date >= '2024-08-28' "
|
"WITH DepartmentVisits AS (SELECT department, SUM(pv) AS 总访问次数 FROM t_1 WHERE sys_imp_date >= '2024-08-28' "
|
||||||
@@ -64,7 +65,7 @@ class SqlWithMergerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test3() throws Exception {
|
void test3() throws SqlParseException {
|
||||||
|
|
||||||
String sql1 = "SELECT COUNT(*) FROM DepartmentVisits WHERE 总访问次数 > 100 LIMIT 1000";
|
String sql1 = "SELECT COUNT(*) FROM DepartmentVisits WHERE 总访问次数 > 100 LIMIT 1000";
|
||||||
|
|
||||||
@@ -88,7 +89,7 @@ class SqlWithMergerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test4() throws Exception {
|
void test4() throws SqlParseException {
|
||||||
String sql1 = "SELECT COUNT(*) FROM DepartmentVisits WHERE 总访问次数 > 100";
|
String sql1 = "SELECT COUNT(*) FROM DepartmentVisits WHERE 总访问次数 > 100";
|
||||||
|
|
||||||
String sql2 =
|
String sql2 =
|
||||||
@@ -111,7 +112,7 @@ class SqlWithMergerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test5() throws Exception {
|
void test5() throws SqlParseException {
|
||||||
|
|
||||||
String sql1 = "SELECT COUNT(*) FROM Department join Visits WHERE 总访问次数 > 100";
|
String sql1 = "SELECT COUNT(*) FROM Department join Visits WHERE 总访问次数 > 100";
|
||||||
|
|
||||||
@@ -131,13 +132,13 @@ class SqlWithMergerTest {
|
|||||||
"WITH t_1 AS (SELECT `t3`.`sys_imp_date`, `t2`.`department`, `t3`.`s2_pv_uv_statis_pv` AS `pv` "
|
"WITH t_1 AS (SELECT `t3`.`sys_imp_date`, `t2`.`department`, `t3`.`s2_pv_uv_statis_pv` AS `pv` "
|
||||||
+ "FROM (SELECT `user_name`, `department` FROM `s2_user_department`) AS `t2` LEFT JOIN "
|
+ "FROM (SELECT `user_name`, `department` FROM `s2_user_department`) AS `t2` LEFT JOIN "
|
||||||
+ "(SELECT 1 AS `s2_pv_uv_statis_pv`, `imp_date` AS `sys_imp_date`, `user_name` FROM `s2_pv_uv_statis`) "
|
+ "(SELECT 1 AS `s2_pv_uv_statis_pv`, `imp_date` AS `sys_imp_date`, `user_name` FROM `s2_pv_uv_statis`) "
|
||||||
+ "AS `t3` ON `t2`.`user_name` = `t3`.`user_name`) SELECT COUNT(*) FROM Department JOIN Visits "
|
+ "AS `t3` ON `t2`.`user_name` = `t3`.`user_name`) SELECT COUNT(*) FROM Department INNER JOIN Visits "
|
||||||
+ "WHERE 总访问次数 > 100");
|
+ "WHERE 总访问次数 > 100");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test6() throws Exception {
|
void test6() throws SqlParseException {
|
||||||
|
|
||||||
String sql1 =
|
String sql1 =
|
||||||
"SELECT COUNT(*) FROM Department join Visits WHERE 总访问次数 > 100 ORDER BY 总访问次数 LIMIT 10";
|
"SELECT COUNT(*) FROM Department join Visits WHERE 总访问次数 > 100 ORDER BY 总访问次数 LIMIT 10";
|
||||||
@@ -158,36 +159,7 @@ class SqlWithMergerTest {
|
|||||||
"WITH t_1 AS (SELECT `t3`.`sys_imp_date`, `t2`.`department`, `t3`.`s2_pv_uv_statis_pv` AS `pv` FROM "
|
"WITH t_1 AS (SELECT `t3`.`sys_imp_date`, `t2`.`department`, `t3`.`s2_pv_uv_statis_pv` AS `pv` FROM "
|
||||||
+ "(SELECT `user_name`, `department` FROM `s2_user_department`) AS `t2` LEFT JOIN (SELECT 1 AS `s2_pv_uv_statis_pv`,"
|
+ "(SELECT `user_name`, `department` FROM `s2_user_department`) AS `t2` LEFT JOIN (SELECT 1 AS `s2_pv_uv_statis_pv`,"
|
||||||
+ " `imp_date` AS `sys_imp_date`, `user_name` FROM `s2_pv_uv_statis`) AS `t3` ON `t2`.`user_name` = `t3`.`user_name`) "
|
+ " `imp_date` AS `sys_imp_date`, `user_name` FROM `s2_pv_uv_statis`) AS `t3` ON `t2`.`user_name` = `t3`.`user_name`) "
|
||||||
+ "SELECT COUNT(*) FROM Department JOIN Visits WHERE 总访问次数 > 100 ORDER BY 总访问次数 LIMIT 10");
|
+ "SELECT COUNT(*) FROM Department INNER JOIN Visits WHERE 总访问次数 > 100 ORDER BY 总访问次数 LIMIT 10");
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void test7() throws Exception {
|
|
||||||
|
|
||||||
String sql1 =
|
|
||||||
"SELECT COUNT(*) FROM Department join Visits WHERE 总访问次数 > 100 AND imp_date >= CURRENT_DATE - "
|
|
||||||
+ "INTERVAL '1 year' AND sys_imp_date < CURRENT_DATE ORDER"
|
|
||||||
+ " BY 总访问次数 LIMIT 10";
|
|
||||||
|
|
||||||
String sql2 =
|
|
||||||
"SELECT `t3`.`sys_imp_date`, `t2`.`department`, `t3`.`s2_pv_uv_statis_pv` AS `pv`\n"
|
|
||||||
+ "FROM\n" + "(SELECT `user_name`, `department`\n" + "FROM\n"
|
|
||||||
+ "`s2_user_department`) AS `t2`\n"
|
|
||||||
+ "LEFT JOIN (SELECT 1 AS `s2_pv_uv_statis_pv`, `imp_date` AS `sys_imp_date`, `user_name`\n"
|
|
||||||
+ "FROM\n"
|
|
||||||
+ "`s2_pv_uv_statis`) AS `t3` ON `t2`.`user_name` = `t3`.`user_name`";
|
|
||||||
|
|
||||||
String mergeSql = SqlMergeWithUtils.mergeWith(EngineType.MYSQL, sql1,
|
|
||||||
Collections.singletonList(sql2), Collections.singletonList("t_1"));
|
|
||||||
|
|
||||||
|
|
||||||
Assert.assertEquals(format(mergeSql),
|
|
||||||
"WITH t_1 AS (SELECT `t3`.`sys_imp_date`, `t2`.`department`, `t3`.`s2_pv_uv_statis_pv` AS `pv` FROM "
|
|
||||||
+ "(SELECT `user_name`, `department` FROM `s2_user_department`) AS `t2` LEFT JOIN (SELECT 1 AS `s2_pv_uv_statis_pv`,"
|
|
||||||
+ " `imp_date` AS `sys_imp_date`, `user_name` FROM `s2_pv_uv_statis`) AS `t3` ON `t2`.`user_name` = `t3`.`user_name`) "
|
|
||||||
+ "SELECT COUNT(*) FROM Department JOIN Visits WHERE 总访问次数 > 100 AND imp_date >= "
|
|
||||||
+ "CURRENT_DATE - INTERVAL '1 year' AND sys_imp_date < CURRENT_DATE ORDER BY 总访问次数 "
|
|
||||||
+ "LIMIT 10");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String format(String mergeSql) {
|
private static String format(String mergeSql) {
|
||||||
|
|||||||
Reference in New Issue
Block a user