Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mypy errors from updated sqlalchemy #933

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions python/lsst/daf/butler/registry/queries/butler_sql_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,16 @@ def to_payload(self, relation: Relation) -> sql.Payload[LogicalColumn]:
# contains (at the same time, since they have the same columns,
# aside from the special 'rownum' window-function column).
search_columns = self.extract_mapping(target.columns, search.columns)
partition_by = [search_columns[tag] for tag in operation.dimensions]
rownum_column = sqlalchemy.sql.func.row_number()
partition_by = [
_assert_column_is_directly_usable_by_sqlalchemy(search_columns[tag])
for tag in operation.dimensions
]
row_number = sqlalchemy.sql.func.row_number()
rank_column = _assert_column_is_directly_usable_by_sqlalchemy(search_columns[operation.rank])
if partition_by:
rownum_column = rownum_column.over(
partition_by=partition_by, order_by=search_columns[operation.rank]
)
rownum_column = row_number.over(partition_by=partition_by, order_by=rank_column)
else:
rownum_column = rownum_column.over(order_by=search_columns[operation.rank])
rownum_column = row_number.over(order_by=rank_column)
window = self.select_items(
search_columns.items(), search, rownum_column.label("rownum")
).subquery(f"{operation.rank.dataset_type}_window")
Expand All @@ -209,3 +211,13 @@ def to_payload(self, relation: Relation) -> sql.Payload[LogicalColumn]:
)
case _:
return super().to_payload(relation)


def _assert_column_is_directly_usable_by_sqlalchemy(column: LogicalColumn) -> sqlalchemy.sql.ColumnElement:
"""Narrow a `LogicalColumn` to a SqlAlchemy ColumnElement, to satisfy the
typechecker in cases where no timespans are expected.
"""
if isinstance(column, TimespanDatabaseRepresentation):
raise TypeError("Timespans not expected here.")

return column