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

Basic implementation of Cypher head() function #149

Merged
merged 1 commit into from
Jul 31, 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 @@ -165,6 +165,17 @@ public void sizeOfPatternExpressionInReturn() {
.containsExactly(2L);
}

@Test
public void head() {
String cypher = "MATCH (n:person) WITH n.name AS name " +
"ORDER BY name RETURN head(collect(name)) AS head";
List<Map<String, Object>> results = submitAndGet(cypher);

assertThat(results)
.extracting("head")
.containsExactly("josh");
}

@Test
public void sizeOfPatternExpressionInWhere() {
String cypher = "MATCH (n:person) WHERE size( (n)-->() ) > 1 RETURN n.name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ GremlinSteps<T, P> choose(GremlinSteps<T, P> traversalPredicate,

GremlinSteps<T, P> limit(long limit);

GremlinSteps<T, P> limit(Scope scope, long limit);

GremlinSteps<T, P> local(GremlinSteps<T, P> localTraversal);

GremlinSteps<T, P> loops();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ public GremlinSteps<Bytecode, P> limit(long limit) {
return this;
}

@Override
public GremlinSteps<Bytecode, P> limit(Scope scope, long limit) {
bytecode.addStep(Symbols.limit, scope, limit);
return this;
}

@Override
public GremlinSteps<Bytecode, P> local(GremlinSteps<Bytecode, P> localTraversal) {
bytecode.addStep(Symbols.local, localTraversal.current());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ public GremlinSteps<String, GroovyPredicate> limit(long limit) {
return this;
}

@Override
public GremlinSteps<String, GroovyPredicate> limit(Scope scope, long limit) {
g.append(chain("limit", scope, limit));
return this;
}

@Override
public GremlinSteps<String, GroovyPredicate> local(GremlinSteps<String, GroovyPredicate> localTraversal) {
g.append(chain("local", traversal(localTraversal)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ public GremlinSteps<GraphTraversal, P> limit(long limit) {
return this;
}

@Override
public GremlinSteps<GraphTraversal, P> limit(Scope scope, long limit) {
g.limit(scope, limit);
return this;
}

@Override
public GremlinSteps<GraphTraversal, P> local(GremlinSteps<GraphTraversal, P> localTraversal) {
g.local(localTraversal.current());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ sealed class TranslationWriter[T, P] private (translator: Translator[T, P], para
g.label()
case Limit(limit) =>
g.limit(limit)
case LimitS(scope, limit) =>
g.limit(scope, limit)
case Local(traversal) =>
g.local(writeLocalSteps(traversal))
case Loops =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ class IRGremlinSteps extends GremlinSteps[Seq[GremlinStep], GremlinPredicate] {
this
}

override def limit(scope: Scope, limit: Long): GremlinSteps[Seq[GremlinStep], GremlinPredicate] = {
buf += LimitS(scope, limit)
this
}

override def local(
traversal: GremlinSteps[Seq[GremlinStep], GremlinPredicate]): GremlinSteps[Seq[GremlinStep], GremlinPredicate] = {
buf += Local(traversal.current())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ case object Label extends GremlinStep

case class Limit(limit: Long) extends GremlinStep

case class LimitS(scope: Scope, limit: Long) extends GremlinStep

case class Local(traversal: Seq[GremlinStep]) extends GremlinStep {
override def mapTraversals(f: Seq[GremlinStep] => Seq[GremlinStep]): GremlinStep = {
Local(f(traversal))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ private class ExpressionWalker[T, P](context: WalkerContext[T, P], g: GremlinSte
case "abs" => traversals.head.math("abs(_)")
case "coalesce" => __.coalesce(traversals.init.map(_.is(p.neq(NULL))) :+ traversals.last: _*)
case "exists" => traversals.head.flatMap(anyMatch(__.is(p.neq(NULL))))
case "head" => traversals.head.flatMap(notNull(__.limit(Scope.local, 1), context))
case "id" => traversals.head.flatMap(notNull(__.id(), context))
case "keys" => traversals.head.properties().key().fold()
case "labels" => traversals.head.label().is(p.neq(Vertex.DEFAULT_LABEL)).fold()
Expand Down