Skip to content

Commit

Permalink
Custom functions cypherProperties and cypherSize fallback (#160)
Browse files Browse the repository at this point in the history
- cypherSize - treat input as collection if type information and extensions are not available
- cypherProperties - treat input as element if type information and extensions are not available
- +2 TCK on Native

Signed-off-by: Dwitry dwitry@users.noreply.github.com
  • Loading branch information
dwitry committed Aug 28, 2018
1 parent ea18370 commit ba1bdc7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
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")
}
}

0 comments on commit ba1bdc7

Please sign in to comment.