Skip to content

Commit

Permalink
drop out unnecessary type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
not-napoleon committed Jan 17, 2024
1 parent 7c660ff commit bda4878
Showing 1 changed file with 4 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.carrotsearch.randomizedtesting.annotations.Name;
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;

import org.elasticsearch.xpack.esql.expression.function.AbstractFunctionTestCase;
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
import org.elasticsearch.xpack.esql.type.EsqlDataTypes;
import org.elasticsearch.xpack.ql.expression.Expression;
Expand All @@ -20,24 +21,17 @@
import java.math.BigInteger;
import java.time.Duration;
import java.time.Period;
import java.time.temporal.TemporalAmount;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

import static org.elasticsearch.xpack.esql.type.EsqlDataTypes.isDateTimeOrTemporal;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypes.isTemporalAmount;
import static org.elasticsearch.xpack.ql.type.DataTypes.isDateTime;
import static org.elasticsearch.xpack.ql.type.DataTypes.isNull;
import static org.elasticsearch.xpack.ql.type.DateUtils.asDateTime;
import static org.elasticsearch.xpack.ql.type.DateUtils.asMillis;
import static org.elasticsearch.xpack.ql.util.NumericUtils.asLongUnsigned;
import static org.elasticsearch.xpack.ql.util.NumericUtils.unsignedLongAsBigInteger;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;

public class AddTests extends AbstractDateTimeArithmeticTestCase {
public class AddTests extends AbstractFunctionTestCase {
public AddTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
this.testCase = testCaseSupplier.get();
}
Expand Down Expand Up @@ -231,56 +225,7 @@ private static String addErrorMessageString(boolean includeOrdinal, List<Set<Dat
}

@Override
protected boolean supportsTypes(DataType lhsType, DataType rhsType) {
if (isDateTimeOrTemporal(lhsType) || isDateTimeOrTemporal(rhsType)) {
return isNull(lhsType)
|| isNull(rhsType)
|| isDateTime(lhsType) && isTemporalAmount(rhsType)
|| isTemporalAmount(lhsType) && isDateTime(rhsType)
|| isTemporalAmount(lhsType) && isTemporalAmount(rhsType) && lhsType == rhsType;
}
return super.supportsTypes(lhsType, rhsType);
}

@Override
protected Add build(Source source, Expression lhs, Expression rhs) {
return new Add(source, lhs, rhs);
}

@Override
protected double expectedValue(double lhs, double rhs) {
return lhs + rhs;
}

@Override
protected int expectedValue(int lhs, int rhs) {
return lhs + rhs;
}

@Override
protected long expectedValue(long lhs, long rhs) {
return lhs + rhs;
}

@Override
protected long expectedUnsignedLongValue(long lhs, long rhs) {
BigInteger lhsBI = unsignedLongAsBigInteger(lhs);
BigInteger rhsBI = unsignedLongAsBigInteger(rhs);
return asLongUnsigned(lhsBI.add(rhsBI).longValue());
}

@Override
protected long expectedValue(long datetime, TemporalAmount temporalAmount) {
return asMillis(asDateTime(datetime).plus(temporalAmount));
}

@Override
protected Period expectedValue(Period lhs, Period rhs) {
return lhs.plus(rhs);
}

@Override
protected Duration expectedValue(Duration lhs, Duration rhs) {
return lhs.plus(rhs);
protected Expression build(Source source, List<Expression> args) {
return new Add(source, args.get(0), args.get(1));
}
}

0 comments on commit bda4878

Please sign in to comment.