Skip to content

Commit

Permalink
Remove JodaCompatibleZonedDateTime (elastic#78417)
Browse files Browse the repository at this point in the history
This change removes JodaCompatibleZonedDateTime and replaces it with ZonedDateTime for use in 
scripting.

Breaking changes:
* JodaCompatibleDateTime no longer exists and cannot be cast to in Painless. Use ZonedDateTime 
instead.
* The dayOfWeek method on ZonedDateTime returns the DayOfWeek enum instead of an int from 
JodaCompatibleDateTime. dayOfWeekEnum still exists on ZonedDateTime as an augmentation to 
support the transition to ZonedDateTime, but is now deprecated in favor of dayOfWeek on 
ZonedDateTime.
  • Loading branch information
jdconrad committed Sep 29, 2021
1 parent 3714812 commit 086ba1a
Show file tree
Hide file tree
Showing 44 changed files with 136 additions and 1,093 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ The following specialized API is available in the Score context.
The following methods are directly callable without a class/instance qualifier. Note parameters denoted by a (*) are treated as read-only values.

* double cosineSimilarity(List *, String *)
* double decayDateExp(String *, String *, String *, double *, JodaCompatibleZonedDateTime)
* double decayDateGauss(String *, String *, String *, double *, JodaCompatibleZonedDateTime)
* double decayDateLinear(String *, String *, String *, double *, JodaCompatibleZonedDateTime)
* double decayDateExp(String *, String *, String *, double *, ZonedDateTime)
* double decayDateGauss(String *, String *, String *, double *, ZonedDateTime)
* double decayDateLinear(String *, String *, String *, double *, ZonedDateTime)
* double decayGeoExp(String *, String *, String *, double *, GeoPoint)
* double decayGeoGauss(String *, String *, String *, double *, GeoPoint)
* double decayGeoLinear(String *, String *, String *, double *, GeoPoint)
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/migration/migrate_8_0.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ coming[8.0.0]
* <<breaking_80_reindex_changes>>
* <<breaking_80_api_changes>>
* <<breaking_80_rollup_changes>>
* <<breaking_80_scripting_changes>>
* <<breaking_80_search_changes>>
* <<breaking_80_security_changes>>
* <<breaking_80_settings_changes>>
Expand Down Expand Up @@ -134,6 +135,7 @@ include::migrate_8_0/packaging.asciidoc[]
include::migrate_8_0/reindex.asciidoc[]
include::migrate_8_0/api.asciidoc[]
include::migrate_8_0/rollup.asciidoc[]
include::migrate_8_0/scripting.asciidoc[]
include::migrate_8_0/search.asciidoc[]
include::migrate_8_0/security.asciidoc[]
include::migrate_8_0/settings.asciidoc[]
Expand Down
25 changes: 25 additions & 0 deletions docs/reference/migration/migrate_8_0/scripting.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[discrete]
[[breaking_80_scripting_changes]]
==== Scripting changes

//NOTE: The notable-breaking-changes tagged regions are re-used in the
//Installation and Upgrade Guide

//tag::notable-breaking-changes[]
.The `JodaCompatibleDateTime` class has been removed.
[%collapsible]
====
*Details* +
As a transition from Joda datetime to Java datetime, scripting used
an intermediate class called `JodaCompatibleDateTime`. This class has
been removed and is replaced by `ZonedDateTime`. Any use of casting
to a `JodaCompatibleDateTime` in a script will result in a compilation
error, and may not allow the upgraded node to start.
*Impact* +
Before upgrading, replace `getDayOfWeek` with `getDayOfWeekEnum().value` in any
scripts. Any use of `getDayOfWeek` expecting a return value of `int` will result
in a compilation error or runtime error and may not allow the upgraded node to
start.
====
// end::notable-breaking-changes[]
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import org.elasticsearch.painless.lookup.PainlessCast;
import org.elasticsearch.painless.lookup.PainlessLookupUtility;
import org.elasticsearch.painless.lookup.def;
import org.elasticsearch.script.JodaCompatibleZonedDateTime;

import java.time.ZonedDateTime;
import java.util.Objects;

/**
Expand Down Expand Up @@ -63,19 +61,11 @@ public static PainlessCast getLegalCast(Location location, Class<?> actual, Clas
return PainlessCast.originalTypetoTargetType(def.class, Float.class, explicit);
} else if (expected == Double.class) {
return PainlessCast.originalTypetoTargetType(def.class, Double.class, explicit);
// TODO: remove this when the transition from Joda to Java datetimes is completed
} else if (expected == ZonedDateTime.class) {
return PainlessCast.originalTypetoTargetType(def.class, ZonedDateTime.class, explicit);
}
} else if (actual == String.class) {
if (expected == char.class && explicit) {
return PainlessCast.originalTypetoTargetType(String.class, char.class, true);
}
// TODO: remove this when the transition from Joda to Java datetimes is completed
} else if (actual == JodaCompatibleZonedDateTime.class) {
if (expected == ZonedDateTime.class) {
return PainlessCast.originalTypetoTargetType(JodaCompatibleZonedDateTime.class, ZonedDateTime.class, explicit);
}
} else if (actual == boolean.class) {
if (expected == def.class) {
return PainlessCast.boxOriginalType(Boolean.class, def.class, explicit, boolean.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
import org.elasticsearch.painless.lookup.PainlessLookupUtility;
import org.elasticsearch.painless.lookup.PainlessMethod;
import org.elasticsearch.painless.symbol.FunctionTable;
import org.elasticsearch.script.JodaCompatibleZonedDateTime;

import java.lang.invoke.CallSite;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.time.ZonedDateTime;
import java.util.BitSet;
import java.util.Collections;
import java.util.Iterator;
Expand Down Expand Up @@ -1202,15 +1200,6 @@ public static String defToStringExplicit(final Object value) {
}
}

// TODO: remove this when the transition from Joda to Java datetimes is completed
public static ZonedDateTime defToZonedDateTime(final Object value) {
if (value instanceof JodaCompatibleZonedDateTime) {
return ((JodaCompatibleZonedDateTime)value).getZonedDateTime();
}

return (ZonedDateTime)value;
}

/**
* "Normalizes" the index into a {@code Map} by making no change to the index.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.elasticsearch.painless.lookup.PainlessCast;
import org.elasticsearch.painless.lookup.PainlessMethod;
import org.elasticsearch.painless.lookup.def;
import org.elasticsearch.script.JodaCompatibleZonedDateTime;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
Expand All @@ -20,7 +19,6 @@
import org.objectweb.asm.commons.Method;

import java.lang.reflect.Modifier;
import java.time.ZonedDateTime;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -62,10 +60,8 @@
import static org.elasticsearch.painless.WriterConstants.DEF_TO_P_SHORT_IMPLICIT;
import static org.elasticsearch.painless.WriterConstants.DEF_TO_STRING_EXPLICIT;
import static org.elasticsearch.painless.WriterConstants.DEF_TO_STRING_IMPLICIT;
import static org.elasticsearch.painless.WriterConstants.DEF_TO_ZONEDDATETIME;
import static org.elasticsearch.painless.WriterConstants.DEF_UTIL_TYPE;
import static org.elasticsearch.painless.WriterConstants.INDY_STRING_CONCAT_BOOTSTRAP_HANDLE;
import static org.elasticsearch.painless.WriterConstants.JCZDT_TO_ZONEDDATETIME;
import static org.elasticsearch.painless.WriterConstants.LAMBDA_BOOTSTRAP_HANDLE;
import static org.elasticsearch.painless.WriterConstants.MAX_INDY_STRING_CONCAT_ARGS;
import static org.elasticsearch.painless.WriterConstants.PAINLESS_ERROR_TYPE;
Expand Down Expand Up @@ -153,9 +149,6 @@ public void writeCast(PainlessCast cast) {
invokeStatic(UTILITY_TYPE, CHAR_TO_STRING);
} else if (cast.originalType == String.class && cast.targetType == char.class) {
invokeStatic(UTILITY_TYPE, STRING_TO_CHAR);
// TODO: remove this when the transition from Joda to Java datetimes is completed
} else if (cast.originalType == JodaCompatibleZonedDateTime.class && cast.targetType == ZonedDateTime.class) {
invokeStatic(UTILITY_TYPE, JCZDT_TO_ZONEDDATETIME);
} else if (cast.unboxOriginalType != null && cast.boxTargetType != null) {
unbox(getType(cast.unboxOriginalType));
writeCast(cast.unboxOriginalType, cast.boxTargetType);
Expand Down Expand Up @@ -191,8 +184,6 @@ public void writeCast(PainlessCast cast) {
else if (cast.targetType == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_FLOAT_EXPLICIT);
else if (cast.targetType == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_DOUBLE_EXPLICIT);
else if (cast.targetType == String.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_STRING_EXPLICIT);
// TODO: remove this when the transition from Joda to Java datetimes is completed
else if (cast.targetType == ZonedDateTime.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_ZONEDDATETIME);
else {
writeCast(cast.originalType, cast.targetType);
}
Expand All @@ -214,8 +205,6 @@ public void writeCast(PainlessCast cast) {
else if (cast.targetType == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_FLOAT_IMPLICIT);
else if (cast.targetType == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_DOUBLE_IMPLICIT);
else if (cast.targetType == String.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_STRING_IMPLICIT);
// TODO: remove this when the transition from Joda to Java datetimes is completed
else if (cast.targetType == ZonedDateTime.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_ZONEDDATETIME);
else {
writeCast(cast.originalType, cast.targetType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@

package org.elasticsearch.painless;

import org.elasticsearch.script.JodaCompatibleZonedDateTime;

import java.time.ZonedDateTime;

/**
* A set of methods for non-native boxing and non-native
* exact math operations used at both compile-time and runtime.
Expand All @@ -36,10 +32,5 @@ public static char StringTochar(final String value) {
return value.charAt(0);
}

// TODO: remove this when the transition from Joda to Java datetimes is completed
public static ZonedDateTime JCZDTToZonedDateTime(final JodaCompatibleZonedDateTime jczdt) {
return jczdt.getZonedDateTime();
}

private Utility() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

package org.elasticsearch.painless;

import org.elasticsearch.script.JodaCompatibleZonedDateTime;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
Expand All @@ -18,7 +17,6 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
Expand Down Expand Up @@ -58,10 +56,6 @@ public final class WriterConstants {
public static final Method STRING_TO_CHAR = getAsmMethod(char.class, "StringTochar", String.class);
public static final Method CHAR_TO_STRING = getAsmMethod(String.class, "charToString", char.class);

// TODO: remove this when the transition from Joda to Java datetimes is completed
public static final Method JCZDT_TO_ZONEDDATETIME =
getAsmMethod(ZonedDateTime.class, "JCZDTToZonedDateTime", JodaCompatibleZonedDateTime.class);

/**
* A Method instance for {@linkplain Pattern}. This isn't available from PainlessLookup because we intentionally don't add it
* there so that the script can't create regexes without this syntax. Essentially, our static regex syntax has a monopoly on building
Expand Down Expand Up @@ -117,9 +111,6 @@ public final class WriterConstants {
public static final Method DEF_TO_STRING_IMPLICIT = getAsmMethod(String.class, "defToStringImplicit", Object.class);
public static final Method DEF_TO_STRING_EXPLICIT = getAsmMethod(String.class, "defToStringExplicit", Object.class);

// TODO: remove this when the transition from Joda to Java datetimes is completed
public static final Method DEF_TO_ZONEDDATETIME = getAsmMethod(ZonedDateTime.class, "defToZonedDateTime", Object.class);

/** invokedynamic bootstrap for lambda expression/method references */
public static final MethodType LAMBDA_BOOTSTRAP_TYPE =
MethodType.methodType(CallSite.class, MethodHandles.Lookup.class, String.class, MethodType.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import org.elasticsearch.common.hash.MessageDigests;

import java.nio.charset.StandardCharsets;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
Expand Down Expand Up @@ -714,4 +716,8 @@ public static Matcher matcher(Pattern receiver, int limitFactor, CharSequence in
public static long toEpochMilli(TemporalAccessor v) {
return v.getLong(ChronoField.INSTANT_SECONDS) * 1_000 + v.get(ChronoField.NANO_OF_SECOND) / 1_000_000;
}

public static DayOfWeek getDayOfWeekEnum(ZonedDateTime receiver) {
return receiver.getDayOfWeek();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ class java.time.YearMonth {
class java.time.ZonedDateTime {
int getDayOfMonth()
DayOfWeek getDayOfWeek()
DayOfWeek org.elasticsearch.painless.api.Augmentation getDayOfWeekEnum()
int getDayOfYear()
int getHour()
LocalDate toLocalDate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ static_import {
double decayNumericLinear(double, double, double, double, double)bound_to org.elasticsearch.script.ScoreScriptUtils$DecayNumericLinear
double decayNumericExp(double, double, double, double, double) bound_to org.elasticsearch.script.ScoreScriptUtils$DecayNumericExp
double decayNumericGauss(double, double, double, double, double) bound_to org.elasticsearch.script.ScoreScriptUtils$DecayNumericGauss
double decayDateLinear(String, String, String, double, JodaCompatibleZonedDateTime) bound_to org.elasticsearch.script.ScoreScriptUtils$DecayDateLinear
double decayDateExp(String, String, String, double, JodaCompatibleZonedDateTime) bound_to org.elasticsearch.script.ScoreScriptUtils$DecayDateExp
double decayDateGauss(String, String, String, double, JodaCompatibleZonedDateTime) bound_to org.elasticsearch.script.ScoreScriptUtils$DecayDateGauss
double decayDateLinear(String, String, String, double, ZonedDateTime) bound_to org.elasticsearch.script.ScoreScriptUtils$DecayDateLinear
double decayDateExp(String, String, String, double, ZonedDateTime) bound_to org.elasticsearch.script.ScoreScriptUtils$DecayDateExp
double decayDateGauss(String, String, String, double, ZonedDateTime) bound_to org.elasticsearch.script.ScoreScriptUtils$DecayDateGauss
}

Original file line number Diff line number Diff line change
Expand Up @@ -69,84 +69,9 @@ class org.elasticsearch.index.fielddata.ScriptDocValues$Longs {
long getValue()
}

class org.elasticsearch.script.JodaCompatibleZonedDateTime {
##### ZonedDateTime methods
int getDayOfMonth()
int getDayOfYear()
int getHour()
LocalDate toLocalDate()
LocalDateTime toLocalDateTime()
int getMinute()
Month getMonth()
int getMonthValue()
int getNano()
int getSecond()
int getYear()
ZonedDateTime minus(TemporalAmount)
ZonedDateTime minus(long,TemporalUnit)
ZonedDateTime minusYears(long)
ZonedDateTime minusMonths(long)
ZonedDateTime minusWeeks(long)
ZonedDateTime minusDays(long)
ZonedDateTime minusHours(long)
ZonedDateTime minusMinutes(long)
ZonedDateTime minusSeconds(long)
ZonedDateTime minusNanos(long)
ZonedDateTime plus(TemporalAmount)
ZonedDateTime plus(long,TemporalUnit)
ZonedDateTime plusDays(long)
ZonedDateTime plusHours(long)
ZonedDateTime plusMinutes(long)
ZonedDateTime plusMonths(long)
ZonedDateTime plusNanos(long)
ZonedDateTime plusSeconds(long)
ZonedDateTime plusWeeks(long)
ZonedDateTime plusYears(long)
OffsetDateTime toOffsetDateTime()
ZonedDateTime truncatedTo(TemporalUnit)
ZonedDateTime with(TemporalAdjuster)
ZonedDateTime with(TemporalField,long)
ZonedDateTime withDayOfMonth(int)
ZonedDateTime withDayOfYear(int)
ZonedDateTime withEarlierOffsetAtOverlap()
ZonedDateTime withFixedOffsetZone()
ZonedDateTime withHour(int)
ZonedDateTime withLaterOffsetAtOverlap()
ZonedDateTime withMinute(int)
ZonedDateTime withMonth(int)
ZonedDateTime withNano(int)
ZonedDateTime withSecond(int)
ZonedDateTime withYear(int)
ZonedDateTime withZoneSameLocal(ZoneId)
ZonedDateTime withZoneSameInstant(ZoneId)

#### Joda time methods
long getMillis()
int getCenturyOfEra()
int getEra()
int getHourOfDay()
int getMillisOfDay()
int getMillisOfSecond()
int getMinuteOfDay()
int getMinuteOfHour()
int getMonthOfYear()
int getSecondOfDay()
int getSecondOfMinute()
int getWeekOfWeekyear()
int getWeekyear()
int getYearOfCentury()
int getYearOfEra()
String toString(String)
String toString(String,Locale)

# conflicting methods
DayOfWeek getDayOfWeekEnum()
int getDayOfWeek()
}

class org.elasticsearch.index.fielddata.ScriptDocValues$Dates {
JodaCompatibleZonedDateTime get(int)
JodaCompatibleZonedDateTime getValue()
ZonedDateTime get(int)
ZonedDateTime getValue()
}

class org.elasticsearch.index.fielddata.ScriptDocValues$Doubles {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,6 @@ public void testStatic() {
assertEquals(15.5f, exec("staticAddFloatsTest(6.5f, 9.0f)"));
}

// TODO: remove this when the transition from Joda to Java datetimes is completed
public void testJCZDTToZonedDateTime() {
assertEquals(0L, exec(
"Instant instant = Instant.ofEpochMilli(434931330000L);" +
"JodaCompatibleZonedDateTime d = new JodaCompatibleZonedDateTime(instant, ZoneId.of('Z'));" +
"ZonedDateTime t = d;" +
"return ChronoUnit.MILLIS.between(d, t);"
));
}

public void testRandomUUID() {
assertTrue(
Pattern.compile("\\p{XDigit}{8}(-\\p{XDigit}{4}){3}-\\p{XDigit}{12}").matcher(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,17 +685,4 @@ public void testConstFoldingDefCast() {
assertFalse((boolean)exec("def chr = (char)10L; return (chr > (byte)10);"));
assertFalse((boolean)exec("def chr = (char)10L; return (chr > (double)(byte)(char)10);"));
}

// TODO: remove this when the transition from Joda to Java datetimes is completed
public void testdefToZonedDateTime() {
assertEquals(0L, exec(
"Instant instant = Instant.ofEpochMilli(434931330000L);" +
"def d = new JodaCompatibleZonedDateTime(instant, ZoneId.of('Z'));" +
"def x = new HashMap(); x.put('dt', d);" +
"ZonedDateTime t = x['dt'];" +
"def y = t;" +
"t = y;" +
"return ChronoUnit.MILLIS.between(d, t);"
));
}
}
Loading

0 comments on commit 086ba1a

Please sign in to comment.