Skip to content

Commit

Permalink
Improve handling of Timespan in queries.
Browse files Browse the repository at this point in the history
Postgres sometimes needs help in guessing column type when doing UNION
of several queries.
  • Loading branch information
andy-slac committed Aug 5, 2024
1 parent 8e366e8 commit a2130ee
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion python/lsst/daf/butler/registry/databases/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,11 @@ def extract(cls, mapping: Mapping[str, Any], name: str | None = None) -> Timespa
def fromLiteral(cls, timespan: Timespan | None) -> _RangeTimespanRepresentation:
# Docstring inherited.
if timespan is None:
return cls(column=sqlalchemy.sql.null(), name=cls.NAME)
# Cast NULL to an expected type, helps Postgres to figure out
# column type when doing UNION.
return cls(
column=sqlalchemy.func.cast(sqlalchemy.sql.null(), type_=_RangeTimespanType), name=cls.NAME
)
return cls(
column=sqlalchemy.sql.cast(
sqlalchemy.sql.literal(timespan, type_=_RangeTimespanType), type_=_RangeTimespanType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def make_query_joiner(self, collections: Sequence[CollectionRecord], fields: Set
)
if "timespan" in fields:
tags_builder.joiner.timespans[self.datasetType.name] = (
self._db.getTimespanRepresentation().fromLiteral(Timespan(None, None))
self._db.getTimespanRepresentation().fromLiteral(None)
)
calibs_builder: QueryBuilder | None = None
if CollectionType.CALIBRATION in collection_types:
Expand Down

0 comments on commit a2130ee

Please sign in to comment.