in services/ui_backend_service/api/utils.py [0:0]
def builtin_conditions_query_dict(query: MultiDict):
conditions = []
values = []
for key, val in query.items():
if not key.startswith("_"):
continue
deconstruct = key.split(":", 1)
if len(deconstruct) > 1:
field = deconstruct[0]
operator = deconstruct[1]
else:
field = key
operator = None
# Tags
if field == "_tags":
tags = val.split(",")
if operator == "likeany" or operator == "likeall":
# `?_tags:likeany` => LIKE ANY (OR)
# `?_tags:likeall` => LIKE ALL (AND)
# Raw SQL: SELECT * FROM runs_v3 WHERE tags||system_tags::text LIKE ANY(array['{%runtime:dev%','%user:m%']');
# Psycopg SQL: SELECT * FROM runs_v3 WHERE tags||system_tags::text LIKE ANY(array[%s,%s]);
# Values for Psycopg: ['%runtime:dev%','%user:m%']
compare = "ANY" if operator == "likeany" else "ALL"
conditions.append(
"tags||system_tags::text LIKE {}(array[{}])"
.format(compare, ",".join(["%s"] * len(tags))))
values += map(lambda t: "%{}%".format(t), tags)
else:
# `?_tags:any` => ?| (OR)
# `?_tags:all` => ?& (AND) (default)
compare = "?|" if operator == "any" else "?&"
conditions.append("tags||system_tags {} array[{}]".format(
compare, ",".join(["%s"] * len(tags))))
values += tags
return conditions, values