From a79baa731564c541511405abd8abd0358466fa94 Mon Sep 17 00:00:00 2001 From: jzl18thu Date: Mon, 22 Jul 2024 19:32:31 +0800 Subject: [PATCH 1/6] feat(core): rename duplicate same columns --- .../logical/generator/QueryGenerator.java | 2 +- .../engine/logical/utils/OperatorUtils.java | 17 ++++--- .../iginx/engine/logical/utils/PathUtils.java | 16 +++--- .../naive/NaiveOperatorMemoryExecutor.java | 7 ++- .../execute/stream/RenameLazyStream.java | 5 +- .../iginx/engine/shared/data/read/Header.java | 16 ++++-- .../engine/shared/expr/BaseExpression.java | 2 +- .../engine/shared/expr/BinaryExpression.java | 2 +- .../engine/shared/expr/BracketExpression.java | 2 +- .../shared/expr/ConstantExpression.java | 2 +- .../shared/expr/MultipleExpression.java | 2 +- .../engine/shared/expr/UnaryExpression.java | 2 +- .../iginx/engine/shared/operator/Rename.java | 20 ++++---- .../sql/statement/frompart/CteFromPart.java | 10 ++-- .../sql/statement/frompart/FromPart.java | 4 +- .../sql/statement/frompart/PathFromPart.java | 9 ++-- .../frompart/ShowColumnsFromPart.java | 11 ++-- .../statement/frompart/SubQueryFromPart.java | 4 +- .../select/BinarySelectStatement.java | 4 +- .../select/CommonTableExpression.java | 10 ++-- .../sql/statement/select/SelectStatement.java | 4 +- .../select/UnarySelectStatement.java | 51 +++++++++++++------ .../optimizer/FilterPushDownOptimizer.java | 16 +++--- .../optimizer/rules/ColumnPruningRule.java | 7 +-- .../rules/FilterPushDownRenameRule.java | 8 +-- .../rules/FunctionDistinctEliminateRule.java | 5 +- .../RowTransformConstantFoldingRule.java | 8 +-- .../integration/func/sql/SQLSessionIT.java | 15 ++++++ 28 files changed, 149 insertions(+), 112 deletions(-) diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/generator/QueryGenerator.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/generator/QueryGenerator.java index 439500ab9e..b8da6eafa3 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/generator/QueryGenerator.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/generator/QueryGenerator.java @@ -804,7 +804,7 @@ private static Operator buildReorder(UnarySelectStatement selectStatement, Opera * @return 添加了Rename操作符的根节点;如果没有AliasMap,返回原根节点 */ private static Operator buildRename(UnarySelectStatement selectStatement, Operator root) { - Map aliasMap = selectStatement.getSelectAliasMap(); + List> aliasMap = selectStatement.getSelectAliasMap(); if (!aliasMap.isEmpty()) { root = new Rename(new OperatorSource(root), aliasMap); } diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/utils/OperatorUtils.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/utils/OperatorUtils.java index efe872e189..f1d1a7c0cc 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/utils/OperatorUtils.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/utils/OperatorUtils.java @@ -42,11 +42,11 @@ import cn.edu.tsinghua.iginx.engine.shared.source.OperatorSource; import cn.edu.tsinghua.iginx.engine.shared.source.Source; import cn.edu.tsinghua.iginx.engine.shared.source.SourceType; +import cn.edu.tsinghua.iginx.utils.Pair; import cn.edu.tsinghua.iginx.utils.StringUtils; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; public class OperatorUtils { @@ -605,7 +605,7 @@ public static List getPatternFromOperatorChildren( Operator visitedOperator = visitedOperators.get(i); if (visitedOperator.getType() == OperatorType.Rename) { Rename rename = (Rename) visitedOperator; - Map aliasMap = rename.getAliasMap(); + List> aliasMap = rename.getAliasMap(); patterns = renamePattern(aliasMap, patterns); } } @@ -652,13 +652,14 @@ private static boolean isPatternMatched(String patternA, String patternB) { * @param patterns 要重命名的模式列表 * @return */ - private static List renamePattern(Map aliasMap, List patterns) { + private static List renamePattern( + List> aliasMap, List patterns) { List renamedPatterns = new ArrayList<>(); for (String pattern : patterns) { boolean matched = false; - for (Map.Entry entry : aliasMap.entrySet()) { - String oldPattern = entry.getKey().replace("*", "(.*)"); - String newPattern = entry.getValue().replace("*", "$1"); + for (Pair pair : aliasMap) { + String oldPattern = pair.k.replace("*", "(.*)"); + String newPattern = pair.v.replace("*", "$1"); if (pattern.matches(oldPattern)) { if (newPattern.contains("$1") && !oldPattern.contains("*")) { newPattern = newPattern.replace("$1", "*"); @@ -668,12 +669,12 @@ private static List renamePattern(Map aliasMap, List recoverRenamedPatterns( - Map aliasMap, List patterns) { + List> aliasMap, List patterns) { return patterns.stream() .map(pattern -> recoverRenamedPattern(aliasMap, pattern)) .collect(Collectors.toList()); } - public static String recoverRenamedPattern(Map aliasMap, String pattern) { - for (Map.Entry entry : aliasMap.entrySet()) { - String oldPattern = entry.getKey().replace("*", "$1"); // 通配符转换为正则的捕获组 - String newPattern = entry.getValue().replace("*", "(.*)"); // 使用反向引用保留原始匹配的部分 + public static String recoverRenamedPattern(List> aliasMap, String pattern) { + for (Pair pair : aliasMap) { + String oldPattern = pair.k.replace("*", "$1"); // 通配符转换为正则的捕获组 + String newPattern = pair.v.replace("*", "(.*)"); // 使用反向引用保留原始匹配的部分 if (pattern.matches(newPattern)) { // 如果旧模式中有通配符,但是新模式中没有,我们需要将新模式中的捕获组替换为通配符 if (oldPattern.contains("$1") && !newPattern.contains("*")) { @@ -85,9 +85,9 @@ public static String recoverRenamedPattern(Map aliasMap, String } return pattern.replaceAll(newPattern, oldPattern); } else if (newPattern.equals(pattern)) { - return entry.getKey(); + return pair.k; } else if (pattern.contains(".*") && newPattern.matches(StringUtils.reformatPath(pattern))) { - return entry.getKey(); + return pair.k; } } return pattern; diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/naive/NaiveOperatorMemoryExecutor.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/naive/NaiveOperatorMemoryExecutor.java index fca427c618..e3b07aac02 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/naive/NaiveOperatorMemoryExecutor.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/naive/NaiveOperatorMemoryExecutor.java @@ -474,10 +474,9 @@ private RowStream executeMappingTransform(MappingTransform mappingTransform, Tab private RowStream executeRename(Rename rename, Table table) { Header header = table.getHeader(); - Map aliasMap = rename.getAliasMap(); - - List ignorePatterns = rename.getIgnorePatterns(); - Header newHeader = header.renamedHeader(aliasMap, ignorePatterns); + List> aliasMap = rename.getAliasMap(); + Header newHeader = header.renamedHeader(aliasMap, rename.getIgnorePatterns()); + ; List rows = new ArrayList<>(); table diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/stream/RenameLazyStream.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/stream/RenameLazyStream.java index 568a119f96..cd899bd29c 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/stream/RenameLazyStream.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/stream/RenameLazyStream.java @@ -23,7 +23,6 @@ import cn.edu.tsinghua.iginx.engine.shared.data.read.Row; import cn.edu.tsinghua.iginx.engine.shared.data.read.RowStream; import cn.edu.tsinghua.iginx.engine.shared.operator.Rename; -import java.util.Map; public class RenameLazyStream extends UnaryLazyStream { @@ -40,9 +39,7 @@ public RenameLazyStream(Rename rename, RowStream stream) { public Header getHeader() throws PhysicalException { if (header == null) { Header header = stream.getHeader(); - Map aliasMap = rename.getAliasMap(); - - this.header = header.renamedHeader(aliasMap, rename.getIgnorePatterns()); + this.header = header.renamedHeader(rename.getAliasMap(), rename.getIgnorePatterns()); } return header; } diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java index e224a26fbb..4743aa006e 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java @@ -127,7 +127,15 @@ public boolean equals(Object o) { && Objects.equals(indexMap, header.indexMap); } - public Header renamedHeader(Map aliasMap, List ignorePatterns) { + // public Header renameHeaderDuplicateColumns(List> aliasMap, List + // ignorePatterns) { + // List newFields = new ArrayList<>(); + // + // + // return new Header(getKey(), newFields); + // } + + public Header renamedHeader(List> aliasMap, List ignorePatterns) { List newFields = new ArrayList<>(); fields.forEach( field -> { @@ -139,8 +147,9 @@ public Header renamedHeader(Map aliasMap, List ignorePat } } String alias = ""; - for (String oldPattern : aliasMap.keySet()) { - String newPattern = aliasMap.get(oldPattern); + for (Pair pair : aliasMap) { + String oldPattern = pair.k; + String newPattern = pair.v; if (oldPattern.equals("*") && newPattern.endsWith(".*")) { String newPrefix = newPattern.substring(0, newPattern.length() - 1); alias = newPrefix + field.getName(); @@ -153,6 +162,7 @@ public Header renamedHeader(Map aliasMap, List ignorePat break; } else if (oldPattern.equals(field.getFullName())) { alias = newPattern; + aliasMap.remove(pair); break; } else { if (StringUtils.match(field.getName(), oldPattern)) { diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/BaseExpression.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/BaseExpression.java index 4d7240f2da..852af9629d 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/BaseExpression.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/BaseExpression.java @@ -52,7 +52,7 @@ public ExpressionType getType() { @Override public boolean hasAlias() { - return alias != null && !alias.equals(""); + return alias != null && !alias.isEmpty(); } @Override diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/BinaryExpression.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/BinaryExpression.java index 9b5dc92226..6a6d8009b0 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/BinaryExpression.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/BinaryExpression.java @@ -77,7 +77,7 @@ public ExpressionType getType() { @Override public boolean hasAlias() { - return alias != null && !alias.equals(""); + return alias != null && !alias.isEmpty(); } @Override diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/BracketExpression.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/BracketExpression.java index 4ee402ef12..9695abea76 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/BracketExpression.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/BracketExpression.java @@ -52,7 +52,7 @@ public ExpressionType getType() { @Override public boolean hasAlias() { - return alias != null && !alias.equals(""); + return alias != null && !alias.isEmpty(); } @Override diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/ConstantExpression.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/ConstantExpression.java index 4377c4ec78..86015e072a 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/ConstantExpression.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/ConstantExpression.java @@ -52,7 +52,7 @@ public ExpressionType getType() { @Override public boolean hasAlias() { - return alias != null && !alias.equals(""); + return alias != null && !alias.isEmpty(); } @Override diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/MultipleExpression.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/MultipleExpression.java index 2df31649bf..8f73f354f4 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/MultipleExpression.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/MultipleExpression.java @@ -92,7 +92,7 @@ public ExpressionType getType() { @Override public boolean hasAlias() { - return alias != null && !alias.equals(""); + return alias != null && !alias.isEmpty(); } @Override diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/UnaryExpression.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/UnaryExpression.java index e3ad98fb39..de051e6478 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/UnaryExpression.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/expr/UnaryExpression.java @@ -58,7 +58,7 @@ public ExpressionType getType() { @Override public boolean hasAlias() { - return alias != null && !alias.equals(""); + return alias != null && !alias.isEmpty(); } @Override diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/operator/Rename.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/operator/Rename.java index 0539de7378..5404ef2d7f 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/operator/Rename.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/operator/Rename.java @@ -20,22 +20,21 @@ import cn.edu.tsinghua.iginx.engine.shared.operator.type.OperatorType; import cn.edu.tsinghua.iginx.engine.shared.source.Source; +import cn.edu.tsinghua.iginx.utils.Pair; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; public class Rename extends AbstractUnaryOperator { - private final Map aliasMap; + private final List> aliasMap; private final List ignorePatterns; // 不进行重命名的列 - public Rename(Source source, Map aliasMap) { + public Rename(Source source, List> aliasMap) { this(source, aliasMap, new ArrayList<>()); } - public Rename(Source source, Map aliasMap, List ignorePatterns) { + public Rename(Source source, List> aliasMap, List ignorePatterns) { super(OperatorType.Rename, source); if (aliasMap == null) { throw new IllegalArgumentException("aliasMap shouldn't be null"); @@ -44,7 +43,7 @@ public Rename(Source source, Map aliasMap, List ignorePa this.ignorePatterns = ignorePatterns; } - public Map getAliasMap() { + public List> getAliasMap() { return aliasMap; } @@ -54,12 +53,12 @@ public List getIgnorePatterns() { @Override public Operator copy() { - return new Rename(getSource().copy(), new HashMap<>(aliasMap)); + return new Rename(getSource().copy(), new ArrayList<>(aliasMap)); } @Override public UnaryOperator copyWithSource(Source source) { - return new Rename(source, new HashMap<>(aliasMap)); + return new Rename(source, new ArrayList<>(aliasMap)); } @Override @@ -67,11 +66,10 @@ public String getInfo() { StringBuilder builder = new StringBuilder(); builder.append("AliasMap: "); aliasMap.forEach( - (k, v) -> builder.append("(").append(k).append(", ").append(v).append(")").append(",")); + p -> builder.append("(").append(p.k).append(", ").append(p.v).append(")").append(",")); builder.deleteCharAt(builder.length() - 1); if (!ignorePatterns.isEmpty()) { - builder.append(", IgnorePatterns: "); - builder.append(ignorePatterns); + builder.append(", IgnorePatterns: ").append(ignorePatterns); } return builder.toString(); } diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/CteFromPart.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/CteFromPart.java index 1b63e0126a..2bcf30ae56 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/CteFromPart.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/CteFromPart.java @@ -24,11 +24,10 @@ import cn.edu.tsinghua.iginx.engine.shared.operator.Operator; import cn.edu.tsinghua.iginx.sql.statement.frompart.join.JoinCondition; import cn.edu.tsinghua.iginx.sql.statement.select.CommonTableExpression; +import cn.edu.tsinghua.iginx.utils.Pair; import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.List; -import java.util.Map; public class CteFromPart implements FromPart { @@ -60,10 +59,9 @@ public boolean hasAlias() { } @Override - public Map getAliasMap() { - Map aliasMap = new HashMap<>(); - aliasMap.put(cte.getName() + ALL_PATH_SUFFIX, alias + ALL_PATH_SUFFIX); - return aliasMap; + public List> getAliasMap() { + return Collections.singletonList( + new Pair<>(cte.getName() + ALL_PATH_SUFFIX, alias + ALL_PATH_SUFFIX)); } @Override diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/FromPart.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/FromPart.java index a7a66ebf47..65e6863a24 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/FromPart.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/FromPart.java @@ -19,14 +19,14 @@ package cn.edu.tsinghua.iginx.sql.statement.frompart; import cn.edu.tsinghua.iginx.sql.statement.frompart.join.JoinCondition; +import cn.edu.tsinghua.iginx.utils.Pair; import java.util.List; -import java.util.Map; public interface FromPart { FromPartType getType(); - Map getAliasMap(); + List> getAliasMap(); boolean hasAlias(); diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/PathFromPart.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/PathFromPart.java index 5f2dd9360e..addeb7a25c 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/PathFromPart.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/PathFromPart.java @@ -22,10 +22,9 @@ import cn.edu.tsinghua.iginx.engine.shared.Constants; import cn.edu.tsinghua.iginx.sql.statement.frompart.join.JoinCondition; +import cn.edu.tsinghua.iginx.utils.Pair; import java.util.Collections; -import java.util.HashMap; import java.util.List; -import java.util.Map; public class PathFromPart implements FromPart { @@ -53,10 +52,8 @@ public FromPartType getType() { } @Override - public Map getAliasMap() { - Map aliasMap = new HashMap<>(); - aliasMap.put(path + ALL_PATH_SUFFIX, alias + ALL_PATH_SUFFIX); - return aliasMap; + public List> getAliasMap() { + return Collections.singletonList(new Pair<>(path + ALL_PATH_SUFFIX, alias + ALL_PATH_SUFFIX)); } @Override diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/ShowColumnsFromPart.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/ShowColumnsFromPart.java index 8c18b32d48..f4cdf2adfa 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/ShowColumnsFromPart.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/ShowColumnsFromPart.java @@ -21,12 +21,11 @@ import cn.edu.tsinghua.iginx.engine.shared.Constants; import cn.edu.tsinghua.iginx.sql.statement.ShowColumnsStatement; import cn.edu.tsinghua.iginx.sql.statement.frompart.join.JoinCondition; +import cn.edu.tsinghua.iginx.utils.Pair; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.HashMap; import java.util.List; -import java.util.Map; public class ShowColumnsFromPart implements FromPart { @@ -48,10 +47,10 @@ public ShowColumnsStatement getShowColumnsStatement() { } @Override - public Map getAliasMap() { - Map aliasMap = new HashMap<>(); - aliasMap.put("path", alias + ".path"); - aliasMap.put("type", alias + ".type"); + public List> getAliasMap() { + List> aliasMap = new ArrayList<>(2); + aliasMap.add(new Pair<>("path", alias + ".path")); + aliasMap.add(new Pair<>("type", alias + ".type")); return aliasMap; } diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/SubQueryFromPart.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/SubQueryFromPart.java index ed45058b51..45286efe7f 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/SubQueryFromPart.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/SubQueryFromPart.java @@ -23,8 +23,8 @@ import cn.edu.tsinghua.iginx.sql.statement.select.BinarySelectStatement; import cn.edu.tsinghua.iginx.sql.statement.select.SelectStatement; import cn.edu.tsinghua.iginx.sql.statement.select.UnarySelectStatement; +import cn.edu.tsinghua.iginx.utils.Pair; import java.util.List; -import java.util.Map; public class SubQueryFromPart implements FromPart { @@ -60,7 +60,7 @@ public FromPartType getType() { } @Override - public Map getAliasMap() { + public List> getAliasMap() { return subQuery.getSubQueryAliasMap(alias); } diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/BinarySelectStatement.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/BinarySelectStatement.java index aec06e51b8..c0ac53df05 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/BinarySelectStatement.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/BinarySelectStatement.java @@ -20,10 +20,10 @@ import cn.edu.tsinghua.iginx.engine.shared.expr.Expression; import cn.edu.tsinghua.iginx.engine.shared.operator.type.OperatorType; +import cn.edu.tsinghua.iginx.utils.Pair; import java.util.ArrayList; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; public class BinarySelectStatement extends SelectStatement { @@ -93,7 +93,7 @@ public void initFreeVariables() { } @Override - public Map getSubQueryAliasMap(String alias) { + public List> getSubQueryAliasMap(String alias) { return leftQuery.getSubQueryAliasMap(alias); } } diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/CommonTableExpression.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/CommonTableExpression.java index 52a1438823..10bc099c11 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/CommonTableExpression.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/CommonTableExpression.java @@ -21,10 +21,10 @@ import cn.edu.tsinghua.iginx.engine.shared.expr.Expression; import cn.edu.tsinghua.iginx.engine.shared.operator.Operator; import cn.edu.tsinghua.iginx.sql.SQLConstant; +import cn.edu.tsinghua.iginx.utils.Pair; +import java.util.ArrayList; import java.util.Collections; -import java.util.HashMap; import java.util.List; -import java.util.Map; public class CommonTableExpression { @@ -66,16 +66,16 @@ public void setRoot(Operator root) { this.root = root; } - public Map getAliasMap() { + public List> getAliasMap() { if (columns.isEmpty()) { return statement.getSubQueryAliasMap(name); } else { - Map aliasMap = new HashMap<>(); + List> aliasMap = new ArrayList<>(columns.size()); for (int i = 0; i < columns.size(); i++) { Expression expression = statement.getExpressions().get(i); String originName = expression.hasAlias() ? expression.getAlias() : expression.getColumnName(); - aliasMap.put(originName, name + SQLConstant.DOT + columns.get(i)); + aliasMap.add(new Pair<>(originName, name + SQLConstant.DOT + columns.get(i))); } return aliasMap; } diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/SelectStatement.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/SelectStatement.java index 10a9eba5c8..6c0b3aa751 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/SelectStatement.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/SelectStatement.java @@ -23,10 +23,10 @@ import cn.edu.tsinghua.iginx.sql.statement.StatementType; import cn.edu.tsinghua.iginx.sql.statement.select.subclause.LimitClause; import cn.edu.tsinghua.iginx.sql.statement.select.subclause.OrderByClause; +import cn.edu.tsinghua.iginx.utils.Pair; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Set; public abstract class SelectStatement extends DataStatement { @@ -135,7 +135,7 @@ public void addFreeVariable(String freeVariable) { public abstract void initFreeVariables(); - public abstract Map getSubQueryAliasMap(String alias); + public abstract List> getSubQueryAliasMap(String alias); public enum SelectStatementType { UNARY, diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/UnarySelectStatement.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/UnarySelectStatement.java index 32dedde8c1..4888480b9e 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/UnarySelectStatement.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/UnarySelectStatement.java @@ -38,8 +38,18 @@ import cn.edu.tsinghua.iginx.sql.statement.frompart.SubQueryFromPart; import cn.edu.tsinghua.iginx.sql.statement.select.subclause.*; import cn.edu.tsinghua.iginx.thrift.AggregateType; +import cn.edu.tsinghua.iginx.utils.Pair; import cn.edu.tsinghua.iginx.utils.StringUtils; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Queue; +import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; public class UnarySelectStatement extends SelectStatement { @@ -425,44 +435,53 @@ public void setQueryType(QueryType queryType) { @Override public List getExpressions() { - List expressions = new ArrayList<>(); - expressions.addAll(selectClause.getExpressions()); - return expressions; + return new ArrayList<>(selectClause.getExpressions()); } public void addSelectClauseExpression(Expression expression) { selectClause.addExpression(expression); } - public Map getSelectAliasMap() { - Map aliasMap = new HashMap<>(); + public List> getSelectAliasMap() { + List> aliasMap = new ArrayList<>(); + AtomicBoolean hasAlias = new AtomicBoolean(false); getExpressions() .forEach( expression -> { if (expression.hasAlias()) { - aliasMap.put(expression.getColumnName(), expression.getAlias()); + aliasMap.add(new Pair<>(expression.getColumnName(), expression.getAlias())); + hasAlias.set(true); + } else { + aliasMap.add(new Pair<>(expression.getColumnName(), expression.getColumnName())); } }); - return aliasMap; + return hasAlias.get() ? aliasMap : Collections.emptyList(); } @Override - public Map getSubQueryAliasMap(String alias) { - Map aliasMap = new HashMap<>(); + public List> getSubQueryAliasMap(String alias) { + List> aliasMap = new ArrayList<>(); getExpressions() .forEach( expression -> { if (expression.hasAlias()) { - aliasMap.put(expression.getAlias(), alias + DOT + expression.getAlias()); + aliasMap.add( + new Pair<>(expression.getAlias(), alias + DOT + expression.getAlias())); } else { if (expression.getType().equals(Expression.ExpressionType.Binary) || expression.getType().equals(Expression.ExpressionType.Unary)) { - aliasMap.put( - expression.getColumnName(), - alias + DOT + L_PARENTHESES + expression.getColumnName() + R_PARENTHESES); + aliasMap.add( + new Pair<>( + expression.getColumnName(), + alias + + DOT + + L_PARENTHESES + + expression.getColumnName() + + R_PARENTHESES)); } else { - aliasMap.put( - expression.getColumnName(), alias + DOT + expression.getColumnName()); + aliasMap.add( + new Pair<>( + expression.getColumnName(), alias + DOT + expression.getColumnName())); } } }); diff --git a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/FilterPushDownOptimizer.java b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/FilterPushDownOptimizer.java index 73aed98b46..bad29ac80c 100644 --- a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/FilterPushDownOptimizer.java +++ b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/FilterPushDownOptimizer.java @@ -264,7 +264,7 @@ private boolean getRenameOperator( return isCorrectRoad; } - private Filter replacePathByRenameMap(Filter filter, Map renameMap) { + private Filter replacePathByRenameMap(Filter filter, List> renameMap) { switch (filter.getType()) { case Or: List orChildren = ((OrFilter) filter).getChildren(); @@ -282,8 +282,8 @@ private Filter replacePathByRenameMap(Filter filter, Map renameM break; case Value: String path = ((ValueFilter) filter).getPath(); - for (Map.Entry entry : renameMap.entrySet()) { - path = replacePathByRenameEntry(path, entry); + for (Pair pair : renameMap) { + path = replacePathByRenameEntry(path, pair); } return new ValueFilter( path, ((ValueFilter) filter).getOp(), ((ValueFilter) filter).getValue()); @@ -291,8 +291,8 @@ private Filter replacePathByRenameMap(Filter filter, Map renameM String pathA = ((PathFilter) filter).getPathA(); String pathB = ((PathFilter) filter).getPathB(); - for (Map.Entry entry : renameMap.entrySet()) { - pathA = replacePathByRenameEntry(pathA, entry); + for (Pair pair : renameMap) { + pathA = replacePathByRenameEntry(pathA, pair); } return new PathFilter(pathA, ((PathFilter) filter).getOp(), pathB); @@ -302,9 +302,9 @@ private Filter replacePathByRenameMap(Filter filter, Map renameM return filter; } - private String replacePathByRenameEntry(String path, Map.Entry entry) { - String nameBeforeRename = entry.getKey(); - String nameAfterRename = entry.getValue(); + private String replacePathByRenameEntry(String path, Pair pair) { + String nameBeforeRename = pair.k; + String nameAfterRename = pair.v; if (path.equals(nameAfterRename)) { return nameBeforeRename; } diff --git a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/ColumnPruningRule.java b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/ColumnPruningRule.java index 5583f00048..f949c8b8b7 100644 --- a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/ColumnPruningRule.java +++ b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/ColumnPruningRule.java @@ -42,6 +42,7 @@ import cn.edu.tsinghua.iginx.utils.StringUtils; import com.google.auto.service.AutoService; import java.util.*; +import java.util.regex.Pattern; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -117,7 +118,7 @@ private void collectColumns( } } else if (operator.getType() == OperatorType.Rename) { Rename rename = (Rename) operator; - Map aliasMap = rename.getAliasMap(); + List> aliasMap = rename.getAliasMap(); columns = new HashSet<>(PathUtils.recoverRenamedPatterns(aliasMap, new ArrayList<>(columns))); @@ -335,13 +336,13 @@ private void collectColumns( new ArrayList<>()); for (String column : columns) { for (String leftPattern : leftPatterns) { - if (OperatorUtils.covers(leftPattern, column)) { + if (Pattern.matches(StringUtils.reformatPath(leftPattern), column)) { leftColumns.add(column); break; } } for (String rightPattern : rightPatterns) { - if (OperatorUtils.covers(rightPattern, column)) { + if (Pattern.matches(StringUtils.reformatPath(rightPattern), column)) { rightColumns.add(column); break; } diff --git a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/FilterPushDownRenameRule.java b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/FilterPushDownRenameRule.java index 2d43357910..8993ac4a68 100644 --- a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/FilterPushDownRenameRule.java +++ b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/FilterPushDownRenameRule.java @@ -25,8 +25,9 @@ import cn.edu.tsinghua.iginx.engine.shared.operator.filter.*; import cn.edu.tsinghua.iginx.engine.shared.source.OperatorSource; import cn.edu.tsinghua.iginx.logical.optimizer.core.RuleCall; +import cn.edu.tsinghua.iginx.utils.Pair; import com.google.auto.service.AutoService; -import java.util.Map; +import java.util.List; @AutoService(Rule.class) public class FilterPushDownRenameRule extends Rule { @@ -56,7 +57,7 @@ public void onMatch(RuleCall call) { call.transformTo(rename); } - private Filter replacePathByRenameMap(Filter filter, Map renameMap) { + private Filter replacePathByRenameMap(Filter filter, List> renameMap) { Filter newFilter = filter.copy(); newFilter.accept( new FilterVisitor() { @@ -96,7 +97,8 @@ public void visit(ExprFilter filter) { return newFilter; } - private void replaceExpressionByRenameMap(Expression expression, Map renameMap) { + private void replaceExpressionByRenameMap( + Expression expression, List> renameMap) { expression.accept( new ExpressionVisitor() { @Override diff --git a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/FunctionDistinctEliminateRule.java b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/FunctionDistinctEliminateRule.java index 2a58bdd611..a5de437d5d 100644 --- a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/FunctionDistinctEliminateRule.java +++ b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/FunctionDistinctEliminateRule.java @@ -25,6 +25,7 @@ import cn.edu.tsinghua.iginx.engine.shared.operator.type.OperatorType; import cn.edu.tsinghua.iginx.engine.shared.source.OperatorSource; import cn.edu.tsinghua.iginx.logical.optimizer.core.RuleCall; +import cn.edu.tsinghua.iginx.utils.Pair; import com.google.auto.service.AutoService; import java.util.*; @@ -67,13 +68,13 @@ public boolean matches(RuleCall call) { public void onMatch(RuleCall call) { AbstractUnaryOperator unaryOperator = (AbstractUnaryOperator) call.getMatchedRoot(); List functionCallList = getFunctionCallList(unaryOperator); - Map renameMap = new HashMap<>(); + List> renameMap = new ArrayList<>(); // 将函数中的distinct去掉 for (FunctionCall functionCall : functionCallList) { String oldName = functionCall.getFunctionStr(); functionCall.getParams().setDistinct(false); - renameMap.put(functionCall.getFunctionStr(), oldName); + renameMap.add(new Pair<>(functionCall.getFunctionStr(), oldName)); } // 添加一个rename节点,将不带distinct的函数结果重命名为原来的函数名,否则显示与用户输入的SQL不一致 diff --git a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/RowTransformConstantFoldingRule.java b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/RowTransformConstantFoldingRule.java index 5ffca61e34..9d48aaa6b5 100644 --- a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/RowTransformConstantFoldingRule.java +++ b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/RowTransformConstantFoldingRule.java @@ -25,10 +25,10 @@ import cn.edu.tsinghua.iginx.engine.shared.operator.RowTransform; import cn.edu.tsinghua.iginx.engine.shared.source.OperatorSource; import cn.edu.tsinghua.iginx.logical.optimizer.core.RuleCall; +import cn.edu.tsinghua.iginx.utils.Pair; import com.google.auto.service.AutoService; -import java.util.HashMap; +import java.util.ArrayList; import java.util.List; -import java.util.Map; @AutoService(Rule.class) public class RowTransformConstantFoldingRule extends Rule { @@ -63,7 +63,7 @@ public boolean matches(RuleCall call) { public void onMatch(RuleCall call) { RowTransform rowTransform = (RowTransform) call.getMatchedRoot(); List functionCallList = rowTransform.getFunctionCallList(); - Map aliasMap = new HashMap<>(); + List> aliasMap = new ArrayList<>(); for (FunctionCall functionCall : functionCallList) { Expression expr = functionCall.getParams().getExpr(); @@ -74,7 +74,7 @@ public void onMatch(RuleCall call) { Expression foldedExpression = ExprUtils.foldExpression(flattenedExpression); functionCall.getParams().setExpr(foldedExpression); String newName = foldedExpression.getColumnName(); - aliasMap.put(newName, oldName); + aliasMap.add(new Pair<>(newName, oldName)); } } } diff --git a/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/sql/SQLSessionIT.java b/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/sql/SQLSessionIT.java index efb83e338f..ba29c93dd1 100644 --- a/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/sql/SQLSessionIT.java +++ b/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/sql/SQLSessionIT.java @@ -3242,6 +3242,21 @@ public void testAlias() { + "+----+-------------------------------+--------------------------+\n" + "Total line number = 10\n"; executor.executeAndCompare(statement, expected); + + // duplicate columns + statement = "SELECT s1 AS a, s1, s1 AS b, s2 AS c, s2 FROM us.d1 WHERE s1 > 50 AND s1 < 55;"; + expected = + "ResultSets:\n" + + "+---+--+--------+--+--+--------+\n" + + "|key| a|us.d1.s1| b| c|us.d1.s2|\n" + + "+---+--+--------+--+--+--------+\n" + + "| 51|51| 51|51|52| 52|\n" + + "| 52|52| 52|52|53| 53|\n" + + "| 53|53| 53|53|54| 54|\n" + + "| 54|54| 54|54|55| 55|\n" + + "+---+--+--------+--+--+--------+\n" + + "Total line number = 4\n"; + executor.executeAndCompare(statement, expected); } @Test From ffabb3a3ad2c0dd32bdbb2cdeb2fe6cfb835fc56 Mon Sep 17 00:00:00 2001 From: jzl18thu Date: Tue, 23 Jul 2024 09:34:11 +0800 Subject: [PATCH 2/6] fix TagIT --- .../naive/NaiveOperatorMemoryExecutor.java | 1 - .../iginx/engine/shared/data/read/Header.java | 109 ++++++++++-------- 2 files changed, 59 insertions(+), 51 deletions(-) diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/naive/NaiveOperatorMemoryExecutor.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/naive/NaiveOperatorMemoryExecutor.java index e3b07aac02..9bd5afc16a 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/naive/NaiveOperatorMemoryExecutor.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/naive/NaiveOperatorMemoryExecutor.java @@ -476,7 +476,6 @@ private RowStream executeRename(Rename rename, Table table) { Header header = table.getHeader(); List> aliasMap = rename.getAliasMap(); Header newHeader = header.renamedHeader(aliasMap, rename.getIgnorePatterns()); - ; List rows = new ArrayList<>(); table diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java index 4743aa006e..40f9a45c92 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java @@ -127,62 +127,71 @@ public boolean equals(Object o) { && Objects.equals(indexMap, header.indexMap); } - // public Header renameHeaderDuplicateColumns(List> aliasMap, List - // ignorePatterns) { - // List newFields = new ArrayList<>(); - // - // - // return new Header(getKey(), newFields); - // } - public Header renamedHeader(List> aliasMap, List ignorePatterns) { List newFields = new ArrayList<>(); - fields.forEach( - field -> { - // 如果列名在ignorePatterns中,对该列不执行rename - for (String ignorePattern : ignorePatterns) { - if (StringUtils.match(field.getName(), ignorePattern)) { - newFields.add(field); - return; - } + int size = getFieldSize(); + for (int i = 0; i < size; i++) { + Field field = fields.get(i); + // 如果列名在ignorePatterns中,对该列不执行rename + boolean ignore = false; + for (String ignorePattern : ignorePatterns) { + if (StringUtils.match(field.getName(), ignorePattern)) { + newFields.add(field); + ignore = true; + break; + } + } + if (ignore) { + continue; + } + String alias = ""; + for (Pair pair : aliasMap) { + String oldPattern = pair.k; + String newPattern = pair.v; + if (oldPattern.equals("*") && newPattern.endsWith(".*")) { + String newPrefix = newPattern.substring(0, newPattern.length() - 1); + alias = newPrefix + field.getName(); + } else if (oldPattern.endsWith(".*") && newPattern.endsWith(".*")) { + String oldPrefix = oldPattern.substring(0, oldPattern.length() - 1); + String newPrefix = newPattern.substring(0, newPattern.length() - 1); + if (field.getName().startsWith(oldPrefix)) { + alias = field.getName().replaceFirst(oldPrefix, newPrefix); } - String alias = ""; - for (Pair pair : aliasMap) { - String oldPattern = pair.k; - String newPattern = pair.v; - if (oldPattern.equals("*") && newPattern.endsWith(".*")) { - String newPrefix = newPattern.substring(0, newPattern.length() - 1); - alias = newPrefix + field.getName(); - } else if (oldPattern.endsWith(".*") && newPattern.endsWith(".*")) { - String oldPrefix = oldPattern.substring(0, oldPattern.length() - 1); - String newPrefix = newPattern.substring(0, newPattern.length() - 1); - if (field.getName().startsWith(oldPrefix)) { - alias = field.getName().replaceFirst(oldPrefix, newPrefix); - } - break; - } else if (oldPattern.equals(field.getFullName())) { - alias = newPattern; - aliasMap.remove(pair); - break; + break; + } else if (oldPattern.equals(field.getName())) { + alias = newPattern; + Set> tagSet = new HashSet<>(); + Field newField = i < size - 1 ? fields.get(i + 1) : null; + tagSet.add(field.getTags()); + while (newField != null + && oldPattern.equals(newField.getName()) + && !tagSet.contains(newField.getTags())) { + newFields.add(new Field(alias, field.getType(), field.getTags())); + field = newField; + i++; + newField = i < size - 1 ? fields.get(i + 1) : null; + tagSet.add(field.getTags()); + } + aliasMap.remove(pair); + break; + } else { + if (StringUtils.match(field.getName(), oldPattern)) { + if (newPattern.endsWith("." + oldPattern)) { + String prefix = newPattern.substring(0, newPattern.length() - oldPattern.length()); + alias = prefix + field.getName(); } else { - if (StringUtils.match(field.getName(), oldPattern)) { - if (newPattern.endsWith("." + oldPattern)) { - String prefix = - newPattern.substring(0, newPattern.length() - oldPattern.length()); - alias = prefix + field.getName(); - } else { - alias = newPattern; - } - break; - } + alias = newPattern; } + break; } - if (alias.isEmpty()) { - newFields.add(field); - } else { - newFields.add(new Field(alias, field.getType(), field.getTags())); - } - }); + } + } + if (alias.isEmpty()) { + newFields.add(field); + } else { + newFields.add(new Field(alias, field.getType(), field.getTags())); + } + } return new Header(getKey(), newFields); } From ffa36160c8d07ef7a003063d8e0a1c9ffd3daade Mon Sep 17 00:00:00 2001 From: jzl18thu Date: Tue, 23 Jul 2024 09:47:45 +0800 Subject: [PATCH 3/6] modify variable name --- .../iginx/engine/shared/data/read/Header.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java index 40f9a45c92..b7163e0825 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java @@ -161,15 +161,15 @@ public Header renamedHeader(List> aliasMap, List ig } else if (oldPattern.equals(field.getName())) { alias = newPattern; Set> tagSet = new HashSet<>(); - Field newField = i < size - 1 ? fields.get(i + 1) : null; + Field nextField = i < size - 1 ? fields.get(i + 1) : null; tagSet.add(field.getTags()); - while (newField != null - && oldPattern.equals(newField.getName()) - && !tagSet.contains(newField.getTags())) { + while (nextField != null + && oldPattern.equals(nextField.getName()) + && !tagSet.contains(nextField.getTags())) { newFields.add(new Field(alias, field.getType(), field.getTags())); - field = newField; + field = nextField; i++; - newField = i < size - 1 ? fields.get(i + 1) : null; + nextField = i < size - 1 ? fields.get(i + 1) : null; tagSet.add(field.getTags()); } aliasMap.remove(pair); From e726a51dd7a288514287006d553d4b4b1b9c9d42 Mon Sep 17 00:00:00 2001 From: jzl18thu Date: Tue, 23 Jul 2024 09:48:47 +0800 Subject: [PATCH 4/6] Update Header.java --- .../cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java index b7163e0825..a60b12fd51 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java @@ -163,6 +163,7 @@ public Header renamedHeader(List> aliasMap, List ig Set> tagSet = new HashSet<>(); Field nextField = i < size - 1 ? fields.get(i + 1) : null; tagSet.add(field.getTags()); + // 处理同一列但不同tag的情况 while (nextField != null && oldPattern.equals(nextField.getName()) && !tagSet.contains(nextField.getTags())) { From 2476c0d5269c7c79ab1f4db072ddcb18da689868 Mon Sep 17 00:00:00 2001 From: jzl18thu Date: Wed, 24 Jul 2024 14:45:48 +0800 Subject: [PATCH 5/6] add test --- .../iginx/integration/func/sql/SQLSessionIT.java | 4 ++-- .../tsinghua/iginx/integration/func/tag/TagIT.java | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/sql/SQLSessionIT.java b/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/sql/SQLSessionIT.java index ba29c93dd1..ba3fcbc96f 100644 --- a/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/sql/SQLSessionIT.java +++ b/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/sql/SQLSessionIT.java @@ -3244,11 +3244,11 @@ public void testAlias() { executor.executeAndCompare(statement, expected); // duplicate columns - statement = "SELECT s1 AS a, s1, s1 AS b, s2 AS c, s2 FROM us.d1 WHERE s1 > 50 AND s1 < 55;"; + statement = "SELECT s1 AS a, s1, s1 AS s1, s2 AS c, s2 FROM us.d1 WHERE s1 > 50 AND s1 < 55;"; expected = "ResultSets:\n" + "+---+--+--------+--+--+--------+\n" - + "|key| a|us.d1.s1| b| c|us.d1.s2|\n" + + "|key| a|us.d1.s1|s1| c|us.d1.s2|\n" + "+---+--+--------+--+--+--------+\n" + "| 51|51| 51|51|52| 52|\n" + "| 52|52| 52|52|53| 53|\n" diff --git a/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/tag/TagIT.java b/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/tag/TagIT.java index 87737b1270..7457b0dbfa 100644 --- a/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/tag/TagIT.java +++ b/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/tag/TagIT.java @@ -1341,6 +1341,18 @@ public void testAlias() { + "Total line number = 2\n"; executeAndCompare(statement, expected); + statement = "SELECT s AS ts, s AS ss FROM ah.hr02;"; + expected = + "ResultSets:\n" + + "+---+----+---------+----+---------+\n" + + "|key| ts|ts{t1=v1}| ss|ss{t1=v1}|\n" + + "+---+----+---------+----+---------+\n" + + "|100|true| null|true| null|\n" + + "|400|null| false|null| false|\n" + + "+---+----+---------+----+---------+\n" + + "Total line number = 2\n"; + executeAndCompare(statement, expected); + statement = "SELECT s FROM ah.hr02 AS result_set;"; expected = "ResultSets:\n" From f008d9fec89a8fc2436fc6d71e0f5f320d7c1777 Mon Sep 17 00:00:00 2001 From: jzl18thu Date: Wed, 24 Jul 2024 15:59:57 +0800 Subject: [PATCH 6/6] modify variable name --- .../logical/generator/QueryGenerator.java | 16 +++++------ .../engine/logical/utils/OperatorUtils.java | 12 ++++---- .../iginx/engine/logical/utils/PathUtils.java | 10 +++---- .../naive/NaiveOperatorMemoryExecutor.java | 4 +-- .../execute/stream/RenameLazyStream.java | 2 +- .../iginx/engine/shared/data/read/Header.java | 6 ++-- .../iginx/engine/shared/operator/Rename.java | 28 +++++++++---------- .../sql/statement/frompart/CteFromPart.java | 2 +- .../sql/statement/frompart/FromPart.java | 2 +- .../sql/statement/frompart/PathFromPart.java | 2 +- .../frompart/ShowColumnsFromPart.java | 10 +++---- .../statement/frompart/SubQueryFromPart.java | 4 +-- .../select/BinarySelectStatement.java | 4 +-- .../select/CommonTableExpression.java | 10 +++---- .../sql/statement/select/SelectStatement.java | 2 +- .../select/UnarySelectStatement.java | 22 +++++++-------- .../optimizer/FilterPushDownOptimizer.java | 2 +- .../optimizer/rules/ColumnPruningRule.java | 6 ++-- .../rules/FilterPushDownRenameRule.java | 2 +- .../RowTransformConstantFoldingRule.java | 6 ++-- .../integration/func/sql/SQLSessionIT.java | 6 ++-- 21 files changed, 78 insertions(+), 80 deletions(-) diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/generator/QueryGenerator.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/generator/QueryGenerator.java index b8da6eafa3..87576c0dbf 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/generator/QueryGenerator.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/generator/QueryGenerator.java @@ -113,7 +113,7 @@ protected Operator generateRoot(Statement statement) { .forEach( cte -> { Operator root = generateRoot(cte.getStatement()); - root = new Rename(new OperatorSource(root), cte.getAliasMap()); + root = new Rename(new OperatorSource(root), cte.getAliasList()); cte.setRoot(root); }); return generateRoot(selectStatement); @@ -382,7 +382,7 @@ private Operator initFromPart(UnarySelectStatement selectStatement) { throw new RuntimeException("Unknown FromPart type: " + fromPart.getType()); } if (fromPart.hasAlias()) { - root = new Rename(new OperatorSource(root), fromPart.getAliasMap()); + root = new Rename(new OperatorSource(root), fromPart.getAliasList()); } return root; } @@ -437,7 +437,7 @@ private Operator initFilterAndMergeFragmentsWithJoin(UnarySelectStatement select throw new RuntimeException("Unknown FromPart type: " + fromPart.getType()); } if (fromPart.hasAlias()) { - root = new Rename(new OperatorSource(root), fromPart.getAliasMap()); + root = new Rename(new OperatorSource(root), fromPart.getAliasList()); } joinList.add(root); }); @@ -797,16 +797,16 @@ private static Operator buildReorder(UnarySelectStatement selectStatement, Opera } /** - * 如果SelectStatement有AliasMap, 在root之上构建一个Rename操作符 + * 如果SelectStatement有AliasList, 在root之上构建一个Rename操作符 * * @param selectStatement Select上下文 * @param root 当前根节点 - * @return 添加了Rename操作符的根节点;如果没有AliasMap,返回原根节点 + * @return 添加了Rename操作符的根节点;如果没有AliasList,返回原根节点 */ private static Operator buildRename(UnarySelectStatement selectStatement, Operator root) { - List> aliasMap = selectStatement.getSelectAliasMap(); - if (!aliasMap.isEmpty()) { - root = new Rename(new OperatorSource(root), aliasMap); + List> aliasList = selectStatement.getSelectAliasList(); + if (!aliasList.isEmpty()) { + root = new Rename(new OperatorSource(root), aliasList); } return root; } diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/utils/OperatorUtils.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/utils/OperatorUtils.java index f1d1a7c0cc..76000d76ff 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/utils/OperatorUtils.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/utils/OperatorUtils.java @@ -334,7 +334,7 @@ private static Operator pushDownApply(Operator root, List correlatedVari root = new Rename( new OperatorSource(pushDownApply(apply, correlatedVariables)), - rename.getAliasMap(), + rename.getAliasList(), ignorePatterns); break; case CrossJoin: @@ -605,8 +605,8 @@ public static List getPatternFromOperatorChildren( Operator visitedOperator = visitedOperators.get(i); if (visitedOperator.getType() == OperatorType.Rename) { Rename rename = (Rename) visitedOperator; - List> aliasMap = rename.getAliasMap(); - patterns = renamePattern(aliasMap, patterns); + List> aliasList = rename.getAliasList(); + patterns = renamePattern(aliasList, patterns); } } return patterns; @@ -648,16 +648,16 @@ private static boolean isPatternMatched(String patternA, String patternB) { /** * 正向重命名模式列表中的pattern,将key中的pattern替换为value中的pattern * - * @param aliasMap 重命名规则, key为旧模式,value为新模式 + * @param aliasList 重命名规则, key为旧模式,value为新模式 * @param patterns 要重命名的模式列表 * @return */ private static List renamePattern( - List> aliasMap, List patterns) { + List> aliasList, List patterns) { List renamedPatterns = new ArrayList<>(); for (String pattern : patterns) { boolean matched = false; - for (Pair pair : aliasMap) { + for (Pair pair : aliasList) { String oldPattern = pair.k.replace("*", "(.*)"); String newPattern = pair.v.replace("*", "$1"); if (pattern.matches(oldPattern)) { diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/utils/PathUtils.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/utils/PathUtils.java index e62283a417..da35bce534 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/utils/PathUtils.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/logical/utils/PathUtils.java @@ -63,19 +63,19 @@ public static ColumnsInterval addSuffix(ColumnsInterval columnsInterval) { /** * 反向重命名模式列表中的模式 * - * @param aliasMap 重命名规则, key为旧模式,value为新模式,在这里我们要将新模式恢复为旧模式 + * @param aliasList 重命名规则, key为旧模式,value为新模式,在这里我们要将新模式恢复为旧模式 * @param patterns 要重命名的模式列表 * @return 重命名后的模式列表 */ public static List recoverRenamedPatterns( - List> aliasMap, List patterns) { + List> aliasList, List patterns) { return patterns.stream() - .map(pattern -> recoverRenamedPattern(aliasMap, pattern)) + .map(pattern -> recoverRenamedPattern(aliasList, pattern)) .collect(Collectors.toList()); } - public static String recoverRenamedPattern(List> aliasMap, String pattern) { - for (Pair pair : aliasMap) { + public static String recoverRenamedPattern(List> aliasList, String pattern) { + for (Pair pair : aliasList) { String oldPattern = pair.k.replace("*", "$1"); // 通配符转换为正则的捕获组 String newPattern = pair.v.replace("*", "(.*)"); // 使用反向引用保留原始匹配的部分 if (pattern.matches(newPattern)) { diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/naive/NaiveOperatorMemoryExecutor.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/naive/NaiveOperatorMemoryExecutor.java index 9bd5afc16a..6993594cce 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/naive/NaiveOperatorMemoryExecutor.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/naive/NaiveOperatorMemoryExecutor.java @@ -474,8 +474,8 @@ private RowStream executeMappingTransform(MappingTransform mappingTransform, Tab private RowStream executeRename(Rename rename, Table table) { Header header = table.getHeader(); - List> aliasMap = rename.getAliasMap(); - Header newHeader = header.renamedHeader(aliasMap, rename.getIgnorePatterns()); + List> aliasList = rename.getAliasList(); + Header newHeader = header.renamedHeader(aliasList, rename.getIgnorePatterns()); List rows = new ArrayList<>(); table diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/stream/RenameLazyStream.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/stream/RenameLazyStream.java index cd899bd29c..eddb89df0a 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/stream/RenameLazyStream.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/physical/memory/execute/stream/RenameLazyStream.java @@ -39,7 +39,7 @@ public RenameLazyStream(Rename rename, RowStream stream) { public Header getHeader() throws PhysicalException { if (header == null) { Header header = stream.getHeader(); - this.header = header.renamedHeader(rename.getAliasMap(), rename.getIgnorePatterns()); + this.header = header.renamedHeader(rename.getAliasList(), rename.getIgnorePatterns()); } return header; } diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java index a60b12fd51..44a07a114f 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/data/read/Header.java @@ -127,7 +127,7 @@ public boolean equals(Object o) { && Objects.equals(indexMap, header.indexMap); } - public Header renamedHeader(List> aliasMap, List ignorePatterns) { + public Header renamedHeader(List> aliasList, List ignorePatterns) { List newFields = new ArrayList<>(); int size = getFieldSize(); for (int i = 0; i < size; i++) { @@ -145,7 +145,7 @@ public Header renamedHeader(List> aliasMap, List ig continue; } String alias = ""; - for (Pair pair : aliasMap) { + for (Pair pair : aliasList) { String oldPattern = pair.k; String newPattern = pair.v; if (oldPattern.equals("*") && newPattern.endsWith(".*")) { @@ -173,7 +173,7 @@ public Header renamedHeader(List> aliasMap, List ig nextField = i < size - 1 ? fields.get(i + 1) : null; tagSet.add(field.getTags()); } - aliasMap.remove(pair); + aliasList.remove(pair); break; } else { if (StringUtils.match(field.getName(), oldPattern)) { diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/operator/Rename.java b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/operator/Rename.java index 5404ef2d7f..5a26576916 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/operator/Rename.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/engine/shared/operator/Rename.java @@ -26,25 +26,25 @@ public class Rename extends AbstractUnaryOperator { - private final List> aliasMap; + private final List> aliasList; private final List ignorePatterns; // 不进行重命名的列 - public Rename(Source source, List> aliasMap) { - this(source, aliasMap, new ArrayList<>()); + public Rename(Source source, List> aliasList) { + this(source, aliasList, new ArrayList<>()); } - public Rename(Source source, List> aliasMap, List ignorePatterns) { + public Rename(Source source, List> aliasList, List ignorePatterns) { super(OperatorType.Rename, source); - if (aliasMap == null) { - throw new IllegalArgumentException("aliasMap shouldn't be null"); + if (aliasList == null) { + throw new IllegalArgumentException("aliasList shouldn't be null"); } - this.aliasMap = aliasMap; + this.aliasList = aliasList; this.ignorePatterns = ignorePatterns; } - public List> getAliasMap() { - return aliasMap; + public List> getAliasList() { + return aliasList; } public List getIgnorePatterns() { @@ -53,19 +53,19 @@ public List getIgnorePatterns() { @Override public Operator copy() { - return new Rename(getSource().copy(), new ArrayList<>(aliasMap)); + return new Rename(getSource().copy(), new ArrayList<>(aliasList)); } @Override public UnaryOperator copyWithSource(Source source) { - return new Rename(source, new ArrayList<>(aliasMap)); + return new Rename(source, new ArrayList<>(aliasList)); } @Override public String getInfo() { StringBuilder builder = new StringBuilder(); - builder.append("AliasMap: "); - aliasMap.forEach( + builder.append("AliasList: "); + aliasList.forEach( p -> builder.append("(").append(p.k).append(", ").append(p.v).append(")").append(",")); builder.deleteCharAt(builder.length() - 1); if (!ignorePatterns.isEmpty()) { @@ -83,6 +83,6 @@ public boolean equals(Object object) { return false; } Rename rename = (Rename) object; - return aliasMap.equals(rename.aliasMap) && ignorePatterns.equals(rename.ignorePatterns); + return aliasList.equals(rename.aliasList) && ignorePatterns.equals(rename.ignorePatterns); } } diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/CteFromPart.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/CteFromPart.java index 2bcf30ae56..75840a9e5a 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/CteFromPart.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/CteFromPart.java @@ -59,7 +59,7 @@ public boolean hasAlias() { } @Override - public List> getAliasMap() { + public List> getAliasList() { return Collections.singletonList( new Pair<>(cte.getName() + ALL_PATH_SUFFIX, alias + ALL_PATH_SUFFIX)); } diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/FromPart.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/FromPart.java index 65e6863a24..4cbb2d5bc5 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/FromPart.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/FromPart.java @@ -26,7 +26,7 @@ public interface FromPart { FromPartType getType(); - List> getAliasMap(); + List> getAliasList(); boolean hasAlias(); diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/PathFromPart.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/PathFromPart.java index addeb7a25c..5a808ae6eb 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/PathFromPart.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/PathFromPart.java @@ -52,7 +52,7 @@ public FromPartType getType() { } @Override - public List> getAliasMap() { + public List> getAliasList() { return Collections.singletonList(new Pair<>(path + ALL_PATH_SUFFIX, alias + ALL_PATH_SUFFIX)); } diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/ShowColumnsFromPart.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/ShowColumnsFromPart.java index f4cdf2adfa..2a8b66d6a4 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/ShowColumnsFromPart.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/ShowColumnsFromPart.java @@ -47,11 +47,11 @@ public ShowColumnsStatement getShowColumnsStatement() { } @Override - public List> getAliasMap() { - List> aliasMap = new ArrayList<>(2); - aliasMap.add(new Pair<>("path", alias + ".path")); - aliasMap.add(new Pair<>("type", alias + ".type")); - return aliasMap; + public List> getAliasList() { + List> aliasList = new ArrayList<>(2); + aliasList.add(new Pair<>("path", alias + ".path")); + aliasList.add(new Pair<>("type", alias + ".type")); + return aliasList; } @Override diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/SubQueryFromPart.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/SubQueryFromPart.java index 45286efe7f..e3e75433d7 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/SubQueryFromPart.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/frompart/SubQueryFromPart.java @@ -60,8 +60,8 @@ public FromPartType getType() { } @Override - public List> getAliasMap() { - return subQuery.getSubQueryAliasMap(alias); + public List> getAliasList() { + return subQuery.getSubQueryAliasList(alias); } @Override diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/BinarySelectStatement.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/BinarySelectStatement.java index c0ac53df05..b7dbe5b082 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/BinarySelectStatement.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/BinarySelectStatement.java @@ -93,7 +93,7 @@ public void initFreeVariables() { } @Override - public List> getSubQueryAliasMap(String alias) { - return leftQuery.getSubQueryAliasMap(alias); + public List> getSubQueryAliasList(String alias) { + return leftQuery.getSubQueryAliasList(alias); } } diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/CommonTableExpression.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/CommonTableExpression.java index 10bc099c11..e08925a4d6 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/CommonTableExpression.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/CommonTableExpression.java @@ -66,18 +66,18 @@ public void setRoot(Operator root) { this.root = root; } - public List> getAliasMap() { + public List> getAliasList() { if (columns.isEmpty()) { - return statement.getSubQueryAliasMap(name); + return statement.getSubQueryAliasList(name); } else { - List> aliasMap = new ArrayList<>(columns.size()); + List> aliasList = new ArrayList<>(columns.size()); for (int i = 0; i < columns.size(); i++) { Expression expression = statement.getExpressions().get(i); String originName = expression.hasAlias() ? expression.getAlias() : expression.getColumnName(); - aliasMap.add(new Pair<>(originName, name + SQLConstant.DOT + columns.get(i))); + aliasList.add(new Pair<>(originName, name + SQLConstant.DOT + columns.get(i))); } - return aliasMap; + return aliasList; } } } diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/SelectStatement.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/SelectStatement.java index 6c0b3aa751..2c3e4ce3c3 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/SelectStatement.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/SelectStatement.java @@ -135,7 +135,7 @@ public void addFreeVariable(String freeVariable) { public abstract void initFreeVariables(); - public abstract List> getSubQueryAliasMap(String alias); + public abstract List> getSubQueryAliasList(String alias); public enum SelectStatementType { UNARY, diff --git a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/UnarySelectStatement.java b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/UnarySelectStatement.java index 4888480b9e..4e2821435f 100644 --- a/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/UnarySelectStatement.java +++ b/core/src/main/java/cn/edu/tsinghua/iginx/sql/statement/select/UnarySelectStatement.java @@ -442,35 +442,35 @@ public void addSelectClauseExpression(Expression expression) { selectClause.addExpression(expression); } - public List> getSelectAliasMap() { - List> aliasMap = new ArrayList<>(); + public List> getSelectAliasList() { + List> aliasList = new ArrayList<>(); AtomicBoolean hasAlias = new AtomicBoolean(false); getExpressions() .forEach( expression -> { if (expression.hasAlias()) { - aliasMap.add(new Pair<>(expression.getColumnName(), expression.getAlias())); + aliasList.add(new Pair<>(expression.getColumnName(), expression.getAlias())); hasAlias.set(true); } else { - aliasMap.add(new Pair<>(expression.getColumnName(), expression.getColumnName())); + aliasList.add(new Pair<>(expression.getColumnName(), expression.getColumnName())); } }); - return hasAlias.get() ? aliasMap : Collections.emptyList(); + return hasAlias.get() ? aliasList : Collections.emptyList(); } @Override - public List> getSubQueryAliasMap(String alias) { - List> aliasMap = new ArrayList<>(); + public List> getSubQueryAliasList(String alias) { + List> aliasList = new ArrayList<>(); getExpressions() .forEach( expression -> { if (expression.hasAlias()) { - aliasMap.add( + aliasList.add( new Pair<>(expression.getAlias(), alias + DOT + expression.getAlias())); } else { if (expression.getType().equals(Expression.ExpressionType.Binary) || expression.getType().equals(Expression.ExpressionType.Unary)) { - aliasMap.add( + aliasList.add( new Pair<>( expression.getColumnName(), alias @@ -479,13 +479,13 @@ public List> getSubQueryAliasMap(String alias) { + expression.getColumnName() + R_PARENTHESES)); } else { - aliasMap.add( + aliasList.add( new Pair<>( expression.getColumnName(), alias + DOT + expression.getColumnName())); } } }); - return aliasMap; + return aliasList; } public boolean needRowTransform() { diff --git a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/FilterPushDownOptimizer.java b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/FilterPushDownOptimizer.java index bad29ac80c..a87823fc87 100644 --- a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/FilterPushDownOptimizer.java +++ b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/FilterPushDownOptimizer.java @@ -108,7 +108,7 @@ private void pushDown(Select selectOperator) { getRenameOperator(selectOperator, project, renameList); Filter unrenamedFilter = filter.copy(); for (Operator rename : renameList) { - unrenamedFilter = replacePathByRenameMap(unrenamedFilter, ((Rename) rename).getAliasMap()); + unrenamedFilter = replacePathByRenameMap(unrenamedFilter, ((Rename) rename).getAliasList()); } // the same meta just call once. diff --git a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/ColumnPruningRule.java b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/ColumnPruningRule.java index f949c8b8b7..5497103508 100644 --- a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/ColumnPruningRule.java +++ b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/ColumnPruningRule.java @@ -18,8 +18,6 @@ package cn.edu.tsinghua.iginx.logical.optimizer.rules; -import static cn.edu.tsinghua.iginx.engine.logical.utils.PathUtils.*; - import cn.edu.tsinghua.iginx.engine.logical.utils.OperatorUtils; import cn.edu.tsinghua.iginx.engine.logical.utils.PathUtils; import cn.edu.tsinghua.iginx.engine.physical.memory.execute.utils.ExprUtils; @@ -118,9 +116,9 @@ private void collectColumns( } } else if (operator.getType() == OperatorType.Rename) { Rename rename = (Rename) operator; - List> aliasMap = rename.getAliasMap(); + List> aliasList = rename.getAliasList(); columns = - new HashSet<>(PathUtils.recoverRenamedPatterns(aliasMap, new ArrayList<>(columns))); + new HashSet<>(PathUtils.recoverRenamedPatterns(aliasList, new ArrayList<>(columns))); } else if (operator.getType() == OperatorType.GroupBy) { GroupBy groupBy = (GroupBy) operator; diff --git a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/FilterPushDownRenameRule.java b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/FilterPushDownRenameRule.java index 8993ac4a68..52a8849129 100644 --- a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/FilterPushDownRenameRule.java +++ b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/FilterPushDownRenameRule.java @@ -51,7 +51,7 @@ public boolean matches(RuleCall call) { public void onMatch(RuleCall call) { Select select = (Select) call.getMatchedRoot(); Rename rename = (Rename) ((OperatorSource) select.getSource()).getOperator(); - select.setFilter(replacePathByRenameMap(select.getFilter(), rename.getAliasMap())); + select.setFilter(replacePathByRenameMap(select.getFilter(), rename.getAliasList())); select.setSource(rename.getSource()); rename.setSource(new OperatorSource(select)); call.transformTo(rename); diff --git a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/RowTransformConstantFoldingRule.java b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/RowTransformConstantFoldingRule.java index 9d48aaa6b5..06018b972e 100644 --- a/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/RowTransformConstantFoldingRule.java +++ b/optimizer/src/main/java/cn/edu/tsinghua/iginx/logical/optimizer/rules/RowTransformConstantFoldingRule.java @@ -63,7 +63,7 @@ public boolean matches(RuleCall call) { public void onMatch(RuleCall call) { RowTransform rowTransform = (RowTransform) call.getMatchedRoot(); List functionCallList = rowTransform.getFunctionCallList(); - List> aliasMap = new ArrayList<>(); + List> aliasList = new ArrayList<>(); for (FunctionCall functionCall : functionCallList) { Expression expr = functionCall.getParams().getExpr(); @@ -74,13 +74,13 @@ public void onMatch(RuleCall call) { Expression foldedExpression = ExprUtils.foldExpression(flattenedExpression); functionCall.getParams().setExpr(foldedExpression); String newName = foldedExpression.getColumnName(); - aliasMap.add(new Pair<>(newName, oldName)); + aliasList.add(new Pair<>(newName, oldName)); } } } // 改完之后要在上面加一层Rename,因为常量折叠改变了表达式输出的列名,但是我们依然要用原列名 - Rename rename = new Rename(new OperatorSource(rowTransform), aliasMap); + Rename rename = new Rename(new OperatorSource(rowTransform), aliasList); call.transformTo(rename); } } diff --git a/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/sql/SQLSessionIT.java b/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/sql/SQLSessionIT.java index ba3fcbc96f..fbf0ef62a6 100644 --- a/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/sql/SQLSessionIT.java +++ b/test/src/test/java/cn/edu/tsinghua/iginx/integration/func/sql/SQLSessionIT.java @@ -6889,7 +6889,7 @@ public void testFilterFragmentOptimizer() { + "|Reorder | Reorder| Order: avg(bb)|\n" + "| +--GroupBy | GroupBy|GroupByCols: aa, FuncList(Name, FuncType): (avg, System), MappingType: SetMapping isDistinct: false|\n" + "| +--Select | Select| Filter: key > 2|\n" - + "| +--Rename | Rename| AliasMap: (us.d2.a, aa),(us.d2.b, bb)|\n" + + "| +--Rename | Rename| AliasList: (us.d2.a, aa),(us.d2.b, bb)|\n" + "| +--Reorder | Reorder| Order: us.d2.a,us.d2.b|\n" + "| +--Project | Project| Patterns: us.d2.a,us.d2.b|\n" + "| +--PathUnion| PathUnion| |\n" @@ -6915,7 +6915,7 @@ public void testFilterFragmentOptimizer() { + "|Reorder | Reorder| Order: count(*)|\n" + "| +--Downsample | Downsample| Precision: 20, SlideDistance: 20, TimeRange: [1000, 1100), FuncList(Name, FunctionType): (count, System), MappingType: SetMapping|\n" + "| +--Select | Select| Filter: (key >= 1000 && key < 1100)|\n" - + "| +--Rename | Rename| AliasMap: (avg(us.d1.s1), avg_s1),(sum(us.d1.s2), sum_s2)|\n" + + "| +--Rename | Rename| AliasList: (avg(us.d1.s1), avg_s1),(sum(us.d1.s2), sum_s2)|\n" + "| +--Reorder | Reorder| Order: avg(us.d1.s1),sum(us.d1.s2)|\n" + "| +--Downsample | Downsample|Precision: 10, SlideDistance: 10, TimeRange: [1000, 1100), FuncList(Name, FunctionType): (avg, System), (sum, System), MappingType: SetMapping|\n" + "| +--Select | Select| Filter: (key >= 1000 && key < 1100)|\n" @@ -6974,7 +6974,7 @@ public void testFilterFragmentOptimizer() { + "|Reorder | Reorder| Order: count(*)|\n" + "| +--Downsample | Downsample| Precision: 20, SlideDistance: 20, TimeRange: [1000, 1100), FuncList(Name, FunctionType): (count, System), MappingType: SetMapping|\n" + "| +--Select | Select| Filter: (key >= 1000 && key < 1100)|\n" - + "| +--Rename | Rename| AliasMap: (avg(us.d1.s1), avg_s1),(sum(us.d1.s2), sum_s2)|\n" + + "| +--Rename | Rename| AliasList: (avg(us.d1.s1), avg_s1),(sum(us.d1.s2), sum_s2)|\n" + "| +--Reorder | Reorder| Order: avg(us.d1.s1),sum(us.d1.s2)|\n" + "| +--Downsample | Downsample|Precision: 10, SlideDistance: 10, TimeRange: [1000, 1100), FuncList(Name, FunctionType): (avg, System), (sum, System), MappingType: SetMapping|\n" + "| +--Select | Select| Filter: (key >= 1000 && key < 1100)|\n"