Skip to content

Re-implement reduce #1053

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

Merged
merged 7 commits into from
Jan 18, 2023
Merged
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 @@ -16,7 +16,6 @@

package com.mongodb.client.model.expressions;

import java.util.function.BinaryOperator;
import java.util.function.Function;

import static com.mongodb.client.model.expressions.Expressions.of;
Expand Down Expand Up @@ -51,28 +50,51 @@ public interface ArrayExpression<T extends Expression> extends Expression {
*/
<R extends Expression> ArrayExpression<R> map(Function<? super T, ? extends R> in);

IntegerExpression size();

BooleanExpression any(Function<? super T, BooleanExpression> predicate);

BooleanExpression all(Function<? super T, BooleanExpression> predicate);

NumberExpression sum(Function<? super T, ? extends NumberExpression> mapper);

NumberExpression multiply(Function<? super T, ? extends NumberExpression> mapper);

T max(T other);

T min(T other);

ArrayExpression<T> maxN(IntegerExpression n);
ArrayExpression<T> minN(IntegerExpression n);

StringExpression join(Function<? super T, StringExpression> mapper);

<R extends Expression> ArrayExpression<R> concat(Function<? super T, ? extends ArrayExpression<? extends R>> mapper);

<R extends Expression> ArrayExpression<R> union(Function<? super T, ? extends ArrayExpression<? extends R>> mapper);

/**
* Performs a reduction on the elements of this array, using the provided
* identity value and an associative reducing function, and returns
* the reduced value. The initial value must be the identity value for the
* reducing function.
* user asserts that i is in bounds for the array
*
* @param initialValue the identity for the reducing function
* @param in the associative reducing function
* @return the reduced value
* @param i
* @return
*/
T reduce(T initialValue, BinaryOperator<T> in);

IntegerExpression size();

T elementAt(IntegerExpression i);

default T elementAt(final int i) {
return this.elementAt(of(i));
}

/**
* user asserts that array is not empty
* @return
*/
T first();

/**
* user asserts that array is not empty
* @return
*/
T last();

BooleanExpression contains(T contains);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.mongodb.client.model.expressions;

import org.bson.conversions.Bson;
import org.bson.types.Decimal128;

import java.time.Instant;

Expand Down Expand Up @@ -47,12 +46,8 @@ default BooleanExpression getBoolean(final String fieldName, final boolean other

NumberExpression getNumber(String fieldName, NumberExpression other);

default NumberExpression getNumber(final String fieldName, final double other) {
return getNumber(fieldName, of(other));
}

default NumberExpression getNumber(final String fieldName, final Decimal128 other) {
return getNumber(fieldName, of(other));
default NumberExpression getNumber(final String fieldName, final Number other) {
return getNumber(fieldName, Expressions.numberToExpression(other));
}

IntegerExpression getInteger(String fieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.bson.BsonArray;
import org.bson.BsonDocument;
import org.bson.BsonInt32;
import org.bson.BsonString;
import org.bson.BsonValue;
import org.bson.codecs.configuration.CodecRegistry;
Expand Down Expand Up @@ -385,7 +386,12 @@ public ArrayExpression<T> filter(final Function<? super T, ? extends BooleanExpr
.append("cond", extractBsonValue(cr, cond.apply(varThis)))));
}

@Override
public ArrayExpression<T> sort() {
return new MqlExpression<>((cr) -> astDoc("$sortArray", new BsonDocument()
.append("input", this.toBsonValue(cr))
.append("sortBy", new BsonInt32(1))));
}

public T reduce(final T initialValue, final BinaryOperator<T> in) {
T varThis = variable("$$this");
T varValue = variable("$$value");
Expand All @@ -395,6 +401,77 @@ public T reduce(final T initialValue, final BinaryOperator<T> in) {
.append("in", extractBsonValue(cr, in.apply(varValue, varThis)))));
}

@Override
public BooleanExpression any(final Function<? super T, BooleanExpression> predicate) {
MqlExpression<BooleanExpression> array = (MqlExpression<BooleanExpression>) this.map(predicate);
return array.reduce(of(false), (a, b) -> a.or(b));
}

@Override
public BooleanExpression all(final Function<? super T, BooleanExpression> predicate) {
MqlExpression<BooleanExpression> array = (MqlExpression<BooleanExpression>) this.map(predicate);
return array.reduce(of(true), (a, b) -> a.and(b));
}

@SuppressWarnings("unchecked")
@Override
public NumberExpression sum(final Function<? super T, ? extends NumberExpression> mapper) {
// no sum that returns IntegerExpression, both have same erasure
MqlExpression<NumberExpression> array = (MqlExpression<NumberExpression>) this.map(mapper);
return array.reduce(of(0), (a, b) -> a.add(b));
}

@SuppressWarnings("unchecked")
@Override
public NumberExpression multiply(final Function<? super T, ? extends NumberExpression> mapper) {
MqlExpression<NumberExpression> array = (MqlExpression<NumberExpression>) this.map(mapper);
return array.reduce(of(0), (NumberExpression a, NumberExpression b) -> a.multiply(b));
}

@Override
public T max(final T other) {
return this.size().eq(of(0)).cond(other, this.maxN(of(1)).first());
}

@Override
public T min(final T other) {
return this.size().eq(of(0)).cond(other, this.minN(of(1)).first());
}

@Override
public ArrayExpression<T> maxN(final IntegerExpression n) {
return newMqlExpression((CodecRegistry cr) -> astDoc("$maxN", new BsonDocument()
.append("input", extractBsonValue(cr, this))
.append("n", extractBsonValue(cr, n))));
}

@Override
public ArrayExpression<T> minN(final IntegerExpression n) {
return newMqlExpression((CodecRegistry cr) -> astDoc("$minN", new BsonDocument()
.append("input", extractBsonValue(cr, this))
.append("n", extractBsonValue(cr, n))));
}

@Override
public StringExpression join(final Function<? super T, StringExpression> mapper) {
MqlExpression<StringExpression> array = (MqlExpression<StringExpression>) this.map(mapper);
return array.reduce(of(""), (a, b) -> a.concat(b));
}

@SuppressWarnings("unchecked")
@Override
public <R extends Expression> ArrayExpression<R> concat(final Function<? super T, ? extends ArrayExpression<? extends R>> mapper) {
MqlExpression<ArrayExpression<R>> array = (MqlExpression<ArrayExpression<R>>) this.map(mapper);
return array.reduce(Expressions.ofArray(), (a, b) -> a.concat(b));
}

@SuppressWarnings("unchecked")
@Override
public <R extends Expression> ArrayExpression<R> union(final Function<? super T, ? extends ArrayExpression<? extends R>> mapper) {
MqlExpression<ArrayExpression<R>> array = (MqlExpression<ArrayExpression<R>>) this.map(mapper);
return array.reduce(Expressions.ofArray(), (a, b) -> a.union(b));
}

@Override
public IntegerExpression size() {
return new MqlExpression<>(astWrapped("$size"));
Expand Down
Loading