Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,20 @@ private void unparseDatetime(SqlWriter writer, SqlCall call) {
switch (writer.getDialect().getDatabaseProduct()) {
case ORACLE:
replaceTimeUnitOperand(call, timeUnit.name(), timeUnitNode.getParserPosition());
unparseDatetimeFunction(writer, call, "TRUNC", true);
unparseDatetimeFunction(writer,
replaceTimeUnitOperand(call, timeUnit.name(), timeUnitNode.getParserPosition()),
"TRUNC", true);
break;
case HSQLDB:
String translatedLit = convertToHsqlDb(timeUnit);
replaceTimeUnitOperand(call, translatedLit, timeUnitNode.getParserPosition());
unparseDatetimeFunction(writer, call, "TRUNC", true);
unparseDatetimeFunction(writer,
replaceTimeUnitOperand(call, translatedLit, timeUnitNode.getParserPosition()),
"TRUNC", true);
break;
case POSTGRESQL:
replaceTimeUnitOperand(call, timeUnit.name(), timeUnitNode.getParserPosition());
unparseDatetimeFunction(writer, call, "DATE_TRUNC", false);
unparseDatetimeFunction(writer,
replaceTimeUnitOperand(call, timeUnit.name(), timeUnitNode.getParserPosition()),
"DATE_TRUNC", false);
break;
case MSSQL:
unparseDatetimeMssql(writer, call);
Expand All @@ -113,9 +117,18 @@ private void unparseDatetime(SqlWriter writer, SqlCall call) {
}
}

private void replaceTimeUnitOperand(SqlCall call, String literal, SqlParserPos pos) {
/**
* Copy the original SqlCall replacing the time unit operand with the given literal.
*
* @param call SqlCall
* @param literal String
* @param pos SqlParserPos
* @return SqlCall
*/
private SqlCall replaceTimeUnitOperand(SqlCall call, String literal, SqlParserPos pos) {
SqlLiteral literalNode = SqlLiteral.createCharString(literal, null, pos);
call.setOperand(1, literalNode);
return call.getOperator().createCall(call.getFunctionQuantifier(), pos,
call.getOperandList().get(0), literalNode);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,19 @@ private void checkLiteral(String s) {
.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>. */
@Test public void testFloorWithGroupBy() {
String query = "SELECT floor(\"hire_date\" TO MINUTE) FROM \"employee\"\n"
+ "GROUP BY floor(\"hire_date\" TO MINUTE)";
String expected = "SELECT TRUNC(hire_date, 'MI')\nFROM foodmart.employee\n"
+ "GROUP BY TRUNC(hire_date, 'MI')";
sql(query)
.dialect(DatabaseProduct.HSQLDB.getDialect())
.ok(expected);
}

@Test public void testMatchRecognizePatternExpression() {
String sql = "select *\n"
+ " from \"product\" match_recognize\n"
Expand Down