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

Fixed related bugs with fold and rewriting #188

Merged
merged 1 commit into from
Oct 5, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ object SimplifySingleProjections extends GremlinRewriter {
case SelectK(key1) :: rest if key == key1 && rest.isEmpty =>
Identity :: Nil
case _ =>
steps.map({ step =>
step.mapTraversals(subSteps => mapTraversals(removeSelect(key, _))(subSteps))
})
removeSelectFromSubtraversals(key, steps)
}
}

Expand All @@ -77,6 +75,15 @@ object SimplifySingleProjections extends GremlinRewriter {
} ::: rest
}

private def removeSelectFromSubtraversals(key: String, steps: Seq[GremlinStep]): Seq[GremlinStep] = {
val (replaceable, rest) =
steps.span(s => !s.isInstanceOf[Constant] || s.isInstanceOf[SelectC] || s.isInstanceOf[SelectK])

replaceable.map({ step =>
step.mapTraversals(subSteps => mapTraversals(removeSelect(key, _))(subSteps))
}) ++ rest
}

private def eqUnused(stepLabel: String, key2: String): Boolean = {
stepLabel == UNUSED && key2 == UNUSED
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ private class ExpressionWalker[T, P](context: WalkerContext[T, P], g: GremlinSte
case "exists" => traversals.head.flatMap(anyMatch(__.is(p.neq(NULL))))
case "head" => traversals.head.flatMap(emptyToNull(__.limit(Scope.local, 1), context))
case "id" => traversals.head.flatMap(notNull(__.id(), context))
case "keys" if onEntity => traversals.head.properties().key().fold()
case "keys" if onEntity => traversals.head.map(__.properties().key().fold())
case "keys" => traversals.head.select(Column.keys)
case "labels" => traversals.head.label().is(p.neq(Vertex.DEFAULT_LABEL)).fold()
case "labels" => traversals.head.map(__.label().is(p.neq(Vertex.DEFAULT_LABEL)).fold())
case "length" => traversals.head.count(Scope.local).math("(_-1)/2")
case "last" => traversals.head.flatMap(emptyToNull(__.tail(Scope.local, 1), context))
case "nodes" => traversals.head.flatMap(filterElements(args, includeNodes = true))
Expand Down Expand Up @@ -258,7 +258,7 @@ private class ExpressionWalker[T, P](context: WalkerContext[T, P], g: GremlinSte
val functionT = walkLocal(function, maybeAlias)

val Variable(dependencyName) = function.dependencies.head
targetT.unfold().as(dependencyName).flatMap(functionT).fold()
targetT.map(__.unfold().as(dependencyName).flatMap(functionT).fold())

case PatternComprehension(_, RelationshipsPattern(relationshipChain), maybePredicate, PathExpression(_), _) =>
val select = __
Expand Down Expand Up @@ -287,10 +287,10 @@ private class ExpressionWalker[T, P](context: WalkerContext[T, P], g: GremlinSte

val functionT = walkLocal(projection, maybeAlias)
if (projection.dependencies.isEmpty) {
traversal.flatMap(functionT).fold()
__.map(traversal.flatMap(functionT).fold())
} else if (projection.dependencies.size == 1) {
val Variable(dependencyName) = projection.dependencies.head
traversal.select(dependencyName).flatMap(functionT).fold()
__.map(traversal.select(dependencyName).flatMap(functionT).fold())
} else {
context.unsupported("pattern comprehension with multiple arguments", expression)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@ class RemoveUselessStepsTest {

val flavor = new TranslatorFlavor(
rewriters = Seq(
InlineFlatMapTraversal
InlineFlatMapTraversal,
SimplifySingleProjections
),
postConditions = Nil
)

@Test def foldUnfold(): Unit = {
assertThat(parse("""
|MATCH (n)
|UNWIND labels(n) as l
|RETURN l
""".stripMargin))
assertThat(parse("MATCH (n) RETURN count(*)"))
.withFlavor(flavor)
.rewritingWith(RemoveUselessSteps)
.removes(__.fold().unfold())
Expand Down