TEA: an answer as of a knowledge cut-off reads each child's version known then, and the measures waiting for a validation stay hidden outside FHIR too

This commit is contained in:
2026-11-12 19:47:19 -06:00
parent a0407e41d6
commit 0f9433949f
6 changed files with 93 additions and 26 deletions
+20
View File
@@ -49,6 +49,7 @@ TIMELINE_AT_PERIOD_START = "linea_de_tiempo_al_inicio_del_periodo"
OF_THE_CHOSEN = "del_elegido"
COLUMN = "columna"
STATUS_AT_PERIOD_END = "estado_al_fin_del_periodo"
KNOWN_VERSION = "version_conocida"
class UndecidedRule(ValueError):
@@ -283,10 +284,15 @@ class MeasureQueries:
if self.status is not None:
names |= {definition.dimensions[n] for n in self.status.columns()}
known |= {definition.dimensions[n] for n in (*self.status.known_at, self.status.exit_known) if n}
version = (getattr(definition, "meta", None) or {}).get(KNOWN_VERSION)
if version is not None:
known.add(definition.dimensions[version])
self.events = table(definition.relation, *(
column(name, FechaHoraUTC) if name in known else column(name) for name in sorted(names)
if name.isidentifier()))
self.periods = period_table
if version is not None:
self.events = self._known_version(definition.dimensions[version])
if self.status is not None:
self.events = self._status_rows()
self.birth_day = table("dim_fecha", *map(column, DIM_FECHA)).alias("dia_nacimiento")
@@ -329,6 +335,20 @@ class MeasureQueries:
conditions += [self._column(known_at).is_not(None), self._column(known_at) <= self.q.knowledge_cutoff]
return and_(*conditions)
def _known_version(self, version: str):
"""Each unit's rows as they were known at the knowledge cut-off, for a model with one set of rows per unit
and knowledge version (`meta.version_conocida`, the column that dates each version): the unit's latest
version by the cut-off, or its latest of all without one. A subquery with the model's name, read as the
model is."""
base = self.events
latest = func.rank().over(partition_by=base.c[self.d.numerator.measure_expr], order_by=desc(base.c[version]))
versions = select(*base.c, latest.label("lugar_de_version"))
if self.q.knowledge_cutoff is not None:
versions = versions.where(base.c[version] <= self.q.knowledge_cutoff)
versions = versions.subquery("versiones")
return (select(*(c for c in versions.c if c.name != "lugar_de_version"))
.where(versions.c.lugar_de_version == 1).subquery(self.d.relation))
def _status_rows(self):
"""Each unit the period counts, with its row of highest order: a subquery with the model's name, so the
rest of the SQL (filters, strata, counts) reads it as it reads the model, plus the period's bounds.