This is an automated email from the ASF dual-hosted git repository. nic pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/kylin.git
commit 609ce65d5bfa18ee4021fde3d96c0b8b22c5382e Author: woyumen4597 <[email protected]> AuthorDate: Sat Feb 1 13:56:53 2020 +0800 KYLIN-4365 fix RDBMS pushdown with clause bug --- .../org/apache/kylin/query/util/PushDownUtil.java | 27 ++++++++++++++++------ .../apache/kylin/query/util/PushDownUtilTest.java | 7 ++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/query/src/main/java/org/apache/kylin/query/util/PushDownUtil.java b/query/src/main/java/org/apache/kylin/query/util/PushDownUtil.java index 4d69272..2167fdd 100644 --- a/query/src/main/java/org/apache/kylin/query/util/PushDownUtil.java +++ b/query/src/main/java/org/apache/kylin/query/util/PushDownUtil.java @@ -24,7 +24,6 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; - import org.apache.calcite.sql.SqlBasicCall; import org.apache.calcite.sql.SqlCall; import org.apache.calcite.sql.SqlDataTypeSpec; @@ -41,16 +40,14 @@ import org.apache.calcite.sql.SqlWith; import org.apache.calcite.sql.SqlWithItem; import org.apache.calcite.sql.parser.SqlParseException; import org.apache.calcite.sql.util.SqlVisitor; - import org.apache.commons.lang.text.StrBuilder; - import org.apache.kylin.common.util.Pair; import org.apache.kylin.metadata.model.tool.CalciteParser; import org.apache.kylin.metadata.querymeta.SelectedColumnMeta; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.collect.Lists; public class PushDownUtil { private static final Logger logger = LoggerFactory.getLogger(PushDownUtil.class); @@ -103,8 +100,7 @@ public class PushDownUtil { StrBuilder afterConvert = new StrBuilder(inputSql); for (Pair<Integer, Integer> pos : tablesPos) { - String tableWithSchema = schema + "." + inputSql.substring(pos.getFirst(), - pos.getSecond()); + String tableWithSchema = schema + "." + inputSql.substring(pos.getFirst(), pos.getSecond()); afterConvert.replace(pos.getFirst(), pos.getSecond(), tableWithSchema); } return afterConvert.toString(); @@ -116,13 +112,26 @@ public class PushDownUtil { */ static class FromTablesVisitor implements SqlVisitor<SqlNode> { private List<SqlNode> tables; + private List<SqlNode> withTables; FromTablesVisitor() { this.tables = new ArrayList<>(); + this.withTables = new ArrayList<>(); } List<SqlNode> getTablesWithoutSchema() { - return tables; + List<SqlNode> sqlNodes = Lists.newArrayList(); + List<String> withs = Lists.newArrayList(); + for (SqlNode withTable : withTables) { + withs.add(((SqlIdentifier) withTable).names.get(0)); // with clause not allow database.table pattern + } + for (SqlNode table : tables) { + SqlIdentifier identifier = (SqlIdentifier) table; + if (!withs.contains(identifier.names.get(0))) { + sqlNodes.add(identifier); + } + } + return sqlNodes; } @Override @@ -156,6 +165,10 @@ public class PushDownUtil { } if (call instanceof SqlWith) { SqlWith sqlWith = (SqlWith) call; + List<SqlNode> list = sqlWith.withList.getList(); + for (SqlNode sqlNode : list) { + withTables.add(((SqlWithItem) sqlNode).name); + } sqlWith.body.accept(this); sqlWith.withList.accept(this); } diff --git a/query/src/test/java/org/apache/kylin/query/util/PushDownUtilTest.java b/query/src/test/java/org/apache/kylin/query/util/PushDownUtilTest.java index f23010e..81fb68b 100644 --- a/query/src/test/java/org/apache/kylin/query/util/PushDownUtilTest.java +++ b/query/src/test/java/org/apache/kylin/query/util/PushDownUtilTest.java @@ -124,4 +124,11 @@ public class PushDownUtilTest { "ORDER BY c_customer_id limit 5"; // Assert.assertEquals(expected, PushDownUtil.schemaCompletion(ori, "EDW")); } + + @Test + public void testWithSyntax2() throws SqlParseException { + String origin = "with tmp as (select id from a),tmp2 as (select * from b) select * from tmp,tmp2 where tmp.id = tmp2.id"; + String expected = "with tmp as (select id from ssb.a),tmp2 as (select * from ssb.b) select * from tmp,tmp2 where tmp.id = tmp2.id"; + Assert.assertEquals(expected, PushDownUtil.schemaCompletion(origin, "ssb")); + } }
