This is an automated email from the ASF dual-hosted git repository.
rubenql pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/master by this push:
new 2678eb9 [CALCITE-3226] RelBuilder doesn't keep the alias when scan
from an expanded view (Jin Xing)
2678eb9 is described below
commit 2678eb9a867c6589208b60c2a07446c658d2d8c6
Author: jinxing <[email protected]>
AuthorDate: Fri Aug 2 11:03:20 2019 +0800
[CALCITE-3226] RelBuilder doesn't keep the alias when scan from an expanded
view (Jin Xing)
---
.../java/org/apache/calcite/tools/RelBuilder.java | 6 ++++
.../org/apache/calcite/test/RelBuilderTest.java | 37 ++++++++++++++++++++++
2 files changed, 43 insertions(+)
diff --git a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
index 239d58a..8db4c98 100644
--- a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
+++ b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
@@ -1058,6 +1058,12 @@ public class RelBuilder {
final RelNode scan = scanFactory.createScan(cluster, relOptTable);
push(scan);
rename(relOptTable.getRowType().getFieldNames());
+
+ // When the node is not a TableScan but from expansion,
+ // we need to explicitly add the alias.
+ if (!(scan instanceof TableScan)) {
+ as(Util.last(ImmutableList.copyOf(tableNames)));
+ }
return this;
}
diff --git a/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java
b/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java
index b7e38cc..c9c4c5a 100644
--- a/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java
+++ b/core/src/test/java/org/apache/calcite/test/RelBuilderTest.java
@@ -699,6 +699,18 @@ public class RelBuilderTest {
assertThat(root, hasTree(expected));
}
+ @Test public void testProjectWithAliasFromScan() {
+ final RelBuilder builder = RelBuilder.create(config().build());
+ RelNode root =
+ builder.scan("EMP")
+ .project(builder.field(1, "EMP", "ENAME"))
+ .build();
+ final String expected =
+ "LogicalProject(ENAME=[$1])\n"
+ + " LogicalTableScan(table=[[scott, EMP]])\n";
+ assertThat(root, hasTree(expected));
+ }
+
private void project1(int value, SqlTypeName sqlTypeName, String message,
String expected) {
final RelBuilder builder = RelBuilder.create(config().build());
RexBuilder rex = builder.getRexBuilder();
@@ -2763,6 +2775,31 @@ public class RelBuilderTest {
}
}
+ @Test public void testExpandViewShouldKeepAlias() throws SQLException {
+ try (Connection connection = DriverManager.getConnection("jdbc:calcite:"))
{
+ final Frameworks.ConfigBuilder configBuilder =
+ expandingConfig(connection);
+ final RelOptTable.ViewExpander viewExpander =
+ (RelOptTable.ViewExpander)
Frameworks.getPlanner(configBuilder.build());
+ final RelFactories.TableScanFactory tableScanFactory =
+ RelFactories.expandingScanFactory(viewExpander,
+ RelFactories.DEFAULT_TABLE_SCAN_FACTORY);
+ configBuilder.context(Contexts.of(tableScanFactory));
+ final RelBuilder builder = RelBuilder.create(configBuilder.build());
+ RelNode node =
+ builder.scan("MYVIEW")
+ .project(
+ builder.field(1, "MYVIEW", "EMPNO"),
+ builder.field(1, "MYVIEW", "ENAME"))
+ .build();
+ String expected =
+ "LogicalProject(EMPNO=[$0], ENAME=[$1])\n"
+ + " LogicalFilter(condition=[=(1, 1)])\n"
+ + " LogicalTableScan(table=[[scott, EMP]])\n";
+ assertThat(node, hasTree(expected));
+ }
+ }
+
@Test public void testExpandTable() throws SQLException {
final RelOptTable.ViewExpander viewExpander =
(rowType, queryString, schemaPath, viewPath) -> null;