Class DuckDBFilterToSQL

Object
FilterToSQL
DuckDBFilterToSQL
All Implemented Interfaces:
ExpressionVisitor, FilterVisitor
Direct Known Subclasses:
GeoParquetFilterToSQL

public class DuckDBFilterToSQL extends FilterToSQL
FilterToSQL implementation for DuckDB with comprehensive spatial support.

This class handles conversion of GeoTools filters to DuckDB-compatible SQL spatial functions and provides locale-independent geometry literal encoding to avoid parsing issues in different system locales.

Features:

  • Spatial Operations: Supports all standard spatial predicates including BBOX, Contains, Crosses, Disjoint, Equals, Intersects, Overlaps, Touches, Within, DWithin, and Beyond
  • Locale Independence: Uses WKB (Well-Known Binary) encoding for geometry literals to avoid locale-specific decimal separator parsing issues (e.g., comma vs dot)
  • DuckDB Compatibility: Leverages DuckDB's ST_GeomFromHEXEWKB function for reliable geometry deserialization
  • Function Support: Extensible support for string, math, date, array, and geometry functions

Locale Handling:

Traditional WKT (Well-Known Text) geometry encoding can fail in locales that use comma as the decimal separator (e.g., Italian, German, French) because DuckDB may misinterpret coordinate separators in strings like "POLYGON ((-10 0, 0 0, ...))" where "0, 0" could be parsed as "0,0" (zero with decimal comma). This implementation avoids this issue entirely by using binary WKB encoding with hexadecimal representation.

See Also:
  • Constructor Details

    • DuckDBFilterToSQL

      public DuckDBFilterToSQL()
  • Method Details

    • visitLiteralGeometry

      protected void visitLiteralGeometry(Literal expression) throws IOException
      Converts a geometry literal to DuckDB SQL using locale-independent WKB encoding.

      This method uses DuckDB's ST_GeomFromHEXEWKB function with hexadecimal-encoded WKB (Well-Known Binary) data instead of the traditional WKT (Well-Known Text) approach. This ensures consistent behavior across different system locales that may use different decimal separators.

      In locales such as Italian (it_IT), German (de_DE), or French (fr_FR), the decimal separator is a comma instead of a dot. When using WKT encoding like:

      POLYGON ((-10 0, 0 0, 0 10, -10 10, -10 0))
      DuckDB's parser may misinterpret the coordinate separator "0, 0" as a decimal number "0,0" with a decimal comma, causing parsing errors like:
      Invalid Input Error: Expected character: ')' at position '17' near: 'POLYGON ((-10 0, 0'
      WKB Solution:

      Using WKB encoding completely bypasses text parsing of coordinates since the geometry is represented as binary data. The hexadecimal representation is locale-independent and works consistently across all systems.

      Overrides:
      visitLiteralGeometry in class FilterToSQL
      Parameters:
      expression - the geometry literal expression to convert
      Throws:
      IOException - if there's an error writing to the output stream
      See Also:
    • createFilterCapabilities

      protected FilterCapabilities createFilterCapabilities()
      Description copied from class: FilterToSQL
      Sets the capabilities of this filter.
      Overrides:
      createFilterCapabilities in class FilterToSQL
      Returns:
      FilterCapabilities for this Filter
    • visitBinarySpatialOperator

      protected Object visitBinarySpatialOperator(BinarySpatialOperator filter, PropertyName property, Literal geometry, boolean swapped, Object extraData)
      Description copied from class: FilterToSQL
      Handles the common case of a PropertyName,Literal geometry binary spatial operator.
      Overrides:
      visitBinarySpatialOperator in class FilterToSQL
    • visitBinarySpatialOperator

      protected Object visitBinarySpatialOperator(BinarySpatialOperator filter, Expression leftExp, Expression rightExpt, Object extraData)
      Description copied from class: FilterToSQL
      Handles the more general case of two generic expressions.

      The most common case is two PropertyName expressions, which happens during a spatial join.

      Overrides:
      visitBinarySpatialOperator in class FilterToSQL
    • visitBBOX

      protected Object visitBBOX(BBOX filter, Expression leftExp, Expression rightExpt, Object extraData)
    • visitDistanceBufferOperator

      protected Object visitDistanceBufferOperator(DistanceBufferOperator filter, Expression leftExp, Expression rightExpt, Object extraData)
    • write

      protected void write(String fmt, Object... args)