mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-13 21:17:08 +00:00
[improvement][headless]Release brand new version of Translator module.
This commit is contained in:
@@ -1,13 +1,10 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
import net.sf.jsqlparser.expression.Alias;
|
||||
import net.sf.jsqlparser.expression.BinaryExpression;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
|
||||
import net.sf.jsqlparser.expression.Function;
|
||||
import net.sf.jsqlparser.expression.*;
|
||||
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
import net.sf.jsqlparser.statement.select.SelectItem;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@@ -50,7 +47,8 @@ public class QueryExpressionReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
String columnName = "";
|
||||
if (expression instanceof Function) {
|
||||
Function leftFunc = (Function) expression;
|
||||
if (leftFunc.getParameters().getExpressions().get(0) instanceof Column) {
|
||||
if (Objects.nonNull(leftFunc.getParameters())
|
||||
&& leftFunc.getParameters().getExpressions().get(0) instanceof Column) {
|
||||
Column column = (Column) leftFunc.getParameters().getExpressions().get(0);
|
||||
columnName = column.getColumnName();
|
||||
toReplace = getReplaceExpr(leftFunc, fieldExprMap);
|
||||
@@ -75,7 +73,10 @@ public class QueryExpressionReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
public static Expression replace(Expression expression, Map<String, String> fieldExprMap) {
|
||||
String toReplace = "";
|
||||
if (expression instanceof Function) {
|
||||
toReplace = getReplaceExpr((Function) expression, fieldExprMap);
|
||||
Function function = (Function) expression;
|
||||
if (function.getParameters().getExpressions().get(0) instanceof Column) {
|
||||
toReplace = getReplaceExpr((Function) expression, fieldExprMap);
|
||||
}
|
||||
}
|
||||
if (expression instanceof Column) {
|
||||
toReplace = getReplaceExpr((Column) expression, fieldExprMap);
|
||||
@@ -109,6 +110,16 @@ public class QueryExpressionReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
|
||||
public static String getReplaceExpr(Function function, Map<String, String> fieldExprMap) {
|
||||
Column column = (Column) function.getParameters().getExpressions().get(0);
|
||||
return getReplaceExpr(column, fieldExprMap);
|
||||
String expr = getReplaceExpr(column, fieldExprMap);
|
||||
// if metric expr itself has agg function then replace original function in the SQL
|
||||
if (StringUtils.isBlank(expr)) {
|
||||
return expr;
|
||||
} else if (!SqlSelectFunctionHelper.getAggregateFunctions(expr).isEmpty()) {
|
||||
return expr;
|
||||
} else {
|
||||
String col = getReplaceExpr(column, fieldExprMap);
|
||||
column.setColumnName(col);
|
||||
return function.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,6 +229,26 @@ public class SqlReplaceHelper {
|
||||
orderByElement.accept(new OrderByReplaceVisitor(fieldNameMap, exactReplace));
|
||||
}
|
||||
}
|
||||
List<Select> selects = operationList.getSelects();
|
||||
if (!CollectionUtils.isEmpty(selects)) {
|
||||
for (Select select : selects) {
|
||||
if (select instanceof PlainSelect) {
|
||||
replaceFieldsInPlainOneSelect(fieldNameMap, exactReplace, (PlainSelect) select);
|
||||
}
|
||||
}
|
||||
}
|
||||
List<WithItem> withItems = operationList.getWithItemsList();
|
||||
if (!CollectionUtils.isEmpty(withItems)) {
|
||||
for (WithItem withItem : withItems) {
|
||||
Select select = withItem.getSelect();
|
||||
if (select instanceof PlainSelect) {
|
||||
replaceFieldsInPlainOneSelect(fieldNameMap, exactReplace, (PlainSelect) select);
|
||||
} else if (select instanceof ParenthesedSelect) {
|
||||
replaceFieldsInPlainOneSelect(fieldNameMap, exactReplace,
|
||||
select.getPlainSelect());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String replaceFunction(String sql, Map<String, String> functionMap) {
|
||||
@@ -611,7 +631,14 @@ public class SqlReplaceHelper {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
List<PlainSelect> plainSelectList = new ArrayList<>();
|
||||
if (selectStatement instanceof PlainSelect) {
|
||||
plainSelectList.add((PlainSelect) selectStatement);
|
||||
// if with statement exists, replace expression in the with statement.
|
||||
if (!CollectionUtils.isEmpty(selectStatement.getWithItemsList())) {
|
||||
selectStatement.getWithItemsList().forEach(withItem -> {
|
||||
plainSelectList.add(withItem.getSelect().getPlainSelect());
|
||||
});
|
||||
} else {
|
||||
plainSelectList.add((PlainSelect) selectStatement);
|
||||
}
|
||||
} else if (selectStatement instanceof SetOperationList) {
|
||||
SetOperationList setOperationList = (SetOperationList) selectStatement;
|
||||
if (!CollectionUtils.isEmpty(setOperationList.getSelects())) {
|
||||
@@ -620,12 +647,35 @@ public class SqlReplaceHelper {
|
||||
plainSelectList.add(subPlainSelect);
|
||||
});
|
||||
}
|
||||
List<Select> selects = setOperationList.getSelects();
|
||||
if (!CollectionUtils.isEmpty(selects)) {
|
||||
for (Select select : selects) {
|
||||
if (select instanceof PlainSelect) {
|
||||
plainSelectList.add((PlainSelect) select);
|
||||
}
|
||||
}
|
||||
}
|
||||
List<WithItem> withItems = setOperationList.getWithItemsList();
|
||||
if (!CollectionUtils.isEmpty(withItems)) {
|
||||
for (WithItem withItem : withItems) {
|
||||
Select select = withItem.getSelect();
|
||||
if (select instanceof PlainSelect) {
|
||||
plainSelectList.add((PlainSelect) select);
|
||||
} else if (select instanceof ParenthesedSelect) {
|
||||
plainSelectList.add(select.getPlainSelect());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return sql;
|
||||
}
|
||||
|
||||
List<PlainSelect> plainSelects = SqlSelectHelper.getPlainSelects(plainSelectList);
|
||||
for (PlainSelect plainSelect : plainSelects) {
|
||||
replacePlainSelectByExpr(plainSelect, replace);
|
||||
if (SqlSelectHelper.hasAggregateFunction(plainSelect)) {
|
||||
SqlSelectHelper.addMissingGroupby(plainSelect);
|
||||
}
|
||||
}
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
@@ -714,61 +714,61 @@ public class SqlSelectHelper {
|
||||
return table.getFullyQualifiedName();
|
||||
}
|
||||
|
||||
public static Set<String> getColumnFromExpr(String expr) {
|
||||
public static Set<String> getFieldsFromExpr(String expr) {
|
||||
Expression expression = QueryExpressionReplaceVisitor.getExpression(expr);
|
||||
Set<String> columns = new HashSet<>();
|
||||
if (Objects.nonNull(expression)) {
|
||||
getColumnFromExpr(expression, columns);
|
||||
getFieldsFromExpr(expression, columns);
|
||||
}
|
||||
return columns;
|
||||
}
|
||||
|
||||
public static void getColumnFromExpr(Expression expression, Set<String> columns) {
|
||||
public static void getFieldsFromExpr(Expression expression, Set<String> columns) {
|
||||
if (expression instanceof Column) {
|
||||
columns.add(((Column) expression).getColumnName());
|
||||
}
|
||||
if (expression instanceof Function) {
|
||||
ExpressionList<?> expressionList = ((Function) expression).getParameters();
|
||||
for (Expression expr : expressionList) {
|
||||
getColumnFromExpr(expr, columns);
|
||||
getFieldsFromExpr(expr, columns);
|
||||
}
|
||||
}
|
||||
if (expression instanceof CaseExpression) {
|
||||
CaseExpression expr = (CaseExpression) expression;
|
||||
if (Objects.nonNull(expr.getWhenClauses())) {
|
||||
for (WhenClause whenClause : expr.getWhenClauses()) {
|
||||
getColumnFromExpr(whenClause.getWhenExpression(), columns);
|
||||
getColumnFromExpr(whenClause.getThenExpression(), columns);
|
||||
getFieldsFromExpr(whenClause.getWhenExpression(), columns);
|
||||
getFieldsFromExpr(whenClause.getThenExpression(), columns);
|
||||
}
|
||||
}
|
||||
if (Objects.nonNull(expr.getElseExpression())) {
|
||||
getColumnFromExpr(expr.getElseExpression(), columns);
|
||||
getFieldsFromExpr(expr.getElseExpression(), columns);
|
||||
}
|
||||
}
|
||||
if (expression instanceof BinaryExpression) {
|
||||
BinaryExpression expr = (BinaryExpression) expression;
|
||||
getColumnFromExpr(expr.getLeftExpression(), columns);
|
||||
getColumnFromExpr(expr.getRightExpression(), columns);
|
||||
getFieldsFromExpr(expr.getLeftExpression(), columns);
|
||||
getFieldsFromExpr(expr.getRightExpression(), columns);
|
||||
}
|
||||
if (expression instanceof InExpression) {
|
||||
InExpression inExpression = (InExpression) expression;
|
||||
getColumnFromExpr(inExpression.getLeftExpression(), columns);
|
||||
getFieldsFromExpr(inExpression.getLeftExpression(), columns);
|
||||
}
|
||||
if (expression instanceof Between) {
|
||||
Between between = (Between) expression;
|
||||
getColumnFromExpr(between.getLeftExpression(), columns);
|
||||
getFieldsFromExpr(between.getLeftExpression(), columns);
|
||||
}
|
||||
if (expression instanceof IsBooleanExpression) {
|
||||
IsBooleanExpression isBooleanExpression = (IsBooleanExpression) expression;
|
||||
getColumnFromExpr(isBooleanExpression.getLeftExpression(), columns);
|
||||
getFieldsFromExpr(isBooleanExpression.getLeftExpression(), columns);
|
||||
}
|
||||
if (expression instanceof IsNullExpression) {
|
||||
IsNullExpression isNullExpression = (IsNullExpression) expression;
|
||||
getColumnFromExpr(isNullExpression.getLeftExpression(), columns);
|
||||
getFieldsFromExpr(isNullExpression.getLeftExpression(), columns);
|
||||
}
|
||||
if (expression instanceof Parenthesis) {
|
||||
Parenthesis expr = (Parenthesis) expression;
|
||||
getColumnFromExpr(expr.getExpression(), columns);
|
||||
getFieldsFromExpr(expr.getExpression(), columns);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -949,4 +949,31 @@ public class SqlSelectHelper {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void addMissingGroupby(PlainSelect plainSelect) {
|
||||
if (Objects.nonNull(plainSelect.getGroupBy())
|
||||
&& !plainSelect.getGroupBy().getGroupByExpressionList().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
GroupByElement groupBy = new GroupByElement();
|
||||
for (SelectItem selectItem : plainSelect.getSelectItems()) {
|
||||
Expression expression = selectItem.getExpression();
|
||||
if (expression instanceof Column) {
|
||||
groupBy.addGroupByExpression(expression);
|
||||
}
|
||||
}
|
||||
if (!groupBy.getGroupByExpressionList().isEmpty()) {
|
||||
plainSelect.setGroupByElement(groupBy);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean hasAggregateFunction(PlainSelect plainSelect) {
|
||||
List<SelectItem<?>> selectItems = plainSelect.getSelectItems();
|
||||
FunctionVisitor visitor = new FunctionVisitor();
|
||||
for (SelectItem selectItem : selectItems) {
|
||||
selectItem.accept(visitor);
|
||||
}
|
||||
return !visitor.getFunctionNames().isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user