Skip to content

Commit 972e4b6

Browse files
committed
GEOMESA-3582 Refactored out magic strings in TrinoFilterToSQL
1 parent 015f19b commit 972e4b6

1 file changed

Lines changed: 34 additions & 19 deletions

File tree

  • geomesa-trino/geomesa-trino-datastore/src/main/java/org/locationtech/geomesa/trino/datastore

geomesa-trino/geomesa-trino-datastore/src/main/java/org/locationtech/geomesa/trino/datastore/TrinoFilterToSQL.java

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private static String quoteIdent(String name) {
7878
* so this is defense-in-depth — it keeps the geometry literal on the same escaping
7979
* discipline as identifiers ({@link #quoteIdent}), feature ids, and the visibility literal. */
8080
private String geomFromText(Geometry geom) {
81-
return "ST_GeometryFromText('" + wkt.write(geom).replace("'", "''") + "')";
81+
return ST_GEOMETRY_FROM_TEXT + "('" + wkt.write(geom).replace("'", "''") + "')";
8282
}
8383

8484
/** Per-instance, NOT static: {@link WKTWriter} is not thread-safe, and a
@@ -104,6 +104,21 @@ private String geomFromText(Geometry geom) {
104104
* prefilter never drops matching rows). Also avoids the division-by-zero at exactly ±90°. */
105105
private static final double NEAR_POLE_LAT = 85.0;
106106

107+
// Trino spatial SQL function names emitted by this translator.
108+
private static final String ST_INTERSECTS = "ST_Intersects";
109+
private static final String ST_WITHIN = "ST_Within";
110+
private static final String ST_CONTAINS = "ST_Contains";
111+
private static final String ST_CROSSES = "ST_Crosses";
112+
private static final String ST_TOUCHES = "ST_Touches";
113+
private static final String ST_OVERLAPS = "ST_Overlaps";
114+
private static final String ST_EQUALS = "ST_Equals";
115+
private static final String ST_DISJOINT = "ST_Disjoint";
116+
private static final String ST_GEOM_FROM_BINARY = "ST_GeomFromBinary";
117+
private static final String ST_GEOMETRY_FROM_TEXT = "ST_GeometryFromText";
118+
private static final String ST_DISTANCE = "ST_Distance";
119+
private static final String TO_SPHERICAL_GEOGRAPHY = "to_spherical_geography";
120+
private static final String GEOMETRY_NEAREST_POINTS = "geometry_nearest_points";
121+
107122
// ── Spatial ───────────────────────────────────────────────────────────────
108123
//
109124
// All binary spatial operators funnel through the base class's dispatcher: the
@@ -126,25 +141,25 @@ protected Object visitBinarySpatialOperator(BinarySpatialOperator filter,
126141
"Unsupported spatial filter literal (expected a geometry): " + filter);
127142
}
128143
if (filter instanceof Intersects) { // symmetric
129-
writeSpatial("ST_Intersects", col, geom);
144+
writeSpatial(ST_INTERSECTS, col, geom);
130145
} else if (filter instanceof Within) {
131-
writeSpatial(swapped ? "ST_Contains" : "ST_Within", col, geom);
146+
writeSpatial(swapped ? ST_CONTAINS : ST_WITHIN, col, geom);
132147
} else if (filter instanceof Contains) {
133-
writeSpatial(swapped ? "ST_Within" : "ST_Contains", col, geom);
148+
writeSpatial(swapped ? ST_WITHIN : ST_CONTAINS, col, geom);
134149
} else if (filter instanceof DWithin d) { // symmetric
135150
writeDWithin(col, geom, convertToMeters(d.getDistance(), d.getDistanceUnits()));
136151
} else if (filter instanceof Beyond b) { // symmetric
137152
writeBeyond(col, geom, convertToMeters(b.getDistance(), b.getDistanceUnits()));
138153
} else if (filter instanceof Crosses) { // symmetric
139-
writeSpatial("ST_Crosses", col, geom);
154+
writeSpatial(ST_CROSSES, col, geom);
140155
} else if (filter instanceof Touches) { // symmetric
141-
writeSpatial("ST_Touches", col, geom);
156+
writeSpatial(ST_TOUCHES, col, geom);
142157
} else if (filter instanceof Overlaps) { // symmetric
143-
writeSpatial("ST_Overlaps", col, geom);
158+
writeSpatial(ST_OVERLAPS, col, geom);
144159
} else if (filter instanceof Equals) { // symmetric
145-
writeSpatial("ST_Equals", col, geom);
160+
writeSpatial(ST_EQUALS, col, geom);
146161
} else if (filter instanceof Disjoint) { // symmetric
147-
writeSpatial("ST_Disjoint", col, geom);
162+
writeSpatial(ST_DISJOINT, col, geom);
148163
} else {
149164
throw new IllegalArgumentException("Unsupported spatial operator: " + filter);
150165
}
@@ -176,7 +191,7 @@ public Object visit(BBOX filter, Object extraData) {
176191
? pn.getPropertyName() : defaultGeomCol();
177192
BoundingBox b = filter.getBounds();
178193
Envelope env = new Envelope(b.getMinX(), b.getMaxX(), b.getMinY(), b.getMaxY());
179-
writeSpatial("ST_Intersects", col, GEOMETRY_FACTORY.toGeometry(env));
194+
writeSpatial(ST_INTERSECTS, col, GEOMETRY_FACTORY.toGeometry(env));
180195
return extraData;
181196
}
182197

@@ -185,7 +200,7 @@ public Object visit(BBOX filter, Object extraData) {
185200
* {@code extractGeomColumnName}/{@code tryExtractEnvelope} recognize for
186201
* bbox/Z2/XZ2 pushdown (ST_Disjoint is deliberately never pushed there). */
187202
private void writeSpatial(String function, String col, Geometry geom) {
188-
write(function + "(ST_GeomFromBinary(" + quoteIdent(col) + "),"
203+
write(function + "(" + ST_GEOM_FROM_BINARY + "(" + quoteIdent(col) + "),"
189204
+ " " + geomFromText(geom) + ")");
190205
}
191206

@@ -228,7 +243,7 @@ private void writeDWithin(String col, Geometry refGeom, double distanceMeters) {
228243
String refWkt = wkt.write(refGeom).replace("'", "''");
229244
String distanceCheck = String.format(Locale.ROOT, "%s <= %.0f",
230245
sphericalDistanceSql(col, refGeom, refWkt), distanceMeters);
231-
write("ST_Intersects(ST_GeomFromBinary(" + quoteIdent(col) + "),"
246+
write(ST_INTERSECTS + "(" + ST_GEOM_FROM_BINARY + "(" + quoteIdent(col) + "),"
232247
+ " " + geomFromText(GEOMETRY_FACTORY.toGeometry(outer)) + ")"
233248
+ " AND " + distanceCheck);
234249
}
@@ -251,15 +266,15 @@ private void writeBeyond(String col, Geometry refGeom, double distanceMeters) {
251266
* slightly from the geodesic nearest at mid latitudes).
252267
*/
253268
private String sphericalDistanceSql(String col, Geometry refGeom, String refWkt) {
254-
String geomExpr = "ST_GeomFromBinary(" + quoteIdent(col) + ")";
255-
String refExpr = "ST_GeometryFromText('" + refWkt + "')";
269+
String geomExpr = ST_GEOM_FROM_BINARY + "(" + quoteIdent(col) + ")";
270+
String refExpr = ST_GEOMETRY_FROM_TEXT + "('" + refWkt + "')";
256271
if (refGeom instanceof Point) {
257-
return "ST_Distance(to_spherical_geography(" + geomExpr + "),"
258-
+ " to_spherical_geography(" + refExpr + "))";
272+
return ST_DISTANCE + "(" + TO_SPHERICAL_GEOGRAPHY + "(" + geomExpr + "),"
273+
+ " " + TO_SPHERICAL_GEOGRAPHY + "(" + refExpr + "))";
259274
}
260-
String np = "geometry_nearest_points(" + geomExpr + ", " + refExpr + ")";
261-
return "ST_Distance(to_spherical_geography(" + np + "[1]),"
262-
+ " to_spherical_geography(" + np + "[2]))";
275+
String np = GEOMETRY_NEAREST_POINTS + "(" + geomExpr + ", " + refExpr + ")";
276+
return ST_DISTANCE + "(" + TO_SPHERICAL_GEOGRAPHY + "(" + np + "[1]),"
277+
+ " " + TO_SPHERICAL_GEOGRAPHY + "(" + np + "[2]))";
263278
}
264279

265280
// ── Temporal ──────────────────────────────────────────────────────────────

0 commit comments

Comments
 (0)