Skip to content

Commit

Permalink
jerin-pair-format
Browse files Browse the repository at this point in the history
  • Loading branch information
tanclary committed Feb 23, 2024
1 parent a8e4f57 commit 1b77e08
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2776,7 +2776,7 @@ private static class FormatDatetimeImplementor
method = BuiltInMethod.FORMAT_TIMESTAMP.method;
}
return implementSafe(method,
ImmutableList.of(translator.getRoot(), operand0, operand1));
ImmutableList.of(operand0, operand1));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,15 @@ private Expression getConvertExpression(
: Expressions.call(
Expressions.new_(
BuiltInMethod.FORMAT_DATE.method.getDeclaringClass()),
BuiltInMethod.FORMAT_DATE.method, DataContext.ROOT, format, operand));
BuiltInMethod.FORMAT_DATE.method, format, operand));

case TIME:
return RexImpTable.optimize2(operand, Expressions.isConstantNull(format)
? Expressions.call(BuiltInMethod.UNIX_TIME_TO_STRING.method, operand)
: Expressions.call(
Expressions.new_(
BuiltInMethod.FORMAT_TIME.method.getDeclaringClass()),
BuiltInMethod.FORMAT_TIME.method, DataContext.ROOT, format, operand));
BuiltInMethod.FORMAT_TIME.method, format, operand));

case TIME_WITH_LOCAL_TIME_ZONE:
return RexImpTable.optimize2(operand, Expressions.isConstantNull(format)
Expand All @@ -373,15 +373,15 @@ private Expression getConvertExpression(
: Expressions.call(
Expressions.new_(
BuiltInMethod.FORMAT_TIME.method.getDeclaringClass()),
BuiltInMethod.FORMAT_TIME.method, DataContext.ROOT, format, operand));
BuiltInMethod.FORMAT_TIME.method, format, operand));

case TIMESTAMP:
return RexImpTable.optimize2(operand, Expressions.isConstantNull(format)
? Expressions.call(BuiltInMethod.UNIX_TIMESTAMP_TO_STRING.method, operand)
: Expressions.call(
Expressions.new_(
BuiltInMethod.FORMAT_TIMESTAMP.method.getDeclaringClass()),
BuiltInMethod.FORMAT_TIMESTAMP.method, DataContext.ROOT, format, operand));
BuiltInMethod.FORMAT_TIMESTAMP.method, format, operand));

case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
return RexImpTable.optimize2(operand, Expressions.isConstantNull(format)
Expand All @@ -390,7 +390,7 @@ private Expression getConvertExpression(
: Expressions.call(
Expressions.new_(
BuiltInMethod.FORMAT_TIMESTAMP.method.getDeclaringClass()),
BuiltInMethod.FORMAT_TIMESTAMP.method, DataContext.ROOT, format, operand));
BuiltInMethod.FORMAT_TIMESTAMP.method, format, operand));

case INTERVAL_YEAR:
case INTERVAL_YEAR_MONTH:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3998,7 +3998,7 @@ private String internalFormatDatetime(String fmtString,
return sb.toString();
}

public String formatTimestamp(DataContext ctx, String fmtString,
public String formatTimestamp(String fmtString,
long timestamp) {
return internalFormatDatetime(fmtString, internalToTimestamp(timestamp));
}
Expand All @@ -4011,11 +4011,11 @@ public String toChar(long timestamp, String pattern) {
return sb.toString().trim();
}

public String formatDate(DataContext ctx, String fmtString, int date) {
public String formatDate(String fmtString, int date) {
return internalFormatDatetime(fmtString, internalToDate(date));
}

public String formatTime(DataContext ctx, String fmtString, int time) {
public String formatTime(String fmtString, int time) {
return internalFormatDatetime(fmtString, internalToTime(time));
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/org/apache/calcite/util/BuiltInMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -640,13 +640,13 @@ public enum BuiltInMethod {
PARSE_TIMESTAMP(SqlFunctions.DateParseFunction.class, "parseTimestamp",
String.class, String.class),
FORMAT_TIMESTAMP(SqlFunctions.DateFormatFunction.class, "formatTimestamp",
DataContext.class, String.class, long.class),
String.class, long.class),
TO_CHAR(SqlFunctions.DateFormatFunction.class, "toChar", long.class,
String.class),
FORMAT_DATE(SqlFunctions.DateFormatFunction.class, "formatDate",
DataContext.class, String.class, int.class),
String.class, int.class),
FORMAT_TIME(SqlFunctions.DateFormatFunction.class, "formatTime",
DataContext.class, String.class, int.class),
String.class, int.class),
UNIX_DATE_TO_STRING(DateTimeUtils.class, "unixDateToString", int.class),
UNIX_TIME_TO_STRING(DateTimeUtils.class, "unixTimeToString", int.class),
UNIX_TIMESTAMP_TO_STRING(DateTimeUtils.class, "unixTimestampToString",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,15 @@ void testCastStringToDateTime(CastType castType, SqlOperatorFixture f) {
"VARCHAR NOT NULL");
}
}

@ParameterizedTest
@MethodSource("safeParameters")
void testCastFormatClauseDateTimeToString(CastType castType, SqlOperatorFixture f) {
// Cast DATE to String
f.checkString("cast(date '2018-01-30' as varchar format 'YYYY')",
"2018",
"VARCHAR NOT NULL");
}

@Test void testCastFormatClauseStringToDateTime() {
final SqlOperatorFixture f = fixture()
Expand Down

0 comments on commit 1b77e08

Please sign in to comment.