Skip to content

Commit

Permalink
TEIID-3038: Adding geoSpatial support for MongoDB translator
Browse files Browse the repository at this point in the history
  • Loading branch information
rareddy committed Aug 4, 2014
1 parent 5f9ff7d commit 4cfe304
Show file tree
Hide file tree
Showing 8 changed files with 460 additions and 123 deletions.
1 change: 1 addition & 0 deletions build/kits/jboss-as7/docs/teiid/teiid-releasenotes.html
Expand Up @@ -38,6 +38,7 @@ <H2><A NAME="Highlights"></A>Highlights</H2>
<li>TEIID-2888 <b>Designer VDB support in Embedded</b> is now available. With a few caveats, Designer .INDEX based vdbs can now be used with Teiid Embedded. See the Embedded Guide for more.
<li>TEIID-2904 <b>Translator Properties</b> Translator import, override and extension-metadata properties are can retrieved through Admin API</li>
<li>TEIID-2627 <b>Accumulo</b> Translator has been added. See Admin and Reference guides for more information. See <a href="https://community.jboss.org/wiki/ApacheAccumuloWithTeiid">example here</a></li>
<li>TEIID-3038 Adding geoSpatial support for MongoDB translator
</ul>

<h2><a name="Compatibility">Compatibility Issues</a></h2>
Expand Down
Expand Up @@ -32,6 +32,8 @@ class ColumnDetail {
String columnName;
String targetDocumentName;
Object expression;
boolean partOfGroupBy;
boolean partOfProject;

public QueryBuilder getQueryBuilder() {
QueryBuilder query = QueryBuilder.start(this.projectedName);
Expand Down
Expand Up @@ -37,6 +37,7 @@
import org.teiid.core.types.*;
import org.teiid.language.*;
import org.teiid.language.visitor.SQLStringVisitor;
import org.teiid.metadata.FunctionMethod;
import org.teiid.metadata.RuntimeMetadata;
import org.teiid.mongodb.MongoDBConnection;
import org.teiid.translator.*;
Expand All @@ -53,9 +54,20 @@

@Translator(name="mongodb", description="MongoDB Translator, reads and writes the data to MongoDB")
public class MongoDBExecutionFactory extends ExecutionFactory<ConnectionFactory, MongoDBConnection> {
public static final Version TWO_4 = Version.getVersion("2.4"); //$NON-NLS-1$
private static final String MONGO = "mongo"; //$NON-NLS-1$
public static final Version TWO_4 = Version.getVersion("2.4"); //$NON-NLS-1$
public static final Version TWO_6 = Version.getVersion("2.6"); //$NON-NLS-1$

public static final String FUNC_GEO_WITHIN = "geoWithin"; //$NON-NLS-1$
public static final String FUNC_GEO_INTERSECTS = "geoIntersects"; //$NON-NLS-1$
public static final String FUNC_GEO_NEAR = "geoNear"; //$NON-NLS-1$
public static final String FUNC_GEO_NEAR_SPHERE = "geoNearSphere"; //$NON-NLS-1$
public static final String FUNC_GEO_POLYGON_WITHIN = "geoPolygonWithin"; //$NON-NLS-1$
public static final String FUNC_GEO_POLYGON_INTERSECTS = "geoPolygonIntersects"; //$NON-NLS-1$

public static final String[] GEOSPATIAL_FUNCTIONS = {FUNC_GEO_WITHIN, FUNC_GEO_INTERSECTS, FUNC_GEO_NEAR, FUNC_GEO_NEAR_SPHERE, FUNC_GEO_POLYGON_WITHIN, FUNC_GEO_POLYGON_INTERSECTS};
public static final String AVOID_PROJECTION = "AVOID_PROJECTION"; //$NON-NLS-1$

protected Map<String, FunctionModifier> functionModifiers = new TreeMap<String, FunctionModifier>(String.CASE_INSENSITIVE_ORDER);
private Version version = TWO_4;

Expand All @@ -66,7 +78,13 @@ public MongoDBExecutionFactory() {
setSourceRequiredForMetadata(false);
setSupportsInnerJoins(true);
setSupportsOuterJoins(true);
setSupportedJoinCriteria(SupportedJoinCriteria.KEY);
setSupportedJoinCriteria(SupportedJoinCriteria.KEY);
}

@SuppressWarnings("nls")
@Override
public void start() throws TranslatorException {
super.start();

registerFunctionModifier("+", new AliasModifier("$add"));//$NON-NLS-1$ //$NON-NLS-2$
registerFunctionModifier("-", new AliasModifier("$subtract"));//$NON-NLS-1$ //$NON-NLS-2$
Expand Down Expand Up @@ -97,12 +115,62 @@ public List<?> translate(Function function) {
registerFunctionModifier(SourceSystemFunctions.HOUR, new AliasModifier("$hour"));//$NON-NLS-1$
registerFunctionModifier(SourceSystemFunctions.MINUTE, new AliasModifier("$minute"));//$NON-NLS-1$
registerFunctionModifier(SourceSystemFunctions.SECOND, new AliasModifier("$second"));//$NON-NLS-1$
registerFunctionModifier(SourceSystemFunctions.IFNULL, new AliasModifier("$ifNull")); //$NON-NLS-1$
registerFunctionModifier(SourceSystemFunctions.IFNULL, new AliasModifier("$ifNull")); //$NON-NLS-1$

FunctionMethod method = null;
method = addPushDownFunction(MONGO, FUNC_GEO_INTERSECTS, TypeFacility.RUNTIME_NAMES.BOOLEAN, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.BIG_DECIMAL+"[][]"); //$NON-NLS-1$ //$NON-NLS-2$
method.setProperty(AVOID_PROJECTION, "true");
method = addPushDownFunction(MONGO, FUNC_GEO_WITHIN, TypeFacility.RUNTIME_NAMES.BOOLEAN, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.BIG_DECIMAL+"[][]"); //$NON-NLS-1$ //$NON-NLS-2$
method.setProperty(AVOID_PROJECTION, "true");
method = addPushDownFunction(MONGO, FUNC_GEO_NEAR, TypeFacility.RUNTIME_NAMES.BOOLEAN, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.BIG_DECIMAL+"[]", TypeFacility.RUNTIME_NAMES.INTEGER); //$NON-NLS-1$ //$NON-NLS-2$
method.setProperty(AVOID_PROJECTION, "true");
method = addPushDownFunction(MONGO, FUNC_GEO_NEAR_SPHERE, TypeFacility.RUNTIME_NAMES.BOOLEAN, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.BIG_DECIMAL+"[]", TypeFacility.RUNTIME_NAMES.INTEGER); //$NON-NLS-1$ //$NON-NLS-2$
method.setProperty(AVOID_PROJECTION, "true");
method = addPushDownFunction(MONGO, FUNC_GEO_POLYGON_INTERSECTS, TypeFacility.RUNTIME_NAMES.BOOLEAN, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.BIG_DECIMAL,TypeFacility.RUNTIME_NAMES.BIG_DECIMAL,TypeFacility.RUNTIME_NAMES.BIG_DECIMAL,TypeFacility.RUNTIME_NAMES.BIG_DECIMAL); //$NON-NLS-1$ //$NON-NLS-2$
method.setProperty(AVOID_PROJECTION, "true");
method = addPushDownFunction(MONGO, FUNC_GEO_POLYGON_WITHIN, TypeFacility.RUNTIME_NAMES.BOOLEAN, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.BIG_DECIMAL,TypeFacility.RUNTIME_NAMES.BIG_DECIMAL,TypeFacility.RUNTIME_NAMES.BIG_DECIMAL,TypeFacility.RUNTIME_NAMES.BIG_DECIMAL); //$NON-NLS-1$ //$NON-NLS-2$
method.setProperty(AVOID_PROJECTION, "true");

registerFunctionModifier(FUNC_GEO_NEAR, new AliasModifier("$near"));//$NON-NLS-1$
registerFunctionModifier(FUNC_GEO_NEAR_SPHERE, new AliasModifier("$nearSphere"));//$NON-NLS-1$
registerFunctionModifier(FUNC_GEO_WITHIN, new AliasModifier("$geoWithin"));//$NON-NLS-1$
registerFunctionModifier(FUNC_GEO_INTERSECTS, new AliasModifier("$geoIntersects"));//$NON-NLS-1$
registerFunctionModifier(FUNC_GEO_POLYGON_INTERSECTS, new GeoPolygonFunctionModifier("$geoIntersects"));//$NON-NLS-1$
registerFunctionModifier(FUNC_GEO_POLYGON_WITHIN, new GeoPolygonFunctionModifier("$geoWithin"));//$NON-NLS-1$
}

@Override
public void start() throws TranslatorException {
super.start();

private static class GeoPolygonFunctionModifier extends FunctionModifier {
private String functionName;

public GeoPolygonFunctionModifier(String name) {
this.functionName = name;
}

@Override
public List<?> translate(Function function) {
List<Expression> args = function.getParameters();
Expression north = args.get(1);
Expression east = args.get(2);
Expression west = args.get(3);
Expression south = args.get(4);

ArrayList<Expression> points = new ArrayList<Expression>();
points.add(new org.teiid.language.Array(TypeFacility.RUNTIME_TYPES.BIG_DECIMAL, Arrays.asList(west, north)));
points.add(new org.teiid.language.Array(TypeFacility.RUNTIME_TYPES.BIG_DECIMAL, Arrays.asList(east, north)));
points.add(new org.teiid.language.Array(TypeFacility.RUNTIME_TYPES.BIG_DECIMAL, Arrays.asList(east, south)));
points.add(new org.teiid.language.Array(TypeFacility.RUNTIME_TYPES.BIG_DECIMAL, Arrays.asList(west, south)));
points.add(new org.teiid.language.Array(TypeFacility.RUNTIME_TYPES.BIG_DECIMAL, Arrays.asList(west, north)));

Expression coordinates = new org.teiid.language.Array(TypeFacility.RUNTIME_TYPES.BIG_DECIMAL, points);

Function func = LanguageFactory.INSTANCE.createFunction(this.functionName,
Arrays.asList(args.get(0),
LanguageFactory.INSTANCE.createLiteral("Polygon", TypeFacility.RUNTIME_TYPES.STRING), //$NON-NLS-1$
coordinates
),
Boolean.class);
return Arrays.asList(func);
}
}

@TranslatorProperty(display="Database Version", description= "Database Version")
Expand Down
Expand Up @@ -62,6 +62,10 @@ public static enum Event implements BundleUtil.Event{
TEIID18026,
TEIID18027,
TEIID18028,
TEIID18029
TEIID18029,
TEIID18030,
TEIID18031,
TEIID18032,
TEIID18033,
}
}

0 comments on commit 4cfe304

Please sign in to comment.