use of com.alibaba.maxgraph.compiler.logical.function.ProcessorFilterFunction in project GraphScope by alibaba.
the class OrTreeNode method buildLogicalQueryPlan.
@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
TreeNodeLabelManager labelManager = contextManager.getTreeNodeLabelManager();
VertexIdManager vertexIdManager = contextManager.getVertexIdManager();
Message.LogicalCompare.Builder logicalCompareBuilder = Message.LogicalCompare.newBuilder().setCompare(Message.CompareType.OR_RELATION);
List<TreeNode> otherTreeNodeList = Lists.newArrayList();
boolean vertexFlag = getInputNode().getOutputValueType() instanceof VertexValueType;
orTreeNodeList.forEach(v -> {
if (v instanceof HasTreeNode && ((HasTreeNode) v).getInputNode() instanceof SourceTreeNode) {
HasTreeNode hasTreeNode = HasTreeNode.class.cast(v);
List<Message.LogicalCompare> logicalCompareList = Lists.newArrayList();
for (HasContainer hasContainer : hasTreeNode.getHasContainerList()) {
logicalCompareList.add(CompilerUtils.parseLogicalCompare(hasContainer, schema, labelManager.getLabelIndexList(), vertexFlag));
}
if (logicalCompareList.size() == 1) {
logicalCompareBuilder.addChildCompareList(logicalCompareList.get(0));
} else {
Message.LogicalCompare andLogicalCompare = Message.LogicalCompare.newBuilder().setCompare(Message.CompareType.AND_RELATION).addAllChildCompareList(logicalCompareList).build();
logicalCompareBuilder.addChildCompareList(andLogicalCompare);
}
} else {
otherTreeNodeList.add(v);
}
});
if (otherTreeNodeList.isEmpty()) {
ProcessorFilterFunction filterFunction = new ProcessorFilterFunction(QueryFlowOuterClass.OperatorType.HAS);
filterFunction.getLogicalCompareList().addAll(Lists.newArrayList(logicalCompareBuilder.build()));
return parseSingleUnaryVertex(vertexIdManager, labelManager, filterFunction, contextManager);
} else {
throw new UnsupportedOperationException();
}
}
use of com.alibaba.maxgraph.compiler.logical.function.ProcessorFilterFunction in project GraphScope by alibaba.
the class TreeNodeUtils method buildFilterTreeNode.
/**
* Build logical plan with filter tree node and output the input value
*/
public static LogicalVertex buildFilterTreeNode(TreeNode treeNode, ContextManager contextManager, LogicalQueryPlan logicalQueryPlan, LogicalVertex sourceVertex, GraphSchema schema) {
TreeNodeLabelManager labelManager = contextManager.getTreeNodeLabelManager();
VertexIdManager vertexIdManager = contextManager.getVertexIdManager();
TreeNode filterTreeNode = TreeNodeUtils.optimizeSubFilterNode(treeNode);
UnaryTreeNode unaryTreeNode = UnaryTreeNode.class.cast(filterTreeNode);
LogicalVertex outputVertex;
if (unaryTreeNode.getInputNode() instanceof SourceTreeNode && (unaryTreeNode instanceof SelectOneTreeNode || unaryTreeNode instanceof PropertyNode)) {
// optimize traversal filter to filter operator
int propId;
if (unaryTreeNode instanceof SelectOneTreeNode) {
propId = labelManager.getLabelIndex(SelectOneTreeNode.class.cast(unaryTreeNode).getSelectLabel());
} else {
propId = SchemaUtils.getPropId(PropertyNode.class.cast(unaryTreeNode).getPropKeyList().iterator().next(), schema);
}
Message.LogicalCompare logicalCompare = Message.LogicalCompare.newBuilder().setCompare(Message.CompareType.EXIST).setPropId(propId).build();
ProcessorFilterFunction processorFunction = new ProcessorFilterFunction(QueryFlowOuterClass.OperatorType.HAS);
if (propId < 0) {
processorFunction.getUsedLabelList().add(propId);
}
processorFunction.getLogicalCompareList().add(logicalCompare);
outputVertex = new LogicalUnaryVertex(vertexIdManager.getId(), processorFunction, false, sourceVertex);
logicalQueryPlan.addLogicalVertex(outputVertex);
logicalQueryPlan.addLogicalEdge(sourceVertex, outputVertex, new LogicalEdge());
} else {
TreeNode currentFilterTreeNode = TreeNodeUtils.buildSingleOutputNode(filterTreeNode, schema);
// build filter plan, and use join direct filter vertex to filter left stream
LogicalSubQueryPlan filterPlan = TreeNodeUtils.buildSubQueryPlan(currentFilterTreeNode, sourceVertex, contextManager);
TreeNode filterSourceNode = TreeNodeUtils.getSourceTreeNode(currentFilterTreeNode);
sourceVertex = filterSourceNode.getOutputVertex();
LogicalVertex rightVertex = filterPlan.getOutputVertex();
logicalQueryPlan.mergeLogicalQueryPlan(filterPlan);
if (TreeNodeUtils.checkJoinSourceFlag(currentFilterTreeNode)) {
LogicalBinaryVertex filterJoinVertex = new LogicalBinaryVertex(vertexIdManager.getId(), new ProcessorFunction(QueryFlowOuterClass.OperatorType.JOIN_DIRECT_FILTER), false, sourceVertex, rightVertex);
logicalQueryPlan.addLogicalVertex(filterJoinVertex);
logicalQueryPlan.addLogicalEdge(sourceVertex, filterJoinVertex, new LogicalEdge());
logicalQueryPlan.addLogicalEdge(rightVertex, filterJoinVertex, new LogicalEdge());
outputVertex = filterJoinVertex;
} else if (TreeNodeUtils.checkSelectFlag(currentFilterTreeNode)) {
String inputLabel = labelManager.createSysLabelStart(sourceVertex, "input");
ProcessorFunction selectFunction = createSelectOneFunction(inputLabel, Pop.last, labelManager.getLabelIndexList());
LogicalUnaryVertex selectVertex = new LogicalUnaryVertex(vertexIdManager.getId(), selectFunction, false, rightVertex);
logicalQueryPlan.addLogicalVertex(selectVertex);
logicalQueryPlan.addLogicalEdge(rightVertex, selectVertex, new LogicalEdge());
outputVertex = logicalQueryPlan.getOutputVertex();
} else {
outputVertex = logicalQueryPlan.getOutputVertex();
}
}
return outputVertex;
}
use of com.alibaba.maxgraph.compiler.logical.function.ProcessorFilterFunction in project GraphScope by alibaba.
the class BranchTreeNode method buildLogicalQueryPlan.
@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
LogicalSubQueryPlan logicalSubQueryPlan = new LogicalSubQueryPlan(contextManager);
LogicalVertex sourceVertex = getInputNode().getOutputVertex();
logicalSubQueryPlan.addLogicalVertex(sourceVertex);
int optionLabelId = 0;
boolean optionJoinFlag = true;
UnaryTreeNode branchUnaryNode = UnaryTreeNode.class.cast(branchTreeNode);
if (branchUnaryNode.getInputNode() instanceof SourceTreeNode) {
if (branchUnaryNode instanceof PropertyNode) {
String prop = PropertyNode.class.cast(branchUnaryNode).getPropKeyList().iterator().next();
optionLabelId = SchemaUtils.getPropId(prop, schema);
optionJoinFlag = false;
} else if (branchUnaryNode instanceof SelectOneTreeNode) {
optionLabelId = contextManager.getTreeNodeLabelManager().getLabelIndex(SelectOneTreeNode.class.cast(branchUnaryNode).getSelectLabel());
optionJoinFlag = false;
} else if (branchUnaryNode instanceof TokenTreeNode) {
optionLabelId = contextManager.getTreeNodeLabelManager().getLabelIndex(TokenTreeNode.class.cast(branchUnaryNode).getToken().getAccessor());
optionJoinFlag = false;
}
}
if (optionJoinFlag) {
// join the value stream to get value and set it to label
if (branchUnaryNode instanceof JoinZeroNode) {
((JoinZeroNode) branchUnaryNode).disableJoinZero();
}
TreeNode currentBranchTreeNode = TreeNodeUtils.buildSingleOutputNode(branchTreeNode, schema);
LogicalQueryPlan branchValuePlan = TreeNodeUtils.buildSubQueryPlan(currentBranchTreeNode, sourceVertex, contextManager);
TreeNode branchSourceNode = TreeNodeUtils.getSourceTreeNode(currentBranchTreeNode);
sourceVertex = branchSourceNode.getOutputVertex();
LogicalVertex branchValueVertex = branchValuePlan.getOutputVertex();
logicalSubQueryPlan.mergeLogicalQueryPlan(branchValuePlan);
String valueLabel = contextManager.getTreeNodeLabelManager().createSysLabelStart("val");
optionLabelId = contextManager.getTreeNodeLabelManager().getLabelIndex(valueLabel);
QueryFlowOuterClass.OperatorType joinOperatorType = CompilerUtils.parseJoinOperatorType(branchTreeNode);
ProcessorFunction joinFunction = new ProcessorFunction(joinOperatorType, Message.Value.newBuilder().setIntValue(optionLabelId));
LogicalBinaryVertex joinVertex = new LogicalBinaryVertex(contextManager.getVertexIdManager().getId(), joinFunction, false, sourceVertex, branchValueVertex);
logicalSubQueryPlan.addLogicalVertex(joinVertex);
logicalSubQueryPlan.addLogicalEdge(sourceVertex, joinVertex, new LogicalEdge());
logicalSubQueryPlan.addLogicalEdge(branchValueVertex, joinVertex, new LogicalEdge());
}
LogicalVertex outputVertex = logicalSubQueryPlan.getOutputVertex();
if (optionLabelId > 0) {
ProcessorFunction fillFunction = new ProcessorFunction(QueryFlowOuterClass.OperatorType.PROP_FILL, Message.Value.newBuilder().addIntValueList(optionLabelId));
LogicalVertex fillPropVertex = new LogicalUnaryVertex(contextManager.getVertexIdManager().getId(), fillFunction, true, outputVertex);
logicalSubQueryPlan.addLogicalVertex(fillPropVertex);
logicalSubQueryPlan.addLogicalEdge(outputVertex, fillPropVertex, outputVertex.isPropLocalFlag() ? new LogicalEdge(EdgeShuffleType.FORWARD) : new LogicalEdge());
outputVertex = fillPropVertex;
}
Map<Object, Message.LogicalCompare> pickCompareList = Maps.newHashMap();
List<Message.LogicalCompare> noneCompareList = Lists.newArrayList();
for (Map.Entry<Object, List<TreeNode>> pickEntry : optionPickList.entrySet()) {
Object pick = pickEntry.getKey();
Object pickValue = pick;
if (optionLabelId == TreeConstants.LABEL_INDEX) {
final String pickString = pick.toString();
pickValue = schema.getElement(pickString).getLabelId();
} else if (optionLabelId == TreeConstants.ID_INDEX) {
pickValue = Long.parseLong(pick.toString());
}
Message.VariantType variantType = CompilerUtils.parseVariantType(pickValue.getClass(), pickValue);
Message.Value.Builder valueBuilder = Message.Value.newBuilder().setValueType(variantType);
switch(variantType) {
case VT_INT:
{
if (pickValue instanceof Integer) {
valueBuilder.setIntValue((Integer) pickValue);
} else {
Class<?> pickTokenKeyClazz = BranchStep.class.getDeclaredClasses()[0];
Number number = ReflectionUtils.getFieldValue(pickTokenKeyClazz, pickValue, "number");
valueBuilder.setIntValue((int) number);
}
break;
}
case VT_LONG:
{
if (pickValue instanceof Long) {
valueBuilder.setLongValue((Long) pickValue);
} else {
Class<?> pickTokenKeyClazz = BranchStep.class.getDeclaredClasses()[0];
Number number = ReflectionUtils.getFieldValue(pickTokenKeyClazz, pickValue, "number");
valueBuilder.setLongValue((long) number);
}
break;
}
case VT_STRING:
{
valueBuilder.setStrValue((String) pickValue).build();
break;
}
default:
{
throw new UnsupportedOperationException(pickValue + " for branch option operator");
}
}
Message.Value value = valueBuilder.build();
pickCompareList.put(pick, Message.LogicalCompare.newBuilder().setPropId(optionLabelId).setCompare(Message.CompareType.EQ).setValue(value).setType(variantType).build());
noneCompareList.add(Message.LogicalCompare.newBuilder().setPropId(optionLabelId).setCompare(Message.CompareType.NEQ).setValue(value).setType(variantType).build());
}
List<LogicalVertex> unionVertexList = Lists.newArrayList();
for (Map.Entry<Object, List<TreeNode>> optionEntry : this.optionPickList.entrySet()) {
List<TreeNode> optionTreeNodeList = optionEntry.getValue();
Message.LogicalCompare logicalCompare = checkNotNull(pickCompareList.get(optionEntry.getKey()));
ProcessorFilterFunction filterFunction = new ProcessorFilterFunction(QueryFlowOuterClass.OperatorType.FILTER, createArgumentBuilder().setBoolValue(true));
filterFunction.getLogicalCompareList().add(logicalCompare);
LogicalVertex filterVertex = new LogicalUnaryVertex(contextManager.getVertexIdManager().getId(), filterFunction, outputVertex);
logicalSubQueryPlan.addLogicalVertex(filterVertex);
logicalSubQueryPlan.addLogicalEdge(outputVertex, filterVertex, LogicalEdge.forwardEdge());
for (TreeNode treeNode : optionTreeNodeList) {
LogicalQueryPlan subQueryPlan = TreeNodeUtils.buildQueryPlanWithSource(treeNode, contextManager.getTreeNodeLabelManager(), contextManager, contextManager.getVertexIdManager(), filterVertex);
unionVertexList.add(subQueryPlan.getOutputVertex());
logicalSubQueryPlan.mergeLogicalQueryPlan(subQueryPlan);
}
}
if (null != noneTreeNode) {
ProcessorFilterFunction filterFunction = new ProcessorFilterFunction(QueryFlowOuterClass.OperatorType.FILTER, createArgumentBuilder().setBoolValue(true));
filterFunction.getLogicalCompareList().addAll(noneCompareList);
LogicalVertex filterVertex = new LogicalUnaryVertex(contextManager.getVertexIdManager().getId(), filterFunction, outputVertex);
logicalSubQueryPlan.addLogicalVertex(filterVertex);
logicalSubQueryPlan.addLogicalEdge(outputVertex, filterVertex, LogicalEdge.forwardEdge());
LogicalQueryPlan subQueryPlan = TreeNodeUtils.buildQueryPlanWithSource(noneTreeNode, contextManager.getTreeNodeLabelManager(), contextManager, contextManager.getVertexIdManager(), filterVertex);
unionVertexList.add(subQueryPlan.getOutputVertex());
logicalSubQueryPlan.mergeLogicalQueryPlan(subQueryPlan);
}
if (null != anyTreeNode) {
LogicalQueryPlan subQueryPlan = TreeNodeUtils.buildQueryPlanWithSource(anyTreeNode, contextManager.getTreeNodeLabelManager(), contextManager, contextManager.getVertexIdManager(), outputVertex);
unionVertexList.add(subQueryPlan.getOutputVertex());
logicalSubQueryPlan.mergeLogicalQueryPlan(subQueryPlan);
}
LogicalVertex currentUnionVertex = unionVertexList.remove(0);
for (LogicalVertex logicalVertex : unionVertexList) {
LogicalVertex unionVertex = new LogicalBinaryVertex(contextManager.getVertexIdManager().getId(), new ProcessorFunction(QueryFlowOuterClass.OperatorType.UNION), false, currentUnionVertex, logicalVertex);
logicalSubQueryPlan.addLogicalVertex(unionVertex);
logicalSubQueryPlan.addLogicalEdge(currentUnionVertex, unionVertex, LogicalEdge.forwardEdge());
logicalSubQueryPlan.addLogicalEdge(logicalVertex, unionVertex, LogicalEdge.forwardEdge());
currentUnionVertex = unionVertex;
}
addUsedLabelAndRequirement(currentUnionVertex, contextManager.getTreeNodeLabelManager());
setFinishVertex(currentUnionVertex, contextManager.getTreeNodeLabelManager());
return logicalSubQueryPlan;
}
use of com.alibaba.maxgraph.compiler.logical.function.ProcessorFilterFunction in project GraphScope by alibaba.
the class HasTreeNode method buildLogicalQueryPlan.
@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
List<Message.LogicalCompare> logicalCompareList = Lists.newArrayList();
hasContainerList.forEach(v -> logicalCompareList.add(CompilerUtils.parseLogicalCompare(v, schema, contextManager.getTreeNodeLabelManager().getLabelIndexList(), getInputNode().getOutputValueType() instanceof VertexValueType)));
ProcessorFilterFunction filterFunction = new ProcessorFilterFunction(logicalCompareList.size() == 1 && logicalCompareList.get(0).getPropId() == 0 ? QueryFlowOuterClass.OperatorType.FILTER : QueryFlowOuterClass.OperatorType.HAS);
filterFunction.getLogicalCompareList().addAll(logicalCompareList);
boolean filterExchangeFlag = false;
for (HasContainer hasContainer : this.hasContainerList) {
if (SchemaUtils.checkPropExist(hasContainer.getKey(), schema)) {
filterExchangeFlag = true;
break;
}
}
if (filterExchangeFlag) {
return parseSingleUnaryVertex(contextManager.getVertexIdManager(), contextManager.getTreeNodeLabelManager(), filterFunction, contextManager);
} else {
return parseSingleUnaryVertex(contextManager.getVertexIdManager(), contextManager.getTreeNodeLabelManager(), filterFunction, contextManager, new LogicalEdge(EdgeShuffleType.FORWARD));
}
}
Aggregations