climate_ref.results._query
#
Filter and Select builders for metric-value queries.
This module is the single source of truth for how metric values are filtered and joined.
Both the typed facade (climate_ref.results.values.Reader) and any power-user / API
caller build on the select_* functions here, so the filter/join semantics live in exactly
one place. This mirrors the existing house style of module-level query functions that take a
Session (see climate_ref.models.execution.get_execution_group_and_latest_filtered).
The functions return a SQLAlchemy Select and never touch a Session -- callers add
their own pagination, wrap in exists(), or hand them to the facade to materialise.
MetricValueFilter
#
Declarative filter over metric values.
Every field is optional; None/empty means "do not constrain on this axis". The same
object drives scalar and series queries, frame conversion and facet collection, so a filter
is written once and reused across output shapes.
Two styles of diagnostic/provider filtering are offered deliberately:
diagnostic_slug/provider_slugare exact matches, for API path-scoped queries (/diagnostics/{provider_slug}/{diagnostic_slug}/values).diagnostic_contains/provider_containsare case-insensitive substring matches (OR-combined), for CLI-style search.
Keeping them separate avoids the Diagnostic.slug vs Diagnostic.name divergence that
exists today between the CLI and the ref-app API.
Source code in packages/climate-ref/src/climate_ref/results/_query.py
diagnostic_contains = attrs.field(default=None, converter=_as_str_tuple)
class-attribute
instance-attribute
#
Case-insensitive substring matches on diagnostic slug (OR-combined), for CLI-style search.
diagnostic_slug = None
class-attribute
instance-attribute
#
Exact-match diagnostic slug, for API path-scoped queries.
dimensions = None
class-attribute
instance-attribute
#
CV dimension filters keyed by registered dimension name; a string is equality, a sequence is IN.
exclude_ids = None
class-attribute
instance-attribute
#
Exclude these value ids; ignored when isolate_ids is set.
execution_group_ids = None
class-attribute
instance-attribute
#
Restrict to values produced by executions belonging to these groups.
execution_ids = None
class-attribute
instance-attribute
#
Restrict to values produced by these executions.
include_retracted = False
class-attribute
instance-attribute
#
Include values produced by retracted executions.
isolate_ids = None
class-attribute
instance-attribute
#
Restrict to exactly these value ids; takes precedence over exclude_ids, matching the API.
promoted_only = True
class-attribute
instance-attribute
#
Restrict to execution groups at the diagnostic's currently promoted version.
provider_contains = attrs.field(default=None, converter=_as_str_tuple)
class-attribute
instance-attribute
#
Case-insensitive substring matches on provider slug (OR-combined), for CLI-style search.
provider_slug = None
class-attribute
instance-attribute
#
Exact-match provider slug, for API path-scoped queries.
reference_only = None
class-attribute
instance-attribute
#
Series-only: True for observation/reference series, False for model series.
dimension_clauses(entity)
#
Build validated SQLAlchemy clauses for the dynamic CV dimension columns.
Raises:
| Type | Description |
|---|---|
KeyError
|
If a key is not a registered CV dimension on |
Source code in packages/climate-ref/src/climate_ref/results/_query.py
count_values(session, stmt)
#
Total row count for a builder's Select, ignoring any offset/limit. For pagination.
Source code in packages/climate-ref/src/climate_ref/results/_query.py
latest_execution_for_group(session, execution_group_id)
#
Return the most recent Execution for a group.
This is the read-side primitive behind the API's /executions/{group_id}/values behaviour
of defaulting to the latest execution when no execution_id is supplied.
Source code in packages/climate-ref/src/climate_ref/results/_query.py
select_scalar_values(f=None)
#
Build a Select over ScalarMetricValue for the given filter.
Ordered by the value id ascending so SQL pagination is deterministic across pages.
Source code in packages/climate-ref/src/climate_ref/results/_query.py
select_series_values(f=None)
#
Build a Select over SeriesMetricValue.
The shared index axis is eager-loaded via the model relationship (lazy="joined"), so
.index / .index_name are safe to read for the returned rows.
Ordered by the value id ascending so SQL pagination is deterministic across pages.