-
Notifications
You must be signed in to change notification settings - Fork 25.6k
ES|QL: Add unsigned long and mixed numeric types support to Decay function #134440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
d57a8e9
58a230c
408d4c9
c86e56c
f43b505
1319903
eee150f
13f103a
e35b86f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| import org.elasticsearch.xpack.esql.core.tree.NodeInfo; | ||
| import org.elasticsearch.xpack.esql.core.tree.Source; | ||
| import org.elasticsearch.xpack.esql.core.type.DataType; | ||
| import org.elasticsearch.xpack.esql.core.util.NumericUtils; | ||
| import org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes; | ||
| import org.elasticsearch.xpack.esql.expression.function.Example; | ||
| import org.elasticsearch.xpack.esql.expression.function.FunctionAppliesTo; | ||
|
|
@@ -63,6 +64,7 @@ | |
| import static org.elasticsearch.xpack.esql.core.type.DataType.LONG; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.TEXT; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.TIME_DURATION; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.UNSIGNED_LONG; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.isDateNanos; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.isGeoPoint; | ||
| import static org.elasticsearch.xpack.esql.core.type.DataType.isMillisOrNanos; | ||
|
|
@@ -91,7 +93,7 @@ public class Decay extends EsqlScalarFunction implements OptionalArgument, PostO | |
|
|
||
| private static final Map<String, Collection<DataType>> ALLOWED_OPTIONS = Map.of( | ||
| OFFSET, | ||
| Set.of(TIME_DURATION, INTEGER, LONG, DOUBLE, KEYWORD, TEXT), | ||
| Set.of(TIME_DURATION, INTEGER, LONG, UNSIGNED_LONG, DOUBLE, KEYWORD, TEXT), | ||
| DECAY, | ||
| Set.of(DOUBLE), | ||
| TYPE, | ||
|
|
@@ -140,25 +142,25 @@ public Decay( | |
| Source source, | ||
| @Param( | ||
| name = "value", | ||
| type = { "double", "integer", "long", "date", "date_nanos", "geo_point", "cartesian_point" }, | ||
| type = { "double", "integer", "long", "unsigned_long", "date", "date_nanos", "geo_point", "cartesian_point" }, | ||
| description = "The input value to apply decay scoring to." | ||
| ) Expression value, | ||
| @Param( | ||
| name = ORIGIN, | ||
| type = { "double", "integer", "long", "date", "date_nanos", "geo_point", "cartesian_point" }, | ||
| type = { "double", "integer", "long", "unsigned_long", "date", "date_nanos", "geo_point", "cartesian_point" }, | ||
| description = "Central point from which the distances are calculated." | ||
| ) Expression origin, | ||
| @Param( | ||
| name = SCALE, | ||
| type = { "double", "integer", "long", "time_duration", "keyword", "text" }, | ||
| type = { "double", "integer", "long", "unsigned_long", "time_duration", "keyword", "text" }, | ||
| description = "Distance from the origin where the function returns the decay value." | ||
| ) Expression scale, | ||
| @MapParam( | ||
| name = "options", | ||
| params = { | ||
| @MapParam.MapParamEntry( | ||
| name = OFFSET, | ||
| type = { "double", "integer", "long", "time_duration", "keyword", "text" }, | ||
| type = { "double", "integer", "long", "unsigned_long", "time_duration", "keyword", "text" }, | ||
| description = "Distance from the origin where no decay occurs." | ||
| ), | ||
| @MapParam.MapParamEntry( | ||
|
|
@@ -285,8 +287,8 @@ public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvalua | |
| FoldContext foldCtx = toEvaluator.foldCtx(); | ||
|
|
||
| // Constants | ||
| Object originFolded = origin.fold(foldCtx); | ||
| Object scaleFolded = getFoldedScale(foldCtx, valueDataType); | ||
| Object originFolded = convertToExpectedType(origin.fold(foldCtx), origin.dataType(), valueDataType); | ||
| Object scaleFolded = convertToExpectedType(getFoldedScale(foldCtx, valueDataType), scale.dataType(), valueDataType); | ||
| Object offsetFolded = getOffset(foldCtx, valueDataType, offsetExpr); | ||
| Double decayFolded = decayExpr != null ? (Double) decayExpr.fold(foldCtx) : DEFAULT_DECAY; | ||
| DecayFunction decayFunction = DecayFunction.fromBytesRef(typeExpr != null ? (BytesRef) typeExpr.fold(foldCtx) : DEFAULT_FUNCTION); | ||
|
|
@@ -319,6 +321,15 @@ public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvalua | |
| decayFolded, | ||
| decayFunction | ||
| ); | ||
| case UNSIGNED_LONG -> new DecayUnsignedLongEvaluator.Factory( | ||
| source(), | ||
| valueFactory, | ||
| (Long) originFolded, | ||
| (Long) scaleFolded, | ||
| (Long) offsetFolded, | ||
| decayFolded, | ||
| decayFunction | ||
| ); | ||
| case GEO_POINT -> new DecayGeoPointEvaluator.Factory( | ||
| source(), | ||
| valueFactory, | ||
|
|
@@ -403,7 +414,24 @@ static double process( | |
| @Fixed DecayFunction decayFunction | ||
| ) { | ||
| return decayFunction.numericDecay(value, origin, scale, offset, decay); | ||
| } | ||
|
|
||
| @Evaluator(extraName = "UnsignedLong") | ||
| static double processUnsignedLong( | ||
| long value, | ||
| @Fixed long origin, | ||
| @Fixed long scale, | ||
| @Fixed long offset, | ||
| @Fixed double decay, | ||
| @Fixed DecayFunction decayFunction | ||
| ) { | ||
| return decayFunction.numericDecay( | ||
| NumericUtils.unsignedLongToDouble(value), | ||
| NumericUtils.unsignedLongToDouble(origin), | ||
| NumericUtils.unsignedLongToDouble(scale), | ||
| NumericUtils.unsignedLongToDouble(offset), | ||
| decay | ||
| ); | ||
| } | ||
|
|
||
| @Evaluator(extraName = "GeoPoint") | ||
|
|
@@ -634,7 +662,7 @@ private Long getTemporalOffsetAsNanos(FoldContext foldCtx, Expression offset) { | |
| private Object getDefaultOffset(DataType valueDataType) { | ||
| return switch (valueDataType) { | ||
| case INTEGER -> DEFAULT_INTEGER_OFFSET; | ||
| case LONG -> DEFAULT_LONG_OFFSET; | ||
| case LONG, UNSIGNED_LONG -> DEFAULT_LONG_OFFSET; | ||
| case DOUBLE -> DEFAULT_DOUBLE_OFFSET; | ||
| case GEO_POINT -> DEFAULT_GEO_POINT_OFFSET; | ||
| case CARTESIAN_POINT -> DEFAULT_CARTESIAN_POINT_OFFSET; | ||
|
|
@@ -643,4 +671,36 @@ private Object getDefaultOffset(DataType valueDataType) { | |
| }; | ||
| } | ||
|
|
||
| private Object convertToExpectedType(Object value, DataType valueType, DataType targetType) { | ||
| if (targetType == INTEGER && value instanceof Integer) { | ||
|
||
| return value; | ||
| } | ||
| if (targetType == LONG && value instanceof Long) { | ||
| return value; | ||
| } | ||
| if (targetType == UNSIGNED_LONG && value instanceof Long) { | ||
| return value; | ||
| } | ||
| if (targetType == DOUBLE && value instanceof Double) { | ||
| return value; | ||
| } | ||
|
|
||
| // Unsigned longs are represented using (Long.MIN_VALUE, Long.MAX_VALUE), therefore we need to convert | ||
| // if the targetType is not "unsigned_long" | ||
| if (valueType == UNSIGNED_LONG && targetType != UNSIGNED_LONG) { | ||
| value = NumericUtils.unsignedLongToDouble(((Number) value).longValue()); | ||
| } | ||
|
|
||
| if (value instanceof Number num) { | ||
| return switch (targetType) { | ||
| case INTEGER -> num.intValue(); | ||
| case LONG, UNSIGNED_LONG -> num.longValue(); | ||
| case DOUBLE -> num.doubleValue(); | ||
| default -> value; | ||
| }; | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.