Skip to content

Commit

Permalink
QueryBuilder: Fix type bugs for PostgreSQL backend (#6658)
Browse files Browse the repository at this point in the history
This PR fixes the bugs that the `of_type` filter operator was failing for `null` and array types on the PostgreSQL storage backend.
  • Loading branch information
rabbull authored Dec 10, 2024
1 parent 37e5431 commit 53be737
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/aiida/storage/psql_dos/orm/querybuilder/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ def cast_according_to_type(path_in_json, value):
elif isinstance(value, dict) or value is None:
type_filter = jsonb_typeof(path_in_json) == 'object'
casted_entity = path_in_json.astext.cast(JSONB) # BOOLEANS?
elif isinstance(value, dict):
elif isinstance(value, list):
type_filter = jsonb_typeof(path_in_json) == 'array'
casted_entity = path_in_json.astext.cast(JSONB) # BOOLEANS?
elif isinstance(value, str):
Expand Down Expand Up @@ -661,10 +661,19 @@ def cast_according_to_type(path_in_json, value):
elif operator == 'of_type':
# http://www.postgresql.org/docs/9.5/static/functions-json.html
# Possible types are object, array, string, number, boolean, and null.
valid_types = ('object', 'array', 'string', 'number', 'boolean', 'null')
value_types = ('object', 'array', 'string', 'number', 'boolean')
null_types = ('null',)
valid_types = value_types + null_types
if value not in valid_types:
raise ValueError(f'value {value} for of_type is not among valid types\n{valid_types}')
expr = jsonb_typeof(database_entity) == value
if value in value_types:
expr = jsonb_typeof(database_entity) == value
elif value in null_types:
# https://www.postgresql.org/docs/current/functions-json.html
# json_typeof('null'::json) → null
# json_typeof(NULL::json) IS NULL → t
tp = jsonb_typeof(database_entity)
expr = or_(tp == 'null', tp.is_(None))
elif operator == 'like':
type_filter, casted_entity = cast_according_to_type(database_entity, value)
expr = case((type_filter, casted_entity.like(value)), else_=False)
Expand Down

0 comments on commit 53be737

Please sign in to comment.