Skip to content

Commit 393430b

Browse files
committed
Add random test cases for date nanos
1 parent 92e2877 commit 393430b

File tree

1 file changed

+67
-0
lines changed
  • x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/score

1 file changed

+67
-0
lines changed

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/score/DecayTests.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import static org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes.CARTESIAN;
3535
import static org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes.GEO;
36+
import static org.hamcrest.Matchers.closeTo;
3637
import static org.hamcrest.Matchers.equalTo;
3738
import static org.hamcrest.Matchers.startsWith;
3839

@@ -585,6 +586,9 @@ public static Iterable<Object[]> parameters() {
585586
)
586587
);
587588

589+
// Datenanos random
590+
testCaseSuppliers.addAll(dateNanosRandomTestCases());
591+
588592
return parameterSuppliersFromTypedData(testCaseSuppliers);
589593
}
590594

@@ -1122,6 +1126,69 @@ private static List<TestCaseSupplier> dateNanosTestCase(
11221126
);
11231127
}
11241128

1129+
private static List<TestCaseSupplier> dateNanosRandomTestCases() {
1130+
return List.of(
1131+
new TestCaseSupplier(List.of(DataType.DATE_NANOS, DataType.DATE_NANOS, DataType.TIME_DURATION, DataType.SOURCE), () -> {
1132+
// 1970-01-01 in nanos
1133+
long minEpochNanos = 0L;
1134+
// 2070-01-01 in nanos
1135+
long maxEpochNanos = 3155673600000L * 1_000_000L;
1136+
long randomValue = randomLongBetween(minEpochNanos, maxEpochNanos);
1137+
long randomOrigin = randomLongBetween(minEpochNanos, maxEpochNanos);
1138+
1139+
// Max 1 year in milliseconds
1140+
long randomScaleMillis = randomNonNegativeLong() % (365L * 24 * 60 * 60 * 1000);
1141+
// Max 30 days in milliseconds
1142+
long randomOffsetMillis = randomNonNegativeLong() % (30L * 24 * 60 * 60 * 1000);
1143+
Duration randomScale = Duration.ofMillis(randomScaleMillis);
1144+
Duration randomOffset = Duration.ofMillis(randomOffsetMillis);
1145+
1146+
double randomDecay = randomDouble();
1147+
String randomType = randomFrom("linear", "gauss", "exp");
1148+
1149+
double scoreScriptNumericResult = dateNanosDecayWithScoreScript(
1150+
randomValue,
1151+
randomOrigin,
1152+
randomScale.toMillis(),
1153+
randomOffset.toMillis(),
1154+
randomDecay,
1155+
randomType
1156+
);
1157+
1158+
return new TestCaseSupplier.TestCase(
1159+
List.of(
1160+
new TestCaseSupplier.TypedData(randomValue, DataType.DATE_NANOS, "value"),
1161+
new TestCaseSupplier.TypedData(randomOrigin, DataType.DATE_NANOS, "origin").forceLiteral(),
1162+
new TestCaseSupplier.TypedData(randomScale, DataType.TIME_DURATION, "scale").forceLiteral(),
1163+
new TestCaseSupplier.TypedData(createOptionsMap(randomOffset, randomDecay, randomType), DataType.SOURCE, "options")
1164+
.forceLiteral()
1165+
),
1166+
startsWith("DecayDateNanosEvaluator["),
1167+
DataType.DOUBLE,
1168+
closeTo(scoreScriptNumericResult, 1e-10)
1169+
);
1170+
})
1171+
);
1172+
}
1173+
1174+
private static double dateNanosDecayWithScoreScript(long value, long origin, long scale, long offset, double decay, String type) {
1175+
long valueMillis = value / 1_000_000L;
1176+
long originMillis = origin / 1_000_000L;
1177+
1178+
String originStr = String.valueOf(originMillis);
1179+
String scaleStr = scale + "ms";
1180+
String offsetStr = offset + "ms";
1181+
1182+
ZonedDateTime valueDateTime = Instant.ofEpochMilli(valueMillis).atZone(ZoneId.of("UTC"));
1183+
1184+
return switch (type) {
1185+
case "linear" -> new ScoreScriptUtils.DecayDateLinear(originStr, scaleStr, offsetStr, decay).decayDateLinear(valueDateTime);
1186+
case "gauss" -> new ScoreScriptUtils.DecayDateGauss(originStr, scaleStr, offsetStr, decay).decayDateGauss(valueDateTime);
1187+
case "exp" -> new ScoreScriptUtils.DecayDateExp(originStr, scaleStr, offsetStr, decay).decayDateExp(valueDateTime);
1188+
default -> throw new IllegalArgumentException("Unknown decay function type [" + type + "]");
1189+
};
1190+
}
1191+
11251192
private static MapExpression createOptionsMap(Object offset, Double decay, String functionType) {
11261193
List<Expression> keyValuePairs = new ArrayList<>();
11271194

0 commit comments

Comments
 (0)