Skip to content
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

apoc.coll.min and max comparsion operator logic like cypher #1473

Merged
merged 1 commit into from
Apr 8, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/main/java/apoc/coll/Coll.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Result;
import org.neo4j.helpers.collection.Pair;
import org.neo4j.kernel.impl.util.statistics.IntCounter;
import org.neo4j.procedure.*;
Expand Down Expand Up @@ -89,18 +90,20 @@ public Double avg(@Name("numbers") List<Number> list) {
@Description("apoc.coll.min([0.5,1,2.3])")
public Object min(@Name("values") List<Object> list) {
if (list == null || list.isEmpty()) return null;
return Collections.min((List)list, Coll::compareAsDoubles);
if (list.size() == 1) return list.get(0);
try (Result result = db.execute("cypher expressionEngine=compiled return reduce(res=null, x in $list | CASE WHEN res IS NULL OR x<res THEN x ELSE res END) as value", Collections.singletonMap("list", list))) {
return result.next().get("value");
}
}

@UserFunction
@Description("apoc.coll.max([0.5,1,2.3])")
public Object max(@Name("values") List<Object> list) {
if (list == null || list.isEmpty()) return null;
return Collections.max((List)list, Coll::compareAsDoubles);
}

private static int compareAsDoubles(Object a, Object b) {
return Double.compare(((Number)a).doubleValue(), ((Number)b).doubleValue());
if (list == null || list.isEmpty()) return null;
if (list.size() == 1) return list.get(0);
try (Result result = db.execute("cypher expressionEngine=compiled return reduce(res=null, x in $list | CASE WHEN res IS NULL OR res<x THEN x ELSE res END) as value", Collections.singletonMap("list", list))) {
return result.next().get("value");
}
}

@Procedure
Expand Down Expand Up @@ -397,6 +400,7 @@ public List<Object> remove(@Name("coll") List<Object> coll, @Name("index") long
@UserFunction
@Description("apoc.coll.indexOf(coll, value) | position of value in the list")
public long indexOf(@Name("coll") List<Object> coll, @Name("value") Object value) {
// return reduce(res=[0,-1], x in $list | CASE WHEN x=$value AND res[1]=-1 THEN [res[0], res[0]+1] ELSE [res[0]+1, res[1]] END)[1] as value
if (coll == null || coll.isEmpty()) return -1;
return new ArrayList<>(coll).indexOf(value);
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/apoc/coll/CollTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ public void testMax() throws Exception {
(row) -> assertEquals(2.3D, row.get("value")));
}

@Test
public void testMaxDate() throws Exception {
testCall(db, "RETURN apoc.coll.max([date('2020-04-01'), date('2020-03-01')]) as value",
(row) -> assertEquals("2020-04-01", row.get("value").toString()));
testCall(db, "RETURN apoc.coll.max([datetime('2020-03-30T12:17:43.175Z'), datetime('2020-03-30T12:17:39.982Z')]) as value",
(row) -> assertEquals("2020-03-30T12:17:43.175Z", row.get("value").toString()));
testCall(db, "RETURN apoc.coll.max([null, datetime('2020-03-30T11:17:39.982Z'), datetime('2020-03-30T12:17:39.982Z'), datetime('2020-03-30T11:17:39.982Z')]) as value",
(row) -> assertEquals("2020-03-30T12:17:39.982Z", row.get("value").toString()));
}

@Test
public void testPartition() throws Exception {
testResult(db, "CALL apoc.coll.partition([1,2,3,4,5],2)",
Expand Down