Skip to content

Commit

Permalink
[Remove] JodaCompatibleZonedDateTime
Browse files Browse the repository at this point in the history
This commit removes the JodaCompatibleZonedDateTime class and tests in
favor of Java time.

Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
  • Loading branch information
nknize committed Aug 15, 2023
1 parent 152321b commit 80e7045
Show file tree
Hide file tree
Showing 22 changed files with 35 additions and 816 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.settings.SecureString;
import org.opensearch.script.JodaCompatibleZonedDateTime;
import org.opensearch.test.OpenSearchTestCase;

import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -400,10 +400,10 @@ public void testOptionalInstantSerialization() throws IOException {
}
}

public void testJodaDateTimeSerialization() throws IOException {
public void testJavaDateTimeSerialization() throws IOException {
final BytesStreamOutput output = new BytesStreamOutput();
long millis = randomIntBetween(0, Integer.MAX_VALUE);
JodaCompatibleZonedDateTime time = new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(millis), ZoneOffset.ofHours(-7));
ZonedDateTime time = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneOffset.ofHours(-7));
output.writeGenericValue(time);

final BytesReference bytesReference = output.bytes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
import org.opensearch.painless.lookup.PainlessCast;
import org.opensearch.painless.lookup.PainlessLookupUtility;
import org.opensearch.painless.lookup.def;
import org.opensearch.script.JodaCompatibleZonedDateTime;

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

/**
Expand Down Expand Up @@ -87,19 +85,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 @@ -36,13 +36,11 @@
import org.opensearch.painless.lookup.PainlessLookupUtility;
import org.opensearch.painless.lookup.PainlessMethod;
import org.opensearch.painless.symbol.FunctionTable;
import org.opensearch.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 @@ -1506,15 +1504,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 @@ -35,7 +35,6 @@
import org.opensearch.painless.lookup.PainlessCast;
import org.opensearch.painless.lookup.PainlessMethod;
import org.opensearch.painless.lookup.def;
import org.opensearch.script.JodaCompatibleZonedDateTime;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
Expand All @@ -44,7 +43,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 @@ -86,10 +84,8 @@
import static org.opensearch.painless.WriterConstants.DEF_TO_P_SHORT_IMPLICIT;
import static org.opensearch.painless.WriterConstants.DEF_TO_STRING_EXPLICIT;
import static org.opensearch.painless.WriterConstants.DEF_TO_STRING_IMPLICIT;
import static org.opensearch.painless.WriterConstants.DEF_TO_ZONEDDATETIME;
import static org.opensearch.painless.WriterConstants.DEF_UTIL_TYPE;
import static org.opensearch.painless.WriterConstants.INDY_STRING_CONCAT_BOOTSTRAP_HANDLE;
import static org.opensearch.painless.WriterConstants.JCZDT_TO_ZONEDDATETIME;
import static org.opensearch.painless.WriterConstants.LAMBDA_BOOTSTRAP_HANDLE;
import static org.opensearch.painless.WriterConstants.MAX_INDY_STRING_CONCAT_ARGS;
import static org.opensearch.painless.WriterConstants.PAINLESS_ERROR_TYPE;
Expand Down Expand Up @@ -181,9 +177,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 @@ -219,8 +212,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 @@ -242,8 +233,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 @@ -32,10 +32,6 @@

package org.opensearch.painless;

import org.opensearch.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 @@ -62,10 +58,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 @@ -32,7 +32,6 @@

package org.opensearch.painless;

import org.opensearch.script.JodaCompatibleZonedDateTime;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
Expand All @@ -42,7 +41,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 @@ -82,13 +80,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 @@ -157,9 +148,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.opensearch.common.hash.MessageDigests;

import java.nio.charset.StandardCharsets;
import java.time.DayOfWeek;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
Expand Down Expand Up @@ -721,4 +723,8 @@ public static Matcher matcher(Pattern receiver, int limitFactor, CharSequence in
}
return receiver.matcher(new LimitedCharSequence(input, receiver, limitFactor));
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -79,62 +79,9 @@ class org.opensearch.index.fielddata.ScriptDocValues$UnsignedLongs {
BigInteger getValue()
}

class org.opensearch.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)
DayOfWeek getDayOfWeekEnum()
}

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

class org.opensearch.index.fielddata.ScriptDocValues$Doubles {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,20 +709,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);"
)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
# allowlist for tests

# TODO: remove this when the transition from Joda to Java datetimes is completed
class org.opensearch.script.JodaCompatibleZonedDateTime {
(Instant, ZoneId)
}

# for unit tests only
class org.opensearch.painless.api.Json {
def load(String)
Expand Down
Loading

0 comments on commit 80e7045

Please sign in to comment.