Skip to content

Commit

Permalink
data type conversions 7
Browse files Browse the repository at this point in the history
  • Loading branch information
fang-xing-esql committed Mar 26, 2024
1 parent fb08dbf commit b61d785
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,20 @@
import org.elasticsearch.compute.data.IntBlock;
import org.elasticsearch.compute.data.LongBlock;
import org.elasticsearch.compute.lucene.UnsupportedValueSource;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xpack.core.esql.action.ColumnInfo;
import org.elasticsearch.xpack.versionfield.Version;

import java.io.IOException;

import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.dateTimeToString;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.ipToString;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.spatialToString;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.versionToString;
import static org.elasticsearch.xpack.ql.util.NumericUtils.unsignedLongAsNumber;
import static org.elasticsearch.xpack.ql.util.SpatialCoordinateTypes.CARTESIAN;
import static org.elasticsearch.xpack.ql.util.SpatialCoordinateTypes.GEO;

abstract class PositionToXContent {
protected final Block block;
Expand Down Expand Up @@ -109,7 +108,7 @@ protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Pa
protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Params params, int valueIndex)
throws IOException {
BytesRef val = ((BytesRefBlock) block).getBytesRef(valueIndex, scratch);
return builder.value(DocValueFormat.IP.format(val));
return builder.value(ipToString(val));
}
};
case "date" -> new PositionToXContent(block) {
Expand All @@ -120,18 +119,11 @@ protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Pa
return builder.value(dateTimeToString(longVal));
}
};
case "geo_point", "geo_shape" -> new PositionToXContent(block) {
case "geo_point", "geo_shape", "cartesian_point", "cartesian_shape" -> new PositionToXContent(block) {
@Override
protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Params params, int valueIndex)
throws IOException {
return builder.value(GEO.wkbToWkt(((BytesRefBlock) block).getBytesRef(valueIndex, scratch)));
}
};
case "cartesian_point", "cartesian_shape" -> new PositionToXContent(block) {
@Override
protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Params params, int valueIndex)
throws IOException {
return builder.value(CARTESIAN.wkbToWkt(((BytesRefBlock) block).getBytesRef(valueIndex, scratch)));
return builder.value(spatialToString(((BytesRefBlock) block).getBytesRef(valueIndex, scratch)));
}
};
case "boolean" -> new PositionToXContent(block) {
Expand All @@ -146,7 +138,7 @@ protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Pa
protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Params params, int valueIndex)
throws IOException {
BytesRef val = ((BytesRefBlock) block).getBytesRef(valueIndex, scratch);
return builder.value(new Version(val).toString());
return builder.value(versionToString(val));
}
};
case "null" -> new PositionToXContent(block) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public static long doubleToUnsignedLong(double field) {
return NumericUtils.asLongUnsigned(safeToUnsignedLong(field));
}

public static int unsignedLongToInt(Long field) {
public static int unsignedLongToInt(long field) {
Number n = NumericUtils.unsignedLongAsNumber(field);
int i = n.intValue();
if (i != n.longValue()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.type.DataTypes;
import org.elasticsearch.xpack.ql.util.NumericUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.bigIntegerToUnsignedLong;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.unsignedLongToDouble;

public class Log10Tests extends AbstractFunctionTestCase {
Expand Down Expand Up @@ -56,7 +56,7 @@ public static Iterable<Object[]> parameters() {
suppliers,
"Log10UnsignedLongEvaluator[val=" + read + "]",
DataTypes.DOUBLE,
ul -> Math.log10(ul == null ? null : unsignedLongToDouble(NumericUtils.asLongUnsigned(ul))),
ul -> Math.log10(ul == null ? null : unsignedLongToDouble(bigIntegerToUnsignedLong(ul))),
BigInteger.ONE,
UNSIGNED_LONG_MAX,
List.of()
Expand Down

0 comments on commit b61d785

Please sign in to comment.