Add support for $asin and $asinh aggregation operators.

Closes #3708
Original pull request: #3796.
This commit is contained in:
divyajnu08
2021-09-01 13:02:43 +05:30
committed by Mark Paluch
parent 8af904b81f
commit 59d0042d13
4 changed files with 142 additions and 0 deletions

View File

@@ -735,6 +735,24 @@ public class ArithmeticOperators {
return usesFieldRef() ? Sinh.sinhOf(fieldReference, unit) : Sinh.sinhOf(expression, unit);
}
/**
* Creates new {@link AggregationExpression} that calculates the inverse sine of a numeric value.
*
* @return new instance of {@link ASin}.
*/
public ASin asin() {
return usesFieldRef() ? ASin.asinOf(fieldReference) : ASin.asinOf(expression);
}
/**
* Creates new {@link AggregationExpression} that calculates the inverse hyperbolic sine of a numeric value.
*
* @return new instance of {@link ASinh}.
*/
public ASinh asinh() {
return usesFieldRef() ? ASinh.asinhOf(fieldReference) : ASinh.asinhOf(expression);
}
/**
* Creates new {@link AggregationExpression} that calculates the cosine of a numeric value given in
* {@link AngularUnit#RADIANS radians}.
@@ -2339,6 +2357,104 @@ public class ArithmeticOperators {
return "$sinh";
}
}
/**
* An {@link AggregationExpression expression} that calculates the inverse sine of a value.
*
*/
public static class ASin extends AbstractAggregationExpression {
private ASin(Object value) {
super(value);
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse sine of a value.
*
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
* @return new instance of {@link ASin}.
*/
public static ASin asinOf(String fieldReference) {
Assert.notNull(fieldReference, "FieldReference must not be null!");
return new ASin(Fields.field(fieldReference));
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse sine of a value.
* <p />
*
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
* @return new instance of {@link ASin}.
*/
public static ASin asinOf(AggregationExpression expression) {
return new ASin(expression);
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse sine of a value.
*
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
* numeric value.
* @return new instance of {@link ASin}.
*/
public static ASin asinOf(Number value) {
return new ASin(value);
}
@Override
protected String getMongoMethod() {
return "$asin";
}
}
/**
* An {@link AggregationExpression expression} that calculates the inverse hyperbolic sine of a value
*/
public static class ASinh extends AbstractAggregationExpression {
private ASinh(Object value) {
super(value);
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse hyperbolic sine of a value.
*
* @param fieldReference the name of the {@link Field field} that resolves to a numeric value.
* @return new instance of {@link ASinh}.
*/
public static ASinh asinhOf(String fieldReference) {
return new ASinh(Fields.field(fieldReference));
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse hyperbolic sine of a value.
* <p />
*
* @param expression the {@link AggregationExpression expression} that resolves to a numeric value.
* @return new instance of {@link ASinh}.
*/
public static ASinh asinhOf(AggregationExpression expression) {
return new ASinh(expression);
}
/**
* Creates a new {@link AggregationExpression} that calculates the inverse hyperbolic sine of a value.
*
* @param value anything ({@link Field field}, {@link AggregationExpression expression}, ...) that resolves to a
* numeric value.
* @return new instance of {@link ASinh}.
*/
public static ASinh asinhOf(Object value) {
return new ASinh(value);
}
@Override
protected String getMongoMethod() {
return "$asinh";
}
}
/**
* An {@link AggregationExpression expression} that calculates the cosine of a value that is measured in radians.

View File

@@ -95,6 +95,8 @@ public class MethodReferenceNode extends ExpressionNode {
map.put("integral", mapArgRef().forOperator("$integral").mappingParametersTo("input", "unit"));
map.put("sin", singleArgRef().forOperator("$sin"));
map.put("sinh", singleArgRef().forOperator("$sinh"));
map.put("asin", singleArgRef().forOperator("$asin"));
map.put("asinh", singleArgRef().forOperator("$asinh"));
map.put("cos", singleArgRef().forOperator("$cos"));
map.put("cosh", singleArgRef().forOperator("$cosh"));
map.put("tan", singleArgRef().forOperator("$tan"));

View File

@@ -109,6 +109,20 @@ class ArithmeticOperatorsUnitTests {
assertThat(valueOf("angle").sinh(AngularUnit.DEGREES).toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ $sinh : { $degreesToRadians : \"$angle\" } }");
}
@Test // DATAMONGO - 3708
void rendersASin() {
assertThat(valueOf("field").asin().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ $asin : \"$field\" }");
}
@Test // DATAMONGO - 3708
void rendersASinh() {
assertThat(valueOf("field").asinh().toDocument(Aggregation.DEFAULT_CONTEXT))
.isEqualTo("{ $asinh : \"$field\" }");
}
@Test // GH-3710
void rendersCos() {

View File

@@ -1078,6 +1078,16 @@ public class SpelExpressionTransformerUnitTests {
void shouldRenderSinh() {
assertThat(transform("sinh(angle)")).isEqualTo("{ \"$sinh\" : \"$angle\"}");
}
@Test // DATAMONGO-3708
void shouldRenderASin() {
assertThat(transform("asin(number)")).isEqualTo("{ \"$asin\" : \"$number\"}");
}
@Test // DATAMONGO-3708
void shouldRenderASinh() {
assertThat(transform("asinh(number)")).isEqualTo("{ \"$asinh\" : \"$number\"}");
}
@Test // GH-3710
void shouldRenderCos() {