Skip to content

DATAMONGO-3709 - Add support for $atan, $atan2 & atanh aggregation operators. #3794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,68 @@ public Cosh cosh(AngularUnit unit) {
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
Expand Down Expand Up @@ -2579,6 +2641,148 @@ protected String getMongoMethod() {
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
Expand Down Expand Up @@ -2684,6 +2888,60 @@ protected String getMongoMethod() {
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,28 @@ void rendersTanhWithValueInDegrees() {
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,21 @@ void shouldRenderTan() {
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() {
Expand Down