Search in sources :

Example 1 with SourceVertexTreeNode

use of com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode in project GraphScope by alibaba.

the class NodeLabelList method buildNodeLabel.

/**
 * Build label list of current node from parent node, tree node and schema
 *
 * @param parentNodeLabel The given parent node
 * @param treeNode        The given tree node
 * @param graphSchema     The given graph schema
 * @return The result node label list
 */
public static NodeLabelList buildNodeLabel(NodeLabelList parentNodeLabel, TreeNode treeNode, GraphSchema graphSchema) {
    NodeLabelList nodeLabelList = new NodeLabelList();
    if (null == parentNodeLabel) {
        BaseTreeNode baseTreeNode = (BaseTreeNode) treeNode;
        final Set<String> labelList = Sets.newHashSet();
        baseTreeNode.getHasContainerList().forEach(v -> {
            if (StringUtils.equals(v.getKey(), T.label.getAccessor())) {
                if (v.getBiPredicate() instanceof Compare && v.getBiPredicate() == Compare.eq) {
                    labelList.add(v.getValue().toString());
                } else if (v.getBiPredicate() instanceof Contains && v.getBiPredicate() == Contains.within) {
                    List<String> labelValueList = (List<String>) v.getValue();
                    labelList.addAll(labelValueList);
                } else {
                    throw new IllegalArgumentException("Not support label compare " + v.toString());
                }
            }
        });
        if (treeNode instanceof SourceVertexTreeNode) {
            List<String> vertexLabelList = graphSchema.getVertexList().stream().map(GraphElement::getLabel).collect(Collectors.toList());
            Set<String> resultLabelList;
            if (labelList.isEmpty()) {
                resultLabelList = Sets.newHashSet(vertexLabelList);
            } else {
                resultLabelList = labelList.stream().filter(vertexLabelList::contains).collect(Collectors.toSet());
            }
            nodeLabelList.addVertexLabel(resultLabelList);
        } else if (treeNode instanceof SourceEdgeTreeNode) {
            List<String> edgeLabelList = graphSchema.getEdgeList().stream().map(GraphElement::getLabel).collect(Collectors.toList());
            Set<String> resultLabelList;
            if (labelList.isEmpty()) {
                resultLabelList = Sets.newHashSet(edgeLabelList);
            } else {
                resultLabelList = labelList.stream().filter(edgeLabelList::contains).collect(Collectors.toSet());
            }
            nodeLabelList.addEdgeLabel(resultLabelList);
        } else {
            nodeLabelList.enableUnknown();
        }
    } else {
        Set<String> parentVertexLabelList = Sets.newHashSet();
        Set<String> parentEdgeLabelList = Sets.newHashSet();
        if (parentNodeLabel.isUnknownFlag()) {
            parentVertexLabelList.addAll(graphSchema.getVertexList().stream().map(GraphElement::getLabel).collect(Collectors.toList()));
            parentEdgeLabelList.addAll(graphSchema.getEdgeList().stream().map(GraphElement::getLabel).collect(Collectors.toList()));
        } else {
            parentVertexLabelList.addAll(parentNodeLabel.getVertexLabelList());
            parentEdgeLabelList.addAll(parentNodeLabel.getEdgeLabelList());
        }
        if (treeNode instanceof VertexTreeNode) {
            VertexTreeNode vertexTreeNode = (VertexTreeNode) treeNode;
            Direction direction = vertexTreeNode.getDirection();
            nodeLabelList.addVertexLabel(computeVertexLabelList(parentVertexLabelList, direction, vertexTreeNode.getEdgeLabels(), graphSchema));
        } else if (treeNode instanceof EdgeTreeNode) {
            EdgeTreeNode edgeTreeNode = (EdgeTreeNode) treeNode;
            Direction direction = edgeTreeNode.getDirection();
            nodeLabelList.addEdgeLabel(computeEdgeLabelList(parentVertexLabelList, direction, edgeTreeNode.getEdgeLabels(), graphSchema));
        } else if (treeNode instanceof EdgeVertexTreeNode) {
            EdgeVertexTreeNode edgeVertexTreeNode = (EdgeVertexTreeNode) treeNode;
            Map<String, Pair<Set<String>, Set<String>>> edgeSourceTargetPairList = getEdgeSourceTargetPairList(parentEdgeLabelList.toArray(new String[0]), graphSchema);
            Direction direction = edgeVertexTreeNode.getDirection();
            edgeSourceTargetPairList.forEach((key, value) -> {
                switch(direction) {
                    case OUT:
                        {
                            nodeLabelList.addVertexLabel(value.getLeft());
                            break;
                        }
                    case IN:
                        {
                            nodeLabelList.addVertexLabel(value.getRight());
                            break;
                        }
                    case BOTH:
                        {
                            nodeLabelList.addVertexLabel(value.getLeft());
                            nodeLabelList.addVertexLabel(value.getRight());
                            break;
                        }
                }
            });
        } else if (treeNode instanceof EdgeOtherVertexTreeNode) {
            Map<String, Pair<Set<String>, Set<String>>> edgeSourceTargetPairList = getEdgeSourceTargetPairList(parentEdgeLabelList.toArray(new String[0]), graphSchema);
            edgeSourceTargetPairList.forEach((key, value) -> {
                nodeLabelList.addVertexLabel(value.getLeft());
                nodeLabelList.addVertexLabel(value.getRight());
            });
        } else {
            nodeLabelList.enableUnknown();
        }
    }
    return nodeLabelList;
}
Also used : BaseTreeNode(com.alibaba.maxgraph.compiler.tree.BaseTreeNode) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) VertexTreeNode(com.alibaba.maxgraph.compiler.tree.VertexTreeNode) Contains(org.apache.tinkerpop.gremlin.process.traversal.Contains) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) Lists(com.google.common.collect.Lists) Pair(org.apache.commons.lang3.tuple.Pair) EdgeRelation(com.alibaba.maxgraph.compiler.api.schema.EdgeRelation) Map(java.util.Map) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) EdgeVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode) SourceEdgeTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode) SourceVertexTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode) MoreObjects(com.google.common.base.MoreObjects) Set(java.util.Set) EdgeTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeTreeNode) T(org.apache.tinkerpop.gremlin.structure.T) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Compare(org.apache.tinkerpop.gremlin.process.traversal.Compare) Direction(org.apache.tinkerpop.gremlin.structure.Direction) List(java.util.List) EdgeOtherVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) Set(java.util.Set) EdgeVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode) SourceVertexTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode) SourceEdgeTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode) EdgeTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeTreeNode) Direction(org.apache.tinkerpop.gremlin.structure.Direction) VertexTreeNode(com.alibaba.maxgraph.compiler.tree.VertexTreeNode) EdgeVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode) SourceVertexTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode) EdgeOtherVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode) EdgeOtherVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode) Contains(org.apache.tinkerpop.gremlin.process.traversal.Contains) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) BaseTreeNode(com.alibaba.maxgraph.compiler.tree.BaseTreeNode) Compare(org.apache.tinkerpop.gremlin.process.traversal.Compare) List(java.util.List) SourceEdgeTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode) Map(java.util.Map) Pair(org.apache.commons.lang3.tuple.Pair)

Example 2 with SourceVertexTreeNode

use of com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode in project GraphScope by alibaba.

the class PropFillTreeNode method buildLogicalQueryPlan.

@Override
public LogicalSubQueryPlan buildLogicalQueryPlan(ContextManager contextManager) {
    TreeNodeLabelManager labelManager = contextManager.getTreeNodeLabelManager();
    VertexIdManager vertexIdManager = contextManager.getVertexIdManager();
    Message.Value.Builder argumentBuilder = Message.Value.newBuilder();
    propKeyList.forEach(v -> argumentBuilder.addIntValueList(SchemaUtils.getPropId(v, schema)));
    ProcessorFunction processorFunction = new ProcessorFunction(QueryFlowOuterClass.OperatorType.PROP_FILL, argumentBuilder);
    if (getInputNode() instanceof SourceVertexTreeNode) {
        return parseSingleUnaryVertex(vertexIdManager, labelManager, processorFunction, contextManager, new LogicalEdge(EdgeShuffleType.FORWARD));
    } else {
        return parseSingleUnaryVertex(vertexIdManager, labelManager, processorFunction, contextManager);
    }
}
Also used : ProcessorFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction) LogicalEdge(com.alibaba.maxgraph.compiler.logical.LogicalEdge) SourceVertexTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode) VertexIdManager(com.alibaba.maxgraph.compiler.logical.VertexIdManager)

Example 3 with SourceVertexTreeNode

use of com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode in project GraphScope by alibaba.

the class TreeBuilder method visitAndStep.

private TreeNode visitAndStep(AndStep step, TreeNode prev) {
    List<Traversal.Admin<?, ?>> traversals = ReflectionUtils.getFieldValue(ConnectiveStep.class, step, "traversals");
    List<TreeNode> andTreeNodeList = Lists.newArrayList();
    boolean saveFlag = rootPathFlag;
    rootPathFlag = false;
    traversals.forEach(v -> {
        TreeNode treeNode = travelTraversalAdmin(v, new SourceDelegateNode(prev, schema));
        if (treeNode instanceof HasTreeNode) {
            TreeNode hasInputNode = ((HasTreeNode) treeNode).getInputNode();
            if (hasInputNode instanceof SourceVertexTreeNode || hasInputNode instanceof SourceEdgeTreeNode || hasInputNode instanceof EdgeTreeNode) {
                prev.addHasContainerList(((HasTreeNode) treeNode).getHasContainerList());
            } else {
                andTreeNodeList.add(treeNode);
            }
        } else {
            andTreeNodeList.add(treeNode);
        }
    });
    rootPathFlag = saveFlag;
    if (andTreeNodeList.isEmpty()) {
        return prev;
    } else {
        AndTreeNode andTreeNode = new AndTreeNode(prev, schema);
        andTreeNode.getAndTreeNodeList().addAll(andTreeNodeList);
        return andTreeNode;
    }
}
Also used : SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) EstimateCountTreeNode(com.alibaba.maxgraph.compiler.tree.source.EstimateCountTreeNode) SourceEdgeTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode) SourceVertexTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode) SourceCreateGraphTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceCreateGraphTreeNode) SourceVertexTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode) SourceEdgeTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode) SourceDelegateNode(com.alibaba.maxgraph.compiler.tree.source.SourceDelegateNode) SourceEdgeTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode)

Example 4 with SourceVertexTreeNode

use of com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode in project GraphScope by alibaba.

the class TreeBuilder method visitLambdaFilterStep.

private TreeNode visitLambdaFilterStep(LambdaFilterStep step, TreeNode prev) {
    if (step.getPredicate() instanceof P) {
        Predicate predicate = step.getPredicate();
        List<HasContainer> hasContainerList = Lists.newArrayList();
        hasContainerList.add(convertPredicateToHasContainer(predicate, prev.getOutputValueType()));
        if (prev instanceof SourceVertexTreeNode || prev instanceof SourceEdgeTreeNode || prev instanceof EdgeTreeNode) {
            prev.addHasContainerList(hasContainerList);
            return prev;
        } else {
            return new HasTreeNode(prev, hasContainerList, schema);
        }
    } else {
        if (this.lambdaEnableFlag) {
            return new LambdaFilterTreeNode(prev, schema, buildLambdaIndex(step));
        } else {
            throw new UnsupportedOperationException("Not support lambda filter yet");
        }
    }
}
Also used : P(org.apache.tinkerpop.gremlin.process.traversal.P) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) SourceVertexTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode) SourceEdgeTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) SourceEdgeTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode) RegexPredicate(com.alibaba.maxgraph.sdkcommon.compiler.custom.RegexPredicate) ListKeyPredicate(com.alibaba.maxgraph.sdkcommon.compiler.custom.ListKeyPredicate) StringKeyPredicate(com.alibaba.maxgraph.sdkcommon.compiler.custom.StringKeyPredicate) CustomPredicate(com.alibaba.maxgraph.sdkcommon.compiler.custom.CustomPredicate) ListPredicate(com.alibaba.maxgraph.sdkcommon.compiler.custom.ListPredicate) Predicate(java.util.function.Predicate) BiPredicate(java.util.function.BiPredicate) RegexKeyPredicate(com.alibaba.maxgraph.sdkcommon.compiler.custom.RegexKeyPredicate) StringPredicate(com.alibaba.maxgraph.sdkcommon.compiler.custom.StringPredicate)

Example 5 with SourceVertexTreeNode

use of com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode in project GraphScope by alibaba.

the class TreeBuilder method visitRangeGlobalStep.

private TreeNode visitRangeGlobalStep(RangeGlobalStep step, TreeNode prev) {
    long low = ReflectionUtils.getFieldValue(RangeGlobalStep.class, step, "low");
    long high = ReflectionUtils.getFieldValue(RangeGlobalStep.class, step, "high");
    boolean bypass = ReflectionUtils.getFieldValue(RangeGlobalStep.class, step, "bypass");
    if (bypass) {
        return prev;
    }
    TreeNode lastRangeNode = prev;
    while (lastRangeNode.getNodeType() == NodeType.MAP) {
        lastRangeNode = UnaryTreeNode.class.cast(lastRangeNode).getInputNode();
    }
    if (lastRangeNode instanceof OrderGlobalTreeNode) {
        lastRangeNode.setRangeLimit(low, high, rootPathFlag);
        return prev;
    } else if (lastRangeNode.getNodeType() == NodeType.FLATMAP || lastRangeNode instanceof SourceVertexTreeNode || lastRangeNode instanceof SourceEdgeTreeNode) {
        lastRangeNode.setRangeLimit(0, high - low, rootPathFlag);
        long tmplow = low;
        low = 0;
        high = high - tmplow;
    }
    TreeNode outputNode = lastRangeNode.getOutputNode();
    RangeGlobalTreeNode rangeGlobalTreeNode = new RangeGlobalTreeNode(lastRangeNode, schema, low, high);
    if (null == outputNode) {
        return rangeGlobalTreeNode;
    } else {
        UnaryTreeNode.class.cast(outputNode).setInputNode(rangeGlobalTreeNode);
        return prev;
    }
}
Also used : SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) EstimateCountTreeNode(com.alibaba.maxgraph.compiler.tree.source.EstimateCountTreeNode) SourceEdgeTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode) SourceVertexTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode) SourceCreateGraphTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceCreateGraphTreeNode) SourceVertexTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode) SourceEdgeTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode)

Aggregations

SourceVertexTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceVertexTreeNode)7 SourceEdgeTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceEdgeTreeNode)5 SourceTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode)4 EstimateCountTreeNode (com.alibaba.maxgraph.compiler.tree.source.EstimateCountTreeNode)2 SourceCreateGraphTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceCreateGraphTreeNode)2 EdgeRelation (com.alibaba.maxgraph.compiler.api.schema.EdgeRelation)1 GraphElement (com.alibaba.maxgraph.compiler.api.schema.GraphElement)1 GraphSchema (com.alibaba.maxgraph.compiler.api.schema.GraphSchema)1 LogicalEdge (com.alibaba.maxgraph.compiler.logical.LogicalEdge)1 VertexIdManager (com.alibaba.maxgraph.compiler.logical.VertexIdManager)1 ProcessorFunction (com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction)1 BaseTreeNode (com.alibaba.maxgraph.compiler.tree.BaseTreeNode)1 EdgeOtherVertexTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode)1 EdgeTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeTreeNode)1 EdgeVertexTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode)1 TreeNode (com.alibaba.maxgraph.compiler.tree.TreeNode)1 VertexTreeNode (com.alibaba.maxgraph.compiler.tree.VertexTreeNode)1 SourceDelegateNode (com.alibaba.maxgraph.compiler.tree.source.SourceDelegateNode)1 CustomPredicate (com.alibaba.maxgraph.sdkcommon.compiler.custom.CustomPredicate)1 ListKeyPredicate (com.alibaba.maxgraph.sdkcommon.compiler.custom.ListKeyPredicate)1