use of com.alibaba.maxgraph.compiler.tree.source.SourceDelegateNode in project GraphScope by alibaba.
the class TreeBuilder method visitFlatMapStep.
private TreeNode visitFlatMapStep(TraversalFlatMapStep step, TreeNode prev) {
Traversal.Admin<?, ?> flatMapTraversal = ReflectionUtils.getFieldValue(TraversalFlatMapStep.class, step, "flatMapTraversal");
boolean saveFlag = rootPathFlag;
rootPathFlag = false;
TreeNode flatMapNode = travelTraversalAdmin(flatMapTraversal, new SourceDelegateNode(prev, schema));
rootPathFlag = saveFlag;
return new TraversalFlatMapTreeNode(prev, schema, flatMapNode);
}
use of com.alibaba.maxgraph.compiler.tree.source.SourceDelegateNode in project GraphScope by alibaba.
the class TreeBuilder method visitRepeatStep.
private TreeNode visitRepeatStep(RepeatStep step, TreeNode prev) {
Traversal.Admin<?, ?> repeatTraversal = ReflectionUtils.getFieldValue(RepeatStep.class, step, "repeatTraversal");
Traversal.Admin<?, ?> untilTraversal = ReflectionUtils.getFieldValue(RepeatStep.class, step, "untilTraversal");
Traversal.Admin<?, ?> emitTraversal = ReflectionUtils.getFieldValue(RepeatStep.class, step, "emitTraversal");
String repeatTraversalString = repeatTraversal.toString();
String untilTraversalString = untilTraversal == null ? "" : untilTraversal.toString();
String emitTraversalString = emitTraversal == null ? "" : emitTraversal.toString();
if (StringUtils.contains(repeatTraversalString, "RepeatStep") || StringUtils.contains(untilTraversalString, "RepeatStep") || StringUtils.contains(emitTraversalString, "RepeatStep")) {
throw new UnsupportedOperationException("Not support nest repeat");
}
boolean untilFirst = ReflectionUtils.getFieldValue(RepeatStep.class, step, "untilFirst");
boolean emitFirst = ReflectionUtils.getFieldValue(RepeatStep.class, step, "emitFirst");
RepeatTreeNode repeatTreeNode = new RepeatTreeNode(prev, schema, this.queryConfig);
SourceDelegateNode sourceDelegateNode = new SourceDelegateNode(prev, schema);
sourceDelegateNode.enableRepeatFlag();
repeatTreeNode.setRepeatBodyTreeNode(travelTraversalAdmin(repeatTraversal, sourceDelegateNode));
boolean saveFlag = rootPathFlag;
this.rootPathFlag = false;
if (null != untilTraversal) {
if (untilTraversal instanceof LoopTraversal) {
repeatTreeNode.setMaxLoopTimes(LoopTraversal.class.cast(untilTraversal).getMaxLoops());
} else {
if (untilFirst) {
repeatTreeNode.setUntilFirstTreeNode(travelTraversalAdmin(untilTraversal, new SourceDelegateNode(prev, schema)));
}
Step finalEndStep = untilTraversal.getEndStep();
if (finalEndStep instanceof OrStep) {
OrStep orStep = OrStep.class.cast(finalEndStep);
List<Traversal.Admin<?, ?>> traversals = ReflectionUtils.getFieldValue(ConnectiveStep.class, orStep, "traversals");
checkArgument(traversals.size() == 2, "Only support two condition in until yet");
Traversal.Admin<?, ?> firstTraversal = traversals.get(0);
Traversal.Admin<?, ?> secondTraversal = traversals.get(1);
Traversal.Admin<?, ?> loopTraversal;
if (firstTraversal.getSteps().get(0) instanceof LoopsStep && !(secondTraversal.getSteps().get(0) instanceof LoopsStep)) {
repeatTreeNode.setUntilTreeNode(travelTraversalAdmin(secondTraversal, new SourceDelegateNode(prev, schema)));
loopTraversal = firstTraversal;
} else if (secondTraversal.getSteps().get(0) instanceof LoopsStep && !(firstTraversal.getSteps().get(0) instanceof LoopsStep)) {
repeatTreeNode.setUntilTreeNode(travelTraversalAdmin(firstTraversal, new SourceDelegateNode(prev, schema)));
loopTraversal = secondTraversal;
} else {
throw new UnsupportedOperationException("There's no looop condition in until");
}
List<Step> loopStepList = loopTraversal.getSteps();
checkArgument(loopStepList.size() == 2 && loopStepList.get(1) instanceof IsStep, "Only support loops().is(gt(loop count)) yet.");
IsStep isStep = (IsStep) loopStepList.get(1);
P predicate = isStep.getPredicate();
BiPredicate biPredicate = predicate.getBiPredicate();
checkArgument(biPredicate == Compare.gt || biPredicate == Compare.gte || biPredicate == Compare.eq, "Only support loops().is(gt/eq/gte(loop count)) yet.");
long loopCount = Long.parseLong(predicate.getValue().toString());
if (biPredicate == Compare.gt) {
loopCount += 1;
}
checkArgument(loopCount > 0, "Invalid loop count must > 0");
repeatTreeNode.setMaxLoopTimes(loopCount);
} else if (finalEndStep instanceof AndStep) {
throw new IllegalArgumentException("Not support and operator in until yet.");
} else {
repeatTreeNode.setUntilTreeNode(travelTraversalAdmin(untilTraversal, new SourceDelegateNode(prev, schema)));
}
}
}
if (null != emitTraversal) {
if (emitFirst) {
repeatTreeNode.setEmitFirstTreeNode(travelTraversalAdmin(emitTraversal, new SourceDelegateNode(prev, schema)));
}
repeatTreeNode.setEmitTreeNode(travelTraversalAdmin(emitTraversal, new SourceDelegateNode(prev, schema)));
}
rootPathFlag = saveFlag;
return repeatTreeNode;
}
use of com.alibaba.maxgraph.compiler.tree.source.SourceDelegateNode in project GraphScope by alibaba.
the class TreeBuilder method visitOrderGlobalStep.
private TreeNode visitOrderGlobalStep(OrderGlobalStep step, TreeNode prev) {
List<org.javatuples.Pair<Traversal.Admin<?, ?>, Comparator<?>>> comparatorList = step.getComparators();
List<Pair<TreeNode, Order>> treeNodeOrderList = Lists.newArrayList();
boolean saveFlag = rootPathFlag;
rootPathFlag = false;
SourceDelegateNode sourceDelegateNode = new SourceDelegateNode(prev, schema);
comparatorList.forEach(v -> treeNodeOrderList.add(Pair.of(travelTraversalAdmin(v.getValue0(), sourceDelegateNode), Order.class.cast(v.getValue1()))));
rootPathFlag = saveFlag;
return new OrderGlobalTreeNode(prev, schema, treeNodeOrderList);
}
use of com.alibaba.maxgraph.compiler.tree.source.SourceDelegateNode in project GraphScope by alibaba.
the class TreeBuilder method visitDedupGlobalStep.
private TreeNode visitDedupGlobalStep(DedupGlobalStep step, TreeNode prev) {
Set<String> dedupLabelList = step.getScopeKeys();
Traversal.Admin<?, ?> dedupTraversal = step.getLocalChildren().isEmpty() ? null : (Traversal.Admin<?, ?>) step.getLocalChildren().get(0);
DedupGlobalTreeNode dedupGlobalTreeNode = new DedupGlobalTreeNode(prev, schema, dedupLabelList);
if (null != dedupTraversal) {
boolean saveFlag = rootPathFlag;
rootPathFlag = false;
dedupGlobalTreeNode.setDedupTreeNode(travelTraversalAdmin(dedupTraversal, new SourceDelegateNode(dedupGlobalTreeNode, schema)));
rootPathFlag = saveFlag;
}
List<TreeNode> treeNodeList = Lists.reverse(TreeNodeUtils.buildTreeNodeListFromLeaf(dedupGlobalTreeNode));
TreeNode sourceTreeNode = treeNodeList.get(treeNodeList.size() - 1);
boolean subQueryNodeFlag = sourceTreeNode instanceof SourceDelegateNode;
for (TreeNode treeNode : treeNodeList) {
if (subQueryNodeFlag) {
treeNode.setSubqueryNode();
}
treeNode.enableDedupLocal();
if (treeNode instanceof SelectTreeNode || treeNode instanceof SelectOneTreeNode || (treeNode instanceof WherePredicateTreeNode && !treeNodeLabelManager.getLabelIndexList().containsKey(((WherePredicateTreeNode) treeNode).getStartKey()))) {
break;
}
}
return dedupGlobalTreeNode;
}
use of com.alibaba.maxgraph.compiler.tree.source.SourceDelegateNode in project GraphScope by alibaba.
the class TreeNodeUtils method buildSubQueryPlanWithKey.
/**
* @param treeNode The given tree node
* @param sourceVertex The source vertex
* @param treeNodeLabelManager The given label manager
* @param contextManager The context manager
* @param vertexIdManager The vertex id manager
* @return The sub query with enter key vertex
*/
public static LogicalSubQueryPlan buildSubQueryPlanWithKey(TreeNode treeNode, LogicalVertex sourceVertex, TreeNodeLabelManager treeNodeLabelManager, ContextManager contextManager, VertexIdManager vertexIdManager) {
List<TreeNode> treeNodeList = buildTreeNodeListFromLeaf(treeNode);
LogicalSubQueryPlan logicalSubQueryPlan = new LogicalSubQueryPlan(contextManager);
LogicalVertex currentSourceVertex = sourceVertex;
for (TreeNode currentNode : treeNodeList) {
if (currentNode instanceof AbstractUseKeyNode) {
((AbstractUseKeyNode) currentNode).enableUseKeyFlag(currentSourceVertex);
}
if (currentNode instanceof SourceDelegateNode) {
LogicalVertex enterKeyVertex = new LogicalUnaryVertex(vertexIdManager.getId(), new ProcessorFunction(QueryFlowOuterClass.OperatorType.ENTER_KEY, Message.Value.newBuilder().setPayload(QueryFlowOuterClass.EnterKeyArgumentProto.newBuilder().setEnterKeyType(QueryFlowOuterClass.EnterKeyTypeProto.KEY_SELF).setUniqFlag(true).build().toByteString())), false, currentSourceVertex);
currentNode.setFinishVertex(enterKeyVertex, treeNodeLabelManager);
logicalSubQueryPlan.addLogicalVertex(currentSourceVertex);
logicalSubQueryPlan.addLogicalVertex(enterKeyVertex);
logicalSubQueryPlan.addLogicalEdge(currentSourceVertex, enterKeyVertex, new LogicalEdge());
currentSourceVertex = enterKeyVertex;
}
logicalSubQueryPlan.mergeLogicalQueryPlan(currentNode.buildLogicalQueryPlan(contextManager));
}
return logicalSubQueryPlan;
}
Aggregations