Skip to content

Commit

Permalink
[CALCITE-6150] JDBC adapter for ClickHouse generates incorrect SQL fo…
Browse files Browse the repository at this point in the history
…r certain units in the EXTRACT function

Close apache#3557
  • Loading branch information
Chen768959 authored and julianhyde committed Dec 6, 2023
1 parent bd7d4e8 commit d3ab0bc
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,29 @@ private static SqlDataTypeSpec createSqlDataTypeSpecByName(String typeAlias,
super.unparseCall(writer, call, leftPrec, rightPrec);
}
break;
case EXTRACT:
SqlLiteral node = call.operand(0);
TimeUnitRange unit = node.getValueAs(TimeUnitRange.class);
String funName;
switch (unit) {
case DOW:
funName = "DAYOFWEEK";
break;
case DOY:
funName = "DAYOFYEAR";
break;
case WEEK:
funName = "toWeek";
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
return;
}
writer.print(funName);
final SqlWriter.Frame frame = writer.startList("(", ")");
call.operand(1).unparse(writer, 0, 0);
writer.endList(frame);
break;
default:
super.unparseCall(writer, call, leftPrec, rightPrec);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2264,6 +2264,75 @@ private SqlDialect nonOrdinalDialect() {
sql(query).withLibrary(SqlLibrary.BIG_QUERY).ok(expected);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6150">[CALCITE-6150]
* JDBC adapter for ClickHouse generates incorrect SQL for certain units in
* the EXTRACT function</a>. Also tests other units in other dialects. */
@Test void testExtract() {
final String sql = "SELECT\n"
+ "EXTRACT(YEAR FROM DATE '2023-12-01'),\n"
+ "EXTRACT(QUARTER FROM DATE '2023-12-01'),\n"
+ "EXTRACT(MONTH FROM DATE '2023-12-01'),\n"
+ "EXTRACT(WEEK FROM DATE '2023-12-01'),\n"
+ "EXTRACT(DOY FROM DATE '2023-12-01'),\n"
+ "EXTRACT(DAY FROM DATE '2023-12-01'),\n"
+ "EXTRACT(DOW FROM DATE '2023-12-01'),\n"
+ "EXTRACT(HOUR FROM TIMESTAMP '2023-12-01 00:00:00'),\n"
+ "EXTRACT(MINUTE FROM TIMESTAMP '2023-12-01 00:00:00'),\n"
+ "EXTRACT(SECOND FROM TIMESTAMP '2023-12-01 00:00:00')";
final String expectedClickHouse = "SELECT "
+ "EXTRACT(YEAR FROM toDate('2023-12-01')), "
+ "EXTRACT(QUARTER FROM toDate('2023-12-01')), "
+ "EXTRACT(MONTH FROM toDate('2023-12-01')), "
+ "toWeek(toDate('2023-12-01')), "
+ "DAYOFYEAR(toDate('2023-12-01')), "
+ "EXTRACT(DAY FROM toDate('2023-12-01')), "
+ "DAYOFWEEK(toDate('2023-12-01')), "
+ "EXTRACT(HOUR FROM toDateTime('2023-12-01 00:00:00')), "
+ "EXTRACT(MINUTE FROM toDateTime('2023-12-01 00:00:00')), "
+ "EXTRACT(SECOND FROM toDateTime('2023-12-01 00:00:00'))";
final String expectedHive = "SELECT "
+ "EXTRACT(YEAR FROM DATE '2023-12-01'), "
+ "EXTRACT(QUARTER FROM DATE '2023-12-01'), "
+ "EXTRACT(MONTH FROM DATE '2023-12-01'), "
+ "EXTRACT(WEEK FROM DATE '2023-12-01'), "
+ "EXTRACT(DOY FROM DATE '2023-12-01'), "
+ "EXTRACT(DAY FROM DATE '2023-12-01'), "
+ "EXTRACT(DOW FROM DATE '2023-12-01'), "
+ "EXTRACT(HOUR FROM TIMESTAMP '2023-12-01 00:00:00'), "
+ "EXTRACT(MINUTE FROM TIMESTAMP '2023-12-01 00:00:00'), "
+ "EXTRACT(SECOND FROM TIMESTAMP '2023-12-01 00:00:00')";
final String expectedPostgresql = "SELECT "
+ "EXTRACT(YEAR FROM DATE '2023-12-01'), "
+ "EXTRACT(QUARTER FROM DATE '2023-12-01'), "
+ "EXTRACT(MONTH FROM DATE '2023-12-01'), "
+ "EXTRACT(WEEK FROM DATE '2023-12-01'), "
+ "EXTRACT(DOY FROM DATE '2023-12-01'), "
+ "EXTRACT(DAY FROM DATE '2023-12-01'), "
+ "EXTRACT(DOW FROM DATE '2023-12-01'), "
+ "EXTRACT(HOUR FROM TIMESTAMP '2023-12-01 00:00:00'), "
+ "EXTRACT(MINUTE FROM TIMESTAMP '2023-12-01 00:00:00'), "
+ "EXTRACT(SECOND FROM TIMESTAMP '2023-12-01 00:00:00')\n"
+ "FROM (VALUES (0)) AS \"t\" (\"ZERO\")";
final String expectedHsqldb = "SELECT "
+ "EXTRACT(YEAR FROM DATE '2023-12-01'), "
+ "EXTRACT(QUARTER FROM DATE '2023-12-01'), "
+ "EXTRACT(MONTH FROM DATE '2023-12-01'), "
+ "EXTRACT(WEEK FROM DATE '2023-12-01'), "
+ "EXTRACT(DOY FROM DATE '2023-12-01'), "
+ "EXTRACT(DAY FROM DATE '2023-12-01'), "
+ "EXTRACT(DOW FROM DATE '2023-12-01'), "
+ "EXTRACT(HOUR FROM TIMESTAMP '2023-12-01 00:00:00'), "
+ "EXTRACT(MINUTE FROM TIMESTAMP '2023-12-01 00:00:00'), "
+ "EXTRACT(SECOND FROM TIMESTAMP '2023-12-01 00:00:00')\n"
+ "FROM (VALUES (0)) AS t (ZERO)";
sql(sql)
.withClickHouse().ok(expectedClickHouse)
.withHive().ok(expectedHive)
.withPostgresql().ok(expectedPostgresql)
.withHsqldb().ok(expectedHsqldb);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-3220">[CALCITE-3220]
* HiveSqlDialect should transform the SQL-standard TRIM function to TRIM,
Expand Down

0 comments on commit d3ab0bc

Please sign in to comment.