Skip to content

Commit

Permalink
#9885 (#32) - consider all geometry properties if propName is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoltz committed Aug 13, 2024
1 parent aca75ad commit 2384006
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,24 @@ protected SQLOperation toProtoSQL(LogicalOperator op) throws UnmappableException
* @throws FilterEvaluationException if the filter contains invalid
* {@link ValueReference}s
*/
protected abstract SQLOperation toProtoSQL(SpatialOperator op)
throws UnmappableException, FilterEvaluationException;
protected SQLOperation toProtoSQL(SpatialOperator op) throws UnmappableException, FilterEvaluationException {
List<SQLExpression> propNameExprs = toProtoSQLSpatial(op.getPropName());
checkIfExpressionsAreSpatial(propNameExprs, op.getPropName());

SQLOperationBuilder builder = new SQLOperationBuilder(BOOLEAN);
boolean isFirst = true;
for (SQLExpression propNameExpr : propNameExprs) {
if (!isFirst) {
builder.add(" OR ");
}
toProtoSql(op, propNameExpr, builder);
isFirst = false;
}
return builder.toOperation();
}

protected abstract void toProtoSql(SpatialOperator op, SQLExpression propNameExpr, SQLOperationBuilder builder)
throws FilterEvaluationException, UnmappableException;

/**
* Translates the given {@link TemporalOperator} into an {@link SQLOperation}.
Expand Down Expand Up @@ -916,22 +932,25 @@ protected SQLExpression toProtoSQL(ValueReference propName) throws UnmappableExc
* @throws FilterEvaluationException if the filter contains invalid
* {@link ValueReference}s
*/
protected SQLExpression toProtoSQLSpatial(ValueReference propName)
protected List<SQLExpression> toProtoSQLSpatial(ValueReference propName)
throws FilterEvaluationException, UnmappableException {
SQLExpression sql = null;
PropertyNameMapping propMapping = mapper.getSpatialMapping(propName, aliasManager);
if (propMapping != null) {
propNameMappingList.add(propMapping);
if (propMapping instanceof ConstantPropertyNameMapping) {
// TODO get rid of ConstantPropertyNameMapping
PrimitiveType pt = new PrimitiveType(STRING);
PrimitiveValue value = new PrimitiveValue("" + ((ConstantPropertyNameMapping) propMapping).getValue(),
pt);
PrimitiveParticleConverter converter = new DefaultPrimitiveConverter(pt, null, false);
sql = new SQLArgument(value, converter);
}
else {
sql = new SQLColumn(propMapping.getTableAlias(), propMapping.getColumn(), propMapping.getConverter());
List<SQLExpression> sql = new ArrayList<>();
List<PropertyNameMapping> propMappings = mapper.getSpatialMappings(propName, aliasManager);
if (!propMappings.isEmpty()) {
for (PropertyNameMapping propMapping : propMappings) {
propNameMappingList.add(propMapping);
if (propMapping instanceof ConstantPropertyNameMapping) {
// TODO get rid of ConstantPropertyNameMapping
PrimitiveType pt = new PrimitiveType(STRING);
PrimitiveValue value = new PrimitiveValue(
"" + ((ConstantPropertyNameMapping) propMapping).getValue(), pt);
PrimitiveParticleConverter converter = new DefaultPrimitiveConverter(pt, null, false);
sql.add(new SQLArgument(value, converter));
}
else {
sql.add(new SQLColumn(propMapping.getTableAlias(), propMapping.getColumn(),
propMapping.getConverter()));
}
}
}
else {
Expand Down Expand Up @@ -1015,4 +1034,19 @@ protected String getStringValueFromFunction(Expression pattern)
return ((PrimitiveValue) value).getAsText();
}

private void checkIfExpressionsAreSpatial(List<SQLExpression> sqlExpressions, ValueReference propName)
throws FilterEvaluationException {
for (SQLExpression sqlExpression : sqlExpressions)
checkIfExpressionIsSpatial(sqlExpression, propName);
}

protected void checkIfExpressionIsSpatial(SQLExpression sqlExpression, ValueReference propName)
throws FilterEvaluationException {
if (!sqlExpression.isSpatial()) {
String msg = "Cannot evaluate spatial operator on database. Targeted property name '" + propName
+ "' does not denote a spatial column.";
throw new FilterEvaluationException(msg);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.deegree.filter.FilterEvaluationException;
import org.deegree.filter.expression.ValueReference;

import java.util.List;

/**
* Implementations provide {@link ValueReference} to table/column mappings for
* {@link AbstractWhereBuilder} implementations.
Expand All @@ -56,10 +58,10 @@ public interface PropertyNameMapper {
* invalid
* @throws UnmappableException
*/
public PropertyNameMapping getMapping(ValueReference propName, TableAliasManager aliasManager)
PropertyNameMapping getMapping(ValueReference propName, TableAliasManager aliasManager)
throws FilterEvaluationException, UnmappableException;

public PropertyNameMapping getSpatialMapping(ValueReference propName, TableAliasManager aliasManager)
List<PropertyNameMapping> getSpatialMappings(ValueReference propName, TableAliasManager aliasManager)
throws FilterEvaluationException, UnmappableException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
----------------------------------------------------------------------------*/
package org.deegree.sqldialect.mssql;

import static java.sql.Types.BOOLEAN;
import static org.deegree.commons.tom.primitive.BaseType.DECIMAL;

import org.deegree.commons.tom.primitive.PrimitiveType;
import org.deegree.commons.tom.primitive.PrimitiveValue;
import org.deegree.commons.tom.sql.DefaultPrimitiveConverter;
Expand Down Expand Up @@ -74,6 +71,9 @@

import java.util.List;

import static java.sql.Types.BOOLEAN;
import static org.deegree.commons.tom.primitive.BaseType.DECIMAL;

/**
* {@link AbstractWhereBuilder} implementation for Microsoft SQL Server databases.
*
Expand Down Expand Up @@ -162,17 +162,7 @@ private String replaceAdditionalMsSqlServerSpecialChars(String sqlEncoded) {
}

@Override
protected SQLOperation toProtoSQL(SpatialOperator op) throws UnmappableException, FilterEvaluationException {

SQLOperationBuilder builder = new SQLOperationBuilder(BOOLEAN);

SQLExpression propNameExpr = toProtoSQLSpatial(op.getPropName());
if (!propNameExpr.isSpatial()) {
String msg = "Cannot evaluate spatial operator on database. Targeted property name '" + op.getPropName()
+ "' does not denote a spatial column.";
throw new FilterEvaluationException(msg);
}

protected void toProtoSql(SpatialOperator op, SQLExpression propNameExpr, SQLOperationBuilder builder) {
ICRS storageCRS = propNameExpr.getCRS();
int srid = propNameExpr.getSRID() != null ? Integer.parseInt(propNameExpr.getSRID()) : 0;

Expand Down Expand Up @@ -269,7 +259,6 @@ protected SQLOperation toProtoSQL(SpatialOperator op) throws UnmappableException
break;
}
}
return builder.toOperation();
}

private SQLExpression toProtoSQL(Geometry geom, ICRS targetCRS, int srid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
import org.junit.Before;
import org.junit.Test;

import java.util.Collections;
import java.util.List;

/**
* Tests for {@link MSSQLWhereBuilder}.
*
Expand All @@ -70,14 +73,13 @@ public void setup() throws FilterEvaluationException, UnmappableException {
PropertyNameMapper mapper = new PropertyNameMapper() {

@Override
public PropertyNameMapping getSpatialMapping(ValueReference propName, TableAliasManager aliasManager)
throws FilterEvaluationException, UnmappableException {
return new PropertyNameMapping(null, null, propName.getAsText(), "table");
public List<PropertyNameMapping> getSpatialMappings(ValueReference propName,
TableAliasManager aliasManager) {
return Collections.singletonList(new PropertyNameMapping(null, null, propName.getAsText(), "table"));
}

@Override
public PropertyNameMapping getMapping(ValueReference propName, TableAliasManager aliasManager)
throws FilterEvaluationException, UnmappableException {
public PropertyNameMapping getMapping(ValueReference propName, TableAliasManager aliasManager) {
return new PropertyNameMapping(null, null, propName.getAsText(), "table");
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,7 @@ class OracleWhereBuilder extends AbstractWhereBuilder {
}

@Override
protected SQLOperation toProtoSQL(SpatialOperator op) throws UnmappableException, FilterEvaluationException {

SQLOperationBuilder builder = new SQLOperationBuilder(BOOLEAN);
SQLExpression propNameExpr = toProtoSQLSpatial(op.getPropName());

protected void toProtoSql(SpatialOperator op, SQLExpression propNameExpr, SQLOperationBuilder builder) {
switch (op.getSubType()) {
case BBOX:
BBOX bbox = (BBOX) op;
Expand Down Expand Up @@ -178,8 +174,6 @@ protected SQLOperation toProtoSQL(SpatialOperator op) throws UnmappableException
appendDWithinOperation(builder, propNameExpr, ((Beyond) op).getGeometry(), ((Beyond) op).getDistance());
break;
}

return builder.toOperation();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
import org.deegree.time.position.TimePosition;
import org.deegree.time.primitive.GenericTimeInstant;
import org.deegree.time.primitive.GenericTimePeriod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* {@link AbstractWhereBuilder} implementation for PostGIS databases.
Expand All @@ -90,6 +92,8 @@
*/
public class PostGISWhereBuilder extends AbstractWhereBuilder {

private static final Logger LOG = LoggerFactory.getLogger(PostGISWhereBuilder.class);

private final boolean useLegacyPredicates;

/**
Expand Down Expand Up @@ -187,13 +191,8 @@ private SQLOperation getOperationFromBuilder(PropertyIsLike op, SQLExpression pr
}

@Override
protected SQLOperation toProtoSQL(SpatialOperator op) throws UnmappableException, FilterEvaluationException {

SQLOperationBuilder builder = new SQLOperationBuilder(BOOLEAN);

SQLExpression propNameExpr = toProtoSQLSpatial(op.getPropName());
checkIfExpressionIsSpatial(propNameExpr, op.getPropName());

protected void toProtoSql(SpatialOperator op, SQLExpression propNameExpr, SQLOperationBuilder builder)
throws FilterEvaluationException, UnmappableException {
ICRS storageCRS = propNameExpr.getCRS();
int srid = propNameExpr.getSRID() != null ? Integer.parseInt(propNameExpr.getSRID()) : -1;

Expand Down Expand Up @@ -376,7 +375,6 @@ protected SQLOperation toProtoSQL(SpatialOperator op) throws UnmappableException
break;
}
}
return builder.toOperation();
}

protected SQLOperation toProtoSQL(TemporalOperator op) throws UnmappableException, FilterEvaluationException {
Expand Down Expand Up @@ -495,7 +493,10 @@ private SQLExpression toProtoSQL(Geometry geom, ICRS targetCRS, int srid) throws
private SQLExpression toProtoSqlSecondParameter(SpatialOperator spatialOperator, ICRS storageCRS, int srid)
throws FilterEvaluationException, UnmappableException {
if (spatialOperator.getValueReference() != null) {
SQLExpression sqlExpression = toProtoSQLSpatial(spatialOperator.getValueReference());
List<SQLExpression> sqlExpressions = toProtoSQLSpatial(spatialOperator.getValueReference());
if (sqlExpressions.size() > 1)
LOG.warn("Multiple spatial geometry mappings as second parameter are currently not supported.");
SQLExpression sqlExpression = sqlExpressions.get(0);
checkIfExpressionIsSpatial(sqlExpression, spatialOperator.getValueReference());
return sqlExpression;
}
Expand Down Expand Up @@ -575,13 +576,4 @@ private boolean isTimeInstant(Expression parameter2) {
return parameter2 instanceof Literal && ((Literal<?>) parameter2).getValue() instanceof GenericTimeInstant;
}

private void checkIfExpressionIsSpatial(SQLExpression sqlExpression, ValueReference propName)
throws FilterEvaluationException {
if (!sqlExpression.isSpatial()) {
String msg = "Cannot evaluate spatial operator on database. Targeted property name '" + propName
+ "' does not denote a spatial column.";
throw new FilterEvaluationException(msg);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.deegree.sqldialect.filter.expression.SQLColumn;
import org.deegree.sqldialect.filter.expression.SQLExpression;
import org.deegree.sqldialect.filter.expression.SQLOperation;
import org.deegree.sqldialect.filter.expression.SQLOperationBuilder;

/**
* @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
Expand All @@ -80,7 +81,8 @@ public H2WhereBuilder(SQLDialect dialect, OperatorFilter filter, SortProperty[]
}

@Override
protected SQLOperation toProtoSQL(SpatialOperator op) throws UnmappableException, FilterEvaluationException {
protected void toProtoSql(SpatialOperator op, SQLExpression propNameExpr, SQLOperationBuilder builder)
throws UnmappableException {
throw new UnmappableException("Spatial operators are currently not mappable in h2.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -1680,8 +1681,7 @@ private AbstractWhereBuilder getWhereBuilderBlob(OperatorFilter filter, Connecti
final String srid = detectConfiguredSrid();
PropertyNameMapper mapper = new PropertyNameMapper() {
@Override
public PropertyNameMapping getMapping(ValueReference propName, TableAliasManager aliasManager)
throws FilterEvaluationException, UnmappableException {
public PropertyNameMapping getMapping(ValueReference propName, TableAliasManager aliasManager) {
GeometryStorageParams geometryParams = new GeometryStorageParams(blobMapping.getCRS(), srid,
CoordinateDimension.DIM_2);
GeometryMapping bboxMapping = new GeometryMapping(null, false, new DBField(blobMapping.getBBoxColumn()),
Expand All @@ -1691,9 +1691,9 @@ public PropertyNameMapping getMapping(ValueReference propName, TableAliasManager
}

@Override
public PropertyNameMapping getSpatialMapping(ValueReference propName, TableAliasManager aliasManager)
throws FilterEvaluationException, UnmappableException {
return getMapping(propName, aliasManager);
public List<PropertyNameMapping> getSpatialMappings(ValueReference propName,
TableAliasManager aliasManager) {
return Collections.singletonList(getMapping(propName, aliasManager));
}
};
return dialect.getWhereBuilder(mapper, filter, null, null, allowInMemoryFiltering);
Expand Down
Loading

0 comments on commit 2384006

Please sign in to comment.