Skip to content

Commit

Permalink
explain why sqlite contains implementation in comment
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbull committed Jan 21, 2025
1 parent d226c26 commit 7617861
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/aiida/storage/sqlite_zip/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,17 @@ def _cast_json_type(comparator: JSON.Comparator, value: Any) -> Tuple[ColumnElem
return case((type_filter, casted_entity.ilike(value, escape='\\')), else_=False)

if operator == 'contains':
# If the operator is 'contains', we must mirror the behavior of the PostgreSQL
# backend, which returns NULL if `attr_key` doesn't exist. To achieve this,
# an additional CASE statement is added to directly return NULL in such cases.
#
# Instead of using `database_entity`, which would be interpreted as a 'null'
# string in SQL, this approach ensures a proper NULL value is returned when
# `attr_key` doesn't exist.
#
# Original implementation:
# return func.json_contains(database_entity, json.dumps(value))

return case(
(func.json_extract(column, '$.' + '.'.join(attr_key)).is_(null()), null()),
else_=func.json_contains(database_entity, json.dumps(value)),
Expand Down

0 comments on commit 7617861

Please sign in to comment.