20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567 | class MongoTransformer(BaseTransformer):
"""A filter transformer for the MongoDB backend.
Parses a lark tree into a dictionary representation to be
used by pymongo or mongomock. Uses post-processing functions
to handle some specific edge-cases for MongoDB.
Attributes:
operator_map: A map from comparison operators
to the mongoDB specific versions.
inverse_operator_map: A map from operators to their
logical inverse.
mapper: A resource mapper object that defines the
expected fields and acts as a container for
various field-related configuration.
"""
operator_map = {
"<": "$lt",
"<=": "$lte",
">": "$gt",
">=": "$gte",
"!=": "$ne",
"=": "$eq",
}
inverse_operator_map = {
"$lt": "$gte",
"$lte": "$gt",
"$gt": "$lte",
"$gte": "$lt",
"$ne": "$eq",
"$eq": "$ne",
"$in": "$nin",
"$nin": "$in",
}
def postprocess(self, query: dict[str, Any]):
"""Used to post-process the nested dictionary of the parsed query."""
query = self._apply_relationship_filtering(query)
query = self._apply_length_operators(query)
query = self._apply_unknown_or_null_filter(query)
query = self._apply_has_only_filter(query)
query = self._apply_mongo_id_filter(query)
query = self._apply_mongo_date_filter(query)
return query
def value_list(self, arg):
# value_list: [ OPERATOR ] value ( "," [ OPERATOR ] value )*
# NOTE: no support for optional OPERATOR, yet, so this takes the
# parsed values and returns an error if that is being attempted
for value in arg:
if str(value) in self.operator_map.keys():
raise NotImplementedError(
f"OPERATOR {value} inside value_list {arg} not implemented."
)
return arg
def value_zip(self, arg):
# value_zip: [ OPERATOR ] value ":" [ OPERATOR ] value (":" [ OPERATOR ] value)*
raise NotImplementedError("Correlated list queries are not supported.")
def value_zip_list(self, arg):
# value_zip_list: value_zip ( "," value_zip )*
raise NotImplementedError("Correlated list queries are not supported.")
def expression(self, arg):
# expression: expression_clause ( OR expression_clause )
# expression with and without 'OR'
return {"$or": arg} if len(arg) > 1 else arg[0]
def expression_clause(self, arg):
# expression_clause: expression_phrase ( AND expression_phrase )*
# expression_clause with and without 'AND'
return {"$and": arg} if len(arg) > 1 else arg[0]
def expression_phrase(self, arg):
# expression_phrase: [ NOT ] ( comparison | "(" expression ")" )
return self._recursive_expression_phrase(arg)
@v_args(inline=True)
def property_first_comparison(self, quantity, query):
# property_first_comparison: property ( value_op_rhs | known_op_rhs | fuzzy_string_op_rhs | set_op_rhs |
# set_zip_op_rhs | length_op_rhs )
# Awkwardly, MongoDB will match null fields in $ne filters,
# so we need to add a check for null equality in evey $ne query.
if "$ne" in query:
return {"$and": [{quantity: query}, {quantity: {"$ne": None}}]}
# Check if a $size query is being made (indicating a length_op_rhs filter); if so, check for
# a defined length alias to replace the $size call with the corresponding filter on the
# length quantity then carefully merge the two queries.
#
# e.g. `("elements", {"$size": 2, "$all": ["Ag", "Au"]})` should become
# `{"elements": {"$all": ["Ag", "Au"]}, "nelements": 2}` if the `elements` -> `nelements`
# length alias is defined.
if "$size" in query:
if (
getattr(self.backend_mapping.get(quantity), "length_quantity", None)
is not None
):
size_query = {
self.backend_mapping[ # type: ignore[union-attr]
quantity
].length_quantity.backend_field: query.pop("$size")
}
final_query = {}
if query:
final_query = {quantity: query}
for q in size_query:
if q in final_query:
final_query[q].update(size_query[q])
else:
final_query[q] = size_query[q]
return final_query
return {quantity: query}
def constant_first_comparison(self, arg):
# constant_first_comparison: constant OPERATOR ( non_string_value | not_implemented_string )
return self.property_first_comparison(
arg[2], {self.operator_map[self._reversed_operator_map[arg[1]]]: arg[0]}
)
@v_args(inline=True)
def value_op_rhs(self, operator, value):
# value_op_rhs: OPERATOR value
return {self.operator_map[operator]: value}
def known_op_rhs(self, arg):
# known_op_rhs: IS ( KNOWN | UNKNOWN )
# The OPTIMADE spec also required a type comparison with null, this must be post-processed
# so here we use a special key "#known" which will get replaced in post-processing with the
# expanded dict
return {"#known": arg[1] == "KNOWN"}
def fuzzy_string_op_rhs(self, arg):
# fuzzy_string_op_rhs: CONTAINS value | STARTS [ WITH ] value | ENDS [ WITH ] value
# The WITH keyword may be omitted.
if isinstance(arg[1], Token) and arg[1].type == "WITH":
pattern = arg[2]
else:
pattern = arg[1]
# CONTAINS
if arg[0] == "CONTAINS":
regex = f"{pattern}"
elif arg[0] == "STARTS":
regex = f"^{pattern}"
elif arg[0] == "ENDS":
regex = f"{pattern}$"
return {"$regex": regex}
def set_op_rhs(self, arg):
# set_op_rhs: HAS ( [ OPERATOR ] value | ALL value_list | ANY value_list | ONLY value_list )
if len(arg) == 2:
# only value without OPERATOR
return {"$in": arg[1:]}
if arg[1] == "ALL":
return {"$all": arg[2]}
if arg[1] == "ANY":
return {"$in": arg[2]}
if arg[1] == "ONLY":
return {"#only": arg[2]}
# value with OPERATOR
raise NotImplementedError(
f"set_op_rhs not implemented for use with OPERATOR. Given: {arg}"
)
def property(self, args):
# property: IDENTIFIER ( "." IDENTIFIER )*
quantity = super().property(args)
if isinstance(quantity, Quantity):
quantity = quantity.backend_field
return ".".join([quantity] + args[1:])
def length_op_rhs(self, arg):
# length_op_rhs: LENGTH [ OPERATOR ] value
if len(arg) == 2 or (len(arg) == 3 and arg[1] == "="):
return {"$size": arg[-1]}
if arg[1] in self.operator_map and arg[1] != "!=":
# create an invalid query that needs to be post-processed
# e.g. {'$size': {'$gt': 2}}, which is not allowed by Mongo.
return {"$size": {self.operator_map[arg[1]]: arg[-1]}}
raise NotImplementedError(
f"Operator {arg[1]} not implemented for LENGTH filter."
)
def set_zip_op_rhs(self, arg):
# set_zip_op_rhs: property_zip_addon HAS ( value_zip | ONLY value_zip_list | ALL value_zip_list |
# ANY value_zip_list )
raise NotImplementedError("Correlated list queries are not supported.")
def property_zip_addon(self, arg):
# property_zip_addon: ":" property (":" property)*
raise NotImplementedError("Correlated list queries are not supported.")
def _recursive_expression_phrase(self, arg: list) -> dict[str, Any]:
"""Helper function for parsing `expression_phrase`. Recursively sorts out
the correct precedence for `$not`, `$and` and `$or`.
Parameters:
arg: A list containing the expression to be evaluated and whether it
is negated, e.g., `["NOT", expr]` or just `[expr]`.
Returns:
The evaluated filter as a nested dictionary.
"""
def handle_not_and(arg: dict[str, list]) -> dict[str, list]:
"""Handle the case of `~(A & B) -> (~A | ~B)`.
We have to check for the special case in which the "and" was created
by a previous NOT, e.g.,
`NOT (NOT ({"a": {"$eq": 6}})) -> NOT({"$and": [{"a": {"$ne": 6}},{"a": {"$ne": None}}]})`
Parameters:
arg: A dictionary with key `"$and"` containing a list of expressions.
Returns:
A dictionary with key `"$or"` containing a list of the appropriate negated expressions.
"""
expr1 = arg["$and"][0]
expr2 = arg["$and"][1]
if expr1.keys() == expr2.keys():
key = list(expr1.keys())[0]
for e, f in itertools.permutations((expr1, expr2)):
if e.get(key) == {"$ne": None}:
return self._recursive_expression_phrase(["NOT", f])
return {
"$or": [
self._recursive_expression_phrase(["NOT", subdict])
for subdict in arg["$and"]
]
}
def handle_not_or(arg: dict[str, list]) -> dict[str, list]:
"""Handle the case of ~(A | B) -> (~A & ~B).
!!! note
Although the MongoDB `$nor` could be used here, it is not convenient as it
will also return documents where the filtered field is missing when testing
for inequality.
Parameters:
arg: A dictionary with key `"$or"` containing a list of expressions.
Returns:
A dictionary with key `"$and"` that lists the appropriate negated expressions.
"""
return {
"$and": [
self._recursive_expression_phrase(["NOT", subdict])
for subdict in arg["$or"]
]
}
if len(arg) == 1:
# without NOT
return arg[0]
if "$or" in arg[1]:
return handle_not_or(arg[1])
if "$and" in arg[1]:
return handle_not_and(arg[1])
prop, expr = next(iter(arg[1].items()))
operator, value = next(iter(expr.items()))
if operator == "$not": # Case of double negation e.g. NOT("$not":{ ...})
return {prop: value}
# If the NOT operator occurs at the lowest nesting level,
# the expression can be simplified by using the opposite operator and removing the not.
if operator in self.inverse_operator_map:
filter_ = {prop: {self.inverse_operator_map[operator]: value}}
if operator in ("$in", "$eq"):
filter_ = {"$and": [filter_, {prop: {"$ne": None}}]} # type: ignore[dict-item]
return filter_
filter_ = {prop: {"$not": expr}}
if "#known" in expr:
return filter_
return {"$and": [filter_, {prop: {"$ne": None}}]}
def _apply_length_operators(self, filter_: dict) -> dict:
"""Check for any invalid pymongo queries that involve applying a
comparison operator to the length of a field, and transform
them into a test for existence of the relevant entry, e.g.
"list LENGTH > 3" becomes "does the 4th list entry exist?".
"""
def check_for_length_op_filter(_, expr):
return (
isinstance(expr, dict)
and "$size" in expr
and isinstance(expr["$size"], dict)
)
def apply_length_op(subdict, prop, expr):
# assumes that the dictionary only has one element by design
# (we just made it above in the transformer)
operator, value = list(expr["$size"].items())[0]
if operator in self.operator_map.values() and operator != "$ne":
# worth being explicit here, I think
_prop = None
existence = None
if operator == "$gt":
_prop = f"{prop}.{value + 1}"
existence = True
elif operator == "$gte":
_prop = f"{prop}.{value}"
existence = True
elif operator == "$lt":
_prop = f"{prop}.{value}"
existence = False
elif operator == "$lte":
_prop = f"{prop}.{value + 1}"
existence = False
if _prop is not None:
subdict.pop(prop)
subdict[_prop] = {"$exists": existence}
return subdict
return recursive_postprocessing(
filter_,
check_for_length_op_filter,
apply_length_op,
)
def _apply_relationship_filtering(self, filter_: dict) -> dict:
"""Check query for property names that match the entry
types, and transform them as relationship filters rather than
property filters.
"""
def check_for_entry_type(prop, _):
return str(prop).count(".") == 1 and str(prop).split(".")[0] in (
"structures",
"references",
)
def replace_with_relationship(subdict, prop, expr):
_prop, _field = str(prop).split(".")
if _field != "id":
raise NotImplementedError(
f'Cannot filter relationships by field "{_field}", only "id" is supported.'
)
subdict[f"relationships.{_prop}.data.{_field}"] = expr
subdict.pop(prop)
return subdict
return recursive_postprocessing(
filter_, check_for_entry_type, replace_with_relationship
)
def _apply_has_only_filter(self, filter_: dict) -> dict:
"""This method loops through the query and replaces the magic key `"#only"`
with the proper 'HAS ONLY' query.
"""
def check_for_only_filter(_, expr):
"""Find cases where the magic key `"#only"` is in the query."""
return isinstance(expr, dict) and ("#only" in expr)
def replace_only_filter(subdict: dict, prop: str, expr: dict):
"""Replace the magic key `"#only"` (added by this transformer) with an `$elemMatch`-based query.
The first part of the query selects all the documents that contain any value that does not
match any target values for the property `prop`.
Subsequently, this selection is inverted, to get the documents that only have
the allowed values.
This inversion also selects documents with edge-case values such as null or empty lists;
these are removed in the second part of the query that makes sure that only documents
with lists that have at least one value are selected.
"""
if "$and" not in subdict:
subdict["$and"] = []
if prop.startswith("relationships."):
if prop not in (
"relationships.references.data.id",
"relationships.structures.data.id",
):
raise BadRequest(f"Unable to query on unrecognised field {prop}.")
first_part_prop = ".".join(prop.split(".")[:-1])
subdict["$and"].append(
{
first_part_prop: {
"$not": {"$elemMatch": {"id": {"$nin": expr["#only"]}}}
}
}
)
subdict["$and"].append({first_part_prop + ".0": {"$exists": True}})
else:
subdict["$and"].append(
{prop: {"$not": {"$elemMatch": {"$nin": expr["#only"]}}}}
)
subdict["$and"].append({prop + ".0": {"$exists": True}})
subdict.pop(prop)
return subdict
return recursive_postprocessing(
filter_, check_for_only_filter, replace_only_filter
)
def _apply_unknown_or_null_filter(self, filter_: dict) -> dict:
"""This method loops through the query and replaces the check for
KNOWN with a check for existence and a check for not null, and the
inverse for UNKNOWN.
"""
def check_for_known_filter(_, expr):
"""Find cases where the query dict looks like
`{"field": {"#known": T/F}}` or
`{"field": "$not": {"#known": T/F}}`, which is a magic word
for KNOWN/UNKNOWN filters in this transformer.
"""
return isinstance(expr, dict) and (
"#known" in expr or "#known" in expr.get("$not", {})
)
def replace_known_filter_with_or(subdict, prop, expr):
"""Replace magic key `"#known"` (added by this transformer) with the appropriate
combination of `$exists` and/or test for nullity.
combination of $exists and/or $eq/$ne null.
"""
not_ = set(expr.keys()) == {"$not"}
if not_:
expr = expr["$not"]
exists = expr["#known"] ^ not_
top_level_key = "$or"
comparison_operator = "$eq"
if exists:
top_level_key = "$and"
comparison_operator = "$ne"
if top_level_key not in subdict:
subdict[top_level_key] = []
subdict[top_level_key].append({prop: {"$exists": exists}})
subdict[top_level_key].append({prop: {comparison_operator: None}})
subdict.pop(prop)
return subdict
return recursive_postprocessing(
filter_, check_for_known_filter, replace_known_filter_with_or
)
def _apply_mongo_id_filter(self, filter_: dict) -> dict:
"""This method loops through the query and replaces any operations
on the special Mongodb `_id` key with the corresponding operation
on a BSON `ObjectId` type.
"""
def check_for_id_key(prop, _):
"""Find cases where the query dict is operating on the `_id` field."""
return prop == "_id"
def replace_str_id_with_objectid(subdict, prop, expr):
from bson import ObjectId
for operator in subdict[prop]:
val = subdict[prop][operator]
if operator not in ("$eq", "$ne"):
if self.mapper is not None:
prop = self.mapper.get_optimade_field(prop)
raise NotImplementedError(
f"Operator {operator} not supported for query on field {prop!r}, can only test for equality"
)
if isinstance(val, str):
subdict[prop][operator] = ObjectId(val)
return subdict
return recursive_postprocessing(
filter_, check_for_id_key, replace_str_id_with_objectid
)
def _apply_mongo_date_filter(self, filter_: dict) -> dict:
"""This method loops through the query and replaces any operations
on suspected timestamp properties with the corresponding operation
on a BSON `DateTime` type.
"""
def check_for_timestamp_field(prop, _):
"""Find cases where the query dict is operating on a timestamp field."""
if self.mapper is not None:
prop = self.mapper.get_optimade_field(prop)
return prop == "last_modified"
def replace_str_date_with_datetime(subdict, prop, expr):
"""Encode suspected dates in with BSON."""
import bson.json_util
for operator in subdict[prop]:
query_datetime = bson.json_util.loads(
bson.json_util.dumps({"$date": subdict[prop][operator]}),
json_options=bson.json_util.DEFAULT_JSON_OPTIONS.with_options(
tz_aware=True, tzinfo=bson.tz_util.utc
),
)
if query_datetime.microsecond != 0:
warnings.warn(
f"Query for timestamp {subdict[prop][operator]!r} for field {prop!r} contained microseconds, which is not RFC3339 compliant. "
"This may cause undefined behaviour for the underlying database.",
TimestampNotRFCCompliant,
)
subdict[prop][operator] = query_datetime
return subdict
return recursive_postprocessing(
filter_, check_for_timestamp_field, replace_str_date_with_datetime
)
|