Skip to content

Commit

Permalink
Add translateGeoIntersects() + test
Browse files Browse the repository at this point in the history
  • Loading branch information
murermader committed Aug 23, 2024
1 parent 690d7c3 commit 04678de
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class QueryParameterizer extends AlgShuttleImpl implements RexVisitor<Rex
@Getter
private final Map<Integer, Map<Integer, List<ParameterValue>>> docs;

private final List<OperatorName> excluded = List.of( OperatorName.MQL_REGEX_MATCH, OperatorName.MQL_QUERY_VALUE, OperatorName.MQL_GEO_WITHIN );
private final List<OperatorName> excluded = List.of( OperatorName.MQL_REGEX_MATCH, OperatorName.MQL_QUERY_VALUE, OperatorName.MQL_GEO_WITHIN, OperatorName.MQL_GEO_INTERSECTS );

@Getter
private final List<AlgDataType> types;
Expand Down
39 changes: 39 additions & 0 deletions dbms/src/test/java/org/polypheny/db/mql/MqlGeoFunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,45 @@ public void beforeEach() {
// execute("use %s; db.%s.deleteMany({})".formatted( namespaceMongo, collectionName ));
}

@Test
public void docGeoIntersectsTest() {
String insertDocuments = """
db.%s.insertMany([
{
name: "Legacy [0,0]",
num: 1,
legacy: [0,0]
},
{
name: "Legacy [1,1]",
num: 2,
legacy: [1,1]
},
{
name: "Legacy [2,2]",
num: 3,
legacy: [2,2]
}
])
""".formatted( collectionName );
execute( insertDocuments );

DocResult result;
String geoIntersects = """
db.%s.find({
legacy: {
$geoIntersects: {
$geometry: {
type: "Polygon",
coordinates: [[ [0,0], [0,1], [1,1], [1,0], [0,0] ]]
}
}
}
})
""".formatted( collectionName );
result = execute( geoIntersects );
assertEquals( result.data.length, 2 );
}

@Test
public void docGeoWithinTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ private void translateMatch2( RexNode node ) {
return;
case MQL_ELEM_MATCH:
translateElemMatch( (RexCall) node );
case MQL_GEO_INTERSECTS:
translateGeoIntersects( (RexCall) node );
return;
case MQL_GEO_WITHIN:
translateGeoWithin( (RexCall) node );
return;
Expand Down Expand Up @@ -733,6 +736,16 @@ private void translateGeoWithin( RexCall node ) {
throw new GenericRuntimeException( "Cannot translate $geoWithin to MongoDB query." );
}

private void translateGeoIntersects( RexCall node ) {
String left = getParamAsKey( node.operands.get( 0 ) );
PolyGeometry filterGeometry = getLiteralAs( node, 1, PolyValue::asGeometry );
BsonDocument geometry = new BsonDocument();
geometry.put("$geometry", BsonDocument.parse( filterGeometry.toJson() ));
BsonDocument geoWithin = new BsonDocument();
geoWithin.put("$geoIntersects", geometry);
attachCondition( null, left, geoWithin );
}


private <E> E getLiteralAs( RexCall node, int pos, Function<PolyValue, E> transformer ) {
return transformer.apply( node.operands.get( pos ).unwrap( RexLiteral.class ).orElseThrow().value );
Expand Down

0 comments on commit 04678de

Please sign in to comment.