Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
Update a lot of the test to use `AnyValue` instead of `Any`
  • Loading branch information
pontusmelke committed Aug 21, 2017
1 parent 6ffd5fd commit dd4a112
Show file tree
Hide file tree
Showing 113 changed files with 1,245 additions and 992 deletions.
Expand Up @@ -26,6 +26,7 @@ import org.neo4j.cypher.internal.compatibility._
import org.neo4j.cypher.internal.compatibility.v3_3.runtime._
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.executionplan.procs.ProcedureCallOrSchemaCommandExecutionPlanBuilder
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.executionplan.{ExecutionPlan => ExecutionPlan_v3_3}
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.helpers.ValueConversion.asValues
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.helpers.simpleExpressionEvaluator
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.phases.CompilationState
import org.neo4j.cypher.internal.compiler.v3_3
Expand All @@ -37,7 +38,6 @@ import org.neo4j.cypher.internal.compiler.v3_3.planner.logical.{CachedMetricsFac
import org.neo4j.cypher.internal.compiler.v3_3.spi.PlanContext
import org.neo4j.cypher.internal.frontend.v3_3.InputPosition
import org.neo4j.cypher.internal.frontend.v3_3.ast.Statement
import org.neo4j.cypher.internal.frontend.v3_3.helpers.Eagerly
import org.neo4j.cypher.internal.frontend.v3_3.helpers.rewriting.RewriterStepSequencer
import org.neo4j.cypher.internal.frontend.v3_3.phases._
import org.neo4j.cypher.internal.javacompat.ExecutionResult
Expand All @@ -50,9 +50,6 @@ import org.neo4j.kernel.api.query.PlannerInfo
import org.neo4j.kernel.impl.query.QueryExecutionMonitor
import org.neo4j.kernel.monitoring.{Monitors => KernelMonitors}
import org.neo4j.logging.Log
import org.neo4j.values.storable.Values
import org.neo4j.values.virtual.VirtualValues
import org.neo4j.values.{AnyValue, AnyValues}

import scala.collection.JavaConverters._
import scala.util.Try
Expand Down Expand Up @@ -191,19 +188,7 @@ trait Compatibility[CONTEXT <: CommunityRuntimeContext,
))
}
}
private def asValues(params: Map[String, Any]): Map[String, AnyValue] = Eagerly.immutableMapValues(params, asValue)
private def asValue(value: Any): AnyValue = value match {
case null => Values.NO_VALUE
case s: String => Values.stringValue(s)
case d: Double => Values.doubleValue(d)
case f: Float => Values.doubleValue(f)
case n: Number => Values.longValue(n.longValue())
case b: Boolean => Values.booleanValue(b)
case m: Map[_, _] => VirtualValues.map(Eagerly.immutableMapValues(m.asInstanceOf[Map[String, Any]], asValue).asJava)
case m: java.util.Map[_, _] => AnyValues.asMapValue(m.asInstanceOf[java.util.Map[String, AnyRef]])
case a: TraversableOnce[_] => VirtualValues.list(a.map(asValue).toArray:_*)
case c: java.util.Collection[_] => AnyValues.asListValue(c)
}


def isPeriodicCommit: Boolean = inner.isPeriodicCommit

Expand Down
Expand Up @@ -25,6 +25,7 @@ import org.neo4j.cypher.internal.compatibility.v3_3.runtime.commands.predicates.
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.helpers.ListSupport
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.pipes.QueryState
import org.neo4j.values.AnyValue
import org.neo4j.values.storable.Values
import org.neo4j.values.virtual.ListValue

import scala.collection.Seq
Expand All @@ -41,7 +42,7 @@ abstract class InList(collectionExpression: Expression, id: String, predicate: P
def isMatch(m: ExecutionContext)(implicit state: QueryState): Option[Boolean] = {
val list = collectionExpression(m)

if (list == null) None
if (list == Values.NO_VALUE) None
else {
val seq = makeTraversable(list)

Expand All @@ -68,8 +69,9 @@ case class AllInList(collection: Expression, symbolName: String, inner: Predicat
private def forAll(collectionValue: ListValue)(predicate: (AnyValue => Option[Boolean])): Option[Boolean] = {
var result: Option[Boolean] = Some(true)

for (i <- 0 to collectionValue.size()) {
predicate(collectionValue.value(i)) match {
val iterator = collectionValue.iterator()
while(iterator.hasNext) {
predicate(iterator.next()) match {
case Some(false) => return Some(false)
case None => result = None
case _ =>
Expand All @@ -93,14 +95,14 @@ case class AnyInList(collection: Expression, symbolName: String, inner: Predicat

private def exists(collectionValue: ListValue)(predicate: (AnyValue => Option[Boolean])): Option[Boolean] = {
var result: Option[Boolean] = Some(false)
for (i <- 0 to collectionValue.size()) {
predicate(collectionValue.value(i)) match {
val iterator = collectionValue.iterator()
while(iterator.hasNext) {
predicate(iterator.next()) match {
case Some(true) => return Some(true)
case None => result = None
case _ =>
}
}

result
}

Expand All @@ -121,8 +123,9 @@ case class NoneInList(collection: Expression, symbolName: String, inner: Predica
private def none(collectionValue: ListValue)(predicate: (AnyValue => Option[Boolean])): Option[Boolean] = {
var result: Option[Boolean] = Some(true)

for (i <- 0 to collectionValue.size()) {
predicate(collectionValue.value(i)) match {
val iterator = collectionValue.iterator()
while(iterator.hasNext) {
predicate(iterator.next()) match {
case Some(true) => return Some(false)
case None => result = None
case _ =>
Expand All @@ -148,9 +151,9 @@ case class SingleInList(collection: Expression, symbolName: String, inner: Predi

private def single(collectionValue: ListValue)(predicate: (AnyValue => Option[Boolean])): Option[Boolean] = {
var matched = false

for (i <- 0 to collectionValue.size()) {
predicate(collectionValue.value(i)) match {
val iterator = collectionValue.iterator()
while(iterator.hasNext) {
predicate(iterator.next()) match {
case Some(true) if matched => return Some(false)
case Some(true) => matched = true
case None => return None
Expand Down
Expand Up @@ -55,7 +55,7 @@ case class PathExpression(pathPattern: Seq[Pattern], predicate: Predicate,
override def apply(ctx: ExecutionContext)(implicit state: QueryState): AnyValue = {
// If any of the points we need is null, the whole expression will return null
val returnNull = interestingPoints.exists(key => ctx.get(key) match {
case Some(null) => true
case Some(Values.NO_VALUE) => true
case None if !allowIntroducingNewIdentifiers =>
throw new AssertionError("This execution plan should not exist.")
case _ => false
Expand Down
Expand Up @@ -19,14 +19,16 @@
*/
package org.neo4j.cypher.internal.compatibility.v3_3.runtime.commands

import org.neo4j.cypher.internal.compatibility.v3_3.runtime.helpers.IsMap
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.helpers.{IsList, IsMap}
import org.neo4j.cypher.internal.frontend.v3_3.CypherTypeException
import org.neo4j.cypher.internal.frontend.v3_3.symbols._
import org.neo4j.cypher.internal.spi.v3_3.QueryContext
import org.neo4j.values._
import org.neo4j.values.storable._
import org.neo4j.values.virtual._

import scala.collection.JavaConverters._

object coerce {

def apply(value: AnyValue, typ: CypherType)(implicit context: QueryContext): AnyValue = {
Expand All @@ -37,8 +39,8 @@ object coerce {
case CTNode => value.asInstanceOf[NodeValue]
case CTRelationship => value.asInstanceOf[EdgeValue]
case CTPath => value.asInstanceOf[PathValue]
case CTInteger => value.asInstanceOf[IntegralValue]
case CTFloat => value.asInstanceOf[DoubleValue]
case CTInteger => Values.longValue(value.asInstanceOf[NumberValue].longValue())
case CTFloat => Values.doubleValue(value.asInstanceOf[NumberValue].doubleValue())
case CTMap => value match {
case IsMap(m) => m
case _ => throw cantCoerce(value, typ)
Expand All @@ -47,7 +49,8 @@ object coerce {
case p: PathValue if t.innerType == CTNode => throw cantCoerce(value, typ)
case p: PathValue if t.innerType == CTRelationship => throw cantCoerce(value, typ)
case p: PathValue => p.asList
case l: ListValue if t.innerType == CTAny => l
case IsList(coll) if t.innerType == CTAny => coll
case IsList(coll) => VirtualValues.list(coll.iterator().asScala.map(coerce(_, t.innerType)).toArray:_*)
case _ => throw cantCoerce(value, typ)
}
case CTBoolean => value.asInstanceOf[BooleanValue]
Expand Down
Expand Up @@ -30,7 +30,7 @@ case class CoalesceFunction(arguments: Expression*) extends Expression {
arguments.
view.
map(expression => expression(ctx)).
find(value => value != null) match {
find(value => value != Values.NO_VALUE) match {
case None => Values.NO_VALUE
case Some(x) => x
}
Expand Down
Expand Up @@ -24,7 +24,7 @@ import org.neo4j.cypher.internal.compatibility.v3_3.runtime.helpers.{CastSupport
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.pipes.QueryState
import org.neo4j.cypher.internal.frontend.v3_3.{CypherTypeException, InvalidArgumentException}
import org.neo4j.values._
import org.neo4j.values.storable.{FloatValue, NumberValue, TextValue, Values}
import org.neo4j.values.storable._

case class ContainerIndex(expression: Expression, index: Expression) extends NullInNullOutExpression(expression)
with ListSupport {
Expand Down Expand Up @@ -63,7 +63,7 @@ with ListSupport {
val number = CastSupport.castOrFail[NumberValue](item)

val longValue = number match {
case _: FloatValue =>
case _: FloatValue | _: DoubleValue=>
throw new CypherTypeException(s"Cannot index a list using an non-integer number, got $number")
case _ => number.longValue()
}
Expand Down
Expand Up @@ -20,11 +20,10 @@
package org.neo4j.cypher.internal.compatibility.v3_3.runtime.commands.expressions

import org.neo4j.cypher.internal.compatibility.v3_3.runtime.ExecutionContext
import org.neo4j.cypher.internal.compatibility.v3_3.runtime._
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.pipes.QueryState
import org.neo4j.cypher.internal.frontend.v3_3.ArithmeticException
import org.neo4j.values._
import org.neo4j.values.storable.{FloatValue, IntegralValue, NumberValue, Values}
import org.neo4j.values.storable._

case class Divide(a: Expression, b: Expression) extends Arithmetics(a, b) {
def operand = "/"
Expand All @@ -37,6 +36,7 @@ case class Divide(a: Expression, b: Expression) extends Arithmetics(a, b) {

(aVal, bVal) match {
case (_, l:IntegralValue) if l.longValue() == 0L => throw new ArithmeticException("/ by zero")
case (_, l:DoubleValue) if l.doubleValue() == 0L => throw new ArithmeticException("/ by zero")
case (_, l:FloatValue) if l.doubleValue() == 0L => throw new ArithmeticException("/ by zero")
case (x, y) if x == Values.NO_VALUE || y == Values.NO_VALUE => Values.NO_VALUE
case (x: NumberValue, y: NumberValue) => calc(x, y)
Expand Down
Expand Up @@ -28,7 +28,8 @@ import org.neo4j.values.AnyValue
case class KeysFunction(expr: Expression) extends NullInNullOutExpression(expr) {

override def compute(value: AnyValue, ctx: ExecutionContext)(implicit state: QueryState) = value match {
case IsMap(map) => map.keys
case IsMap(map) =>
map.keys

case x =>
throw new CypherTypeException(s"Expected $expr to be a node, a relationship, or a literal map, but it was ${x.getClass.getSimpleName}")
Expand Down
Expand Up @@ -29,7 +29,7 @@ case class LabelsFunction(nodeExpr: Expression) extends NullInNullOutExpression(

override def compute(value: AnyValue, m: ExecutionContext)
(implicit state: QueryState): AnyValue = value match {
case n: NodeValue => VirtualValues.list(n.labels())
case n: NodeValue => VirtualValues.fromArray(n.labels())
case x => throw new ParameterWrongTypeException("Expected a Node, got: " + x)
}

Expand Down
Expand Up @@ -20,12 +20,13 @@
package org.neo4j.cypher.internal.compatibility.v3_3.runtime.commands.expressions

import org.neo4j.cypher.internal.compatibility.v3_3.runtime.ExecutionContext
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.helpers.ValueConversion.asValue
import org.neo4j.cypher.internal.compatibility.v3_3.runtime.pipes.QueryState
import org.neo4j.values.{AnyValue, AnyValues}
import org.neo4j.values.AnyValue

case class Literal(v: Any) extends Expression {
//TODO this could have been figured out earlier
private val anyVal = AnyValues.of(v)
val anyVal = asValue(v)
def apply(ctx: ExecutionContext)(implicit state: QueryState): AnyValue = anyVal

def rewrite(f: (Expression) => Expression) = f(this)
Expand Down
Expand Up @@ -63,7 +63,7 @@ trait NumericHelper {

private def asNumber(a: AnyValue): NumberValue = a match {
case null => throw new CypherTypeException("Expected a numeric value for " + toString + ", but got null")
case n if n == Values.NO_VALUE => throw new CypherTypeException("Expected a numeric value for " + toString + ", but got null")
case Values.NO_VALUE => throw new CypherTypeException("Expected a numeric value for " + toString + ", but got null")
case n: NumberValue => n
case _ => throw new CypherTypeException("Expected a numeric value for " + toString + ", but got: " + a.toString)
}
Expand Down
Expand Up @@ -20,7 +20,9 @@
package org.neo4j.cypher.internal.compatibility.v3_3.runtime.commands.expressions

import org.neo4j.values.AnyValue
import org.neo4j.values.storable.Values
import org.neo4j.values.virtual._

import scala.collection.JavaConverters._
import scala.collection.mutable.ArrayBuffer

Expand All @@ -30,7 +32,7 @@ final class PathValueBuilder {
private val rels = ArrayBuffer.empty[EdgeValue]
private var nulled = false
private var previousNode: NodeValue = null
def result(): PathValue = if (nulled) null else VirtualValues.path(nodes.toArray, rels.toArray)
def result(): AnyValue = if (nulled) Values.NO_VALUE else VirtualValues.path(nodes.toArray, rels.toArray)

def clear(): PathValueBuilder = {
nodes.clear()
Expand Down
Expand Up @@ -53,7 +53,7 @@ case class ToStringFunction(argument: Expression) extends StringFunction(argumen

override def compute(value: AnyValue, m: ExecutionContext)(implicit state: QueryState): AnyValue = argument(m) match {
case v: IntegralValue => Values.stringValue(v.longValue().toString)
case v: FloatValue => Values.stringValue(v.doubleValue().toString)
case v: FloatingPointValue => Values.stringValue(v.doubleValue().toString)
case v: TextValue => v
case v: BooleanValue => Values.stringValue(v.booleanValue().toString)
case v =>
Expand Down
Expand Up @@ -39,7 +39,7 @@ case class ToBooleanFunction(a: Expression) extends NullInNullOutExpression(a) {
Values.booleanValue(v.stringValue().trim.toBoolean)
} catch {
case e: IllegalArgumentException =>
null
Values.NO_VALUE
}
case v =>
throw new ParameterWrongTypeException("Expected a Boolean or String, got: " + v.toString)
Expand Down
Expand Up @@ -44,7 +44,7 @@ case class ToIntegerFunction(a: Expression) extends NullInNullOutExpression(a) {
try {
val d = BigDecimal(v.stringValue())
if (d <= Long.MaxValue && d >= Long.MinValue) Values.longValue(d.toLong)
else throw new CypherTypeException(s"integer, $v, is too large")
else throw new CypherTypeException(s"integer, ${v.stringValue()}, is too large")
} catch {
case _: NumberFormatException =>
Values.NO_VALUE
Expand Down
Expand Up @@ -89,8 +89,7 @@ object indexQuery extends GraphElementPropertyFunctions {
Iterator.empty
else {
val neoValues = values.map(makeValueNeoSafe)
val index1 = index(neoValues)
index1.toIterator
index(neoValues).toIterator
}
}

Expand Down
Expand Up @@ -19,6 +19,8 @@
*/
package org.neo4j.cypher.internal.compatibility.v3_3.runtime.commands.predicates

import java.util

import org.neo4j.values.AnyValue
import org.neo4j.values.storable.Values
import org.neo4j.values.virtual.ListValue
Expand All @@ -33,14 +35,15 @@ trait Checker {
def contains(value: AnyValue): (Option[Boolean], Checker)
}

class BuildUp(iterator: ListValue) extends Checker {
class BuildUp(list: ListValue) extends Checker {
val iterator: util.Iterator[AnyValue] = list.iterator()
assert(iterator.hasNext)
private val cachedSet: mutable.Set[AnyValue] = new mutable.HashSet[AnyValue]

// If we don't return true, this is what we will return. If the collection contains any nulls, we'll return None,
// else we return Some(false).
private var falseResult: Option[Boolean] = Some(false)

assert(iterator.nonEmpty)

override def contains(value: AnyValue): (Option[Boolean], Checker) = {
if (value == Values.NO_VALUE) (None, this)
Expand All @@ -54,23 +57,20 @@ class BuildUp(iterator: ListValue) extends Checker {

private def checkAndBuildUpCache(value: AnyValue): (Option[Boolean], Checker) = {
var foundMatch = false
var i = 0
while (i < iterator.size() && !foundMatch) {
val nextValue = iterator.value(i)
while (iterator.hasNext && !foundMatch) {
val nextValue = iterator.next()

if (nextValue == Values.NO_VALUE) {
falseResult = None
} else {
cachedSet.add(nextValue)
foundMatch = nextValue.equals(value)
foundMatch = nextValue == value
}
i = i + 1
}

if (cachedSet.isEmpty) {
(None, NullListChecker)
} else {
val nextState = if (i < iterator.size()) this else new SetChecker(cachedSet, falseResult)
val nextState = if (iterator.hasNext) this else new SetChecker(cachedSet, falseResult)
val result = if (foundMatch) Some(true) else falseResult

(result, nextState)
Expand Down

0 comments on commit dd4a112

Please sign in to comment.