mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-10 19:51:00 +00:00
(improvement)(chat) SQL field replacement supports IN. (#1460)
This commit is contained in:
@@ -1,10 +1,5 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
import com.tencent.supersonic.common.util.JsonUtil;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.expression.DoubleValue;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
@@ -24,6 +19,11 @@ import net.sf.jsqlparser.schema.Column;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@Slf4j
|
||||
public class FieldlValueReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
|
||||
@@ -71,17 +71,13 @@ public class FieldlValueReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
values.add(((StringValue) o).getValue());
|
||||
}
|
||||
});
|
||||
if (valueMap == null) {
|
||||
if (valueMap == null || CollectionUtils.isEmpty(values)) {
|
||||
return;
|
||||
}
|
||||
String value = valueMap.get(JsonUtil.toString(values));
|
||||
if (StringUtils.isBlank(value)) {
|
||||
return;
|
||||
}
|
||||
List<String> valueList = JsonUtil.toList(value, String.class);
|
||||
List<Expression> newExpressions = new ArrayList<>();
|
||||
valueList.stream().forEach(o -> {
|
||||
StringValue stringValue = new StringValue(o);
|
||||
values.stream().forEach(o -> {
|
||||
String replaceValue = valueMap.getOrDefault(o, o);
|
||||
StringValue stringValue = new StringValue(replaceValue);
|
||||
newExpressions.add(stringValue);
|
||||
});
|
||||
rightItemsList.setExpressions(newExpressions);
|
||||
|
||||
@@ -94,31 +94,6 @@ public class Parameter {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean isVisible(Map<String, String> otherParameterValues) {
|
||||
if (dependencies == null) {
|
||||
return true;
|
||||
}
|
||||
for (Dependency dependency : dependencies) {
|
||||
String dependentValue = otherParameterValues.get(dependency.getName());
|
||||
if (dependentValue == null || !dependency.getShow().getIncludesValue().contains(dependentValue)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void applyDefaultValue(Map<String, String> otherParameterValues) {
|
||||
if (dependencies == null) {
|
||||
return;
|
||||
}
|
||||
for (Dependency dependency : dependencies) {
|
||||
String dependentValue = otherParameterValues.get(dependency.getName());
|
||||
if (dependentValue != null && dependency.getSetDefaultValue().containsKey(dependentValue)) {
|
||||
this.defaultValue = dependency.getSetDefaultValue().get(dependentValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Dependency {
|
||||
private String name;
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
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;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* SqlParserReplaceHelperTest
|
||||
@@ -111,6 +112,20 @@ class SqlReplaceHelperTest {
|
||||
+ "歌手名 = '周杰伦' AND 歌手名 = '林俊杰' AND 歌手名 = '陈奕迅' AND 歌曲发布时 = '2023-08-01' "
|
||||
+ "AND 播放量 < (SELECT min(播放量) FROM 歌曲库 WHERE 语种 = '英文')) AND 数据日期 = '2023-08-09' "
|
||||
+ "ORDER BY 播放量 DESC LIMIT 11", replaceSql);
|
||||
|
||||
|
||||
Map<String, Map<String, String>> filedNameToValueMap3 = new HashMap<>();
|
||||
|
||||
Map<String, String> valueMap3 = new HashMap<>();
|
||||
valueMap3.put("周杰伦", "1");
|
||||
valueMap3.put("林俊杰", "2");
|
||||
valueMap3.put("陈奕迅", "3");
|
||||
filedNameToValueMap3.put("歌手名", valueMap3);
|
||||
replaceSql = "SELECT 歌曲名 FROM 歌曲库 WHERE 歌手名 in ('周杰伦','林俊杰','陈奕迅') ";
|
||||
replaceSql = SqlReplaceHelper.replaceValue(replaceSql, filedNameToValueMap3, true);
|
||||
|
||||
Assert.assertEquals(
|
||||
"SELECT 歌曲名 FROM 歌曲库 WHERE 歌手名 IN ('1', '2', '3')", replaceSql);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user