Skip to content

API Reference parrot.tools.dataset_manager.sources.table

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

parrot.tools.dataset_manager.sources.table

View source

TableSource — schema-prefetch DataSource for database tables.

On registration, prefetch_schema() runs a driver-aware INFORMATION_SCHEMA query to retrieve column names and types without materializing any rows. The LLM receives full schema awareness before any data is fetched.

At fetch time, the LLM provides a SQL statement which is validated to reference the registered table before execution.

Alias DataSource

Re-exported from parrot.tools.dataset_manager.sources.base.DataSource.

Class TableSource

DataSource for a database table with INFORMATION_SCHEMA schema prefetch.

Registers a table reference (e.g. "troc.finance_visits_details") for a given AsyncDB driver. On registration (via add_table_source), prefetch_schema() is called to retrieve column names and data types from INFORMATION_SCHEMA — no rows are fetched.

The LLM can then build a SQL query using the schema and call fetch(sql=...) to materialize actual data. The SQL is validated to reference self.table before execution.

Parameter Type Default Description
table str Fully-qualified table name, e.g. "public.orders" or "troc.finance_visits_details". For BigQuery use "dataset.table".
driver str AsyncDB driver name, e.g. "pg", "bigquery", "mysql".
dsn Optional[str] None Optional DSN string. If None, credentials are resolved from navconfig.
credentials Optional[Dict] None Optional credentials dict. Takes priority over navconfig defaults when dsn is also None.
strict_schema bool True If True (default), prefetch_schema() failures raise and registration fails. If False, failures log a warning and register with empty schema.
permanent_filter Optional[Dict[str, Any]] None Optional dict of equality conditions that are always injected as a WHERE clause into every fetch() SQL. Scalar values produce col = 'val'; list/tuple values produce col IN ('a', 'b'). Column names are validated against _SAFE_IDENTIFIER_RE; values are safely escaped.
allowed_columns Optional[List[str]] None Optional list of column names to restrict access. When set, only these columns appear in the schema, describe() output, guide, and metadata. SQL queries referencing other columns (or SELECT *) are rejected at fetch() time.

Attributes:

Attribute Type Description
schema_name Optional[str] Return the schema/dataset prefix, or None if unqualified. For BigQuery this is the dataset name (e.g. census_data), for PostgreSQL/MySQL it is the schema (e.g. public).
short_table_name str Return the unqualified table name (part after the last dot).
allowed_columns Optional[List[str]] Return the allowed columns list, or None if unrestricted.
cache_key str Stable Redis cache key for this table source. Format: table:{driver}:{table} or table:{driver}:{table}:f={hash} when a permanent filter is set, with an optional :ac={hash} suffix when allowed_columns is set.

Method TableSource.prefetch_schema

Fetch column names and types from INFORMATION_SCHEMA.

Runs a driver-specific metadata query that returns no data rows. Result is stored in self._schema and returned.

prefetch_schema() -> Dict[str, str]

Returns: Dict[str, str] — Dict mapping column_name → data_type string.

Method TableSource.prefetch_row_count

Estimate the row count for this table via COUNT(*).

The result is stored in self._row_count_estimate and surfaced to the LLM so it can decide whether to use aggregation queries instead of SELECT *.

When a permanent_filter is set the count reflects only the filtered subset, matching what the LLM would see at fetch time.

prefetch_row_count() -> Optional[int]

Returns: Optional[int] — Estimated row count, or None if the query fails.

Method TableSource.fetch

Execute a SQL query against the registered table.

The SQL is validated to contain the table name as a whole word (case-insensitive, word-boundary check) to prevent the LLM from executing arbitrary queries outside the registered scope.

When the registered table is schema-qualified (e.g. schema.table), SQL that references just the table name is accepted and automatically rewritten to use the fully-qualified name before execution.

.. note:: This is an allowlist heuristic, not a full SQL parser. It prevents most accidental misuse but is not a security boundary. Agent-level permissions control which sources are accessible.

fetch(sql: Optional[str] = None, **params) -> pd.DataFrame

Parameter Type Default Description
sql Optional[str] None The SQL statement to execute. Required for TableSource. Build it using the columns from prefetch_schema() / describe().
**params {} Additional params (ignored; SQL must be fully formed).

Returns: pd.DataFrame — DataFrame with the query results.

Raises:

  • ValueError — If sql is not provided or does not reference self.table.
  • RuntimeError — If the query fails at the database level.

Method TableSource.describe

Return a human-readable description for the LLM guide.

describe() -> str

Returns: str — String describing the table, driver, number of known columns, and estimated row count with size warning if applicable.

Function dialect_hint

Return concise SQL-dialect guidance for driver (empty if unknown).

The driver is normalized first, so aliases like bq/postgres/ mariadb resolve to their canonical dialect hint.

dialect_hint(driver: str) -> str

Parameter Type Default Description
driver str

Returns: str

Alias lazy_import

Re-exported from parrot._imports.lazy_import.

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