[ 
https://issues.apache.org/jira/browse/CALCITE-3226?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

jin xing updated CALCITE-3226:
------------------------------
    Description: 
In current implementation of {{RelBuilder::scan}} 
([https://github.com/apache/calcite/blob/master/core/src/main/java/org/apache/calcite/tools/RelBuilder.java#L1048]),
 the alias can be derived and recorded into {{Frame}} only when the {{RelNode}} 
is a {{TableScan}} 
([https://github.com/apache/calcite/blob/master/core/src/main/java/org/apache/calcite/tools/RelBuilder.java#L2754]).
 But when {{RelBuilder::scan}} from an expanded view, the node is not a 
{{TableScan}} and the alias is not kept.  Below test failed - we cannot 
reference a field by alias "MYVIEW".
{code:java}
  @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"),           // Exception 
thrown from here.
                  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));
    }
  }
{code}

  was:
In current implementation of {{RelBuilder::scan}} 
([https://github.com/apache/calcite/blob/master/core/src/main/java/org/apache/calcite/tools/RelBuilder.java#L1048]),
 the alias can be derived and recorded into {{Frame}} only when the {{RelNode}} 
is a {{TableScan}} 
([https://github.com/apache/calcite/blob/master/core/src/main/java/org/apache/calcite/tools/RelBuilder.java#L2754]).
 But when {{RelBuilder::scan}} from an expanded view, the node is not a 
{{TableScan}} and the alias is not kept.  Below test failed - we cannot 
reference a field by alias "MYVIEW".
{code:java}
  @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));
    }
  }
{code}


> RelBuilder doesn't keep the alias when scan from an expanded view
> -----------------------------------------------------------------
>
>                 Key: CALCITE-3226
>                 URL: https://issues.apache.org/jira/browse/CALCITE-3226
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>            Reporter: jin xing
>            Priority: Major
>              Labels: pull-request-available
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> In current implementation of {{RelBuilder::scan}} 
> ([https://github.com/apache/calcite/blob/master/core/src/main/java/org/apache/calcite/tools/RelBuilder.java#L1048]),
>  the alias can be derived and recorded into {{Frame}} only when the 
> {{RelNode}} is a {{TableScan}} 
> ([https://github.com/apache/calcite/blob/master/core/src/main/java/org/apache/calcite/tools/RelBuilder.java#L2754]).
>  But when {{RelBuilder::scan}} from an expanded view, the node is not a 
> {{TableScan}} and the alias is not kept.  Below test failed - we cannot 
> reference a field by alias "MYVIEW".
> {code:java}
>   @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"),           // Exception 
> thrown from here.
>                   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));
>     }
>   }
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)

Reply via email to