mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-13 13:07:32 +00:00
(improvement)(build) Add spotless during the build process. (#1639)
This commit is contained in:
@@ -24,12 +24,13 @@ public class LoadRemoveService {
|
||||
}
|
||||
List<String> resultList = new ArrayList<>(value);
|
||||
if (StringUtils.isNotBlank(mapperRemoveNaturePrefix)) {
|
||||
resultList.removeIf(nature -> {
|
||||
if (Objects.isNull(nature)) {
|
||||
return false;
|
||||
}
|
||||
return nature.startsWith(mapperRemoveNaturePrefix);
|
||||
});
|
||||
resultList.removeIf(
|
||||
nature -> {
|
||||
if (Objects.isNull(nature)) {
|
||||
return false;
|
||||
}
|
||||
return nature.startsWith(mapperRemoveNaturePrefix);
|
||||
});
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
@@ -46,5 +47,4 @@ public class LoadRemoveService {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,26 +20,16 @@ import java.util.Set;
|
||||
@Slf4j
|
||||
public abstract class BaseNode<V> implements Comparable<BaseNode> {
|
||||
|
||||
/**
|
||||
* 状态数组,方便读取的时候用
|
||||
*/
|
||||
/** 状态数组,方便读取的时候用 */
|
||||
static final Status[] ARRAY_STATUS = Status.values();
|
||||
|
||||
/**
|
||||
* 子节点
|
||||
*/
|
||||
/** 子节点 */
|
||||
protected BaseNode[] child;
|
||||
/**
|
||||
* 节点状态
|
||||
*/
|
||||
/** 节点状态 */
|
||||
protected Status status;
|
||||
/**
|
||||
* 节点代表的字符
|
||||
*/
|
||||
/** 节点代表的字符 */
|
||||
protected char c;
|
||||
/**
|
||||
* 节点代表的值
|
||||
*/
|
||||
/** 节点代表的值 */
|
||||
protected V value;
|
||||
|
||||
protected String prefix = null;
|
||||
@@ -238,25 +228,18 @@ public abstract class BaseNode<V> implements Comparable<BaseNode> {
|
||||
}
|
||||
|
||||
public enum Status {
|
||||
/**
|
||||
* 未指定,用于删除词条
|
||||
*/
|
||||
/** 未指定,用于删除词条 */
|
||||
UNDEFINED_0,
|
||||
/**
|
||||
* 不是词语的结尾
|
||||
*/
|
||||
/** 不是词语的结尾 */
|
||||
NOT_WORD_1,
|
||||
/**
|
||||
* 是个词语的结尾,并且还可以继续
|
||||
*/
|
||||
/** 是个词语的结尾,并且还可以继续 */
|
||||
WORD_MIDDLE_2,
|
||||
/**
|
||||
* 是个词语的结尾,并且没有继续
|
||||
*/
|
||||
/** 是个词语的结尾,并且没有继续 */
|
||||
WORD_END_3,
|
||||
}
|
||||
|
||||
public class TrieEntry extends AbstractMap.SimpleEntry<String, V> implements Comparable<TrieEntry> {
|
||||
public class TrieEntry extends AbstractMap.SimpleEntry<String, V>
|
||||
implements Comparable<TrieEntry> {
|
||||
|
||||
public TrieEntry(String key, V value) {
|
||||
super(key, value);
|
||||
@@ -295,8 +278,9 @@ public abstract class BaseNode<V> implements Comparable<BaseNode> {
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* walk limit
|
||||
/**
|
||||
* * walk limit
|
||||
*
|
||||
* @param sb
|
||||
* @param entrySet
|
||||
*/
|
||||
@@ -322,5 +306,4 @@ public abstract class BaseNode<V> implements Comparable<BaseNode> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.hankcs.hanlp.dictionary;
|
||||
|
||||
|
||||
import com.hankcs.hanlp.HanLP;
|
||||
import com.hankcs.hanlp.collection.trie.DoubleArrayTrie;
|
||||
import com.hankcs.hanlp.corpus.io.ByteArray;
|
||||
@@ -8,6 +7,7 @@ import com.hankcs.hanlp.corpus.io.IOUtil;
|
||||
import com.hankcs.hanlp.corpus.tag.Nature;
|
||||
import com.hankcs.hanlp.utility.Predefine;
|
||||
import com.hankcs.hanlp.utility.TextUtility;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
@@ -21,9 +21,7 @@ import java.util.List;
|
||||
import java.util.TreeMap;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 使用DoubleArrayTrie实现的核心词典
|
||||
*/
|
||||
/** 使用DoubleArrayTrie实现的核心词典 */
|
||||
public class CoreDictionary {
|
||||
|
||||
public static DoubleArrayTrie<Attribute> trie = new DoubleArrayTrie<Attribute>();
|
||||
@@ -36,8 +34,13 @@ public class CoreDictionary {
|
||||
if (!load(PATH)) {
|
||||
throw new IllegalArgumentException("核心词典" + PATH + "加载失败");
|
||||
} else {
|
||||
Predefine.logger.info(PATH + "加载成功," + trie.size() + "个词条,耗时"
|
||||
+ (System.currentTimeMillis() - start) + "ms");
|
||||
Predefine.logger.info(
|
||||
PATH
|
||||
+ "加载成功,"
|
||||
+ trie.size()
|
||||
+ "个词条,耗时"
|
||||
+ (System.currentTimeMillis() - start)
|
||||
+ "ms");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,15 +78,21 @@ public class CoreDictionary {
|
||||
totalFrequency += attribute.totalFrequency;
|
||||
}
|
||||
Predefine.logger.info(
|
||||
"核心词典读入词条" + map.size() + " 全部频次" + totalFrequency + ",耗时" + (
|
||||
System.currentTimeMillis() - start)
|
||||
"核心词典读入词条"
|
||||
+ map.size()
|
||||
+ " 全部频次"
|
||||
+ totalFrequency
|
||||
+ ",耗时"
|
||||
+ (System.currentTimeMillis() - start)
|
||||
+ "ms");
|
||||
br.close();
|
||||
trie.build(map);
|
||||
Predefine.logger.info("核心词典加载成功:" + trie.size() + "个词条,下面将写入缓存……");
|
||||
try {
|
||||
DataOutputStream out = new DataOutputStream(
|
||||
new BufferedOutputStream(IOUtil.newOutputStream(path + Predefine.BIN_EXT)));
|
||||
DataOutputStream out =
|
||||
new DataOutputStream(
|
||||
new BufferedOutputStream(
|
||||
IOUtil.newOutputStream(path + Predefine.BIN_EXT)));
|
||||
Collection<Attribute> attributeList = map.values();
|
||||
out.writeInt(attributeList.size());
|
||||
for (Attribute attribute : attributeList) {
|
||||
@@ -202,25 +211,18 @@ public class CoreDictionary {
|
||||
return trie.get(key) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 核心词典中的词属性
|
||||
*/
|
||||
/** 核心词典中的词属性 */
|
||||
public static class Attribute implements Serializable {
|
||||
|
||||
/**
|
||||
* 词性列表
|
||||
*/
|
||||
/** 词性列表 */
|
||||
public Nature[] nature;
|
||||
/**
|
||||
* 词性对应的词频
|
||||
*/
|
||||
/** 词性对应的词频 */
|
||||
public int[] frequency;
|
||||
|
||||
public int totalFrequency;
|
||||
public String[] originals;
|
||||
public String original = null;
|
||||
|
||||
|
||||
public Attribute(int size) {
|
||||
nature = new Nature[size];
|
||||
frequency = new int[size];
|
||||
@@ -276,8 +278,11 @@ public class CoreDictionary {
|
||||
}
|
||||
return attribute;
|
||||
} catch (Exception e) {
|
||||
Predefine.logger.warning("使用字符串" + natureWithFrequency + "创建词条属性失败!"
|
||||
+ TextUtility.exceptionToString(e));
|
||||
Predefine.logger.warning(
|
||||
"使用字符串"
|
||||
+ natureWithFrequency
|
||||
+ "创建词条属性失败!"
|
||||
+ TextUtility.exceptionToString(e));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -404,7 +409,10 @@ public class CoreDictionary {
|
||||
if (originals == null || originals.length == 0) {
|
||||
return null;
|
||||
}
|
||||
return Arrays.stream(originals).filter(o -> o != null).distinct().collect(Collectors.toList());
|
||||
return Arrays.stream(originals)
|
||||
.filter(o -> o != null)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -431,4 +439,3 @@ public class CoreDictionary {
|
||||
return load(path);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
package com.hankcs.hanlp.seg;
|
||||
|
||||
|
||||
import com.hankcs.hanlp.algorithm.Viterbi;
|
||||
import com.hankcs.hanlp.collection.AhoCorasick.AhoCorasickDoubleArrayTrie;
|
||||
import com.hankcs.hanlp.collection.trie.DoubleArrayTrie;
|
||||
import com.hankcs.hanlp.corpus.tag.Nature;
|
||||
import com.hankcs.hanlp.dictionary.CoreDictionary;
|
||||
import com.hankcs.hanlp.seg.common.Term;
|
||||
import com.hankcs.hanlp.dictionary.CoreDictionaryTransformMatrixDictionary;
|
||||
import com.hankcs.hanlp.dictionary.other.CharType;
|
||||
import com.hankcs.hanlp.seg.NShort.Path.AtomNode;
|
||||
import com.hankcs.hanlp.seg.common.Graph;
|
||||
import com.hankcs.hanlp.seg.common.Term;
|
||||
import com.hankcs.hanlp.seg.common.Vertex;
|
||||
import com.hankcs.hanlp.seg.common.WordNet;
|
||||
import com.hankcs.hanlp.utility.TextUtility;
|
||||
@@ -21,11 +20,9 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
|
||||
public abstract class WordBasedSegment extends Segment {
|
||||
|
||||
public WordBasedSegment() {
|
||||
}
|
||||
public WordBasedSegment() {}
|
||||
|
||||
protected static void generateWord(List<Vertex> linkedArray, WordNet wordNetOptimum) {
|
||||
fixResultByRule(linkedArray);
|
||||
@@ -50,7 +47,9 @@ public abstract class WordBasedSegment extends Segment {
|
||||
}
|
||||
|
||||
vertex = (Vertex) var1.next();
|
||||
} while (!vertex.realWord.equals("--") && !vertex.realWord.equals("—") && !vertex.realWord.equals("-"));
|
||||
} while (!vertex.realWord.equals("--")
|
||||
&& !vertex.realWord.equals("—")
|
||||
&& !vertex.realWord.equals("-"));
|
||||
|
||||
vertex.confirmNature(Nature.w);
|
||||
}
|
||||
@@ -64,9 +63,12 @@ public abstract class WordBasedSegment extends Segment {
|
||||
for (Vertex current = next; listIterator.hasNext(); current = next) {
|
||||
next = (Vertex) listIterator.next();
|
||||
Nature currentNature = current.getNature();
|
||||
if (currentNature == Nature.nx && (next.hasNature(Nature.q) || next.hasNature(Nature.n))) {
|
||||
if (currentNature == Nature.nx
|
||||
&& (next.hasNature(Nature.q) || next.hasNature(Nature.n))) {
|
||||
String[] param = current.realWord.split("-", 1);
|
||||
if (param.length == 2 && TextUtility.isAllNum(param[0]) && TextUtility.isAllNum(param[1])) {
|
||||
if (param.length == 2
|
||||
&& TextUtility.isAllNum(param[0])
|
||||
&& TextUtility.isAllNum(param[1])) {
|
||||
current = current.copy();
|
||||
current.realWord = param[0];
|
||||
current.confirmNature(Nature.m);
|
||||
@@ -79,7 +81,6 @@ public abstract class WordBasedSegment extends Segment {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +91,8 @@ public abstract class WordBasedSegment extends Segment {
|
||||
|
||||
for (Vertex current = next; listIterator.hasNext(); current = next) {
|
||||
next = (Vertex) listIterator.next();
|
||||
if (TextUtility.isAllNum(current.realWord) || TextUtility.isAllChineseNum(current.realWord)) {
|
||||
if (TextUtility.isAllNum(current.realWord)
|
||||
|| TextUtility.isAllChineseNum(current.realWord)) {
|
||||
String nextWord = next.realWord;
|
||||
if (nextWord.length() == 1 && "月日时分秒".contains(nextWord)
|
||||
|| nextWord.length() == 2 && nextWord.equals("月份")) {
|
||||
@@ -110,8 +112,10 @@ public abstract class WordBasedSegment extends Segment {
|
||||
current.confirmNature(Nature.m, true);
|
||||
} else if (current.realWord.length() > 1) {
|
||||
char last = current.realWord.charAt(current.realWord.length() - 1);
|
||||
current = Vertex.newNumberInstance(
|
||||
current.realWord.substring(0, current.realWord.length() - 1));
|
||||
current =
|
||||
Vertex.newNumberInstance(
|
||||
current.realWord.substring(
|
||||
0, current.realWord.length() - 1));
|
||||
listIterator.previous();
|
||||
listIterator.previous();
|
||||
listIterator.set(current);
|
||||
@@ -121,7 +125,6 @@ public abstract class WordBasedSegment extends Segment {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,9 +146,7 @@ public abstract class WordBasedSegment extends Segment {
|
||||
return wordNet.toGraph();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
/** @deprecated */
|
||||
private static List<AtomNode> atomSegment(String sSentence, int start, int end) {
|
||||
if (end < start) {
|
||||
throw new RuntimeException("start=" + start + " < end=" + end);
|
||||
@@ -161,7 +162,10 @@ public abstract class WordBasedSegment extends Segment {
|
||||
charTypeArray[i] = CharType.get(c);
|
||||
if (c == '.' && i < charArray.length - 1 && CharType.get(charArray[i + 1]) == 9) {
|
||||
charTypeArray[i] = 9;
|
||||
} else if (c == '.' && i < charArray.length - 1 && charArray[i + 1] >= '0' && charArray[i + 1] <= '9') {
|
||||
} else if (c == '.'
|
||||
&& i < charArray.length - 1
|
||||
&& charArray[i + 1] >= '0'
|
||||
&& charArray[i + 1] <= '9') {
|
||||
charTypeArray[i] = 5;
|
||||
} else if (charTypeArray[i] == 8) {
|
||||
charTypeArray[i] = 5;
|
||||
@@ -222,8 +226,10 @@ public abstract class WordBasedSegment extends Segment {
|
||||
while (true) {
|
||||
while (listIterator.hasNext()) {
|
||||
next = (Vertex) listIterator.next();
|
||||
if (!TextUtility.isAllNum(current.realWord) && !TextUtility.isAllChineseNum(current.realWord)
|
||||
|| !TextUtility.isAllNum(next.realWord) && !TextUtility.isAllChineseNum(next.realWord)) {
|
||||
if (!TextUtility.isAllNum(current.realWord)
|
||||
&& !TextUtility.isAllChineseNum(current.realWord)
|
||||
|| !TextUtility.isAllNum(next.realWord)
|
||||
&& !TextUtility.isAllChineseNum(next.realWord)) {
|
||||
current = next;
|
||||
} else {
|
||||
current = Vertex.newNumberInstance(current.realWord + next.realWord);
|
||||
@@ -246,16 +252,24 @@ public abstract class WordBasedSegment extends Segment {
|
||||
DoubleArrayTrie.Searcher searcher = CoreDictionary.trie.getSearcher(charArray, 0);
|
||||
|
||||
while (searcher.next()) {
|
||||
wordNetStorage.add(searcher.begin + 1, new Vertex(new String(charArray, searcher.begin, searcher.length),
|
||||
(CoreDictionary.Attribute) searcher.value, searcher.index));
|
||||
wordNetStorage.add(
|
||||
searcher.begin + 1,
|
||||
new Vertex(
|
||||
new String(charArray, searcher.begin, searcher.length),
|
||||
(CoreDictionary.Attribute) searcher.value,
|
||||
searcher.index));
|
||||
}
|
||||
|
||||
if (this.config.forceCustomDictionary) {
|
||||
this.customDictionary.parseText(charArray, new AhoCorasickDoubleArrayTrie.IHit<CoreDictionary.Attribute>() {
|
||||
public void hit(int begin, int end, CoreDictionary.Attribute value) {
|
||||
wordNetStorage.add(begin + 1, new Vertex(new String(charArray, begin, end - begin), value));
|
||||
}
|
||||
});
|
||||
this.customDictionary.parseText(
|
||||
charArray,
|
||||
new AhoCorasickDoubleArrayTrie.IHit<CoreDictionary.Attribute>() {
|
||||
public void hit(int begin, int end, CoreDictionary.Attribute value) {
|
||||
wordNetStorage.add(
|
||||
begin + 1,
|
||||
new Vertex(new String(charArray, begin, end - begin), value));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
LinkedList<Vertex>[] vertexes = wordNetStorage.getVertexes();
|
||||
@@ -266,9 +280,10 @@ public abstract class WordBasedSegment extends Segment {
|
||||
if (vertexes[i].isEmpty()) {
|
||||
int j;
|
||||
for (j = i + 1;
|
||||
j < vertexes.length - 1 && (vertexes[j].isEmpty() || CharType.get(charArray[j - 1]) == 11);
|
||||
++j) {
|
||||
}
|
||||
j < vertexes.length - 1
|
||||
&& (vertexes[j].isEmpty()
|
||||
|| CharType.get(charArray[j - 1]) == 11);
|
||||
++j) {}
|
||||
|
||||
wordNetStorage.add(i, Segment.quickAtomSegment(charArray, i - 1, j - 1));
|
||||
i = j;
|
||||
@@ -291,12 +306,14 @@ public abstract class WordBasedSegment extends Segment {
|
||||
for (int i = 0; i < length; ++i) {
|
||||
Vertex vertex = (Vertex) listIterator.next();
|
||||
Term termMain = Segment.convert(vertex);
|
||||
//termList.add(termMain);
|
||||
// termList.add(termMain);
|
||||
addTerms(termList, vertex, line - 1);
|
||||
termMain.offset = line - 1;
|
||||
if (vertex.realWord.length() > 2) {
|
||||
label43:
|
||||
for (int currentLine = line; currentLine < line + vertex.realWord.length(); ++currentLine) {
|
||||
for (int currentLine = line;
|
||||
currentLine < line + vertex.realWord.length();
|
||||
++currentLine) {
|
||||
Iterator iterator = wordNetAll.descendingIterator(currentLine);
|
||||
|
||||
while (true) {
|
||||
@@ -310,11 +327,12 @@ public abstract class WordBasedSegment extends Segment {
|
||||
&& smallVertex.realWord.length() < this.config.indexMode);
|
||||
|
||||
if (smallVertex != vertex
|
||||
&& currentLine + smallVertex.realWord.length() <= line + vertex.realWord.length()) {
|
||||
&& currentLine + smallVertex.realWord.length()
|
||||
<= line + vertex.realWord.length()) {
|
||||
listIterator.add(smallVertex);
|
||||
//Term termSub = convert(smallVertex);
|
||||
//termSub.offset = currentLine - 1;
|
||||
//termList.add(termSub);
|
||||
// Term termSub = convert(smallVertex);
|
||||
// termSub.offset = currentLine - 1;
|
||||
// termList.add(termSub);
|
||||
addTerms(termList, smallVertex, currentLine - 1);
|
||||
}
|
||||
}
|
||||
@@ -328,7 +346,8 @@ public abstract class WordBasedSegment extends Segment {
|
||||
}
|
||||
|
||||
protected static void speechTagging(List<Vertex> vertexList) {
|
||||
Viterbi.compute(vertexList, CoreDictionaryTransformMatrixDictionary.transformMatrixDictionary);
|
||||
Viterbi.compute(
|
||||
vertexList, CoreDictionaryTransformMatrixDictionary.transformMatrixDictionary);
|
||||
}
|
||||
|
||||
protected void addTerms(List<Term> terms, Vertex vertex, int offset) {
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
package com.hankcs.hanlp.seg.common;
|
||||
|
||||
import com.hankcs.hanlp.corpus.tag.Nature;
|
||||
//import com.hankcs.hanlp.dictionary.CoreDictionary;
|
||||
//import com.hankcs.hanlp.dictionary.CustomDictionary;
|
||||
//import com.hankcs.hanlp.dictionary.DynamicCustomDictionary;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
//import static com.hankcs.hanlp.HanLP.Config.CustomDictionaryPath;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
public class Term {
|
||||
@@ -72,5 +67,4 @@ public class Term {
|
||||
}
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,4 +14,4 @@ public class S2MysqlSqlDialect extends MysqlSqlDialect {
|
||||
buf.append(val.replace(this.literalEndQuoteString, this.literalEscapedQuote));
|
||||
buf.append(this.literalEndQuoteString);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,5 @@
|
||||
package com.tencent.supersonic.common.calcite;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
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.calcite.sql.SqlBasicCall;
|
||||
import org.apache.calcite.sql.SqlDialect;
|
||||
import org.apache.calcite.sql.SqlIdentifier;
|
||||
@@ -25,9 +17,15 @@ import org.apache.calcite.sql.util.SqlString;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* sql parse utils
|
||||
*/
|
||||
import java.util.HashMap;
|
||||
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;
|
||||
|
||||
/** sql parse utils */
|
||||
public class SqlParseUtils {
|
||||
|
||||
/**
|
||||
@@ -44,10 +42,13 @@ public class SqlParseUtils {
|
||||
|
||||
handlerSQL(sqlNode, sqlParserInfo);
|
||||
|
||||
sqlParserInfo.setAllFields(sqlParserInfo.getAllFields().stream().distinct().collect(Collectors.toList()));
|
||||
sqlParserInfo.setAllFields(
|
||||
sqlParserInfo.getAllFields().stream().distinct().collect(Collectors.toList()));
|
||||
|
||||
sqlParserInfo.setSelectFields(
|
||||
sqlParserInfo.getSelectFields().stream().distinct().collect(Collectors.toList()));
|
||||
sqlParserInfo.getSelectFields().stream()
|
||||
.distinct()
|
||||
.collect(Collectors.toList()));
|
||||
|
||||
return sqlParserInfo;
|
||||
} catch (SqlParseException e) {
|
||||
@@ -107,16 +108,18 @@ public class SqlParseUtils {
|
||||
SqlSelect sqlSelect = (SqlSelect) select;
|
||||
SqlNodeList selectList = sqlSelect.getSelectList();
|
||||
|
||||
selectList.getList().forEach(list -> {
|
||||
Set<String> selectFields = handlerField(list);
|
||||
sqlParserInfo.getSelectFields().addAll(selectFields);
|
||||
});
|
||||
selectList
|
||||
.getList()
|
||||
.forEach(
|
||||
list -> {
|
||||
Set<String> selectFields = handlerField(list);
|
||||
sqlParserInfo.getSelectFields().addAll(selectFields);
|
||||
});
|
||||
String tableName = handlerFrom(sqlSelect.getFrom());
|
||||
sqlParserInfo.setTableName(tableName);
|
||||
|
||||
Set<String> selectFields = handlerSelectField(sqlSelect);
|
||||
allFields.addAll(selectFields);
|
||||
|
||||
}
|
||||
|
||||
private static Set<String> handlerSelectField(SqlSelect sqlSelect) {
|
||||
@@ -126,10 +129,14 @@ public class SqlParseUtils {
|
||||
results.addAll(formFields);
|
||||
}
|
||||
|
||||
sqlSelect.getSelectList().getList().forEach(list -> {
|
||||
Set<String> selectFields = handlerField(list);
|
||||
results.addAll(selectFields);
|
||||
});
|
||||
sqlSelect
|
||||
.getSelectList()
|
||||
.getList()
|
||||
.forEach(
|
||||
list -> {
|
||||
Set<String> selectFields = handlerField(list);
|
||||
results.addAll(selectFields);
|
||||
});
|
||||
|
||||
if (sqlSelect.hasWhere()) {
|
||||
Set<String> whereFields = handlerField(sqlSelect.getWhere());
|
||||
@@ -141,11 +148,11 @@ public class SqlParseUtils {
|
||||
}
|
||||
SqlNodeList group = sqlSelect.getGroup();
|
||||
if (group != null) {
|
||||
group.forEach(groupField -> {
|
||||
Set<String> groupByFields = handlerField(groupField);
|
||||
results.addAll(groupByFields);
|
||||
|
||||
});
|
||||
group.forEach(
|
||||
groupField -> {
|
||||
Set<String> groupByFields = handlerField(groupField);
|
||||
results.addAll(groupByFields);
|
||||
});
|
||||
}
|
||||
return results;
|
||||
}
|
||||
@@ -206,9 +213,12 @@ public class SqlParseUtils {
|
||||
}
|
||||
}
|
||||
if (field instanceof SqlNodeList) {
|
||||
((SqlNodeList) field).getList().forEach(node -> {
|
||||
fields.addAll(handlerField(node));
|
||||
});
|
||||
((SqlNodeList) field)
|
||||
.getList()
|
||||
.forEach(
|
||||
node -> {
|
||||
fields.addAll(handlerField(node));
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -232,10 +242,14 @@ public class SqlParseUtils {
|
||||
if (CollectionUtils.isNotEmpty(operandList) && operandList.size() == 1) {
|
||||
SqlIdentifier sqlIdentifier = (SqlIdentifier) operandList.get(0);
|
||||
String simple = sqlIdentifier.getSimple();
|
||||
SqlBasicCall aliasedNode = new SqlBasicCall(
|
||||
SqlStdOperatorTable.AS,
|
||||
new SqlNode[]{sqlBasicCall, new SqlIdentifier(simple.toLowerCase(), SqlParserPos.ZERO)},
|
||||
SqlParserPos.ZERO);
|
||||
SqlBasicCall aliasedNode =
|
||||
new SqlBasicCall(
|
||||
SqlStdOperatorTable.AS,
|
||||
new SqlNode[] {
|
||||
sqlBasicCall,
|
||||
new SqlIdentifier(simple.toLowerCase(), SqlParserPos.ZERO)
|
||||
},
|
||||
SqlParserPos.ZERO);
|
||||
selectList.set(selectList.indexOf(node), aliasedNode);
|
||||
}
|
||||
}
|
||||
@@ -245,7 +259,8 @@ public class SqlParseUtils {
|
||||
return newSql.getSql().replaceAll("`", "");
|
||||
}
|
||||
|
||||
public static String addFieldsToSql(String sql, List<String> addFields) throws SqlParseException {
|
||||
public static String addFieldsToSql(String sql, List<String> addFields)
|
||||
throws SqlParseException {
|
||||
if (CollectionUtils.isEmpty(addFields)) {
|
||||
return sql;
|
||||
}
|
||||
@@ -335,8 +350,8 @@ public class SqlParseUtils {
|
||||
SqlNode sqlNodeCase = parser.parseExpression();
|
||||
if (sqlNodeCase instanceof SqlCase) {
|
||||
SqlCase sqlCase = (SqlCase) sqlNodeCase;
|
||||
if (CollectionUtils.isEmpty(sqlCase.getThenOperands()) || CollectionUtils.isEmpty(
|
||||
sqlCase.getWhenOperands())) {
|
||||
if (CollectionUtils.isEmpty(sqlCase.getThenOperands())
|
||||
|| CollectionUtils.isEmpty(sqlCase.getWhenOperands())) {
|
||||
return ret;
|
||||
}
|
||||
SqlDialect dialect = new S2MysqlSqlDialect(S2MysqlSqlDialect.DEFAULT_CONTEXT);
|
||||
@@ -346,10 +361,12 @@ public class SqlParseUtils {
|
||||
SqlBasicCall when = (SqlBasicCall) sqlNode;
|
||||
if (!org.springframework.util.CollectionUtils.isEmpty(when.getOperandList())
|
||||
&& when.getOperandList().size() > 1) {
|
||||
String value = when.getOperandList().get(1).toSqlString(dialect).getSql();
|
||||
String value =
|
||||
when.getOperandList().get(1).toSqlString(dialect).getSql();
|
||||
if (sqlCase.getThenOperands().get(i) != null) {
|
||||
if (sqlCase.getThenOperands().get(i) instanceof SqlIdentifier) {
|
||||
SqlIdentifier sqlIdentifier = (SqlIdentifier) sqlCase.getThenOperands().get(i);
|
||||
SqlIdentifier sqlIdentifier =
|
||||
(SqlIdentifier) sqlCase.getThenOperands().get(i);
|
||||
String field = sqlIdentifier.getSimple();
|
||||
ret.put(value, field);
|
||||
}
|
||||
@@ -364,6 +381,4 @@ public class SqlParseUtils {
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package com.tencent.supersonic.common.calcite;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
@@ -15,4 +16,4 @@ public class SqlParserInfo implements Serializable {
|
||||
private List<String> selectFields = new ArrayList<>();
|
||||
|
||||
private List<String> allFields = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,55 +21,97 @@ import java.util.List;
|
||||
public class ChatModelParameterConfig extends ParameterConfig {
|
||||
|
||||
public static final Parameter CHAT_MODEL_PROVIDER =
|
||||
new Parameter("s2.chat.model.provider", OpenAiModelFactory.PROVIDER,
|
||||
"接口协议", "", "list",
|
||||
"对话模型配置", getCandidateValues());
|
||||
new Parameter(
|
||||
"s2.chat.model.provider",
|
||||
OpenAiModelFactory.PROVIDER,
|
||||
"接口协议",
|
||||
"",
|
||||
"list",
|
||||
"对话模型配置",
|
||||
getCandidateValues());
|
||||
|
||||
public static final Parameter CHAT_MODEL_BASE_URL =
|
||||
new Parameter("s2.chat.model.base.url", OpenAiModelFactory.DEFAULT_BASE_URL,
|
||||
"BaseUrl", "", "string",
|
||||
"对话模型配置", null, getBaseUrlDependency());
|
||||
new Parameter(
|
||||
"s2.chat.model.base.url",
|
||||
OpenAiModelFactory.DEFAULT_BASE_URL,
|
||||
"BaseUrl",
|
||||
"",
|
||||
"string",
|
||||
"对话模型配置",
|
||||
null,
|
||||
getBaseUrlDependency());
|
||||
public static final Parameter CHAT_MODEL_ENDPOINT =
|
||||
new Parameter("s2.chat.model.endpoint", "llama_2_70b",
|
||||
"Endpoint", "", "string",
|
||||
"对话模型配置", null, getEndpointDependency());
|
||||
new Parameter(
|
||||
"s2.chat.model.endpoint",
|
||||
"llama_2_70b",
|
||||
"Endpoint",
|
||||
"",
|
||||
"string",
|
||||
"对话模型配置",
|
||||
null,
|
||||
getEndpointDependency());
|
||||
public static final Parameter CHAT_MODEL_API_KEY =
|
||||
new Parameter("s2.chat.model.api.key", DEMO,
|
||||
"ApiKey", "", "password",
|
||||
"对话模型配置", null, getApiKeyDependency()
|
||||
);
|
||||
new Parameter(
|
||||
"s2.chat.model.api.key",
|
||||
DEMO,
|
||||
"ApiKey",
|
||||
"",
|
||||
"password",
|
||||
"对话模型配置",
|
||||
null,
|
||||
getApiKeyDependency());
|
||||
public static final Parameter CHAT_MODEL_SECRET_KEY =
|
||||
new Parameter("s2.chat.model.secretKey", "demo",
|
||||
"SecretKey", "", "password",
|
||||
"对话模型配置", null, getSecretKeyDependency());
|
||||
new Parameter(
|
||||
"s2.chat.model.secretKey",
|
||||
"demo",
|
||||
"SecretKey",
|
||||
"",
|
||||
"password",
|
||||
"对话模型配置",
|
||||
null,
|
||||
getSecretKeyDependency());
|
||||
|
||||
public static final Parameter CHAT_MODEL_NAME =
|
||||
new Parameter("s2.chat.model.name", "gpt-3.5-turbo",
|
||||
"ModelName", "", "string",
|
||||
"对话模型配置", null, getModelNameDependency());
|
||||
new Parameter(
|
||||
"s2.chat.model.name",
|
||||
"gpt-3.5-turbo",
|
||||
"ModelName",
|
||||
"",
|
||||
"string",
|
||||
"对话模型配置",
|
||||
null,
|
||||
getModelNameDependency());
|
||||
|
||||
public static final Parameter CHAT_MODEL_ENABLE_SEARCH =
|
||||
new Parameter("s2.chat.model.enableSearch", "false",
|
||||
"是否启用搜索增强功能,设为false表示不启用", "", "bool",
|
||||
"对话模型配置", null, getEnableSearchDependency());
|
||||
new Parameter(
|
||||
"s2.chat.model.enableSearch",
|
||||
"false",
|
||||
"是否启用搜索增强功能,设为false表示不启用",
|
||||
"",
|
||||
"bool",
|
||||
"对话模型配置",
|
||||
null,
|
||||
getEnableSearchDependency());
|
||||
|
||||
public static final Parameter CHAT_MODEL_TEMPERATURE =
|
||||
new Parameter("s2.chat.model.temperature", "0.0",
|
||||
"Temperature", "",
|
||||
"slider", "对话模型配置");
|
||||
new Parameter(
|
||||
"s2.chat.model.temperature", "0.0", "Temperature", "", "slider", "对话模型配置");
|
||||
|
||||
public static final Parameter CHAT_MODEL_TIMEOUT =
|
||||
new Parameter("s2.chat.model.timeout", "60",
|
||||
"超时时间(秒)", "",
|
||||
"number", "对话模型配置");
|
||||
new Parameter("s2.chat.model.timeout", "60", "超时时间(秒)", "", "number", "对话模型配置");
|
||||
|
||||
@Override
|
||||
public List<Parameter> getSysParameters() {
|
||||
return Lists.newArrayList(
|
||||
CHAT_MODEL_PROVIDER, CHAT_MODEL_BASE_URL, CHAT_MODEL_ENDPOINT,
|
||||
CHAT_MODEL_API_KEY, CHAT_MODEL_SECRET_KEY, CHAT_MODEL_NAME,
|
||||
CHAT_MODEL_ENABLE_SEARCH, CHAT_MODEL_TEMPERATURE, CHAT_MODEL_TIMEOUT
|
||||
);
|
||||
CHAT_MODEL_PROVIDER,
|
||||
CHAT_MODEL_BASE_URL,
|
||||
CHAT_MODEL_ENDPOINT,
|
||||
CHAT_MODEL_API_KEY,
|
||||
CHAT_MODEL_SECRET_KEY,
|
||||
CHAT_MODEL_NAME,
|
||||
CHAT_MODEL_ENABLE_SEARCH,
|
||||
CHAT_MODEL_TEMPERATURE,
|
||||
CHAT_MODEL_TIMEOUT);
|
||||
}
|
||||
|
||||
public ChatModelConfig convert() {
|
||||
@@ -108,7 +150,8 @@ public class ChatModelParameterConfig extends ParameterConfig {
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getBaseUrlDependency() {
|
||||
return getDependency(CHAT_MODEL_PROVIDER.getName(),
|
||||
return getDependency(
|
||||
CHAT_MODEL_PROVIDER.getName(),
|
||||
getCandidateValues(),
|
||||
ImmutableMap.of(
|
||||
OpenAiModelFactory.PROVIDER, OpenAiModelFactory.DEFAULT_BASE_URL,
|
||||
@@ -117,32 +160,31 @@ public class ChatModelParameterConfig extends ParameterConfig {
|
||||
QianfanModelFactory.PROVIDER, QianfanModelFactory.DEFAULT_BASE_URL,
|
||||
ZhipuModelFactory.PROVIDER, ZhipuModelFactory.DEFAULT_BASE_URL,
|
||||
LocalAiModelFactory.PROVIDER, LocalAiModelFactory.DEFAULT_BASE_URL,
|
||||
DashscopeModelFactory.PROVIDER, DashscopeModelFactory.DEFAULT_BASE_URL)
|
||||
);
|
||||
DashscopeModelFactory.PROVIDER, DashscopeModelFactory.DEFAULT_BASE_URL));
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getApiKeyDependency() {
|
||||
return getDependency(CHAT_MODEL_PROVIDER.getName(),
|
||||
return getDependency(
|
||||
CHAT_MODEL_PROVIDER.getName(),
|
||||
Lists.newArrayList(
|
||||
OpenAiModelFactory.PROVIDER,
|
||||
QianfanModelFactory.PROVIDER,
|
||||
ZhipuModelFactory.PROVIDER,
|
||||
LocalAiModelFactory.PROVIDER,
|
||||
AzureModelFactory.PROVIDER,
|
||||
DashscopeModelFactory.PROVIDER
|
||||
),
|
||||
DashscopeModelFactory.PROVIDER),
|
||||
ImmutableMap.of(
|
||||
OpenAiModelFactory.PROVIDER, DEMO,
|
||||
QianfanModelFactory.PROVIDER, DEMO,
|
||||
ZhipuModelFactory.PROVIDER, DEMO,
|
||||
LocalAiModelFactory.PROVIDER, DEMO,
|
||||
AzureModelFactory.PROVIDER, DEMO,
|
||||
DashscopeModelFactory.PROVIDER, DEMO
|
||||
));
|
||||
DashscopeModelFactory.PROVIDER, DEMO));
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getModelNameDependency() {
|
||||
return getDependency(CHAT_MODEL_PROVIDER.getName(),
|
||||
return getDependency(
|
||||
CHAT_MODEL_PROVIDER.getName(),
|
||||
getCandidateValues(),
|
||||
ImmutableMap.of(
|
||||
OpenAiModelFactory.PROVIDER, OpenAiModelFactory.DEFAULT_MODEL_NAME,
|
||||
@@ -151,29 +193,28 @@ public class ChatModelParameterConfig extends ParameterConfig {
|
||||
ZhipuModelFactory.PROVIDER, ZhipuModelFactory.DEFAULT_MODEL_NAME,
|
||||
LocalAiModelFactory.PROVIDER, LocalAiModelFactory.DEFAULT_MODEL_NAME,
|
||||
AzureModelFactory.PROVIDER, AzureModelFactory.DEFAULT_MODEL_NAME,
|
||||
DashscopeModelFactory.PROVIDER, DashscopeModelFactory.DEFAULT_MODEL_NAME
|
||||
)
|
||||
);
|
||||
DashscopeModelFactory.PROVIDER, DashscopeModelFactory.DEFAULT_MODEL_NAME));
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getEndpointDependency() {
|
||||
return getDependency(CHAT_MODEL_PROVIDER.getName(),
|
||||
return getDependency(
|
||||
CHAT_MODEL_PROVIDER.getName(),
|
||||
Lists.newArrayList(QianfanModelFactory.PROVIDER),
|
||||
ImmutableMap.of(QianfanModelFactory.PROVIDER, QianfanModelFactory.DEFAULT_ENDPOINT)
|
||||
);
|
||||
ImmutableMap.of(
|
||||
QianfanModelFactory.PROVIDER, QianfanModelFactory.DEFAULT_ENDPOINT));
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getEnableSearchDependency() {
|
||||
return getDependency(CHAT_MODEL_PROVIDER.getName(),
|
||||
return getDependency(
|
||||
CHAT_MODEL_PROVIDER.getName(),
|
||||
Lists.newArrayList(DashscopeModelFactory.PROVIDER),
|
||||
ImmutableMap.of(DashscopeModelFactory.PROVIDER, "false")
|
||||
);
|
||||
ImmutableMap.of(DashscopeModelFactory.PROVIDER, "false"));
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getSecretKeyDependency() {
|
||||
return getDependency(CHAT_MODEL_PROVIDER.getName(),
|
||||
return getDependency(
|
||||
CHAT_MODEL_PROVIDER.getName(),
|
||||
Lists.newArrayList(QianfanModelFactory.PROVIDER),
|
||||
ImmutableMap.of(QianfanModelFactory.PROVIDER, DEMO)
|
||||
);
|
||||
ImmutableMap.of(QianfanModelFactory.PROVIDER, DEMO));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
package com.tencent.supersonic.common.config;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
@Primary
|
||||
public class DataBaseConfig {
|
||||
@@ -21,5 +21,4 @@ public class DataBaseConfig {
|
||||
druidDataSource.setValidationQuery("select 1");
|
||||
return druidDataSource;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,5 +32,4 @@ public class EmbeddingConfig {
|
||||
public String getMemoryCollectionName(Integer agentId) {
|
||||
return memoryCollectionPrefix + agentId;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,46 +22,89 @@ import java.util.List;
|
||||
@Slf4j
|
||||
public class EmbeddingModelParameterConfig extends ParameterConfig {
|
||||
public static final Parameter EMBEDDING_MODEL_PROVIDER =
|
||||
new Parameter("s2.embedding.model.provider", InMemoryModelFactory.PROVIDER,
|
||||
"接口协议", "", "list",
|
||||
"向量模型配置", getCandidateValues());
|
||||
new Parameter(
|
||||
"s2.embedding.model.provider",
|
||||
InMemoryModelFactory.PROVIDER,
|
||||
"接口协议",
|
||||
"",
|
||||
"list",
|
||||
"向量模型配置",
|
||||
getCandidateValues());
|
||||
public static final Parameter EMBEDDING_MODEL_BASE_URL =
|
||||
new Parameter("s2.embedding.model.base.url", "",
|
||||
"BaseUrl", "", "string",
|
||||
"向量模型配置", null, getBaseUrlDependency()
|
||||
);
|
||||
new Parameter(
|
||||
"s2.embedding.model.base.url",
|
||||
"",
|
||||
"BaseUrl",
|
||||
"",
|
||||
"string",
|
||||
"向量模型配置",
|
||||
null,
|
||||
getBaseUrlDependency());
|
||||
|
||||
public static final Parameter EMBEDDING_MODEL_API_KEY =
|
||||
new Parameter("s2.embedding.model.api.key", "",
|
||||
"ApiKey", "", "password",
|
||||
"向量模型配置", null, getApiKeyDependency());
|
||||
new Parameter(
|
||||
"s2.embedding.model.api.key",
|
||||
"",
|
||||
"ApiKey",
|
||||
"",
|
||||
"password",
|
||||
"向量模型配置",
|
||||
null,
|
||||
getApiKeyDependency());
|
||||
|
||||
public static final Parameter EMBEDDING_MODEL_SECRET_KEY =
|
||||
new Parameter("s2.embedding.model.secretKey", "demo",
|
||||
"SecretKey", "", "password",
|
||||
"向量模型配置", null, getSecretKeyDependency());
|
||||
new Parameter(
|
||||
"s2.embedding.model.secretKey",
|
||||
"demo",
|
||||
"SecretKey",
|
||||
"",
|
||||
"password",
|
||||
"向量模型配置",
|
||||
null,
|
||||
getSecretKeyDependency());
|
||||
|
||||
public static final Parameter EMBEDDING_MODEL_NAME =
|
||||
new Parameter("s2.embedding.model.name", EmbeddingModelConstant.BGE_SMALL_ZH,
|
||||
"ModelName", "", "string",
|
||||
"向量模型配置", null, getModelNameDependency());
|
||||
new Parameter(
|
||||
"s2.embedding.model.name",
|
||||
EmbeddingModelConstant.BGE_SMALL_ZH,
|
||||
"ModelName",
|
||||
"",
|
||||
"string",
|
||||
"向量模型配置",
|
||||
null,
|
||||
getModelNameDependency());
|
||||
|
||||
public static final Parameter EMBEDDING_MODEL_PATH =
|
||||
new Parameter("s2.embedding.model.path", "",
|
||||
"模型路径", "", "string",
|
||||
"向量模型配置", null, getModelPathDependency());
|
||||
new Parameter(
|
||||
"s2.embedding.model.path",
|
||||
"",
|
||||
"模型路径",
|
||||
"",
|
||||
"string",
|
||||
"向量模型配置",
|
||||
null,
|
||||
getModelPathDependency());
|
||||
public static final Parameter EMBEDDING_MODEL_VOCABULARY_PATH =
|
||||
new Parameter("s2.embedding.model.vocabulary.path", "",
|
||||
"词汇表路径", "", "string",
|
||||
"向量模型配置", null, getModelPathDependency());
|
||||
new Parameter(
|
||||
"s2.embedding.model.vocabulary.path",
|
||||
"",
|
||||
"词汇表路径",
|
||||
"",
|
||||
"string",
|
||||
"向量模型配置",
|
||||
null,
|
||||
getModelPathDependency());
|
||||
|
||||
@Override
|
||||
public List<Parameter> getSysParameters() {
|
||||
return Lists.newArrayList(
|
||||
EMBEDDING_MODEL_PROVIDER, EMBEDDING_MODEL_BASE_URL, EMBEDDING_MODEL_API_KEY,
|
||||
EMBEDDING_MODEL_SECRET_KEY, EMBEDDING_MODEL_NAME, EMBEDDING_MODEL_PATH,
|
||||
EMBEDDING_MODEL_VOCABULARY_PATH
|
||||
);
|
||||
EMBEDDING_MODEL_PROVIDER,
|
||||
EMBEDDING_MODEL_BASE_URL,
|
||||
EMBEDDING_MODEL_API_KEY,
|
||||
EMBEDDING_MODEL_SECRET_KEY,
|
||||
EMBEDDING_MODEL_NAME,
|
||||
EMBEDDING_MODEL_PATH,
|
||||
EMBEDDING_MODEL_VOCABULARY_PATH);
|
||||
}
|
||||
|
||||
public EmbeddingModelConfig convert() {
|
||||
@@ -91,13 +134,14 @@ public class EmbeddingModelParameterConfig extends ParameterConfig {
|
||||
AzureModelFactory.PROVIDER,
|
||||
DashscopeModelFactory.PROVIDER,
|
||||
QianfanModelFactory.PROVIDER,
|
||||
ZhipuModelFactory.PROVIDER
|
||||
);
|
||||
ZhipuModelFactory.PROVIDER);
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getBaseUrlDependency() {
|
||||
return getDependency(EMBEDDING_MODEL_PROVIDER.getName(),
|
||||
Lists.newArrayList(OpenAiModelFactory.PROVIDER,
|
||||
return getDependency(
|
||||
EMBEDDING_MODEL_PROVIDER.getName(),
|
||||
Lists.newArrayList(
|
||||
OpenAiModelFactory.PROVIDER,
|
||||
OllamaModelFactory.PROVIDER,
|
||||
AzureModelFactory.PROVIDER,
|
||||
DashscopeModelFactory.PROVIDER,
|
||||
@@ -109,28 +153,34 @@ public class EmbeddingModelParameterConfig extends ParameterConfig {
|
||||
AzureModelFactory.PROVIDER, AzureModelFactory.DEFAULT_BASE_URL,
|
||||
DashscopeModelFactory.PROVIDER, DashscopeModelFactory.DEFAULT_BASE_URL,
|
||||
QianfanModelFactory.PROVIDER, QianfanModelFactory.DEFAULT_BASE_URL,
|
||||
ZhipuModelFactory.PROVIDER, ZhipuModelFactory.DEFAULT_BASE_URL
|
||||
)
|
||||
);
|
||||
ZhipuModelFactory.PROVIDER, ZhipuModelFactory.DEFAULT_BASE_URL));
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getApiKeyDependency() {
|
||||
return getDependency(EMBEDDING_MODEL_PROVIDER.getName(),
|
||||
Lists.newArrayList(OpenAiModelFactory.PROVIDER,
|
||||
return getDependency(
|
||||
EMBEDDING_MODEL_PROVIDER.getName(),
|
||||
Lists.newArrayList(
|
||||
OpenAiModelFactory.PROVIDER,
|
||||
AzureModelFactory.PROVIDER,
|
||||
DashscopeModelFactory.PROVIDER,
|
||||
QianfanModelFactory.PROVIDER,
|
||||
ZhipuModelFactory.PROVIDER),
|
||||
ImmutableMap.of(OpenAiModelFactory.PROVIDER, DEMO,
|
||||
AzureModelFactory.PROVIDER, DEMO,
|
||||
DashscopeModelFactory.PROVIDER, DEMO,
|
||||
QianfanModelFactory.PROVIDER, DEMO,
|
||||
ZhipuModelFactory.PROVIDER, DEMO)
|
||||
);
|
||||
ImmutableMap.of(
|
||||
OpenAiModelFactory.PROVIDER,
|
||||
DEMO,
|
||||
AzureModelFactory.PROVIDER,
|
||||
DEMO,
|
||||
DashscopeModelFactory.PROVIDER,
|
||||
DEMO,
|
||||
QianfanModelFactory.PROVIDER,
|
||||
DEMO,
|
||||
ZhipuModelFactory.PROVIDER,
|
||||
DEMO));
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getModelNameDependency() {
|
||||
return getDependency(EMBEDDING_MODEL_PROVIDER.getName(),
|
||||
return getDependency(
|
||||
EMBEDDING_MODEL_PROVIDER.getName(),
|
||||
Lists.newArrayList(
|
||||
InMemoryModelFactory.PROVIDER,
|
||||
OpenAiModelFactory.PROVIDER,
|
||||
@@ -138,31 +188,33 @@ public class EmbeddingModelParameterConfig extends ParameterConfig {
|
||||
AzureModelFactory.PROVIDER,
|
||||
DashscopeModelFactory.PROVIDER,
|
||||
QianfanModelFactory.PROVIDER,
|
||||
ZhipuModelFactory.PROVIDER
|
||||
),
|
||||
ZhipuModelFactory.PROVIDER),
|
||||
ImmutableMap.of(
|
||||
InMemoryModelFactory.PROVIDER, EmbeddingModelConstant.BGE_SMALL_ZH,
|
||||
OpenAiModelFactory.PROVIDER, OpenAiModelFactory.DEFAULT_EMBEDDING_MODEL_NAME,
|
||||
OllamaModelFactory.PROVIDER, OllamaModelFactory.DEFAULT_EMBEDDING_MODEL_NAME,
|
||||
OpenAiModelFactory.PROVIDER,
|
||||
OpenAiModelFactory.DEFAULT_EMBEDDING_MODEL_NAME,
|
||||
OllamaModelFactory.PROVIDER,
|
||||
OllamaModelFactory.DEFAULT_EMBEDDING_MODEL_NAME,
|
||||
AzureModelFactory.PROVIDER, AzureModelFactory.DEFAULT_EMBEDDING_MODEL_NAME,
|
||||
DashscopeModelFactory.PROVIDER, DashscopeModelFactory.DEFAULT_EMBEDDING_MODEL_NAME,
|
||||
QianfanModelFactory.PROVIDER, QianfanModelFactory.DEFAULT_EMBEDDING_MODEL_NAME,
|
||||
ZhipuModelFactory.PROVIDER, ZhipuModelFactory.DEFAULT_EMBEDDING_MODEL_NAME
|
||||
)
|
||||
);
|
||||
DashscopeModelFactory.PROVIDER,
|
||||
DashscopeModelFactory.DEFAULT_EMBEDDING_MODEL_NAME,
|
||||
QianfanModelFactory.PROVIDER,
|
||||
QianfanModelFactory.DEFAULT_EMBEDDING_MODEL_NAME,
|
||||
ZhipuModelFactory.PROVIDER,
|
||||
ZhipuModelFactory.DEFAULT_EMBEDDING_MODEL_NAME));
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getModelPathDependency() {
|
||||
return getDependency(EMBEDDING_MODEL_PROVIDER.getName(),
|
||||
return getDependency(
|
||||
EMBEDDING_MODEL_PROVIDER.getName(),
|
||||
Lists.newArrayList(InMemoryModelFactory.PROVIDER),
|
||||
ImmutableMap.of(InMemoryModelFactory.PROVIDER, "")
|
||||
);
|
||||
ImmutableMap.of(InMemoryModelFactory.PROVIDER, ""));
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getSecretKeyDependency() {
|
||||
return getDependency(EMBEDDING_MODEL_PROVIDER.getName(),
|
||||
return getDependency(
|
||||
EMBEDDING_MODEL_PROVIDER.getName(),
|
||||
Lists.newArrayList(QianfanModelFactory.PROVIDER),
|
||||
ImmutableMap.of(QianfanModelFactory.PROVIDER, DEMO)
|
||||
);
|
||||
ImmutableMap.of(QianfanModelFactory.PROVIDER, DEMO));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,47 +16,82 @@ import java.util.List;
|
||||
@Slf4j
|
||||
public class EmbeddingStoreParameterConfig extends ParameterConfig {
|
||||
public static final Parameter EMBEDDING_STORE_PROVIDER =
|
||||
new Parameter("s2.embedding.store.provider", EmbeddingStoreType.IN_MEMORY.name(),
|
||||
"向量库类型", "目前支持三种类型:IN_MEMORY、MILVUS、CHROMA", "list",
|
||||
"向量库配置", getCandidateValues());
|
||||
new Parameter(
|
||||
"s2.embedding.store.provider",
|
||||
EmbeddingStoreType.IN_MEMORY.name(),
|
||||
"向量库类型",
|
||||
"目前支持三种类型:IN_MEMORY、MILVUS、CHROMA",
|
||||
"list",
|
||||
"向量库配置",
|
||||
getCandidateValues());
|
||||
|
||||
public static final Parameter EMBEDDING_STORE_BASE_URL =
|
||||
new Parameter("s2.embedding.store.base.url", "",
|
||||
"BaseUrl", "", "string",
|
||||
"向量库配置", null, getBaseUrlDependency());
|
||||
new Parameter(
|
||||
"s2.embedding.store.base.url",
|
||||
"",
|
||||
"BaseUrl",
|
||||
"",
|
||||
"string",
|
||||
"向量库配置",
|
||||
null,
|
||||
getBaseUrlDependency());
|
||||
|
||||
public static final Parameter EMBEDDING_STORE_API_KEY =
|
||||
new Parameter("s2.embedding.store.api.key", "",
|
||||
"ApiKey", "", "password",
|
||||
"向量库配置", null, getApiKeyDependency());
|
||||
new Parameter(
|
||||
"s2.embedding.store.api.key",
|
||||
"",
|
||||
"ApiKey",
|
||||
"",
|
||||
"password",
|
||||
"向量库配置",
|
||||
null,
|
||||
getApiKeyDependency());
|
||||
|
||||
public static final Parameter EMBEDDING_STORE_PERSIST_PATH =
|
||||
new Parameter("s2.embedding.store.persist.path", "",
|
||||
"持久化路径", "默认不持久化,如需持久化请填写持久化路径。"
|
||||
+ "注意:如果变更了向量模型需删除该路径下已保存的文件或修改持久化路径", "string",
|
||||
"向量库配置", null, getPathDependency());
|
||||
new Parameter(
|
||||
"s2.embedding.store.persist.path",
|
||||
"",
|
||||
"持久化路径",
|
||||
"默认不持久化,如需持久化请填写持久化路径。" + "注意:如果变更了向量模型需删除该路径下已保存的文件或修改持久化路径",
|
||||
"string",
|
||||
"向量库配置",
|
||||
null,
|
||||
getPathDependency());
|
||||
|
||||
public static final Parameter EMBEDDING_STORE_TIMEOUT =
|
||||
new Parameter("s2.embedding.store.timeout", "60",
|
||||
"超时时间(秒)", "",
|
||||
"number", "向量库配置");
|
||||
new Parameter("s2.embedding.store.timeout", "60", "超时时间(秒)", "", "number", "向量库配置");
|
||||
|
||||
public static final Parameter EMBEDDING_STORE_DIMENSION =
|
||||
new Parameter("s2.embedding.store.dimension", "",
|
||||
"纬度", "", "number",
|
||||
"向量库配置", null, getDimensionDependency());
|
||||
new Parameter(
|
||||
"s2.embedding.store.dimension",
|
||||
"",
|
||||
"纬度",
|
||||
"",
|
||||
"number",
|
||||
"向量库配置",
|
||||
null,
|
||||
getDimensionDependency());
|
||||
public static final Parameter EMBEDDING_STORE_DATABASE_NAME =
|
||||
new Parameter("s2.embedding.store.databaseName", "",
|
||||
"DatabaseName", "", "string",
|
||||
"向量库配置", null, getDatabaseNameDependency());
|
||||
new Parameter(
|
||||
"s2.embedding.store.databaseName",
|
||||
"",
|
||||
"DatabaseName",
|
||||
"",
|
||||
"string",
|
||||
"向量库配置",
|
||||
null,
|
||||
getDatabaseNameDependency());
|
||||
|
||||
@Override
|
||||
public List<Parameter> getSysParameters() {
|
||||
return Lists.newArrayList(
|
||||
EMBEDDING_STORE_PROVIDER, EMBEDDING_STORE_BASE_URL, EMBEDDING_STORE_API_KEY,
|
||||
EMBEDDING_STORE_DATABASE_NAME, EMBEDDING_STORE_PERSIST_PATH,
|
||||
EMBEDDING_STORE_TIMEOUT, EMBEDDING_STORE_DIMENSION
|
||||
);
|
||||
EMBEDDING_STORE_PROVIDER,
|
||||
EMBEDDING_STORE_BASE_URL,
|
||||
EMBEDDING_STORE_API_KEY,
|
||||
EMBEDDING_STORE_DATABASE_NAME,
|
||||
EMBEDDING_STORE_PERSIST_PATH,
|
||||
EMBEDDING_STORE_TIMEOUT,
|
||||
EMBEDDING_STORE_DIMENSION);
|
||||
}
|
||||
|
||||
public EmbeddingStoreConfig convert() {
|
||||
@@ -70,9 +105,15 @@ public class EmbeddingStoreParameterConfig extends ParameterConfig {
|
||||
if (StringUtils.isNumeric(getParameterValue(EMBEDDING_STORE_DIMENSION))) {
|
||||
dimension = Integer.valueOf(getParameterValue(EMBEDDING_STORE_DIMENSION));
|
||||
}
|
||||
return EmbeddingStoreConfig.builder().provider(provider).baseUrl(baseUrl)
|
||||
.apiKey(apiKey).persistPath(persistPath).databaseName(databaseName)
|
||||
.timeOut(Long.valueOf(timeOut)).dimension(dimension).build();
|
||||
return EmbeddingStoreConfig.builder()
|
||||
.provider(provider)
|
||||
.baseUrl(baseUrl)
|
||||
.apiKey(apiKey)
|
||||
.persistPath(persistPath)
|
||||
.databaseName(databaseName)
|
||||
.timeOut(Long.valueOf(timeOut))
|
||||
.dimension(dimension)
|
||||
.build();
|
||||
}
|
||||
|
||||
private static ArrayList<String> getCandidateValues() {
|
||||
@@ -83,40 +124,40 @@ public class EmbeddingStoreParameterConfig extends ParameterConfig {
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getBaseUrlDependency() {
|
||||
return getDependency(EMBEDDING_STORE_PROVIDER.getName(),
|
||||
return getDependency(
|
||||
EMBEDDING_STORE_PROVIDER.getName(),
|
||||
Lists.newArrayList(
|
||||
EmbeddingStoreType.MILVUS.name(), EmbeddingStoreType.CHROMA.name()),
|
||||
ImmutableMap.of(
|
||||
EmbeddingStoreType.MILVUS.name(), "http://localhost:19530",
|
||||
EmbeddingStoreType.CHROMA.name(), "http://localhost:8000"
|
||||
)
|
||||
);
|
||||
EmbeddingStoreType.CHROMA.name(), "http://localhost:8000"));
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getApiKeyDependency() {
|
||||
return getDependency(EMBEDDING_STORE_PROVIDER.getName(),
|
||||
return getDependency(
|
||||
EMBEDDING_STORE_PROVIDER.getName(),
|
||||
Lists.newArrayList(EmbeddingStoreType.MILVUS.name()),
|
||||
ImmutableMap.of(EmbeddingStoreType.MILVUS.name(), DEMO)
|
||||
);
|
||||
ImmutableMap.of(EmbeddingStoreType.MILVUS.name(), DEMO));
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getPathDependency() {
|
||||
return getDependency(EMBEDDING_STORE_PROVIDER.getName(),
|
||||
return getDependency(
|
||||
EMBEDDING_STORE_PROVIDER.getName(),
|
||||
Lists.newArrayList(EmbeddingStoreType.IN_MEMORY.name()),
|
||||
ImmutableMap.of(EmbeddingStoreType.IN_MEMORY.name(), ""));
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getDimensionDependency() {
|
||||
return getDependency(EMBEDDING_STORE_PROVIDER.getName(),
|
||||
return getDependency(
|
||||
EMBEDDING_STORE_PROVIDER.getName(),
|
||||
Lists.newArrayList(EmbeddingStoreType.MILVUS.name()),
|
||||
ImmutableMap.of(EmbeddingStoreType.MILVUS.name(), "384")
|
||||
);
|
||||
ImmutableMap.of(EmbeddingStoreType.MILVUS.name(), "384"));
|
||||
}
|
||||
|
||||
private static List<Parameter.Dependency> getDatabaseNameDependency() {
|
||||
return getDependency(EMBEDDING_STORE_PROVIDER.getName(),
|
||||
return getDependency(
|
||||
EMBEDDING_STORE_PROVIDER.getName(),
|
||||
Lists.newArrayList(EmbeddingStoreType.MILVUS.name()),
|
||||
ImmutableMap.of(EmbeddingStoreType.MILVUS.name(), "")
|
||||
);
|
||||
ImmutableMap.of(EmbeddingStoreType.MILVUS.name(), ""));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.tencent.supersonic.common.config;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
public class JdbcTemplateConfig {
|
||||
|
||||
@@ -14,5 +14,4 @@ public class JdbcTemplateConfig {
|
||||
public JdbcTemplate jdbcTemplate(@Qualifier("h2") DataSource dataSource) {
|
||||
return new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,24 +15,19 @@ import java.util.Map;
|
||||
@Service
|
||||
public abstract class ParameterConfig {
|
||||
public static final String DEMO = "demo";
|
||||
@Autowired
|
||||
private SystemConfigService sysConfigService;
|
||||
@Autowired private SystemConfigService sysConfigService;
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
@Autowired private Environment environment;
|
||||
|
||||
/**
|
||||
* @return system parameters to be set with user interface
|
||||
*/
|
||||
/** @return system parameters to be set with user interface */
|
||||
protected List<Parameter> getSysParameters() {
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parameter value will be derived in the following order:
|
||||
* 1. `system config` set with user interface
|
||||
* 2. `system property` set with application.yaml file
|
||||
* 3. `default value` set with parameter declaration
|
||||
* Parameter value will be derived in the following order: 1. `system config` set with user
|
||||
* interface 2. `system property` set with application.yaml file 3. `default value` set with
|
||||
* parameter declaration
|
||||
*
|
||||
* @param parameter instance
|
||||
* @return parameter value
|
||||
@@ -67,5 +62,4 @@ public abstract class ParameterConfig {
|
||||
dependencies.add(dependency);
|
||||
return dependencies;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,5 +10,4 @@ import lombok.NoArgsConstructor;
|
||||
public class PromptConfig {
|
||||
|
||||
private String promptTemplate;
|
||||
|
||||
}
|
||||
|
||||
@@ -38,8 +38,11 @@ public class SystemConfig {
|
||||
if (StringUtils.isBlank(name)) {
|
||||
return "";
|
||||
}
|
||||
Map<String, String> nameToValue = getParameters().stream()
|
||||
.collect(Collectors.toMap(Parameter::getName, Parameter::getValue, (k1, k2) -> k1));
|
||||
Map<String, String> nameToValue =
|
||||
getParameters().stream()
|
||||
.collect(
|
||||
Collectors.toMap(
|
||||
Parameter::getName, Parameter::getValue, (k1, k2) -> k1));
|
||||
return nameToValue.get(name);
|
||||
}
|
||||
|
||||
@@ -66,13 +69,16 @@ public class SystemConfig {
|
||||
if (CollectionUtils.isEmpty(parameters)) {
|
||||
return defaultParameters;
|
||||
}
|
||||
Map<String, String> parameterNameValueMap = parameters.stream()
|
||||
.collect(Collectors.toMap(Parameter::getName, Parameter::getValue, (v1, v2) -> v2));
|
||||
Map<String, String> parameterNameValueMap =
|
||||
parameters.stream()
|
||||
.collect(
|
||||
Collectors.toMap(
|
||||
Parameter::getName, Parameter::getValue, (v1, v2) -> v2));
|
||||
for (Parameter parameter : defaultParameters) {
|
||||
parameter.setValue(parameterNameValueMap.getOrDefault(parameter.getName(),
|
||||
parameter.getDefaultValue()));
|
||||
parameter.setValue(
|
||||
parameterNameValueMap.getOrDefault(
|
||||
parameter.getName(), parameter.getDefaultValue()));
|
||||
}
|
||||
return defaultParameters;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,4 +11,4 @@ public class ThreadContextConfig {
|
||||
public S2ThreadContext s2ThreadContext() {
|
||||
return new S2ThreadContext();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,5 +8,4 @@ public class VisualConfig {
|
||||
private boolean enableSimpleMode;
|
||||
|
||||
private boolean showDebugInfo;
|
||||
|
||||
}
|
||||
|
||||
@@ -13,4 +13,3 @@ public class WebMvcConfig implements WebMvcConfigurer {
|
||||
registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.tencent.supersonic.common.interceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.tencent.supersonic.common.util.TraceIdUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -7,15 +10,13 @@ import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class LogInterceptor implements HandlerInterceptor {
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
//use previous traceId
|
||||
public boolean preHandle(
|
||||
HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
// use previous traceId
|
||||
String traceId = request.getHeader(TraceIdUtil.TRACE_ID);
|
||||
if (StringUtils.isBlank(traceId)) {
|
||||
TraceIdUtil.setTraceId(TraceIdUtil.generateTraceId());
|
||||
@@ -26,16 +27,18 @@ public class LogInterceptor implements HandlerInterceptor {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postHandle(HttpServletRequest request, HttpServletResponse response,
|
||||
Object handler, ModelAndView modelAndView)
|
||||
throws Exception {
|
||||
|
||||
}
|
||||
public void postHandle(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
Object handler,
|
||||
ModelAndView modelAndView)
|
||||
throws Exception {}
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
|
||||
public void afterCompletion(
|
||||
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
|
||||
throws Exception {
|
||||
//remove after Completing
|
||||
// remove after Completing
|
||||
TraceIdUtil.remove();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,8 @@ public enum AggregateEnum {
|
||||
|
||||
public static Map<String, String> getAggregateEnum() {
|
||||
return Arrays.stream(AggregateEnum.values())
|
||||
.collect(Collectors.toMap(AggregateEnum::getAggregateCh, AggregateEnum::getAggregateEN));
|
||||
.collect(
|
||||
Collectors.toMap(
|
||||
AggregateEnum::getAggregateCh, AggregateEnum::getAggregateEN));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,8 @@ import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
|
||||
@Slf4j
|
||||
public class DateFunctionHelper {
|
||||
|
||||
public static String getStartDateStr(ComparisonOperator minorThanEquals, ExpressionList<?> expressions) {
|
||||
public static String getStartDateStr(
|
||||
ComparisonOperator minorThanEquals, ExpressionList<?> expressions) {
|
||||
String unitValue = getUnit(expressions);
|
||||
String dateValue = getEndDateValue(expressions);
|
||||
String dateStr = "";
|
||||
@@ -22,9 +23,11 @@ public class DateFunctionHelper {
|
||||
if (rightExpression instanceof DoubleValue) {
|
||||
DoubleValue value = (DoubleValue) rightExpression;
|
||||
double doubleValue = value.getValue();
|
||||
if (DatePeriodEnum.YEAR.equals(datePeriodEnum) && doubleValue == JsqlConstants.HALF_YEAR) {
|
||||
if (DatePeriodEnum.YEAR.equals(datePeriodEnum)
|
||||
&& doubleValue == JsqlConstants.HALF_YEAR) {
|
||||
datePeriodEnum = DatePeriodEnum.MONTH;
|
||||
dateStr = DateUtils.getBeforeDate(dateValue, JsqlConstants.SIX_MONTH, datePeriodEnum);
|
||||
dateStr =
|
||||
DateUtils.getBeforeDate(dateValue, JsqlConstants.SIX_MONTH, datePeriodEnum);
|
||||
}
|
||||
} else if (rightExpression instanceof LongValue) {
|
||||
LongValue value = (LongValue) rightExpression;
|
||||
@@ -47,5 +50,4 @@ public class DateFunctionHelper {
|
||||
StringValue unit = (StringValue) expressions.get(0);
|
||||
return unit.getValue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
@@ -12,6 +11,8 @@ import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class DateVisitor extends ExpressionVisitorAdapter {
|
||||
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import net.sf.jsqlparser.expression.BinaryExpression;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
|
||||
@@ -10,11 +8,13 @@ import net.sf.jsqlparser.expression.WhenClause;
|
||||
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ExpressionReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
|
||||
private Map<String, String> fieldExprMap;
|
||||
|
||||
|
||||
public ExpressionReplaceVisitor(Map<String, String> fieldExprMap) {
|
||||
this.fieldExprMap = fieldExprMap;
|
||||
}
|
||||
@@ -23,8 +23,9 @@ public class ExpressionReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
expr.getWhenExpression().accept(this);
|
||||
if (expr.getThenExpression() instanceof Column) {
|
||||
Column column = (Column) expr.getThenExpression();
|
||||
Expression expression = QueryExpressionReplaceVisitor.getExpression(
|
||||
QueryExpressionReplaceVisitor.getReplaceExpr(column, fieldExprMap));
|
||||
Expression expression =
|
||||
QueryExpressionReplaceVisitor.getExpression(
|
||||
QueryExpressionReplaceVisitor.getReplaceExpr(column, fieldExprMap));
|
||||
if (Objects.nonNull(expression)) {
|
||||
expr.setThenExpression(expression);
|
||||
}
|
||||
@@ -51,16 +52,20 @@ public class ExpressionReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
}
|
||||
}
|
||||
if (left instanceof Column) {
|
||||
Expression expression = QueryExpressionReplaceVisitor.getExpression(
|
||||
QueryExpressionReplaceVisitor.getReplaceExpr((Column) left, fieldExprMap));
|
||||
Expression expression =
|
||||
QueryExpressionReplaceVisitor.getExpression(
|
||||
QueryExpressionReplaceVisitor.getReplaceExpr(
|
||||
(Column) left, fieldExprMap));
|
||||
if (Objects.nonNull(expression)) {
|
||||
expr.setLeftExpression(expression);
|
||||
leftVisited = true;
|
||||
}
|
||||
}
|
||||
if (right instanceof Column) {
|
||||
Expression expression = QueryExpressionReplaceVisitor.getExpression(
|
||||
QueryExpressionReplaceVisitor.getReplaceExpr((Column) right, fieldExprMap));
|
||||
Expression expression =
|
||||
QueryExpressionReplaceVisitor.getExpression(
|
||||
QueryExpressionReplaceVisitor.getReplaceExpr(
|
||||
(Column) right, fieldExprMap));
|
||||
if (Objects.nonNull(expression)) {
|
||||
expr.setRightExpression(expression);
|
||||
rightVisited = true;
|
||||
@@ -76,8 +81,9 @@ public class ExpressionReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
|
||||
private boolean visitFunction(Function function) {
|
||||
if (function.getParameters().getExpressions().get(0) instanceof Column) {
|
||||
Expression expression = QueryExpressionReplaceVisitor.getExpression(
|
||||
QueryExpressionReplaceVisitor.getReplaceExpr(function, fieldExprMap));
|
||||
Expression expression =
|
||||
QueryExpressionReplaceVisitor.getExpression(
|
||||
QueryExpressionReplaceVisitor.getReplaceExpr(function, fieldExprMap));
|
||||
if (Objects.nonNull(expression)) {
|
||||
ExpressionList<Expression> expressions = new ExpressionList<>();
|
||||
expressions.add(expression);
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
import java.util.Set;
|
||||
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class FieldAcquireVisitor extends ExpressionVisitorAdapter {
|
||||
|
||||
private Set<String> fields;
|
||||
@@ -17,4 +18,4 @@ public class FieldAcquireVisitor extends ExpressionVisitorAdapter {
|
||||
String columnName = column.getColumnName();
|
||||
fields.add(columnName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,6 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
import com.tencent.supersonic.common.pojo.enums.DatePeriodEnum;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import net.sf.jsqlparser.expression.DoubleValue;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
|
||||
@@ -25,6 +19,13 @@ import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class FieldAndValueAcquireVisitor extends ExpressionVisitorAdapter {
|
||||
|
||||
private Set<FieldExpression> fieldExpressions;
|
||||
@@ -124,14 +125,16 @@ public class FieldAndValueAcquireVisitor extends ExpressionVisitorAdapter {
|
||||
fieldExpression.setFieldName(field.getColumnName());
|
||||
fieldExpression.setFunction(functionName);
|
||||
fieldExpression.setOperator(expr.getStringExpression());
|
||||
//deal with DAY/WEEK function
|
||||
List<DatePeriodEnum> collect = Arrays.stream(DatePeriodEnum.values()).collect(Collectors.toList());
|
||||
// deal with DAY/WEEK function
|
||||
List<DatePeriodEnum> collect =
|
||||
Arrays.stream(DatePeriodEnum.values()).collect(Collectors.toList());
|
||||
DatePeriodEnum periodEnum = DatePeriodEnum.get(functionName);
|
||||
if (Objects.nonNull(periodEnum) && collect.contains(periodEnum)) {
|
||||
fieldExpression.setFieldValue(getFieldValue(rightExpression) + periodEnum.getChName());
|
||||
fieldExpression.setFieldValue(
|
||||
getFieldValue(rightExpression) + periodEnum.getChName());
|
||||
return fieldExpression;
|
||||
} else {
|
||||
//deal with aggregate function
|
||||
// deal with aggregate function
|
||||
fieldExpression.setFieldValue(getFieldValue(rightExpression));
|
||||
return fieldExpression;
|
||||
}
|
||||
@@ -142,7 +145,8 @@ public class FieldAndValueAcquireVisitor extends ExpressionVisitorAdapter {
|
||||
}
|
||||
|
||||
private Column getColumn(Function leftExpressionFunction) {
|
||||
//List<Expression> leftExpressions = leftExpressionFunction.getParameters().getExpressions();
|
||||
// List<Expression> leftExpressions =
|
||||
// leftExpressionFunction.getParameters().getExpressions();
|
||||
ExpressionList<?> leftExpressions = leftExpressionFunction.getParameters();
|
||||
if (CollectionUtils.isEmpty(leftExpressions)) {
|
||||
return null;
|
||||
|
||||
@@ -12,5 +12,4 @@ public class FieldExpression {
|
||||
private Object fieldValue;
|
||||
|
||||
private String function;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,4 +33,4 @@ public class FieldReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
exactReplace.set(originalExactReplace);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,8 @@ public class FieldValueReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
private boolean exactReplace;
|
||||
private Map<String, Map<String, String>> filedNameToValueMap;
|
||||
|
||||
public FieldValueReplaceVisitor(boolean exactReplace, Map<String, Map<String, String>> filedNameToValueMap) {
|
||||
public FieldValueReplaceVisitor(
|
||||
boolean exactReplace, Map<String, Map<String, String>> filedNameToValueMap) {
|
||||
this.exactReplace = exactReplace;
|
||||
this.filedNameToValueMap = filedNameToValueMap;
|
||||
}
|
||||
@@ -66,20 +67,24 @@ public class FieldValueReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
ExpressionList rightItemsList = (ExpressionList) inExpression.getRightExpression();
|
||||
List<Expression> expressions = rightItemsList.getExpressions();
|
||||
List<String> values = new ArrayList<>();
|
||||
expressions.stream().forEach(o -> {
|
||||
if (o instanceof StringValue) {
|
||||
values.add(((StringValue) o).getValue());
|
||||
}
|
||||
});
|
||||
expressions.stream()
|
||||
.forEach(
|
||||
o -> {
|
||||
if (o instanceof StringValue) {
|
||||
values.add(((StringValue) o).getValue());
|
||||
}
|
||||
});
|
||||
if (valueMap == null || CollectionUtils.isEmpty(values)) {
|
||||
return;
|
||||
}
|
||||
List<Expression> newExpressions = new ArrayList<>();
|
||||
values.stream().forEach(o -> {
|
||||
String replaceValue = valueMap.getOrDefault(o, o);
|
||||
StringValue stringValue = new StringValue(replaceValue);
|
||||
newExpressions.add(stringValue);
|
||||
});
|
||||
values.stream()
|
||||
.forEach(
|
||||
o -> {
|
||||
String replaceValue = valueMap.getOrDefault(o, o);
|
||||
StringValue stringValue = new StringValue(replaceValue);
|
||||
newExpressions.add(stringValue);
|
||||
});
|
||||
rightItemsList.setExpressions(newExpressions);
|
||||
inExpression.setRightExpression(rightItemsList);
|
||||
}
|
||||
@@ -107,21 +112,24 @@ public class FieldValueReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
}
|
||||
if (rightExpression instanceof LongValue) {
|
||||
LongValue rightStringValue = (LongValue) rightExpression;
|
||||
String replaceValue = getReplaceValue(valueMap, String.valueOf(rightStringValue.getValue()));
|
||||
String replaceValue =
|
||||
getReplaceValue(valueMap, String.valueOf(rightStringValue.getValue()));
|
||||
if (StringUtils.isNotEmpty(replaceValue)) {
|
||||
rightStringValue.setValue(Long.parseLong(replaceValue));
|
||||
}
|
||||
}
|
||||
if (rightExpression instanceof DoubleValue) {
|
||||
DoubleValue rightStringValue = (DoubleValue) rightExpression;
|
||||
String replaceValue = getReplaceValue(valueMap, String.valueOf(rightStringValue.getValue()));
|
||||
String replaceValue =
|
||||
getReplaceValue(valueMap, String.valueOf(rightStringValue.getValue()));
|
||||
if (StringUtils.isNotEmpty(replaceValue)) {
|
||||
rightStringValue.setValue(Double.parseDouble(replaceValue));
|
||||
}
|
||||
}
|
||||
if (rightExpression instanceof StringValue) {
|
||||
StringValue rightStringValue = (StringValue) rightExpression;
|
||||
String replaceValue = getReplaceValue(valueMap, String.valueOf(rightStringValue.getValue()));
|
||||
String replaceValue =
|
||||
getReplaceValue(valueMap, String.valueOf(rightStringValue.getValue()));
|
||||
if (StringUtils.isNotEmpty(replaceValue)) {
|
||||
rightStringValue.setValue(replaceValue);
|
||||
}
|
||||
|
||||
@@ -8,5 +8,4 @@ public class FiledExpression {
|
||||
private String operator;
|
||||
|
||||
private String fieldName;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,8 @@ public class FiledFilterReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
|
||||
@Override
|
||||
public void visit(GreaterThanEquals expr) {
|
||||
List<Expression> expressions = parserFilter(expr, JsqlConstants.GREATER_THAN_EQUALS_CONSTANT);
|
||||
List<Expression> expressions =
|
||||
parserFilter(expr, JsqlConstants.GREATER_THAN_EQUALS_CONSTANT);
|
||||
if (Objects.nonNull(expressions)) {
|
||||
waitingForAdds.addAll(expressions);
|
||||
}
|
||||
@@ -101,7 +102,8 @@ public class FiledFilterReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
}
|
||||
|
||||
try {
|
||||
ComparisonOperator parsedExpression = (ComparisonOperator) CCJSqlParserUtil.parseCondExpression(condExpr);
|
||||
ComparisonOperator parsedExpression =
|
||||
(ComparisonOperator) CCJSqlParserUtil.parseCondExpression(condExpr);
|
||||
comparisonOperator.setLeftExpression(parsedExpression.getLeftExpression());
|
||||
comparisonOperator.setRightExpression(parsedExpression.getRightExpression());
|
||||
comparisonOperator.setASTNode(parsedExpression.getASTNode());
|
||||
|
||||
@@ -34,9 +34,11 @@ public class FiledNameReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
Expression leftExpression = expr.getLeftExpression();
|
||||
Expression rightExpression = expr.getRightExpression();
|
||||
|
||||
if (!(rightExpression instanceof StringValue) || !(leftExpression instanceof Column)
|
||||
if (!(rightExpression instanceof StringValue)
|
||||
|| !(leftExpression instanceof Column)
|
||||
|| CollectionUtils.isEmpty(fieldValueToFieldNames)
|
||||
|| Objects.isNull(rightExpression) || Objects.isNull(leftExpression)) {
|
||||
|| Objects.isNull(rightExpression)
|
||||
|| Objects.isNull(leftExpression)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -44,9 +46,9 @@ public class FiledNameReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
StringValue rightStringValue = (StringValue) rightExpression;
|
||||
|
||||
Set<String> fieldNames = fieldValueToFieldNames.get(rightStringValue.getValue());
|
||||
if (!CollectionUtils.isEmpty(fieldNames) && !fieldNames.contains(leftColumn.getColumnName())) {
|
||||
if (!CollectionUtils.isEmpty(fieldNames)
|
||||
&& !fieldNames.contains(leftColumn.getColumnName())) {
|
||||
leftColumn.setColumnName(fieldNames.stream().findFirst().get());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
import net.sf.jsqlparser.expression.Function;
|
||||
import net.sf.jsqlparser.statement.select.SelectItem;
|
||||
import net.sf.jsqlparser.statement.select.SelectItemVisitorAdapter;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import net.sf.jsqlparser.expression.Function;
|
||||
import net.sf.jsqlparser.statement.select.SelectItem;
|
||||
import net.sf.jsqlparser.statement.select.SelectItemVisitorAdapter;
|
||||
|
||||
public class FunctionAliasReplaceVisitor extends SelectItemVisitorAdapter {
|
||||
|
||||
@@ -16,11 +17,12 @@ public class FunctionAliasReplaceVisitor extends SelectItemVisitorAdapter {
|
||||
if (selectExpressionItem.getExpression() instanceof Function) {
|
||||
Function function = (Function) selectExpressionItem.getExpression();
|
||||
String columnName = SqlSelectHelper.getColumnName(function);
|
||||
//1.exist alias. as
|
||||
//2.alias's fieldName not equal. "sum(pv) as pv" cannot be replaced.
|
||||
if (Objects.nonNull(selectExpressionItem.getAlias()) && !selectExpressionItem.getAlias().getName()
|
||||
.equalsIgnoreCase(columnName)) {
|
||||
aliasToActualExpression.put(selectExpressionItem.getAlias().getName(), function.toString());
|
||||
// 1.exist alias. as
|
||||
// 2.alias's fieldName not equal. "sum(pv) as pv" cannot be replaced.
|
||||
if (Objects.nonNull(selectExpressionItem.getAlias())
|
||||
&& !selectExpressionItem.getAlias().getName().equalsIgnoreCase(columnName)) {
|
||||
aliasToActualExpression.put(
|
||||
selectExpressionItem.getAlias().getName(), function.toString());
|
||||
selectExpressionItem.setAlias(null);
|
||||
}
|
||||
}
|
||||
@@ -29,4 +31,4 @@ public class FunctionAliasReplaceVisitor extends SelectItemVisitorAdapter {
|
||||
public Map<String, String> getAliasToActualExpression() {
|
||||
return aliasToActualExpression;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,8 @@ public class FunctionNameReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
private Map<String, String> functionMap;
|
||||
private Map<String, UnaryOperator> functionCallMap;
|
||||
|
||||
public FunctionNameReplaceVisitor(Map<String, String> functionMap, Map<String, UnaryOperator> functionCallMap) {
|
||||
public FunctionNameReplaceVisitor(
|
||||
Map<String, String> functionMap, Map<String, UnaryOperator> functionCallMap) {
|
||||
this.functionMap = functionMap;
|
||||
this.functionCallMap = functionCallMap;
|
||||
}
|
||||
@@ -35,5 +36,4 @@ public class FunctionNameReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
|
||||
import net.sf.jsqlparser.expression.Function;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import net.sf.jsqlparser.expression.ExpressionVisitorAdapter;
|
||||
import net.sf.jsqlparser.expression.Function;
|
||||
|
||||
public class FunctionVisitor extends ExpressionVisitorAdapter {
|
||||
|
||||
@@ -18,4 +19,4 @@ public class FunctionVisitor extends ExpressionVisitorAdapter {
|
||||
super.visit(function);
|
||||
functionNames.add(function.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,8 @@ public class GroupByFunctionReplaceVisitor implements GroupByVisitor {
|
||||
private Map<String, String> functionMap;
|
||||
private Map<String, UnaryOperator> functionCallMap;
|
||||
|
||||
public GroupByFunctionReplaceVisitor(Map<String, String> functionMap, Map<String, UnaryOperator> functionCallMap) {
|
||||
public GroupByFunctionReplaceVisitor(
|
||||
Map<String, String> functionMap, Map<String, UnaryOperator> functionCallMap) {
|
||||
this.functionMap = functionMap;
|
||||
this.functionCallMap = functionCallMap;
|
||||
}
|
||||
@@ -48,4 +49,4 @@ public class GroupByFunctionReplaceVisitor implements GroupByVisitor {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,8 @@ public class GroupByReplaceVisitor implements GroupByVisitor {
|
||||
Expression expression = groupByExpressions.get(i);
|
||||
String columnName = getColumnName(expression);
|
||||
|
||||
String replaceColumn = parseVisitorHelper.getReplaceValue(columnName, fieldNameMap, exactReplace);
|
||||
String replaceColumn =
|
||||
parseVisitorHelper.getReplaceValue(columnName, fieldNameMap, exactReplace);
|
||||
if (StringUtils.isNotEmpty(replaceColumn)) {
|
||||
replaceExpression(groupByExpressions, i, expression, replaceColumn);
|
||||
}
|
||||
@@ -52,10 +53,11 @@ public class GroupByReplaceVisitor implements GroupByVisitor {
|
||||
return expression.toString();
|
||||
}
|
||||
|
||||
private void replaceExpression(List<Expression> groupByExpressions,
|
||||
int index,
|
||||
Expression expression,
|
||||
String replaceColumn) {
|
||||
private void replaceExpression(
|
||||
List<Expression> groupByExpressions,
|
||||
int index,
|
||||
Expression expression,
|
||||
String replaceColumn) {
|
||||
if (expression instanceof Column) {
|
||||
groupByExpressions.set(index, new Column(replaceColumn));
|
||||
} else if (expression instanceof Function) {
|
||||
@@ -66,9 +68,9 @@ public class GroupByReplaceVisitor implements GroupByVisitor {
|
||||
|
||||
Function function = (Function) expression;
|
||||
if (function.getParameters().size() > 1) {
|
||||
function.getParameters().stream().skip(1).forEach(
|
||||
e -> newExpressionList.add((Function) e)
|
||||
);
|
||||
function.getParameters().stream()
|
||||
.skip(1)
|
||||
.forEach(e -> newExpressionList.add((Function) e));
|
||||
}
|
||||
function.setParameters(newExpressionList);
|
||||
} catch (JSQLParserException e) {
|
||||
@@ -76,4 +78,4 @@ public class GroupByReplaceVisitor implements GroupByVisitor {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,4 +15,4 @@ public class GroupByVisitor implements net.sf.jsqlparser.statement.select.GroupB
|
||||
public boolean isHasAggregateFunction() {
|
||||
return hasAggregateFunction;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,19 +27,26 @@ public class JsqlConstants {
|
||||
public static final String IN_CONSTANT = " 1 in (1) ";
|
||||
public static final String LIKE_CONSTANT = "1 like 1";
|
||||
public static final String IN = "IN";
|
||||
public static final Map<String, String> rightMap = Stream.of(
|
||||
new AbstractMap.SimpleEntry<>("<=", "<="),
|
||||
new AbstractMap.SimpleEntry<>("<", "<"),
|
||||
new AbstractMap.SimpleEntry<>(">=", "<="),
|
||||
new AbstractMap.SimpleEntry<>(">", "<"),
|
||||
new AbstractMap.SimpleEntry<>("=", "<="))
|
||||
.collect(toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
|
||||
public static final Map<String, String> leftMap = Stream.of(
|
||||
new AbstractMap.SimpleEntry<>("<=", ">="),
|
||||
new AbstractMap.SimpleEntry<>("<", ">"),
|
||||
new AbstractMap.SimpleEntry<>(">=", "<="),
|
||||
new AbstractMap.SimpleEntry<>(">", "<"),
|
||||
new AbstractMap.SimpleEntry<>("=", ">="))
|
||||
.collect(toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue));
|
||||
|
||||
public static final Map<String, String> rightMap =
|
||||
Stream.of(
|
||||
new AbstractMap.SimpleEntry<>("<=", "<="),
|
||||
new AbstractMap.SimpleEntry<>("<", "<"),
|
||||
new AbstractMap.SimpleEntry<>(">=", "<="),
|
||||
new AbstractMap.SimpleEntry<>(">", "<"),
|
||||
new AbstractMap.SimpleEntry<>("=", "<="))
|
||||
.collect(
|
||||
toMap(
|
||||
AbstractMap.SimpleEntry::getKey,
|
||||
AbstractMap.SimpleEntry::getValue));
|
||||
public static final Map<String, String> leftMap =
|
||||
Stream.of(
|
||||
new AbstractMap.SimpleEntry<>("<=", ">="),
|
||||
new AbstractMap.SimpleEntry<>("<", ">"),
|
||||
new AbstractMap.SimpleEntry<>(">=", "<="),
|
||||
new AbstractMap.SimpleEntry<>(">", "<"),
|
||||
new AbstractMap.SimpleEntry<>("=", ">="))
|
||||
.collect(
|
||||
toMap(
|
||||
AbstractMap.SimpleEntry::getKey,
|
||||
AbstractMap.SimpleEntry::getValue));
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
import com.tencent.supersonic.common.pojo.Constants;
|
||||
import java.util.Set;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.Function;
|
||||
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
|
||||
@@ -9,6 +8,8 @@ import net.sf.jsqlparser.schema.Column;
|
||||
import net.sf.jsqlparser.statement.select.OrderByElement;
|
||||
import net.sf.jsqlparser.statement.select.OrderByVisitorAdapter;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class OrderByAcquireVisitor extends OrderByVisitorAdapter {
|
||||
|
||||
private Set<FieldExpression> fields;
|
||||
@@ -26,7 +27,7 @@ public class OrderByAcquireVisitor extends OrderByVisitorAdapter {
|
||||
}
|
||||
if (expression instanceof Function) {
|
||||
Function function = (Function) expression;
|
||||
//List<Expression> expressions = function.getParameters().getExpressions();
|
||||
// List<Expression> expressions = function.getParameters().getExpressions();
|
||||
ExpressionList<?> expressions = function.getParameters();
|
||||
for (Expression column : expressions) {
|
||||
if (column instanceof Column) {
|
||||
@@ -42,4 +43,4 @@ public class OrderByAcquireVisitor extends OrderByVisitorAdapter {
|
||||
fields.add(fieldExpression);
|
||||
super.visit(orderBy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,5 +12,4 @@ public class OrderByExpression {
|
||||
private Object fieldValue;
|
||||
|
||||
private String function;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
import java.util.Map;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.Function;
|
||||
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
|
||||
@@ -8,6 +7,8 @@ import net.sf.jsqlparser.schema.Column;
|
||||
import net.sf.jsqlparser.statement.select.OrderByElement;
|
||||
import net.sf.jsqlparser.statement.select.OrderByVisitorAdapter;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class OrderByReplaceVisitor extends OrderByVisitorAdapter {
|
||||
|
||||
ParseVisitorHelper parseVisitorHelper = new ParseVisitorHelper();
|
||||
@@ -27,7 +28,7 @@ public class OrderByReplaceVisitor extends OrderByVisitorAdapter {
|
||||
}
|
||||
if (expression instanceof Function) {
|
||||
Function function = (Function) expression;
|
||||
//List<Expression> expressions = function.getParameters().getExpressions();
|
||||
// List<Expression> expressions = function.getParameters().getExpressions();
|
||||
ExpressionList<?> expressions = function.getParameters();
|
||||
for (Expression column : expressions) {
|
||||
if (column instanceof Column) {
|
||||
@@ -37,4 +38,4 @@ public class OrderByReplaceVisitor extends OrderByVisitorAdapter {
|
||||
}
|
||||
super.visit(orderBy);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
@Slf4j
|
||||
public class ParseVisitorHelper {
|
||||
|
||||
public void replaceColumn(Column column, Map<String, String> fieldNameMap, boolean exactReplace) {
|
||||
public void replaceColumn(
|
||||
Column column, Map<String, String> fieldNameMap, boolean exactReplace) {
|
||||
String columnName = column.getColumnName();
|
||||
String replaceColumn = getReplaceValue(columnName, fieldNameMap, exactReplace);
|
||||
if (StringUtils.isNotBlank(replaceColumn)) {
|
||||
@@ -19,7 +21,8 @@ public class ParseVisitorHelper {
|
||||
}
|
||||
}
|
||||
|
||||
public String getReplaceValue(String beforeValue, Map<String, String> valueMap, boolean exactReplace) {
|
||||
public String getReplaceValue(
|
||||
String beforeValue, Map<String, String> valueMap, boolean exactReplace) {
|
||||
String value = valueMap.get(beforeValue);
|
||||
if (StringUtils.isNotBlank(value)) {
|
||||
return value;
|
||||
@@ -27,13 +30,19 @@ public class ParseVisitorHelper {
|
||||
if (exactReplace) {
|
||||
return null;
|
||||
}
|
||||
Optional<Entry<String, String>> first = valueMap.entrySet().stream().sorted((k1, k2) -> {
|
||||
String k1Value = k1.getKey();
|
||||
String k2Value = k2.getKey();
|
||||
Double k1Similarity = getSimilarity(beforeValue, k1Value);
|
||||
Double k2Similarity = getSimilarity(beforeValue, k2Value);
|
||||
return k2Similarity.compareTo(k1Similarity);
|
||||
}).collect(Collectors.toList()).stream().findFirst();
|
||||
Optional<Entry<String, String>> first =
|
||||
valueMap.entrySet().stream()
|
||||
.sorted(
|
||||
(k1, k2) -> {
|
||||
String k1Value = k1.getKey();
|
||||
String k2Value = k2.getKey();
|
||||
Double k1Similarity = getSimilarity(beforeValue, k1Value);
|
||||
Double k2Similarity = getSimilarity(beforeValue, k2Value);
|
||||
return k2Similarity.compareTo(k1Similarity);
|
||||
})
|
||||
.collect(Collectors.toList())
|
||||
.stream()
|
||||
.findFirst();
|
||||
|
||||
if (first.isPresent()) {
|
||||
return first.get().getValue();
|
||||
@@ -58,10 +67,16 @@ public class ParseVisitorHelper {
|
||||
char cj = word2.charAt(j - 1);
|
||||
if (ci == cj) {
|
||||
dp[i][j] = dp[i - 1][j - 1];
|
||||
} else if (i > 1 && j > 1 && ci == word2.charAt(j - 2) && cj == word1.charAt(i - 2)) {
|
||||
} else if (i > 1
|
||||
&& j > 1
|
||||
&& ci == word2.charAt(j - 2)
|
||||
&& cj == word1.charAt(i - 2)) {
|
||||
dp[i][j] = 1 + Math.min(dp[i - 2][j - 2], Math.min(dp[i][j - 1], dp[i - 1][j]));
|
||||
} else {
|
||||
dp[i][j] = Math.min(dp[i - 1][j - 1] + 1, Math.min(dp[i][j - 1] + 1, dp[i - 1][j] + 1));
|
||||
dp[i][j] =
|
||||
Math.min(
|
||||
dp[i - 1][j - 1] + 1,
|
||||
Math.min(dp[i][j - 1] + 1, dp[i - 1][j] + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,4 +86,4 @@ public class ParseVisitorHelper {
|
||||
public double getSimilarity(String word1, String word2) {
|
||||
return 1 - (double) editDistance(word1, word2) / Math.max(word2.length(), word1.length());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import net.sf.jsqlparser.expression.Alias;
|
||||
import net.sf.jsqlparser.expression.BinaryExpression;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
@@ -11,6 +9,9 @@ import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
||||
import net.sf.jsqlparser.schema.Column;
|
||||
import net.sf.jsqlparser.statement.select.SelectItem;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public class QueryExpressionReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
|
||||
private Map<String, String> fieldExprMap;
|
||||
@@ -40,7 +41,6 @@ public class QueryExpressionReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
}
|
||||
expr.getLeftExpression().accept(this);
|
||||
expr.getRightExpression().accept(this);
|
||||
|
||||
}
|
||||
|
||||
public void visit(SelectItem selectExpressionItem) {
|
||||
@@ -55,7 +55,6 @@ public class QueryExpressionReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
columnName = column.getColumnName();
|
||||
toReplace = getReplaceExpr(leftFunc, fieldExprMap);
|
||||
}
|
||||
|
||||
}
|
||||
if (expression instanceof Column) {
|
||||
Column column = (Column) expression;
|
||||
@@ -103,12 +102,13 @@ public class QueryExpressionReplaceVisitor extends ExpressionVisitorAdapter {
|
||||
}
|
||||
|
||||
public static String getReplaceExpr(Column column, Map<String, String> fieldExprMap) {
|
||||
return fieldExprMap.containsKey(column.getColumnName()) ? fieldExprMap.get(column.getColumnName()) : "";
|
||||
return fieldExprMap.containsKey(column.getColumnName())
|
||||
? fieldExprMap.get(column.getColumnName())
|
||||
: "";
|
||||
}
|
||||
|
||||
public static String getReplaceExpr(Function function, Map<String, String> fieldExprMap) {
|
||||
Column column = (Column) function.getParameters().getExpressions().get(0);
|
||||
return getReplaceExpr(column, fieldExprMap);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,5 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
|
||||
import com.tencent.supersonic.common.pojo.enums.TimeDimensionEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
@@ -22,18 +15,23 @@ import net.sf.jsqlparser.schema.Column;
|
||||
import net.sf.jsqlparser.schema.Table;
|
||||
import net.sf.jsqlparser.statement.select.GroupByElement;
|
||||
import net.sf.jsqlparser.statement.select.OrderByElement;
|
||||
import net.sf.jsqlparser.statement.select.ParenthesedSelect;
|
||||
import net.sf.jsqlparser.statement.select.PlainSelect;
|
||||
import net.sf.jsqlparser.statement.select.Select;
|
||||
import net.sf.jsqlparser.statement.select.SelectItem;
|
||||
import net.sf.jsqlparser.statement.select.SelectVisitorAdapter;
|
||||
import net.sf.jsqlparser.statement.select.SetOperationList;
|
||||
import net.sf.jsqlparser.statement.select.ParenthesedSelect;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* Sql Parser add Helper
|
||||
*/
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/** Sql Parser add Helper */
|
||||
@Slf4j
|
||||
public class SqlAddHelper {
|
||||
|
||||
@@ -45,21 +43,32 @@ public class SqlAddHelper {
|
||||
}
|
||||
if (selectStatement instanceof PlainSelect) {
|
||||
PlainSelect plainSelect = (PlainSelect) selectStatement;
|
||||
fields.stream().filter(Objects::nonNull).forEach(field -> {
|
||||
SelectItem<Column> selectExpressionItem = new SelectItem(new Column(field));
|
||||
plainSelect.addSelectItems(selectExpressionItem);
|
||||
});
|
||||
fields.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.forEach(
|
||||
field -> {
|
||||
SelectItem<Column> selectExpressionItem =
|
||||
new SelectItem(new Column(field));
|
||||
plainSelect.addSelectItems(selectExpressionItem);
|
||||
});
|
||||
|
||||
} else if (selectStatement instanceof SetOperationList) {
|
||||
SetOperationList setOperationList = (SetOperationList) selectStatement;
|
||||
if (!CollectionUtils.isEmpty(setOperationList.getSelects())) {
|
||||
setOperationList.getSelects().forEach(subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
fields.stream().forEach(field -> {
|
||||
SelectItem<Column> selectExpressionItem = new SelectItem(new Column(field));
|
||||
subPlainSelect.addSelectItems(selectExpressionItem);
|
||||
});
|
||||
});
|
||||
setOperationList
|
||||
.getSelects()
|
||||
.forEach(
|
||||
subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
fields.stream()
|
||||
.forEach(
|
||||
field -> {
|
||||
SelectItem<Column> selectExpressionItem =
|
||||
new SelectItem(new Column(field));
|
||||
subPlainSelect.addSelectItems(
|
||||
selectExpressionItem);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
return selectStatement.toString();
|
||||
@@ -76,12 +85,16 @@ public class SqlAddHelper {
|
||||
PlainSelect plainSelect = (PlainSelect) selectStatement.getPlainSelect();
|
||||
plainSelectList.add(plainSelect);
|
||||
} else if (selectStatement instanceof SetOperationList) {
|
||||
SetOperationList setOperationList = (SetOperationList) selectStatement.getSetOperationList();
|
||||
SetOperationList setOperationList =
|
||||
(SetOperationList) selectStatement.getSetOperationList();
|
||||
if (!CollectionUtils.isEmpty(setOperationList.getSelects())) {
|
||||
setOperationList.getSelects().forEach(subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
plainSelectList.add(subPlainSelect);
|
||||
});
|
||||
setOperationList
|
||||
.getSelects()
|
||||
.forEach(
|
||||
subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
plainSelectList.add(subPlainSelect);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,15 +238,18 @@ public class SqlAddHelper {
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
selectStatement.accept(new SelectVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(PlainSelect plainSelect) {
|
||||
addAggregateToSelectItems(plainSelect.getSelectItems(), fieldNameToAggregate);
|
||||
addAggregateToOrderByItems(plainSelect.getOrderByElements(), fieldNameToAggregate);
|
||||
addAggregateToGroupByItems(plainSelect.getGroupBy(), fieldNameToAggregate);
|
||||
addAggregateToWhereItems(plainSelect.getWhere(), fieldNameToAggregate);
|
||||
}
|
||||
});
|
||||
selectStatement.accept(
|
||||
new SelectVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(PlainSelect plainSelect) {
|
||||
addAggregateToSelectItems(
|
||||
plainSelect.getSelectItems(), fieldNameToAggregate);
|
||||
addAggregateToOrderByItems(
|
||||
plainSelect.getOrderByElements(), fieldNameToAggregate);
|
||||
addAggregateToGroupByItems(plainSelect.getGroupBy(), fieldNameToAggregate);
|
||||
addAggregateToWhereItems(plainSelect.getWhere(), fieldNameToAggregate);
|
||||
}
|
||||
});
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
@@ -260,11 +276,12 @@ public class SqlAddHelper {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
private static void addAggregateToSelectItems(List<SelectItem<?>> selectItems,
|
||||
Map<String, String> fieldNameToAggregate) {
|
||||
private static void addAggregateToSelectItems(
|
||||
List<SelectItem<?>> selectItems, Map<String, String> fieldNameToAggregate) {
|
||||
for (SelectItem selectItem : selectItems) {
|
||||
Expression expression = selectItem.getExpression();
|
||||
Function function = SqlSelectFunctionHelper.getFunction(expression, fieldNameToAggregate);
|
||||
Function function =
|
||||
SqlSelectFunctionHelper.getFunction(expression, fieldNameToAggregate);
|
||||
if (function == null) {
|
||||
continue;
|
||||
}
|
||||
@@ -272,14 +289,15 @@ public class SqlAddHelper {
|
||||
}
|
||||
}
|
||||
|
||||
private static void addAggregateToOrderByItems(List<OrderByElement> orderByElements,
|
||||
Map<String, String> fieldNameToAggregate) {
|
||||
private static void addAggregateToOrderByItems(
|
||||
List<OrderByElement> orderByElements, Map<String, String> fieldNameToAggregate) {
|
||||
if (orderByElements == null) {
|
||||
return;
|
||||
}
|
||||
for (OrderByElement orderByElement : orderByElements) {
|
||||
Expression expression = orderByElement.getExpression();
|
||||
Function function = SqlSelectFunctionHelper.getFunction(expression, fieldNameToAggregate);
|
||||
Function function =
|
||||
SqlSelectFunctionHelper.getFunction(expression, fieldNameToAggregate);
|
||||
if (function == null) {
|
||||
continue;
|
||||
}
|
||||
@@ -287,14 +305,15 @@ public class SqlAddHelper {
|
||||
}
|
||||
}
|
||||
|
||||
private static void addAggregateToGroupByItems(GroupByElement groupByElement,
|
||||
Map<String, String> fieldNameToAggregate) {
|
||||
private static void addAggregateToGroupByItems(
|
||||
GroupByElement groupByElement, Map<String, String> fieldNameToAggregate) {
|
||||
if (groupByElement == null) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < groupByElement.getGroupByExpressionList().size(); i++) {
|
||||
Expression expression = (Expression) groupByElement.getGroupByExpressionList().get(i);
|
||||
Function function = SqlSelectFunctionHelper.getFunction(expression, fieldNameToAggregate);
|
||||
Function function =
|
||||
SqlSelectFunctionHelper.getFunction(expression, fieldNameToAggregate);
|
||||
if (function == null) {
|
||||
continue;
|
||||
}
|
||||
@@ -302,15 +321,16 @@ public class SqlAddHelper {
|
||||
}
|
||||
}
|
||||
|
||||
private static void addAggregateToWhereItems(Expression whereExpression, Map<String, String> fieldNameToAggregate) {
|
||||
private static void addAggregateToWhereItems(
|
||||
Expression whereExpression, Map<String, String> fieldNameToAggregate) {
|
||||
if (whereExpression == null) {
|
||||
return;
|
||||
}
|
||||
modifyWhereExpression(whereExpression, fieldNameToAggregate);
|
||||
}
|
||||
|
||||
private static void modifyWhereExpression(Expression whereExpression,
|
||||
Map<String, String> fieldNameToAggregate) {
|
||||
private static void modifyWhereExpression(
|
||||
Expression whereExpression, Map<String, String> fieldNameToAggregate) {
|
||||
if (SqlSelectHelper.isLogicExpression(whereExpression)) {
|
||||
if (whereExpression instanceof AndExpression) {
|
||||
AndExpression andExpression = (AndExpression) whereExpression;
|
||||
@@ -327,29 +347,36 @@ public class SqlAddHelper {
|
||||
modifyWhereExpression(rightExpression, fieldNameToAggregate);
|
||||
}
|
||||
} else if (whereExpression instanceof Parenthesis) {
|
||||
modifyWhereExpression(((Parenthesis) whereExpression).getExpression(), fieldNameToAggregate);
|
||||
modifyWhereExpression(
|
||||
((Parenthesis) whereExpression).getExpression(), fieldNameToAggregate);
|
||||
} else {
|
||||
setAggToFunction(whereExpression, fieldNameToAggregate);
|
||||
}
|
||||
}
|
||||
|
||||
private static void setAggToFunction(Expression expression, Map<String, String> fieldNameToAggregate) {
|
||||
private static void setAggToFunction(
|
||||
Expression expression, Map<String, String> fieldNameToAggregate) {
|
||||
if (!(expression instanceof ComparisonOperator)) {
|
||||
return;
|
||||
}
|
||||
ComparisonOperator comparisonOperator = (ComparisonOperator) expression;
|
||||
if (comparisonOperator.getRightExpression() instanceof Column) {
|
||||
String columnName = ((Column) (comparisonOperator).getRightExpression()).getColumnName();
|
||||
Function function = SqlSelectFunctionHelper.getFunction(comparisonOperator.getRightExpression(),
|
||||
fieldNameToAggregate.get(columnName));
|
||||
String columnName =
|
||||
((Column) (comparisonOperator).getRightExpression()).getColumnName();
|
||||
Function function =
|
||||
SqlSelectFunctionHelper.getFunction(
|
||||
comparisonOperator.getRightExpression(),
|
||||
fieldNameToAggregate.get(columnName));
|
||||
if (Objects.nonNull(function)) {
|
||||
comparisonOperator.setRightExpression(function);
|
||||
}
|
||||
}
|
||||
if (comparisonOperator.getLeftExpression() instanceof Column) {
|
||||
String columnName = ((Column) (comparisonOperator).getLeftExpression()).getColumnName();
|
||||
Function function = SqlSelectFunctionHelper.getFunction(comparisonOperator.getLeftExpression(),
|
||||
fieldNameToAggregate.get(columnName));
|
||||
Function function =
|
||||
SqlSelectFunctionHelper.getFunction(
|
||||
comparisonOperator.getLeftExpression(),
|
||||
fieldNameToAggregate.get(columnName));
|
||||
if (Objects.nonNull(function)) {
|
||||
comparisonOperator.setLeftExpression(function);
|
||||
}
|
||||
@@ -364,7 +391,7 @@ public class SqlAddHelper {
|
||||
}
|
||||
|
||||
PlainSelect plainSelect = (PlainSelect) selectStatement;
|
||||
//replace metric to 1 and 1 and add having metric
|
||||
// replace metric to 1 and 1 and add having metric
|
||||
Expression where = plainSelect.getWhere();
|
||||
FiledFilterReplaceVisitor visitor = new FiledFilterReplaceVisitor(fieldNames);
|
||||
if (Objects.nonNull(where)) {
|
||||
@@ -423,4 +450,3 @@ public class SqlAddHelper {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
import com.tencent.supersonic.common.pojo.enums.TimeDimensionEnum;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.statement.select.PlainSelect;
|
||||
|
||||
/**
|
||||
* Date field parsing helper class
|
||||
*/
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Date field parsing helper class */
|
||||
@Slf4j
|
||||
public class SqlDateSelectHelper {
|
||||
|
||||
@@ -32,4 +30,3 @@ public class SqlDateSelectHelper {
|
||||
return dateVisitor.getDateBoundInfo();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,9 +36,7 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Sql Parser remove Helper
|
||||
*/
|
||||
/** Sql Parser remove Helper */
|
||||
@Slf4j
|
||||
public class SqlRemoveHelper {
|
||||
|
||||
@@ -67,14 +65,15 @@ public class SqlRemoveHelper {
|
||||
}
|
||||
List<SelectItem<?>> selectItems = ((PlainSelect) selectStatement).getSelectItems();
|
||||
Set<String> fields = new HashSet<>();
|
||||
selectItems.removeIf(selectItem -> {
|
||||
String field = selectItem.getExpression().toString();
|
||||
if (fields.contains(field)) {
|
||||
return true;
|
||||
}
|
||||
fields.add(field);
|
||||
return false;
|
||||
});
|
||||
selectItems.removeIf(
|
||||
selectItem -> {
|
||||
String field = selectItem.getExpression().toString();
|
||||
if (fields.contains(field)) {
|
||||
return true;
|
||||
}
|
||||
fields.add(field);
|
||||
return false;
|
||||
});
|
||||
((PlainSelect) selectStatement).setSelectItems(selectItems);
|
||||
return selectStatement.toString();
|
||||
}
|
||||
@@ -84,16 +83,18 @@ public class SqlRemoveHelper {
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
selectStatement.accept(new SelectVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(PlainSelect plainSelect) {
|
||||
removeWhereCondition(plainSelect.getWhere(), removeFieldNames);
|
||||
}
|
||||
});
|
||||
selectStatement.accept(
|
||||
new SelectVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(PlainSelect plainSelect) {
|
||||
removeWhereCondition(plainSelect.getWhere(), removeFieldNames);
|
||||
}
|
||||
});
|
||||
return removeNumberFilter(selectStatement.toString());
|
||||
}
|
||||
|
||||
private static void removeWhereCondition(Expression whereExpression, Set<String> removeFieldNames) {
|
||||
private static void removeWhereCondition(
|
||||
Expression whereExpression, Set<String> removeFieldNames) {
|
||||
if (whereExpression == null) {
|
||||
return;
|
||||
}
|
||||
@@ -108,15 +109,18 @@ public class SqlRemoveHelper {
|
||||
Expression where = ((PlainSelect) selectStatement).getWhere();
|
||||
Expression having = ((PlainSelect) selectStatement).getHaving();
|
||||
try {
|
||||
((PlainSelect) selectStatement).setWhere(filteredExpression(where, SqlEditEnum.NUMBER_FILTER));
|
||||
((PlainSelect) selectStatement).setHaving(filteredExpression(having, SqlEditEnum.NUMBER_FILTER));
|
||||
((PlainSelect) selectStatement)
|
||||
.setWhere(filteredExpression(where, SqlEditEnum.NUMBER_FILTER));
|
||||
((PlainSelect) selectStatement)
|
||||
.setHaving(filteredExpression(having, SqlEditEnum.NUMBER_FILTER));
|
||||
} catch (Exception e) {
|
||||
log.info("replaceFunction has an exception:{}", e.toString());
|
||||
}
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
private static void removeWhereExpression(Expression whereExpression, Set<String> removeFieldNames) {
|
||||
private static void removeWhereExpression(
|
||||
Expression whereExpression, Set<String> removeFieldNames) {
|
||||
if (SqlSelectHelper.isLogicExpression(whereExpression)) {
|
||||
BinaryExpression binaryExpression = (BinaryExpression) whereExpression;
|
||||
Expression leftExpression = binaryExpression.getLeftExpression();
|
||||
@@ -125,7 +129,8 @@ public class SqlRemoveHelper {
|
||||
removeWhereExpression(leftExpression, removeFieldNames);
|
||||
removeWhereExpression(rightExpression, removeFieldNames);
|
||||
} else if (whereExpression instanceof Parenthesis) {
|
||||
removeWhereExpression(((Parenthesis) whereExpression).getExpression(), removeFieldNames);
|
||||
removeWhereExpression(
|
||||
((Parenthesis) whereExpression).getExpression(), removeFieldNames);
|
||||
} else {
|
||||
removeExpressionWithConstant(whereExpression, removeFieldNames);
|
||||
}
|
||||
@@ -145,22 +150,25 @@ public class SqlRemoveHelper {
|
||||
return constant;
|
||||
}
|
||||
|
||||
private static void removeExpressionWithConstant(Expression expression, Set<String> removeFieldNames) {
|
||||
private static void removeExpressionWithConstant(
|
||||
Expression expression, Set<String> removeFieldNames) {
|
||||
if (expression instanceof EqualsTo
|
||||
|| expression instanceof GreaterThanEquals
|
||||
|| expression instanceof GreaterThan
|
||||
|| expression instanceof MinorThanEquals
|
||||
|| expression instanceof MinorThan) {
|
||||
ComparisonOperator comparisonOperator = (ComparisonOperator) expression;
|
||||
String columnName = SqlSelectHelper.getColumnName(comparisonOperator.getLeftExpression(),
|
||||
comparisonOperator.getRightExpression());
|
||||
String columnName =
|
||||
SqlSelectHelper.getColumnName(
|
||||
comparisonOperator.getLeftExpression(),
|
||||
comparisonOperator.getRightExpression());
|
||||
if (!removeFieldNames.contains(columnName)) {
|
||||
return;
|
||||
}
|
||||
String constant = getConstant(expression);
|
||||
try {
|
||||
ComparisonOperator constantExpression = (ComparisonOperator) CCJSqlParserUtil.parseCondExpression(
|
||||
constant);
|
||||
ComparisonOperator constantExpression =
|
||||
(ComparisonOperator) CCJSqlParserUtil.parseCondExpression(constant);
|
||||
comparisonOperator.setLeftExpression(constantExpression.getLeftExpression());
|
||||
comparisonOperator.setRightExpression(constantExpression.getRightExpression());
|
||||
comparisonOperator.setASTNode(constantExpression.getASTNode());
|
||||
@@ -170,14 +178,16 @@ public class SqlRemoveHelper {
|
||||
}
|
||||
if (expression instanceof InExpression) {
|
||||
InExpression inExpression = (InExpression) expression;
|
||||
String columnName = SqlSelectHelper.getColumnName(inExpression.getLeftExpression(),
|
||||
inExpression.getRightExpression());
|
||||
String columnName =
|
||||
SqlSelectHelper.getColumnName(
|
||||
inExpression.getLeftExpression(), inExpression.getRightExpression());
|
||||
if (!removeFieldNames.contains(columnName)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
InExpression constantExpression = (InExpression) CCJSqlParserUtil.parseCondExpression(
|
||||
JsqlConstants.IN_CONSTANT);
|
||||
InExpression constantExpression =
|
||||
(InExpression)
|
||||
CCJSqlParserUtil.parseCondExpression(JsqlConstants.IN_CONSTANT);
|
||||
inExpression.setLeftExpression(constantExpression.getLeftExpression());
|
||||
inExpression.setRightExpression(constantExpression.getRightExpression());
|
||||
inExpression.setASTNode(constantExpression.getASTNode());
|
||||
@@ -187,14 +197,17 @@ public class SqlRemoveHelper {
|
||||
}
|
||||
if (expression instanceof LikeExpression) {
|
||||
LikeExpression likeExpression = (LikeExpression) expression;
|
||||
String columnName = SqlSelectHelper.getColumnName(likeExpression.getLeftExpression(),
|
||||
likeExpression.getRightExpression());
|
||||
String columnName =
|
||||
SqlSelectHelper.getColumnName(
|
||||
likeExpression.getLeftExpression(),
|
||||
likeExpression.getRightExpression());
|
||||
if (!removeFieldNames.contains(columnName)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
LikeExpression constantExpression = (LikeExpression) CCJSqlParserUtil.parseCondExpression(
|
||||
JsqlConstants.LIKE_CONSTANT);
|
||||
LikeExpression constantExpression =
|
||||
(LikeExpression)
|
||||
CCJSqlParserUtil.parseCondExpression(JsqlConstants.LIKE_CONSTANT);
|
||||
likeExpression.setLeftExpression(constantExpression.getLeftExpression());
|
||||
likeExpression.setRightExpression(constantExpression.getRightExpression());
|
||||
} catch (JSQLParserException e) {
|
||||
@@ -208,12 +221,13 @@ public class SqlRemoveHelper {
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
selectStatement.accept(new SelectVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(PlainSelect plainSelect) {
|
||||
removeWhereCondition(plainSelect.getHaving(), removeFieldNames);
|
||||
}
|
||||
});
|
||||
selectStatement.accept(
|
||||
new SelectVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(PlainSelect plainSelect) {
|
||||
removeWhereCondition(plainSelect.getHaving(), removeFieldNames);
|
||||
}
|
||||
});
|
||||
return removeNumberFilter(selectStatement.toString());
|
||||
}
|
||||
|
||||
@@ -227,13 +241,16 @@ public class SqlRemoveHelper {
|
||||
return sql;
|
||||
}
|
||||
ExpressionList groupByExpressionList = groupByElement.getGroupByExpressionList();
|
||||
groupByExpressionList.getExpressions().removeIf(expression -> {
|
||||
if (expression instanceof Column) {
|
||||
Column column = (Column) expression;
|
||||
return fields.contains(column.getColumnName());
|
||||
}
|
||||
return false;
|
||||
});
|
||||
groupByExpressionList
|
||||
.getExpressions()
|
||||
.removeIf(
|
||||
expression -> {
|
||||
if (expression instanceof Column) {
|
||||
Column column = (Column) expression;
|
||||
return fields.contains(column.getColumnName());
|
||||
}
|
||||
return false;
|
||||
});
|
||||
if (CollectionUtils.isEmpty(groupByExpressionList.getExpressions())) {
|
||||
((PlainSelect) selectStatement).setGroupByElement(null);
|
||||
}
|
||||
@@ -249,14 +266,15 @@ public class SqlRemoveHelper {
|
||||
Iterator<SelectItem<?>> iterator = selectItems.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
SelectItem selectItem = iterator.next();
|
||||
selectItem.accept(new SelectItemVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(SelectItem item) {
|
||||
if (fields.contains(item.getExpression().toString())) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
});
|
||||
selectItem.accept(
|
||||
new SelectItemVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(SelectItem item) {
|
||||
if (fields.contains(item.getExpression().toString())) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if (selectItems.isEmpty()) {
|
||||
selectItems.add(new SelectItem(new AllColumns()));
|
||||
@@ -264,15 +282,18 @@ public class SqlRemoveHelper {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static Expression filteredExpression(Expression where, SqlEditEnum sqlEditEnum) throws Exception {
|
||||
public static Expression filteredExpression(Expression where, SqlEditEnum sqlEditEnum)
|
||||
throws Exception {
|
||||
if (Objects.isNull(where)) {
|
||||
return null;
|
||||
}
|
||||
if (where instanceof Parenthesis) {
|
||||
Expression expression = filteredExpression(((Parenthesis) where).getExpression(), sqlEditEnum);
|
||||
Expression expression =
|
||||
filteredExpression(((Parenthesis) where).getExpression(), sqlEditEnum);
|
||||
if (expression != null) {
|
||||
try {
|
||||
Expression parseExpression = CCJSqlParserUtil.parseExpression("(" + expression + ")");
|
||||
Expression parseExpression =
|
||||
CCJSqlParserUtil.parseExpression("(" + expression + ")");
|
||||
return parseExpression;
|
||||
} catch (JSQLParserException jsqlParserException) {
|
||||
log.info("jsqlParser has an exception:{}", jsqlParserException.toString());
|
||||
@@ -294,8 +315,10 @@ public class SqlRemoveHelper {
|
||||
|
||||
private static <T extends BinaryExpression> Expression filteredLogicExpression(
|
||||
T binaryExpression, SqlEditEnum sqlEditEnum) throws Exception {
|
||||
Expression leftExpression = filteredExpression(binaryExpression.getLeftExpression(), sqlEditEnum);
|
||||
Expression rightExpression = filteredExpression(binaryExpression.getRightExpression(), sqlEditEnum);
|
||||
Expression leftExpression =
|
||||
filteredExpression(binaryExpression.getLeftExpression(), sqlEditEnum);
|
||||
Expression rightExpression =
|
||||
filteredExpression(binaryExpression.getRightExpression(), sqlEditEnum);
|
||||
if (leftExpression != null && rightExpression != null) {
|
||||
binaryExpression.setLeftExpression(leftExpression);
|
||||
binaryExpression.setRightExpression(rightExpression);
|
||||
@@ -309,13 +332,17 @@ public class SqlRemoveHelper {
|
||||
}
|
||||
}
|
||||
|
||||
private static Expression dealComparisonOperatorFilter(Expression expression, SqlEditEnum sqlEditEnum) {
|
||||
private static Expression dealComparisonOperatorFilter(
|
||||
Expression expression, SqlEditEnum sqlEditEnum) {
|
||||
if (Objects.isNull(expression)) {
|
||||
return null;
|
||||
}
|
||||
if (expression instanceof GreaterThanEquals || expression instanceof GreaterThan
|
||||
|| expression instanceof MinorThan || expression instanceof MinorThanEquals
|
||||
|| expression instanceof EqualsTo || expression instanceof NotEqualsTo) {
|
||||
if (expression instanceof GreaterThanEquals
|
||||
|| expression instanceof GreaterThan
|
||||
|| expression instanceof MinorThan
|
||||
|| expression instanceof MinorThanEquals
|
||||
|| expression instanceof EqualsTo
|
||||
|| expression instanceof NotEqualsTo) {
|
||||
return removeSingleFilter((ComparisonOperator) expression, sqlEditEnum);
|
||||
} else if (expression instanceof InExpression) {
|
||||
InExpression inExpression = (InExpression) expression;
|
||||
@@ -335,7 +362,8 @@ public class SqlRemoveHelper {
|
||||
return recursionBase(leftExpression, comparisonExpression, sqlEditEnum);
|
||||
}
|
||||
|
||||
private static Expression recursionBase(Expression leftExpression, Expression expression, SqlEditEnum sqlEditEnum) {
|
||||
private static Expression recursionBase(
|
||||
Expression leftExpression, Expression expression, SqlEditEnum sqlEditEnum) {
|
||||
if (sqlEditEnum.equals(SqlEditEnum.NUMBER_FILTER)) {
|
||||
return distinguishNumberFilter(leftExpression, expression);
|
||||
}
|
||||
@@ -345,7 +373,8 @@ public class SqlRemoveHelper {
|
||||
return expression;
|
||||
}
|
||||
|
||||
private static Expression distinguishNumberFilter(Expression leftExpression, Expression expression) {
|
||||
private static Expression distinguishNumberFilter(
|
||||
Expression leftExpression, Expression expression) {
|
||||
if (leftExpression instanceof LongValue) {
|
||||
return null;
|
||||
} else {
|
||||
@@ -356,6 +385,4 @@ public class SqlRemoveHelper {
|
||||
private static boolean isInvalidSelect(Select selectStatement) {
|
||||
return Objects.isNull(selectStatement) || !(selectStatement instanceof PlainSelect);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -44,9 +44,7 @@ import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
/**
|
||||
* Sql Parser replace Helper
|
||||
*/
|
||||
/** Sql Parser replace Helper */
|
||||
@Slf4j
|
||||
public class SqlReplaceHelper {
|
||||
|
||||
@@ -55,73 +53,98 @@ public class SqlReplaceHelper {
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
((PlainSelect) selectStatement).getSelectItems().stream().forEach(o -> {
|
||||
SelectItem selectExpressionItem = (SelectItem) o;
|
||||
String alias = "";
|
||||
if (selectExpressionItem.getExpression() instanceof Function) {
|
||||
Function function = (Function) selectExpressionItem.getExpression();
|
||||
Column column = (Column) function.getParameters().getExpressions().get(0);
|
||||
if (fieldNameMap.containsKey(column.getColumnName())) {
|
||||
String value = fieldNameMap.get(column.getColumnName());
|
||||
alias = value;
|
||||
function.withParameters(new Column(value));
|
||||
}
|
||||
}
|
||||
if (selectExpressionItem.getExpression() instanceof Column) {
|
||||
Column column = (Column) selectExpressionItem.getExpression();
|
||||
String columnName = column.getColumnName();
|
||||
if (fieldNameMap.containsKey(columnName)) {
|
||||
String value = fieldNameMap.get(columnName);
|
||||
alias = value;
|
||||
if (StringUtils.isNotBlank(value)) {
|
||||
selectExpressionItem.setExpression(new Column(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Objects.nonNull(selectExpressionItem.getAlias()) && StringUtils.isNotBlank(alias)) {
|
||||
selectExpressionItem.getAlias().setName(alias);
|
||||
}
|
||||
});
|
||||
((PlainSelect) selectStatement)
|
||||
.getSelectItems().stream()
|
||||
.forEach(
|
||||
o -> {
|
||||
SelectItem selectExpressionItem = (SelectItem) o;
|
||||
String alias = "";
|
||||
if (selectExpressionItem.getExpression() instanceof Function) {
|
||||
Function function =
|
||||
(Function) selectExpressionItem.getExpression();
|
||||
Column column =
|
||||
(Column)
|
||||
function.getParameters()
|
||||
.getExpressions()
|
||||
.get(0);
|
||||
if (fieldNameMap.containsKey(column.getColumnName())) {
|
||||
String value = fieldNameMap.get(column.getColumnName());
|
||||
alias = value;
|
||||
function.withParameters(new Column(value));
|
||||
}
|
||||
}
|
||||
if (selectExpressionItem.getExpression() instanceof Column) {
|
||||
Column column =
|
||||
(Column) selectExpressionItem.getExpression();
|
||||
String columnName = column.getColumnName();
|
||||
if (fieldNameMap.containsKey(columnName)) {
|
||||
String value = fieldNameMap.get(columnName);
|
||||
alias = value;
|
||||
if (StringUtils.isNotBlank(value)) {
|
||||
selectExpressionItem.setExpression(
|
||||
new Column(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Objects.nonNull(selectExpressionItem.getAlias())
|
||||
&& StringUtils.isNotBlank(alias)) {
|
||||
selectExpressionItem.getAlias().setName(alias);
|
||||
}
|
||||
});
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static String replaceAggFields(String sql, Map<String, Pair<String, String>> fieldNameToAggMap) {
|
||||
public static String replaceAggFields(
|
||||
String sql, Map<String, Pair<String, String>> fieldNameToAggMap) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
}
|
||||
((PlainSelect) selectStatement).getSelectItems().stream().forEach(o -> {
|
||||
SelectItem selectExpressionItem = (SelectItem) 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);
|
||||
}
|
||||
function.withParameters(new Column(field));
|
||||
if (Objects.nonNull(selectExpressionItem.getAlias()) && StringUtils.isNotBlank(field)) {
|
||||
selectExpressionItem.getAlias().setName(field);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
((PlainSelect) selectStatement)
|
||||
.getSelectItems().stream()
|
||||
.forEach(
|
||||
o -> {
|
||||
SelectItem selectExpressionItem = (SelectItem) 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);
|
||||
}
|
||||
function.withParameters(new Column(field));
|
||||
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) {
|
||||
public static String replaceValue(
|
||||
String sql, Map<String, Map<String, String>> filedNameToValueMap) {
|
||||
return replaceValue(sql, filedNameToValueMap, true);
|
||||
}
|
||||
|
||||
public static String replaceValue(String sql, Map<String, Map<String, String>> filedNameToValueMap,
|
||||
boolean exactReplace) {
|
||||
public static String replaceValue(
|
||||
String sql,
|
||||
Map<String, Map<String, String>> filedNameToValueMap,
|
||||
boolean exactReplace) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
@@ -129,7 +152,8 @@ public class SqlReplaceHelper {
|
||||
List<PlainSelect> plainSelects = SqlSelectHelper.getPlainSelect(selectStatement);
|
||||
for (PlainSelect plainSelect : plainSelects) {
|
||||
Expression where = plainSelect.getWhere();
|
||||
FieldValueReplaceVisitor visitor = new FieldValueReplaceVisitor(exactReplace, filedNameToValueMap);
|
||||
FieldValueReplaceVisitor visitor =
|
||||
new FieldValueReplaceVisitor(exactReplace, filedNameToValueMap);
|
||||
if (Objects.nonNull(where)) {
|
||||
where.accept(visitor);
|
||||
}
|
||||
@@ -137,7 +161,8 @@ public class SqlReplaceHelper {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static String replaceFieldNameByValue(String sql, Map<String, Set<String>> fieldValueToFieldNames) {
|
||||
public static String replaceFieldNameByValue(
|
||||
String sql, Map<String, Set<String>> fieldValueToFieldNames) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
@@ -168,11 +193,14 @@ public class SqlReplaceHelper {
|
||||
} else if (select instanceof SetOperationList) {
|
||||
SetOperationList setOperationList = (SetOperationList) select;
|
||||
if (!CollectionUtils.isEmpty(setOperationList.getSelects())) {
|
||||
setOperationList.getSelects().forEach(subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
plainSelectList.add(subPlainSelect);
|
||||
getFromSelect(subPlainSelect.getFromItem(), plainSelectList);
|
||||
});
|
||||
setOperationList
|
||||
.getSelects()
|
||||
.forEach(
|
||||
subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
plainSelectList.add(subPlainSelect);
|
||||
getFromSelect(subPlainSelect.getFromItem(), plainSelectList);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -181,7 +209,8 @@ public class SqlReplaceHelper {
|
||||
return replaceFields(sql, fieldNameMap, false);
|
||||
}
|
||||
|
||||
public static String replaceFields(String sql, Map<String, String> fieldNameMap, boolean exactReplace) {
|
||||
public static String replaceFields(
|
||||
String sql, Map<String, String> fieldNameMap, boolean exactReplace) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
List<PlainSelect> plainSelectList = SqlSelectHelper.getWithItem(selectStatement);
|
||||
if (selectStatement instanceof PlainSelect) {
|
||||
@@ -191,11 +220,14 @@ public class SqlReplaceHelper {
|
||||
} else if (selectStatement instanceof SetOperationList) {
|
||||
SetOperationList setOperationList = (SetOperationList) selectStatement;
|
||||
if (!CollectionUtils.isEmpty(setOperationList.getSelects())) {
|
||||
setOperationList.getSelects().forEach(subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
plainSelectList.add(subPlainSelect);
|
||||
getFromSelect(subPlainSelect.getFromItem(), plainSelectList);
|
||||
});
|
||||
setOperationList
|
||||
.getSelects()
|
||||
.forEach(
|
||||
subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
plainSelectList.add(subPlainSelect);
|
||||
getFromSelect(subPlainSelect.getFromItem(), plainSelectList);
|
||||
});
|
||||
}
|
||||
List<OrderByElement> orderByElements = setOperationList.getOrderByElements();
|
||||
if (!CollectionUtils.isEmpty(orderByElements)) {
|
||||
@@ -213,16 +245,16 @@ public class SqlReplaceHelper {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
private static void replaceFieldsInPlainOneSelect(Map<String, String> fieldNameMap, boolean exactReplace,
|
||||
PlainSelect plainSelect) {
|
||||
//1. replace where fields
|
||||
private static void replaceFieldsInPlainOneSelect(
|
||||
Map<String, String> fieldNameMap, boolean exactReplace, PlainSelect plainSelect) {
|
||||
// 1. replace where fields
|
||||
Expression where = plainSelect.getWhere();
|
||||
FieldReplaceVisitor visitor = new FieldReplaceVisitor(fieldNameMap, exactReplace);
|
||||
if (Objects.nonNull(where)) {
|
||||
where.accept(visitor);
|
||||
}
|
||||
|
||||
//2. replace select fields
|
||||
// 2. replace select fields
|
||||
for (SelectItem selectItem : plainSelect.getSelectItems()) {
|
||||
selectItem.accept(visitor);
|
||||
replaceAsName(fieldNameMap, selectItem);
|
||||
@@ -237,27 +269,31 @@ public class SqlReplaceHelper {
|
||||
} else if (select instanceof SetOperationList) {
|
||||
SetOperationList setOperationList = (SetOperationList) select;
|
||||
if (!CollectionUtils.isEmpty(setOperationList.getSelects())) {
|
||||
setOperationList.getSelects().forEach(subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
replaceFieldsInPlainOneSelect(fieldNameMap, exactReplace, subPlainSelect);
|
||||
});
|
||||
setOperationList
|
||||
.getSelects()
|
||||
.forEach(
|
||||
subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
replaceFieldsInPlainOneSelect(
|
||||
fieldNameMap, exactReplace, subPlainSelect);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//3. replace oder by fields
|
||||
// 3. replace oder by fields
|
||||
List<OrderByElement> orderByElements = plainSelect.getOrderByElements();
|
||||
if (!CollectionUtils.isEmpty(orderByElements)) {
|
||||
for (OrderByElement orderByElement : orderByElements) {
|
||||
orderByElement.accept(new OrderByReplaceVisitor(fieldNameMap, exactReplace));
|
||||
}
|
||||
}
|
||||
//4. replace group by fields
|
||||
// 4. replace group by fields
|
||||
GroupByElement groupByElement = plainSelect.getGroupBy();
|
||||
if (Objects.nonNull(groupByElement)) {
|
||||
groupByElement.accept(new GroupByReplaceVisitor(fieldNameMap, exactReplace));
|
||||
}
|
||||
//5. replace having fields
|
||||
// 5. replace having fields
|
||||
Expression having = plainSelect.getHaving();
|
||||
if (Objects.nonNull(having)) {
|
||||
having.accept(visitor);
|
||||
@@ -266,9 +302,11 @@ public class SqlReplaceHelper {
|
||||
if (!CollectionUtils.isEmpty(joins)) {
|
||||
for (Join join : joins) {
|
||||
if (!CollectionUtils.isEmpty(join.getOnExpressions())) {
|
||||
join.getOnExpressions().stream().forEach(onExpression -> {
|
||||
onExpression.accept(visitor);
|
||||
});
|
||||
join.getOnExpressions().stream()
|
||||
.forEach(
|
||||
onExpression -> {
|
||||
onExpression.accept(visitor);
|
||||
});
|
||||
}
|
||||
if (!(join.getRightItem() instanceof ParenthesedSelect)) {
|
||||
continue;
|
||||
@@ -276,7 +314,8 @@ public class SqlReplaceHelper {
|
||||
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) join.getRightItem();
|
||||
List<PlainSelect> plainSelectList = new ArrayList<>();
|
||||
plainSelectList.add(parenthesedSelect.getPlainSelect());
|
||||
List<PlainSelect> subPlainSelects = SqlSelectHelper.getPlainSelects(plainSelectList);
|
||||
List<PlainSelect> subPlainSelects =
|
||||
SqlSelectHelper.getPlainSelects(plainSelectList);
|
||||
for (PlainSelect subPlainSelect : subPlainSelects) {
|
||||
replaceFieldsInPlainOneSelect(fieldNameMap, exactReplace, subPlainSelect);
|
||||
}
|
||||
@@ -295,15 +334,14 @@ public class SqlReplaceHelper {
|
||||
if (StringUtils.isNotBlank(replaceFieldName)) {
|
||||
alias.setName(replaceFieldName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static String replaceFunction(String sql, Map<String, String> functionMap) {
|
||||
return replaceFunction(sql, functionMap, null);
|
||||
}
|
||||
|
||||
public static String replaceFunction(String sql, Map<String, String> functionMap,
|
||||
Map<String, UnaryOperator> functionCall) {
|
||||
public static String replaceFunction(
|
||||
String sql, Map<String, String> functionMap, Map<String, UnaryOperator> functionCall) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
@@ -317,19 +355,23 @@ public class SqlReplaceHelper {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
private static void replaceFunction(Map<String, String> functionMap, Map<String, UnaryOperator> functionCall,
|
||||
PlainSelect selectBody) {
|
||||
private static void replaceFunction(
|
||||
Map<String, String> functionMap,
|
||||
Map<String, UnaryOperator> functionCall,
|
||||
PlainSelect selectBody) {
|
||||
PlainSelect plainSelect = selectBody;
|
||||
//1. replace where dataDiff function
|
||||
// 1. replace where dataDiff function
|
||||
Expression where = plainSelect.getWhere();
|
||||
|
||||
FunctionNameReplaceVisitor visitor = new FunctionNameReplaceVisitor(functionMap, functionCall);
|
||||
FunctionNameReplaceVisitor visitor =
|
||||
new FunctionNameReplaceVisitor(functionMap, functionCall);
|
||||
if (Objects.nonNull(where)) {
|
||||
where.accept(visitor);
|
||||
}
|
||||
GroupByElement groupBy = plainSelect.getGroupBy();
|
||||
if (Objects.nonNull(groupBy)) {
|
||||
GroupByFunctionReplaceVisitor replaceVisitor = new GroupByFunctionReplaceVisitor(functionMap, functionCall);
|
||||
GroupByFunctionReplaceVisitor replaceVisitor =
|
||||
new GroupByFunctionReplaceVisitor(functionMap, functionCall);
|
||||
groupBy.accept(replaceVisitor);
|
||||
}
|
||||
|
||||
@@ -376,7 +418,8 @@ public class SqlReplaceHelper {
|
||||
}
|
||||
}
|
||||
|
||||
private static void replaceComparisonOperatorFunction(Map<String, String> functionMap, Expression expression) {
|
||||
private static void replaceComparisonOperatorFunction(
|
||||
Map<String, String> functionMap, Expression expression) {
|
||||
if (Objects.isNull(expression)) {
|
||||
return;
|
||||
}
|
||||
@@ -395,8 +438,8 @@ public class SqlReplaceHelper {
|
||||
}
|
||||
}
|
||||
|
||||
private static void replaceOrderByFunction(Map<String, String> functionMap,
|
||||
List<OrderByElement> orderByElementList) {
|
||||
private static void replaceOrderByFunction(
|
||||
Map<String, String> functionMap, List<OrderByElement> orderByElementList) {
|
||||
if (Objects.isNull(orderByElementList)) {
|
||||
return;
|
||||
}
|
||||
@@ -429,22 +472,25 @@ public class SqlReplaceHelper {
|
||||
List<PlainSelect> plainSelectList = SqlSelectHelper.getWithItem(selectStatement);
|
||||
if (!CollectionUtils.isEmpty(plainSelectList)) {
|
||||
List<String> withNameList = SqlSelectHelper.getWithName(sql);
|
||||
plainSelectList.stream().forEach(plainSelect -> {
|
||||
if (plainSelect.getFromItem() instanceof Table) {
|
||||
Table table = (Table) plainSelect.getFromItem();
|
||||
if (!withNameList.contains(table.getName())) {
|
||||
replaceSingleTable(plainSelect, tableName);
|
||||
}
|
||||
}
|
||||
if (plainSelect.getFromItem() instanceof ParenthesedSelect) {
|
||||
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) plainSelect.getFromItem();
|
||||
PlainSelect subPlainSelect = parenthesedSelect.getPlainSelect();
|
||||
Table table = (Table) subPlainSelect.getFromItem();
|
||||
if (!withNameList.contains(table.getName())) {
|
||||
replaceSingleTable(subPlainSelect, tableName);
|
||||
}
|
||||
}
|
||||
});
|
||||
plainSelectList.stream()
|
||||
.forEach(
|
||||
plainSelect -> {
|
||||
if (plainSelect.getFromItem() instanceof Table) {
|
||||
Table table = (Table) plainSelect.getFromItem();
|
||||
if (!withNameList.contains(table.getName())) {
|
||||
replaceSingleTable(plainSelect, tableName);
|
||||
}
|
||||
}
|
||||
if (plainSelect.getFromItem() instanceof ParenthesedSelect) {
|
||||
ParenthesedSelect parenthesedSelect =
|
||||
(ParenthesedSelect) plainSelect.getFromItem();
|
||||
PlainSelect subPlainSelect = parenthesedSelect.getPlainSelect();
|
||||
Table table = (Table) subPlainSelect.getFromItem();
|
||||
if (!withNameList.contains(table.getName())) {
|
||||
replaceSingleTable(subPlainSelect, tableName);
|
||||
}
|
||||
}
|
||||
});
|
||||
return selectStatement.toString();
|
||||
}
|
||||
if (selectStatement instanceof PlainSelect) {
|
||||
@@ -454,11 +500,14 @@ public class SqlReplaceHelper {
|
||||
} else if (selectStatement instanceof SetOperationList) {
|
||||
SetOperationList setOperationList = (SetOperationList) selectStatement;
|
||||
if (!CollectionUtils.isEmpty(setOperationList.getSelects())) {
|
||||
setOperationList.getSelects().forEach(subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
replaceSingleTable(subPlainSelect, tableName);
|
||||
replaceSubTable(subPlainSelect, tableName);
|
||||
});
|
||||
setOperationList
|
||||
.getSelects()
|
||||
.forEach(
|
||||
subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
replaceSingleTable(subPlainSelect, tableName);
|
||||
replaceSubTable(subPlainSelect, tableName);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -493,7 +542,9 @@ public class SqlReplaceHelper {
|
||||
new SelectVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(PlainSelect plainSelect) {
|
||||
plainSelect.getFromItem().accept(new TableNameReplaceVisitor(tableName));
|
||||
plainSelect
|
||||
.getFromItem()
|
||||
.accept(new TableNameReplaceVisitor(tableName));
|
||||
}
|
||||
});
|
||||
List<Join> joins = painSelect.getJoins();
|
||||
@@ -502,9 +553,12 @@ public class SqlReplaceHelper {
|
||||
if (join.getRightItem() instanceof ParenthesedFromItem) {
|
||||
List<PlainSelect> plainSelectList = new ArrayList<>();
|
||||
plainSelectList.add((PlainSelect) join.getRightItem());
|
||||
List<PlainSelect> subPlainSelects = SqlSelectHelper.getPlainSelects(plainSelectList);
|
||||
List<PlainSelect> subPlainSelects =
|
||||
SqlSelectHelper.getPlainSelects(plainSelectList);
|
||||
for (PlainSelect subPlainSelect : subPlainSelects) {
|
||||
subPlainSelect.getFromItem().accept(new TableNameReplaceVisitor(tableName));
|
||||
subPlainSelect
|
||||
.getFromItem()
|
||||
.accept(new TableNameReplaceVisitor(tableName));
|
||||
}
|
||||
} else if (join.getRightItem() instanceof Table) {
|
||||
Table table = (Table) join.getRightItem();
|
||||
@@ -532,7 +586,8 @@ public class SqlReplaceHelper {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static String replaceHavingValue(String sql, Map<String, Map<String, String>> filedNameToValueMap) {
|
||||
public static String replaceHavingValue(
|
||||
String sql, Map<String, Map<String, String>> filedNameToValueMap) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(sql);
|
||||
if (!(selectStatement instanceof PlainSelect)) {
|
||||
return sql;
|
||||
@@ -546,7 +601,8 @@ public class SqlReplaceHelper {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
public static Expression distinguishDateDiffFilter(Expression leftExpression, Expression expression) {
|
||||
public static Expression distinguishDateDiffFilter(
|
||||
Expression leftExpression, Expression expression) {
|
||||
if (leftExpression instanceof Function) {
|
||||
Function function = (Function) leftExpression;
|
||||
if (function.getName().equals(JsqlConstants.DATE_FUNCTION)) {
|
||||
@@ -555,27 +611,35 @@ public class SqlReplaceHelper {
|
||||
Column field = (Column) function.getParameters().getExpressions().get(1);
|
||||
String columnName = field.getColumnName();
|
||||
try {
|
||||
String startDateValue = DateFunctionHelper.getStartDateStr(comparisonOperator, leftExpressions);
|
||||
String startDateValue =
|
||||
DateFunctionHelper.getStartDateStr(comparisonOperator, leftExpressions);
|
||||
String endDateValue = DateFunctionHelper.getEndDateValue(leftExpressions);
|
||||
String dateOperator = comparisonOperator.getStringExpression();
|
||||
String endDateOperator = JsqlConstants.rightMap.get(dateOperator);
|
||||
String startDateOperator = JsqlConstants.leftMap.get(dateOperator);
|
||||
|
||||
String endDateCondExpr = columnName + endDateOperator + StringUtil.getCommaWrap(endDateValue);
|
||||
ComparisonOperator rightExpression = (ComparisonOperator)
|
||||
CCJSqlParserUtil.parseCondExpression(endDateCondExpr);
|
||||
String endDateCondExpr =
|
||||
columnName + endDateOperator + StringUtil.getCommaWrap(endDateValue);
|
||||
ComparisonOperator rightExpression =
|
||||
(ComparisonOperator)
|
||||
CCJSqlParserUtil.parseCondExpression(endDateCondExpr);
|
||||
|
||||
String startDateCondExpr = columnName + StringUtil.getSpaceWrap(startDateOperator)
|
||||
+ StringUtil.getCommaWrap(startDateValue);
|
||||
ComparisonOperator newLeftExpression = (ComparisonOperator)
|
||||
CCJSqlParserUtil.parseCondExpression(startDateCondExpr);
|
||||
String startDateCondExpr =
|
||||
columnName
|
||||
+ StringUtil.getSpaceWrap(startDateOperator)
|
||||
+ StringUtil.getCommaWrap(startDateValue);
|
||||
ComparisonOperator newLeftExpression =
|
||||
(ComparisonOperator)
|
||||
CCJSqlParserUtil.parseCondExpression(startDateCondExpr);
|
||||
|
||||
AndExpression andExpression = new AndExpression(newLeftExpression, rightExpression);
|
||||
AndExpression andExpression =
|
||||
new AndExpression(newLeftExpression, rightExpression);
|
||||
if (JsqlConstants.GREATER_THAN.equals(dateOperator)
|
||||
|| JsqlConstants.GREATER_THAN_EQUALS.equals(dateOperator)) {
|
||||
return newLeftExpression;
|
||||
} else {
|
||||
return CCJSqlParserUtil.parseCondExpression("(" + andExpression.toString() + ")");
|
||||
return CCJSqlParserUtil.parseCondExpression(
|
||||
"(" + andExpression.toString() + ")");
|
||||
}
|
||||
} catch (JSQLParserException e) {
|
||||
log.error("JSQLParserException", e);
|
||||
@@ -597,7 +661,8 @@ public class SqlReplaceHelper {
|
||||
if (Objects.nonNull(f.getAlias()) && f.getExpression() instanceof Function) {
|
||||
Function function = (Function) f.getExpression();
|
||||
String alias = f.getAlias().getName();
|
||||
if (function.getParameters().size() == 1 && function.getParameters().get(0) instanceof Column) {
|
||||
if (function.getParameters().size() == 1
|
||||
&& function.getParameters().get(0) instanceof Column) {
|
||||
Column column = (Column) function.getParameters().get(0);
|
||||
if (column.getColumnName().equalsIgnoreCase(alias)) {
|
||||
selectNames.put(alias, String.valueOf(i + 1));
|
||||
@@ -605,21 +670,30 @@ public class SqlReplaceHelper {
|
||||
}
|
||||
}
|
||||
}
|
||||
plainSelect.getOrderByElements().stream().forEach(o -> {
|
||||
if (o.getExpression() instanceof Function) {
|
||||
Function function = (Function) o.getExpression();
|
||||
if (function.getParameters().size() == 1 && function.getParameters().get(0) instanceof Column) {
|
||||
Column column = (Column) function.getParameters().get(0);
|
||||
if (selectNames.containsKey(column.getColumnName())) {
|
||||
o.setExpression(new LongValue(selectNames.get(column.getColumnName())));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
plainSelect.getOrderByElements().stream()
|
||||
.forEach(
|
||||
o -> {
|
||||
if (o.getExpression() instanceof Function) {
|
||||
Function function = (Function) o.getExpression();
|
||||
if (function.getParameters().size() == 1
|
||||
&& function.getParameters().get(0)
|
||||
instanceof Column) {
|
||||
Column column =
|
||||
(Column) function.getParameters().get(0);
|
||||
if (selectNames.containsKey(column.getColumnName())) {
|
||||
o.setExpression(
|
||||
new LongValue(
|
||||
selectNames.get(
|
||||
column.getColumnName())));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
if (plainSelect.getFromItem() instanceof ParenthesedSelect) {
|
||||
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) plainSelect.getFromItem();
|
||||
parenthesedSelect.setSelect(replaceAggAliasOrderItem(parenthesedSelect.getSelect()));
|
||||
parenthesedSelect.setSelect(
|
||||
replaceAggAliasOrderItem(parenthesedSelect.getSelect()));
|
||||
}
|
||||
return selectStatement;
|
||||
}
|
||||
@@ -637,7 +711,8 @@ public class SqlReplaceHelper {
|
||||
if (expression instanceof Column && replace.containsKey(expr)) {
|
||||
return replace.get(expr);
|
||||
}
|
||||
ExpressionReplaceVisitor expressionReplaceVisitor = new ExpressionReplaceVisitor(replace);
|
||||
ExpressionReplaceVisitor expressionReplaceVisitor =
|
||||
new ExpressionReplaceVisitor(replace);
|
||||
expression.accept(expressionReplaceVisitor);
|
||||
return expression.toString();
|
||||
}
|
||||
@@ -652,10 +727,13 @@ public class SqlReplaceHelper {
|
||||
} else if (selectStatement instanceof SetOperationList) {
|
||||
SetOperationList setOperationList = (SetOperationList) selectStatement;
|
||||
if (!CollectionUtils.isEmpty(setOperationList.getSelects())) {
|
||||
setOperationList.getSelects().forEach(subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
plainSelectList.add(subPlainSelect);
|
||||
});
|
||||
setOperationList
|
||||
.getSelects()
|
||||
.forEach(
|
||||
subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
plainSelectList.add(subPlainSelect);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
return sql;
|
||||
@@ -667,8 +745,10 @@ public class SqlReplaceHelper {
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
private static void replacePlainSelectByExpr(PlainSelect plainSelect, Map<String, String> replace) {
|
||||
QueryExpressionReplaceVisitor expressionReplaceVisitor = new QueryExpressionReplaceVisitor(replace);
|
||||
private static void replacePlainSelectByExpr(
|
||||
PlainSelect plainSelect, Map<String, String> replace) {
|
||||
QueryExpressionReplaceVisitor expressionReplaceVisitor =
|
||||
new QueryExpressionReplaceVisitor(replace);
|
||||
for (SelectItem selectItem : plainSelect.getSelectItems()) {
|
||||
selectItem.accept(expressionReplaceVisitor);
|
||||
}
|
||||
@@ -686,7 +766,8 @@ public class SqlReplaceHelper {
|
||||
if (!CollectionUtils.isEmpty(orderByElements)) {
|
||||
for (OrderByElement orderByElement : orderByElements) {
|
||||
orderByElement.setExpression(
|
||||
QueryExpressionReplaceVisitor.replace(orderByElement.getExpression(), replace));
|
||||
QueryExpressionReplaceVisitor.replace(
|
||||
orderByElement.getExpression(), replace));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -694,19 +775,22 @@ public class SqlReplaceHelper {
|
||||
public static String dealAliasToOrderBy(String querySql) {
|
||||
Select selectStatement = SqlSelectHelper.getSelect(querySql);
|
||||
List<PlainSelect> plainSelectList = new ArrayList<>();
|
||||
//List<PlainSelect> withPlainSelectList = SqlSelectHelper.getWithItem(selectStatement);
|
||||
//if (!CollectionUtils.isEmpty(withPlainSelectList)) {
|
||||
// List<PlainSelect> withPlainSelectList = SqlSelectHelper.getWithItem(selectStatement);
|
||||
// if (!CollectionUtils.isEmpty(withPlainSelectList)) {
|
||||
// plainSelectList.addAll(withPlainSelectList);
|
||||
//}
|
||||
// }
|
||||
if (selectStatement instanceof PlainSelect) {
|
||||
plainSelectList.add((PlainSelect) selectStatement);
|
||||
} else if (selectStatement instanceof SetOperationList) {
|
||||
SetOperationList setOperationList = (SetOperationList) selectStatement;
|
||||
if (!CollectionUtils.isEmpty(setOperationList.getSelects())) {
|
||||
setOperationList.getSelects().forEach(subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
plainSelectList.add(subPlainSelect);
|
||||
});
|
||||
setOperationList
|
||||
.getSelects()
|
||||
.forEach(
|
||||
subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
plainSelectList.add(subPlainSelect);
|
||||
});
|
||||
}
|
||||
}
|
||||
for (PlainSelect plainSelect : plainSelectList) {
|
||||
@@ -718,19 +802,20 @@ public class SqlReplaceHelper {
|
||||
Map<String, Expression> map = new HashMap<>();
|
||||
for (int i = 0; i < selectItemList.size(); i++) {
|
||||
if (!Objects.isNull(selectItemList.get(i).getAlias())) {
|
||||
map.put(selectItemList.get(i).getAlias().getName(), selectItemList.get(i).getExpression());
|
||||
map.put(
|
||||
selectItemList.get(i).getAlias().getName(),
|
||||
selectItemList.get(i).getExpression());
|
||||
selectItemList.get(i).setAlias(null);
|
||||
}
|
||||
}
|
||||
for (OrderByElement orderByElement : orderByElementList) {
|
||||
if (map.containsKey(orderByElement.getExpression().toString())) {
|
||||
orderByElement.setExpression(map.get(orderByElement.getExpression().toString()));
|
||||
orderByElement.setExpression(
|
||||
map.get(orderByElement.getExpression().toString()));
|
||||
}
|
||||
}
|
||||
plainSelect.setOrderByElements(orderByElementList);
|
||||
}
|
||||
return selectStatement.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,6 @@
|
||||
package com.tencent.supersonic.common.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;
|
||||
@@ -21,13 +13,21 @@ import net.sf.jsqlparser.statement.select.SelectItem;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* Sql Parser Select function Helper
|
||||
*/
|
||||
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;
|
||||
|
||||
/** Sql Parser Select function Helper */
|
||||
@Slf4j
|
||||
public class SqlSelectFunctionHelper {
|
||||
|
||||
public static List<String> aggregateFunctionName = Arrays.asList("SUM", "COUNT", "MAX", "MIN", "AVG");
|
||||
public static List<String> aggregateFunctionName =
|
||||
Arrays.asList("SUM", "COUNT", "MAX", "MIN", "AVG");
|
||||
|
||||
public static boolean hasAggregateFunction(String sql) {
|
||||
if (!CollectionUtils.isEmpty(getFunctions(sql))) {
|
||||
@@ -58,7 +58,8 @@ public class SqlSelectFunctionHelper {
|
||||
return visitor.getFunctionNames();
|
||||
}
|
||||
|
||||
public static Function getFunction(Expression expression, Map<String, String> fieldNameToAggregate) {
|
||||
public static Function getFunction(
|
||||
Expression expression, Map<String, String> fieldNameToAggregate) {
|
||||
if (!(expression instanceof Column)) {
|
||||
return null;
|
||||
}
|
||||
@@ -100,7 +101,8 @@ public class SqlSelectFunctionHelper {
|
||||
expression.accept(visitor);
|
||||
Set<String> functions = visitor.getFunctionNames();
|
||||
return functions.stream()
|
||||
.filter(t -> aggregateFunctionName.contains(t.toUpperCase())).collect(Collectors.toList());
|
||||
.filter(t -> aggregateFunctionName.contains(t.toUpperCase()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
return new ArrayList<>();
|
||||
}
|
||||
@@ -119,4 +121,3 @@ public class SqlSelectFunctionHelper {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -51,9 +51,7 @@ import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Sql Parser Select Helper
|
||||
*/
|
||||
/** Sql Parser Select Helper */
|
||||
@Slf4j
|
||||
public class SqlSelectHelper {
|
||||
|
||||
@@ -73,9 +71,12 @@ public class SqlSelectHelper {
|
||||
having.accept(new FieldAndValueAcquireVisitor(result));
|
||||
}
|
||||
}
|
||||
result = result.stream()
|
||||
.filter(fieldExpression -> StringUtils.isNotBlank(fieldExpression.getFieldName()))
|
||||
.collect(Collectors.toSet());
|
||||
result =
|
||||
result.stream()
|
||||
.filter(
|
||||
fieldExpression ->
|
||||
StringUtils.isNotBlank(fieldExpression.getFieldName()))
|
||||
.collect(Collectors.toSet());
|
||||
return new ArrayList<>(result);
|
||||
}
|
||||
|
||||
@@ -90,27 +91,31 @@ public class SqlSelectHelper {
|
||||
}
|
||||
|
||||
public static void getWhereFields(List<PlainSelect> plainSelectList, Set<String> result) {
|
||||
plainSelectList.stream().forEach(plainSelect -> {
|
||||
Expression where = plainSelect.getWhere();
|
||||
if (Objects.nonNull(where)) {
|
||||
where.accept(new FieldAcquireVisitor(result));
|
||||
}
|
||||
});
|
||||
plainSelectList.stream()
|
||||
.forEach(
|
||||
plainSelect -> {
|
||||
Expression where = plainSelect.getWhere();
|
||||
if (Objects.nonNull(where)) {
|
||||
where.accept(new FieldAcquireVisitor(result));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static List<String> gePureSelectFields(String sql) {
|
||||
List<PlainSelect> plainSelectList = getPlainSelect(sql);
|
||||
Set<String> result = new HashSet<>();
|
||||
plainSelectList.stream().forEach(plainSelect -> {
|
||||
List<SelectItem<?>> selectItems = plainSelect.getSelectItems();
|
||||
for (SelectItem selectItem : selectItems) {
|
||||
if (!(selectItem.getExpression() instanceof Column)) {
|
||||
continue;
|
||||
}
|
||||
Column column = (Column) selectItem.getExpression();
|
||||
result.add(column.getColumnName());
|
||||
}
|
||||
});
|
||||
plainSelectList.stream()
|
||||
.forEach(
|
||||
plainSelect -> {
|
||||
List<SelectItem<?>> selectItems = plainSelect.getSelectItems();
|
||||
for (SelectItem selectItem : selectItems) {
|
||||
if (!(selectItem.getExpression() instanceof Column)) {
|
||||
continue;
|
||||
}
|
||||
Column column = (Column) selectItem.getExpression();
|
||||
result.add(column.getColumnName());
|
||||
}
|
||||
});
|
||||
return new ArrayList<>(result);
|
||||
}
|
||||
|
||||
@@ -124,12 +129,14 @@ public class SqlSelectHelper {
|
||||
|
||||
public static Set<String> getSelectFields(List<PlainSelect> plainSelectList) {
|
||||
Set<String> result = new HashSet<>();
|
||||
plainSelectList.stream().forEach(plainSelect -> {
|
||||
List<SelectItem<?>> selectItems = plainSelect.getSelectItems();
|
||||
for (SelectItem selectItem : selectItems) {
|
||||
selectItem.accept(new FieldAcquireVisitor(result));
|
||||
}
|
||||
});
|
||||
plainSelectList.stream()
|
||||
.forEach(
|
||||
plainSelect -> {
|
||||
List<SelectItem<?>> selectItems = plainSelect.getSelectItems();
|
||||
for (SelectItem selectItem : selectItems) {
|
||||
selectItem.accept(new FieldAcquireVisitor(result));
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -146,10 +153,13 @@ public class SqlSelectHelper {
|
||||
} else if (selectStatement instanceof SetOperationList) {
|
||||
SetOperationList setOperationList = (SetOperationList) selectStatement;
|
||||
if (!CollectionUtils.isEmpty(setOperationList.getSelects())) {
|
||||
setOperationList.getSelects().forEach(subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
getSubPlainSelect(subPlainSelect, plainSelectList);
|
||||
});
|
||||
setOperationList
|
||||
.getSelects()
|
||||
.forEach(
|
||||
subSelectBody -> {
|
||||
PlainSelect subPlainSelect = (PlainSelect) subSelectBody;
|
||||
getSubPlainSelect(subPlainSelect, plainSelectList);
|
||||
});
|
||||
}
|
||||
}
|
||||
return plainSelectList;
|
||||
@@ -226,37 +236,39 @@ public class SqlSelectHelper {
|
||||
List<PlainSelect> plainSelects = new ArrayList<>();
|
||||
for (PlainSelect plainSelect : plainSelectList) {
|
||||
plainSelects.add(plainSelect);
|
||||
ExpressionVisitorAdapter expressionVisitor = new ExpressionVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(Select subSelect) {
|
||||
if (subSelect instanceof ParenthesedSelect) {
|
||||
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) subSelect;
|
||||
if (parenthesedSelect.getSelect() instanceof PlainSelect) {
|
||||
plainSelects.add(parenthesedSelect.getPlainSelect());
|
||||
ExpressionVisitorAdapter expressionVisitor =
|
||||
new ExpressionVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(Select subSelect) {
|
||||
if (subSelect instanceof ParenthesedSelect) {
|
||||
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) subSelect;
|
||||
if (parenthesedSelect.getSelect() instanceof PlainSelect) {
|
||||
plainSelects.add(parenthesedSelect.getPlainSelect());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
plainSelect.accept(new SelectVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(PlainSelect plainSelect) {
|
||||
Expression whereExpression = plainSelect.getWhere();
|
||||
if (whereExpression != null) {
|
||||
whereExpression.accept(expressionVisitor);
|
||||
}
|
||||
Expression having = plainSelect.getHaving();
|
||||
if (Objects.nonNull(having)) {
|
||||
having.accept(expressionVisitor);
|
||||
}
|
||||
List<SelectItem<?>> selectItems = plainSelect.getSelectItems();
|
||||
if (!CollectionUtils.isEmpty(selectItems)) {
|
||||
for (SelectItem selectItem : selectItems) {
|
||||
selectItem.accept(expressionVisitor);
|
||||
plainSelect.accept(
|
||||
new SelectVisitorAdapter() {
|
||||
@Override
|
||||
public void visit(PlainSelect plainSelect) {
|
||||
Expression whereExpression = plainSelect.getWhere();
|
||||
if (whereExpression != null) {
|
||||
whereExpression.accept(expressionVisitor);
|
||||
}
|
||||
Expression having = plainSelect.getHaving();
|
||||
if (Objects.nonNull(having)) {
|
||||
having.accept(expressionVisitor);
|
||||
}
|
||||
List<SelectItem<?>> selectItems = plainSelect.getSelectItems();
|
||||
if (!CollectionUtils.isEmpty(selectItems)) {
|
||||
for (SelectItem selectItem : selectItems) {
|
||||
selectItem.accept(expressionVisitor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
return plainSelects;
|
||||
}
|
||||
@@ -297,17 +309,19 @@ public class SqlSelectHelper {
|
||||
if (Objects.nonNull(having)) {
|
||||
having.accept(new FieldAcquireVisitor(result));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void getLateralViewsFields(PlainSelect plainSelect, Set<String> result) {
|
||||
List<LateralView> lateralViews = plainSelect.getLateralViews();
|
||||
if (!CollectionUtils.isEmpty(lateralViews)) {
|
||||
lateralViews.stream().forEach(l -> {
|
||||
if (Objects.nonNull(l.getGeneratorFunction())) {
|
||||
l.getGeneratorFunction().accept(new FieldAcquireVisitor(result));
|
||||
}
|
||||
});
|
||||
lateralViews.stream()
|
||||
.forEach(
|
||||
l -> {
|
||||
if (Objects.nonNull(l.getGeneratorFunction())) {
|
||||
l.getGeneratorFunction()
|
||||
.accept(new FieldAcquireVisitor(result));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -412,10 +426,11 @@ public class SqlSelectHelper {
|
||||
|
||||
private static void getOrderByFields(PlainSelect plainSelect, Set<String> result) {
|
||||
Set<FieldExpression> orderByFieldExpressions = getOrderByFields(plainSelect);
|
||||
Set<String> collect = orderByFieldExpressions.stream()
|
||||
.map(fieldExpression -> fieldExpression.getFieldName())
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
Set<String> collect =
|
||||
orderByFieldExpressions.stream()
|
||||
.map(fieldExpression -> fieldExpression.getFieldName())
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
result.addAll(collect);
|
||||
}
|
||||
|
||||
@@ -474,12 +489,13 @@ public class SqlSelectHelper {
|
||||
if (selectItem.getExpression() instanceof Function) {
|
||||
Function function = (Function) selectItem.getExpression();
|
||||
if (Objects.nonNull(function.getParameters())
|
||||
&& !CollectionUtils.isEmpty(function.getParameters().getExpressions())) {
|
||||
String columnName = function.getParameters().getExpressions().get(0).toString();
|
||||
&& !CollectionUtils.isEmpty(
|
||||
function.getParameters().getExpressions())) {
|
||||
String columnName =
|
||||
function.getParameters().getExpressions().get(0).toString();
|
||||
result.add(columnName);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return new ArrayList<>(result);
|
||||
@@ -502,13 +518,14 @@ public class SqlSelectHelper {
|
||||
result.add(alias.getName());
|
||||
} else {
|
||||
if (Objects.nonNull(function.getParameters())
|
||||
&& !CollectionUtils.isEmpty(function.getParameters().getExpressions())) {
|
||||
String columnName = function.getParameters().getExpressions().get(0).toString();
|
||||
&& !CollectionUtils.isEmpty(
|
||||
function.getParameters().getExpressions())) {
|
||||
String columnName =
|
||||
function.getParameters().getExpressions().get(0).toString();
|
||||
result.add(columnName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return new ArrayList<>(result);
|
||||
@@ -542,8 +559,9 @@ public class SqlSelectHelper {
|
||||
}
|
||||
|
||||
public static boolean isLogicExpression(Expression whereExpression) {
|
||||
return whereExpression instanceof AndExpression || (whereExpression instanceof OrExpression
|
||||
|| (whereExpression instanceof XorExpression));
|
||||
return whereExpression instanceof AndExpression
|
||||
|| (whereExpression instanceof OrExpression
|
||||
|| (whereExpression instanceof XorExpression));
|
||||
}
|
||||
|
||||
public static String getColumnName(Expression leftExpression, Expression rightExpression) {
|
||||
@@ -553,7 +571,8 @@ public class SqlSelectHelper {
|
||||
if (leftExpression instanceof Function) {
|
||||
ExpressionList<?> expressionList = ((Function) leftExpression).getParameters();
|
||||
|
||||
if (!CollectionUtils.isEmpty(expressionList) && expressionList.get(0) instanceof Column) {
|
||||
if (!CollectionUtils.isEmpty(expressionList)
|
||||
&& expressionList.get(0) instanceof Column) {
|
||||
return ((Column) expressionList.get(0)).getColumnName();
|
||||
}
|
||||
}
|
||||
@@ -619,7 +638,8 @@ public class SqlSelectHelper {
|
||||
PlainSelect withPlainSelect = (PlainSelect) withSelect;
|
||||
plainSelectList.add(withPlainSelect);
|
||||
if (withPlainSelect.getFromItem() instanceof ParenthesedSelect) {
|
||||
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) withPlainSelect.getFromItem();
|
||||
ParenthesedSelect parenthesedSelect =
|
||||
(ParenthesedSelect) withPlainSelect.getFromItem();
|
||||
plainSelectList.add(parenthesedSelect.getPlainSelect());
|
||||
}
|
||||
}
|
||||
@@ -666,7 +686,8 @@ public class SqlSelectHelper {
|
||||
}
|
||||
if (plainSelect.getFromItem() instanceof ParenthesedSelect) {
|
||||
|
||||
PlainSelect subSelect = ((ParenthesedSelect) plainSelect.getFromItem()).getPlainSelect();
|
||||
PlainSelect subSelect =
|
||||
((ParenthesedSelect) plainSelect.getFromItem()).getPlainSelect();
|
||||
return getTable(subSelect.getSelectBody().toString());
|
||||
}
|
||||
|
||||
@@ -775,7 +796,8 @@ public class SqlSelectHelper {
|
||||
return results;
|
||||
}
|
||||
|
||||
private static void getFieldsWithSubQuery(PlainSelect plainSelect, Map<String, Set<String>> fields) {
|
||||
private static void getFieldsWithSubQuery(
|
||||
PlainSelect plainSelect, Map<String, Set<String>> fields) {
|
||||
if (plainSelect.getFromItem() instanceof Table) {
|
||||
List<String> withAlias = new ArrayList<>();
|
||||
if (!CollectionUtils.isEmpty(plainSelect.getWithItemsList())) {
|
||||
@@ -792,9 +814,10 @@ public class SqlSelectHelper {
|
||||
if (!fields.containsKey(table.getFullyQualifiedName())) {
|
||||
fields.put(tableName, new HashSet<>());
|
||||
}
|
||||
List<String> sqlFields = getFieldsByPlainSelect(plainSelect).stream().map(f -> f.replaceAll("`", ""))
|
||||
.collect(
|
||||
Collectors.toList());
|
||||
List<String> sqlFields =
|
||||
getFieldsByPlainSelect(plainSelect).stream()
|
||||
.map(f -> f.replaceAll("`", ""))
|
||||
.collect(Collectors.toList());
|
||||
fields.get(tableName).addAll(sqlFields);
|
||||
}
|
||||
}
|
||||
@@ -806,13 +829,14 @@ public class SqlSelectHelper {
|
||||
}
|
||||
for (Join join : plainSelect.getJoins()) {
|
||||
if (join.getRightItem() instanceof ParenthesedSelect) {
|
||||
getFieldsWithSubQuery(((ParenthesedSelect) join.getRightItem()).getPlainSelect(), fields);
|
||||
getFieldsWithSubQuery(
|
||||
((ParenthesedSelect) join.getRightItem()).getPlainSelect(), fields);
|
||||
}
|
||||
if (join.getFromItem() instanceof ParenthesedSelect) {
|
||||
getFieldsWithSubQuery(((ParenthesedSelect) join.getFromItem()).getPlainSelect(), fields);
|
||||
getFieldsWithSubQuery(
|
||||
((ParenthesedSelect) join.getFromItem()).getPlainSelect(), fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package com.tencent.supersonic.common.jsqlparser;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
|
||||
/**
|
||||
* Sql Parser valid Helper
|
||||
*/
|
||||
import java.util.List;
|
||||
|
||||
/** Sql Parser valid Helper */
|
||||
@Slf4j
|
||||
public class SqlValidHelper {
|
||||
|
||||
@@ -19,7 +18,7 @@ public class SqlValidHelper {
|
||||
* @return
|
||||
*/
|
||||
public static boolean equals(String thisSql, String otherSql) {
|
||||
//1. select fields
|
||||
// 1. select fields
|
||||
List<String> thisSelectFields = SqlSelectHelper.getSelectFields(thisSql);
|
||||
List<String> otherSelectFields = SqlSelectHelper.getSelectFields(otherSql);
|
||||
|
||||
@@ -27,7 +26,7 @@ public class SqlValidHelper {
|
||||
return false;
|
||||
}
|
||||
|
||||
//2. all fields
|
||||
// 2. all fields
|
||||
List<String> thisAllFields = SqlSelectHelper.getAllSelectFields(thisSql);
|
||||
List<String> otherAllFields = SqlSelectHelper.getAllSelectFields(otherSql);
|
||||
|
||||
@@ -35,28 +34,31 @@ public class SqlValidHelper {
|
||||
return false;
|
||||
}
|
||||
|
||||
//3. where
|
||||
// 3. where
|
||||
List<FieldExpression> thisFieldExpressions = SqlSelectHelper.getFilterExpression(thisSql);
|
||||
List<FieldExpression> otherFieldExpressions = SqlSelectHelper.getFilterExpression(otherSql);
|
||||
|
||||
if (!CollectionUtils.isEqualCollection(thisFieldExpressions, otherFieldExpressions)) {
|
||||
return false;
|
||||
}
|
||||
//4. tableName
|
||||
// 4. tableName
|
||||
if (!SqlSelectHelper.getDbTableName(thisSql)
|
||||
.equalsIgnoreCase(SqlSelectHelper.getDbTableName(otherSql))) {
|
||||
return false;
|
||||
}
|
||||
//5. having
|
||||
// 5. having
|
||||
List<FieldExpression> thisHavingExpressions = SqlSelectHelper.getHavingExpressions(thisSql);
|
||||
List<FieldExpression> otherHavingExpressions = SqlSelectHelper.getHavingExpressions(otherSql);
|
||||
List<FieldExpression> otherHavingExpressions =
|
||||
SqlSelectHelper.getHavingExpressions(otherSql);
|
||||
|
||||
if (!CollectionUtils.isEqualCollection(thisHavingExpressions, otherHavingExpressions)) {
|
||||
return false;
|
||||
}
|
||||
//6. orderBy
|
||||
List<FieldExpression> thisOrderByExpressions = SqlSelectHelper.getOrderByExpressions(thisSql);
|
||||
List<FieldExpression> otherOrderByExpressions = SqlSelectHelper.getOrderByExpressions(otherSql);
|
||||
// 6. orderBy
|
||||
List<FieldExpression> thisOrderByExpressions =
|
||||
SqlSelectHelper.getOrderByExpressions(thisSql);
|
||||
List<FieldExpression> otherOrderByExpressions =
|
||||
SqlSelectHelper.getOrderByExpressions(otherSql);
|
||||
|
||||
if (!CollectionUtils.isEqualCollection(thisOrderByExpressions, otherOrderByExpressions)) {
|
||||
return false;
|
||||
@@ -73,6 +75,4 @@ public class SqlValidHelper {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -15,4 +15,4 @@ public class TableNameReplaceVisitor extends FromItemVisitorAdapter {
|
||||
public void visit(Table table) {
|
||||
table.setName(tableName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,5 +14,4 @@ public class SystemConfigDO {
|
||||
private String parameters;
|
||||
|
||||
private String admin;
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,4 @@ import com.tencent.supersonic.common.persistence.dataobject.SystemConfigDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface SystemConfigMapper extends BaseMapper<SystemConfigDO> {
|
||||
|
||||
|
||||
}
|
||||
public interface SystemConfigMapper extends BaseMapper<SystemConfigDO> {}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.tencent.supersonic.common.pojo;
|
||||
|
||||
import com.tencent.supersonic.common.pojo.enums.AggOperatorEnum;
|
||||
|
||||
import java.util.List;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
import com.tencent.supersonic.common.pojo.enums.AggOperatorEnum;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class Aggregator {
|
||||
|
||||
@@ -21,8 +21,7 @@ public class Aggregator {
|
||||
|
||||
private String alias;
|
||||
|
||||
public Aggregator() {
|
||||
}
|
||||
public Aggregator() {}
|
||||
|
||||
public Aggregator(String column, AggOperatorEnum func) {
|
||||
this.column = column;
|
||||
@@ -44,16 +43,11 @@ public class Aggregator {
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("{");
|
||||
sb.append("\"column\":\"")
|
||||
.append(column).append('\"');
|
||||
sb.append(",\"func\":")
|
||||
.append(func);
|
||||
sb.append(",\"nameCh\":\"")
|
||||
.append(nameCh).append('\"');
|
||||
sb.append(",\"args\":")
|
||||
.append(args);
|
||||
sb.append(",\"alias\":")
|
||||
.append(alias);
|
||||
sb.append("\"column\":\"").append(column).append('\"');
|
||||
sb.append(",\"func\":").append(func);
|
||||
sb.append(",\"nameCh\":\"").append(nameCh).append('\"');
|
||||
sb.append(",\"args\":").append(args);
|
||||
sb.append(",\"alias\":").append(alias);
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@@ -31,4 +31,4 @@ public class ChatModelConfig implements Serializable {
|
||||
public String keyDecrypt() {
|
||||
return AESEncryptionUtil.aesDecryptECB(getApiKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package com.tencent.supersonic.common.pojo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
|
||||
@@ -2,7 +2,6 @@ package com.tencent.supersonic.common.pojo;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
||||
public class Constants {
|
||||
|
||||
public static final String COMMA = ",";
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.tencent.supersonic.common.pojo;
|
||||
|
||||
|
||||
import com.tencent.supersonic.common.pojo.enums.FilterOperatorEnum;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Criterion {
|
||||
@@ -26,7 +26,8 @@ public class Criterion {
|
||||
this.value = value;
|
||||
this.dataType = dataType;
|
||||
|
||||
if (FilterOperatorEnum.BETWEEN.name().equals(operator) || FilterOperatorEnum.IN.name().equals(operator)
|
||||
if (FilterOperatorEnum.BETWEEN.name().equals(operator)
|
||||
|| FilterOperatorEnum.IN.name().equals(operator)
|
||||
|| FilterOperatorEnum.NOT_IN.name().equals(operator)) {
|
||||
this.values = (List) value;
|
||||
}
|
||||
@@ -34,11 +35,11 @@ public class Criterion {
|
||||
|
||||
public boolean isNeedApostrophe() {
|
||||
return Arrays.stream(StringDataType.values())
|
||||
.filter(value -> this.dataType.equalsIgnoreCase(value.getType())).findFirst()
|
||||
.filter(value -> this.dataType.equalsIgnoreCase(value.getType()))
|
||||
.findFirst()
|
||||
.isPresent();
|
||||
}
|
||||
|
||||
|
||||
public enum NumericDataType {
|
||||
TINYINT("TINYINT"),
|
||||
SMALLINT("SMALLINT"),
|
||||
@@ -62,7 +63,6 @@ public class Criterion {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public enum StringDataType {
|
||||
VARCHAR("VARCHAR"),
|
||||
STRING("STRING"),
|
||||
@@ -77,6 +77,4 @@ public class Criterion {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package com.tencent.supersonic.common.pojo;
|
||||
|
||||
|
||||
import com.tencent.supersonic.common.pojo.enums.EventType;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import java.util.List;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DataEvent extends ApplicationEvent {
|
||||
|
||||
|
||||
@@ -8,6 +8,4 @@ public class DataFormat {
|
||||
private boolean needMultiply100;
|
||||
|
||||
private Integer decimalPlaces;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -8,9 +8,7 @@ import lombok.Data;
|
||||
@Builder
|
||||
public class DataItem {
|
||||
|
||||
/***
|
||||
* This field uses an underscore (_) at the end.
|
||||
*/
|
||||
/** * This field uses an underscore (_) at the end. */
|
||||
private String id;
|
||||
|
||||
private String bizName;
|
||||
@@ -21,9 +19,7 @@ public class DataItem {
|
||||
|
||||
private TypeEnums type;
|
||||
|
||||
/***
|
||||
* This field uses an underscore (_) at the end.
|
||||
*/
|
||||
/** * This field uses an underscore (_) at the end. */
|
||||
private String modelId;
|
||||
|
||||
private String defaultAgg;
|
||||
|
||||
@@ -11,7 +11,8 @@ public class DataUpdateEvent extends ApplicationEvent {
|
||||
private Long id;
|
||||
private TypeEnums type;
|
||||
|
||||
public DataUpdateEvent(Object source, String name, String newName, Long modelId, Long id, TypeEnums type) {
|
||||
public DataUpdateEvent(
|
||||
Object source, String name, String newName, Long modelId, Long id, TypeEnums type) {
|
||||
super(source);
|
||||
this.name = name;
|
||||
this.newName = newName;
|
||||
|
||||
@@ -8,6 +8,7 @@ import org.springframework.util.CollectionUtils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static java.time.LocalDate.now;
|
||||
|
||||
@Data
|
||||
@@ -15,34 +16,23 @@ public class DateConf {
|
||||
|
||||
private static final long serialVersionUID = 3074129990945004340L;
|
||||
|
||||
|
||||
private DateMode dateMode = DateMode.RECENT;
|
||||
|
||||
/**
|
||||
* like 2021-10-22, dateMode=1
|
||||
*/
|
||||
/** like 2021-10-22, dateMode=1 */
|
||||
private String startDate = now().plusDays(-1).toString();
|
||||
|
||||
private String endDate = now().toString();
|
||||
|
||||
/**
|
||||
* [2021-10-22, 2022-01-22], dateMode=2
|
||||
*/
|
||||
/** [2021-10-22, 2022-01-22], dateMode=2 */
|
||||
private List<String> dateList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* the last unit time unit,
|
||||
* such as the last 7 days, unit = 7
|
||||
*/
|
||||
/** the last unit time unit, such as the last 7 days, unit = 7 */
|
||||
private Integer unit = 1;
|
||||
|
||||
/**
|
||||
* DAY,WEEK,MONTH
|
||||
*/
|
||||
/** DAY,WEEK,MONTH */
|
||||
private String period = Constants.DAY;
|
||||
|
||||
/**
|
||||
* the text parse from , example "last 7 days" , "last mouth"
|
||||
*/
|
||||
/** the text parse from , example "last 7 days" , "last mouth" */
|
||||
private String detectWord;
|
||||
|
||||
private boolean isInherited;
|
||||
@@ -93,33 +83,28 @@ public class DateConf {
|
||||
|
||||
public enum DateMode {
|
||||
/**
|
||||
* date mode
|
||||
* 1 - BETWEEN, continuous static value, [startDate, endDate]
|
||||
* 2 - LIST, discrete static value, [dateList]
|
||||
* 3 - RECENT, dynamic time related to the actual available time of the element, [unit, period]
|
||||
* 4 - AVAILABLE, dynamic time which guaranteed to query some data, [startDate, endDate]
|
||||
* 5 - ALL, all table data
|
||||
* date mode 1 - BETWEEN, continuous static value, [startDate, endDate] 2 - LIST, discrete
|
||||
* static value, [dateList] 3 - RECENT, dynamic time related to the actual available time of
|
||||
* the element, [unit, period] 4 - AVAILABLE, dynamic time which guaranteed to query some
|
||||
* data, [startDate, endDate] 5 - ALL, all table data
|
||||
*/
|
||||
BETWEEN, LIST, RECENT, AVAILABLE, ALL
|
||||
BETWEEN,
|
||||
LIST,
|
||||
RECENT,
|
||||
AVAILABLE,
|
||||
ALL
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("{");
|
||||
sb.append("\"dateMode\":")
|
||||
.append(dateMode);
|
||||
sb.append(",\"startDate\":\"")
|
||||
.append(startDate).append('\"');
|
||||
sb.append(",\"endDate\":\"")
|
||||
.append(endDate).append('\"');
|
||||
sb.append(",\"dateList\":")
|
||||
.append(dateList);
|
||||
sb.append(",\"unit\":")
|
||||
.append(unit);
|
||||
sb.append(",\"period\":\"")
|
||||
.append(period).append('\"');
|
||||
sb.append(",\"text\":\"")
|
||||
.append(detectWord).append('\"');
|
||||
sb.append("\"dateMode\":").append(dateMode);
|
||||
sb.append(",\"startDate\":\"").append(startDate).append('\"');
|
||||
sb.append(",\"endDate\":\"").append(endDate).append('\"');
|
||||
sb.append(",\"dateList\":").append(dateList);
|
||||
sb.append(",\"unit\":").append(unit);
|
||||
sb.append(",\"period\":\"").append(period).append('\"');
|
||||
sb.append(",\"text\":\"").append(detectWord).append('\"');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@@ -4,7 +4,5 @@ public class DimensionConstants {
|
||||
|
||||
public static final String DIMENSION_TIME_FORMAT = "time_format";
|
||||
|
||||
|
||||
public static final String DIMENSION_TYPE = "dimension_type";
|
||||
|
||||
}
|
||||
|
||||
@@ -24,4 +24,4 @@ public class EmbeddingModelConfig implements Serializable {
|
||||
private Integer maxToken;
|
||||
private Boolean logRequests;
|
||||
private Boolean logResponses;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,4 +22,4 @@ public class EmbeddingStoreConfig implements Serializable {
|
||||
private Long timeOut = 60L;
|
||||
private Integer dimension;
|
||||
private String databaseName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.tencent.supersonic.common.pojo;
|
||||
|
||||
|
||||
import com.tencent.supersonic.common.pojo.enums.FilterOperatorEnum;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@@ -36,23 +36,19 @@ public class Filter {
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("{");
|
||||
sb.append("\"relation\":")
|
||||
.append(relation);
|
||||
sb.append(",\"bizName\":\"")
|
||||
.append(bizName).append('\"');
|
||||
sb.append(",\"name\":\"")
|
||||
.append(name).append('\"');
|
||||
sb.append(",\"operator\":")
|
||||
.append(operator);
|
||||
sb.append(",\"value\":")
|
||||
.append(value);
|
||||
sb.append(",\"children\":")
|
||||
.append(children);
|
||||
sb.append("\"relation\":").append(relation);
|
||||
sb.append(",\"bizName\":\"").append(bizName).append('\"');
|
||||
sb.append(",\"name\":\"").append(name).append('\"');
|
||||
sb.append(",\"operator\":").append(operator);
|
||||
sb.append(",\"value\":").append(value);
|
||||
sb.append(",\"children\":").append(children);
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public enum Relation {
|
||||
FILTER, OR, AND
|
||||
FILTER,
|
||||
OR,
|
||||
AND
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package com.tencent.supersonic.common.pojo;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
@@ -17,4 +18,4 @@ public class ItemDateResp {
|
||||
private String endDate;
|
||||
private String datePeriod;
|
||||
private List<String> unavailableDateList;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,5 +15,4 @@ public class JoinCondition {
|
||||
private String rightField;
|
||||
|
||||
private FilterOperatorEnum operator;
|
||||
|
||||
}
|
||||
|
||||
@@ -13,5 +13,4 @@ public class ModelConfig implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private ChatModelConfig chatModel;
|
||||
private EmbeddingModelConfig embeddingModel;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.tencent.supersonic.common.pojo;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
@@ -16,9 +15,8 @@ public class ModelRela extends RecordInfo {
|
||||
|
||||
private Long toModelId;
|
||||
|
||||
//left join, inner join, right join, outer join
|
||||
// left join, inner join, right join, outer join
|
||||
private String joinType;
|
||||
|
||||
private List<JoinCondition> joinConditions;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.tencent.supersonic.common.pojo;
|
||||
|
||||
import static com.tencent.supersonic.common.pojo.Constants.ASC_UPPER;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import lombok.Data;
|
||||
|
||||
import static com.tencent.supersonic.common.pojo.Constants.ASC_UPPER;
|
||||
|
||||
@Data
|
||||
public class Order {
|
||||
|
||||
@@ -20,16 +20,13 @@ public class Order {
|
||||
this.direction = direction;
|
||||
}
|
||||
|
||||
public Order() {
|
||||
}
|
||||
public Order() {}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder("{");
|
||||
sb.append("\"column\":\"")
|
||||
.append(column).append('\"');
|
||||
sb.append(",\"direction\":\"")
|
||||
.append(direction).append('\"');
|
||||
sb.append("\"column\":\"").append(column).append('\"');
|
||||
sb.append(",\"direction\":\"").append(direction).append('\"');
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
@@ -43,8 +40,7 @@ public class Order {
|
||||
return false;
|
||||
}
|
||||
Order order = (Order) o;
|
||||
return Objects.equal(column, order.column) && Objects.equal(direction,
|
||||
order.direction);
|
||||
return Objects.equal(column, order.column) && Objects.equal(direction, order.direction);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -18,5 +18,4 @@ public class PageBaseReq {
|
||||
public Integer getLimitSize() {
|
||||
return this.pageSize;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,42 +13,24 @@ import java.util.Map;
|
||||
/**
|
||||
* 1.Password Field:
|
||||
*
|
||||
* dataType: string
|
||||
* name: password
|
||||
* require: true/false or any value/empty
|
||||
* placeholder: 'Please enter the relevant configuration information'
|
||||
* value: initial value
|
||||
* Text Input Field:
|
||||
* <p>dataType: string name: password require: true/false or any value/empty placeholder: 'Please
|
||||
* enter the relevant configuration information' value: initial value Text Input Field:
|
||||
*
|
||||
* 2.dataType: string
|
||||
* require: true/false or any value/empty
|
||||
* placeholder: 'Please enter the relevant configuration information'
|
||||
* value: initial value
|
||||
* Long Text Input Field:
|
||||
* <p>2.dataType: string require: true/false or any value/empty placeholder: 'Please enter the
|
||||
* relevant configuration information' value: initial value Long Text Input Field:
|
||||
*
|
||||
* 3.dataType: longText
|
||||
* require: true/false or any value/empty
|
||||
* placeholder: 'Please enter the relevant configuration information'
|
||||
* value: initial value
|
||||
* Number Input Field:
|
||||
* <p>3.dataType: longText require: true/false or any value/empty placeholder: 'Please enter the
|
||||
* relevant configuration information' value: initial value Number Input Field:
|
||||
*
|
||||
* 4.dataType: number
|
||||
* require: true/false or any value/empty
|
||||
* placeholder: 'Please enter the relevant configuration information'
|
||||
* value: initial value
|
||||
* Switch Component:
|
||||
* <p>4.dataType: number require: true/false or any value/empty placeholder: 'Please enter the
|
||||
* relevant configuration information' value: initial value Switch Component:
|
||||
*
|
||||
* 5.dataType: bool
|
||||
* require: true/false or any value/empty
|
||||
* value: initial value
|
||||
* Select Dropdown Component:
|
||||
* <p>5.dataType: bool require: true/false or any value/empty value: initial value Select Dropdown
|
||||
* Component:
|
||||
*
|
||||
* 6.dataType: list
|
||||
* candidateValues: ["OPEN_AI", "OLLAMA"] or
|
||||
* [{label: 'Model Name 1', value: 'OPEN_AI'}, {label: 'Model Name 2', value: 'OLLAMA'}]
|
||||
* require: true/false or any value/empty
|
||||
* placeholder: 'Please enter the relevant configuration information'
|
||||
* value: initial value
|
||||
* <p>6.dataType: list candidateValues: ["OPEN_AI", "OLLAMA"] or [{label: 'Model Name 1', value:
|
||||
* 'OPEN_AI'}, {label: 'Model Name 2', value: 'OLLAMA'}] require: true/false or any value/empty
|
||||
* placeholder: 'Please enter the relevant configuration information' value: initial value
|
||||
*/
|
||||
public class Parameter {
|
||||
private String name;
|
||||
@@ -61,18 +43,36 @@ public class Parameter {
|
||||
private List<String> candidateValues;
|
||||
private List<Dependency> dependencies;
|
||||
|
||||
public Parameter(String name, String defaultValue, String comment,
|
||||
String description, String dataType, String module) {
|
||||
public Parameter(
|
||||
String name,
|
||||
String defaultValue,
|
||||
String comment,
|
||||
String description,
|
||||
String dataType,
|
||||
String module) {
|
||||
this(name, defaultValue, comment, description, dataType, module, null, null);
|
||||
}
|
||||
|
||||
public Parameter(String name, String defaultValue, String comment, String description,
|
||||
String dataType, String module, List<String> candidateValues) {
|
||||
public Parameter(
|
||||
String name,
|
||||
String defaultValue,
|
||||
String comment,
|
||||
String description,
|
||||
String dataType,
|
||||
String module,
|
||||
List<String> candidateValues) {
|
||||
this(name, defaultValue, comment, description, dataType, module, candidateValues, null);
|
||||
}
|
||||
|
||||
public Parameter(String name, String defaultValue, String comment, String description,
|
||||
String dataType, String module, List<String> candidateValues, List<Dependency> dependencies) {
|
||||
public Parameter(
|
||||
String name,
|
||||
String defaultValue,
|
||||
String comment,
|
||||
String description,
|
||||
String dataType,
|
||||
String module,
|
||||
List<String> candidateValues,
|
||||
List<Dependency> dependencies) {
|
||||
this.name = name;
|
||||
this.defaultValue = defaultValue;
|
||||
this.comment = comment;
|
||||
@@ -105,4 +105,4 @@ public class Parameter {
|
||||
private List<String> includesValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.tencent.supersonic.common.pojo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
@ToString
|
||||
@@ -21,4 +21,4 @@ public class QueryAuthorization {
|
||||
public QueryAuthorization(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,8 +41,7 @@ public class RecordInfo {
|
||||
return false;
|
||||
}
|
||||
RecordInfo that = (RecordInfo) o;
|
||||
return Objects.equal(createdBy, that.createdBy) && Objects.equal(
|
||||
updatedBy, that.updatedBy);
|
||||
return Objects.equal(createdBy, that.createdBy) && Objects.equal(updatedBy, that.updatedBy);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,9 +5,7 @@ import com.tencent.supersonic.common.util.TraceIdUtil;
|
||||
import lombok.Data;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
/***
|
||||
* result data
|
||||
*/
|
||||
/** * result data */
|
||||
@Data
|
||||
public class ResultData<T> {
|
||||
private int code;
|
||||
@@ -36,5 +34,4 @@ public class ResultData<T> {
|
||||
resultData.setTraceId(MDC.get(TraceIdUtil.TRACE_ID));
|
||||
return resultData;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,5 +20,4 @@ public class Text2SQLExemplar {
|
||||
private String dbSchema;
|
||||
|
||||
private String sql;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,14 +17,11 @@ public class ThreadPoolExecutorMdcWrapper extends ThreadPoolTaskExecutor {
|
||||
|
||||
@Override
|
||||
public <T> Future<T> submit(Callable<T> task) {
|
||||
return super
|
||||
.submit(ThreadMdcUtil.wrap(task, MDC.getCopyOfContextMap()));
|
||||
return super.submit(ThreadMdcUtil.wrap(task, MDC.getCopyOfContextMap()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<?> submit(Runnable task) {
|
||||
return super
|
||||
.submit(ThreadMdcUtil.wrap(task, MDC.getCopyOfContextMap()));
|
||||
return super.submit(ThreadMdcUtil.wrap(task, MDC.getCopyOfContextMap()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum AggOperatorEnum {
|
||||
|
||||
MAX("MAX"),
|
||||
|
||||
MIN("MIN"),
|
||||
@@ -43,14 +42,14 @@ public enum AggOperatorEnum {
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if aggType is count_Distinct type
|
||||
* 1.outer SQL parses the count_distinct(field) operator as count(DISTINCT field).
|
||||
* 2.tableSQL generates aggregation that ignores the count_distinct operator.
|
||||
* Determine if aggType is count_Distinct type 1.outer SQL parses the count_distinct(field)
|
||||
* operator as count(DISTINCT field). 2.tableSQL generates aggregation that ignores the
|
||||
* count_distinct operator.
|
||||
*
|
||||
* @param aggType aggType
|
||||
* @return is count_Distinct type or not
|
||||
*/
|
||||
public static boolean isCountDistinct(String aggType) {
|
||||
return null != aggType && aggType.toUpperCase().equals(COUNT_DISTINCT.getOperator());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum ApiItemType {
|
||||
|
||||
METRIC,
|
||||
TAG,
|
||||
DIMENSION
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum AuthType {
|
||||
|
||||
VISIBLE,
|
||||
ADMIN
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum ConfigMode {
|
||||
|
||||
|
||||
DETAIL("DETAIL"),
|
||||
AGG("AGG"),
|
||||
UNKNOWN("UNKNOWN");
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum DataFormatTypeEnum {
|
||||
|
||||
PERCENT("percent"),
|
||||
|
||||
DECIMAL("decimal");
|
||||
@@ -15,5 +14,4 @@ public enum DataFormatTypeEnum {
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum DataTypeEnums {
|
||||
|
||||
ARRAY("ARRAY"),
|
||||
|
||||
MAP("MAP"),
|
||||
@@ -42,4 +41,4 @@ public enum DataTypeEnums {
|
||||
}
|
||||
return DataTypeEnums.UNKNOWN;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,5 +24,4 @@ public enum DatePeriodEnum {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/***
|
||||
* nature type
|
||||
* such as : metric、dimension etc.
|
||||
*/
|
||||
/** * nature type such as : metric、dimension etc. */
|
||||
public enum DictWordType {
|
||||
|
||||
METRIC("metric"),
|
||||
|
||||
DIMENSION("dimension"),
|
||||
@@ -48,13 +45,15 @@ public enum DictWordType {
|
||||
return dictWordType;
|
||||
}
|
||||
}
|
||||
//dataSet
|
||||
// dataSet
|
||||
String[] natures = nature.split(DictWordType.NATURE_SPILT);
|
||||
if (natures.length == 2 && StringUtils.isNumeric(natures[1])) {
|
||||
return DATASET;
|
||||
}
|
||||
//dimension value
|
||||
if (natures.length == 3 && StringUtils.isNumeric(natures[1]) && StringUtils.isNumeric(natures[2])) {
|
||||
// dimension value
|
||||
if (natures.length == 3
|
||||
&& StringUtils.isNumeric(natures[1])
|
||||
&& StringUtils.isNumeric(natures[2])) {
|
||||
return VALUE;
|
||||
}
|
||||
return null;
|
||||
@@ -76,5 +75,4 @@ public enum DictWordType {
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum ErrorCode {
|
||||
|
||||
MULTIPLE_ERRORS_PLACEHOLDER,
|
||||
MULTIPLE_ERRORS,
|
||||
NULL_POINTER,
|
||||
@@ -12,6 +11,5 @@ public enum ErrorCode {
|
||||
DUPLICATED_THEME,
|
||||
UNKNOWN;
|
||||
|
||||
private ErrorCode() {
|
||||
}
|
||||
private ErrorCode() {}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum EventType {
|
||||
|
||||
ADD,
|
||||
UPDATE,
|
||||
DELETE
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
import net.sf.jsqlparser.expression.operators.relational.ComparisonOperator;
|
||||
@@ -35,7 +34,8 @@ public enum FilterOperatorEnum {
|
||||
@JsonCreator
|
||||
public static FilterOperatorEnum getSqlOperator(String type) {
|
||||
for (FilterOperatorEnum operatorEnum : FilterOperatorEnum.values()) {
|
||||
if (operatorEnum.value.equalsIgnoreCase(type) || operatorEnum.name().equalsIgnoreCase(type)) {
|
||||
if (operatorEnum.value.equalsIgnoreCase(type)
|
||||
|| operatorEnum.name().equalsIgnoreCase(type)) {
|
||||
return operatorEnum;
|
||||
}
|
||||
}
|
||||
@@ -48,9 +48,12 @@ public enum FilterOperatorEnum {
|
||||
}
|
||||
|
||||
public static boolean isValueCompare(FilterOperatorEnum filterOperatorEnum) {
|
||||
return EQUALS.equals(filterOperatorEnum) || GREATER_THAN.equals(filterOperatorEnum)
|
||||
|| GREATER_THAN_EQUALS.equals(filterOperatorEnum) || MINOR_THAN.equals(filterOperatorEnum)
|
||||
|| MINOR_THAN_EQUALS.equals(filterOperatorEnum) || NOT_EQUALS.equals(filterOperatorEnum);
|
||||
return EQUALS.equals(filterOperatorEnum)
|
||||
|| GREATER_THAN.equals(filterOperatorEnum)
|
||||
|| GREATER_THAN_EQUALS.equals(filterOperatorEnum)
|
||||
|| MINOR_THAN.equals(filterOperatorEnum)
|
||||
|| MINOR_THAN_EQUALS.equals(filterOperatorEnum)
|
||||
|| NOT_EQUALS.equals(filterOperatorEnum);
|
||||
}
|
||||
|
||||
public static ComparisonOperator createExpression(FilterOperatorEnum operator) {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum FilterType {
|
||||
//filtering between different dimensions will directly splice the AND clause
|
||||
// filtering between different dimensions will directly splice the AND clause
|
||||
AND,
|
||||
//filtering between different dimensions will generate multiple sql clauses and splice them together using union
|
||||
// filtering between different dimensions will generate multiple sql clauses and splice them
|
||||
// together using union
|
||||
UNION
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum PublishEnum {
|
||||
|
||||
UN_PUBLISHED(0),
|
||||
PUBLISHED(1);
|
||||
|
||||
@@ -14,5 +13,4 @@ public enum PublishEnum {
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,20 +1,12 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
/**
|
||||
* Enumerate query types supported by SuperSonic.
|
||||
*/
|
||||
/** Enumerate query types supported by SuperSonic. */
|
||||
public enum QueryType {
|
||||
/**
|
||||
* queries with metric calculation (optionally slice and dice by dimensions)
|
||||
*/
|
||||
/** queries with metric calculation (optionally slice and dice by dimensions) */
|
||||
METRIC,
|
||||
/**
|
||||
* queries with tag-based entity targeting
|
||||
*/
|
||||
/** queries with tag-based entity targeting */
|
||||
DETAIL,
|
||||
/**
|
||||
* queries with ID-based entity selection
|
||||
*/
|
||||
/** queries with ID-based entity selection */
|
||||
ID;
|
||||
|
||||
public boolean isNativeAggQuery() {
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum SensitiveLevelEnum {
|
||||
|
||||
LOW(0),
|
||||
MID(1),
|
||||
HIGH(2);
|
||||
|
||||
|
||||
private Integer code;
|
||||
|
||||
SensitiveLevelEnum(Integer code) {
|
||||
@@ -16,5 +14,4 @@ public enum SensitiveLevelEnum {
|
||||
public Integer getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,17 +1,14 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum SinkDbEnum {
|
||||
|
||||
TDW("TDW"),
|
||||
|
||||
DORIS("DORIS"),
|
||||
|
||||
ICEBERY("ICEBERY"),
|
||||
|
||||
|
||||
NOT_SUPPORT("NOT_SUPPORT");
|
||||
|
||||
|
||||
private String db;
|
||||
|
||||
SinkDbEnum(String db) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum StatusEnum {
|
||||
|
||||
INITIALIZED("INITIALIZED", 0),
|
||||
ONLINE("ONLINE", 1),
|
||||
OFFLINE("OFFLINE", 2),
|
||||
@@ -9,7 +8,6 @@ public enum StatusEnum {
|
||||
UNAVAILABLE("UNAVAILABLE", 4),
|
||||
UNKNOWN("UNKNOWN", -1);
|
||||
|
||||
|
||||
private String status;
|
||||
private Integer code;
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum TaskStatusEnum {
|
||||
|
||||
INITIAL("initial", -2),
|
||||
|
||||
ERROR("error", -1),
|
||||
@@ -47,5 +46,4 @@ public enum TaskStatusEnum {
|
||||
}
|
||||
return TaskStatusEnum.UNKNOWN;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum Text2SQLType {
|
||||
|
||||
ONLY_RULE, ONLY_LLM, RULE_AND_LLM;
|
||||
ONLY_RULE,
|
||||
ONLY_LLM,
|
||||
RULE_AND_LLM;
|
||||
|
||||
public boolean enableRule() {
|
||||
return this.equals(ONLY_RULE) || this.equals(RULE_AND_LLM);
|
||||
@@ -11,5 +12,4 @@ public enum Text2SQLType {
|
||||
public boolean enableLLM() {
|
||||
return this.equals(ONLY_LLM) || this.equals(RULE_AND_LLM);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user