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

QueryBuilder: Fix type bugs for PostgreSQL backend #6658

Merged
merged 1 commit into from
Dec 10, 2024
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
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 @@
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 @@
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
Loading