Skip to content

Commit 53a2a63

Browse files
authored
Fix spatial filter WKB rendering (#6)
1 parent a9699c1 commit 53a2a63

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎vgi/src/main/java/farm/query/vgi/pushdown/PushdownFilters.java‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ private static String expressionSql(FilterExpression expression) {
229229
case FilterExpression.ColumnRef value -> quoteIdentifier(value.columnName());
230230
case FilterExpression.FieldRef value -> expressionSql(value.expression()) + "."
231231
+ quoteIdentifier(value.fieldName());
232-
case FilterExpression.Literal value -> sqlLiteral(value.value());
232+
case FilterExpression.Literal value -> literalSql(value);
233233
case FilterExpression.Comparison value -> "(" + expressionSql(value.left()) + " "
234234
+ value.op().symbol() + " " + expressionSql(value.right()) + ")";
235235
case FilterExpression.BooleanExpression value -> "(" + value.children().stream()
@@ -271,10 +271,24 @@ private static String functionName(Object function) {
271271
return standard.name().toLowerCase();
272272
}
273273
FilterIdentity identity = (FilterIdentity) function;
274-
if (identity.equals(new FilterIdentity("duckdb.spatial", "intersects_extent", 1))) return "&&";
274+
if (identity.equals(new FilterIdentity("duckdb.spatial", "intersects_extent", 1))) {
275+
return "st_intersects_extent";
276+
}
275277
return identity.namespace() + "." + identity.name();
276278
}
277279

280+
private static String literalSql(FilterExpression.Literal literal) {
281+
if (isWkb(literal.field()) && literal.value() instanceof byte[] bytes) {
282+
return "ST_GeomFromHEXWKB('" + java.util.HexFormat.of().formatHex(bytes) + "')";
283+
}
284+
return sqlLiteral(literal.value());
285+
}
286+
287+
private static boolean isWkb(org.apache.arrow.vector.types.pojo.Field field) {
288+
return field != null && field.getMetadata() != null
289+
&& "geoarrow.wkb".equals(field.getMetadata().get("ARROW:extension:name"));
290+
}
291+
278292
/**
279293
* Format the filters as a human-readable {@code AND}-joined SQL-like
280294
* string with values inlined. Used by diagnostic fixtures like

‎vgi/src/test/java/farm/query/vgi/pushdown/PushdownFiltersV2Test.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,31 @@ void identitiesAreStrictEvenForAdvisoryPredicates() {
253253
() -> new FilterIdentity("example.filters", "custom", 0));
254254
}
255255

256+
@Test
257+
void spatialExpressionsRenderWkbAsGeometry() {
258+
Field geometry = new Field("geom", new FieldType(true, new ArrowType.Binary(), null,
259+
Map.of("ARROW:extension:name", "geoarrow.wkb",
260+
"ARROW:extension:metadata", "{}")), null);
261+
Field literal = new Field("value_0", new FieldType(true, new ArrowType.Binary(), null,
262+
Map.of("ARROW:extension:name", "geoarrow.wkb",
263+
"ARROW:extension:metadata", "{}")), null);
264+
String expression = "{\"node\":\"call\",\"function\":{"
265+
+ "\"namespace\":\"duckdb.spatial\",\"name\":\"intersects_extent\",\"version\":1},"
266+
+ "\"arguments\":[" + column("geom")
267+
+ ",{\"node\":\"literal\",\"value_ref\":0}]}";
268+
PushdownFiltersDecoder.Capabilities capabilities = new PushdownFiltersDecoder.Capabilities(
269+
java.util.Set.of(new FilterIdentity("duckdb.spatial", "intersects_extent", 1)),
270+
java.util.Set.of(), Map.of());
271+
PushdownFilters filters = PushdownFiltersDecoder.decode(
272+
batch(snapshot(predicate("spatial", "required", expression)),
273+
List.of(literal), List.of(new byte[] {1, 2, (byte) 0xff})),
274+
new Schema(List.of(geometry)), List.of(), capabilities);
275+
276+
assertEquals(List.of("st_intersects_extent(\"geom\", "
277+
+ "ST_GeomFromHEXWKB('0102ff'))"),
278+
filters.expressionPredicates());
279+
}
280+
256281
@Test
257282
void configuredLimitsAndFullUint64RevisionsAreEnforced() {
258283
String longId = "x".repeat(129);

0 commit comments

Comments
 (0)