|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.expression.function.scalar.spatial; |
| 9 | + |
| 10 | +import org.apache.lucene.util.BytesRef; |
| 11 | +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; |
| 12 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 13 | +import org.elasticsearch.compute.ann.Evaluator; |
| 14 | +import org.elasticsearch.compute.ann.Fixed; |
| 15 | +import org.elasticsearch.compute.operator.EvalOperator; |
| 16 | +import org.elasticsearch.geometry.Point; |
| 17 | +import org.elasticsearch.geometry.utils.Geohash; |
| 18 | +import org.elasticsearch.xpack.esql.core.expression.Expression; |
| 19 | +import org.elasticsearch.xpack.esql.core.expression.FoldContext; |
| 20 | +import org.elasticsearch.xpack.esql.core.expression.function.scalar.BinaryScalarFunction; |
| 21 | +import org.elasticsearch.xpack.esql.core.tree.NodeInfo; |
| 22 | +import org.elasticsearch.xpack.esql.core.tree.Source; |
| 23 | +import org.elasticsearch.xpack.esql.core.type.DataType; |
| 24 | +import org.elasticsearch.xpack.esql.evaluator.mapper.EvaluatorMapper; |
| 25 | +import org.elasticsearch.xpack.esql.expression.function.Example; |
| 26 | +import org.elasticsearch.xpack.esql.expression.function.FunctionInfo; |
| 27 | +import org.elasticsearch.xpack.esql.expression.function.Param; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | + |
| 31 | +import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST; |
| 32 | +import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND; |
| 33 | +import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isType; |
| 34 | +import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isWholeNumber; |
| 35 | +import static org.elasticsearch.xpack.esql.core.type.DataType.GEO_POINT; |
| 36 | +import static org.elasticsearch.xpack.esql.core.type.DataType.KEYWORD; |
| 37 | +import static org.elasticsearch.xpack.esql.core.util.SpatialCoordinateTypes.GEO; |
| 38 | + |
| 39 | +/** |
| 40 | + * Calculates the geohash of geo_point geometries. |
| 41 | + */ |
| 42 | +public class StGeohash extends UnarySpatialBinaryFunction implements EvaluatorMapper { |
| 43 | + public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry( |
| 44 | + Expression.class, |
| 45 | + "StGeohash", |
| 46 | + StGeohash::new |
| 47 | + ); |
| 48 | + |
| 49 | + @FunctionInfo( |
| 50 | + returnType = "keyword", |
| 51 | + description = "Calculates the `geohash` of the supplied geo_point at the specified precision.", |
| 52 | + examples = @Example(file = "spatial-grid", tag = "st_geohash-grid") |
| 53 | + ) |
| 54 | + public StGeohash( |
| 55 | + Source source, |
| 56 | + @Param( |
| 57 | + name = "point", |
| 58 | + type = { "geo_point" }, |
| 59 | + description = "Expression of type `geo_point`. If `null`, the function returns `null`." |
| 60 | + ) Expression field, |
| 61 | + @Param( |
| 62 | + name = "precision", |
| 63 | + type = { "integer" }, |
| 64 | + description = "Expression of type `integer`. If `null`, the function returns `null`." |
| 65 | + ) Expression precision |
| 66 | + ) { |
| 67 | + this(source, field, precision, false); |
| 68 | + } |
| 69 | + |
| 70 | + private StGeohash(Source source, Expression field, Expression precision, boolean spatialDocValues) { |
| 71 | + super(source, field, precision, spatialDocValues); |
| 72 | + } |
| 73 | + |
| 74 | + private StGeohash(StreamInput in) throws IOException { |
| 75 | + super(in, false); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public UnarySpatialBinaryFunction withDocValues(boolean useDocValues) { |
| 80 | + // Only update the docValues flags if the field is found in the attributes |
| 81 | + boolean leftDV = this.spatialDocsValues || useDocValues; |
| 82 | + return new StGeohash(source(), left(), right(), leftDV); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public String getWriteableName() { |
| 87 | + return ENTRY.name; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public DataType dataType() { |
| 92 | + return KEYWORD; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + protected BinaryScalarFunction replaceChildren(Expression newLeft, Expression newRight) { |
| 97 | + return new StGeohash(source(), newLeft, newRight); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + protected NodeInfo<? extends Expression> info() { |
| 102 | + return NodeInfo.create(this, StGeohash::new, left(), right()); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + protected TypeResolution resolveType() { |
| 107 | + if (childrenResolved() == false) { |
| 108 | + return new TypeResolution("Unresolved children"); |
| 109 | + } |
| 110 | + |
| 111 | + TypeResolution resolution = isGeoPoint(left(), sourceText()); |
| 112 | + if (resolution.unresolved()) { |
| 113 | + return resolution; |
| 114 | + } |
| 115 | + |
| 116 | + resolution = isWholeNumber(right(), sourceText(), SECOND); |
| 117 | + if (resolution.unresolved()) { |
| 118 | + return resolution; |
| 119 | + } |
| 120 | + |
| 121 | + return TypeResolution.TYPE_RESOLVED; |
| 122 | + } |
| 123 | + |
| 124 | + protected static Expression.TypeResolution isGeoPoint(Expression e, String operationName) { |
| 125 | + |
| 126 | + return isType(e, t -> t.equals(GEO_POINT), operationName, FIRST, GEO_POINT.typeName()); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) { |
| 131 | + // TODO: Implement |
| 132 | + if (left().foldable()) { |
| 133 | + // Assume right is not foldable, since that would be dealt with in isFoldable() and fold() |
| 134 | + var point = (BytesRef) left().fold(toEvaluator.foldCtx()); |
| 135 | + return new StGeohashFromLiteralAndFieldEvaluator.Factory(source(), point, toEvaluator.apply(right())); |
| 136 | + } else if (right().foldable()) { |
| 137 | + // Assume left is not foldable, since that would be dealt with in isFoldable() and fold() |
| 138 | + int precision = (int) right().fold(toEvaluator.foldCtx()); |
| 139 | + return spatialDocsValues |
| 140 | + ? new StGeohashFromFieldDocValuesAndLiteralEvaluator.Factory(source(), toEvaluator.apply(left()), precision) |
| 141 | + : new StGeohashFromFieldAndLiteralEvaluator.Factory(source(), toEvaluator.apply(left()), precision); |
| 142 | + } else { |
| 143 | + // Both arguments come from index fields |
| 144 | + return spatialDocsValues |
| 145 | + ? new StGeohashFromFieldDocValuesAndFieldEvaluator.Factory(source(), toEvaluator.apply(left()), toEvaluator.apply(right())) |
| 146 | + : new StGeohashFromFieldAndFieldEvaluator.Factory(source(), toEvaluator.apply(left()), toEvaluator.apply(right())); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public Object fold(FoldContext ctx) { |
| 152 | + var point = (BytesRef) left().fold(ctx); |
| 153 | + int precision = (int) right().fold(ctx); |
| 154 | + return calculateGeohash(GEO.wkbAsPoint(point), precision); |
| 155 | + } |
| 156 | + |
| 157 | + @Evaluator(extraName = "FromFieldAndLiteral", warnExceptions = { IllegalArgumentException.class }) |
| 158 | + static BytesRef fromFieldAndLiteral(BytesRef in, @Fixed int precision) { |
| 159 | + return calculateGeohash(GEO.wkbAsPoint(in), precision); |
| 160 | + } |
| 161 | + |
| 162 | + @Evaluator(extraName = "FromFieldDocValuesAndLiteral", warnExceptions = { IllegalArgumentException.class }) |
| 163 | + static BytesRef fromFieldDocValuesAndLiteral(long encoded, @Fixed int precision) { |
| 164 | + return calculateGeohash(GEO.longAsPoint(encoded), precision); |
| 165 | + } |
| 166 | + |
| 167 | + @Evaluator(extraName = "FromFieldAndField", warnExceptions = { IllegalArgumentException.class }) |
| 168 | + static BytesRef fromFieldAndField(BytesRef in, int precision) { |
| 169 | + return calculateGeohash(GEO.wkbAsPoint(in), precision); |
| 170 | + } |
| 171 | + |
| 172 | + @Evaluator(extraName = "FromFieldDocValuesAndField", warnExceptions = { IllegalArgumentException.class }) |
| 173 | + static BytesRef fromFieldDocValuesAndField(long encoded, int precision) { |
| 174 | + return calculateGeohash(GEO.longAsPoint(encoded), precision); |
| 175 | + } |
| 176 | + |
| 177 | + @Evaluator(extraName = "FromLiteralAndField", warnExceptions = { IllegalArgumentException.class }) |
| 178 | + static BytesRef fromLiteralAndField(@Fixed BytesRef in, int precision) { |
| 179 | + return calculateGeohash(GEO.wkbAsPoint(in), precision); |
| 180 | + } |
| 181 | + |
| 182 | + protected static BytesRef calculateGeohash(Point point, int precision) { |
| 183 | + return new BytesRef(Geohash.stringEncode(point.getX(), point.getY(), precision)); |
| 184 | + } |
| 185 | +} |
0 commit comments