SQL/MM compliant spatial functions for creating, measuring, and analyzing geographic data with practical examples.
Apache Pinot provides a comprehensive set of geospatial functions that follow the SQL/MM specification with the ST_ prefix. These functions enable you to create, manipulate, measure, and analyze spatial data.
-- Create San Francisco pointSELECT ST_Point(-122.4194, 37.7749) as sf_point;-- Use in query to find nearby locationsSELECT store_name FROM stores WHERE ST_Distance(location, ST_Point(-122.4194, 37.7749)) < 1000;
Creates geography from Well-Known Text with spherical calculations.
Copy
Ask AI
ST_GeogFromText(String wkt) → Geography
Examples:
Copy
Ask AI
-- Create geography for accurate distance calculationsSELECT ST_GeogFromText('POINT(-122.4194 37.7749)') as sf_geography;-- Use with ST_Distance for meter-based resultsSELECT ST_Distance( ST_GeogFromText('POINT(-122.4194 37.7749)'), ST_GeogFromText('POINT(-74.0060 40.7128)')) as nyc_to_sf_meters;
Creates geography from Well-Known Binary representation.
Copy
Ask AI
ST_GeogFromWKB(bytes wkb) → Geography
Examples:
Copy
Ask AI
-- Create geography from WKB data columnSELECT ST_GeogFromWKB(wkb_geography_column) FROM geo_table;-- Convert point to WKB and back to geographySELECT ST_Point(-122, 37) AS point, ST_GeogFromWKB( ST_AsBinary(ST_Point(-122, 37)) ) AS geography_from_wkbFROM ignoreMe;
-- Create geography for accurate calculationsSELECT ST_GeogFromGeoJson('{"type":"Point","coordinates":[-122.4194,37.7749]}');-- Use in distance calculationsSELECT ST_Distance( location_geography, ST_GeogFromGeoJson('{"type":"Point","coordinates":[-122.4194,37.7749]}')) as distance_meters FROM stores;
For Geometry: Returns 2D Cartesian distance in coordinate units For Geography: Returns great-circle distance in meters
Geography Limitation: When using geography types, ST_Distance only accepts POINT geometries. For other geometry types (LINESTRING, POLYGON), use geometry types instead.
Examples:
Copy
Ask AI
-- Distance with geometry types (any geometry supported)SELECT ST_Distance( ST_GeomFromText('LINESTRING(-122.4194 37.7749, -122.4094 37.7849)'), ST_GeomFromText('POINT(-74.0060 40.7128)')) as coordinate_distance;-- Distance with geography types (points only)SELECT ST_Distance( ST_GeogFromText('POINT(-122.4194 37.7749)'), ST_GeogFromText('POINT(-74.0060 40.7128)')) as meters_distance;-- Find nearby stores (indexed query)SELECT store_name, addressFROM stores WHERE ST_Distance(location, ST_Point(-122.4194, 37.7749)) < 5000ORDER BY ST_Distance(location, ST_Point(-122.4194, 37.7749));
Calculates the area of a polygon geometry or geography.
Copy
Ask AI
ST_Area(Geometry/Geography g) → double
For Geometry: Returns 2D Euclidean area in coordinate units² For Geography: Returns area in square meters using spherical modelExamples:
Copy
Ask AI
-- Area of a polygon in coordinate units²SELECT ST_Area(ST_GeomFromText('POLYGON((-122.5 37.7, -122.4 37.7, -122.4 37.8, -122.5 37.8, -122.5 37.7))'));-- Area in square meters using geographySELECT ST_Area(ST_GeogFromText('POLYGON((-122.5 37.7, -122.4 37.7, -122.4 37.8, -122.5 37.8, -122.5 37.7))'));-- Calculate area of all regionsSELECT region_name, ST_Area(boundary_geography) as area_sqm FROM geographic_regions ORDER BY area_sqm DESC;
Returns true if every point of g1 is a point of g2, and the interiors of the two geometries have at least one point in common.Examples:
Copy
Ask AI
-- Find points within a regionSELECT location_name FROM locations WHERE ST_Within( location_point, ST_GeomFromText('POLYGON((-122.5 37.7, -122.4 37.7, -122.4 37.8, -122.5 37.8, -122.5 37.7))'));-- Check if delivery addresses are within service areaSELECT customer_id, addressFROM customers WHERE ST_Within(delivery_address, service_boundary);-- **Index Usage**: First argument must be column, second must be literalWHERE ST_Within(point_column, '<polygon_literal>') -- Uses index
Converts geometry or geography to Well-Known Text (WKT).
Copy
Ask AI
ST_AsText(Geometry/Geography g) → string
Examples:
Copy
Ask AI
-- Convert geometry to readable textSELECT ST_AsText(location_column) as wkt_location FROM spatial_data;-- Debug spatial queriesSELECT store_name, ST_AsText(location) as location_wktFROM stores WHERE ST_Distance(location, ST_Point(-122.4194, 37.7749)) < 1000;
Converts geometry or geography to Well-Known Binary (WKB).
Copy
Ask AI
ST_AsBinary(Geometry/Geography g) → bytes
Examples:
Copy
Ask AI
-- Convert to binary format for storageSELECT ST_AsBinary(location_geom) as wkb_data FROM locations;-- Export binary dataSELECT id, ST_AsBinary(boundary) as boundary_wkb FROM regions WHERE region_type = 'administrative';
-- Convert point to GeoJSONSELECT ST_AsGeoJson(ST_Point(-122.4194, 37.7749));-- Returns: {"type":"Point","coordinates":[-122.4194,37.7749],"crs":{"type":"name","properties":{"name":"EPSG:0"}}}-- Export data for web applicationsSELECT store_name, ST_AsGeoJson(location) as geojson_location, categoryFROM stores WHERE city = 'San Francisco';-- Create GeoJSON FeatureCollection-ready outputSELECT json_object( 'type', 'Feature', 'geometry', json(ST_AsGeoJson(location)), 'properties', json_object('name', store_name, 'category', category)) as geojson_featureFROM stores;
Converts a Geometry object to spherical geography.
Copy
Ask AI
toSphericalGeography(Geometry g) → Geography
Examples:
Copy
Ask AI
-- Convert geometry to geography for accurate distance calculationsSELECT toSphericalGeography(ST_GeomFromText('POINT(-122.4194 37.7749)'));-- Use in transform functions"transformFunction": "toSphericalGeography(stPoint(longitude, latitude))"-- Convert existing geometry columnSELECT location_name, ST_Distance( toSphericalGeography(geometry_location), toSphericalGeography(ST_Point(-122.4194, 37.7749)) ) as distance_metersFROM locations;
Converts a spherical geography object to geometry.
Copy
Ask AI
toGeometry(Geography g) → Geometry
Examples:
Copy
Ask AI
-- Convert geography back to geometrySELECT toGeometry(geography_column) FROM spatial_data;-- Use when you need faster calculations over small areasSELECT location_nameFROM locations WHERE ST_Distance( toGeometry(geography_location), toGeometry(reference_geography)) < 0.01; -- Approximate distance in degrees
Combines multiple geometries into a single MULTI geometry.
Copy
Ask AI
ST_Union(geometry[] g1_array) → Geometry
This aggregate function returns a MULTI geometry or NON-MULTI geometry from a set of geometries. It ignores NULL geometries.Examples:
Copy
Ask AI
-- Combine all store locations into a MultiPointSELECT ST_Union(ARRAY_AGG(location)) as all_store_locationsFROM stores WHERE city = 'San Francisco';-- Create union of all delivery zonesSELECT region, ST_Union(ARRAY_AGG(delivery_polygon)) as combined_delivery_areaFROM delivery_zones GROUP BY region;-- Combine geometries by categorySELECT store_category, ST_AsText(ST_Union(ARRAY_AGG(location))) as combined_locationsFROM stores GROUP BY store_category;
These functions automatically leverage geospatial indexes when properly configured:
Function
Index Usage
Requirements
ST_Distance
FILTER_H3_INDEX
Column as first arg, literal as second, used in range condition
ST_Within
INCLUSION_FILTER_H3_INDEX
Column as first arg, literal as second
ST_Contains
INCLUSION_FILTER_H3_INDEX
Literal as first arg, column as second
Index-Optimized Query Patterns:
Copy
Ask AI
-- These queries use the H3 index:WHERE ST_Distance(location_column, ST_Point(-122, 37)) < 5000WHERE ST_Within(location_column, ST_GeomFromText('POLYGON(...)')) WHERE ST_Contains(ST_GeomFromText('POLYGON(...)'), location_column)-- These queries do NOT use the index:WHERE ST_Distance(ST_Point(-122, 37), location_column) < 5000 -- Arguments reversedWHERE ST_Within(ST_GeomFromText('POLYGON(...)'), location_column) -- Arguments reversed
-- For distance-based queriesST_Distance + geography types = Most accurate (points only)ST_Distance + geometry types = Faster, accepts any geometry type-- For containment queries ST_Within = Point-in-polygon testingST_Contains = Polygon-contains-point testing (reverse of ST_Within)-- For data exportST_AsGeoJson = Modern web applicationsST_AsText = Debugging and human-readable outputST_AsBinary = Compact storage and transmission