Skip to content

Commit

Permalink
Updated to get Locale and TimeZone from DataContext
Browse files Browse the repository at this point in the history
  • Loading branch information
normanj-bitquill committed Sep 16, 2024
1 parent 9281a3e commit b2c1346
Show file tree
Hide file tree
Showing 9 changed files with 945 additions and 927 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -852,11 +852,13 @@ Builder populate2() {

// Datetime formatting methods
defineReflective(TO_CHAR, BuiltInMethod.TO_CHAR.method);
defineReflective(TO_CHAR_PG, BuiltInMethod.TO_CHAR_PG.method);
map.put(TO_CHAR_PG, new ToCharPgImplementor());
defineReflective(TO_DATE, BuiltInMethod.TO_DATE.method);
defineReflective(TO_DATE_PG, BuiltInMethod.TO_DATE_PG.method);
map.put(TO_DATE_PG, new ToTimestampPgImplementor("toDate", BuiltInMethod.TO_DATE_PG.method));
defineReflective(TO_TIMESTAMP, BuiltInMethod.TO_TIMESTAMP.method);
defineReflective(TO_TIMESTAMP_PG, BuiltInMethod.TO_TIMESTAMP_PG.method);
map.put(
TO_TIMESTAMP_PG, new ToTimestampPgImplementor("toTimestamp",
BuiltInMethod.TO_TIMESTAMP_PG.method));
final FormatDatetimeImplementor datetimeFormatImpl =
new FormatDatetimeImplementor();
map.put(FORMAT_DATE, datetimeFormatImpl);
Expand Down Expand Up @@ -4709,4 +4711,36 @@ private static class SessionImplementor implements TableFunctionCallImplementor
gapInterval));
}
}

/** Implementor for the {@code T_CHAR} function for PostgreSQL. */
private static class ToCharPgImplementor extends AbstractRexCallImplementor {
ToCharPgImplementor() {
super("toChar", NullPolicy.STRICT, false);
}

@Override Expression implementSafe(RexToLixTranslator translator, RexCall call,
List<Expression> argValueList) {
final Expression operand0 = argValueList.get(0);
final Expression operand1 = argValueList.get(1);
return Expressions.call(BuiltInMethod.TO_CHAR_PG.method, translator.getRoot(),
operand0, operand1);
}
}

/** Implementor for the {@code TO_DATE} or {@code TO_TIMESTAMP} functions for PostgreSQL. */
private static class ToTimestampPgImplementor extends AbstractRexCallImplementor {
private final Method method;

ToTimestampPgImplementor(String name, Method method) {
super(name, NullPolicy.STRICT, false);
this.method = method;
}

@Override Expression implementSafe(RexToLixTranslator translator, RexCall call,
List<Expression> argValueList) {
final Expression operand0 = argValueList.get(0);
final Expression operand1 = argValueList.get(1);
return Expressions.call(method, translator.getRoot(), operand0, operand1);
}
}
}
21 changes: 13 additions & 8 deletions core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -4401,22 +4401,25 @@ public String toChar(long timestamp, String pattern) {
return sb.toString().trim();
}

public String toCharPg(long timestamp, String pattern) {
public static String toCharPg(DataContext root, long timestamp, String pattern) {
final ZoneId zoneId = DataContext.Variable.TIME_ZONE.<TimeZone>get(root).toZoneId();
final Locale locale = DataContext.Variable.LOCALE.get(root);
final Timestamp sqlTimestamp = internalToTimestamp(timestamp);
final ZonedDateTime zonedDateTime =
ZonedDateTime.of(sqlTimestamp.toLocalDateTime(), ZoneId.systemDefault());
return PostgresqlDateTimeFormatter.toChar(pattern, zonedDateTime).trim();
ZonedDateTime.of(sqlTimestamp.toLocalDateTime(), zoneId);
return PostgresqlDateTimeFormatter.toChar(pattern, zonedDateTime, locale).trim();

Check failure on line 4410 in core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java

View workflow job for this annotation

GitHub Actions / CheckerFramework (JDK 11), oldest Guava

[Task :core:compileJava] [argument.type.incompatible] incompatible argument for parameter locale of toChar. return PostgresqlDateTimeFormatter.toChar(pattern, zonedDateTime, locale).trim(); ^ found : @initialized @nullable Locale

Check failure on line 4410 in core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java

View workflow job for this annotation

GitHub Actions / CheckerFramework (JDK 11)

[Task :core:compileJava] [argument.type.incompatible] incompatible argument for parameter locale of toChar. return PostgresqlDateTimeFormatter.toChar(pattern, zonedDateTime, locale).trim(); ^ found : @initialized @nullable Locale
}

public int toDate(String dateString, String fmtString) {
return toInt(
new java.sql.Date(internalToDateTime(dateString, fmtString)));
}

public int toDatePg(String dateString, String fmtString) {
public static int toDatePg(DataContext root, String dateString, String fmtString) {
try {
return (int) PostgresqlDateTimeFormatter.toTimestamp(dateString, fmtString,
LOCAL_ZONE)
final Locale locale = DataContext.Variable.LOCALE.get(root);
return (int) PostgresqlDateTimeFormatter.toTimestamp(dateString, fmtString, LOCAL_ZONE,
locale)

Check failure on line 4422 in core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java

View workflow job for this annotation

GitHub Actions / CheckerFramework (JDK 11), oldest Guava

[Task :core:compileJava] [argument.type.incompatible] incompatible argument for parameter locale of toTimestamp. locale) ^ found : @initialized @nullable Locale

Check failure on line 4422 in core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java

View workflow job for this annotation

GitHub Actions / CheckerFramework (JDK 11)

[Task :core:compileJava] [argument.type.incompatible] incompatible argument for parameter locale of toTimestamp. locale) ^ found : @initialized @nullable Locale
.getLong(ChronoField.EPOCH_DAY);
} catch (Exception e) {
SQLException sqlEx =
Expand All @@ -4433,9 +4436,11 @@ public long toTimestamp(String timestampString, String fmtString) {
new java.sql.Timestamp(internalToDateTime(timestampString, fmtString)));
}

public long toTimestampPg(String timestampString, String fmtString) {
public static long toTimestampPg(DataContext root, String timestampString, String fmtString) {
try {
return PostgresqlDateTimeFormatter.toTimestamp(timestampString, fmtString, LOCAL_ZONE)
final Locale locale = DataContext.Variable.LOCALE.get(root);
return PostgresqlDateTimeFormatter.toTimestamp(timestampString, fmtString, LOCAL_ZONE,
locale)

Check failure on line 4443 in core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java

View workflow job for this annotation

GitHub Actions / CheckerFramework (JDK 11), oldest Guava

[Task :core:compileJava] [argument.type.incompatible] incompatible argument for parameter locale of toTimestamp. locale) ^ found : @initialized @nullable Locale

Check failure on line 4443 in core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java

View workflow job for this annotation

GitHub Actions / CheckerFramework (JDK 11)

[Task :core:compileJava] [argument.type.incompatible] incompatible argument for parameter locale of toTimestamp. locale) ^ found : @initialized @nullable Locale
.toInstant().toEpochMilli();
} catch (Exception e) {
SQLException sqlEx =
Expand Down
10 changes: 5 additions & 5 deletions core/src/main/java/org/apache/calcite/util/BuiltInMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -708,16 +708,16 @@ public enum BuiltInMethod {
String.class, long.class),
TO_CHAR(SqlFunctions.DateFormatFunction.class, "toChar", long.class,
String.class),
TO_CHAR_PG(SqlFunctions.DateFormatFunction.class, "toCharPg", long.class,
TO_CHAR_PG(SqlFunctions.DateFormatFunction.class, "toCharPg", DataContext.class, long.class,
String.class),
TO_DATE(SqlFunctions.DateFormatFunction.class, "toDate", String.class,
String.class),
TO_DATE_PG(SqlFunctions.DateFormatFunction.class, "toDatePg", String.class,
String.class),
TO_DATE_PG(SqlFunctions.DateFormatFunction.class, "toDatePg", DataContext.class,
String.class, String.class),
TO_TIMESTAMP(SqlFunctions.DateFormatFunction.class, "toTimestamp", String.class,
String.class),
TO_TIMESTAMP_PG(SqlFunctions.DateFormatFunction.class, "toTimestampPg", String.class,
String.class),
TO_TIMESTAMP_PG(SqlFunctions.DateFormatFunction.class, "toTimestampPg", DataContext.class,
String.class, String.class),
FORMAT_DATE(SqlFunctions.DateFormatFunction.class, "formatDate",
String.class, int.class),
FORMAT_TIME(SqlFunctions.DateFormatFunction.class, "formatTime",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public String[] getPatterns() {
* @return the string representation of the datetime based on the format pattern
*/
public abstract @Nullable String convert(ParsePosition parsePosition, String formatString,
ZonedDateTime dateTime);
ZonedDateTime dateTime, Locale locale);

/**
* Get the ChronoUnitEnum value that this format pattern represents. For example, the
Expand All @@ -86,7 +86,7 @@ public String[] getPatterns() {
* @throws ParseException if the pattern could not be applied to the input
*/
public long parse(ParsePosition inputPosition, String input, ParsePosition formatPosition,
String formatString, boolean enforceLength) throws ParseException {
String formatString, boolean enforceLength, Locale locale) throws ParseException {

boolean haveFillMode = false;
boolean haveTranslateMode = false;
Expand All @@ -112,8 +112,8 @@ public long parse(ParsePosition inputPosition, String input, ParsePosition forma
}

try {
final Locale locale = haveTranslateMode ? Locale.getDefault() : Locale.US;
long parsedValue = parseValue(inputPosition, input, locale, haveFillMode, enforceLength);
final Locale localeToUse = haveTranslateMode ? locale : Locale.US;
long parsedValue = parseValue(inputPosition, input, localeToUse, haveFillMode, enforceLength);
formatPosition.setIndex(formatString.length() - formatTrimmed.length());

return parsedValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected NumberFormatPattern(ChronoUnitEnum chronoUnit, int minValue,
}

@Override public @Nullable String convert(ParsePosition parsePosition, String formatString,
ZonedDateTime dateTime) {
ZonedDateTime dateTime, Locale locale) {
String formatStringTrimmed = formatString.substring(parsePosition.getIndex());

boolean haveFillMode = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ private PostgresqlDateTimeFormatter() {
* @param dateTime datetime to convert
* @return formatted string representation of the datetime
*/
public static String toChar(String formatString, ZonedDateTime dateTime) {
public static String toChar(String formatString, ZonedDateTime dateTime, Locale locale) {
final ParsePosition parsePosition = new ParsePosition(0);
final StringBuilder sb = new StringBuilder();

Expand All @@ -462,7 +462,7 @@ public static String toChar(String formatString, ZonedDateTime dateTime) {

for (FormatPattern formatPattern : FORMAT_PATTERNS) {
final String formattedString =
formatPattern.convert(parsePosition, formatString, dateTime);
formatPattern.convert(parsePosition, formatString, dateTime, locale);
if (formattedString != null) {
sb.append(formattedString);
matched = true;
Expand All @@ -479,8 +479,8 @@ public static String toChar(String formatString, ZonedDateTime dateTime) {
return sb.toString();
}

public static ZonedDateTime toTimestamp(String input, String formatString, ZoneId zoneId)
throws Exception {
public static ZonedDateTime toTimestamp(String input, String formatString, ZoneId zoneId,
Locale locale) throws Exception {
final ParsePosition inputParsePosition = new ParsePosition(0);
final ParsePosition formatParsePosition = new ParsePosition(0);
final Map<ChronoUnitEnum, Long> dateTimeParts = new HashMap<>();
Expand Down Expand Up @@ -515,7 +515,7 @@ public static ZonedDateTime toTimestamp(String input, String formatString, ZoneI

parsedValue =
formatPattern.parse(inputParsePosition, input, formatParsePosition, formatString,
enforceLength);
enforceLength, locale);
matchedPattern = formatPattern;
break;
}
Expand Down Expand Up @@ -563,7 +563,7 @@ public static ZonedDateTime toTimestamp(String input, String formatString, ZoneI

private static ZonedDateTime constructDateTimeFromParts(Map<ChronoUnitEnum, Long> dateParts,
ZoneId zoneId) {
LocalDateTime constructedDateTime = LocalDateTime.now(ZoneId.systemDefault())
LocalDateTime constructedDateTime = LocalDateTime.now(zoneId)
.truncatedTo(ChronoUnit.DAYS);

DateCalendarEnum calendar = DateCalendarEnum.NONE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected StringFormatPattern(ChronoUnitEnum chronoUnit, String... patterns) {
}

@Override public @Nullable String convert(ParsePosition parsePosition, String formatString,
ZonedDateTime dateTime) {
ZonedDateTime dateTime, Locale locale) {
String formatStringTrimmed = formatString.substring(parsePosition.getIndex());

boolean haveFillMode = false;
Expand Down Expand Up @@ -86,7 +86,7 @@ protected StringFormatPattern(ChronoUnitEnum chronoUnit, String... patterns) {
dateTime,
haveFillMode,
suffix,
haveTranslationMode ? Locale.getDefault() : Locale.US);
haveTranslationMode ? locale : Locale.US);
}

@Override public ChronoUnitEnum getChronoUnit() {
Expand Down
Loading

0 comments on commit b2c1346

Please sign in to comment.