mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-14 22:25:19 +00:00
[release][project] supersonic 0.7.3 version backend update (#40)
* [improvement] add some features * [improvement] revise CHANGELOG --------- Co-authored-by: zuopengge <hwzuopengge@tencent.com>
This commit is contained in:
@@ -53,11 +53,11 @@ public class DateConf {
|
||||
return false;
|
||||
}
|
||||
DateConf dateConf = (DateConf) o;
|
||||
return dateMode == dateConf.dateMode &&
|
||||
Objects.equals(startDate, dateConf.startDate) &&
|
||||
Objects.equals(endDate, dateConf.endDate) &&
|
||||
Objects.equals(unit, dateConf.unit) &&
|
||||
Objects.equals(period, dateConf.period);
|
||||
return dateMode == dateConf.dateMode
|
||||
&& Objects.equals(startDate, dateConf.startDate)
|
||||
&& Objects.equals(endDate, dateConf.endDate)
|
||||
&& Objects.equals(unit, dateConf.unit)
|
||||
&& Objects.equals(period, dateConf.period);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,4 +16,8 @@ public class QueryAuthorization {
|
||||
private List<String> dimensionFilters;
|
||||
private List<String> dimensionFiltersDesc;
|
||||
private String message;
|
||||
|
||||
public QueryAuthorization(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@@ -33,9 +33,9 @@ public class ChatGptHelper {
|
||||
private Integer proxyPort;
|
||||
|
||||
|
||||
public ChatGPT getChatGPT(){
|
||||
public ChatGPT getChatGPT() {
|
||||
Proxy proxy = null;
|
||||
if (!"default".equals(proxyIp)){
|
||||
if (!"default".equals(proxyIp)) {
|
||||
proxy = Proxys.http(proxyIp, proxyPort);
|
||||
}
|
||||
return ChatGPT.builder()
|
||||
@@ -47,8 +47,8 @@ public class ChatGptHelper {
|
||||
.init();
|
||||
}
|
||||
|
||||
public Message getChatCompletion(Message system,Message message){
|
||||
ChatCompletion chatCompletion = ChatCompletion.builder()
|
||||
public Message getChatCompletion(Message system, Message message) {
|
||||
ChatCompletion chatCompletion = ChatCompletion.builder()
|
||||
.model(ChatCompletion.Model.GPT_3_5_TURBO_16K.getName())
|
||||
.messages(Arrays.asList(system, message))
|
||||
.maxTokens(10000)
|
||||
@@ -58,41 +58,61 @@ public class ChatGptHelper {
|
||||
return response.getChoices().get(0).getMessage();
|
||||
}
|
||||
|
||||
public String inferredTime(String queryText){
|
||||
public String inferredTime(String queryText) {
|
||||
long nowTime = System.currentTimeMillis();
|
||||
Date date = new Date(nowTime);
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
String formattedDate = sdf.format(date);
|
||||
Message system = Message.ofSystem("现在时间 "+formattedDate+",你是一个专业的数据分析师,你的任务是基于数据,专业的解答用户的问题。" +
|
||||
"你需要遵守以下规则:\n" +
|
||||
"1.返回规范的数据格式,json,如: 输入:近 10 天的日活跃数,输出:{\"start\":\"2023-07-21\",\"end\":\"2023-07-31\"}" +
|
||||
"2.你对时间数据要求规范,能从近 10 天,国庆节,端午节,获取到相应的时间,填写到 json 中。\n"+
|
||||
"3.你的数据时间,只有当前及之前时间即可,超过则回复去年\n" +
|
||||
"4.只需要解析出时间,时间可以是时间月和年或日、日历采用公历\n"+
|
||||
"5.时间给出要是绝对正确,不能瞎编\n"
|
||||
Message system = Message.ofSystem("现在时间 " + formattedDate + ",你是一个专业的数据分析师,你的任务是基于数据,专业的解答用户的问题。"
|
||||
+ "你需要遵守以下规则:\n"
|
||||
+ "1.返回规范的数据格式,json,如: 输入:近 10 天的日活跃数,输出:{\"start\":\"2023-07-21\",\"end\":\"2023-07-31\"}"
|
||||
+ "2.你对时间数据要求规范,能从近 10 天,国庆节,端午节,获取到相应的时间,填写到 json 中。\n"
|
||||
+ "3.你的数据时间,只有当前及之前时间即可,超过则回复去年\n"
|
||||
+ "4.只需要解析出时间,时间可以是时间月和年或日、日历采用公历\n"
|
||||
+ "5.时间给出要是绝对正确,不能瞎编\n"
|
||||
);
|
||||
Message message = Message.of("输入:"+queryText+",输出:");
|
||||
Message message = Message.of("输入:" + queryText + ",输出:");
|
||||
Message res = getChatCompletion(system, message);
|
||||
return res.getContent();
|
||||
}
|
||||
|
||||
|
||||
public String mockAlias(String mockType,String name,String bizName,String table,String desc,Boolean isPercentage){
|
||||
String msg = "Assuming you are a professional data analyst specializing in indicators, you have a vast amount of data analysis indicator content. You are familiar with the basic format of the content,Now, Construct your answer Based on the following json-schema.\n" +
|
||||
"{\n" +
|
||||
"\"$schema\": \"http://json-schema.org/draft-07/schema#\",\n" +
|
||||
"\"type\": \"array\",\n" +
|
||||
"\"minItems\": 2,\n" +
|
||||
"\"maxItems\": 4,\n" +
|
||||
"\"items\": {\n" +
|
||||
"\"type\": \"string\",\n" +
|
||||
"\"description\": \"Assuming you are a data analyst and give a defined "+mockType+" name: " +name+","+
|
||||
"this "+mockType+" is from database and table: "+table+ ",This "+mockType+" calculates the field source: "+bizName+", The description of this indicator is: "+desc+", provide some aliases for this,please take chinese or english,but more chinese and Not repeating,\"\n" +
|
||||
"},\n" +
|
||||
"\"additionalProperties\":false}\n" +
|
||||
"Please double-check whether the answer conforms to the format described in the JSON-schema.\n" +
|
||||
"ANSWER JSON:";
|
||||
log.info("msg:{}",msg);
|
||||
public String mockAlias(String mockType,
|
||||
String name,
|
||||
String bizName,
|
||||
String table,
|
||||
String desc,
|
||||
Boolean isPercentage) {
|
||||
String msg = "Assuming you are a professional data analyst specializing in indicators, "
|
||||
+ "you have a vast amount of data analysis indicator content. You are familiar with the basic"
|
||||
+ " format of the content,Now, Construct your answer Based on the following json-schema.\n"
|
||||
+ "{\n"
|
||||
+ "\"$schema\": \"http://json-schema.org/draft-07/schema#\",\n"
|
||||
+ "\"type\": \"array\",\n"
|
||||
+ "\"minItems\": 2,\n"
|
||||
+ "\"maxItems\": 4,\n"
|
||||
+ "\"items\": {\n"
|
||||
+ "\"type\": \"string\",\n"
|
||||
+ "\"description\": \"Assuming you are a data analyst and give a defined "
|
||||
+ mockType
|
||||
+ " name: "
|
||||
+ name + ","
|
||||
+ "this "
|
||||
+ mockType
|
||||
+ " is from database and table: "
|
||||
+ table + ",This "
|
||||
+ mockType
|
||||
+ " calculates the field source: "
|
||||
+ bizName
|
||||
+ ", The description of this indicator is: "
|
||||
+ desc
|
||||
+ ", provide some aliases for this,please take chinese or english,"
|
||||
+ "but more chinese and Not repeating,\"\n"
|
||||
+ "},\n"
|
||||
+ "\"additionalProperties\":false}\n"
|
||||
+ "Please double-check whether the answer conforms to the format described in the JSON-schema.\n"
|
||||
+ "ANSWER JSON:";
|
||||
log.info("msg:{}", msg);
|
||||
Message system = Message.ofSystem("");
|
||||
Message message = Message.of(msg);
|
||||
Message res = getChatCompletion(system, message);
|
||||
@@ -100,17 +120,19 @@ public class ChatGptHelper {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String mockDimensionValueAlias(String json){
|
||||
String msg = "Assuming you are a professional data analyst specializing in indicators,for you a json list," +
|
||||
"the required content to follow is as follows: " +
|
||||
"1. The format of JSON," +
|
||||
"2. Only return in JSON format," +
|
||||
"3. the array item > 1 and < 5,more alias," +
|
||||
"for example:input:[\"qq_music\",\"kugou_music\"],out:{\"tran\":[\"qq音乐\",\"酷狗音乐\"],\"alias\":{\"qq_music\":[\"q音\",\"qq音乐\"],\"kugou_music\":[\"kugou\",\"酷狗\"]}}," +
|
||||
"now input: " + json + ","+
|
||||
"answer json:";
|
||||
log.info("msg:{}",msg);
|
||||
public String mockDimensionValueAlias(String json) {
|
||||
String msg = "Assuming you are a professional data analyst specializing in indicators,for you a json list,"
|
||||
+ "the required content to follow is as follows: "
|
||||
+ "1. The format of JSON,"
|
||||
+ "2. Only return in JSON format,"
|
||||
+ "3. the array item > 1 and < 5,more alias,"
|
||||
+ "for example:input:[\"qq_music\",\"kugou_music\"],"
|
||||
+ "out:{\"tran\":[\"qq音乐\",\"酷狗音乐\"],\"alias\":{\"qq_music\":[\"q音\",\"qq音乐\"],"
|
||||
+ "\"kugou_music\":[\"kugou\",\"酷狗\"]}},"
|
||||
+ "now input: "
|
||||
+ json + ","
|
||||
+ "answer json:";
|
||||
log.info("msg:{}", msg);
|
||||
Message system = Message.ofSystem("");
|
||||
Message message = Message.of(msg);
|
||||
Message res = getChatCompletion(system, message);
|
||||
@@ -118,12 +140,9 @@ public class ChatGptHelper {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
ChatGptHelper chatGptHelper = new ChatGptHelper();
|
||||
System.out.println(chatGptHelper.mockAlias("","","","","",false));
|
||||
System.out.println(chatGptHelper.mockAlias("", "", "", "", "", false));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ public class DateUtils {
|
||||
dateFormat.parse(date);
|
||||
return DateTimeFormatter.ofPattern(format);
|
||||
} catch (Exception e) {
|
||||
log.info("date parse has a exception:{}", e.toString());
|
||||
}
|
||||
}
|
||||
return DateTimeFormatter.ofPattern(formats[0]);
|
||||
@@ -44,6 +45,7 @@ public class DateUtils {
|
||||
LocalDateTime.parse(date, dateTimeFormatter);
|
||||
return dateTimeFormatter;
|
||||
} catch (Exception e) {
|
||||
log.info("date parse has a exception:{}", e.toString());
|
||||
}
|
||||
}
|
||||
return DateTimeFormatter.ofPattern(formats[0]);
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.tencent.supersonic.common.util.jsqlparser;
|
||||
|
||||
import java.util.Set;
|
||||
import net.sf.jsqlparser.expression.DoubleValue;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
|
||||
import net.sf.jsqlparser.expression.LongValue;
|
||||
import net.sf.jsqlparser.expression.StringValue;
|
||||
import net.sf.jsqlparser.expression.operators.relational.ComparisonOperator;
|
||||
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
|
||||
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
|
||||
import net.sf.jsqlparser.expression.operators.relational.MinorThan;
|
||||
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
|
||||
public class FieldAndValueAcquireVisitor extends ExpressionVisitorAdapter {
|
||||
|
||||
private Set<FilterExpression> filterExpressions;
|
||||
|
||||
public FieldAndValueAcquireVisitor(Set<FilterExpression> filterExpressions) {
|
||||
this.filterExpressions = filterExpressions;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void visit(MinorThan expr) {
|
||||
FilterExpression filterExpression = getFilterExpression(expr);
|
||||
filterExpressions.add(filterExpression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(EqualsTo expr) {
|
||||
FilterExpression filterExpression = getFilterExpression(expr);
|
||||
filterExpressions.add(filterExpression);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void visit(MinorThanEquals expr) {
|
||||
FilterExpression filterExpression = getFilterExpression(expr);
|
||||
filterExpressions.add(filterExpression);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void visit(GreaterThan expr) {
|
||||
FilterExpression filterExpression = getFilterExpression(expr);
|
||||
filterExpressions.add(filterExpression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(GreaterThanEquals expr) {
|
||||
FilterExpression filterExpression = getFilterExpression(expr);
|
||||
filterExpressions.add(filterExpression);
|
||||
}
|
||||
|
||||
private FilterExpression getFilterExpression(ComparisonOperator expr) {
|
||||
Expression leftExpression = expr.getLeftExpression();
|
||||
Expression rightExpression = expr.getRightExpression();
|
||||
|
||||
FilterExpression filterExpression = new FilterExpression();
|
||||
String columnName = null;
|
||||
if (leftExpression instanceof Column) {
|
||||
Column column = (Column) leftExpression;
|
||||
columnName = column.getColumnName();
|
||||
filterExpression.setFieldName(columnName);
|
||||
}
|
||||
if (rightExpression instanceof StringValue) {
|
||||
StringValue stringValue = (StringValue) rightExpression;
|
||||
filterExpression.setFieldValue(stringValue.getValue());
|
||||
}
|
||||
if (rightExpression instanceof DoubleValue) {
|
||||
DoubleValue doubleValue = (DoubleValue) rightExpression;
|
||||
filterExpression.setFieldValue(doubleValue.getValue());
|
||||
}
|
||||
if (rightExpression instanceof LongValue) {
|
||||
LongValue longValue = (LongValue) rightExpression;
|
||||
filterExpression.setFieldValue(longValue.getValue());
|
||||
}
|
||||
filterExpression.setOperator(expr.getStringExpression());
|
||||
return filterExpression;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.tencent.supersonic.common.util.jsqlparser;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
|
||||
import net.sf.jsqlparser.expression.StringValue;
|
||||
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
public class FiledValueReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
|
||||
private Map<String, Set<String>> fieldValueToFieldNames;
|
||||
|
||||
public FiledValueReplaceVisitor(Map<String, Set<String>> fieldValueToFieldNames) {
|
||||
this.fieldValueToFieldNames = fieldValueToFieldNames;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(EqualsTo expr) {
|
||||
Expression leftExpression = expr.getLeftExpression();
|
||||
Expression rightExpression = expr.getRightExpression();
|
||||
if (!(rightExpression instanceof StringValue)) {
|
||||
return;
|
||||
}
|
||||
if (!(leftExpression instanceof Column)) {
|
||||
return;
|
||||
}
|
||||
if (CollectionUtils.isEmpty(fieldValueToFieldNames)) {
|
||||
return;
|
||||
}
|
||||
if (Objects.isNull(rightExpression) || Objects.isNull(leftExpression)) {
|
||||
return;
|
||||
}
|
||||
Column leftColumnName = (Column) leftExpression;
|
||||
StringValue rightStringValue = (StringValue) rightExpression;
|
||||
|
||||
Set<String> fieldNames = fieldValueToFieldNames.get(rightStringValue.getValue());
|
||||
if (!CollectionUtils.isEmpty(fieldNames) && !fieldNames.contains(leftColumnName.getColumnName())) {
|
||||
leftColumnName.setColumnName(fieldNames.stream().findFirst().get());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.tencent.supersonic.common.util.jsqlparser;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class FilterExpression {
|
||||
|
||||
private String operator;
|
||||
|
||||
private String fieldName;
|
||||
|
||||
private Object fieldValue;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.tencent.supersonic.common.util.jsqlparser;
|
||||
|
||||
import java.util.Set;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
import net.sf.jsqlparser.statement.select.OrderByElement;
|
||||
import net.sf.jsqlparser.statement.select.OrderByVisitorAdapter;
|
||||
|
||||
public class OrderByAcquireVisitor extends OrderByVisitorAdapter {
|
||||
|
||||
private Set<String> fields;
|
||||
|
||||
public OrderByAcquireVisitor(Set<String> fields) {
|
||||
this.fields = fields;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(OrderByElement orderBy) {
|
||||
Expression expression = orderBy.getExpression();
|
||||
if (expression instanceof Column) {
|
||||
fields.add(((Column) expression).getColumnName());
|
||||
}
|
||||
super.visit(orderBy);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
package com.tencent.supersonic.common.util.jsqlparser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
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.Expression;
|
||||
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
|
||||
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
import net.sf.jsqlparser.schema.Table;
|
||||
import net.sf.jsqlparser.statement.Statement;
|
||||
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.SelectBody;
|
||||
import net.sf.jsqlparser.statement.select.SelectItem;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* Sql Parser Select Helper
|
||||
*/
|
||||
@Slf4j
|
||||
public class SqlParserSelectHelper {
|
||||
public static List<FilterExpression> getFilterExpression(String sql) {
|
||||
PlainSelect plainSelect = getPlainSelect(sql);
|
||||
if (Objects.isNull(plainSelect)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Set<FilterExpression> result = new HashSet<>();
|
||||
Expression where = plainSelect.getWhere();
|
||||
if (Objects.nonNull(where)) {
|
||||
where.accept(new FieldAndValueAcquireVisitor(result));
|
||||
}
|
||||
return new ArrayList<>(result);
|
||||
}
|
||||
|
||||
public static List<String> getWhereFields(String sql) {
|
||||
PlainSelect plainSelect = getPlainSelect(sql);
|
||||
if (Objects.isNull(plainSelect)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Set<String> result = new HashSet<>();
|
||||
Expression where = plainSelect.getWhere();
|
||||
if (Objects.nonNull(where)) {
|
||||
where.accept(new FieldAcquireVisitor(result));
|
||||
}
|
||||
return new ArrayList<>(result);
|
||||
}
|
||||
|
||||
public static List<String> getOrderByFields(String sql) {
|
||||
PlainSelect plainSelect = getPlainSelect(sql);
|
||||
if (Objects.isNull(plainSelect)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Set<String> result = new HashSet<>();
|
||||
List<OrderByElement> orderByElements = plainSelect.getOrderByElements();
|
||||
if (!CollectionUtils.isEmpty(orderByElements)) {
|
||||
for (OrderByElement orderByElement : orderByElements) {
|
||||
orderByElement.accept(new OrderByAcquireVisitor(result));
|
||||
}
|
||||
}
|
||||
return new ArrayList<>(result);
|
||||
}
|
||||
|
||||
public static List<String> getSelectFields(String sql) {
|
||||
PlainSelect plainSelect = getPlainSelect(sql);
|
||||
if (Objects.isNull(plainSelect)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return new ArrayList<>(getSelectFields(plainSelect));
|
||||
}
|
||||
|
||||
public static Set<String> getSelectFields(PlainSelect plainSelect) {
|
||||
List<SelectItem> selectItems = plainSelect.getSelectItems();
|
||||
Set<String> result = new HashSet<>();
|
||||
for (SelectItem selectItem : selectItems) {
|
||||
selectItem.accept(new FieldAcquireVisitor(result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static PlainSelect getPlainSelect(String sql) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
if (selectStatement == null) {
|
||||
return null;
|
||||
}
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectBody instanceof PlainSelect)) {
|
||||
return null;
|
||||
}
|
||||
return (PlainSelect) selectBody;
|
||||
}
|
||||
|
||||
public static Select getSelect(String sql) {
|
||||
Statement statement = null;
|
||||
try {
|
||||
statement = CCJSqlParserUtil.parse(sql);
|
||||
} catch (JSQLParserException e) {
|
||||
log.error("parse error", e);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!(statement instanceof Select)) {
|
||||
return null;
|
||||
}
|
||||
return (Select) statement;
|
||||
}
|
||||
|
||||
|
||||
public static List<String> getAllFields(String sql) {
|
||||
|
||||
PlainSelect plainSelect = getPlainSelect(sql);
|
||||
if (Objects.isNull(plainSelect)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Set<String> result = getSelectFields(plainSelect);
|
||||
|
||||
GroupByElement groupBy = plainSelect.getGroupBy();
|
||||
if (groupBy != null) {
|
||||
List<Expression> groupByExpressions = groupBy.getGroupByExpressions();
|
||||
for (Expression expression : groupByExpressions) {
|
||||
if (expression instanceof Column) {
|
||||
Column column = (Column) expression;
|
||||
result.add(column.getColumnName());
|
||||
}
|
||||
}
|
||||
}
|
||||
List<OrderByElement> orderByElements = plainSelect.getOrderByElements();
|
||||
if (orderByElements != null) {
|
||||
for (OrderByElement orderByElement : orderByElements) {
|
||||
Expression expression = orderByElement.getExpression();
|
||||
|
||||
if (expression instanceof Column) {
|
||||
Column column = (Column) expression;
|
||||
result.add(column.getColumnName());
|
||||
}
|
||||
}
|
||||
}
|
||||
Expression where = plainSelect.getWhere();
|
||||
if (where != null) {
|
||||
where.accept(new ExpressionVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(Column column) {
|
||||
result.add(column.getColumnName());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return new ArrayList<>(result);
|
||||
}
|
||||
|
||||
public static String getTableName(String sql) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
if (selectStatement == null) {
|
||||
return null;
|
||||
}
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
PlainSelect plainSelect = (PlainSelect) selectBody;
|
||||
|
||||
Table table = (Table) plainSelect.getFromItem();
|
||||
return table.getName();
|
||||
}
|
||||
|
||||
|
||||
public static boolean hasAggregateFunction(String sql) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectBody instanceof PlainSelect)) {
|
||||
return false;
|
||||
}
|
||||
PlainSelect plainSelect = (PlainSelect) selectBody;
|
||||
List<SelectItem> selectItems = plainSelect.getSelectItems();
|
||||
AggregateFunctionVisitor visitor = new AggregateFunctionVisitor();
|
||||
for (SelectItem selectItem : selectItems) {
|
||||
selectItem.accept(visitor);
|
||||
}
|
||||
return visitor.hasAggregateFunction();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,23 +1,17 @@
|
||||
package com.tencent.supersonic.common.util.jsqlparser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.JSQLParserException;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
|
||||
import net.sf.jsqlparser.expression.LongValue;
|
||||
import net.sf.jsqlparser.expression.StringValue;
|
||||
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
|
||||
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
|
||||
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
import net.sf.jsqlparser.schema.Table;
|
||||
import net.sf.jsqlparser.statement.Statement;
|
||||
import net.sf.jsqlparser.statement.select.GroupByElement;
|
||||
import net.sf.jsqlparser.statement.select.OrderByElement;
|
||||
import net.sf.jsqlparser.statement.select.PlainSelect;
|
||||
@@ -29,115 +23,30 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* CC JSql ParserUtils
|
||||
* Sql Parser Update Helper
|
||||
*/
|
||||
@Slf4j
|
||||
public class CCJSqlParserUtils {
|
||||
public class SqlParserUpdateHelper {
|
||||
|
||||
public static List<String> getWhereFields(String sql) {
|
||||
PlainSelect plainSelect = getPlainSelect(sql);
|
||||
if (Objects.isNull(plainSelect)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Set<String> result = new HashSet<>();
|
||||
Expression where = plainSelect.getWhere();
|
||||
if (Objects.nonNull(where)) {
|
||||
where.accept(new FieldAcquireVisitor(result));
|
||||
}
|
||||
return new ArrayList<>(result);
|
||||
}
|
||||
|
||||
public static List<String> getSelectFields(String sql) {
|
||||
PlainSelect plainSelect = getPlainSelect(sql);
|
||||
if (Objects.isNull(plainSelect)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return new ArrayList<>(getSelectFields(plainSelect));
|
||||
}
|
||||
|
||||
private static Set<String> getSelectFields(PlainSelect plainSelect) {
|
||||
List<SelectItem> selectItems = plainSelect.getSelectItems();
|
||||
Set<String> result = new HashSet<>();
|
||||
for (SelectItem selectItem : selectItems) {
|
||||
selectItem.accept(new FieldAcquireVisitor(result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static PlainSelect getPlainSelect(String sql) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
if (selectStatement == null) {
|
||||
return null;
|
||||
}
|
||||
public static String replaceValueFields(String sql, Map<String, Set<String>> fieldValueToFieldNames) {
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectBody instanceof PlainSelect)) {
|
||||
return null;
|
||||
}
|
||||
return (PlainSelect) selectBody;
|
||||
}
|
||||
|
||||
private static Select getSelect(String sql) {
|
||||
Statement statement = null;
|
||||
try {
|
||||
statement = CCJSqlParserUtil.parse(sql);
|
||||
} catch (JSQLParserException e) {
|
||||
log.error("parse error", e);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!(statement instanceof Select)) {
|
||||
return null;
|
||||
}
|
||||
return (Select) statement;
|
||||
}
|
||||
|
||||
|
||||
public static List<String> getAllFields(String sql) {
|
||||
|
||||
PlainSelect plainSelect = getPlainSelect(sql);
|
||||
if (Objects.isNull(plainSelect)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Set<String> result = getSelectFields(plainSelect);
|
||||
|
||||
GroupByElement groupBy = plainSelect.getGroupBy();
|
||||
if (groupBy != null) {
|
||||
List<Expression> groupByExpressions = groupBy.getGroupByExpressions();
|
||||
for (Expression expression : groupByExpressions) {
|
||||
if (expression instanceof Column) {
|
||||
Column column = (Column) expression;
|
||||
result.add(column.getColumnName());
|
||||
}
|
||||
}
|
||||
}
|
||||
List<OrderByElement> orderByElements = plainSelect.getOrderByElements();
|
||||
if (orderByElements != null) {
|
||||
for (OrderByElement orderByElement : orderByElements) {
|
||||
Expression expression = orderByElement.getExpression();
|
||||
|
||||
if (expression instanceof Column) {
|
||||
Column column = (Column) expression;
|
||||
result.add(column.getColumnName());
|
||||
}
|
||||
}
|
||||
return sql;
|
||||
}
|
||||
PlainSelect plainSelect = (PlainSelect) selectBody;
|
||||
//1. replace where fields
|
||||
Expression where = plainSelect.getWhere();
|
||||
if (where != null) {
|
||||
where.accept(new ExpressionVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(Column column) {
|
||||
result.add(column.getColumnName());
|
||||
}
|
||||
});
|
||||
FiledValueReplaceVisitor visitor = new FiledValueReplaceVisitor(fieldValueToFieldNames);
|
||||
if (Objects.nonNull(where)) {
|
||||
where.accept(visitor);
|
||||
}
|
||||
|
||||
return new ArrayList<>(result);
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
|
||||
public static String replaceFields(String sql, Map<String, String> fieldToBizName) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
if (!(selectBody instanceof PlainSelect)) {
|
||||
return sql;
|
||||
@@ -173,7 +82,7 @@ public class CCJSqlParserUtils {
|
||||
|
||||
|
||||
public static String replaceFunction(String sql) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
if (!(selectBody instanceof PlainSelect)) {
|
||||
return sql;
|
||||
@@ -208,7 +117,7 @@ public class CCJSqlParserUtils {
|
||||
|
||||
|
||||
public static String addFieldsToSelect(String sql, List<String> fields) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
// add fields to select
|
||||
for (String field : fields) {
|
||||
SelectUtils.addExpression(selectStatement, new Column(field));
|
||||
@@ -216,24 +125,11 @@ public class CCJSqlParserUtils {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
|
||||
public static String getTableName(String sql) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
if (selectStatement == null) {
|
||||
return null;
|
||||
}
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
PlainSelect plainSelect = (PlainSelect) selectBody;
|
||||
|
||||
Table table = (Table) plainSelect.getFromItem();
|
||||
return table.getName();
|
||||
}
|
||||
|
||||
public static String replaceTable(String sql, String tableName) {
|
||||
if (StringUtils.isEmpty(tableName)) {
|
||||
return sql;
|
||||
}
|
||||
Select selectStatement = getSelect(sql);
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
PlainSelect plainSelect = (PlainSelect) selectBody;
|
||||
// replace table name
|
||||
@@ -247,7 +143,7 @@ public class CCJSqlParserUtils {
|
||||
if (StringUtils.isEmpty(column) || Objects.isNull(value)) {
|
||||
return sql;
|
||||
}
|
||||
Select selectStatement = getSelect(sql);
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectBody instanceof PlainSelect)) {
|
||||
@@ -271,7 +167,7 @@ public class CCJSqlParserUtils {
|
||||
|
||||
|
||||
public static String addWhere(String sql, Expression expression) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectBody instanceof PlainSelect)) {
|
||||
@@ -288,23 +184,5 @@ public class CCJSqlParserUtils {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
|
||||
public static boolean hasAggregateFunction(String sql) {
|
||||
Select selectStatement = getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
|
||||
if (!(selectBody instanceof PlainSelect)) {
|
||||
return false;
|
||||
}
|
||||
PlainSelect plainSelect = (PlainSelect) selectBody;
|
||||
List<SelectItem> selectItems = plainSelect.getSelectItems();
|
||||
AggregateFunctionVisitor visitor = new AggregateFunctionVisitor();
|
||||
for (SelectItem selectItem : selectItems) {
|
||||
selectItem.accept(visitor);
|
||||
}
|
||||
return visitor.hasAggregateFunction();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user