You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@calcite.apache.org by "Julian Hyde (Jira)" <ji...@apache.org> on 2019/10/24 23:33:00 UTC

[jira] [Resolved] (CALCITE-3440) RelToSqlConverter does not properly alias ambiguous ORDER BY

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

Julian Hyde resolved CALCITE-3440.
----------------------------------
    Fix Version/s: 1.22.0
       Resolution: Fixed

Fixed in [8fd8fcca0|https://github.com/apache/calcite/commit/8fd8fcca02240b04b21fa76a8ca8e147dd227ed3]. Now, when generating an {{ORDER BY}} clause, the SQL generator uses the expression only if there is not a conflicting alias in the {{SELECT}} clause. If there is a conflict, uses an ordinal (e.g. ORDER BY 2 DESC), which is understood by just about all dialects.

> RelToSqlConverter does not properly alias ambiguous ORDER BY
> ------------------------------------------------------------
>
>                 Key: CALCITE-3440
>                 URL: https://issues.apache.org/jira/browse/CALCITE-3440
>             Project: Calcite
>          Issue Type: Bug
>            Reporter: Steven Talbot
>            Assignee: Julian Hyde
>            Priority: Major
>             Fix For: 1.22.0
>
>
> Best illustrated by a test that will fail if you put it in RelToSqlConverterTest
>  
> {code:java}
> @Test public void testSelectQueryWithAmbiguousOrderByClause() {
>   String query = "select \"product_id\" as \"p\", \"net_weight\" as \"product_id\"  from \"product\" order by 1";
>   final String expected = "SELECT \"product_id\" AS \"p\", \"net_weight\" AS \"product_id\"\n"
>           + "FROM \"foodmart\".\"product\"\n"
>           + "ORDER BY \"product\".\"product_id\"";
>   sql(query).ok(expected);
> }{code}
>  
> Kind of a strange case, but basically you select a column, alias it, and order by it. And then you select something else and alias it as that column. RelToSqlConverter will ORDER BY just the column, unaliased ("product_id"), which, at least on HyperSQL and MySQL where I tested, ends up ordering by the aliased expression ("net_weight" as "product_id") vs the column  (either "product"."product_id" or "p" would work in this case).



--
This message was sent by Atlassian Jira
(v8.3.4#803005)