Add support for $atan, $atan2 and $atanh aggregation operators.

Closes #3709
Original pull request: #3794.
This commit is contained in:
divya srivastava
2021-08-29 19:07:14 +05:30
committed by Mark Paluch
parent 34d66a276a
commit ffceed8da9
5 changed files with 299 additions and 1 deletions

View File

@@ -790,6 +790,68 @@ public class ArithmeticOperators {
public Tan tan() {
return tan(AngularUnit.RADIANS);
}
/**
* Creates new {@link AggregationExpression} that calculates the inverse tangent of a numeric value.
*
* @return new instance of {@link ATan}.
*/
public ATan atan() {
return usesFieldRef() ? ATan.atanOf(fieldReference) : ATan.atanOf(expression);
}
/**
* Creates new {@link AggregationExpression} that calculates the inverse tangent of the the numeric value
* divided by the given numeric value in the argument.
*
* @param the numeric value
* @return new instance of {@link ATan2}.
*/
public ATan2 atan2(Number value) {
Assert.notNull(value, "Value must not be null!");
return createATan2().atan2of(value);
}
/**
* Creates new {@link AggregationExpression} that calculates the inverse tangent of the the numeric value
* divided by the given field reference in the argument.
*
* @param the numeric value
* @return new instance of {@link ATan2}.
*/
public ATan2 atan2(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null!");
return createATan2().atan2of(fieldReference);
}
/**
* Creates new {@link AggregationExpression} that calculates the inverse tangent of the the numeric value
* divided by the given {@link AggregationExpression} in the argument.
*
* @param the numeric value
* @return new instance of {@link ATan2}.
*/
public ATan2 atan2(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null!");
return createATan2().atan2of(expression);
}
private ATan2 createATan2() {
return usesFieldRef() ? ATan2.valueOf(fieldReference) : ATan2.valueOf(expression);
}
/**
* Creates new {@link AggregationExpression} that calculates the inverse hyperbolic tangent of a numeric value.
*
* @return new instance of {@link ATanh}.
*/
public ATanh atanh() {
return usesFieldRef() ? ATanh.atanhOf(fieldReference) : ATanh.atanhOf(expression);
}
/**
* Creates new {@link AggregationExpression} that calculates the tangent of a numeric value in the given
@@ -2579,6 +2641,148 @@ public class ArithmeticOperators {
return "$tan";
}
}
/**
* An {@link AggregationExpression expression} that calculates the inverse tangent of a value.
*
*/
public static class ATan extends AbstractAggregationExpression {
private ATan(Object value) {
super(value);
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse tangent of a value.
*
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
* @return new instance of {@link ATan}.
*/
public static ATan atanOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null!");
return new ATan(Fields.field(fieldReference));
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse tangent of a value.
* <p />
*
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
* @return new instance of {@link ATan}.
*/
public static ATan atanOf(AggregationExpression expression) {
return new ATan(expression);
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse tangent of a value.
*
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
* numeric value.
* @return new instance of {@link ATan}.
*/
public static ATan atanof(Number value) {
return new ATan(value);
}
@Override
protected String getMongoMethod() {
return "$atan";
}
}
/**
* An {@link AggregationExpression expression} that calculates the inverse
* tangent of y / x, where y and x are the first and second values passed to the
* expression respectively.
*
*/
public static class ATan2 extends AbstractAggregationExpression {
private ATan2(List<?> value) {
super(value);
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse
* tangent of of y / x, where y and x are the first and second values passed to
* the expression respectively.
*
* @param fieldReference the name of the {@link Field field} that resolves to a
* numeric value.
* @return new instance of {@link ATan2}.
*/
public static ATan2 valueOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null!");
return new ATan2(asFields(fieldReference));
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse
* tangent of of y / x, where y and x are the first and second values passed to
* the expression respectively.
*
* @param expression the {@link AggregationExpression expression} that resolves
* to a numeric value.
* @return new instance of {@link ATan2}.
*/
public static ATan2 valueOf(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null!");
return new ATan2((Collections.singletonList(expression)));
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse
* tangent of of y / x, where y and x are the first and second values passed to
* the expression respectively.
*
* @param value anything ({@link Field field}, {@link AggregationExpression
* expression}, ...) that resolves to a numeric value.
* @return new instance of {@link ATan2}.
*/
public ATan2 atan2of(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null!");
return new ATan2(append(Fields.field(fieldReference)));
}
/**
* Creates a new {@link AggregationExpression} that calculates the hyperbolic tangent of a value that is measured in
* {@link AngularUnit#RADIANS}.
*
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
* numeric value.
* @return new instance of {@link ATan2}.
*/
public ATan2 atan2of(AggregationExpression expression) {
Assert.notNull(expression, "Expression must not be null!");
return new ATan2(append(expression));
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse
* tangent of of y / x, where y and x are the first and second values passed to
* the expression respectively.
*
* @param value of type {@link Number}
* @return new instance of {@link ATan2}.
*/
public ATan2 atan2of(Number value) {
return new ATan2(append(value));
}
@Override
protected String getMongoMethod() {
return "$atan2";
}
}
/**
* An {@link AggregationExpression expression} that calculates the hyperbolic tangent of a value that is measured in
@@ -2684,6 +2888,60 @@ public class ArithmeticOperators {
return "$tanh";
}
}
/**
* An {@link AggregationExpression expression} that calculates the inverse
* hyperbolic tangent of a value
*
*/
public static class ATanh extends AbstractAggregationExpression {
private ATanh(Object value) {
super(value);
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse
* hyperbolic tangent of a value.
*
* @param fieldReference the name of the {@link Field field} that resolves to a
* numeric value.
* @return new instance of {@link ATanh}.
*/
public static ATanh atanhOf(String fieldReference) {
return new ATanh(Fields.field(fieldReference));
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse
* hyperbolic tangent of a value.
* <p />
*
* @param expression the {@link AggregationExpression expression} that resolves
* to a numeric value.
* @return new instance of {@link ATanh}.
*/
public static ATanh atanhOf(AggregationExpression expression) {
return new ATanh(expression);
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse
* hyperbolic tangent of a value.
*
* @param value anything ({@link Field field}, {@link AggregationExpression
* expression}, ...) that resolves to a numeric value.
* @return new instance of {@link ATanh}.
*/
public static ATanh atanhof(Object value) {
return new ATanh(value);
}
@Override
protected String getMongoMethod() {
return "$atanh";
}
}
/**
* {@link Rand} returns a floating value between 0 and 1.

View File

@@ -100,6 +100,9 @@ public class MethodReferenceNode extends ExpressionNode {
map.put("tan", singleArgRef().forOperator("$tan"));
map.put("tanh", singleArgRef().forOperator("$tanh"));
map.put("rand", emptyRef().forOperator("$rand"));
map.put("atan", singleArgRef().forOperator("$atan"));
map.put("atan2", arrayArgRef().forOperator("$atan2"));
map.put("atanh", singleArgRef().forOperator("$atanh"));
// STRING OPERATORS
map.put("concat", arrayArgRef().forOperator("$concat"));

View File

@@ -113,4 +113,4 @@ public abstract class RegexFlags {
return flag;
}
}
}

View File

@@ -166,6 +166,28 @@ class ArithmeticOperatorsUnitTests {
assertThat(valueOf("angle").tanh(AngularUnit.DEGREES).toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ $tanh : { $degreesToRadians : \"$angle\" } }");
}
@Test // DATAMONGO - 3709
void rendersATan() {
assertThat(valueOf("field").atan().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ $atan : \"$field\" }");
}
@Test // DATAMONGO - 3709
void rendersATan2() {
assertThat(valueOf("field1").atan2("field2").toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ $atan2 : [ \"$field1\" , \"$field2\" ] }");
}
@Test // DATAMONGO - 3709
void rendersATanh() {
assertThat(valueOf("field").atanh().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ $atanh : \"$field\" }");
}
@Test // GH-3724
void rendersRand() {

View File

@@ -1098,6 +1098,21 @@ public class SpelExpressionTransformerUnitTests {
void shouldRenderTanh() {
assertThat(transform("tanh(angle)")).isEqualTo("{ \"$tanh\" : \"$angle\"}");
}
@Test // DATAMONGO - 3709
void shouldRenderATan() {
assertThat(transform("atan(number)")).isEqualTo("{ \"$atan\" : \"$number\"}");
}
@Test // DATAMONGO - 3709
void shouldRenderATan2() {
assertThat(transform("atan2(number1,number2)")).isEqualTo("{ \"$atan2\" : [ \"$number1\" , \"$number2\" ] }");
}
@Test // DATAMONGO - 3709
void shouldRenderATanh() {
assertThat(transform("atanh(number)")).isEqualTo("{ \"$atanh\" : \"$number\"}");
}
@Test // GH-3713
void shouldRenderDateAdd() {