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

Provide return types for standalone call queries #143

Merged
merged 5 commits into from
Jul 18, 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 @@ -20,6 +20,9 @@
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.opencypher.gremlin.extension.CypherBinding.binding;
import static org.opencypher.gremlin.extension.CypherBindingType.FLOAT;
import static org.opencypher.gremlin.extension.CypherBindingType.INTEGER;
import static org.opencypher.gremlin.extension.CypherBindingType.STRING;
import static org.opencypher.gremlin.extension.CypherProcedure.cypherProcedure;

import java.util.HashSet;
Expand All @@ -36,7 +39,7 @@ public TestProcedures() {
procedures.add(cypherProcedure(
"test.getName",
emptyList(),
singletonList(binding("name", String.class)),
singletonList(binding("name", STRING)),
arguments -> asList(
singletonMap("name", "marko"),
singletonMap("name", "vadas")
Expand All @@ -45,8 +48,8 @@ public TestProcedures() {

procedures.add(cypherProcedure(
"test.inc",
singletonList(binding("a", Long.class)),
singletonList(binding("r", Long.class)),
singletonList(binding("a", INTEGER)),
singletonList(binding("r", INTEGER)),
arguments -> {
long a = (long) arguments.get("a");
return singletonList(singletonMap("r", a + 1));
Expand All @@ -55,8 +58,8 @@ public TestProcedures() {

procedures.add(cypherProcedure(
"test.incF",
singletonList(binding("a", Double.class)),
singletonList(binding("r", Double.class)),
singletonList(binding("a", FLOAT)),
singletonList(binding("r", FLOAT)),
arguments -> {
double a = (double) arguments.get("a");
return singletonList(singletonMap("r", a + 1));
Expand All @@ -66,7 +69,7 @@ public TestProcedures() {
procedures.add(cypherProcedure(
"test.multi",
emptyList(),
asList(binding("foo", String.class), binding("bar", String.class)),
asList(binding("foo", STRING), binding("bar", STRING)),
arguments -> {
Map<String, Object> row = new LinkedHashMap<>();
row.put("bar", "bar");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ private List<Map<String, Object>> submitAndGet(String cypher, Map<String, Object
.traversal(g)
.enableCypherExtensions()
.build();
CypherAst ast = CypherAst.parse(cypher, parameters);
Seq<GremlinStep> ir = ast.translate(flavor, procedureContext);
CypherAst ast = CypherAst.parse(cypher, parameters, procedureContext);
Seq<GremlinStep> ir = ast.translate(flavor);
GraphTraversal<?, ?> traversal = TranslationWriter.write(ir, translator, parameters);
ReturnNormalizer returnNormalizer = ReturnNormalizer.create(ast.getReturnTypes());
return traversal.toStream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.opencypher.gremlin.extension.CypherBinding;
import org.opencypher.gremlin.extension.CypherBindingType;

public final class PredefinedProcedureRegistry {
private PredefinedProcedureRegistry() {
Expand Down Expand Up @@ -72,27 +73,12 @@ private static List<CypherBinding> matchArguments(String input) {
Matcher matcher = ARGUMENT_PATTERN.matcher(input);
while (matcher.find()) {
String name = matcher.group("name");
Class<?> type = typeFromSignature(matcher.group("type"));
CypherBindingType type = CypherBindingType.fromSignature(matcher.group("type"));
arguments.add(binding(name, type));
}
return arguments;
}

private static Class<?> typeFromSignature(String type) {
switch (type) {
case "FLOAT?":
return Double.class;
case "INTEGER?":
return Long.class;
case "NUMBER?":
return Number.class;
case "STRING?":
return String.class;
default:
throw new IllegalArgumentException("Unparsable procedure type: " + type);
}
}

private static Map<String, Object> extractKeys(List<String> keys, Map<String, Object> src) {
Map<String, Object> dest = new HashMap<>();
for (String key : keys) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.apache.tinkerpop.gremlin.structure.Vertex
import org.opencypher.gremlin.translation.CypherAst
import org.opencypher.gremlin.translation.ReturnProperties._
import org.opencypher.gremlin.translation.exception.{ConstraintException, SyntaxException, TypeException}
import org.opencypher.gremlin.traversal.ProcedureContext
import org.opencypher.tools.tck.api.{CypherValueRecords, ExecutionFailed}
import org.opencypher.tools.tck.constants.TCKErrorPhases.RUNTIME
import org.opencypher.tools.tck.values._
Expand Down Expand Up @@ -98,7 +99,9 @@ object TckGremlinCypherValueConverter {

private def emptyHeaderWorkaround(query: String, header: List[String], rows: List[Map[String, CypherValue]]) = {
if (rows.isEmpty) {
val columns = CypherAst.parse(query, new util.HashMap[String, Any]).statement.returnColumns
val procedureContext = ProcedureContext.global()
val ast = CypherAst.parse(query, new util.HashMap[String, Any], procedureContext)
val columns = ast.statement.returnColumns
CypherValueRecords.emptyWithHeader(columns)
} else {
CypherValueRecords(header, rows)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ private void evalCypher(Context context) throws OpProcessorException {
GraphTraversalSource gts = traversal(context);
DefaultGraphTraversal g = new DefaultGraphTraversal(gts.clone());
Map<String, Object> parameters = ParameterNormalizer.normalize(getParameters(args));
CypherAst ast = CypherAst.parse(cypher, parameters);

ProcedureContext procedureContext = ProcedureContext.global();
CypherAst ast = CypherAst.parse(cypher, parameters, procedureContext);

TranslatorFlavor flavor = TranslatorFlavor.gremlinServer();
Seq<GremlinStep> ir = ast.translate(flavor, procedureContext);
Seq<GremlinStep> ir = ast.translate(flavor);

Translator<String, GroovyPredicate> stringTranslator = Translator.builder()
.gremlinGroovy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@

public final class CypherBinding {
private final String name;
private final Class<?> type;
private final CypherBindingType type;

public CypherBinding(String name, Class<?> type) {
public CypherBinding(String name, CypherBindingType type) {
this.name = name;
this.type = type;
}

public static CypherBinding binding(String name, Class<?> type) {
public static CypherBinding binding(String name, CypherBindingType type) {
return new CypherBinding(name, type);
}

public String getName() {
return name;
}

public Class<?> getType() {
public CypherBindingType getType() {
return type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2018 "Neo4j, Inc." [https://neo4j.com]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opencypher.gremlin.extension;

import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public enum CypherBindingType {
ANY(Object.class),
BOOLEAN(Boolean.class),
STRING(String.class),
NUMBER(Number.class),
FLOAT(Float.class),
INTEGER(Integer.class),
MAP(Map.class),
LIST(List.class),
NODE(Map.class),
RELATIONSHIP(Map.class);

private final Class<?> javaClass;

CypherBindingType(Class<?> javaClass) {
this.javaClass = javaClass;
}

public Class<?> getJavaClass() {
return javaClass;
}

public boolean isAssignableFrom(CypherBindingType type) {
return getJavaClass().isAssignableFrom(type.getJavaClass());
}

public static CypherBindingType fromSignature(String type) {
return Stream.of(values())
.filter(v -> (v.name() + "?").equals(type))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Unparsable procedure type: " + type));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static java.util.stream.Collectors.toList;
import static org.opencypher.gremlin.extension.CypherBindingType.FLOAT;
import static org.opencypher.gremlin.extension.CypherBindingType.INTEGER;
import static org.opencypher.gremlin.extension.CypherBindingType.NUMBER;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -31,6 +34,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import org.opencypher.gremlin.extension.CypherBinding;
import org.opencypher.gremlin.extension.CypherBindingType;
import org.opencypher.gremlin.extension.CypherProcedure;

public final class ProcedureContext {
Expand Down Expand Up @@ -97,7 +101,8 @@ private Object call(String name, Collection<?> arguments) {
// Defined argument types
List<Class<?>> defArgTypes = defArgs.stream()
.map(CypherBinding::getType)
.map(type -> Number.class.isAssignableFrom(type) ? Number.class : type)
.map(type -> NUMBER.isAssignableFrom(type) ? NUMBER : type)
.map(CypherBindingType::getJavaClass)
.collect(toList());

// Argument types in call
Expand Down Expand Up @@ -125,7 +130,7 @@ private Object call(String name, Collection<?> arguments) {
Map<String, Object> implArgs = new HashMap<>();
for (int i = 0; i < defArgsSize; i++) {
String argName = defArgs.get(i).getName();
Class<?> argType = defArgs.get(i).getType();
CypherBindingType argType = defArgs.get(i).getType();
Object argValue = numericCast(callArgs.get(i), argType);
implArgs.put(argName, argValue);
}
Expand All @@ -138,7 +143,7 @@ private Object call(String name, Collection<?> arguments) {
Map<String, Object> orderedRow = new LinkedHashMap<>();
for (CypherBinding res : defResults) {
String resName = res.getName();
Class<?> resType = res.getType();
CypherBindingType resType = res.getType();
Object resValue = numericCast(row.get(resName), resType);
orderedRow.put(resName, resValue);
}
Expand All @@ -147,13 +152,13 @@ private Object call(String name, Collection<?> arguments) {
return results;
}

private static Object numericCast(Object value, Class<?> type) {
private static Object numericCast(Object value, CypherBindingType type) {
if (value instanceof Number) {
Number number = (Number) value;
if (type.equals(Long.class)) {
if (type.equals(INTEGER)) {
return number.longValue();
}
if (type.equals(Double.class)) {
if (type.equals(FLOAT)) {
return number.doubleValue();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2018 "Neo4j, Inc." [https://neo4j.com]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opencypher.gremlin.extension

object CypherProcedures {
def procedureName(namespaceParts: Seq[String], name: String): String = {
namespaceParts.map(_ + ".").mkString + name
}
}
Loading