Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-3440

RelToSqlConverter does not properly alias ambiguous ORDER BY

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.22.0
    • None
    • None

    Description

      Best illustrated by a test that will fail if you put it in RelToSqlConverterTest

       

      @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);
      }

       

      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).

      Attachments

        Issue Links

          Activity

            People

              julianhyde Julian Hyde
              swtalbot Steven Talbot
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: