forked from typedb/typedb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query planner refactor 1 (typedb#5087)
## What is the goal of this PR? Begin cleaning up very procedural and overly generic code in Query Planner to be more understandible. ## What are the changes implemented in this PR? * Remove Generic from `DirectedEdge<T>` and all associated classes, including tests. Usage was early exclusively `DirectedEdge<Node>`. Now none of the classes in the Query Planner accept a generic. It seems like the generic originally existed to allow using an Integer as a Node, rather than a Node object in tests - tests for now use `Node` with the Integer encoded as a variable string. * `Node` had methods `addIfPresent(a, b, allNodes)`, which did a create-if-present in the `allNodes` map and returned the Node. This functionality is moved out. * `NodeId` now uses a factory constructor because the instances are not meant to be unique * Move a large chunk of isolated code from `GreedyTraversalPlan.java` into `RelationTypeInference.java` - all to do with inferring types from role players to generate more Label fragments. * `Fragment`s provide the `Node`s that go into the internal QueryPlanner traversal graph - Fragments that represent Janus edges for instance provide a `middle` node that is not a valid starting point. These are collected into the set of all Nodes at the start of query planning now rather than deep in the chain of c-like function calls. * Move chunks of code into their own methods to deal with later (`chooseStartingNodes`, `buildDependenciesBetweenNodes`) * _important_ experimental change to NOT access all the indexed vertices first thing in the query plan - do them as a normal step in the flattened minimum spanning tree as required (starting point is still almost always an indexed vertex) * Remove `lowPriorityStartingNodes` as we currently always reify nodes, so implicit relationships are as good of a starting point as any
- Loading branch information
1 parent
1b5c16e
commit 58b6d60
Showing
29 changed files
with
799 additions
and
597 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/* | ||
* GRAKN.AI - THE KNOWLEDGE GRAPH | ||
* Copyright (C) 2018 Grakn Labs Ltd | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package grakn.core.graql.gremlin; | ||
|
||
import com.google.common.collect.HashMultimap; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.google.common.collect.Iterators; | ||
import com.google.common.collect.Multimap; | ||
import com.google.common.collect.Sets; | ||
import grakn.core.concept.Label; | ||
import grakn.core.concept.type.RelationType; | ||
import grakn.core.concept.type.Role; | ||
import grakn.core.concept.type.SchemaConcept; | ||
import grakn.core.concept.type.Type; | ||
import grakn.core.graql.gremlin.fragment.Fragment; | ||
import grakn.core.graql.gremlin.fragment.Fragments; | ||
import grakn.core.graql.gremlin.fragment.InIsaFragment; | ||
import grakn.core.graql.gremlin.fragment.InSubFragment; | ||
import grakn.core.graql.gremlin.fragment.LabelFragment; | ||
import grakn.core.graql.gremlin.fragment.OutRolePlayerFragment; | ||
import grakn.core.graql.gremlin.sets.EquivalentFragmentSets; | ||
import grakn.core.server.session.TransactionOLTP; | ||
import graql.lang.property.IsaProperty; | ||
import graql.lang.property.TypeProperty; | ||
import graql.lang.statement.Statement; | ||
import graql.lang.statement.Variable; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static graql.lang.Graql.var; | ||
|
||
public class RelationTypeInference { | ||
// infer type of relation type if we know the type of the role players | ||
// add label fragment and isa fragment if we can infer any | ||
public static Set<Fragment> inferRelationTypes(TransactionOLTP tx, Set<Fragment> allFragments) { | ||
|
||
Set<Fragment> inferredFragments = new HashSet<>(); | ||
|
||
Map<Variable, Type> labelVarTypeMap = getLabelVarTypeMap(tx, allFragments); | ||
if (labelVarTypeMap.isEmpty()) return inferredFragments; | ||
|
||
Multimap<Variable, Type> instanceVarTypeMap = getInstanceVarTypeMap(allFragments, labelVarTypeMap); | ||
|
||
Multimap<Variable, Variable> relationRolePlayerMap = getRelationRolePlayerMap(allFragments, instanceVarTypeMap); | ||
if (relationRolePlayerMap.isEmpty()) return inferredFragments; | ||
|
||
// for each type, get all possible relation type it could be in | ||
Multimap<Type, RelationType> relationMap = HashMultimap.create(); | ||
labelVarTypeMap.values().stream().distinct().forEach( | ||
type -> addAllPossibleRelations(relationMap, type)); | ||
|
||
// inferred labels should be kept separately, even if they are already in allFragments set | ||
Map<Label, Statement> inferredLabels = new HashMap<>(); | ||
relationRolePlayerMap.asMap().forEach((relationVar, rolePlayerVars) -> { | ||
|
||
Set<Type> possibleRelationTypes = rolePlayerVars.stream() | ||
.filter(instanceVarTypeMap::containsKey) | ||
.map(rolePlayer -> getAllPossibleRelationTypes( | ||
instanceVarTypeMap.get(rolePlayer), relationMap)) | ||
.reduce(Sets::intersection).orElse(Collections.emptySet()); | ||
|
||
//TODO: if possibleRelationTypes here is empty, the query will not match any data | ||
if (possibleRelationTypes.size() == 1) { | ||
|
||
Type relationType = possibleRelationTypes.iterator().next(); | ||
Label label = relationType.label(); | ||
|
||
// add label fragment if this label has not been inferred | ||
if (!inferredLabels.containsKey(label)) { | ||
Statement labelVar = var(); | ||
inferredLabels.put(label, labelVar); | ||
Fragment labelFragment = Fragments.label(new TypeProperty(label.getValue()), labelVar.var(), ImmutableSet.of(label)); | ||
inferredFragments.add(labelFragment); | ||
} | ||
|
||
// finally, add inferred isa fragments | ||
Statement labelVar = inferredLabels.get(label); | ||
IsaProperty isaProperty = new IsaProperty(labelVar); | ||
EquivalentFragmentSet isaEquivalentFragmentSet = EquivalentFragmentSets.isa(isaProperty, | ||
relationVar, labelVar.var(), relationType.isImplicit()); | ||
inferredFragments.addAll(isaEquivalentFragmentSet.fragments()); | ||
} | ||
}); | ||
|
||
return inferredFragments; | ||
} | ||
|
||
// find all vars with direct or indirect out isa edges | ||
private static Multimap<Variable, Type> getInstanceVarTypeMap( | ||
Set<Fragment> allFragments, Map<Variable, Type> labelVarTypeMap) { | ||
Multimap<Variable, Type> instanceVarTypeMap = HashMultimap.create(); | ||
int oldSize; | ||
do { | ||
oldSize = instanceVarTypeMap.size(); | ||
allFragments.stream() | ||
.filter(fragment -> labelVarTypeMap.containsKey(fragment.start())) // restrict to types | ||
.filter(fragment -> fragment instanceof InIsaFragment || fragment instanceof InSubFragment) // | ||
.forEach(fragment -> instanceVarTypeMap.put(fragment.end(), labelVarTypeMap.get(fragment.start()))); | ||
} while (oldSize != instanceVarTypeMap.size()); | ||
return instanceVarTypeMap; | ||
} | ||
|
||
// find all vars representing types | ||
private static Map<Variable, Type> getLabelVarTypeMap(TransactionOLTP tx, Set<Fragment> allFragments) { | ||
Map<Variable, Type> labelVarTypeMap = new HashMap<>(); | ||
allFragments.stream() | ||
.filter(LabelFragment.class::isInstance) | ||
.forEach(fragment -> { | ||
// TODO: labels() should return ONE label instead of a set | ||
SchemaConcept schemaConcept = tx.getSchemaConcept( | ||
Iterators.getOnlyElement(((LabelFragment) fragment).labels().iterator())); | ||
if (schemaConcept != null && !schemaConcept.isRole() && !schemaConcept.isRule()) { | ||
labelVarTypeMap.put(fragment.start(), schemaConcept.asType()); | ||
} | ||
}); | ||
return labelVarTypeMap; | ||
} | ||
|
||
private static Multimap<Variable, Variable> getRelationRolePlayerMap( | ||
Set<Fragment> allFragments, Multimap<Variable, Type> instanceVarTypeMap) { | ||
// relation vars and its role player vars | ||
Multimap<Variable, Variable> relationRolePlayerMap = HashMultimap.create(); | ||
allFragments.stream().filter(OutRolePlayerFragment.class::isInstance) | ||
.forEach(fragment -> relationRolePlayerMap.put(fragment.start(), fragment.end())); | ||
|
||
// find all the relation requiring type inference | ||
Iterator<Variable> iterator = relationRolePlayerMap.keySet().iterator(); | ||
while (iterator.hasNext()) { | ||
Variable relation = iterator.next(); | ||
|
||
// the relation should have at least 2 known role players so we can infer something useful | ||
if (instanceVarTypeMap.containsKey(relation) || | ||
relationRolePlayerMap.get(relation).size() < 2) { | ||
iterator.remove(); | ||
} else { | ||
int numRolePlayersHaveType = 0; | ||
for (Variable rolePlayer : relationRolePlayerMap.get(relation)) { | ||
if (instanceVarTypeMap.containsKey(rolePlayer)) { | ||
numRolePlayersHaveType++; | ||
} | ||
} | ||
if (numRolePlayersHaveType < 2) { | ||
iterator.remove(); | ||
} | ||
} | ||
} | ||
return relationRolePlayerMap; | ||
} | ||
|
||
private static void addAllPossibleRelations(Multimap<Type, RelationType> relationMap, Type metaType) { | ||
metaType.subs().forEach(type -> type.playing().flatMap(Role::relations) | ||
.forEach(relationType -> relationMap.put(type, relationType))); | ||
} | ||
|
||
private static Set<Type> getAllPossibleRelationTypes( | ||
Collection<Type> instanceVarTypes, Multimap<Type, RelationType> relationMap) { | ||
|
||
return instanceVarTypes.stream() | ||
.map(rolePlayerType -> (Set<Type>) new HashSet<Type>(relationMap.get(rolePlayerType))) | ||
.reduce(Sets::intersection).orElse(Collections.emptySet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.