mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-14 13:47:09 +00:00
(improvement)(chat) aggregator supports from chinese to english in s2sql (#371)
This commit is contained in:
@@ -65,5 +65,4 @@ public class Constants {
|
||||
public static final Long DEFAULT_FREQUENCY = 100000L;
|
||||
|
||||
public static final String TABLE_PREFIX = "t_";
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.tencent.supersonic.common.util.jsqlparser;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public enum AggregateEnum {
|
||||
MOST("最多", "max"),
|
||||
HIGHEST("最高", "max"),
|
||||
MAXIMUN("最大", "max"),
|
||||
LEAST("最少", "min"),
|
||||
SMALLEST("最小", "min"),
|
||||
LOWEST("最低", "min"),
|
||||
AVERAGE("平均", "avg");
|
||||
private String aggregateCh;
|
||||
private String aggregateEN;
|
||||
|
||||
AggregateEnum(String aggregateCh, String aggregateEN) {
|
||||
this.aggregateCh = aggregateCh;
|
||||
this.aggregateEN = aggregateEN;
|
||||
}
|
||||
|
||||
public String getAggregateCh() {
|
||||
return aggregateCh;
|
||||
}
|
||||
|
||||
public String getAggregateEN() {
|
||||
return aggregateEN;
|
||||
}
|
||||
|
||||
public static Map<String, String> getAggregateEnum() {
|
||||
Map<String, String> aggregateMap = Arrays.stream(AggregateEnum.values())
|
||||
.collect(Collectors.toMap(AggregateEnum::getAggregateCh, AggregateEnum::getAggregateEN));
|
||||
return aggregateMap;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,19 @@
|
||||
package com.tencent.supersonic.common.util.jsqlparser;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.JSQLParserException;
|
||||
import net.sf.jsqlparser.expression.BinaryExpression;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.Parenthesis;
|
||||
import net.sf.jsqlparser.expression.LongValue;
|
||||
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
||||
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
|
||||
import net.sf.jsqlparser.expression.operators.relational.ComparisonOperator;
|
||||
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
||||
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
|
||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
|
||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
|
||||
import net.sf.jsqlparser.expression.operators.relational.InExpression;
|
||||
@@ -49,6 +54,21 @@ public class SqlParserRemoveHelper {
|
||||
}
|
||||
removeWhereExpression(whereExpression, removeFieldNames);
|
||||
}
|
||||
public static String removeWhereCondition(String sql) {
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectBody instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
Expression where = ((PlainSelect) selectBody).getWhere();
|
||||
Expression having = ((PlainSelect) selectBody).getHaving();
|
||||
where = filteredWhereExpression(where);
|
||||
having = filteredWhereExpression(having);
|
||||
((PlainSelect) selectBody).setWhere(where);
|
||||
((PlainSelect) selectBody).setHaving(having);
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
private static void removeWhereExpression(Expression whereExpression, Set<String> removeFieldNames) {
|
||||
if (SqlParserSelectHelper.isLogicExpression(whereExpression)) {
|
||||
@@ -171,5 +191,78 @@ public class SqlParserRemoveHelper {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
private static Expression filteredWhereExpression(Expression where) {
|
||||
if (Objects.isNull(where)) {
|
||||
return null;
|
||||
}
|
||||
if (where instanceof Parenthesis) {
|
||||
Expression expression = filteredWhereExpression(((Parenthesis) where).getExpression());
|
||||
if (expression != null) {
|
||||
try {
|
||||
Expression parseExpression = CCJSqlParserUtil.parseExpression("(" + expression + ")");
|
||||
return parseExpression;
|
||||
} catch (JSQLParserException jsqlParserException) {
|
||||
log.info("jsqlParser has an exception:{}", jsqlParserException.toString());
|
||||
}
|
||||
} else {
|
||||
return expression;
|
||||
}
|
||||
} else if (where instanceof AndExpression) {
|
||||
AndExpression andExpression = (AndExpression) where;
|
||||
return filteredNumberExpression(andExpression);
|
||||
} else if (where instanceof OrExpression) {
|
||||
OrExpression orExpression = (OrExpression) where;
|
||||
return filteredNumberExpression(orExpression);
|
||||
} else {
|
||||
return replaceComparisonOperatorFunction(where);
|
||||
}
|
||||
return where;
|
||||
}
|
||||
|
||||
private static <T extends BinaryExpression> Expression filteredNumberExpression(T binaryExpression) {
|
||||
Expression leftExpression = filteredWhereExpression(binaryExpression.getLeftExpression());
|
||||
Expression rightExpression = filteredWhereExpression(binaryExpression.getRightExpression());
|
||||
if (leftExpression != null && rightExpression != null) {
|
||||
binaryExpression.setLeftExpression(leftExpression);
|
||||
binaryExpression.setRightExpression(rightExpression);
|
||||
return binaryExpression;
|
||||
} else if (leftExpression != null && rightExpression == null) {
|
||||
return leftExpression;
|
||||
} else if (leftExpression == null && rightExpression != null) {
|
||||
return rightExpression;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Expression replaceComparisonOperatorFunction(Expression expression) {
|
||||
if (Objects.isNull(expression)) {
|
||||
return null;
|
||||
}
|
||||
if (expression instanceof GreaterThanEquals) {
|
||||
return removeSingleFilter((GreaterThanEquals) expression);
|
||||
} else if (expression instanceof GreaterThan) {
|
||||
return removeSingleFilter((GreaterThan) expression);
|
||||
} else if (expression instanceof MinorThan) {
|
||||
return removeSingleFilter((MinorThan) expression);
|
||||
} else if (expression instanceof MinorThanEquals) {
|
||||
return removeSingleFilter((MinorThanEquals) expression);
|
||||
} else if (expression instanceof EqualsTo) {
|
||||
return removeSingleFilter((EqualsTo) expression);
|
||||
} else if (expression instanceof NotEqualsTo) {
|
||||
return removeSingleFilter((NotEqualsTo) expression);
|
||||
}
|
||||
return expression;
|
||||
}
|
||||
|
||||
private static <T extends ComparisonOperator> Expression removeSingleFilter(T comparisonExpression) {
|
||||
Expression leftExpression = comparisonExpression.getLeftExpression();
|
||||
if (leftExpression instanceof LongValue) {
|
||||
return null;
|
||||
} else {
|
||||
return comparisonExpression;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -6,11 +6,22 @@ import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.Function;
|
||||
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
||||
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
|
||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
|
||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
|
||||
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
|
||||
import net.sf.jsqlparser.expression.operators.relational.MinorThan;
|
||||
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
||||
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
|
||||
import net.sf.jsqlparser.expression.operators.relational.ComparisonOperator;
|
||||
import net.sf.jsqlparser.statement.select.GroupByElement;
|
||||
import net.sf.jsqlparser.statement.select.OrderByElement;
|
||||
import net.sf.jsqlparser.statement.select.PlainSelect;
|
||||
import net.sf.jsqlparser.statement.select.Select;
|
||||
import net.sf.jsqlparser.statement.select.SubSelect;
|
||||
import net.sf.jsqlparser.statement.select.Join;
|
||||
import net.sf.jsqlparser.statement.select.SelectBody;
|
||||
import net.sf.jsqlparser.statement.select.SelectItem;
|
||||
import net.sf.jsqlparser.statement.select.SelectVisitorAdapter;
|
||||
@@ -110,6 +121,17 @@ public class SqlParserReplaceHelper {
|
||||
if (Objects.nonNull(having)) {
|
||||
having.accept(visitor);
|
||||
}
|
||||
List<Join> joins = plainSelect.getJoins();
|
||||
if (!CollectionUtils.isEmpty(joins)) {
|
||||
for (Join join : joins) {
|
||||
join.getOnExpression().accept(visitor);
|
||||
SelectBody subSelectBody = ((SubSelect) join.getRightItem()).getSelectBody();
|
||||
List<PlainSelect> subPlainSelects = SqlParserSelectHelper.getPlainSelects((PlainSelect) subSelectBody);
|
||||
for (PlainSelect subPlainSelect : subPlainSelects) {
|
||||
replaceFieldsInPlainOneSelect(fieldNameMap, exactReplace, subPlainSelect);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String replaceFunction(String sql, Map<String, String> functionMap) {
|
||||
@@ -143,6 +165,12 @@ public class SqlParserReplaceHelper {
|
||||
for (SelectItem selectItem : plainSelect.getSelectItems()) {
|
||||
selectItem.accept(visitor);
|
||||
}
|
||||
Expression having = plainSelect.getHaving();
|
||||
if (Objects.nonNull(having)) {
|
||||
replaceHavingFunction(functionMap, having);
|
||||
}
|
||||
List<OrderByElement> orderByElementList = plainSelect.getOrderByElements();
|
||||
replaceOrderByFunction(functionMap, orderByElementList);
|
||||
}
|
||||
|
||||
public static String replaceFunction(String sql) {
|
||||
@@ -172,6 +200,67 @@ public class SqlParserReplaceHelper {
|
||||
addWaitingExpression(plainSelect, where, waitingForAdds);
|
||||
}
|
||||
|
||||
private static void replaceHavingFunction(Map<String, String> functionMap, Expression having) {
|
||||
if (Objects.nonNull(having)) {
|
||||
if (having instanceof AndExpression) {
|
||||
AndExpression andExpression = (AndExpression) having;
|
||||
replaceHavingFunction(functionMap, andExpression.getLeftExpression());
|
||||
replaceHavingFunction(functionMap, andExpression.getRightExpression());
|
||||
} else if (having instanceof OrExpression) {
|
||||
OrExpression orExpression = (OrExpression) having;
|
||||
replaceHavingFunction(functionMap, orExpression.getLeftExpression());
|
||||
replaceHavingFunction(functionMap, orExpression.getRightExpression());
|
||||
} else {
|
||||
replaceComparisonOperatorFunction(functionMap, having);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void replaceComparisonOperatorFunction(Map<String, String> functionMap, Expression expression) {
|
||||
if (Objects.isNull(expression)) {
|
||||
return;
|
||||
}
|
||||
if (expression instanceof GreaterThanEquals) {
|
||||
replaceFilterFunction(functionMap, (GreaterThanEquals) expression);
|
||||
} else if (expression instanceof GreaterThan) {
|
||||
replaceFilterFunction(functionMap, (GreaterThan) expression);
|
||||
} else if (expression instanceof MinorThan) {
|
||||
replaceFilterFunction(functionMap, (MinorThan) expression);
|
||||
} else if (expression instanceof MinorThanEquals) {
|
||||
replaceFilterFunction(functionMap, (MinorThanEquals) expression);
|
||||
} else if (expression instanceof EqualsTo) {
|
||||
replaceFilterFunction(functionMap, (EqualsTo) expression);
|
||||
} else if (expression instanceof NotEqualsTo) {
|
||||
replaceFilterFunction(functionMap, (NotEqualsTo) expression);
|
||||
}
|
||||
}
|
||||
|
||||
private static void replaceOrderByFunction(Map<String, String> functionMap,
|
||||
List<OrderByElement> orderByElementList) {
|
||||
if (Objects.isNull(orderByElementList)) {
|
||||
return;
|
||||
}
|
||||
for (OrderByElement orderByElement : orderByElementList) {
|
||||
if (orderByElement.getExpression() instanceof Function) {
|
||||
Function function = (Function) orderByElement.getExpression();
|
||||
if (functionMap.containsKey(function.getName())) {
|
||||
function.setName(functionMap.get(function.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static <T extends ComparisonOperator> void replaceFilterFunction(
|
||||
Map<String, String> functionMap, T comparisonExpression) {
|
||||
Expression expression = comparisonExpression.getLeftExpression();
|
||||
if (expression instanceof Function) {
|
||||
Function function = (Function) expression;
|
||||
if (functionMap.containsKey(function.getName())) {
|
||||
function.setName(functionMap.get(function.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void addWaitingExpression(PlainSelect plainSelect, Expression where,
|
||||
List<Expression> waitingForAdds) {
|
||||
if (CollectionUtils.isEmpty(waitingForAdds)) {
|
||||
@@ -204,6 +293,17 @@ public class SqlParserReplaceHelper {
|
||||
plainSelect.getFromItem().accept(new TableNameReplaceVisitor(tableName));
|
||||
}
|
||||
});
|
||||
List<Join> joins = painSelect.getJoins();
|
||||
if (!CollectionUtils.isEmpty(joins)) {
|
||||
for (Join join : joins) {
|
||||
SelectBody subSelectBody = ((SubSelect) join.getRightItem()).getSelectBody();
|
||||
List<PlainSelect> subPlainSelects = SqlParserSelectHelper.getPlainSelects(
|
||||
(PlainSelect) subSelectBody);
|
||||
for (PlainSelect subPlainSelect : subPlainSelects) {
|
||||
subPlainSelect.getFromItem().accept(new TableNameReplaceVisitor(tableName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user