Skip to content

Commit

Permalink
Merge branch 'version4' of https://github.com/ontop/ontop into version4
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzBuehmann committed Feb 10, 2021
2 parents 32bb1aa + a8e8133 commit e003a01
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ protected static void initOBDA(String dbScriptRelativePath, String obdaRelativeP
protected static void initOBDA(String dbScriptRelativePath, String obdaRelativePath,
@Nullable String ontologyRelativePath, @Nullable String propertyFile,
@Nullable String viewFile) throws SQLException, IOException {
initOBDA(dbScriptRelativePath, obdaRelativePath, ontologyRelativePath, propertyFile, viewFile, null);
}

protected static void initOBDA(String dbScriptRelativePath, String obdaRelativePath,
@Nullable String ontologyRelativePath, @Nullable String propertyFile,
@Nullable String viewFile, @Nullable String dbMetadataFile) throws SQLException, IOException {
String jdbcUrl = URL_PREFIX + UUID.randomUUID().toString();

SQL_CONNECTION = DriverManager.getConnection(jdbcUrl, USER, PASSWORD);
Expand Down Expand Up @@ -84,6 +90,9 @@ protected static void initOBDA(String dbScriptRelativePath, String obdaRelativeP
if (viewFile != null)
builder.ontopViewFile(AbstractRDF4JTest.class.getResource(viewFile).getPath());

if (dbMetadataFile != null)
builder.dbMetadataFile(AbstractRDF4JTest.class.getResource(dbMetadataFile).getPath());

OntopSQLOWLAPIConfiguration config = builder.build();

OntopRepository repo = OntopRepository.defaultRepository(config);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package it.unibz.inf.ontop.rdf4j.repository;

import com.google.common.collect.ImmutableList;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.sql.SQLException;

public class BasicViewPersonDBMetadataTest extends AbstractRDF4JTest {
private static final String OBDA_FILE = "/person/person_basicviews.obda";
private static final String SQL_SCRIPT = "/person/person.sql";
private static final String VIEW_FILE = "/person/views/basic_views.json";
private static final String DBMETADATA_FILE = "/person/person_with_constraints.db-extract.json";

@BeforeClass
public static void before() throws IOException, SQLException {
initOBDA(SQL_SCRIPT, OBDA_FILE, null, null, VIEW_FILE, DBMETADATA_FILE);
}

@AfterClass
public static void after() throws SQLException {
release();
}

/**
* Concatenation and upper case
*/
@Test
public void testPersonConcat() throws Exception {
String query = "PREFIX : <http://person.example.org/>\n" +
"PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" +
"SELECT ?v \n" +
"WHERE {\n" +
" ?x a :Person . \n" +
" ?x :fullNameAndLocality ?v . \n" +
"}";
runQueryAndCompare(query, ImmutableList.of("ROGER SMITH Botzen"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,27 @@
"isPrimaryKey" : true
} ],
"otherFunctionalDependencies" : [ ],
"foreignKeys" : [ ],
"foreignKeys" : [
{
"to": {
"columns": [
"\"id\""
],
"relation": [
"\"entity\""
]
},
"from": {
"columns": [
"\"id\""
],
"relation": [
"\"person\""
]
},
"name": "entity-fkey"
}
],
"columns" : [ {
"name" : "\"id\"",
"datatype" : "INTEGER",
Expand Down Expand Up @@ -47,7 +67,23 @@
"isNullable" : false
} ],
"name" : ["\"statuses\""]
} ],
},
{
"uniqueConstraints" : [ {
"name" : "CONSTRAINT_D",
"determinants" : [ "\"id\"" ],
"isPrimaryKey" : true
} ],
"otherFunctionalDependencies" : [ ],
"foreignKeys" : [ ],
"columns" : [ {
"name" : "\"id\"",
"datatype" : "INTEGER",
"isNullable" : false
}],
"name" : ["\"entity\""]
}
],
"metadata" : {
"dbmsProductName" : "H2",
"dbmsVersion" : "1.4.199 (2019-03-13)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
public class ProjectionDecomposerImpl implements ProjectionDecomposer {

private final Predicate<ImmutableFunctionalTerm> decompositionOracle;
private final Predicate<NonFunctionalTerm> postprocessNonFunctionalDefinitionOracle;
private final SubstitutionFactory substitutionFactory;
private final TermFactory termFactory;

@AssistedInject
private ProjectionDecomposerImpl(@Assisted Predicate<ImmutableFunctionalTerm> decompositionOracle,
@Assisted Predicate<NonFunctionalTerm> postprocessNonFunctionalDefinitionOracle,
SubstitutionFactory substitutionFactory, TermFactory termFactory) {
this.decompositionOracle = decompositionOracle;
this.postprocessNonFunctionalDefinitionOracle = postprocessNonFunctionalDefinitionOracle;
this.substitutionFactory = substitutionFactory;
this.termFactory = termFactory;
}
Expand Down Expand Up @@ -101,6 +104,19 @@ private DefinitionDecomposition decomposeDefinition(ImmutableTerm term, Variable
substitutionFactory.getSubstitution(variable, functionalTerm));
}
}
/*
* When the definition of the substitution (not a sub-term of a functional term) is not functional,
* we may also decide not to post-process it.
*
* Useful for using Ontop for generating SQL queries with no post-processing
* (for other purposes than SPARQL query answering)
*
*/
else if (definedVariable.isPresent()
&& (!postprocessNonFunctionalDefinitionOracle.test((NonFunctionalTerm) term))) {
Variable variable = definedVariable.get();
return new DefinitionDecomposition(variable, substitutionFactory.getSubstitution(variable, term));
}
else
return new DefinitionDecomposition(term);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public interface DBFunctionSymbolFactory {

DBFunctionSymbol getDBIfThenElse();

DBFunctionSymbol getDBNullIf();

DBFunctionSymbol getDBUpper();

DBFunctionSymbol getDBLower();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,11 @@ public DBFunctionSymbol getDBIfThenElse() {
return new MockupDBIfElseNullFunctionSymbol(dbBooleanType, abstractRootDBType);
}

@Override
public DBFunctionSymbol getDBNullIf() {
throw new UnsupportedOperationException("Operation not supported by the MockupDBFunctionSymbolFactory");
}

@Override
public DBFunctionSymbol getDBUpper() {
return getRegularDBFunctionSymbol("UPPER", 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import it.unibz.inf.ontop.iq.node.VariableNullability;
import it.unibz.inf.ontop.iq.tools.ProjectionDecomposer;
import it.unibz.inf.ontop.model.term.ImmutableFunctionalTerm;
import it.unibz.inf.ontop.model.term.NonFunctionalTerm;
import it.unibz.inf.ontop.model.term.Variable;

import java.util.Collection;
Expand Down Expand Up @@ -33,5 +34,6 @@ VariableNullability createVariableNullability(@Assisted("nullableGroups") Immuta

VariableNullability createEmptyVariableNullability(ImmutableSet<Variable> scope);

ProjectionDecomposer createProjectionDecomposer(Predicate<ImmutableFunctionalTerm> decompositionOracle);
ProjectionDecomposer createProjectionDecomposer(Predicate<ImmutableFunctionalTerm> decompositionOracle,
Predicate<NonFunctionalTerm> postprocessNonFunctionalDefinitionOracle);
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ public ImmutableList<RelationID> getRelationIDs() throws MetadataExtractionExcep
}

@Override
public void insertIntegrityConstraints(NamedRelationDefinition relation, MetadataLookup metadataLookup) throws MetadataExtractionException {
public void insertIntegrityConstraints(NamedRelationDefinition relation, MetadataLookup metadataLookupForFk) throws MetadataExtractionException {
JsonDatabaseTable jsonTable = relationMap.get(relation.getID());
if (jsonTable == null)
throw new IllegalArgumentException("The relation " + relation.getID().getSQLRendering()
+ " is unknown to the JsonSerializedMetadataProvider");

jsonTable.insertIntegrityConstraints(metadataLookup);
jsonTable.insertIntegrityConstraints(relation, metadataLookupForFk);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,19 @@ public DatabaseTableDefinition createDatabaseTableDefinition(DBParameters dbPara
return new DatabaseTableDefinition(allIDs, attributeListBuilder);
}

public void insertIntegrityConstraints(MetadataLookup lookup) throws MetadataExtractionException {
DatabaseTableDefinition relation = (DatabaseTableDefinition)lookup.getRelation(
JsonMetadata.deserializeRelationID(lookup.getQuotedIDFactory(), name));
public void insertIntegrityConstraints(NamedRelationDefinition relation, MetadataLookup lookupForFk) throws MetadataExtractionException {

for (JsonUniqueConstraint uc: uniqueConstraints)
uc.insert(relation, lookup.getQuotedIDFactory());
uc.insert(relation, lookupForFk.getQuotedIDFactory());

for (JsonFunctionalDependency fd: otherFunctionalDependencies)
fd.insert(relation, lookup.getQuotedIDFactory());
fd.insert(relation, lookupForFk.getQuotedIDFactory());

for (JsonForeignKey fk : foreignKeys) {
if (!fk.from.relation.equals(this.name))
throw new MetadataExtractionException("Table names mismatch: " + name + " != " + fk.from.relation);

fk.insert(relation, lookup);
fk.insert(relation, lookupForFk);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.fasterxml.jackson.annotation.*;
import it.unibz.inf.ontop.dbschema.*;
import it.unibz.inf.ontop.dbschema.impl.DatabaseTableDefinition;
import it.unibz.inf.ontop.exception.MetadataExtractionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.stream.Stream;
Expand All @@ -18,6 +19,8 @@ public class JsonForeignKey extends JsonOpenObject {
public final String name;
public final Part from, to;

private static final Logger LOGGER = LoggerFactory.getLogger(JsonForeignKey.class);

@JsonCreator
public JsonForeignKey(@JsonProperty("name") String name,
@JsonProperty("from") Part from,
Expand All @@ -35,10 +38,17 @@ public JsonForeignKey(ForeignKeyConstraint fk) {
.map(ForeignKeyConstraint.Component::getReferencedAttribute));
}

public void insert(DatabaseTableDefinition relation, MetadataLookup lookup) throws MetadataExtractionException {
public void insert(NamedRelationDefinition relation, MetadataLookup lookup) throws MetadataExtractionException {

ForeignKeyConstraint.Builder builder = ForeignKeyConstraint.builder(name, relation,
lookup.getRelation(JsonMetadata.deserializeRelationID(lookup.getQuotedIDFactory(), to.relation)));
ForeignKeyConstraint.Builder builder;
try {
builder = ForeignKeyConstraint.builder(name, relation,
lookup.getRelation(JsonMetadata.deserializeRelationID(lookup.getQuotedIDFactory(), to.relation)));
}
catch (MetadataExtractionException e) {
LOGGER.warn("Cannot find table {} for FK {}", to.relation, name);
return ;
}

try {
for (int i = 0; i < from.columns.size(); i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.fasterxml.jackson.annotation.*;
import it.unibz.inf.ontop.dbschema.FunctionalDependency;
import it.unibz.inf.ontop.dbschema.NamedRelationDefinition;
import it.unibz.inf.ontop.dbschema.QuotedIDFactory;
import it.unibz.inf.ontop.dbschema.impl.DatabaseTableDefinition;
import it.unibz.inf.ontop.exception.MetadataExtractionException;

import java.util.List;
Expand All @@ -28,7 +28,7 @@ public JsonFunctionalDependency(FunctionalDependency fd) {
this.dependents = JsonMetadata.serializeAttributeList(fd.getDependents().stream());
}

public void insert(DatabaseTableDefinition relation, QuotedIDFactory idFactory) throws MetadataExtractionException {
public void insert(NamedRelationDefinition relation, QuotedIDFactory idFactory) throws MetadataExtractionException {
FunctionalDependency.Builder builder = FunctionalDependency.defaultBuilder(relation);
JsonMetadata.deserializeAttributeList(idFactory, determinants, builder::addDeterminant);
JsonMetadata.deserializeAttributeList(idFactory, dependents, builder::addDependent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.fasterxml.jackson.annotation.*;
import it.unibz.inf.ontop.dbschema.FunctionalDependency;
import it.unibz.inf.ontop.dbschema.NamedRelationDefinition;
import it.unibz.inf.ontop.dbschema.QuotedIDFactory;
import it.unibz.inf.ontop.dbschema.UniqueConstraint;
import it.unibz.inf.ontop.dbschema.impl.DatabaseTableDefinition;
import it.unibz.inf.ontop.exception.MetadataExtractionException;

import java.util.List;
Expand Down Expand Up @@ -35,7 +35,7 @@ public JsonUniqueConstraint(UniqueConstraint uc) {
this.determinants = JsonMetadata.serializeAttributeList(uc.getAttributes().stream());
}

public void insert(DatabaseTableDefinition relation, QuotedIDFactory idFactory) throws MetadataExtractionException {
public void insert(NamedRelationDefinition relation, QuotedIDFactory idFactory) throws MetadataExtractionException {
FunctionalDependency.Builder builder = isPrimaryKey
? UniqueConstraint.primaryKeyBuilder(relation, name)
: UniqueConstraint.builder(relation, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface OntopSQLCredentialConfiguration extends OntopSQLCoreConfigurati
@Override
OntopSQLCredentialSettings getSettings();

static Builder<? extends Builder> defaultBuilder() {
static Builder<? extends Builder<?>> defaultBuilder() {
return new OntopSQLCredentialConfigurationImpl.BuilderImpl<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,11 @@ public DBFunctionSymbol getDBIfThenElse() {
return ifThenElse;
}

@Override
public DBFunctionSymbol getDBNullIf() {
return getRegularDBFunctionSymbol(NULLIF_STR, 2);
}

@Override
public DBFunctionSymbol getDBUpper() {
return getRegularDBFunctionSymbol(UPPER_STR, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ private PostProcessingProjectionSplitterImpl(IntermediateQueryFactory iqFactory,
this.iqFactory = iqFactory;
this.substitutionFactory = substitutionFactory;
this.avoidPostProcessingDecomposer = coreUtilsFactory.createProjectionDecomposer(
PostProcessingProjectionSplitterImpl::hasFunctionalToBePostProcessed);
this.proPostProcessingDecomposer = coreUtilsFactory.createProjectionDecomposer(ImmutableFunctionalTerm::canBePostProcessed);
PostProcessingProjectionSplitterImpl::hasFunctionalToBePostProcessed,
t -> !(t.isNull() || (t instanceof Variable) || (t instanceof DBConstant)));
this.proPostProcessingDecomposer = coreUtilsFactory.createProjectionDecomposer(
ImmutableFunctionalTerm::canBePostProcessed, t -> true);
this.distinctNormalizer = distinctNormalizer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface OntopMappingConfiguration extends OntopOBDAConfiguration, Ontop



static Builder<? extends Builder> defaultBuilder() {
static Builder<? extends Builder<?>> defaultBuilder() {
return new OntopMappingConfigurationImpl.BuilderImpl<>();
}

Expand Down

0 comments on commit e003a01

Please sign in to comment.