Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrated aggregation functions in Cypher
Added the infrastructure to be able to call and run user-defined aggregation function in Cypher.
- Loading branch information
1 parent
207ff6a
commit e0dae39
Showing
41 changed files
with
1,147 additions
and
74 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
...a/org/neo4j/internal/cypher/acceptance/AggregationFunctionCallSupportAcceptanceTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (c) 2002-2016 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.internal.cypher.acceptance | ||
|
||
import java.util | ||
|
||
import org.neo4j.kernel.api.proc.Neo4jTypes | ||
|
||
class AggregationFunctionCallSupportAcceptanceTest extends ProcedureCallAcceptanceTest { | ||
|
||
test("should return correctly typed map result (even if converting to and from scala representation internally)") { | ||
val value = new util.HashMap[String, Any]() | ||
value.put("name", "Cypher") | ||
value.put("level", 9001) | ||
|
||
registerUserAggregationFunction(value) | ||
|
||
// Using graph execute to get a Java value | ||
graph.execute("RETURN my.first.value()").stream().toArray.toList should equal(List( | ||
java.util.Collections.singletonMap("my.first.value()", value) | ||
)) | ||
} | ||
|
||
test("should return correctly typed list result (even if converting to and from scala representation internally)") { | ||
val value = new util.ArrayList[Any]() | ||
value.add("Norris") | ||
value.add("Strange") | ||
|
||
registerUserAggregationFunction(value) | ||
|
||
// Using graph execute to get a Java value | ||
graph.execute("RETURN my.first.value() AS out").stream().toArray.toList should equal(List( | ||
java.util.Collections.singletonMap("out", value) | ||
)) | ||
} | ||
|
||
test("should return correctly typed stream result (even if converting to and from scala representation internally)") { | ||
val value = new util.ArrayList[Any]() | ||
value.add("Norris") | ||
value.add("Strange") | ||
val stream = value.stream() | ||
|
||
registerUserAggregationFunction(stream) | ||
|
||
// Using graph execute to get a Java value | ||
graph.execute("RETURN my.first.value() AS out").stream().toArray.toList should equal(List( | ||
java.util.Collections.singletonMap("out", stream) | ||
)) | ||
} | ||
|
||
test("should not copy lists unnecessarily") { | ||
val value = new util.ArrayList[Any]() | ||
value.add("Norris") | ||
value.add("Strange") | ||
|
||
registerUserAggregationFunction(value) | ||
|
||
// Using graph execute to get a Java value | ||
val returned = graph.execute("RETURN my.first.value() AS out").next().get("out") | ||
|
||
returned shouldBe an [util.ArrayList[_]] | ||
returned shouldBe value | ||
} | ||
|
||
test("should not copy unnecessarily with nested types") { | ||
val value = new util.ArrayList[Any]() | ||
val inner = new util.ArrayList[Any]() | ||
value.add("Norris") | ||
value.add(inner) | ||
|
||
registerUserAggregationFunction(value) | ||
|
||
// Using graph execute to get a Java value | ||
val returned = graph.execute("RETURN my.first.value() AS out").next().get("out") | ||
|
||
returned shouldBe an [util.ArrayList[_]] | ||
returned shouldBe value | ||
} | ||
|
||
test("should handle interacting with list") { | ||
val value = new util.ArrayList[Integer]() | ||
value.add(1) | ||
value.add(3) | ||
|
||
registerUserAggregationFunction(value, Neo4jTypes.NTList(Neo4jTypes.NTInteger)) | ||
|
||
// Using graph execute to get a Java value | ||
val returned = graph.execute("WITH my.first.value() AS list RETURN list[0] + list[1] AS out") | ||
.next().get("out") | ||
|
||
returned should equal(4) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...4j/cypher/internal/compiler/v3_2/commands/expressions/AggregationFunctionInvocation.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (c) 2002-2016 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.cypher.internal.compiler.v3_2.commands.expressions | ||
|
||
import org.neo4j.cypher.internal.compiler.v3_2.ExecutionContext | ||
import org.neo4j.cypher.internal.compiler.v3_2.helpers.{RuntimeJavaValueConverter, RuntimeScalaValueConverter} | ||
import org.neo4j.cypher.internal.compiler.v3_2.pipes.QueryState | ||
import org.neo4j.cypher.internal.compiler.v3_2.pipes.aggregation.AggregationFunction | ||
import org.neo4j.cypher.internal.compiler.v3_2.spi.UserFunctionSignature | ||
|
||
case class AggregationFunctionInvocation(signature: UserFunctionSignature, arguments: IndexedSeq[Expression]) | ||
extends AggregationExpression { | ||
|
||
override def createAggregationFunction: AggregationFunction = new AggregationFunction { | ||
private var inner: UserDefinedAggregator = null | ||
|
||
override def result(implicit state:QueryState) = { | ||
val isGraphKernelResultValue = state.query.isGraphKernelResultValue _ | ||
val scalaValues = new RuntimeScalaValueConverter(isGraphKernelResultValue, state.typeConverter.asPrivateType) | ||
scalaValues.asDeepScalaValue(aggregator.result) | ||
} | ||
|
||
override def apply(data: ExecutionContext) | ||
(implicit state: QueryState) = { | ||
val converter = new RuntimeJavaValueConverter(state.query.isGraphKernelResultValue, state.typeConverter.asPublicType) | ||
val argValues = arguments.map(arg => converter.asDeepJavaValue(arg(data)(state))) | ||
aggregator.update(argValues) | ||
} | ||
|
||
private def aggregator(implicit state: QueryState) = { | ||
if (inner == null) { | ||
inner = state.query.aggregateFunction(signature.name, signature.allowed) | ||
} | ||
inner | ||
} | ||
|
||
|
||
} | ||
|
||
override def rewrite(f: (Expression) => Expression): Expression = f( | ||
AggregationFunctionInvocation(signature, arguments.map(a => a.rewrite(f)))) | ||
|
||
override def symbolTableDependencies: Set[String] = arguments.flatMap(_.symbolTableDependencies).toSet | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
.../org/neo4j/cypher/internal/compiler/v3_2/commands/expressions/UserDefinedAggregator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) 2002-2016 "Neo Technology," | ||
* Network Engine for Objects in Lund AB [http://neotechnology.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.cypher.internal.compiler.v3_2.commands.expressions | ||
|
||
trait UserDefinedAggregator { | ||
def update(args: IndexedSeq[Any]): Unit | ||
def result: Any | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.