Simplify to_date function not to depend on cast to TIMESTAMP
@@ -16,8 +16,6 @@ package com.facebook.presto.teradata.functions;
import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.function.Description;
-import com.facebook.presto.spi.function.OperatorDependency;
-import com.facebook.presto.spi.function.OperatorType;
import com.facebook.presto.spi.function.ScalarFunction;
import com.facebook.presto.spi.function.SqlType;
import com.facebook.presto.spi.type.StandardTypes;
@@ -28,7 +26,7 @@ import org.joda.time.DateTimeZone;
import org.joda.time.chrono.ISOChronology;
import org.joda.time.format.DateTimeFormatter;
-import java.lang.invoke.MethodHandle;
+import java.util.Locale;
import static com.facebook.presto.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR;
import static com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT;
@@ -41,6 +39,7 @@ import static com.facebook.presto.teradata.functions.dateformat.DateFormatParser
import static com.google.common.base.Throwables.throwIfInstanceOf;
import static io.airlift.slice.Slices.utf8Slice;
import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
public final class TeradataDateFunctions
{
@@ -78,19 +77,11 @@ public final class TeradataDateFunctions
@Description("Converts a string to a DATE data type")
@ScalarFunction("to_date")
@SqlType(StandardTypes.DATE)
- public static long toDate(
- @OperatorDependency(
- operator = OperatorType.CAST,
- returnType = StandardTypes.DATE,
- argumentTypes = StandardTypes.TIMESTAMP)
- MethodHandle castToDate,
- ConnectorSession session,
- @SqlType(StandardTypes.VARCHAR) Slice dateTime,
- @SqlType(StandardTypes.VARCHAR) Slice formatString)
+ public static long toDate(ConnectorSession session, @SqlType(StandardTypes.VARCHAR) Slice dateTime, @SqlType(StandardTypes.VARCHAR) Slice formatString)
{
try {
- long millis = parseMillis(session, dateTime, formatString);
- return (long) castToDate.invokeExact(session, millis);
+ long millis = parseMillis(UTC_KEY, session.getLocale(), dateTime, formatString);
+ return MILLISECONDS.toDays(millis);
}
catch (Throwable t) {
throwIfInstanceOf(t, Error.class);
@@ -116,9 +107,14 @@ public final class TeradataDateFunctions
if (session.isLegacyTimestamp()) {
timeZoneKey = session.getTimeZoneKey();
}
+ return parseMillis(timeZoneKey, session.getLocale(), dateTime, formatString);
+ }
+
+ private static long parseMillis(TimeZoneKey timeZoneKey, Locale locale, Slice dateTime, Slice formatString)
+ {
DateTimeFormatter formatter = DATETIME_FORMATTER_CACHE.get(formatString)
.withChronology(CHRONOLOGIES[timeZoneKey.getKey()])
- .withLocale(session.getLocale());
+ .withLocale(locale);
try {
return formatter.parseMillis(dateTime.toString(UTF_8));
