use of com.alibaba.maxgraph.compiler.tree.addition.PropertyNode in project GraphScope by alibaba.
the class CostEstimate method estimateNetworkCost.
public double estimateNetworkCost(TreeNode node) {
if (node instanceof FoldTreeNode) {
return NET_DEFAULT_COST * NET_DEFAULT_SCALE_RATIO * NET_FOLD_RATIO;
} else if (node.getNodeType() == NodeType.AGGREGATE) {
return NET_DEFAULT_COST * NET_DEFAULT_SCALE_RATIO;
} else if (node instanceof PropertyNode) {
PropertyNode propertyNode = (PropertyNode) node;
Set<String> propNameList = propertyNode.getPropKeyList();
if (propNameList.size() <= 0) {
return NET_DEFAULT_COST * NET_DEFAULT_SCALE_RATIO * PROP_DEFAULT_COUNT;
} else {
return NET_DEFAULT_COST * NET_DEFAULT_SCALE_RATIO * propNameList.size();
}
} else if (node.getOutputValueType() instanceof VertexValueType) {
return NET_DEFAULT_COST;
} else {
return NET_DEFAULT_COST * NET_DEFAULT_SCALE_RATIO;
}
}
use of com.alibaba.maxgraph.compiler.tree.addition.PropertyNode 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.tree.addition.PropertyNode 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.tree.addition.PropertyNode in project GraphScope by alibaba.
the class TreeBuilder method visitVertexByModulatingStep.
private TreeNode visitVertexByModulatingStep(VertexByModulatingStep step, TreeNode prev) {
checkNotNull(prev);
Direction direction = step.getDirection();
String[] edgeLabels = step.getEdgeLabels();
SampleGlobalStep sampleGlobalStep = step.getSampleGlobalStep();
SampleNode sampleNode;
if (step.returnsVertex()) {
sampleNode = new VertexTreeNode(prev, direction, edgeLabels, schema);
} else {
sampleNode = new EdgeTreeNode(prev, direction, edgeLabels, schema);
}
if (null != sampleGlobalStep) {
int amountToSample = ReflectionUtils.getFieldValue(SampleGlobalStep.class, sampleGlobalStep, "amountToSample");
Traversal.Admin<?, ?> probabilityTraversal = ReflectionUtils.getFieldValue(SampleGlobalStep.class, sampleGlobalStep, "probabilityTraversal");
boolean saveFlag = rootPathFlag;
rootPathFlag = false;
String probabilityProperty = null;
TreeNode probabilityTreeNode = travelTraversalAdmin(probabilityTraversal, new SourceDelegateNode((TreeNode) sampleNode, schema));
if (probabilityTreeNode instanceof SourceTreeNode) {
probabilityProperty = "";
} else if ((UnaryTreeNode.class.cast(probabilityTreeNode).getInputNode() instanceof SourceTreeNode && (probabilityTreeNode instanceof PropertyNode))) {
probabilityProperty = PropertyNode.class.cast(probabilityTreeNode).getPropKeyList().iterator().next();
} else {
throw new IllegalArgumentException("Only support sample by property here.");
}
rootPathFlag = saveFlag;
sampleNode.setSample(amountToSample, probabilityProperty);
}
return (TreeNode) sampleNode;
}
use of com.alibaba.maxgraph.compiler.tree.addition.PropertyNode in project GraphScope by alibaba.
the class TreeBuilder method visitTraversalFilterStep.
private TreeNode visitTraversalFilterStep(TraversalFilterStep step, TreeNode prev) {
Traversal.Admin<?, ?> filterTraversal = (Traversal.Admin<?, ?>) step.getLocalChildren().get(0);
TreeNode filterTreeNode;
boolean saveFlag = rootPathFlag;
rootPathFlag = false;
filterTreeNode = travelTraversalAdmin(filterTraversal, new SourceDelegateNode(prev, schema));
rootPathFlag = saveFlag;
if (!(filterTreeNode instanceof RangeGlobalTreeNode)) {
TreeNode currentTreeNode = filterTreeNode;
boolean addRangeFlag = false;
while (currentTreeNode instanceof UnaryTreeNode) {
if (currentTreeNode.getNodeType() == NodeType.FILTER || currentTreeNode.getNodeType() == NodeType.MAP) {
currentTreeNode = UnaryTreeNode.class.cast(currentTreeNode).getInputNode();
continue;
}
if (currentTreeNode.getNodeType() == NodeType.FLATMAP) {
addRangeFlag = true;
}
break;
}
if (addRangeFlag) {
filterTreeNode = new RangeGlobalTreeNode(filterTreeNode, schema, 0, 1);
}
}
if (filterTreeNode instanceof SourceTreeNode) {
throw new IllegalArgumentException();
} else if (UnaryTreeNode.class.cast(filterTreeNode).getInputNode() instanceof SourceTreeNode && (filterTreeNode instanceof SelectOneTreeNode || filterTreeNode instanceof PropertyNode)) {
String key;
if (filterTreeNode instanceof SelectOneTreeNode) {
key = SelectOneTreeNode.class.cast(filterTreeNode).getSelectLabel();
} else {
key = PropertyNode.class.cast(filterTreeNode).getPropKeyList().iterator().next();
}
HasContainer hasContainer = new HasContainer(key, null);
if (prev instanceof SourceTreeNode && !(prev instanceof SourceDelegateNode)) {
SourceTreeNode.class.cast(prev).addHasContainer(hasContainer);
return prev;
} else {
return new HasTreeNode(prev, Lists.newArrayList(hasContainer), schema);
}
} else {
TraversalFilterTreeNode traversalFilterTreeNode = new TraversalFilterTreeNode(prev, schema);
traversalFilterTreeNode.setFilterTreeNode(filterTreeNode);
return traversalFilterTreeNode;
}
}
Aggregations