(improvement)(Headless) corrector supports subselect sql and opt BenchMarkDemoDataLoader (#1023)

* [improvement] corrector and QueryReqConverter  support subselect

* [improvement] corrector and QueryReqConverter  support subselect

* [improvement] adapt BenchMarkDemoDataLoader

* [improvement] adapt checkstyle

---------

Co-authored-by: zuopengge
This commit is contained in:
mainmain
2024-05-22 15:56:06 +08:00
committed by GitHub
parent 2cc107ebc9
commit 418abef982
5 changed files with 95 additions and 62 deletions

View File

@@ -5,6 +5,8 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import com.tencent.supersonic.common.pojo.enums.TimeDimensionEnum;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Function;
@@ -23,6 +25,7 @@ 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;
@@ -148,6 +151,24 @@ public class SqlAddHelper {
return sql;
}
PlainSelect plainSelect = (PlainSelect) selectStatement;
List<String> chNameList = TimeDimensionEnum.getChNameList();
Boolean dateWhere = false;
for (String chName : chNameList) {
if (expression.toString().contains(chName)) {
dateWhere = true;
}
}
if (plainSelect.getFromItem() instanceof ParenthesedSelect && dateWhere) {
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) plainSelect.getFromItem();
PlainSelect subPlainSelect = parenthesedSelect.getPlainSelect();
Expression subWhere = subPlainSelect.getWhere();
if (subWhere == null) {
subPlainSelect.setWhere(expression);
} else {
subPlainSelect.setWhere(new AndExpression(subWhere, expression));
}
return selectStatement.toString();
}
Expression where = plainSelect.getWhere();
if (where == null) {

View File

@@ -397,12 +397,14 @@ public class SqlReplaceHelper {
if (selectStatement instanceof PlainSelect) {
PlainSelect plainSelect = (PlainSelect) selectStatement;
replaceSingleTable(plainSelect, tableName);
replaceSubTable(plainSelect, tableName);
} 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);
});
}
}
@@ -410,6 +412,14 @@ public class SqlReplaceHelper {
return selectStatement.toString();
}
public static void replaceSubTable(PlainSelect plainSelect, String tableName) {
if (plainSelect.getFromItem() instanceof ParenthesedSelect) {
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) plainSelect.getFromItem();
PlainSelect subPlainSelect = parenthesedSelect.getPlainSelect();
replaceSingleTable(subPlainSelect, tableName);
}
}
public static void replaceSingleTable(PlainSelect plainSelect, String tableName) {
// replace table name
List<PlainSelect> plainSelects = new ArrayList<>();