All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.vertexium.cypher.functions.aggregate.MaxFunction Maven / Gradle / Ivy

There is a newer version: 4.10.0
Show newest version
package org.vertexium.cypher.functions.aggregate;

import org.vertexium.cypher.CypherResultRow;
import org.vertexium.cypher.VertexiumCypherQueryContext;
import org.vertexium.cypher.executionPlan.AggregationFunctionInvocationExecutionStep;
import org.vertexium.cypher.executionPlan.ExecutionStepWithResultName;
import org.vertexium.cypher.utils.ObjectUtils;

import java.util.stream.Stream;

public class MaxFunction implements AggregationFunction {
    @Override
    public ExecutionStepWithResultName create(String resultName, boolean distinct, ExecutionStepWithResultName[] argumentsExecutionStep) {
        return new AggregationFunctionInvocationExecutionStep(getClass().getSimpleName(), resultName, distinct, argumentsExecutionStep) {
            @Override
            protected CypherResultRow executeAggregation(VertexiumCypherQueryContext ctx, CypherResultRow group, Stream rows) {
                if (rows == null) {
                    return group.clone()
                        .pushScope(getResultName(), null);
                }

                Object maxValue = rows
                    .filter(r -> r.arguments[0] != null)
                    .max((row1, row2) -> ObjectUtils.compare(row1.arguments[0], row2.arguments[0]))
                    .map(r -> r.arguments[0])
                    .orElse(null);
                return group.clone()
                    .pushScope(resultName, maxValue);
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy