Search in sources :

Example 11 with GraphSchema

use of com.alibaba.maxgraph.compiler.api.schema.GraphSchema 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());
            }
        }
    }
}
Also used : BaseTreeNode(com.alibaba.maxgraph.compiler.tree.BaseTreeNode) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) ProcessorLabelValueFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorLabelValueFunction) CompilerUtils(com.alibaba.maxgraph.compiler.utils.CompilerUtils) Set(java.util.Set) ContextManager(com.alibaba.maxgraph.compiler.optimizer.ContextManager) TreeNodeUtils(com.alibaba.maxgraph.compiler.utils.TreeNodeUtils) CostMappingManager(com.alibaba.maxgraph.compiler.cost.CostMappingManager) Message(com.alibaba.maxgraph.Message) ProcessorFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) CostModelManager(com.alibaba.maxgraph.compiler.cost.CostModelManager) Sets(com.google.common.collect.Sets) RowField(com.alibaba.maxgraph.compiler.cost.RowField) QueryFlowOuterClass(com.alibaba.maxgraph.QueryFlowOuterClass) TreeManager(com.alibaba.maxgraph.compiler.tree.TreeManager) List(java.util.List) Lists(com.google.common.collect.Lists) RowFieldManager(com.alibaba.maxgraph.compiler.cost.RowFieldManager) TreeNodeLabelManager(com.alibaba.maxgraph.compiler.tree.TreeNodeLabelManager) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) ProcessorFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorFunction) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) BaseTreeNode(com.alibaba.maxgraph.compiler.tree.BaseTreeNode) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) BaseTreeNode(com.alibaba.maxgraph.compiler.tree.BaseTreeNode) ProcessorLabelValueFunction(com.alibaba.maxgraph.compiler.logical.function.ProcessorLabelValueFunction)

Example 12 with GraphSchema

use of com.alibaba.maxgraph.compiler.api.schema.GraphSchema in project GraphScope by alibaba.

the class CostDataStatistics method getOutERatio.

/**
 * Compute outE scale ratio with start vertex label list and edge list
 *
 * @param input The given start vertex label list
 * @param edgeList The given edge label list
 * @return The result ratio
 */
public NodeStatistics getOutERatio(NodeStatistics input, Set<String> edgeList) {
    GraphSchema schema = schemaFetcher.getSchemaSnapshotPair().getLeft();
    NodeStatistics nodeStatistics = new NodeStatistics(schema);
    Map<String, Double> inputVertexCountList = input.getVertexCountList();
    for (String edgeLabel : edgeList) {
        try {
            GraphEdge edge = (GraphEdge) schema.getElement(edgeLabel);
            List<EdgeRelation> relationList = edge.getRelationList();
            if (relationList.size() > 0) {
                double avgRelationRatio = 1.0 / edge.getRelationList().size();
                relationList.stream().filter(v -> inputVertexCountList.containsKey(v.getSource().getLabel())).forEach(v -> {
                    double sourceVertexCount = inputVertexCountList.getOrDefault(v.getSource().getLabel(), INIT_VERTEX_COUNT);
                    double currEdgeCount = (this.edgeCountList.getOrDefault(edge.getLabel(), INIT_EDGE_COUNT) * avgRelationRatio / this.vertexCountList.getOrDefault(v.getSource().getLabel(), INIT_VERTEX_COUNT)) * sourceVertexCount;
                    nodeStatistics.addEdgeCount(edgeLabel, currEdgeCount);
                });
            }
        } catch (Exception ignored) {
        }
    }
    return nodeStatistics;
}
Also used : GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) SchemaFetcher(com.alibaba.maxgraph.compiler.api.schema.SchemaFetcher) EdgeVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode) VertexTreeNode(com.alibaba.maxgraph.compiler.tree.VertexTreeNode) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EdgeTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeTreeNode) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) NodeType(com.alibaba.maxgraph.compiler.tree.NodeType) Sets(com.google.common.collect.Sets) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge) Direction(org.apache.tinkerpop.gremlin.structure.Direction) List(java.util.List) Lists(com.google.common.collect.Lists) EdgeRelation(com.alibaba.maxgraph.compiler.api.schema.EdgeRelation) EdgeOtherVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode) Map(java.util.Map) JSONObject(com.alibaba.fastjson.JSONObject) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) EdgeRelation(com.alibaba.maxgraph.compiler.api.schema.EdgeRelation) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema)

Example 13 with GraphSchema

use of com.alibaba.maxgraph.compiler.api.schema.GraphSchema in project GraphScope by alibaba.

the class CostDataStatistics method vertexQueryList.

public Map<String, String> vertexQueryList() {
    GraphSchema schema = schemaFetcher.getSchemaSnapshotPair().getLeft();
    Map<String, String> queryList = Maps.newHashMap();
    schema.getVertexList().forEach(v -> {
        queryList.put(v.getLabel(), String.format(VERTEX_QUERY, v.getLabel()));
    });
    return queryList;
}
Also used : GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema)

Example 14 with GraphSchema

use of com.alibaba.maxgraph.compiler.api.schema.GraphSchema in project GraphScope by alibaba.

the class CostDataStatistics method computeStepCount.

/**
 * Compute step count with given node and node label
 *
 * @param inputStatistics The given input statistics
 * @param treeNode The given tree node
 * @param currNodeLabel The given current node label
 * @return The result step count
 */
private NodeStatistics computeStepCount(NodeStatistics inputStatistics, TreeNode treeNode, NodeLabelList currNodeLabel) {
    GraphSchema schema = schemaFetcher.getSchemaSnapshotPair().getLeft();
    NodeStatistics nodeStatistics = new NodeStatistics(schema);
    if (null == inputStatistics) {
        if (currNodeLabel.isUnknownFlag()) {
            for (Map.Entry<String, Double> entry : vertexCountList.entrySet()) {
                nodeStatistics.addVertexCount(entry.getKey(), entry.getValue());
            }
        } else {
            SourceTreeNode sourceTreeNode = (SourceTreeNode) treeNode;
            Object[] ids = sourceTreeNode.getIds();
            Set<String> vertexLabelList = currNodeLabel.getVertexLabelList();
            for (String vertexLabel : vertexLabelList) {
                double vertexCount = null == ids ? vertexCountList.getOrDefault(vertexLabel, INIT_VERTEX_COUNT) : (ids.length * 1.0 / vertexLabelList.size());
                nodeStatistics.addVertexCount(vertexLabel, vertexCount);
            }
            Set<String> edgeLabelList = currNodeLabel.getEdgeLabelList();
            for (String edgeLabel : currNodeLabel.getEdgeLabelList()) {
                double edgeCount = null == ids ? edgeCountList.getOrDefault(edgeLabel, INIT_EDGE_COUNT) : (ids.length * 1.0 / edgeLabelList.size());
                nodeStatistics.addEdgeCount(edgeLabel, edgeCount);
            }
        }
    } else {
        if (treeNode instanceof VertexTreeNode) {
            VertexTreeNode vertexTreeNode = (VertexTreeNode) treeNode;
            Direction direction = vertexTreeNode.getDirection();
            Set<String> edgeLabelList;
            if (null == vertexTreeNode.getEdgeLabels()) {
                edgeLabelList = Sets.newHashSet();
            } else {
                edgeLabelList = Sets.newHashSet(vertexTreeNode.getEdgeLabels());
            }
            switch(direction) {
                case OUT:
                    {
                        nodeStatistics.merge(getOutRatio(inputStatistics, edgeLabelList));
                        break;
                    }
                case IN:
                    {
                        nodeStatistics.merge(getInRatio(inputStatistics, edgeLabelList));
                        break;
                    }
                case BOTH:
                    {
                        nodeStatistics.merge(getOutRatio(inputStatistics, edgeLabelList));
                        nodeStatistics.merge(getInRatio(inputStatistics, edgeLabelList));
                        break;
                    }
            }
        } else if (treeNode instanceof EdgeTreeNode) {
            Set<String> edgeLabelList = currNodeLabel.getEdgeLabelList();
            Direction direction = ((EdgeTreeNode) treeNode).getDirection();
            switch(direction) {
                case OUT:
                    {
                        nodeStatistics.merge(getOutERatio(inputStatistics, edgeLabelList));
                        break;
                    }
                case IN:
                    {
                        nodeStatistics.merge(getInERatio(inputStatistics, edgeLabelList));
                        break;
                    }
                case BOTH:
                    {
                        nodeStatistics.merge(getOutERatio(inputStatistics, edgeLabelList));
                        nodeStatistics.merge(getInERatio(inputStatistics, edgeLabelList));
                        break;
                    }
            }
        } else if (treeNode instanceof EdgeVertexTreeNode) {
            Map<String, Double> edgeCountList = inputStatistics.getEdgeCountList();
            Direction direction = ((EdgeVertexTreeNode) treeNode).getDirection();
            edgeCountList.keySet().stream().map(v -> {
                try {
                    return schema.getElement(v);
                } catch (Exception ignored) {
                    return null;
                }
            }).filter(v -> null != v && v instanceof GraphEdge).map(v -> (GraphEdge) v).forEach(v -> {
                v.getRelationList().forEach(vv -> {
                    double currEdgeCount = edgeCountList.get(v.getLabel());
                    if (direction == Direction.OUT || direction == Direction.BOTH) {
                        List<String> resultVertexLabelList = Lists.newArrayList();
                        if (currNodeLabel.getVertexLabelList().contains(vv.getSource().getLabel())) {
                            resultVertexLabelList.add(vv.getSource().getLabel());
                        }
                        double avgVertexCount = currEdgeCount / resultVertexLabelList.size();
                        for (String vertexLabel : resultVertexLabelList) {
                            nodeStatistics.addVertexCount(vertexLabel, avgVertexCount);
                        }
                    }
                    if (direction == Direction.IN || direction == Direction.BOTH) {
                        List<String> resultVertexLabelList = Lists.newArrayList();
                        if (currNodeLabel.getVertexLabelList().contains(vv.getTarget().getLabel())) {
                            resultVertexLabelList.add(vv.getTarget().getLabel());
                        }
                        double avgVertexCount = currEdgeCount / resultVertexLabelList.size();
                        for (String vertexLabel : resultVertexLabelList) {
                            nodeStatistics.addVertexCount(vertexLabel, avgVertexCount);
                        }
                    }
                });
            });
        } else if (treeNode instanceof EdgeOtherVertexTreeNode) {
            Set<String> vertexLabelList = currNodeLabel.getVertexLabelList();
            double avgVertexCount = inputStatistics.totalCount() / vertexLabelList.size();
            vertexLabelList.forEach(v -> nodeStatistics.addVertexCount(v, avgVertexCount));
        } else {
            NodeType nodeType = treeNode.getNodeType();
            if (NodeType.MAP == nodeType) {
                nodeStatistics.addElementCount(inputStatistics.totalCount());
            } else if (NodeType.FILTER == nodeType) {
                nodeStatistics.merge(inputStatistics, FILTER_RATIO);
            } else if (NodeType.FLATMAP == nodeType) {
                nodeStatistics.addElementCount(inputStatistics.totalCount() * FLATMAP_RATIO);
            } else if (NodeType.AGGREGATE == nodeType) {
                nodeStatistics.addElementCount(1);
            } else {
                nodeStatistics.merge(inputStatistics);
            }
        }
    }
    return nodeStatistics;
}
Also used : GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) SchemaFetcher(com.alibaba.maxgraph.compiler.api.schema.SchemaFetcher) EdgeVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode) VertexTreeNode(com.alibaba.maxgraph.compiler.tree.VertexTreeNode) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EdgeTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeTreeNode) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) NodeType(com.alibaba.maxgraph.compiler.tree.NodeType) Sets(com.google.common.collect.Sets) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge) Direction(org.apache.tinkerpop.gremlin.structure.Direction) List(java.util.List) Lists(com.google.common.collect.Lists) EdgeRelation(com.alibaba.maxgraph.compiler.api.schema.EdgeRelation) EdgeOtherVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode) Map(java.util.Map) JSONObject(com.alibaba.fastjson.JSONObject) TreeNode(com.alibaba.maxgraph.compiler.tree.TreeNode) GraphElement(com.alibaba.maxgraph.compiler.api.schema.GraphElement) Set(java.util.Set) EdgeVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode) SourceTreeNode(com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode) EdgeTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeTreeNode) Direction(org.apache.tinkerpop.gremlin.structure.Direction) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) EdgeVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode) VertexTreeNode(com.alibaba.maxgraph.compiler.tree.VertexTreeNode) EdgeOtherVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode) EdgeOtherVertexTreeNode(com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode) NodeType(com.alibaba.maxgraph.compiler.tree.NodeType) JSONObject(com.alibaba.fastjson.JSONObject) Map(java.util.Map) GraphEdge(com.alibaba.maxgraph.compiler.api.schema.GraphEdge)

Example 15 with GraphSchema

use of com.alibaba.maxgraph.compiler.api.schema.GraphSchema in project GraphScope by alibaba.

the class MaxTestGraph method dropData.

private void dropData() {
    CompletableFuture<GraphSchema> future = new CompletableFuture<>();
    this.clientService.dropSchema(DropSchemaRequest.newBuilder().build(), new StreamObserver<DropSchemaResponse>() {

        @Override
        public void onNext(DropSchemaResponse value) {
            future.complete(GraphDef.parseProto(value.getGraphDef()));
        }

        @Override
        public void onError(Throwable t) {
            future.completeExceptionally(t);
        }

        @Override
        public void onCompleted() {
        }
    });
    GraphSchema schema;
    try {
        schema = future.get();
    } catch (Exception e) {
        throw new MaxGraphException(e);
    }
    logger.info("drop schema: " + ((GraphDef) schema).toProto().toString());
}
Also used : DropSchemaResponse(com.alibaba.maxgraph.proto.groot.DropSchemaResponse) CompletableFuture(java.util.concurrent.CompletableFuture) MaxGraphException(com.alibaba.maxgraph.compiler.api.exception.MaxGraphException) GraphSchema(com.alibaba.maxgraph.compiler.api.schema.GraphSchema) URISyntaxException(java.net.URISyntaxException) MaxGraphException(com.alibaba.maxgraph.compiler.api.exception.MaxGraphException) IOException(java.io.IOException)

Aggregations

GraphSchema (com.alibaba.maxgraph.compiler.api.schema.GraphSchema)26 GraphElement (com.alibaba.maxgraph.compiler.api.schema.GraphElement)12 List (java.util.List)9 Map (java.util.Map)8 Lists (com.google.common.collect.Lists)7 Sets (com.google.common.collect.Sets)7 Set (java.util.Set)7 TreeNode (com.alibaba.maxgraph.compiler.tree.TreeNode)6 Maps (com.google.common.collect.Maps)6 Collectors (java.util.stream.Collectors)6 EdgeRelation (com.alibaba.maxgraph.compiler.api.schema.EdgeRelation)5 GraphEdge (com.alibaba.maxgraph.compiler.api.schema.GraphEdge)5 EdgeOtherVertexTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeOtherVertexTreeNode)5 EdgeTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeTreeNode)5 EdgeVertexTreeNode (com.alibaba.maxgraph.compiler.tree.EdgeVertexTreeNode)5 VertexTreeNode (com.alibaba.maxgraph.compiler.tree.VertexTreeNode)5 SourceTreeNode (com.alibaba.maxgraph.compiler.tree.source.SourceTreeNode)5 Direction (org.apache.tinkerpop.gremlin.structure.Direction)5 JSONObject (com.alibaba.fastjson.JSONObject)4 SchemaFetcher (com.alibaba.maxgraph.compiler.api.schema.SchemaFetcher)4