Skip to content

Commit

Permalink
[GAIA-IR] Map KeyId back into KeyName for labels and properties (#1615)
Browse files Browse the repository at this point in the history
[IR-Core] provide id-name-mapping query in IR-Core
[IR Compiler] parse label or property from id to name
[IR Compiler] add ci tests of name mapping for vineyard and groot
  • Loading branch information
BingqingLyu committed May 26, 2022
1 parent fa5718c commit 76d3388
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 285 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -974,43 +974,44 @@
reason = "unsupported")

// todo: return label is integer
@Graph.OptOut(
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
method = "g_VX1X_outE",
reason = "returned label is id")
@Graph.OptOut(
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
method = "g_VX4X_bothEXcreatedX",
reason = "returned label is id")
@Graph.OptOut(
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
method = "g_VX4X_bothE",
reason = "returned label is id")
@Graph.OptOut(
test = "org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasTest",
method = "g_E_hasXlabelXknowsX",
reason = "returned label is id")
@Graph.OptOut(
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
method = "g_VX2X_inE",
reason = "returned label is id")
@Graph.OptOut(
method =
"g_VX1X_outEXknowsX_asXhereX_hasXweight_1X_asXfakeX_inV_hasXname_joshX_selectXhereX",
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
reason = "returned label is id")
@Graph.OptOut(
method = "g_VX1X_outEXknowsX_asXhereX_hasXweight_1X_inV_hasXname_joshX_selectXhereX",
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
reason = "returned label is id")
@Graph.OptOut(
method = "g_VX1X_outEXknowsX_hasXweight_1X_asXhereX_inV_hasXname_joshX_selectXhereX",
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
reason = "returned label is id")
@Graph.OptOut(
method = "g_VX1X_outE_asXhereX_inV_hasXname_vadasX_selectXhereX",
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
reason = "returned label is id")
// @Graph.OptOut(
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
// method = "g_VX1X_outE",
// reason = "returned label is id")
// @Graph.OptOut(
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
// method = "g_VX4X_bothEXcreatedX",
// reason = "returned label is id")
// @Graph.OptOut(
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
// method = "g_VX4X_bothE",
// reason = "returned label is id")
// @Graph.OptOut(
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasTest",
// method = "g_E_hasXlabelXknowsX",
// reason = "returned label is id")
// @Graph.OptOut(
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
// method = "g_VX2X_inE",
// reason = "returned label is id")
// @Graph.OptOut(
// method =
//
// "g_VX1X_outEXknowsX_asXhereX_hasXweight_1X_asXfakeX_inV_hasXname_joshX_selectXhereX",
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
// reason = "returned label is id")
// @Graph.OptOut(
// method = "g_VX1X_outEXknowsX_asXhereX_hasXweight_1X_inV_hasXname_joshX_selectXhereX",
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
// reason = "returned label is id")
// @Graph.OptOut(
// method = "g_VX1X_outEXknowsX_hasXweight_1X_asXhereX_inV_hasXname_joshX_selectXhereX",
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
// reason = "returned label is id")
// @Graph.OptOut(
// method = "g_VX1X_outE_asXhereX_inV_hasXname_vadasX_selectXhereX",
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
// reason = "returned label is id")

// add more ignored tests which are out of ir range
@Graph.OptOut(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ public String getPlanAsJson() throws IOException {
if (file.exists()) {
file.delete();
}
irCoreLib.write_plan_to_json(ptrPlan, PLAN_JSON_FILE);
irCoreLib.writePlanToJson(ptrPlan, PLAN_JSON_FILE);
json = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
if (file.exists()) {
file.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface IrCoreLibrary extends Library {

Pointer initLogicalPlan();

void write_plan_to_json(Pointer plan, String jsonFile);
void writePlanToJson(Pointer plan, String jsonFile);

void destroyLogicalPlan(Pointer plan);

Expand Down Expand Up @@ -224,4 +224,6 @@ FfiError.ByValue appendPatternOperator(
FfiError.ByValue setParamsPredicate(Pointer params, String predicate);

FfiError.ByValue setParamsIsAllColumns(Pointer params);

FfiKeyResult.ByValue getKeyName(int keyId, FfiKeyType keyType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.alibaba.graphscope.common.jna.type;

import com.alibaba.graphscope.common.jna.IrTypeMapper;
import com.sun.jna.Structure;

@Structure.FieldOrder({"keyName", "error"})
public class FfiKeyResult extends Structure {
public FfiKeyResult() {
super(IrTypeMapper.INSTANCE);
}

public static class ByValue extends FfiKeyResult implements Structure.ByValue {}

public String keyName;
public FfiError.ByValue error;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.alibaba.graphscope.common.jna.type;

import com.alibaba.graphscope.common.jna.IntEnum;

public enum FfiKeyType implements IntEnum<FfiKeyType> {
Entity,
Relation,
Column;

@Override
public int getInt() {
return this.ordinal();
}

@Override
public FfiKeyType getEnum(int i) {
FfiKeyType opts[] = values();
if (i < opts.length && i >= 0) {
return opts[i];
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -998,43 +998,44 @@
reason = "unsupported")

// todo: return label is integer
@Graph.OptOut(
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
method = "g_VX1X_outE",
reason = "returned label is id")
@Graph.OptOut(
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
method = "g_VX4X_bothEXcreatedX",
reason = "returned label is id")
@Graph.OptOut(
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
method = "g_VX4X_bothE",
reason = "returned label is id")
@Graph.OptOut(
test = "org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasTest",
method = "g_E_hasXlabelXknowsX",
reason = "returned label is id")
@Graph.OptOut(
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
method = "g_VX2X_inE",
reason = "returned label is id")
@Graph.OptOut(
method =
"g_VX1X_outEXknowsX_asXhereX_hasXweight_1X_asXfakeX_inV_hasXname_joshX_selectXhereX",
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
reason = "returned label is id")
@Graph.OptOut(
method = "g_VX1X_outEXknowsX_asXhereX_hasXweight_1X_inV_hasXname_joshX_selectXhereX",
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
reason = "returned label is id")
@Graph.OptOut(
method = "g_VX1X_outEXknowsX_hasXweight_1X_asXhereX_inV_hasXname_joshX_selectXhereX",
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
reason = "returned label is id")
@Graph.OptOut(
method = "g_VX1X_outE_asXhereX_inV_hasXname_vadasX_selectXhereX",
test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
reason = "returned label is id")
// @Graph.OptOut(
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
// method = "g_VX1X_outE",
// reason = "returned label is id")
// @Graph.OptOut(
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
// method = "g_VX4X_bothEXcreatedX",
// reason = "returned label is id")
// @Graph.OptOut(
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
// method = "g_VX4X_bothE",
// reason = "returned label is id")
// @Graph.OptOut(
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.filter.HasTest",
// method = "g_E_hasXlabelXknowsX",
// reason = "returned label is id")
// @Graph.OptOut(
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.VertexTest",
// method = "g_VX2X_inE",
// reason = "returned label is id")
// @Graph.OptOut(
// method =
//
// "g_VX1X_outEXknowsX_asXhereX_hasXweight_1X_asXfakeX_inV_hasXname_joshX_selectXhereX",
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
// reason = "returned label is id")
// @Graph.OptOut(
// method = "g_VX1X_outEXknowsX_asXhereX_hasXweight_1X_inV_hasXname_joshX_selectXhereX",
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
// reason = "returned label is id")
// @Graph.OptOut(
// method = "g_VX1X_outEXknowsX_hasXweight_1X_asXhereX_inV_hasXname_joshX_selectXhereX",
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
// reason = "returned label is id")
// @Graph.OptOut(
// method = "g_VX1X_outE_asXhereX_inV_hasXname_vadasX_selectXhereX",
// test = "org.apache.tinkerpop.gremlin.process.traversal.step.map.SelectTest",
// reason = "returned label is id")

// add more ignored tests which are out of ir range
@Graph.OptOut(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.alibaba.graphscope.gremlin.result;

import com.alibaba.graphscope.common.jna.type.FfiKeyType;
import com.alibaba.graphscope.gaia.proto.Common;
import com.alibaba.graphscope.gaia.proto.IrResult;
import com.alibaba.graphscope.gaia.proto.OuterExpression;
Expand Down Expand Up @@ -61,7 +62,7 @@ public Object parseFrom(IrResult.Results results) {
@Override
public Object parseFrom(IrResult.Results results) {
IrResult.Record record = results.getRecord();
logger.info("{}", record);
logger.debug("{}", record);
Map<String, Object> projectResult = new HashMap<>();
record.getColumnsList()
.forEach(
Expand All @@ -75,12 +76,13 @@ public Object parseFrom(IrResult.Results results) {
projectTags.forEach(
(k, v) -> {
if (!(v instanceof EmptyValue)) {
String property = (String) k.get(1);
if (property.isEmpty()) {
String nameOrId = (String) k.get(1);
if (nameOrId.isEmpty()) {
throw new GremlinResultParserException(
"map value should have property"
+ " key");
}
String property = getPropertyName(nameOrId);
Map tagEntry =
(Map)
projectResult.computeIfAbsent(
Expand Down Expand Up @@ -124,6 +126,17 @@ private String getColumnKeyAsResultKey(OuterExpression.NameOrId columnKey) {
throw new GremlinResultParserException(columnKey.getItemCase() + " is invalid");
}
}

// propertyId is in String format, i.e. "1"
private String getPropertyName(String nameOrId) {
OuterExpression.NameOrId.Builder builder = OuterExpression.NameOrId.newBuilder();
if (nameOrId.matches("^[0-9]+$")) {
builder.setId(Integer.valueOf(nameOrId));
} else {
builder.setName(nameOrId);
}
return ParserUtils.getKeyName(builder.build(), FfiKeyType.Column);
}
},
GROUP {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

package com.alibaba.graphscope.gremlin.result;

import com.alibaba.graphscope.common.jna.IrCoreLibrary;
import com.alibaba.graphscope.common.jna.type.*;
import com.alibaba.graphscope.gaia.proto.Common;
import com.alibaba.graphscope.gaia.proto.IrResult;
import com.alibaba.graphscope.gaia.proto.OuterExpression;
import com.alibaba.graphscope.gremlin.exception.GremlinResultParserException;

import org.apache.tinkerpop.gremlin.structure.Edge;
Expand All @@ -34,6 +37,7 @@

public class ParserUtils {
private static final Logger logger = LoggerFactory.getLogger(ParserUtils.class);
private static final IrCoreLibrary irCoreLib = IrCoreLibrary.INSTANCE;

public static Object parseElement(IrResult.Element element) {
switch (element.getInnerCase()) {
Expand Down Expand Up @@ -109,22 +113,45 @@ private static Object parseCommonValue(Common.Value value) {

private static Vertex parseVertex(IrResult.Vertex vertex) {
Map<String, Object> properties = parseProperties(vertex.getPropertiesList());
return new DetachedVertex(vertex.getId(), vertex.getLabel().getName(), properties);
return new DetachedVertex(
vertex.getId(), getKeyName(vertex.getLabel(), FfiKeyType.Entity), properties);
}

private static Edge parseEdge(IrResult.Edge edge) {
Map<String, Object> edgeProperties = parseProperties(edge.getPropertiesList());
return new DetachedEdge(
edge.getId(),
edge.getLabel().getName(),
getKeyName(edge.getLabel(), FfiKeyType.Relation),
edgeProperties,
edge.getSrcId(),
edge.getSrcLabel().getName(),
getKeyName(edge.getSrcLabel(), FfiKeyType.Entity),
edge.getDstId(),
edge.getDstLabel().getName());
getKeyName(edge.getDstLabel(), FfiKeyType.Entity));
}

private static Map<String, Object> parseProperties(List<IrResult.Property> properties) {
return new HashMap<>();
}

public static String getKeyName(OuterExpression.NameOrId key, FfiKeyType type) {
switch (key.getItemCase()) {
case NAME:
return key.getName();
case ID:
{
FfiKeyResult result = irCoreLib.getKeyName(key.getId(), type);
if (result.error == null || result.error.code != ResultCode.Success) {
String errorMsg =
(result.error == null) ? "error code is null" : result.error.msg;
throw new GremlinResultParserException("getKeyName fail " + errorMsg);
}
return result.keyName;
}
default:
// throw new GremlinResultParserException("key type " + key.getItemCase().name() + "
// is invalid");
logger.error("{}", "key type is not set");
return "";
}
}
}

0 comments on commit 76d3388

Please sign in to comment.