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

Custom functions cypherProperties and cypherSize fallback (#160) #161

Merged
merged 1 commit into from
Aug 28, 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 @@ -15,12 +15,12 @@
*/
package org.opencypher.gremlin.translation.ir.rewrite

import org.apache.tinkerpop.gremlin.structure.Column.values
import org.apache.tinkerpop.gremlin.process.traversal.Scope.local
import org.opencypher.gremlin.translation.Tokens.NULL
import org.opencypher.gremlin.translation.exception.CypherExceptions
import org.opencypher.gremlin.translation.ir.TraversalHelper._
import org.opencypher.gremlin.translation.ir.model._
import org.opencypher.gremlin.traversal.CustomFunction.{cypherException, cypherPlus}
import org.opencypher.gremlin.traversal.CustomFunction.{cypherException, cypherPlus, cypherProperties, cypherSize}

/**
* Replaces Custom Functions with "The Best We Could Do" Gremlin native alternatives
Expand All @@ -37,6 +37,12 @@ object CustomFunctionFallback extends GremlinRewriter {

case SelectC(values) :: MapF(function) :: rest if function.getName == cypherPlus().getName =>
SelectC(values) :: Local(Unfold :: ChooseP(Neq(NULL), Sum :: Nil, Constant(NULL) :: Nil) :: Nil) :: rest

case MapF(function) :: rest if function.getName == cypherSize().getName =>
CountS(local) :: rest

case MapF(function) :: rest if function.getName == cypherProperties().getName =>
Local(Properties() :: Group :: By(Key :: Nil, None) :: By(MapT(Value :: Nil) :: Nil, None) :: Nil) :: rest
}))(steps)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ public void noCypherExtensions() {
"MATCH (n:N) " +
"WITH n.p AS s " +
"WHERE s STARTS WITH 'x' AND s ENDS WITH 'x' AND s CONTAINS 'x' " +
"RETURN size(s), toString(s)"
"RETURN toString(s)"
);
Translator<String, GroovyPredicate> translator = Translator.builder().gremlinGroovy().build();

assertThatThrownBy(() -> ast.buildTranslation(translator))
.hasMessageContaining("cypherContains, cypherEndsWith, cypherSize, cypherStarsWith, cypherToString");
.hasMessageContaining("cypherContains, cypherEndsWith, cypherStarsWith, cypherToString");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.opencypher.gremlin.translation.ir.rewrite

import org.apache.tinkerpop.gremlin.process.traversal.Scope
import org.apache.tinkerpop.gremlin.structure.Column
import org.junit.Test
import org.opencypher.gremlin.translation.CypherAst.parse
Expand Down Expand Up @@ -61,4 +62,28 @@ class CustomFunctionsFallbackTest {
.local(__.unfold().choose(P.neq(Tokens.NULL), __.sum(), __.start().constant(Tokens.NULL))))
}

@Test
def cypherSizeFallback(): Unit = {
assertThat(parse("RETURN size($noType) AS a"))
.withFlavor(flavor)
.rewritingWith(CustomFunctionFallback)
.removes(__.map(CustomFunction.cypherSize()))
.adds(__.count(Scope.local))
}

@Test
def cypherPropertiesFallback(): Unit = {
assertThat(parse("RETURN properties($noType) AS a"))
.withFlavor(flavor)
.rewritingWith(CustomFunctionFallback)
.removes(__.map(CustomFunction.cypherProperties()))
.adds(
__.local(
__.properties()
.group()
.by(__.key())
.by(__.map(__.value()))
))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ class NoCustomFunctionsTest {
|MATCH (n:N)
|WITH n.p AS s
|WHERE s STARTS WITH 'x' AND s ENDS WITH 'x' AND s CONTAINS 'x'
|RETURN size(s), toString(s)
|RETURN toString(s)
""".stripMargin)
val translator = Translator.builder.gremlinGroovy.build(flavor)

assertThatThrownBy(() => ast.buildTranslation(translator))
.hasMessageContaining("cypherContains, cypherEndsWith, cypherSize, cypherStarsWith, cypherToString")
.hasMessageContaining("cypherContains, cypherEndsWith, cypherStarsWith, cypherToString")
}
}