(feature)(chat) support querying of dimension aliases and metric aliases (#260)

This commit is contained in:
lexluo09
2023-10-18 21:35:12 +08:00
committed by GitHub
parent ba1d14f40a
commit 34eb94320e
5 changed files with 68 additions and 37 deletions

View File

@@ -35,9 +35,18 @@ public abstract class BaseSemanticCorrector implements SemanticCorrector {
dbAllFields.addAll(semanticSchema.getMetrics()); dbAllFields.addAll(semanticSchema.getMetrics());
dbAllFields.addAll(semanticSchema.getDimensions()); dbAllFields.addAll(semanticSchema.getDimensions());
// support fieldName and field alias
Map<String, String> result = dbAllFields.stream() Map<String, String> result = dbAllFields.stream()
.filter(entry -> entry.getModel().equals(modelId)) .filter(entry -> entry.getModel().equals(modelId))
.collect(Collectors.toMap(SchemaElement::getName, a -> a.getName(), (k1, k2) -> k1)); .flatMap(schemaElement -> {
Set<String> elements = new HashSet<>();
elements.add(schemaElement.getName());
if (!CollectionUtils.isEmpty(schemaElement.getAlias())) {
elements.addAll(schemaElement.getAlias());
}
return elements.stream();
})
.collect(Collectors.toMap(a -> a, a -> a, (k1, k2) -> k1));
result.put(DateUtils.DATE_FIELD, DateUtils.DATE_FIELD); result.put(DateUtils.DATE_FIELD, DateUtils.DATE_FIELD);
return result; return result;
} }

View File

@@ -356,6 +356,9 @@ public class LLMS2QLParser implements SemanticParser {
llmReq.setLinking(linking); llmReq.setLinking(linking);
String currentDate = S2QLDateHelper.getReferenceDate(modelId); String currentDate = S2QLDateHelper.getReferenceDate(modelId);
if (StringUtils.isEmpty(currentDate)) {
currentDate = DateUtils.getBeforeDate(0);
}
llmReq.setCurrentDate(currentDate); llmReq.setCurrentDate(currentDate);
return llmReq; return llmReq;
} }
@@ -423,14 +426,13 @@ public class LLMS2QLParser implements SemanticParser {
|| SchemaElementType.VALUE.equals(elementType); || SchemaElementType.VALUE.equals(elementType);
}) })
.map(schemaElementMatch -> { .map(schemaElementMatch -> {
SchemaElementType elementType = schemaElementMatch.getElement().getType(); SchemaElement element = schemaElementMatch.getElement();
SchemaElementType elementType = element.getType();
if (!SchemaElementType.VALUE.equals(elementType)) { if (SchemaElementType.VALUE.equals(elementType)) {
return schemaElementMatch.getWord(); return itemIdToName.get(element.getId());
} }
return itemIdToName.get(schemaElementMatch.getElement().getId()); return schemaElementMatch.getWord();
}) })
.filter(name -> StringUtils.isNotEmpty(name) && !name.contains("%"))
.collect(Collectors.toSet()); .collect(Collectors.toSet());
return fieldNameList; return fieldNameList;
} }

View File

@@ -9,28 +9,24 @@ import com.tencent.supersonic.chat.api.pojo.SchemaValueMap;
import com.tencent.supersonic.semantic.api.model.pojo.DimValueMap; import com.tencent.supersonic.semantic.api.model.pojo.DimValueMap;
import com.tencent.supersonic.semantic.api.model.pojo.Entity; import com.tencent.supersonic.semantic.api.model.pojo.Entity;
import com.tencent.supersonic.semantic.api.model.pojo.RelateDimension; import com.tencent.supersonic.semantic.api.model.pojo.RelateDimension;
import com.tencent.supersonic.semantic.api.model.pojo.SchemaItem;
import com.tencent.supersonic.semantic.api.model.response.DimSchemaResp; import com.tencent.supersonic.semantic.api.model.response.DimSchemaResp;
import com.tencent.supersonic.semantic.api.model.response.MetricSchemaResp; import com.tencent.supersonic.semantic.api.model.response.MetricSchemaResp;
import com.tencent.supersonic.semantic.api.model.response.ModelSchemaResp; import com.tencent.supersonic.semantic.api.model.response.ModelSchemaResp;
import org.apache.commons.lang3.StringUtils; 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 org.apache.logging.log4j.util.Strings; import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.util.Set;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
import java.util.Map;
import java.util.stream.Collectors;
public class ModelSchemaBuilder { public class ModelSchemaBuilder {
private static String aliasSplit = ",";
public static ModelSchema build(ModelSchemaResp resp) { public static ModelSchema build(ModelSchemaResp resp) {
ModelSchema modelSchema = new ModelSchema(); ModelSchema modelSchema = new ModelSchema();
SchemaElement domain = SchemaElement.builder() SchemaElement domain = SchemaElement.builder()
@@ -39,14 +35,14 @@ public class ModelSchemaBuilder {
.name(resp.getName()) .name(resp.getName())
.bizName(resp.getBizName()) .bizName(resp.getBizName())
.type(SchemaElementType.MODEL) .type(SchemaElementType.MODEL)
.alias(getAliasList(resp.getAlias())) .alias(SchemaItem.getAliasList(resp.getAlias()))
.build(); .build();
modelSchema.setModel(domain); modelSchema.setModel(domain);
Set<SchemaElement> metrics = new HashSet<>(); Set<SchemaElement> metrics = new HashSet<>();
for (MetricSchemaResp metric : resp.getMetrics()) { for (MetricSchemaResp metric : resp.getMetrics()) {
List<String> alias = getAliasList(metric.getAlias()); List<String> alias = SchemaItem.getAliasList(metric.getAlias());
SchemaElement metricToAdd = SchemaElement.builder() SchemaElement metricToAdd = SchemaElement.builder()
.model(resp.getId()) .model(resp.getId())
@@ -68,7 +64,7 @@ public class ModelSchemaBuilder {
Set<SchemaElement> dimensionValues = new HashSet<>(); Set<SchemaElement> dimensionValues = new HashSet<>();
for (DimSchemaResp dim : resp.getDimensions()) { for (DimSchemaResp dim : resp.getDimensions()) {
List<String> alias = getAliasList(dim.getAlias()); List<String> alias = SchemaItem.getAliasList(dim.getAlias());
Set<String> dimValueAlias = new HashSet<>(); Set<String> dimValueAlias = new HashSet<>();
List<DimValueMap> dimValueMaps = dim.getDimValueMaps(); List<DimValueMap> dimValueMaps = dim.getDimValueMaps();
List<SchemaValueMap> schemaValueMaps = new ArrayList<>(); List<SchemaValueMap> schemaValueMaps = new ArrayList<>();
@@ -133,13 +129,6 @@ public class ModelSchemaBuilder {
return modelSchema; return modelSchema;
} }
private static List<String> getAliasList(String alias) {
if (StringUtils.isEmpty(alias)) {
return new ArrayList<>();
}
return Arrays.asList(alias.split(aliasSplit));
}
private static List<RelateSchemaElement> getRelateSchemaElement(MetricSchemaResp metricSchemaResp) { private static List<RelateSchemaElement> getRelateSchemaElement(MetricSchemaResp metricSchemaResp) {
RelateDimension relateDimension = metricSchemaResp.getRelateDimension(); RelateDimension relateDimension = metricSchemaResp.getRelateDimension();
if (relateDimension == null || CollectionUtils.isEmpty(relateDimension.getDrillDownDimensions())) { if (relateDimension == null || CollectionUtils.isEmpty(relateDimension.getDrillDownDimensions())) {

View File

@@ -5,13 +5,18 @@ import com.tencent.supersonic.common.pojo.RecordInfo;
import com.tencent.supersonic.common.pojo.enums.SensitiveLevelEnum; import com.tencent.supersonic.common.pojo.enums.SensitiveLevelEnum;
import com.tencent.supersonic.common.pojo.enums.StatusEnum; import com.tencent.supersonic.common.pojo.enums.StatusEnum;
import com.tencent.supersonic.common.pojo.enums.TypeEnums; import com.tencent.supersonic.common.pojo.enums.TypeEnums;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import lombok.Data; import lombok.Data;
import lombok.ToString; import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
@Data @Data
@ToString(callSuper = true) @ToString(callSuper = true)
public class SchemaItem extends RecordInfo { public class SchemaItem extends RecordInfo {
private static String aliasSplit = ",";
private Long id; private Long id;
private String name; private String name;
@@ -49,4 +54,11 @@ public class SchemaItem extends RecordInfo {
public int hashCode() { public int hashCode() {
return Objects.hashCode(super.hashCode(), id, name, bizName, description, status, typeEnum, sensitiveLevel); return Objects.hashCode(super.hashCode(), id, name, bizName, description, status, typeEnum, sensitiveLevel);
} }
public static List<String> getAliasList(String alias) {
if (StringUtils.isEmpty(alias)) {
return new ArrayList<>();
}
return Arrays.asList(alias.split(aliasSplit));
}
} }

View File

@@ -27,13 +27,16 @@ import com.tencent.supersonic.semantic.query.service.SemanticQueryEngine;
import com.tencent.supersonic.semantic.query.utils.QueryStructUtils; import com.tencent.supersonic.semantic.query.utils.QueryStructUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -172,14 +175,30 @@ public class QueryReqConverter {
protected Map<String, String> getFieldNameToBizNameMap(ModelSchemaResp modelSchemaResp) { protected Map<String, String> getFieldNameToBizNameMap(ModelSchemaResp modelSchemaResp) {
List<SchemaItem> allSchemaItems = new ArrayList<>(); // support fieldName and field alias to bizName
allSchemaItems.addAll(modelSchemaResp.getDimensions()); Map<String, String> dimensionResults = modelSchemaResp.getDimensions().stream()
allSchemaItems.addAll(modelSchemaResp.getMetrics()); .flatMap(entry -> getPairStream(entry.getAlias(), entry.getName(), entry.getBizName()))
.collect(Collectors.toMap(a -> a.getLeft(), a -> a.getRight(), (k1, k2) -> k1));
Map<String, String> result = allSchemaItems.stream() Map<String, String> metricResults = modelSchemaResp.getMetrics().stream()
.collect(Collectors.toMap(SchemaItem::getName, a -> a.getBizName(), (k1, k2) -> k1)); .flatMap(entry -> getPairStream(entry.getAlias(), entry.getName(), entry.getBizName()))
result.put(DateUtils.DATE_FIELD, TimeDimensionEnum.DAY.getName()); .collect(Collectors.toMap(a -> a.getLeft(), a -> a.getRight(), (k1, k2) -> k1));
return result;
dimensionResults.put(DateUtils.DATE_FIELD, TimeDimensionEnum.DAY.getName());
dimensionResults.putAll(metricResults);
return dimensionResults;
}
private Stream<Pair<String, String>> getPairStream(String aliasStr, String name, String bizName) {
Set<Pair<String, String>> elements = new HashSet<>();
elements.add(Pair.of(name, bizName));
if (StringUtils.isNotBlank(aliasStr)) {
List<String> aliasList = SchemaItem.getAliasList(aliasStr);
for (String alias : aliasList) {
elements.add(Pair.of(alias, bizName));
}
}
return elements.stream();
} }
public void correctTableName(QueryS2QLReq databaseReq) { public void correctTableName(QueryS2QLReq databaseReq) {