use of com.alibaba.maxgraph.compiler.logical.LogicalSubQueryPlan 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.LogicalSubQueryPlan in project GraphScope by alibaba.
the class OutputVineyardTreeNode method buildLogicalQueryPlan.
@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
TreeNode inputNode = this.getInputNode();
LogicalVertex inputVertex = inputNode.getOutputVertex();
LogicalSubQueryPlan logicalSubQueryPlan = new LogicalSubQueryPlan(contextManager);
logicalSubQueryPlan.addLogicalVertex(inputVertex);
LogicalVertex vineyardVertex = new LogicalUnaryVertex(contextManager.getVertexIdManager().getId(), new ProcessorFunction(QueryFlowOuterClass.OperatorType.OUTPUT_VINEYARD_VERTEX, Message.Value.newBuilder().setStrValue(this.graphName)), inputVertex);
logicalSubQueryPlan.addLogicalVertex(vineyardVertex);
logicalSubQueryPlan.addLogicalEdge(inputVertex, vineyardVertex, LogicalEdge.shuffleByKey(0));
LogicalVertex vineyardEdge = new LogicalUnaryVertex(contextManager.getVertexIdManager().getId(), new ProcessorFunction(QueryFlowOuterClass.OperatorType.OUTPUT_VINEYARD_EDGE, Message.Value.newBuilder().setStrValue(this.graphName)), inputVertex);
logicalSubQueryPlan.addLogicalVertex(vineyardEdge);
logicalSubQueryPlan.addLogicalEdge(vineyardVertex, vineyardEdge, LogicalEdge.forwardEdge());
LogicalVertex sumVertex = new LogicalUnaryVertex(contextManager.getVertexIdManager().getId(), new ProcessorFunction(QueryFlowOuterClass.OperatorType.SUM, Message.Value.newBuilder().setValueType(Message.VariantType.VT_LONG)), vineyardEdge);
logicalSubQueryPlan.addLogicalVertex(sumVertex);
logicalSubQueryPlan.addLogicalEdge(vineyardEdge, sumVertex, LogicalEdge.shuffleConstant());
setFinishVertex(sumVertex, contextManager.getTreeNodeLabelManager());
addUsedLabelAndRequirement(sumVertex, contextManager.getTreeNodeLabelManager());
return logicalSubQueryPlan;
}
use of com.alibaba.maxgraph.compiler.logical.LogicalSubQueryPlan in project GraphScope by alibaba.
the class GroupCountTreeNode method buildLogicalQueryPlan.
@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
LogicalSubQueryPlan logicalSubQueryPlan = new LogicalSubQueryPlan(contextManager);
LogicalVertex sourceVertex = getInputNode().getOutputVertex();
logicalSubQueryPlan.addLogicalVertex(sourceVertex);
LogicalVertex outputVertex;
if (null == keyTreeNode || keyTreeNode instanceof SourceDelegateNode) {
ProcessorFunction processorFunction = new ProcessorFunction(QueryFlowOuterClass.OperatorType.GROUP_COUNT);
outputVertex = new LogicalUnaryVertex(contextManager.getVertexIdManager().getId(), processorFunction, false, sourceVertex);
logicalSubQueryPlan.addLogicalVertex(outputVertex);
logicalSubQueryPlan.addLogicalEdge(sourceVertex, outputVertex, new LogicalEdge());
} else {
TreeNode currentKeyNode = TreeNodeUtils.buildSingleOutputNode(keyTreeNode, schema);
if (currentKeyNode instanceof JoinZeroNode) {
((JoinZeroNode) currentKeyNode).disableJoinZero();
}
LogicalSubQueryPlan keyValuePlan = TreeNodeUtils.buildSubQueryPlan(currentKeyNode, sourceVertex, contextManager);
LogicalVertex groupValueVertex = keyValuePlan.getOutputVertex();
LogicalVertex enterKeyVertex = TreeNodeUtils.getSourceTreeNode(currentKeyNode).getOutputVertex();
logicalSubQueryPlan.mergeLogicalQueryPlan(keyValuePlan);
if (TreeNodeUtils.checkJoinSourceFlag(currentKeyNode)) {
String valueLabel = contextManager.getTreeNodeLabelManager().createSysLabelStart("val");
getUsedLabelList().add(valueLabel);
int valueLabelId = contextManager.getTreeNodeLabelManager().getLabelIndex(valueLabel);
LogicalBinaryVertex logicalBinaryVertex = new LogicalBinaryVertex(contextManager.getVertexIdManager().getId(), new ProcessorFunction(CompilerUtils.parseJoinOperatorType(currentKeyNode), Message.Value.newBuilder().setIntValue(valueLabelId)), false, enterKeyVertex, groupValueVertex);
logicalSubQueryPlan.addLogicalVertex(logicalBinaryVertex);
logicalSubQueryPlan.addLogicalEdge(enterKeyVertex, logicalBinaryVertex, new LogicalEdge());
logicalSubQueryPlan.addLogicalEdge(groupValueVertex, logicalBinaryVertex, new LogicalEdge());
ProcessorFunction selectValueFunction = TreeNodeUtils.createSelectOneFunction(valueLabel, Pop.first, contextManager.getTreeNodeLabelManager().getLabelIndexList());
LogicalVertex selectLabelVertex = new LogicalUnaryVertex(contextManager.getVertexIdManager().getId(), selectValueFunction, false, logicalBinaryVertex);
logicalSubQueryPlan.addLogicalVertex(selectLabelVertex);
logicalSubQueryPlan.addLogicalEdge(logicalBinaryVertex, selectLabelVertex, LogicalEdge.forwardEdge());
}
groupValueVertex = logicalSubQueryPlan.getOutputVertex();
ProcessorFunction processorFunction = new ProcessorFunction(QueryFlowOuterClass.OperatorType.GROUP_COUNT, Message.Value.newBuilder());
outputVertex = new LogicalUnaryVertex(contextManager.getVertexIdManager().getId(), processorFunction, false, groupValueVertex);
logicalSubQueryPlan.addLogicalVertex(outputVertex);
logicalSubQueryPlan.addLogicalEdge(groupValueVertex, outputVertex, new LogicalEdge());
}
ProcessorFunction foldMapFunction = new ProcessorFunction(QueryFlowOuterClass.OperatorType.FOLDMAP);
LogicalVertex foldMapVertex = new LogicalUnaryVertex(contextManager.getVertexIdManager().getId(), foldMapFunction, false, outputVertex);
logicalSubQueryPlan.addLogicalVertex(foldMapVertex);
logicalSubQueryPlan.addLogicalEdge(outputVertex, foldMapVertex, new LogicalEdge());
addUsedLabelAndRequirement(foldMapVertex, contextManager.getTreeNodeLabelManager());
setFinishVertex(foldMapVertex, contextManager.getTreeNodeLabelManager());
return logicalSubQueryPlan;
}
use of com.alibaba.maxgraph.compiler.logical.LogicalSubQueryPlan in project GraphScope by alibaba.
the class UnaryTreeNode method parseSingleUnaryVertex.
protected LogicalSubQueryPlan parseSingleUnaryVertex(VertexIdManager vertexIdManager, TreeNodeLabelManager labelManager, ProcessorFunction processorFunction, ContextManager contextManager, LogicalEdge logicalEdge, boolean outputFlag) {
LogicalSubQueryPlan logicalSubQueryPlan = new LogicalSubQueryPlan(contextManager);
LogicalVertex sourceVertex = getInputNode().getOutputVertex();
logicalSubQueryPlan.addLogicalVertex(sourceVertex);
LogicalUnaryVertex logicalUnaryVertex = new LogicalUnaryVertex(vertexIdManager.getId(), processorFunction, false, sourceVertex);
logicalUnaryVertex.setEarlyStopFlag(super.earlyStopArgument);
logicalSubQueryPlan.addLogicalVertex(logicalUnaryVertex);
logicalSubQueryPlan.addLogicalEdge(sourceVertex, logicalUnaryVertex, logicalEdge);
if (outputFlag) {
setFinishVertex(logicalUnaryVertex, labelManager);
addUsedLabelAndRequirement(logicalUnaryVertex, labelManager);
}
return logicalSubQueryPlan;
}
use of com.alibaba.maxgraph.compiler.logical.LogicalSubQueryPlan in project GraphScope by alibaba.
the class TreeNodeUtils method buildQueryPlanWithSource.
/**
* Build query plan from given leaf tree node
*
* @param treeNode The given leaf tree node
* @param treeNodeLabelManager The tree node label manager
* @param contextManager The context manager
* @param vertexIdManager The vertex id manager
* @return The query plan of sub query
*/
public static LogicalSubQueryPlan buildQueryPlanWithSource(TreeNode treeNode, TreeNodeLabelManager treeNodeLabelManager, ContextManager contextManager, VertexIdManager vertexIdManager, LogicalVertex sourceVertex) {
List<TreeNode> treeNodeList = buildTreeNodeListFromLeaf(treeNode);
LogicalSubQueryPlan logicalSubQueryPlan = new LogicalSubQueryPlan(contextManager);
for (TreeNode currentNode : treeNodeList) {
if (currentNode instanceof SourceDelegateNode) {
currentNode.setFinishVertex(sourceVertex, treeNodeLabelManager);
}
logicalSubQueryPlan.mergeLogicalQueryPlan(currentNode.buildLogicalQueryPlan(contextManager));
}
return logicalSubQueryPlan;
}
Aggregations