gfw.common.query#
Utilities for repsenting SQL queries in a structured, reusable way.
- This module defines the
Queryclass, which provides: Automatic detection and setup of the Jinja2 environment using
EnvironmentLoader.Helpers for rendering and formatting SQL queries.
A utility to expand
typing.NamedTupleschemas intoSELECTclauses.Built-in conversion for datetime fields to BigQuery-compatible Unix timestamps.
Support for dependency injection of custom
jinja2.Environmentinstances.
This design centralizes SQL generation, improves testability, and ensures consistency across queries within pipelines or shared libraries.
Classes
Abstract base class for SQL queries rendered via Jinja2 templates. |
- class Query[source]#
Abstract base class for SQL queries rendered via Jinja2 templates.
- Subclasses must define:
A Jinja2 template filename (via
template_filenameproperty).Variables to render into the template (via
template_varsproperty).
The base class handles Jinja2 environment setup, SQL rendering, and optional query formatting.
- classmethod datetime_to_timestamp(field)[source]#
Returns SQL expression to cast a datetime field to TIMESTAMP.
- property output_type: type[NamedTuple] | None#
Optional schema for the query results.
Subclasses may override this property to return a
NamedTupletype that models the expected rows. The fields of this type are automatically expanded into the generatedSELECTclause.- Returns:
A subclass of
NamedTupledescribing the result schema, orNoneif no schema is defined.
- abstract property template_filename: str#
Name of the Jinja2 template file for this query.
Subclasses must override this property to point to the SQL template file (relative to the
DEFAULT_JINJA_FOLDER).- Returns:
The filename of the template (e.g.,
messages.sql.j2).
- abstract property template_vars: dict[str, str]#
Variables to inject into the Jinja2 template.
Subclasses must override this property to return the dictionary of template context variables (e.g.,
start_date,end_date, …).- Returns:
A dictionary of key-value pairs for template rendering.
- property top_level_package: str#
Detects the top-level package name for this query class.
This is used to locate the query templates when using
EnvironmentLoader.- Returns:
The name of the top-level package as a string.
- property jinja_env: Environment#
Returns or lazily creates a Jinja2 environment for this query.
If no environment was explicitly set with
with_env(), one is created usingfrom_package()and the detected package name.
- with_env(env)[source]#
Injects a custom
jinja2.Environmentinto this query.This method enables dependency injection for testing or when using shared environments. Returns
selffor fluent chaining.- Parameters:
env (Environment) – A configured
jinja2.Environment.- Returns:
self (the same query instance).
- Return type:
- get_select_fields(convert_datetime_to_timestamp=False)[source]#
Builds the
SELECTclause fields from the output schema.- Parameters:
convert_datetime_to_timestamp (bool) – If True, fields typed as
datetimeare automatically cast to Unix float timestamps (viadatetime_to_timestamp()). All other fields are passed through.- Returns:
A comma-separated string of
SELECTfields.- Return type: