|
33 | 33 |
|
34 | 34 | import static org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes.CARTESIAN; |
35 | 35 | import static org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes.GEO; |
| 36 | +import static org.hamcrest.Matchers.closeTo; |
36 | 37 | import static org.hamcrest.Matchers.equalTo; |
37 | 38 | import static org.hamcrest.Matchers.startsWith; |
38 | 39 |
|
@@ -585,6 +586,9 @@ public static Iterable<Object[]> parameters() { |
585 | 586 | ) |
586 | 587 | ); |
587 | 588 |
|
| 589 | + // Datenanos random |
| 590 | + testCaseSuppliers.addAll(dateNanosRandomTestCases()); |
| 591 | + |
588 | 592 | return parameterSuppliersFromTypedData(testCaseSuppliers); |
589 | 593 | } |
590 | 594 |
|
@@ -1122,6 +1126,69 @@ private static List<TestCaseSupplier> dateNanosTestCase( |
1122 | 1126 | ); |
1123 | 1127 | } |
1124 | 1128 |
|
| 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 | + |
1125 | 1192 | private static MapExpression createOptionsMap(Object offset, Double decay, String functionType) { |
1126 | 1193 | List<Expression> keyValuePairs = new ArrayList<>(); |
1127 | 1194 |
|
|
0 commit comments