summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwangwei <[email protected]>2023-05-15 16:01:31 +0800
committerwangwei <[email protected]>2023-05-31 09:24:51 +0800
commit058a830b1a65ab149c629d22015b66be732d470d (patch)
tree480c09d0848aa1ef8971fd7e0e9e2bbb777588ca
parent2d18f9c0bccdbfa125df460ca3569bb2a3741a99 (diff)
fix(回表优化):TSG-15045 处理查询条件携带别名的情况,增加测试用例361.2
-rw-r--r--config/nacos/config/fixed-127.0.0.1_8848-tsg_nacos/data/config-data-tenant/tsg/Galaxy/engine-queries-template.sql4
-rw-r--r--src/main/java/com/mesalab/common/utils/sqlparser/CondExpressionHelper.java26
-rw-r--r--src/main/java/com/mesalab/qgw/dialect/ClickHouseDialect.java6
3 files changed, 21 insertions, 15 deletions
diff --git a/config/nacos/config/fixed-127.0.0.1_8848-tsg_nacos/data/config-data-tenant/tsg/Galaxy/engine-queries-template.sql b/config/nacos/config/fixed-127.0.0.1_8848-tsg_nacos/data/config-data-tenant/tsg/Galaxy/engine-queries-template.sql
index 4893ad77..0a8de4be 100644
--- a/config/nacos/config/fixed-127.0.0.1_8848-tsg_nacos/data/config-data-tenant/tsg/Galaxy/engine-queries-template.sql
+++ b/config/nacos/config/fixed-127.0.0.1_8848-tsg_nacos/data/config-data-tenant/tsg/Galaxy/engine-queries-template.sql
@@ -129,4 +129,6 @@ SELECT QUANTILE(common_c2s_byte_num) AS c2s FROM session_record WHERE common_rec
--Q65.QUANTILE(druid)
SELECT QUANTILE(established_conn_num, 0.6) FROM traffic_metrics_log WHERE __time >= @start AND __time < @end limit 1
--Q66.Top Optimizer
-SELECT common_client_ip AS common_client_ip, count(*) AS count, sum(common_c2s_byte_num + common_s2c_byte_num) / 1024 / 1024 AS bytes_MB FROM session_record WHERE common_recv_time >= toDateTime(@start) AND common_recv_time < toDateTime(@end) GROUP BY common_client_ip ORDER BY count DESC LIMIT 10 \ No newline at end of file
+SELECT common_client_ip AS common_client_ip, count(*) AS count, sum(common_c2s_byte_num + common_s2c_byte_num) / 1024 / 1024 AS bytes_MB FROM session_record WHERE common_recv_time >= toDateTime(@start) AND common_recv_time < toDateTime(@end) GROUP BY common_client_ip ORDER BY count DESC LIMIT 10
+--Q70.subQuery optimizing, filter field contain table name
+SELECT FROM_UNIXTIME(common_recv_time) AS stat_time FROM session_record WHERE common_recv_time >= toDateTime(@start) AND common_recv_time < toDateTime(@end) AND (common_client_ip = '5.32.144.55') AND session_record.common_vsys_id IN (32, 1, 27, 4) ORDER BY common_recv_time DESC LIMIT 0, 50 \ No newline at end of file
diff --git a/src/main/java/com/mesalab/common/utils/sqlparser/CondExpressionHelper.java b/src/main/java/com/mesalab/common/utils/sqlparser/CondExpressionHelper.java
index 27045141..21781506 100644
--- a/src/main/java/com/mesalab/common/utils/sqlparser/CondExpressionHelper.java
+++ b/src/main/java/com/mesalab/common/utils/sqlparser/CondExpressionHelper.java
@@ -1,8 +1,6 @@
package com.mesalab.common.utils.sqlparser;
import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import com.zdjizhi.utils.StringUtil;
import lombok.SneakyThrows;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
@@ -44,15 +42,14 @@ public class CondExpressionHelper {
* Desc: rewrite conditional expression, replace alias with exact value
*
* @param condExpr conditional expression after where
- * @param map key-alias, value-ExactValue
+ * @param aliasFields key-alias, value-ExactValue
* @return {@link Expression}
* @created by wWei
* @date 2023/3/2 09:52
*/
- public static Expression rewireCondExprBaseOnCK(String condExpr, Map<String, String> map) throws JSQLParserException {
+ public static Expression rewireCondExprBaseOnCK(String condExpr, Map<String, String> aliasFields, boolean updateTable, String table) throws JSQLParserException {
Expression expr = CCJSqlParserUtil.parseCondExpression(condExpr, false);
- ExprActualFieldParser exprParseAlias = new ExprActualFieldParser();
- exprParseAlias.addAliasFields(map);
+ ExprActualFieldParser exprParseAlias = new ExprActualFieldParser(aliasFields, updateTable, table);
expr.accept(exprParseAlias);
return expr;
}
@@ -93,15 +90,22 @@ public class CondExpressionHelper {
}
private static class ExprActualFieldParser extends ExpressionVisitorAdapter {
- private final Map<String, Object> map = Maps.newHashMap();
-
- public void addAliasFields(Map<String, String> aliasFields) {
- map.putAll(aliasFields);
+ private final Map<String, String> map;
+ private final Boolean updateTable;
+ private final String tableName;
+
+ private ExprActualFieldParser(Map<String, String> map, Boolean updateTable, String tableName) {
+ this.map = map;
+ this.updateTable = updateTable;
+ this.tableName = tableName;
}
@Override
public void visit(Column column) {
- if (StringUtil.isNotEmpty(column.getTable())) {
+ if (column.getTable() != null) {
+ if (updateTable) {
+ column.getTable().setName(tableName);
+ }
return;
}
String columnName = getFieldName(column);
diff --git a/src/main/java/com/mesalab/qgw/dialect/ClickHouseDialect.java b/src/main/java/com/mesalab/qgw/dialect/ClickHouseDialect.java
index a6c35bd7..d84334ec 100644
--- a/src/main/java/com/mesalab/qgw/dialect/ClickHouseDialect.java
+++ b/src/main/java/com/mesalab/qgw/dialect/ClickHouseDialect.java
@@ -541,7 +541,7 @@ public class ClickHouseDialect extends AbstractDataSourceDialect {
if (StringUtil.isEmpty(indexTables) || StringUtil.isEmpty(metadataService.getIndexKey(param.getTableName()))) {
return StringUtil.EMPTY;
}
- Expression expression = CondExpressionHelper.rewireCondExprBaseOnCK(param.getDbQuerySource().getExpr(), param.getDbQuerySource().getAliasFields());
+ Expression expression = CondExpressionHelper.rewireCondExprBaseOnCK(param.getDbQuerySource().getExpr(), param.getDbQuerySource().getAliasFields(), false, null);
List<String> actualFieldOfWhere = CondExpressionHelper.getDistinctFields(expression.toString());
table:
for (String tempIndexTable : indexTables) {
@@ -632,7 +632,7 @@ public class ClickHouseDialect extends AbstractDataSourceDialect {
StringBuilder subQuery = new StringBuilder();
subQuery.append("select " + columnName + " from ").append(tableName)
- .append(" where ").append(CondExpressionHelper.rewireCondExprBaseOnCK(param.getDbQuerySource().getExpr(), param.getDbQuerySource().getAliasFields()));
+ .append(" where ").append(CondExpressionHelper.rewireCondExprBaseOnCK(param.getDbQuerySource().getExpr(), param.getDbQuerySource().getAliasFields(), true, tableName));
//order by中元素存在别名时做替换处理
List<OrderByElement> listOrderElement = param.getDbQuerySource().getListOrderElement();
@@ -681,7 +681,7 @@ public class ClickHouseDialect extends AbstractDataSourceDialect {
}
for (OrderByElement order : listOrderElement) {
OrderByElement orderByElement = new OrderByElement();
- Expression expression = CondExpressionHelper.rewireCondExprBaseOnCK(String.valueOf(order.getExpression()), param.getDbQuerySource().getAliasFields());
+ Expression expression = CondExpressionHelper.rewireCondExprBaseOnCK(String.valueOf(order.getExpression()), param.getDbQuerySource().getAliasFields(), false, null);
orderByElement.setExpression(expression);
List<String> fields = CondExpressionHelper.getDistinctFieldsOfOrderBy(orderByElement);
for (String field : fields) {