private ImmutableSet<Integer> extractNonMatchedRightAttributeIndexes(ImmutableCollection<UniqueConstraint> matchedUCs, ImmutableCollection<ForeignKeyConstraint> matchedFKs, int arity) { return IntStream.range(0, arity) .filter(i -> (matchedUCs.stream() .noneMatch(uc -> uc.getAttributes().stream() .anyMatch(a -> a.getIndex() == (i + 1))))) .filter(i -> (matchedFKs.stream() .noneMatch(fk -> fk.getComponents().stream() .anyMatch(c -> c.getReference().getIndex() == (i + 1))))) .boxed() .collect(ImmutableCollectors.toSet()); }
/** * Definition row graph: an RDF graph consisting of the following triples: * * - a reference triple for each <column name list> in a table's foreign keys where none of the column values is NULL. * */ private ImmutableList<TargetAtom> getRefCQ(ForeignKeyConstraint fk) { ImmutableTerm sub = generateSubject(fk.getRelation(), true); ImmutableTerm obj = generateSubject(fk.getReferencedRelation(), true); TargetAtom atom = getAtom(getReferencePropertyIRI(fk), sub, obj); return ImmutableList.of(atom); }
/** * builds a FOREIGN KEY constraint * * @param name * @return null if the list of components is empty */ public ForeignKeyConstraint build(String name) { ImmutableList<Component> components = builder.build(); if (components.isEmpty()) return null; return new ForeignKeyConstraint(name, components); } }
private String getReferencePropertyIRI(ForeignKeyConstraint fk) { List<String> attributes = new ArrayList<>(fk.getComponents().size()); for (Component component : fk.getComponents()) attributes.add(R2RMLIRISafeEncoder.encode(component.getAttribute().getID().getName())); return baseIRI + R2RMLIRISafeEncoder.encode(fk.getRelation().getID().getTableName()) + "#ref-" + Joiner.on(";").join(attributes); }
/*** * Definition reference triple: an RDF triple with: * <p/> * subject: the row node for the row. * predicate: the reference property IRI for the columns. * object: the row node for the referenced row. * * @param fk * @return */ private String getRefSQL(ForeignKeyConstraint fk) { Set<Object> columns = new LinkedHashSet<>(); // Set avoids duplicated and LinkedHashSet keeps the insertion order for (Attribute attr : getIdentifyingAttributes(fk.getRelation())) columns.add(getColumnNameWithAlias(attr)); List<String> conditions = new ArrayList<>(fk.getComponents().size()); for (ForeignKeyConstraint.Component comp : fk.getComponents()) { columns.add(getColumnNameWithAlias(comp.getReference())); conditions.add(getColumnName(comp.getAttribute()) + " = " + getColumnName(comp.getReference())); } for (Attribute attr : getIdentifyingAttributes(fk.getReferencedRelation())) columns.add(getColumnNameWithAlias(attr)); final String tables = fk.getRelation().getID().getSQLRendering() + ", " + fk.getReferencedRelation().getID().getSQLRendering(); return String.format("SELECT %s FROM %s WHERE %s", Joiner.on(", ").join(columns), tables, Joiner.on(" AND ").join(conditions)); }
for (ForeignKeyConstraint fks : def.getForeignKeys()) { DatabaseRelationDefinition def2 = fks.getReferencedRelation(); for (ForeignKeyConstraint.Component comp : fks.getComponents()) {
private Stream<ForeignKeyConstraint> extractMatchedFKsForARelation( DatabaseRelationDefinition leftRelation, Collection<ImmutableList<? extends VariableOrGroundTerm>> leftArgumentLists, ImmutableList<? extends VariableOrGroundTerm> rightArguments, DatabaseRelationDefinition rightRelation) { return leftRelation.getForeignKeys().stream() .filter(fk -> fk.getReferencedRelation().equals(rightRelation)) .filter(fk -> leftArgumentLists.stream() .anyMatch(leftArguments -> isFkMatching(fk, leftArguments, rightArguments))); }
ForeignKeyConstraint.Builder builder = ForeignKeyConstraint.builder(fkTable, pkTable); for (int i = 0; i < pkAttrs.length; i++) { QuotedID pkAttrId = idfac.createAttributeID(pkAttrs[i]);
/*** * Definition reference triple: an RDF triple with: * <p/> * subject: the row node for the row. * predicate: the reference property IRI for the columns. * object: the row node for the referenced row. * * @param fk * @return */ private String getRefSQL(ForeignKeyConstraint fk) { Set<Object> columns = new LinkedHashSet<>(); // Set avoids duplicated and LinkedHashSet keeps the insertion order for (Attribute attr : getIdentifyingAttributes(fk.getRelation())) columns.add(getColumnNameWithAlias(attr)); List<String> conditions = new ArrayList<>(fk.getComponents().size()); for (ForeignKeyConstraint.Component comp : fk.getComponents()) { columns.add(getColumnNameWithAlias(comp.getReference())); conditions.add(getColumnName(comp.getAttribute()) + " = " + getColumnName(comp.getReference())); } for (Attribute attr : getIdentifyingAttributes(fk.getReferencedRelation())) columns.add(getColumnNameWithAlias(attr)); final String tables = fk.getRelation().getID().getSQLRendering() + ", " + fk.getReferencedRelation().getID().getSQLRendering(); return String.format("SELECT %s FROM %s WHERE %s", Joiner.on(", ").join(columns), tables, Joiner.on(" AND ").join(conditions)); }
private IRI getReferencePropertyIRI(ForeignKeyConstraint fk) { List<String> attributes = new ArrayList<>(fk.getComponents().size()); for (Component component : fk.getComponents()) attributes.add(R2RMLIRISafeEncoder.encode(component.getAttribute().getID().getName())); return rdfFactory.createIRI(baseIRI + R2RMLIRISafeEncoder.encode(fk.getRelation().getID().getTableName()) + "#ref-" + Joiner.on(";").join(attributes)); }
private Stream<ForeignKeyConstraint> extractMatchedFKsForARelation( RelationDefinition leftRelation, Collection<ImmutableList<? extends VariableOrGroundTerm>> leftArgumentLists, ImmutableList<? extends VariableOrGroundTerm> rightArguments, RelationDefinition rightRelation) { return leftRelation.getForeignKeys().stream() .filter(fk -> fk.getReferencedRelation().equals(rightRelation)) .filter(fk -> leftArgumentLists.stream() .anyMatch(leftArguments -> isFkMatching(fk, leftArguments, rightArguments))); }
ForeignKeyConstraint.Builder builder = ForeignKeyConstraint.builder(fkTable, pkTable); for (int i = 0; i < pkAttrs.length; i++) { QuotedID pkAttrId = idfac.createAttributeID(pkAttrs[i]);
private LinearInclusionDependency getLinearInclusionDependency(ForeignKeyConstraint fk) { DatabaseRelationDefinition def = fk.getRelation(); DatabaseRelationDefinition def2 = fk.getReferencedRelation(); // create variables for the current table int len1 = def.getAttributes().size(); List<Term> terms1 = new ArrayList<>(len1); for (int i = 1; i <= len1; i++) terms1.add(termFactory.getVariable("t" + i)); // create variables for the referenced table int len2 = def2.getAttributes().size(); List<Term> terms2 = new ArrayList<>(len2); for (int i = 1; i <= len2; i++) terms2.add(termFactory.getVariable("p" + i)); for (ForeignKeyConstraint.Component comp : fk.getComponents()) { // indexes start at 1 int pos1 = comp.getAttribute().getIndex() - 1; // current column (1) int pos2 = comp.getReference().getIndex() - 1; // referenced column (2) terms1.set(pos1, terms2.get(pos2)); } Function head = termFactory.getFunction(def2.getAtomPredicate(), terms2); Function body = termFactory.getFunction(def.getAtomPredicate(), terms1); return new LinearInclusionDependency(head, body); }
private boolean isFkMatching(ForeignKeyConstraint foreignKey, ImmutableList<? extends VariableOrGroundTerm> leftArguments, ImmutableList<? extends VariableOrGroundTerm> rightArguments) { return foreignKey.getComponents().stream() .allMatch(c -> leftArguments.get(c.getAttribute().getIndex() - 1) .equals(rightArguments.get(c.getReference().getIndex() - 1)) // Excludes nullable attributes for the moment. TODO: reconsider it && (!c.getAttribute().canNull())); }
/** * Definition row graph: an RDF graph consisting of the following triples: * * - a reference triple for each <column name list> in a table's foreign keys where none of the column values is NULL. * */ private ImmutableList<ImmutableFunctionalTerm> getRefCQ(ForeignKeyConstraint fk) { ImmutableTerm sub = generateSubject(fk.getRelation(), true); ImmutableTerm obj = generateSubject(fk.getReferencedRelation(), true); ImmutableFunctionalTerm atom = df.getImmutableFunctionalTerm( df.getObjectPropertyPredicate(getReferencePropertyIRI(fk)), sub, obj); return ImmutableList.of(atom); }
/** * TODO: explain */ private Stream<DataNode> selectRedundantNodesForConstraint(DatabaseRelationDefinition sourceRelation, ForeignKeyConstraint constraint, IntermediateQuery query, InnerJoinNode joinNode, ImmutableMultimap<DatabaseRelationDefinition, DataNode> dataNodeMap) { /** * "Target" data nodes === "referenced" data nodes */ ImmutableCollection<DataNode> targetDataNodes = dataNodeMap.get(constraint.getReferencedRelation()); /** * No optimization possible */ if (targetDataNodes.isEmpty()) { return Stream.empty(); } return dataNodeMap.get(sourceRelation).stream() .flatMap(s -> targetDataNodes.stream() .filter(t -> areMatching(s,t,constraint))) .distinct() .filter(t -> areNonFKColumnsUnused(t, query, constraint)); }
/** * builds a FOREIGN KEY constraint * * @param name * @return null if the list of components is empty */ public ForeignKeyConstraint build(String name) { ImmutableList<Component> components = builder.build(); if (components.isEmpty()) return null; return new ForeignKeyConstraint(name, components); } }
private ImmutableSet<Integer> extractNonMatchedRightAttributeIndexes(ImmutableCollection<UniqueConstraint> matchedUCs, ImmutableCollection<ForeignKeyConstraint> matchedFKs, int arity) { return IntStream.range(0, arity) .filter(i -> (matchedUCs.stream() .noneMatch(uc -> uc.getAttributes().stream() .anyMatch(a -> a.getIndex() == (i + 1))))) .filter(i -> (matchedFKs.stream() .noneMatch(fk -> fk.getComponents().stream() .anyMatch(c -> c.getReference().getIndex() == (i + 1))))) .boxed() .collect(ImmutableCollectors.toSet()); }
/** * TODO: explain */ private Stream<ExtensionalDataNode> selectRedundantNodesForConstraint(RelationDefinition sourceRelation, ForeignKeyConstraint constraint, IntermediateQuery query, InnerJoinNode joinNode, ImmutableMultimap<RelationDefinition, ExtensionalDataNode> dataNodeMap) { /** * "Target" data nodes === "referenced" data nodes */ ImmutableCollection<ExtensionalDataNode> targetDataNodes = dataNodeMap.get(constraint.getReferencedRelation()); /** * No optimization possible */ if (targetDataNodes.isEmpty()) { return Stream.empty(); } return dataNodeMap.get(sourceRelation).stream() .flatMap(s -> targetDataNodes.stream() .filter(t -> areMatching(s,t,constraint))) .distinct() .filter(t -> areNonFKColumnsUnused(t, query, constraint)); }
private boolean isFkMatching(ForeignKeyConstraint foreignKey, ImmutableList<? extends VariableOrGroundTerm> leftArguments, ImmutableList<? extends VariableOrGroundTerm> rightArguments) { return foreignKey.getComponents().stream() .allMatch(c -> leftArguments.get(c.getAttribute().getIndex() - 1) .equals(rightArguments.get(c.getReference().getIndex() - 1)) // Excludes nullable attributes for the moment. TODO: reconsider it && (!c.getAttribute().canNull())); }