Skip to content

Commit

Permalink
MV functions and operators
Browse files Browse the repository at this point in the history
  • Loading branch information
fang-xing-esql committed Mar 26, 2024
1 parent 869f034 commit ff59af3
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

import java.util.List;

import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.unsignedLongToDouble;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypes.isRepresentable;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isType;
import static org.elasticsearch.xpack.ql.util.NumericUtils.unsignedLongToDouble;

/**
* Reduce a multivalued field to a single valued field containing the average value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import java.util.Arrays;
import java.util.List;

import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.bigIntegerToUnsignedLong;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.unsignedLongToBigInteger;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypes.isRepresentable;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.isType;
import static org.elasticsearch.xpack.ql.util.NumericUtils.asLongUnsigned;
import static org.elasticsearch.xpack.ql.util.NumericUtils.unsignedLongAsBigInteger;

/**
* Reduce a multivalued field to a single valued field containing the average value.
Expand Down Expand Up @@ -156,9 +156,9 @@ static long finishUnsignedLong(Longs longs) {
Arrays.sort(longs.values, 0, longs.count);
int middle = longs.count / 2;
longs.count = 0;
BigInteger a = unsignedLongAsBigInteger(longs.values[middle - 1]);
BigInteger b = unsignedLongAsBigInteger(longs.values[middle]);
return asLongUnsigned(a.add(b).shiftRight(1).longValue());
BigInteger a = unsignedLongToBigInteger(longs.values[middle - 1]);
BigInteger b = unsignedLongToBigInteger(longs.values[middle]);
return bigIntegerToUnsignedLong(a.add(b).shiftRight(1));
}

/**
Expand All @@ -169,9 +169,9 @@ static long ascendingUnsignedLong(LongBlock values, int firstValue, int count) {
if (count % 2 == 1) {
return values.getLong(middle);
}
BigInteger a = unsignedLongAsBigInteger(values.getLong(middle - 1));
BigInteger b = unsignedLongAsBigInteger(values.getLong(middle));
return asLongUnsigned(a.add(b).shiftRight(1).longValue());
BigInteger a = unsignedLongToBigInteger(values.getLong(middle - 1));
BigInteger b = unsignedLongToBigInteger(values.getLong(middle));
return bigIntegerToUnsignedLong(a.add(b).shiftRight(1));
}

static class Ints {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Objects;
import java.util.function.Function;

import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.stringToInt;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.FIRST;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.SECOND;
import static org.elasticsearch.xpack.ql.expression.TypeResolutions.ParamOrdinal.THIRD;
Expand Down Expand Up @@ -129,8 +130,8 @@ public EvalOperator.ExpressionEvaluator.Factory toEvaluator(
Function<Expression, EvalOperator.ExpressionEvaluator.Factory> toEvaluator
) {
if (start.foldable() && end.foldable()) {
int startOffset = Integer.parseInt(String.valueOf(start.fold()));
int endOffset = Integer.parseInt(String.valueOf(end.fold()));
int startOffset = stringToInt(String.valueOf(start.fold()));
int endOffset = stringToInt(String.valueOf(end.fold()));
checkStartEnd(startOffset, endOffset);
}
return switch (PlannerUtils.toElementType(field.dataType())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.elasticsearch.xpack.ql.type.DataType;

import static org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic.EsqlArithmeticOperation.OperationSymbol.DIV;
import static org.elasticsearch.xpack.ql.util.NumericUtils.asLongUnsigned;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.longToUnsignedLong;

public class Div extends EsqlArithmeticOperation implements BinaryComparisonInversible {

Expand Down Expand Up @@ -73,7 +73,7 @@ static long processLongs(long lhs, long rhs) {

@Evaluator(extraName = "UnsignedLongs", warnExceptions = { ArithmeticException.class })
static long processUnsignedLongs(long lhs, long rhs) {
return asLongUnsigned(Long.divideUnsigned(asLongUnsigned(lhs), asLongUnsigned(rhs)));
return longToUnsignedLong(Long.divideUnsigned(longToUnsignedLong(lhs, true), longToUnsignedLong(rhs, true)), true);
}

@Evaluator(extraName = "Doubles")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.elasticsearch.xpack.ql.tree.Source;

import static org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic.EsqlArithmeticOperation.OperationSymbol.MOD;
import static org.elasticsearch.xpack.ql.util.NumericUtils.asLongUnsigned;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.longToUnsignedLong;

public class Mod extends EsqlArithmeticOperation {

Expand Down Expand Up @@ -52,7 +52,7 @@ static long processLongs(long lhs, long rhs) {

@Evaluator(extraName = "UnsignedLongs", warnExceptions = { ArithmeticException.class })
static long processUnsignedLongs(long lhs, long rhs) {
return asLongUnsigned(Long.remainderUnsigned(asLongUnsigned(lhs), asLongUnsigned(rhs)));
return longToUnsignedLong(Long.remainderUnsigned(longToUnsignedLong(lhs, true), longToUnsignedLong(rhs, true)), true);
}

@Evaluator(extraName = "Doubles")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.function.Supplier;
import java.util.stream.DoubleStream;

import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.unsignedLongToDouble;
import static org.hamcrest.Matchers.equalTo;

public class MvAvgTests extends AbstractMultivalueFunctionTestCase {
Expand Down Expand Up @@ -53,7 +54,7 @@ public static Iterable<Object[]> parameters() {
* So we have to go back to encoded `long` and then convert to double
* using the production conversion. That'll round in the same way.
*/
(size, data) -> avg.apply(size, data.mapToDouble(v -> NumericUtils.unsignedLongToDouble(NumericUtils.asLongUnsigned(v))))
(size, data) -> avg.apply(size, data.mapToDouble(v -> unsignedLongToDouble(NumericUtils.asLongUnsigned(v))))
);
return parameterSuppliersFromTypedData(errorsForCasesWithoutExamples(anyNullIsNull(true, cases)));
}
Expand Down

0 comments on commit ff59af3

Please sign in to comment.