[ 
https://issues.apache.org/jira/browse/CALCITE-1962?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16140351#comment-16140351
 ] 

Paul O'Riordan commented on CALCITE-1962:
-----------------------------------------

I created the following test expecting it to fail. 

{code:java}
  /** Test case for
   * <a href="https://issues.apache.org/jira/browse/CALCITE-1962";>[CALCITE-1962]
   * SqlOrderBy unparse ignores dialect.</a>
   * 
   * <p>Test runs on MySQL as it does not support fetch next</p>
   */
  @Test public void testFetchNextTranslatesToLimit() {
      System.out.println(CalciteAssert.DB);
      CalciteAssert
          .model(JdbcTest.FOODMART_MODEL)
          .enable(CalciteAssert.DB == CalciteAssert.DatabaseInstance.MYSQL)
          .query("select \"employee_id\"\n"
                  + "from \"foodmart\".\"employee\" fetch next 10 rows only")
          .runs()
          .returnsCount(10);
  }
{code}

While initially invalid SQL is generated, this gets dropped from the query 
before it's passed down MySQL. The statement executed by MySQL ends up as 
follows:

{noformat}
SELECT *\nFROM `foodmart`.`employee`
{noformat}

Is this the expected behavior? And is there a better place to add the unit 
test? 



> SqlOrderBy unparse ignores dialect.
> -----------------------------------
>
>                 Key: CALCITE-1962
>                 URL: https://issues.apache.org/jira/browse/CALCITE-1962
>             Project: Calcite
>          Issue Type: Bug
>            Reporter: Paul O'Riordan
>            Assignee: Julian Hyde
>
> Given the following code:
> {noformat}
>         final SqlParser.ConfigBuilder configBuilder =  
> SqlParser.configBuilder();
>         configBuilder.setLex(Lex.MYSQL);
>         final SqlParser.Config config = configBuilder.build();
>         final SqlParser parser = SqlParser.create(str, config);
>         final SqlNode parsedStatement = parser.parseQuery("select id from 
> Test limit 1");
>         final SqlDialect mysqlDialect = new SqlDialect(DatabaseProduct.MYSQL, 
> "mysql", "`", NullCollation.LAST);
>         final SqlPrettyWriter spw1 = new SqlPrettyWriter(mysqlDialect);
>         System.out.println(spw1.format(parsedStatement));
> {noformat}
> The output is:
> {noformat}
> SELECT `id`
> FROM `Test`
> FETCH NEXT 1 ROWS ONLY
> {noformat}
> Expected output:
> {noformat}
> SELECT `id`
> FROM `Test`
> LIMIT 1
> {noformat}
> Suggested fix:
> {noformat}
> diff --git a/core/src/main/java/org/apache/calcite/sql/SqlOrderBy.java 
> b/core/src/main/java/org/apache/calcite/sql/SqlOrderBy.java
> index 421b9e21d..b26ffd67b 100644
> --- a/core/src/main/java/org/apache/calcite/sql/SqlOrderBy.java
> +++ b/core/src/main/java/org/apache/calcite/sql/SqlOrderBy.java
> @@ -95,26 +95,7 @@ public void unparse(
>          unparseListClause(writer, orderBy.orderList);
>          writer.endList(listFrame);
>        }
> -      if (orderBy.offset != null) {
> -        final SqlWriter.Frame frame2 =
> -            writer.startList(SqlWriter.FrameTypeEnum.OFFSET);
> -        writer.newlineAndIndent();
> -        writer.keyword("OFFSET");
> -        orderBy.offset.unparse(writer, -1, -1);
> -        writer.keyword("ROWS");
> -        writer.endList(frame2);
> -      }
> -      if (orderBy.fetch != null) {
> -        final SqlWriter.Frame frame3 =
> -            writer.startList(SqlWriter.FrameTypeEnum.FETCH);
> -        writer.newlineAndIndent();
> -        writer.keyword("FETCH");
> -        writer.keyword("NEXT");
> -        orderBy.fetch.unparse(writer, -1, -1);
> -        writer.keyword("ROWS");
> -        writer.keyword("ONLY");
> -        writer.endList(frame3);
> -      }
> +      writer.fetchOffset(orderBy.fetch, orderBy.offset);
>        writer.endList(frame);
>      }
>    }
> {noformat}
> See PR: https://github.com/apache/calcite/pull/525



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to