Skip to content

Commit d1b517d

Browse files
committed
RF/FIX: Decompose filter construction for special queries and lists
1 parent 52290b0 commit d1b517d

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

bids/layout/layout.py

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -745,29 +745,45 @@ def _build_file_query(self, **kwargs):
745745
for name, val in filters.items():
746746
tag_alias = aliased(Tag)
747747

748-
if isinstance(val, (list, tuple)) and len(val) == 1:
749-
val = val[0]
750-
748+
val_list = list(listify(val)) if val is not None else [None]
749+
if not val_list:
750+
continue
751+
752+
none = any_ = False
753+
if None in val_list:
754+
none = True
755+
val_list.remove(None)
756+
if Query.NONE in val_list:
757+
none = True
758+
val_list.remove(Query.NONE)
759+
if Query.ANY in val_list:
760+
any_ = True
761+
val_list.remove(Query.ANY)
762+
763+
if none and any_:
764+
# Always true, apply no filter
765+
continue
766+
767+
# Baseline, use join, start accumulating clauses
751768
join_method = query.join
769+
val_clauses = []
752770

753-
if val is None or val == Query.NONE:
771+
# NONE and ANY get special treatment
772+
if none:
754773
join_method = query.outerjoin
755-
val_clause = tag_alias._value.is_(None)
756-
elif val == Query.ANY:
757-
val_clause = tag_alias._value.isnot(None)
758-
elif regex:
759-
if isinstance(val, (list, tuple)):
760-
val_clause = sa.or_(*[
761-
tag_alias._value.op('REGEXP')(str(v))
762-
for v in val
763-
])
764-
else:
765-
val_clause = tag_alias._value.op('REGEXP')(str(val))
766-
else:
767-
if isinstance(val, (list, tuple)):
768-
val_clause = tag_alias._value.in_(val)
769-
else:
770-
val_clause = tag_alias._value == val
774+
val_clauses.append(tag_alias._value.is_(None))
775+
if any_:
776+
val_clauses.append(tag_alias._value.isnot(None))
777+
778+
# Any remaining values
779+
if regex:
780+
val_clauses.extend([tag_alias._value.op('REGEXP')(str(v))
781+
for v in val_list])
782+
elif val_list:
783+
val_clauses.append(tag_alias._value.in_(val_list))
784+
785+
# Looking for intersection with list of vals, so use OR
786+
val_clause = sa.or_(*val_clauses) if len(val_clauses) > 1 else val_clauses[0]
771787

772788
query = join_method(
773789
tag_alias,

0 commit comments

Comments
 (0)