[
https://issues.apache.org/jira/browse/CALCITE-3440?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16958099#comment-16958099
]
Julian Hyde commented on CALCITE-3440:
--------------------------------------
Agreed, this is a bug. Calcite would have to know how the target dialect
interprets the expression in the ORDER BY clause, but in all dialects I know,
an identifier in the ORDER BY clause is taken as an alias in preference to an
unqualified column name. That information is in {{SqlConformance}} (e.g.
{{isSortByAlias()}} and {{isSortByAliasObscures()}}).
> 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
> Priority: Major
>
> 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)