mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-13 13:07:32 +00:00
(improvement)(chat) Switching metric supports default aggregation method of metric (#534)
Co-authored-by: jolunoluo
This commit is contained in:
@@ -20,6 +20,8 @@ public class DataItem {
|
||||
|
||||
private Long modelId;
|
||||
|
||||
private String defaultAgg;
|
||||
|
||||
public String getNewName() {
|
||||
return newName == null ? name : newName;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
package com.tencent.supersonic.common.util.jsqlparser;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.tencent.supersonic.common.pojo.enums.AggOperatorEnum;
|
||||
import com.tencent.supersonic.common.util.StringUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.JSQLParserException;
|
||||
@@ -32,10 +27,16 @@ import net.sf.jsqlparser.statement.select.SelectBody;
|
||||
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
|
||||
import net.sf.jsqlparser.statement.select.SelectItem;
|
||||
import net.sf.jsqlparser.statement.select.SelectVisitorAdapter;
|
||||
import net.sf.jsqlparser.statement.select.SubSelect;
|
||||
import net.sf.jsqlparser.statement.select.SetOperationList;
|
||||
import net.sf.jsqlparser.statement.select.SubSelect;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Sql Parser replace Helper
|
||||
@@ -81,6 +82,39 @@ public class SqlParserReplaceHelper {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static String replaceAggFields(String sql, Map<String, Pair<String, String>> fieldNameToAggMap) {
|
||||
Select selectStatement = SqlParserSelectHelper.getSelect(sql);
|
||||
SelectBody selectBody = selectStatement.getSelectBody();
|
||||
if (!(selectBody instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
((PlainSelect) selectBody).getSelectItems().stream().forEach(o -> {
|
||||
SelectExpressionItem selectExpressionItem = (SelectExpressionItem) o;
|
||||
if (selectExpressionItem.getExpression() instanceof Function) {
|
||||
Function function = (Function) selectExpressionItem.getExpression();
|
||||
Column column = (Column) function.getParameters().getExpressions().get(0);
|
||||
if (fieldNameToAggMap.containsKey(column.getColumnName())) {
|
||||
Pair<String, String> agg = fieldNameToAggMap.get(column.getColumnName());
|
||||
String field = agg.getKey();
|
||||
String func = agg.getRight();
|
||||
if (AggOperatorEnum.isCountDistinct(func)) {
|
||||
function.setName("count");
|
||||
function.setDistinct(true);
|
||||
} else {
|
||||
function.setName(func);
|
||||
}
|
||||
List<Expression> expressions = new ArrayList<>();
|
||||
expressions.add(new Column(field));
|
||||
function.getParameters().setExpressions(expressions);
|
||||
if (Objects.nonNull(selectExpressionItem.getAlias()) && StringUtils.isNotBlank(field)) {
|
||||
selectExpressionItem.getAlias().setName(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static String replaceValue(String sql, Map<String, Map<String, String>> filedNameToValueMap) {
|
||||
return replaceValue(sql, filedNameToValueMap, true);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
package com.tencent.supersonic.common.util.jsqlparser;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.HashSet;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.tencent.supersonic.common.pojo.enums.AggOperatorEnum;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* SqlParserReplaceHelperTest
|
||||
*/
|
||||
@@ -35,6 +37,18 @@ class SqlParserReplaceHelperTest {
|
||||
Assert.assertEquals("SELECT 维度1, 播放量1 FROM 数据库 WHERE (歌手名 = '张三') AND 数据日期 = '2023-11-17' GROUP BY 维度1", sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
void replaceAggField() {
|
||||
String sql = "SELECT 维度1,sum(播放量) FROM 数据库 "
|
||||
+ "WHERE (歌手名 = '张三') AND 数据日期 = '2023-11-17' GROUP BY 维度1";
|
||||
Map<String, Pair<String, String>> fieldMap = new HashMap<>();
|
||||
fieldMap.put("播放量", Pair.of("收听用户数", AggOperatorEnum.COUNT_DISTINCT.name()));
|
||||
sql = SqlParserReplaceHelper.replaceAggFields(sql, fieldMap);
|
||||
System.out.println(sql);
|
||||
Assert.assertEquals("SELECT 维度1, count(DISTINCT 收听用户数) FROM 数据库 "
|
||||
+ "WHERE (歌手名 = '张三') AND 数据日期 = '2023-11-17' GROUP BY 维度1", sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
void replaceValue() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user