Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added tests for all empty aggregations, and a better default for AVG

  • Loading branch information...
commit b759f4985f3f9f5d82a1c61dccaf8189fdc5f60b 1 parent c41ecd4
@systay authored
View
6 cypher/src/main/scala/org/neo4j/cypher/internal/pipes/aggregation/AvgFunction.scala
@@ -32,7 +32,11 @@ class AvgFunction(val value: Expression)
private var count: Int = 0
private var sofar: Any = 0
- def result: Any = divide(sofar, count)
+ def result =
+ if (count > 0)
+ divide(sofar, count)
+ else
+ null
def apply(data: Map[String, Any]) {
actOnNumber(value(data), (number) => {
View
12 cypher/src/test/scala/org/neo4j/cypher/internal/pipes/EagerAggregationPipeTest.scala
@@ -28,7 +28,7 @@ import org.scalatest.junit.JUnitSuite
import org.neo4j.cypher.SyntaxException
import org.neo4j.cypher.internal.symbols._
import collection.mutable.Map
-import java.lang.{Iterable=>JIterable}
+import java.lang.{Iterable => JIterable}
class EagerAggregationPipeTest extends JUnitSuite {
@Test def shouldReturnColumnsFromReturnItems() {
@@ -72,10 +72,11 @@ class EagerAggregationPipeTest extends JUnitSuite {
val source = new FakePipe(List(), createSymbolTableFor("name"))
val returnItems = List()
- val grouping = List(CountStar())
+ val grouping = List(CountStar(), Avg(Property("name", "age")), Collect(Property("name", "age")), Count(Property("name", "age")), Max(Property("name", "age")), Min(Property("name", "age")), Sum(Property("name", "age")))
val aggregationPipe = new EagerAggregationPipe(source, returnItems, grouping)
- assertThat(getResults(aggregationPipe), hasItems(Map[String, Any]("count(*)" -> 0)))
+ val results = getResults(aggregationPipe)
+ assertThat(results, hasItems(Map[String, Any]("avg(name.age)" -> null, "sum(name.age)" -> 0, "count(name.age)" -> 0, "min(name.age)" -> null, "collect(name.age)" -> List(), "max(name.age)" -> null, "count(*)" -> 0)))
}
@Test def shouldCountNonNullValues() {
@@ -92,6 +93,7 @@ class EagerAggregationPipeTest extends JUnitSuite {
assertEquals(List(Map("count(name)" -> 3)), aggregationPipe.createResults(QueryState()).toList)
}
- private def createSymbolTableFor(name:String) = new SymbolTable(Identifier(name, NodeType()))
- private def getResults(p:Pipe):JIterable[Map[String, Any]] = p.createResults(QueryState()).map(_.m).toIterable.asJava
+ private def createSymbolTableFor(name: String) = new SymbolTable(Identifier(name, NodeType()))
+
+ private def getResults(p: Pipe): JIterable[Map[String, Any]] = p.createResults(QueryState()).map(_.m).toIterable.asJava
}

0 comments on commit b759f49

Please sign in to comment.
Something went wrong with that request. Please try again.