As Andrei says, using "RelBuilder.alias(RexNode, String)” on an expression in 
RelBuilder.project works. But only most of the time - RelBuilder reserves the 
right to discard projects that do nothing more than rename columns.

Try using RelBuilder.rename. It will force creation of a Project even if it is 
the identity.

Ultimately there are no guarantees. If RelBuilder finds a way to simplify, it 
will simplify, even if that discards the Project that renamed fields.

Julian


> On Aug 23, 2018, at 3:43 PM, Andrei Sereda <[email protected]> wrote:
> 
> The following works for me
> 
> @Test public void relBuilderProjectAlias() throws Exception {
>  final RelBuilder builder = RelBuilder.create(config().build());
>  final RelNode root =
>      builder.scan("EMP")
>          .project(
>                    builder.alias(builder.field("EMPNO"), "aaa"),
>                    builder.alias(builder.field("ENAME"), "bbb")
>                  )
>          .build();
> 
>  try (PreparedStatement stm = RelRunners.run(root);
>      ResultSet rset = stm.executeQuery();
>  ) {
>    while (rset.next()) {
>      System.out.printf("aaa=%s bbb=%s\n",
>          rset.getString("aaa"),
>          rset.getString("bbb"));
>    }
>  }
> }
> 
> 
> aaa=7369 bbb=SMITH
> aaa=7499 bbb=ALLEN
> aaa=7521 bbb=WARD
> 
> 
> On Thu, Aug 23, 2018 at 5:54 PM Anand Gupta <[email protected]> wrote:
> 
>> Hi All,
>> 
>> I am using RelBuilder to build relational expression and then converting it
>> to a SQL query using Rel2SqlConverter. I skimmed through the examples of
>> RelBuilder
>> <
>> https://github.com/apache/calcite/blob/master/core/src/test/java/org/apache/calcite/examples/RelBuilderExample.java
>>> 
>> but
>> couldn't figure out a way to assign an alias to a column name.
>> 
>> I am able to select columns from a table and create a query like
>> 
>> "select col1, col2, col3 from table1"
>> 
>> However, I want to do something like
>> 
>> "select col1 as t1, col2 as t2, col3 as n9 from table1"
>> 
>> Ideally, there should be a way to set an alias while projecting the
>> columns. However, I couldn't find it. I found two relevant methods "as" and
>> "alias" in the code. However, couldn't make them work - "as" only works on
>> table and "alias" refer to table name alias.
>> 
>> Any suggestion on how to make this work will be appreciated.
>> 
>> Thanks,
>> -A
>> 

Reply via email to