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

Ternary logic expressions in RETURN #43

Merged
merged 1 commit into from
Apr 12, 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 @@ -16,6 +16,7 @@
package org.opencypher.gremlin.queries;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -25,6 +26,7 @@
import com.google.common.collect.ImmutableMap;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
Expand All @@ -39,7 +41,11 @@ public class ReturnTest {
public static final GremlinServerExternalResource gremlinServer = new GremlinServerExternalResource();

private List<Map<String, Object>> submitAndGet(String cypher) {
return gremlinServer.cypherGremlinClient().submit(cypher).all();
return submitAndGet(cypher, emptyMap());
}

private List<Map<String, Object>> submitAndGet(String cypher, Map<String, ?> parameters) {
return gremlinServer.cypherGremlinClient().submit(cypher, parameters).all();
}

@Test
Expand Down Expand Up @@ -388,4 +394,52 @@ public void returnCoalesce() throws Exception {
"java", "java"
);
}

@Test
public void ternaryLogic() throws Exception {
Map<String, Object> args = new HashMap<>();
args.put("t", true);
args.put("t2", true);
args.put("f", false);
args.put("f2", false);
args.put("n", null);
args.put("n2", null);

Map<String, Boolean> tests = new LinkedHashMap<>();
tests.put("NOT $n", null);
tests.put("NOT $t", false);
tests.put("NOT $f", true);

tests.put("$t AND $t2", true);
tests.put("$t AND $f", false);
tests.put("$f AND $f2", false);
tests.put("$t AND $n", null);
tests.put("$f AND $n", false);

tests.put("$t OR $t2", true);
tests.put("$t OR $f", true);
tests.put("$f OR $f2", false);
tests.put("$t OR $n", true);
tests.put("$f OR $n", null);

tests.put("$t XOR $t2", false);
tests.put("$f XOR $f2", false);
tests.put("$t XOR $f", true);
tests.put("$f XOR $t", true);
tests.put("$n XOR $n2", null);
tests.put("$n XOR $t", null);
tests.put("$t XOR $n", null);
tests.put("$n XOR $f", null);
tests.put("$f XOR $n", null);

for (Map.Entry<String, Boolean> entry : tests.entrySet()) {
String expr = entry.getKey();
Boolean result = entry.getValue();
List<Map<String, Object>> results = submitAndGet("RETURN " + expr, args);

assertThat(results)
.extracting(expr)
.containsExactly(result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
*/
package org.opencypher.gremlin.translation.bytecode;

import static org.opencypher.gremlin.translation.Tokens.NULL;

import java.util.Optional;
import org.apache.tinkerpop.gremlin.process.traversal.Bytecode.Binding;
import org.opencypher.gremlin.translation.GremlinBindings;

public class BytecodeGremlinBindings implements GremlinBindings {
@Override
public Object bind(String name, Object value) {
return new Binding<>(name, value);
return new Binding<>(name, Optional.ofNullable(value).orElse(NULL));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
package org.opencypher.gremlin.translation.traversal;

import org.opencypher.gremlin.translation.GremlinBindings;
import org.opencypher.gremlin.translation.Tokens;

public class TraversalGremlinBindings implements GremlinBindings {
@Override
public Object bind(String name, Object value) {
if (value == null) {
return Tokens.NULL;
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,72 @@ private class ProjectionWalker[T, P](context: StatementContext[T, P], g: Gremlin
)
)
)

case Add(e1, e2) => math(alias, unfold, finalize, e1, e2, "+")
case Subtract(e1, e2) => math(alias, unfold, finalize, e1, e2, "-")
case Multiply(e1, e2) => math(alias, unfold, finalize, e1, e2, "*")
case Divide(e1, e2) => math(alias, unfold, finalize, e1, e2, "/")
case Pow(e1, e2) => math(alias, unfold, finalize, e1, e2, "^")
case Modulo(e1, e2) => math(alias, unfold, finalize, e1, e2, "%")

case Not(rhs) =>
val rhsT = pivot(alias, rhs, select, unfold, finalize = false)._2
(
Pivot,
__.choose(
__.map(rhsT).is(p.isEq(NULL)),
__.constant(NULL),
__.choose(
__.map(rhsT).is(p.isEq(true)),
__.constant(false),
__.constant(true)
)
)
)
case Ands(ands) =>
val traversals = ands.map(pivot(alias, _, select, unfold, finalize = false)._2).toSeq
(
Pivot,
__.choose(
__.and(traversals.map(__.map(_)).map(_.is(p.isEq(true))): _*),
__.constant(true),
__.choose(
__.or(traversals.map(__.map(_)).map(_.is(p.isEq(false))): _*),
__.constant(false),
__.constant(NULL)
)
)
)
case Ors(ors) =>
val traversals = ors.map(pivot(alias, _, select, unfold, finalize = false)._2).toSeq
(
Pivot,
__.choose(
__.or(traversals.map(__.map(_)).map(_.is(p.isEq(true))): _*),
__.constant(true),
__.choose(
__.and(traversals.map(__.map(_)).map(_.is(p.isEq(false))): _*),
__.constant(false),
__.constant(NULL)
)
)
)
case Xor(lhs, rhs) =>
val lhsT = pivot(alias, lhs, select, unfold, finalize = false)._2
val rhsT = pivot(alias, rhs, select, unfold, finalize = false)._2
(
Pivot,
__.choose(
__.or(__.map(lhsT).is(p.isEq(NULL)), __.map(rhsT).is(p.isEq(NULL))),
__.constant(NULL),
__.choose(
__.map(rhsT).as(TEMP).map(lhsT).where(p.neq(TEMP)),
__.constant(true),
__.constant(false)
)
)
)

case _ => aggregation(alias, expression, select, finalize)
}
}
Expand Down