Skip to content

Commit

Permalink
Merge pull request #7542 from Mats-SX/3.1-named-paths-pattern-compreh…
Browse files Browse the repository at this point in the history
…ensions

Support named paths in pattern comprehensions
  • Loading branch information
Stefan Plantikow committed Jul 14, 2016
2 parents 6a64f6a + fe5ed30 commit cd4c108
Show file tree
Hide file tree
Showing 21 changed files with 561 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,61 @@
*/
package org.neo4j.internal.cypher.acceptance

import org.neo4j.cypher.internal.compiler.v3_1.commands.expressions.PathImpl
import org.neo4j.cypher.{ExecutionEngineFunSuite, NewPlannerTestSupport}

class PatternComprehensionAcceptanceTest extends ExecutionEngineFunSuite with NewPlannerTestSupport {

test("with named path") {
val n1 = createLabeledNode("Start")
val n2 = createLabeledNode("End")
val r = relate(n1, n2)

val query = "MATCH (n:Start) RETURN [p = (n)-->() | p] AS list"

val result = executeWithCostPlannerOnly(query)

result.toList should equal(List(Map("list" -> List(PathImpl(n1, r, n2)))))
}

test("with named path and predicate") {
val n1 = createLabeledNode("Start")
val n2 = createLabeledNode("End")
val n3 = createLabeledNode("NotEnd")
val r = relate(n1, n2)
relate(n2, n3)

val query = "MATCH (n:Start) RETURN [p = (n)-->() WHERE last(nodes(p)):End | p] AS list"

val result = executeWithCostPlannerOnly(query)

result.toList should equal(List(Map("list" -> List(PathImpl(n1, r, n2)))))
}

test("with named path and shadowed variable in predicate") {
val n1 = createLabeledNode("Start")
val n2 = createLabeledNode("End")
val r = relate(n1, n2)

val query = "MATCH (n:Start) RETURN [p = (n)-->(b) WHERE head([p IN ['foo'] | true ]) | p] AS list"

val result = executeWithCostPlannerOnly(query)

result.toList should equal(List(Map("list" -> List(PathImpl(n1, r, n2)))))
}

test("with named path and shadowed variable in projection") {
val n1 = createLabeledNode("Start")
val n2 = createLabeledNode("End")
val r = relate(n1, n2)

val query = "MATCH (n:Start) RETURN [p = (n)-->() | {path: p, other: [p IN ['foo'] | true ]} ] AS list"

val result = executeWithCostPlannerOnly(query)

result.toList should equal(List(Map("list" -> List(Map("path" -> PathImpl(n1, r, n2), "other" -> List(true))))))
}

test("one relationship out") {
val n1 = createLabeledNode(Map("x" -> 1), "START")
val n2 = createLabeledNode(Map("x" -> 2), "START")
Expand All @@ -35,7 +87,6 @@ class PatternComprehensionAcceptanceTest extends ExecutionEngineFunSuite with Ne
relate(n2, n4)
relate(n2, n5)

// val result = executeWithCostPlannerOnly("match (n:START) return n.x, [(n)-->() | n] as coll")
val result = executeWithCostPlannerOnly("match (n:START) return n.x, [(n)-->(other) | other.x] as coll")

result.toList should equal(List(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ class ASTRewriter(rewriterSequencer: (String) => RewriterStepSequencer, shouldEx
addUniquenessPredicates,
isolateAggregation,
enableCondition(aggregationsAreIsolated),
replaceLiteralDynamicPropertyLookups
replaceLiteralDynamicPropertyLookups,
namePatternComprehensionPatternElements,
enableCondition(noUnnamedPatternElementsInPatternComprehension),
inlineNamedPathsInPatternComprehensions
)

val rewrittenStatement = statement.endoRewrite(contract.rewriter)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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_1.ast.conditions

import org.neo4j.cypher.internal.frontend.v3_1.ast._
import org.neo4j.cypher.internal.compiler.v3_1.tracing.rewriters.Condition
import org.neo4j.cypher.internal.frontend.v3_1.Foldable._

case object noUnnamedPatternElementsInPatternComprehension extends Condition {

override def name: String = productPrefix

override def apply(that: Any): Seq[String] = that.treeFold(Seq.empty[String]) {
case expr: PatternComprehension if containsUnNamedPatternElement(expr.pattern) =>
acc => (acc :+ s"Expression $expr contains pattern elements which are not named", None)
}

private def containsUnNamedPatternElement(expr: RelationshipsPattern) = expr.exists {
case p: PatternElement => p.variable.isEmpty
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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_1.ast.rewriters

import org.neo4j.cypher.internal.frontend.v3_1.ast._
import org.neo4j.cypher.internal.frontend.v3_1.{Rewriter, bottomUp}

case object inlineNamedPathsInPatternComprehensions extends Rewriter {

private val instance = bottomUp(Rewriter.lift {
case expr @ PatternComprehension(Some(path), pattern, predicate, projection) =>
val patternElement = pattern.element
expr.copy(
namedPath = None,
predicate = predicate.map(_.inline(path, patternElement)),
projection = projection.inline(path, patternElement)
)(expr.position)
})

private implicit final class InliningExpression(val expr: Expression) extends AnyVal {
def inline(path: Variable, patternElement: PatternElement) =
expr.copyAndReplace(path) by {
PathExpression(projectNamedPaths.patternPartPathExpression(patternElement))(expr.position)
}
}

override def apply(v: AnyRef): AnyRef = instance(v)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
*/
package org.neo4j.cypher.internal.compiler.v3_1.ast.rewriters

import org.neo4j.cypher.internal.frontend.v3_1.ast._
import org.neo4j.cypher.internal.compiler.v3_1.helpers.UnNamedNameGenerator
import org.neo4j.cypher.internal.compiler.v3_1.planner.logical.PatternExpressionPatternElementNamer
import org.neo4j.cypher.internal.frontend.v3_1.ast._
import org.neo4j.cypher.internal.frontend.v3_1.{Rewriter, bottomUp}

case object nameAllPatternElements extends Rewriter {
Expand All @@ -38,14 +37,3 @@ case object nameAllPatternElements extends Rewriter {
pattern.copy(variable = Some(Variable(syntheticName)(pattern.position)))(pattern.position)
})
}

case object namePatternPredicatePatternElements extends Rewriter {

override def apply(in: AnyRef): AnyRef = instance.apply(in)

private val instance: Rewriter = bottomUp(Rewriter.lift {
case expr: PatternExpression =>
val (rewrittenExpr, _) = PatternExpressionPatternElementNamer(expr)
rewrittenExpr
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package org.neo4j.cypher.internal.compiler.v3_1.ast.rewriters

import org.neo4j.cypher.internal.frontend.v3_1.ast._
import org.neo4j.cypher.internal.frontend.v3_1.{bottomUp, Rewriter}
import org.neo4j.cypher.internal.frontend.v3_1.{Rewriter, bottomUp}

case object nameMatchPatternElements extends Rewriter {

Expand All @@ -35,23 +35,7 @@ case object nameMatchPatternElements extends Rewriter {
private val instance = bottomUp(rewriter, _.isInstanceOf[Expression])
}

case object nameUpdatingClauses extends Rewriter {

def apply(that: AnyRef): AnyRef = instance(that)

private val findingRewriter: Rewriter = Rewriter.lift {
case createUnique@CreateUnique(pattern) =>
val rewrittenPattern = pattern.endoRewrite(nameAllPatternElements.namingRewriter)
createUnique.copy(pattern = rewrittenPattern)(createUnique.position)

case create@Create(pattern) =>
val rewrittenPattern = pattern.endoRewrite(nameAllPatternElements.namingRewriter)
create.copy(pattern = rewrittenPattern)(create.position)

case merge@Merge(pattern, _) =>
val rewrittenPattern = pattern.endoRewrite(nameAllPatternElements.namingRewriter)
merge.copy(pattern = rewrittenPattern)(merge.position)
}

private val instance = bottomUp(findingRewriter, _.isInstanceOf[Expression])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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_1.ast.rewriters

import org.neo4j.cypher.internal.compiler.v3_1.planner.logical.PatternExpressionPatternElementNamer
import org.neo4j.cypher.internal.frontend.v3_1.ast._
import org.neo4j.cypher.internal.frontend.v3_1.{Rewriter, bottomUp}

case object namePatternComprehensionPatternElements extends Rewriter {

private val instance = bottomUp(Rewriter.lift {
case expr: PatternComprehension =>
val (namedComprehension, _) = PatternExpressionPatternElementNamer(expr)
namedComprehension
})

override def apply(v: AnyRef): AnyRef = instance(v)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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_1.ast.rewriters

import org.neo4j.cypher.internal.frontend.v3_1.ast._
import org.neo4j.cypher.internal.compiler.v3_1.planner.logical.PatternExpressionPatternElementNamer
import org.neo4j.cypher.internal.frontend.v3_1.{Rewriter, bottomUp}

case object namePatternPredicatePatternElements extends Rewriter {

override def apply(in: AnyRef): AnyRef = instance.apply(in)

private val instance: Rewriter = bottomUp(Rewriter.lift {
case expr: PatternExpression =>
val (rewrittenExpr, _) = PatternExpressionPatternElementNamer(expr)
rewrittenExpr
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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_1.ast.rewriters

import org.neo4j.cypher.internal.frontend.v3_1.ast._
import org.neo4j.cypher.internal.frontend.v3_1.{Rewriter, bottomUp}

case object nameUpdatingClauses extends Rewriter {

def apply(that: AnyRef): AnyRef = instance(that)

private val findingRewriter: Rewriter = Rewriter.lift {
case createUnique@CreateUnique(pattern) =>
val rewrittenPattern = pattern.endoRewrite(nameAllPatternElements.namingRewriter)
createUnique.copy(pattern = rewrittenPattern)(createUnique.position)

case create@Create(pattern) =>
val rewrittenPattern = pattern.endoRewrite(nameAllPatternElements.namingRewriter)
create.copy(pattern = rewrittenPattern)(create.position)

case merge@Merge(pattern, _) =>
val rewrittenPattern = pattern.endoRewrite(nameAllPatternElements.namingRewriter)
merge.copy(pattern = rewrittenPattern)(merge.position)
}

private val instance = bottomUp(findingRewriter, _.isInstanceOf[Expression])
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,24 @@ case object projectNamedPaths extends Rewriter {
}

def patternPartPathExpression(patternPart: AnonymousPatternPart): PathStep = patternPart match {
case EveryPath(element) => flip(element, NilPathStep)
case EveryPath(element) => patternPartPathExpression(element)
case _ => throw new CantHandleQueryException
}

def patternPartPathExpression(element: PatternElement): PathStep = flip(element, NilPathStep)

@tailrec
private def flip(element: PatternElement, step: PathStep): PathStep = {
element match {
case NodePattern(node, _, _) =>
NodePathStep(node.get, step)
NodePathStep(node.get.copyId, step)

case RelationshipChain(relChain, RelationshipPattern(rel, _, _, length, _, direction), _) => length match {
case None =>
flip(relChain, SingleRelationshipPathStep(rel.get, direction, step))
flip(relChain, SingleRelationshipPathStep(rel.get.copyId, direction, step))

case Some(_) =>
flip(relChain, MultiRelationshipPathStep(rel.get, direction, step))
flip(relChain, MultiRelationshipPathStep(rel.get.copyId, direction, step))
}
}
}
Expand Down

0 comments on commit cd4c108

Please sign in to comment.