diff --git a/python/lsst/daf/butler/registry/databases/postgresql.py b/python/lsst/daf/butler/registry/databases/postgresql.py index fcca457a01..51df4e9f3c 100644 --- a/python/lsst/daf/butler/registry/databases/postgresql.py +++ b/python/lsst/daf/butler/registry/databases/postgresql.py @@ -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 diff --git a/python/lsst/daf/butler/registry/datasets/byDimensions/_storage.py b/python/lsst/daf/butler/registry/datasets/byDimensions/_storage.py index d66e114f45..9c518f1026 100644 --- a/python/lsst/daf/butler/registry/datasets/byDimensions/_storage.py +++ b/python/lsst/daf/butler/registry/datasets/byDimensions/_storage.py @@ -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: