Skip to content

Commit e4e9672

Browse files
committed
Add test cases for defaults
1 parent 7929e31 commit e4e9672

File tree

2 files changed

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

2 files changed

+77
-26
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,16 @@ public class Decay extends EsqlScalarFunction implements OptionalArgument, PostA
9595
Set.of(KEYWORD)
9696
);
9797

98+
// Default offsets
9899
private static final Integer DEFAULT_INTEGER_OFFSET = 0;
99100
private static final Long DEFAULT_LONG_OFFSET = 0L;
100101
private static final Double DEFAULT_DOUBLE_OFFSET = 0.0;
101102
private static final BytesRef DEFAULT_GEO_POINT_OFFSET = new BytesRef("0m");
102103
private static final Double DEFAULT_CARTESIAN_POINT_OFFSET = 0.0;
103104
private static final Long DEFAULT_TEMPORAL_OFFSET = 0L;
105+
104106
private static final Double DEFAULT_DECAY = 0.5;
107+
105108
private static final String DEFAULT_FUNCTION = "linear";
106109

107110
private final Expression origin;

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

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.time.ZoneId;
2424
import java.util.ArrayList;
2525
import java.util.List;
26+
import java.util.Objects;
2627
import java.util.function.Supplier;
2728

2829
import static org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes.CARTESIAN;
@@ -62,6 +63,9 @@ public static Iterable<Object[]> parameters() {
6263
testCaseSuppliers.addAll(intTestCase(100, 17, 156, 23, 0.123, "gauss", 0.7334501109633149));
6364
testCaseSuppliers.addAll(intTestCase(2500, 0, 10, 0, 0.5, "gauss", 0.0));
6465

66+
// Int defaults
67+
testCaseSuppliers.addAll(intTestCase(10, 0, 10, null, null, null, 0.5));
68+
6569
// Long Linear
6670
testCaseSuppliers.addAll(longTestCase(0L, 10L, 10000000L, 200L, 0.33, "linear", 1.0));
6771
testCaseSuppliers.addAll(longTestCase(10L, 10L, 10000000L, 200L, 0.33, "linear", 1.0));
@@ -83,6 +87,9 @@ public static Iterable<Object[]> parameters() {
8387
testCaseSuppliers.addAll(longTestCase(300000L, 10L, 10000000L, 200L, 0.33, "gauss", 0.9990040963055015));
8488
testCaseSuppliers.addAll(longTestCase(123456789112123L, 10L, 10000000L, 200L, 0.33, "gauss", 0.0));
8589

90+
// Long defaults
91+
testCaseSuppliers.addAll(longTestCase(10L, 0L, 10L, null, null, null, 0.5));
92+
8693
// Double Linear
8794
testCaseSuppliers.addAll(doubleTestCase(0.0, 10.0, 10000000.0, 200.0, 0.25, "linear", 1.0));
8895
testCaseSuppliers.addAll(doubleTestCase(10.0, 10.0, 10000000.0, 200.0, 0.25, "linear", 1.0));
@@ -104,6 +111,9 @@ public static Iterable<Object[]> parameters() {
104111
testCaseSuppliers.addAll(doubleTestCase(300000.0, 10.0, 10000000.0, 200.0, 0.25, "gauss", 0.9987548570291238));
105112
testCaseSuppliers.addAll(doubleTestCase(123456789112.123, 10.0, 10000000.0, 200.0, 0.25, "gauss", 0.0));
106113

114+
// Double defaults
115+
testCaseSuppliers.addAll(doubleTestCase(10.0, 0.0, 10.0, null, null, null, 0.5));
116+
107117
// GeoPoint Linear
108118
testCaseSuppliers.addAll(geoPointTestCase("POINT (1.0 1.0)", "POINT (1 1)", "10000km", "10km", 0.33, "linear", 1.0));
109119
testCaseSuppliers.addAll(geoPointTestCase("POINT (0 0)", "POINT (1 1)", "10000km", "10km", 0.33, "linear", 0.9901342769495362));
@@ -143,6 +153,9 @@ public static Iterable<Object[]> parameters() {
143153
testCaseSuppliers.addAll(geoPointTestCaseKeywordScale("POINT (1 1)", "POINT (1 1)", "200km", "0km", 0.5, "linear", 1.0));
144154
testCaseSuppliers.addAll(geoPointOffsetKeywordTestCase("POINT (1 1)", "POINT (1 1)", "200km", "0km", 0.5, "linear", 1.0));
145155

156+
// GeoPoint defaults
157+
testCaseSuppliers.addAll(geoPointTestCase("POINT (12.3 45.6)", "POINT (1 1)", "10000km", null, null, null, 0.7459413262379005));
158+
146159
// CartesianPoint Linear
147160
testCaseSuppliers.addAll(cartesianPointTestCase("POINT (0 0)", "POINT (1 1)", 10000.0, 10.0, 0.33, "linear", 1.0));
148161
testCaseSuppliers.addAll(cartesianPointTestCase("POINT (1 1)", "POINT (1 1)", 10000.0, 10.0, 0.33, "linear", 1.0));
@@ -180,6 +193,11 @@ public static Iterable<Object[]> parameters() {
180193
cartesianPointTestCase("POINT (10000 20000)", "POINT (1 1)", 10000.0, 10.0, 0.33, "gauss", 0.003935602627423666)
181194
);
182195

196+
// CartesianPoint defaults
197+
testCaseSuppliers.addAll(
198+
cartesianPointTestCase("POINT (1000.0 2000.0)", "POINT (0 0)", 10000.0, null, null, null, 0.8881966011250104)
199+
);
200+
183201
// Datetime Linear
184202
testCaseSuppliers.addAll(
185203
datetimeTestCase(
@@ -351,6 +369,19 @@ public static Iterable<Object[]> parameters() {
351369
)
352370
);
353371

372+
// Datetime Defaults
373+
testCaseSuppliers.addAll(
374+
datetimeTestCase(
375+
LocalDateTime.of(2020, 8, 20, 0, 0, 0).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(),
376+
LocalDateTime.of(2000, 1, 1, 0, 0, 0).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(),
377+
Duration.ofDays(10000),
378+
null,
379+
null,
380+
null,
381+
0.62315
382+
)
383+
);
384+
354385
// Datenanos Linear
355386
testCaseSuppliers.addAll(
356387
dateNanosTestCase(
@@ -522,6 +553,19 @@ public static Iterable<Object[]> parameters() {
522553
)
523554
);
524555

556+
// Datenanos default
557+
testCaseSuppliers.addAll(
558+
dateNanosTestCase(
559+
LocalDateTime.of(2025, 8, 20, 0, 0, 0).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(),
560+
LocalDateTime.of(2000, 1, 1, 0, 0, 0).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(),
561+
Duration.ofDays(10000),
562+
null,
563+
null,
564+
null,
565+
0.53185
566+
)
567+
);
568+
525569
return parameterSuppliersFromTypedData(testCaseSuppliers);
526570
}
527571

@@ -540,7 +584,7 @@ private static List<TestCaseSupplier> intTestCase(
540584
int origin,
541585
int scale,
542586
Integer offset,
543-
double decay,
587+
Double decay,
544588
String functionType,
545589
double expected
546590
) {
@@ -568,7 +612,7 @@ private static List<TestCaseSupplier> longTestCase(
568612
long origin,
569613
long scale,
570614
Long offset,
571-
double decay,
615+
Double decay,
572616
String functionType,
573617
double expected
574618
) {
@@ -596,7 +640,7 @@ private static List<TestCaseSupplier> doubleTestCase(
596640
double origin,
597641
double scale,
598642
Double offset,
599-
double decay,
643+
Double decay,
600644
String functionType,
601645
double expected
602646
) {
@@ -624,7 +668,7 @@ private static List<TestCaseSupplier> geoPointTestCase(
624668
String originWkt,
625669
String scale,
626670
String offset,
627-
double decay,
671+
Double decay,
628672
String functionType,
629673
double expected
630674
) {
@@ -652,7 +696,7 @@ private static List<TestCaseSupplier> geoPointTestCaseKeywordScale(
652696
String originWkt,
653697
String scale,
654698
String offset,
655-
double decay,
699+
Double decay,
656700
String functionType,
657701
double expected
658702
) {
@@ -680,7 +724,7 @@ private static List<TestCaseSupplier> geoPointOffsetKeywordTestCase(
680724
String originWkt,
681725
String scale,
682726
String offset,
683-
double decay,
727+
Double decay,
684728
String functionType,
685729
double expected
686730
) {
@@ -707,8 +751,8 @@ private static List<TestCaseSupplier> cartesianPointTestCase(
707751
String valueWkt,
708752
String originWkt,
709753
double scale,
710-
double offset,
711-
double decay,
754+
Double offset,
755+
Double decay,
712756
String functionType,
713757
double expected
714758
) {
@@ -736,7 +780,7 @@ private static List<TestCaseSupplier> datetimeTestCase(
736780
long origin,
737781
Duration scale,
738782
Duration offset,
739-
double decay,
783+
Double decay,
740784
String functionType,
741785
double expected
742786
) {
@@ -764,7 +808,7 @@ private static List<TestCaseSupplier> dateNanosTestCase(
764808
long origin,
765809
Duration scale,
766810
Duration offset,
767-
double decay,
811+
Double decay,
768812
String functionType,
769813
double expected
770814
) {
@@ -787,30 +831,34 @@ private static List<TestCaseSupplier> dateNanosTestCase(
787831
);
788832
}
789833

790-
private static MapExpression createOptionsMap(Object offset, double decay, String functionType) {
834+
private static MapExpression createOptionsMap(Object offset, Double decay, String functionType) {
791835
List<Expression> keyValuePairs = new ArrayList<>();
792836

793837
// Offset
794-
keyValuePairs.add(Literal.keyword(Source.EMPTY, "offset"));
795-
if (offset instanceof Integer) {
796-
keyValuePairs.add(Literal.integer(Source.EMPTY, (Integer) offset));
797-
} else if (offset instanceof Long) {
798-
keyValuePairs.add(Literal.fromLong(Source.EMPTY, (Long) offset));
799-
} else if (offset instanceof Double) {
800-
keyValuePairs.add(Literal.fromDouble(Source.EMPTY, (Double) offset));
801-
} else if (offset instanceof String) {
802-
keyValuePairs.add(Literal.text(Source.EMPTY, (String) offset));
803-
} else if (offset instanceof Duration) {
804-
keyValuePairs.add(Literal.timeDuration(Source.EMPTY, (Duration) offset));
838+
if (Objects.nonNull(offset)) {
839+
keyValuePairs.add(Literal.keyword(Source.EMPTY, "offset"));
840+
switch (offset) {
841+
case Integer value -> keyValuePairs.add(Literal.integer(Source.EMPTY, value));
842+
case Long value -> keyValuePairs.add(Literal.fromLong(Source.EMPTY, value));
843+
case Double value -> keyValuePairs.add(Literal.fromDouble(Source.EMPTY, value));
844+
case String value -> keyValuePairs.add(Literal.text(Source.EMPTY, value));
845+
case Duration value -> keyValuePairs.add(Literal.timeDuration(Source.EMPTY, value));
846+
default -> {
847+
}
848+
}
805849
}
806850

807851
// Decay
808-
keyValuePairs.add(Literal.keyword(Source.EMPTY, "decay"));
809-
keyValuePairs.add(Literal.fromDouble(Source.EMPTY, decay));
852+
if (Objects.nonNull(decay)) {
853+
keyValuePairs.add(Literal.keyword(Source.EMPTY, "decay"));
854+
keyValuePairs.add(Literal.fromDouble(Source.EMPTY, decay));
855+
}
810856

811857
// Type
812-
keyValuePairs.add(Literal.keyword(Source.EMPTY, "type"));
813-
keyValuePairs.add(Literal.keyword(Source.EMPTY, functionType));
858+
if (Objects.nonNull(functionType)) {
859+
keyValuePairs.add(Literal.keyword(Source.EMPTY, "type"));
860+
keyValuePairs.add(Literal.keyword(Source.EMPTY, functionType));
861+
}
814862

815863
return new MapExpression(Source.EMPTY, keyValuePairs);
816864
}

0 commit comments

Comments
 (0)