Skip to content

Support lists of values in query filters - #156

Open
apdavison wants to merge 2 commits into
HumanBrainProject:masterfrom
apdavison:list-valued-filters
Open

apdavison wants to merge 2 commits into
HumanBrainProject:masterfrom
apdavison:list-valued-filters

Conversation

@apdavison

Copy link
Copy Markdown
Member

Currently, passing a list of values as a filter does not work. For example:

omcore.SoftwareVersion.list(client, developers__family_name=["Baggins", "Oakenshield"])

sends the list as the value of a single filter, which the KG rejects with a 500 error, and a list of linked objects (e.g. Chapter.list(client, authors=[p1, p2])) raises an AttributeError while the query is being generated.

This PR expresses a list of values in the generated KG query by repeating the top-level property once per value, each copy being required and carrying a single value:

{"propertyName": "Qdevelopers1", "path": ".../developer", "required": true,
 "structure": [{"propertyName": "Qfamily_name", "path": ".../familyName",
                "filter": {"op": "CONTAINS", "value": "Baggins"}, "required": true}]},
{"propertyName": "Qdevelopers2", "path": ".../developer", "required": true,
 "structure": [{"propertyName": "Qfamily_name", "path": ".../familyName",
                "filter": {"op": "CONTAINS", "value": "Oakenshield"}, "required": true}]}

A result must therefore match every value in the list, in any order.

  • A one-item list is treated as a single value, so the query is unchanged.
  • An empty list raises a ValueError, as does more than one list within the same top-level property (e.g. lists for both developers__family_name and developers__given_name), since there is no way to pair the items.
  • Other filters within the same property are repeated in each copy.
  • A list of ID strings is accepted for a link property, as a single ID string already is.
  • Equals and Regex values in a list keep their own operator.

The PR also fixes expand_filter(), which currently drops all but the last of several filters sharing a prefix, e.g. developers__given_name and developers__family_name (separate commit).

This is a first step towards supporting list-valued properties in existence queries: _build_existence_query() currently raises CannotBuildExistenceQuery("not implemented yet") for them, so objects such as a File with hashes are always treated as new when saved.

Tests

  • Offline tests of the generated queries in test/test_queries.py, and of expand_filter() in test/test_utility.py.
  • A live test, test_execute_query_with_list_filter, checks against the KG that filtering software versions on a list of two developer family names returns exactly the overlap between the two single-name queries. Previously, this test would have failed, producing a 500 error from the KG.

When several filters shared a prefix, e.g. `developers__given_name` and `developers__family_name`, each one replaced the nested dict created by the previous one, so all but the last were silently dropped. The nested dicts are now merged.
A list of filter values, e.g. `SoftwareVersion.list(client, developers__family_name=["Baggins", "Oakenshield"])`, is now expressed in the generated KG query by repeating the top-level property once per value, each copy being required and carrying a single value. A result must therefore match every value, in any order. Previously the list was sent as the value of a single filter, which the KG rejects with a 500 error, and a list of linked objects raised an AttributeError.

A one-item list is treated as a single value. An empty list, or more than one list within the same top-level property, raises a ValueError. A list of ID strings is now accepted for a link property, as a single ID string already was.
@apdavison apdavison added the bug Something isn't working label Oct 16, 2026
@apdavison apdavison added this to the 0.16 milestone Oct 16, 2026
@apdavison apdavison moved this from Todo to In Progress in fairgraph development Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant