Skip to content

API Reference parrot.tools.dataset_manager.filtering.compiler

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

parrot.tools.dataset_manager.filtering.compiler

View source

Filter Compiler for FEAT-225 Module 3.

Translates a :class:FilterCondition into either:

  • A SQL WHERE fragment (for SQL-backed TableSource / QuerySlugSource datasets), following the same predicate style used by TableSource._build_filter_clause.
  • A pandas boolean mask (for in-memory DataFrame datasets).

Both compile_where and compile_pandas are deterministic and I/O-free. Execution — iterating datasets and deciding which path to take — is the responsibility of :meth:DatasetManager.apply_filters (TASK-1467).

Note: from __future__ import annotations is intentionally omitted so that Pydantic v2 resolves type hints at class-definition time without a manual model_rebuild() call.

Class FilterCompiler

Stateless compiler that translates FilterCondition to SQL or pandas.

All methods are pure (no I/O, no state) so instances are reusable and easily unit-testable.

SQL dialect notes:

  • Column names are always double-quoted via _quote_column to prevent SQL injection through column name interpolation.
  • Values are escaped with single-quoting (strings) or left as literals (numbers), matching the TableSource._build_filter_clause pattern.
  • ne emits <>; not_in emits NOT IN.
  • range emits BETWEEN … AND ….

Method FilterCompiler.compile_where

Translate a FilterCondition to a SQL WHERE fragment.

Reserved for future in-database push-down (SQL path). The current DatasetManager.apply_filters implementation materializes datasets to pandas first and uses compile_pandas instead.

TODO(FEAT-225-SQL-PUSHDOWN): Wire this method into apply_filters

once in-database predicate push-down is implemented. Currently dead

code — all filtering goes through the pandas path for reliability.

Returns a (fragment, params) pair. fragment is a safe SQL predicate string ready to be AND-ed into a WHERE clause. Column names are double-quoted to prevent SQL injection. params is currently empty (values are inlined via escaping), reserved for future parameterised-query support.

compile_where(column: str, condition: FilterCondition) -> Tuple[str, List[Any]]

Parameter Type Default Description
column str The column name to filter on.
condition FilterCondition The filter condition (op + value).

Returns: Tuple[str, List[Any]] — Tuple of (sql_fragment, params_list).

Raises:

  • ValueError — When condition.op is not supported, range value is malformed, or the column name contains a double-quote character.

Method FilterCompiler.compile_pandas

Translate a FilterCondition to a pandas boolean Series (mask).

compile_pandas(df: pd.DataFrame, column: str, condition: FilterCondition) -> pd.Series

Parameter Type Default Description
df pd.DataFrame The DataFrame to build the mask against.
column str The column name to filter on.
condition FilterCondition The filter condition (op + value).

Returns: pd.Series — Boolean Series with the same index as df.

Raises:

  • ValueError — When column is not in df.columns, when condition.op is unsupported, or when a range value is malformed.

Alias FilterCondition

Re-exported from parrot.tools.dataset_manager.filtering.contracts.FilterCondition.

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