Skip to content

API Reference parrot.tools.dataset_manager.spatial.compiler

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.tools.dataset_manager.spatial.compiler

View source

SpatialCompiler — compile and execute spatial filter queries (FEAT-219).

Two execution paths, selected by getattr(source, "driver", None):

  1. Engine push-down (pg, bigquery): emits an ST_DWITHIN + ST_AsGeoJSON SQL query using driver-specific dialect templates. compile() is I/O-free and syrupy-snapshotable. execute() runs via AsyncDB.

  2. Pandas bbox fallback (mysql, unknown, InMemorySource): derives a bounding box from (point, radius), pushes a BETWEEN predicate, fetches box survivors, then refines to the exact circle with vectorized haversine (numpy).

Design principles (FEAT-219 spec §2):

  • compile() is deterministic and I/O-free — no DB calls.
  • execute() is async and performs all I/O.
  • geodesic is declared on the profile and verified at compile time; the true path (geodesic vs spherical-approx) is returned in CompiledQuery.geodesic.
  • Never route through DatasetEntry.materialize / Redis Parquet cache (spec G4).
  • TASK-1437 resolved NO-GO for Ibis — two hand-written SQL dialect templates are used.

Class CompiledQuery

Immutable result of SpatialCompiler.compile().

All fields are set at compile time; no I/O occurs during construction.

Parameter Type Default Description
sql Optional[str]
driver str
path str
geodesic bool
profile_dataset str
point tuple
radius_m float
property_cols List[str]
description_template str
bbox Optional[tuple] None
lat_col Optional[str] None
lng_col Optional[str] None
geom_col Optional[str] None
cap int _DEFAULT_ENGINE_CAP
geodesic_warning str ''
count_sql Optional[str] None
extra Dict[str, Any] dict()

Fields:

Field Type Default Description
sql Optional[str] The SQL string to execute (engine push-down) or None for the in-memory fallback path.
driver str Normalised AsyncDB driver name ("pg", "bigquery", "mysql", or other).
path str "engine" for pg/bigquery push-down; "pandas" for bbox fallback.
geodesic bool True if the executed path is geodesic (native geography column or BigQuery GEOGRAPHY). False = spherical-approximate haversine.
profile_dataset str The dataset name this compiled query is for.
point tuple (lat, lng) of the query centre point (used in haversine refine).
radius_m float Search radius converted to metres.
property_cols List[str] Column names to include in GeoJSON feature properties.
description_template str Python str.format_map template for feature description.
bbox Optional[tuple] None (min_lat, max_lat, min_lng, max_lng) for the pandas path (used in the BETWEEN predicate). None for the engine path.
lat_col Optional[str] None Latitude column name used in the fallback path. None for engine path.
lng_col Optional[str] None Longitude column name used in the fallback path. None for engine path.
geom_col Optional[str] None Geometry/geography column name for the engine path.
cap int _DEFAULT_ENGINE_CAP Maximum features to return for this dataset.
geodesic_warning str '' Non-empty string if a geodesic mismatch was detected.
count_sql Optional[str] None
extra Dict[str, Any] field(default_factory=dict)

Alias DatasetSpatialProfile

Re-exported from parrot.tools.dataset_manager.spatial.contracts.DatasetSpatialProfile.

Class SpatialCompiler

Stateless spatial filter compiler and executor.

compile() is pure (I/O-free, syrupy-snapshotable). execute() is async and performs DB/DataFrame I/O.

Method SpatialCompiler.compile

Compile a spatial filter spec into a CompiledQuery.

I/O-free: this method does not touch the database. The returned CompiledQuery is deterministic for a given (spec, profile, driver) triple and can be snapshot-tested without a DB connection.

compile(spec: 'SpatialFilterSpec', profile: 'DatasetSpatialProfile', source: Any = None, cap: int = _DEFAULT_ENGINE_CAP) -> CompiledQuery

Parameter Type Default Description
spec 'SpatialFilterSpec' The spatial filter request.
profile 'DatasetSpatialProfile' The dataset's spatial profile.
source Any None The DataSource for this dataset (TableSource or InMemorySource). Used only to read source.driver and source.table — no I/O. May be None for pure compile-time tests.
cap int _DEFAULT_ENGINE_CAP Maximum number of features to return for this dataset.

Returns: CompiledQuery — CompiledQuery populated for either the engine push-down or pandas fallback path.

Method SpatialCompiler.execute

Execute a compiled query against the given DataSource.

Routes to the engine push-down or pandas fallback path based on compiled.path. Does NOT route through DatasetEntry.materialize (spec G4).

execute(compiled: CompiledQuery, source: Any) -> Tuple[List[dict], int]

Parameter Type Default Description
compiled CompiledQuery Output of compile().
source Any The DataSource for this dataset.

Returns: Tuple[List[dict], int] — Tuple of (features, true_count) where true_count is the total number of matching rows BEFORE any cap is applied. When the engine path has a count_sql, true_count may exceed len(features).

Alias SpatialFilterSpec

Re-exported from parrot.tools.dataset_manager.spatial.contracts.SpatialFilterSpec.

Attribute logger

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally