Skip to content

Commit

Permalink
Add explict type casts for postgresql filters (home-assistant#72615)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored May 27, 2022
1 parent ea1e40a commit 34323ce
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions homeassistant/components/recorder/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import json
from typing import Any

from sqlalchemy import Column, not_, or_
from sqlalchemy import JSON, Column, Text, cast, not_, or_
from sqlalchemy.sql.elements import ClauseList

from homeassistant.const import CONF_DOMAINS, CONF_ENTITIES, CONF_EXCLUDE, CONF_INCLUDE
Expand Down Expand Up @@ -110,8 +110,7 @@ def events_entity_filter(self) -> ClauseList:
"""Generate the entity filter query."""
_encoder = json.dumps
return or_(
(ENTITY_ID_IN_EVENT == _encoder(None))
& (OLD_ENTITY_ID_IN_EVENT == _encoder(None)),
(ENTITY_ID_IN_EVENT == JSON.NULL) & (OLD_ENTITY_ID_IN_EVENT == JSON.NULL),
self._generate_filter_for_columns(
(ENTITY_ID_IN_EVENT, OLD_ENTITY_ID_IN_EVENT), _encoder
).self_group(),
Expand All @@ -123,7 +122,7 @@ def _globs_to_like(
) -> ClauseList:
"""Translate glob to sql."""
return or_(
column.like(encoder(glob_str.translate(GLOB_TO_SQL_CHARS)))
cast(column, Text()).like(encoder(glob_str.translate(GLOB_TO_SQL_CHARS)))
for glob_str in glob_strs
for column in columns
)
Expand All @@ -133,7 +132,7 @@ def _entity_matcher(
entity_ids: Iterable[str], columns: Iterable[Column], encoder: Callable[[Any], Any]
) -> ClauseList:
return or_(
column.in_([encoder(entity_id) for entity_id in entity_ids])
cast(column, Text()).in_([encoder(entity_id) for entity_id in entity_ids])
for column in columns
)

Expand All @@ -142,5 +141,7 @@ def _domain_matcher(
domains: Iterable[str], columns: Iterable[Column], encoder: Callable[[Any], Any]
) -> ClauseList:
return or_(
column.like(encoder(f"{domain}.%")) for domain in domains for column in columns
cast(column, Text()).like(encoder(f"{domain}.%"))
for domain in domains
for column in columns
)

0 comments on commit 34323ce

Please sign in to comment.