Skip to content

Commit

Permalink
[CALCITE-1866] JDBC adapter generates incorrect code when pushing FLO…
Browse files Browse the repository at this point in the history
…OR to MySQL (Kang Wang, Sergey Nuyanzin)

Fix format string. DateTime FLOOR to HOUR cause MySQL use '%H' rather than '%k'. (Kang Wang)

Add test-cases for MySQL (FLOOR to HOUR, FLOOR to MINUTE, FLOOR to SECOND) (Sergey Nuyanzin)

Close apache#745
  • Loading branch information
snuyanzin authored and julianhyde committed Jul 9, 2018
1 parent cf3eca2 commit bc269aa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ private void unparseFloor(SqlWriter writer, SqlCall call) {
format = "%Y-%m-%d";
break;
case HOUR:
format = "%Y-%m-%d %k:00:00";
format = "%Y-%m-%d %H:00:00";
break;
case MINUTE:
format = "%Y-%m-%d %k:%i:00";
format = "%Y-%m-%d %H:%i:00";
break;
case SECOND:
format = "%Y-%m-%d %k:%i:%s";
format = "%Y-%m-%d %H:%i:%s";
break;
default:
throw new AssertionError("MYSQL does not support FLOOR for time unit: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,33 @@ private void checkLiteral2(String expression, String expected) {
.ok(expected);
}

@Test public void testFloorMysqlHour() {
String query = "SELECT floor(\"hire_date\" TO HOUR) FROM \"employee\"";
String expected = "SELECT DATE_FORMAT(`hire_date`, '%Y-%m-%d %H:00:00')\n"
+ "FROM `foodmart`.`employee`";
sql(query)
.withMysql()
.ok(expected);
}

@Test public void testFloorMysqlMinute() {
String query = "SELECT floor(\"hire_date\" TO MINUTE) FROM \"employee\"";
String expected = "SELECT DATE_FORMAT(`hire_date`, '%Y-%m-%d %H:%i:00')\n"
+ "FROM `foodmart`.`employee`";
sql(query)
.withMysql()
.ok(expected);
}

@Test public void testFloorMysqlSecond() {
String query = "SELECT floor(\"hire_date\" TO SECOND) FROM \"employee\"";
String expected = "SELECT DATE_FORMAT(`hire_date`, '%Y-%m-%d %H:%i:%s')\n"
+ "FROM `foodmart`.`employee`";
sql(query)
.withMysql()
.ok(expected);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-1826">[CALCITE-1826]
* JDBC dialect-specific FLOOR fails when in GROUP BY</a>. */
Expand All @@ -1303,9 +1330,9 @@ private void checkLiteral2(String expression, String expected) {
+ "FROM \"foodmart\".\"employee\"\n"
+ "GROUP BY DATE_TRUNC('MINUTE', \"hire_date\")";
final String expectedMysql = "SELECT"
+ " DATE_FORMAT(`hire_date`, '%Y-%m-%d %k:%i:00')\n"
+ " DATE_FORMAT(`hire_date`, '%Y-%m-%d %H:%i:00')\n"
+ "FROM `foodmart`.`employee`\n"
+ "GROUP BY DATE_FORMAT(`hire_date`, '%Y-%m-%d %k:%i:00')";
+ "GROUP BY DATE_FORMAT(`hire_date`, '%Y-%m-%d %H:%i:00')";
sql(query)
.withHsqldb()
.ok(expected)
Expand Down

0 comments on commit bc269aa

Please sign in to comment.