mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-11 12:07:42 +00:00
@@ -1,11 +1,14 @@
|
||||
package com.tencent.supersonic.common.util.jsqlparser;
|
||||
|
||||
import com.tencent.supersonic.common.pojo.enums.AggOperatorEnum;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.Function;
|
||||
@@ -23,6 +26,8 @@ import org.springframework.util.CollectionUtils;
|
||||
@Slf4j
|
||||
public class SqlSelectFunctionHelper {
|
||||
|
||||
public static List<String> aggregateFunctionName = Arrays.asList("SUM", "COUNT", "MAX", "MIN", "AVG");
|
||||
|
||||
public static boolean hasAggregateFunction(String sql) {
|
||||
if (!CollectionUtils.isEmpty(getFunctions(sql))) {
|
||||
return true;
|
||||
@@ -84,5 +89,22 @@ public class SqlSelectFunctionHelper {
|
||||
return sumFunction;
|
||||
}
|
||||
|
||||
public static String getFirstAggregateFunctions(String expr) {
|
||||
List<String> functions = getAggregateFunctions(expr);
|
||||
return CollectionUtils.isEmpty(functions) ? "" : functions.get(0);
|
||||
}
|
||||
|
||||
public static List<String> getAggregateFunctions(String expr) {
|
||||
Expression expression = QueryExpressionReplaceVisitor.getExpression(expr);
|
||||
if (Objects.nonNull(expression)) {
|
||||
FunctionVisitor visitor = new FunctionVisitor();
|
||||
expression.accept(visitor);
|
||||
Set<String> functions = visitor.getFunctionNames();
|
||||
return functions.stream()
|
||||
.filter(t -> aggregateFunctionName.contains(t.toUpperCase())).collect(Collectors.toList());
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ import com.tencent.supersonic.common.pojo.enums.StatusEnum;
|
||||
import com.tencent.supersonic.common.pojo.enums.TypeEnums;
|
||||
import com.tencent.supersonic.common.util.BeanMapper;
|
||||
import com.tencent.supersonic.common.util.ChatGptHelper;
|
||||
import com.tencent.supersonic.common.util.jsqlparser.SqlSelectFunctionHelper;
|
||||
import com.tencent.supersonic.headless.api.pojo.DrillDownDimension;
|
||||
import com.tencent.supersonic.headless.api.pojo.Measure;
|
||||
import com.tencent.supersonic.headless.api.pojo.MeasureParam;
|
||||
@@ -64,14 +65,6 @@ import com.tencent.supersonic.headless.server.service.TagMetaService;
|
||||
import com.tencent.supersonic.headless.server.utils.MetricCheckUtils;
|
||||
import com.tencent.supersonic.headless.server.utils.MetricConverter;
|
||||
import com.tencent.supersonic.headless.server.utils.ModelClusterBuilder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@@ -85,6 +78,13 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
@@ -700,11 +700,9 @@ public class MetricServiceImpl implements MetricService {
|
||||
public void batchFillMetricDefaultAgg(List<MetricResp> metricResps, List<ModelResp> modelResps) {
|
||||
Map<Long, ModelResp> modelRespMap = modelResps.stream().collect(Collectors.toMap(ModelResp::getId, m -> m));
|
||||
for (MetricResp metricResp : metricResps) {
|
||||
if (MetricDefineType.MEASURE.equals(metricResp.getMetricDefineType())) {
|
||||
fillDefaultAgg(metricResp, modelRespMap.get(metricResp.getModelId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void fillDefaultAgg(MetricResp metricResp) {
|
||||
if (MetricDefineType.MEASURE.equals(metricResp.getMetricDefineType())) {
|
||||
@@ -715,9 +713,28 @@ public class MetricServiceImpl implements MetricService {
|
||||
}
|
||||
|
||||
private void fillDefaultAgg(MetricResp metricResp, ModelResp modelResp) {
|
||||
if (modelResp == null) {
|
||||
if (modelResp == null || (Objects.nonNull(metricResp.getDefaultAgg()) && !metricResp.getDefaultAgg()
|
||||
.isEmpty())) {
|
||||
return;
|
||||
}
|
||||
// FIELD define will get from expr
|
||||
if (MetricDefineType.FIELD.equals(metricResp.getMetricDefineType())) {
|
||||
metricResp.setDefaultAgg(SqlSelectFunctionHelper.getFirstAggregateFunctions(metricResp.getExpr()));
|
||||
return;
|
||||
}
|
||||
// METRIC define will get from first metric
|
||||
if (MetricDefineType.METRIC.equals(metricResp.getMetricDefineType())) {
|
||||
if (!CollectionUtils.isEmpty(
|
||||
metricResp.getMetricDefineByMetricParams().getMetrics())) {
|
||||
MetricParam metricParam = metricResp.getMetricDefineByMetricParams().getMetrics().get(0);
|
||||
MetricResp firstMetricResp = getMetric(modelResp.getDomainId(), metricParam.getBizName());
|
||||
if (Objects.nonNull(firstMetricResp)) {
|
||||
fillDefaultAgg(firstMetricResp, modelResp);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Measure define will get from first measure
|
||||
List<Measure> measures = modelResp.getModelDetail().getMeasures();
|
||||
MeasureParam firstMeasure = metricResp.getMetricDefineByMeasureParams()
|
||||
.getMeasures().get(0);
|
||||
|
||||
Reference in New Issue
Block a user