use of com.alibaba.maxgraph.compiler.optimizer.ContextManager in project GraphScope by alibaba.
the class OrderGlobalTreeNode method buildLogicalQueryPlan.
@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
LogicalSubQueryPlan logicalSubQueryPlan = new LogicalSubQueryPlan(contextManager);
LogicalVertex delegateSourceVertex = getInputNode().getOutputVertex();
logicalSubQueryPlan.addLogicalVertex(delegateSourceVertex);
TreeNodeLabelManager labelManager = contextManager.getTreeNodeLabelManager();
VertexIdManager vertexIdManager = contextManager.getVertexIdManager();
Message.OrderComparatorList.Builder comparatorList = Message.OrderComparatorList.newBuilder();
Set<String> propFillList = Sets.newHashSet();
Set<Integer> usedLabelIdList = Sets.newHashSet();
LogicalVertex lastJoinVertex = null;
LogicalVertex inputVertex = delegateSourceVertex;
for (int i = 0; i < treeNodeOrderList.size(); i++) {
Pair<TreeNode, Order> orderPair = treeNodeOrderList.get(i);
if (orderPair.getLeft() instanceof SourceDelegateNode) {
comparatorList.addOrderComparator(Message.OrderComparator.newBuilder().setPropId(0).setOrderType(Message.OrderType.valueOf(StringUtils.upperCase(orderPair.getRight().name()))).build());
} else {
UnaryTreeNode unaryTreeNode = UnaryTreeNode.class.cast(orderPair.getLeft());
if (unaryTreeNode.getInputNode() instanceof SourceDelegateNode && ((unaryTreeNode instanceof SelectOneTreeNode && SelectOneTreeNode.class.cast(unaryTreeNode).getTraversalTreeNode() == null) || (unaryTreeNode instanceof ElementValueTreeNode && ElementValueTreeNode.class.cast(unaryTreeNode).getByPassTraversal() == null) || unaryTreeNode instanceof TokenTreeNode || (unaryTreeNode instanceof TraversalMapTreeNode && ((TraversalMapTreeNode) unaryTreeNode).getTraversalNode() instanceof ColumnTreeNode && ((ColumnTreeNode) ((TraversalMapTreeNode) unaryTreeNode).getTraversalNode()).getInputNode() instanceof SourceDelegateNode))) {
if (unaryTreeNode instanceof SelectOneTreeNode) {
int labelId = labelManager.getLabelIndex(SelectOneTreeNode.class.cast(unaryTreeNode).getSelectLabel());
comparatorList.addOrderComparator(Message.OrderComparator.newBuilder().setPropId(labelId).setOrderType(Message.OrderType.valueOf(StringUtils.upperCase(orderPair.getRight().name()))).build());
usedLabelIdList.add(labelId);
} else if (unaryTreeNode instanceof TokenTreeNode) {
int labelId = labelManager.getLabelIndex(TokenTreeNode.class.cast(unaryTreeNode).getToken().getAccessor());
comparatorList.addOrderComparator(Message.OrderComparator.newBuilder().setPropId(labelId).setOrderType(Message.OrderType.valueOf(StringUtils.upperCase(orderPair.getRight().name()))).build());
} else if (unaryTreeNode instanceof ElementValueTreeNode) {
String propKey = ElementValueTreeNode.class.cast(unaryTreeNode).getPropKeyList().iterator().next();
propFillList.add(propKey);
comparatorList.addOrderComparator(Message.OrderComparator.newBuilder().setPropId(CompilerUtils.getPropertyId(schema, propKey)).setOrderType(Message.OrderType.valueOf(StringUtils.upperCase(orderPair.getRight().name()))).build());
} else {
TraversalMapTreeNode traversalMapTreeNode = TraversalMapTreeNode.class.cast(unaryTreeNode);
ColumnTreeNode columnTreeNode = (ColumnTreeNode) traversalMapTreeNode.getTraversalNode();
comparatorList.addOrderComparator(Message.OrderComparator.newBuilder().setPropId(labelManager.getLabelIndex(columnTreeNode.getColumn().name())).setOrderType(Message.OrderType.valueOf(StringUtils.upperCase(orderPair.getRight().name()))).build());
}
} else {
TreeNode compareTreeNode = TreeNodeUtils.buildSingleOutputNode(orderPair.getLeft(), schema);
Pair<LogicalQueryPlan, Integer> planLabelPair = TreeNodeUtils.buildSubQueryWithLabel(compareTreeNode, inputVertex, contextManager);
inputVertex = planLabelPair.getLeft().getOutputVertex();
logicalSubQueryPlan.mergeLogicalQueryPlan(planLabelPair.getLeft());
comparatorList.addOrderComparator(Message.OrderComparator.newBuilder().setPropId(planLabelPair.getRight()).setOrderType(Message.OrderType.valueOf(StringUtils.upperCase(orderPair.getRight().name()))).build());
usedLabelIdList.add(planLabelPair.getRight());
}
}
}
if (!propFillList.isEmpty() && getInputNode().getOutputValueType() instanceof VertexValueType) {
LogicalVertex propFillInputVertex = logicalSubQueryPlan.getOutputVertex();
ProcessorFunction propFillFunction = new ProcessorFunction(QueryFlowOuterClass.OperatorType.PROP_FILL, Message.Value.newBuilder().addAllIntValueList(propFillList.stream().map(v -> CompilerUtils.getPropertyId(schema, v)).collect(Collectors.toSet())));
LogicalVertex propFillVertex = new LogicalUnaryVertex(vertexIdManager.getId(), propFillFunction, true, propFillInputVertex);
LogicalEdge logicalEdge;
if (propFillInputVertex.isPropLocalFlag()) {
logicalEdge = new LogicalEdge(EdgeShuffleType.FORWARD);
} else {
logicalEdge = new LogicalEdge(EdgeShuffleType.SHUFFLE_BY_KEY);
}
logicalSubQueryPlan.addLogicalVertex(propFillVertex);
logicalSubQueryPlan.addLogicalEdge(propFillInputVertex, propFillVertex, logicalEdge);
}
Message.Value.Builder argumentBuilder = Message.Value.newBuilder().setPayload(comparatorList.build().toByteString());
if (orderFlag) {
argumentBuilder.setBoolValue(true).setIntValue(labelManager.getLabelIndex(orderFlagLabel));
}
argumentBuilder.setBoolFlag(partitionIdFlag).setLongValue(SHUFFLE_THRESHOLD).setOrderFlag(orderKeyFlag);
ProcessorFunction orderFunction = new ProcessorFunction(QueryFlowOuterClass.OperatorType.ORDER, argumentBuilder, rangeLimit);
orderFunction.getUsedLabelList().addAll(usedLabelIdList);
LogicalVertex outputVertex = logicalSubQueryPlan.getOutputVertex();
LogicalVertex orderVertex = new LogicalUnaryVertex(vertexIdManager.getId(), orderFunction, false, outputVertex);
logicalSubQueryPlan.addLogicalVertex(orderVertex);
logicalSubQueryPlan.addLogicalEdge(outputVertex, orderVertex, new LogicalEdge());
addUsedLabelAndRequirement(orderVertex, labelManager);
setFinishVertex(orderVertex, labelManager);
return logicalSubQueryPlan;
}
use of com.alibaba.maxgraph.compiler.optimizer.ContextManager in project GraphScope by alibaba.
the class LogicalPlanBuilder method build.
/**
* Build logical plan from tree manager
*
* @param treeManager The given tree manager
* @return The result logical plan
*/
public LogicalQueryPlan build(TreeManager treeManager) {
CostModelManager costModelManager = treeManager.optimizeCostModel();
TreeNodeLabelManager labelManager = treeManager.getLabelManager();
VertexIdManager vertexIdManager = VertexIdManager.createVertexIdManager();
ContextManager contextManager = new ContextManager(costModelManager, treeManager.getQueryConfig(), vertexIdManager, labelManager);
TreeNode leafNode = treeManager.getTreeLeaf();
return buildPlan(leafNode, contextManager, treeManager.getSchema());
}
use of com.alibaba.maxgraph.compiler.optimizer.ContextManager in project GraphScope by alibaba.
the class LogicalPlanBuilder method buildLabelValuePlan.
private void buildLabelValuePlan(LogicalQueryPlan queryPlan, ContextManager contextManager, GraphSchema schema, LogicalVertex outputVertex, TreeNodeLabelManager treeNodeLabelManager, VertexIdManager vertexIdManager, CostMappingManager costMappingManager, String field, TreeNode currNode) {
// Create field value here
String parentField = costMappingManager.getValueParent(field);
if (!StringUtils.isEmpty(parentField)) {
BaseTreeNode nextNode = (BaseTreeNode) currNode.getOutputNode();
nextNode.removeBeforeLabel(parentField);
// If the field is value field, process it and remove the label requirement in vertex
int labelIndex = treeNodeLabelManager.getLabelIndex(parentField);
TreeNode fieldValueTreeNode = costMappingManager.getComputeTreeByValue(parentField);
if (!(fieldValueTreeNode instanceof SourceTreeNode)) {
for (QueryFlowOuterClass.RequirementValue.Builder reqValue : outputVertex.getAfterRequirementList()) {
if (reqValue.getReqType() == QueryFlowOuterClass.RequirementType.LABEL_START) {
List<Integer> labelIndexList = reqValue.getReqArgumentBuilder().getIntValueListList().stream().filter(v -> v != labelIndex).collect(Collectors.toList());
reqValue.getReqArgumentBuilder().clearIntValueList().addAllIntValueList(labelIndexList);
}
}
TreeNode currentFilterTreeNode = TreeNodeUtils.buildSingleOutputNode(fieldValueTreeNode, schema);
// build filter plan, and use join direct filter vertex to filter left stream
LogicalSubQueryPlan fieldValuePlan = TreeNodeUtils.buildSubQueryPlan(currentFilterTreeNode, outputVertex, contextManager);
List<LogicalVertex> fieldValueVertexList = fieldValuePlan.getLogicalVertexList();
if (fieldValueVertexList.size() == 2) {
LogicalVertex fieldValueVertex = fieldValueVertexList.get(1);
ProcessorLabelValueFunction labelValueFunction = new ProcessorLabelValueFunction(labelIndex, fieldValueVertex);
LogicalVertex labelValueVertex = new LogicalUnaryVertex(vertexIdManager.getId(), labelValueFunction, outputVertex);
queryPlan.addLogicalVertex(labelValueVertex);
queryPlan.addLogicalEdge(outputVertex, labelValueVertex, LogicalEdge.shuffleByKey(labelIndex));
} else {
LogicalVertex fieldValueVertex = fieldValuePlan.getOutputVertex();
fieldValueVertex.getAfterRequirementList().add(QueryFlowOuterClass.RequirementValue.newBuilder().setReqType(QueryFlowOuterClass.RequirementType.LABEL_START).setReqArgument(Message.Value.newBuilder().addIntValueList(labelIndex)));
queryPlan.mergeLogicalQueryPlan(fieldValuePlan);
LogicalVertex outputKeyVertex = new LogicalUnaryVertex(vertexIdManager.getId(), new ProcessorFunction(QueryFlowOuterClass.OperatorType.KEY_MESSAGE), fieldValueVertex);
queryPlan.addLogicalVertex(outputKeyVertex);
queryPlan.addLogicalEdge(fieldValueVertex, outputKeyVertex, LogicalEdge.forwardEdge());
}
}
}
}
Aggregations