Skip to content

Commit

Permalink
fix type bugs in psql query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbull committed Dec 10, 2024
1 parent 37e5431 commit 854545e
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

Check warning on line 666 in src/aiida/storage/psql_dos/orm/querybuilder/main.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/storage/psql_dos/orm/querybuilder/main.py#L664-L666

Added lines #L664 - L666 were not covered by tests
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:

Check warning on line 671 in src/aiida/storage/psql_dos/orm/querybuilder/main.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/storage/psql_dos/orm/querybuilder/main.py#L669-L671

Added lines #L669 - L671 were not covered by tests
# 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))

Check warning on line 676 in src/aiida/storage/psql_dos/orm/querybuilder/main.py

View check run for this annotation

Codecov / codecov/patch

src/aiida/storage/psql_dos/orm/querybuilder/main.py#L675-L676

Added lines #L675 - L676 were not covered by tests
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 854545e

Please sign in to comment.