Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase;
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
import org.elasticsearch.xpack.esql.expression.function.scalar.score.Decay.DecayFunction;
import org.hamcrest.Matcher;

import java.math.BigDecimal;
Expand Down Expand Up @@ -638,9 +639,10 @@ private static List<TestCaseSupplier> intRandomTestCases() {
return List.of(new TestCaseSupplier(List.of(DataType.INTEGER, DataType.INTEGER, DataType.INTEGER, DataType.SOURCE), () -> {
int randomValue = randomInt();
int randomOrigin = randomInt();
int randomScale = randomInt();
// scale must be > 0
int randomScale = randomIntBetween(1, Integer.MAX_VALUE);
int randomOffset = randomInt();
double randomDecay = randomDouble();
double randomDecay = randomDecayOpenUnitInterval();
String randomType = getRandomType();

double scoreScriptNumericResult = intDecayWithScoreScript(
Expand Down Expand Up @@ -712,9 +714,9 @@ private static List<TestCaseSupplier> longRandomTestCases() {
return List.of(new TestCaseSupplier(List.of(DataType.LONG, DataType.LONG, DataType.LONG, DataType.SOURCE), () -> {
long randomValue = randomLong();
long randomOrigin = randomLong();
long randomScale = randomLong();
long randomScale = randomLongBetween(1L, Long.MAX_VALUE);
long randomOffset = randomLong();
double randomDecay = randomDouble();
double randomDecay = randomDecayOpenUnitInterval();
String randomType = randomFrom("linear", "gauss", "exp");

double scoreScriptNumericResult = longDecayWithScoreScript(
Expand Down Expand Up @@ -782,9 +784,9 @@ private static List<TestCaseSupplier> doubleRandomTestCases() {
return List.of(new TestCaseSupplier(List.of(DataType.DOUBLE, DataType.DOUBLE, DataType.DOUBLE, DataType.SOURCE), () -> {
double randomValue = randomLong();
double randomOrigin = randomLong();
double randomScale = randomLong();
double randomScale = randomLongBetween(1L, Long.MAX_VALUE);
double randomOffset = randomLong();
double randomDecay = randomDouble();
double randomDecay = randomDecayOpenUnitInterval();
String randomType = randomFrom("linear", "gauss", "exp");

double scoreScriptNumericResult = doubleDecayWithScoreScript(
Expand Down Expand Up @@ -882,7 +884,7 @@ private static List<TestCaseSupplier> geoPointRandomTestCases() {
GeoPoint randomOrigin = randomGeoPoint();
String randomScale = randomDistance();
String randomOffset = randomDistance();
double randomDecay = randomDouble();
double randomDecay = randomDecayOpenUnitInterval();
String randomType = randomDecayType();

double scoreScriptNumericResult = geoPointDecayWithScoreScript(
Expand Down Expand Up @@ -1061,7 +1063,7 @@ private static List<TestCaseSupplier> datetimeRandomTestCases() {
long randomOffsetMillis = randomNonNegativeLong() % (30L * 24 * 60 * 60 * 1000);
Duration randomScale = Duration.ofMillis(randomScaleMillis);
Duration randomOffset = Duration.ofMillis(randomOffsetMillis);
double randomDecay = randomDouble();
double randomDecay = randomDecayOpenUnitInterval();
String randomType = randomFrom("linear", "gauss", "exp");

double scoreScriptNumericResult = datetimeDecayWithScoreScript(
Expand Down Expand Up @@ -1148,14 +1150,14 @@ private static List<TestCaseSupplier> dateNanosRandomTestCases() {
Duration randomScale = Duration.ofMillis(randomScaleMillis);
Duration randomOffset = Duration.ofMillis(randomOffsetMillis);

double randomDecay = randomDouble();
double randomDecay = randomDecayOpenUnitInterval();
String randomType = randomFrom("linear", "gauss", "exp");

double scoreScriptNumericResult = dateNanosDecayWithScoreScript(
double scoreScriptNumericResult = expectedDateNanosTemporalDecay(
randomValue,
randomOrigin,
randomScale.toMillis(),
randomOffset.toMillis(),
randomScale.toNanos(),
randomOffset.toNanos(),
randomDecay,
randomType
);
Expand All @@ -1176,24 +1178,30 @@ private static List<TestCaseSupplier> dateNanosRandomTestCases() {
);
}

private static double dateNanosDecayWithScoreScript(long value, long origin, long scale, long offset, double decay, String type) {
long valueMillis = value / 1_000_000L;
long originMillis = origin / 1_000_000L;

String originStr = String.valueOf(originMillis);
String scaleStr = scale + "ms";
String offsetStr = offset + "ms";

ZonedDateTime valueDateTime = Instant.ofEpochMilli(valueMillis).atZone(ZoneId.of("UTC"));
private static double expectedDateNanosTemporalDecay(
long valueNanos,
long originNanos,
long scaleNanos,
long offsetNanos,
double decay,
String type
) {
return decayFunctionForType(type).temporalDecay(valueNanos, originNanos, scaleNanos, offsetNanos, decay);
}

private static DecayFunction decayFunctionForType(String type) {
return switch (type) {
case "linear" -> new ScoreScriptUtils.DecayDateLinear(originStr, scaleStr, offsetStr, decay).decayDateLinear(valueDateTime);
case "gauss" -> new ScoreScriptUtils.DecayDateGauss(originStr, scaleStr, offsetStr, decay).decayDateGauss(valueDateTime);
case "exp" -> new ScoreScriptUtils.DecayDateExp(originStr, scaleStr, offsetStr, decay).decayDateExp(valueDateTime);
case "linear" -> DecayFunction.LINEAR;
case "gauss" -> DecayFunction.GAUSSIAN;
case "exp" -> DecayFunction.EXPONENTIAL;
default -> throw new IllegalArgumentException("Unknown decay function type [" + type + "]");
};
}

private static double randomDecayOpenUnitInterval() {
return randomDoubleBetween(1e-12, Math.nextDown(1.0), true);
}

private static MapExpression createOptionsMap(Object offset, Double decay, String functionType) {
List<Expression> keyValuePairs = new ArrayList<>();

Expand Down
Loading