Source code

Download source archive ↓

src/rdflib/plugins/sparql/operators.py

1"""
2This contains evaluation functions for expressions
3
4They get bound as instances-methods to the CompValue objects from parserutils
5using setEvalFn
6"""
7
8from __future__ import annotations
9
10import datetime as py_datetime  # naming conflict with function within this module
11import hashlib
12import math
13import operator as pyop  # python operators
14import random
15import re
16import uuid
17import warnings
18from decimal import ROUND_HALF_DOWN, ROUND_HALF_UP, Decimal, InvalidOperation
19from functools import reduce
20from typing import Any, Callable, Dict, NoReturn, Optional, Tuple, Union, overload
21from urllib.parse import quote
22
23from pyparsing import ParseResults
24
25from rdflib.namespace import RDF, XSD
26from rdflib.plugins.sparql.datatypes import (
27    XSD_DateTime_DTs,
28    XSD_DTs,
29    XSD_Duration_DTs,
30    type_promotion,
31)
32from rdflib.plugins.sparql.parserutils import CompValue, Expr
33from rdflib.plugins.sparql.sparql import (
34    FrozenBindings,
35    QueryContext,
36    SPARQLError,
37    SPARQLTypeError,
38)
39from rdflib.term import (
40    BNode,
41    IdentifiedNode,
42    Identifier,
43    Literal,
44    Node,
45    URIRef,
46    Variable,
47)
48from rdflib.xsd_datetime import Duration, parse_datetime  # type: ignore[attr-defined]
49
50
51def Builtin_IRI(expr: Expr, ctx: FrozenBindings) -> URIRef:
52    """
53    http://www.w3.org/TR/sparql11-query/#func-iri
54    """
55
56    a = expr.arg
57
58    if isinstance(a, URIRef):
59        return a
60    if isinstance(a, Literal):
61        # type error: Item "None" of "Optional[Prologue]" has no attribute "absolutize"
62        # type error: Incompatible return value type (got "Union[CompValue, str, None, Any]", expected "URIRef")
63        return ctx.prologue.absolutize(URIRef(a))  # type: ignore[union-attr,return-value]
64
65    raise SPARQLError("IRI function only accepts URIRefs or Literals/Strings!")
66
67
68def Builtin_isBLANK(expr: Expr, ctx: FrozenBindings) -> Literal:
69    return Literal(isinstance(expr.arg, BNode))
70
71
72def Builtin_isLITERAL(expr, ctx) -> Literal:
73    return Literal(isinstance(expr.arg, Literal))
74
75
76def Builtin_isIRI(expr, ctx) -> Literal:
77    return Literal(isinstance(expr.arg, URIRef))
78
79
80def Builtin_isNUMERIC(expr, ctx) -> Literal:
81    try:
82        numeric(expr.arg)
83        return Literal(True)
84    except:  # noqa: E722
85        return Literal(False)
86
87
88def Builtin_BNODE(expr, ctx) -> BNode:
89    """
90    http://www.w3.org/TR/sparql11-query/#func-bnode
91    """
92
93    a = expr.arg
94
95    if a is None:
96        return BNode()
97
98    if isinstance(a, Literal):
99        return ctx.bnodes[a]  # defaultdict does the right thing
100
101    raise SPARQLError("BNode function only accepts no argument or literal/string")
102
103
104def Builtin_ABS(expr: Expr, ctx) -> Literal:
105    """
106    http://www.w3.org/TR/sparql11-query/#func-abs
107    """
108
109    return Literal(abs(numeric(expr.arg)))
110
111
112def Builtin_IF(expr: Expr, ctx):
113    """
114    http://www.w3.org/TR/sparql11-query/#func-if
115    """
116
117    return expr.arg2 if EBV(expr.arg1) else expr.arg3
118
119
120def Builtin_RAND(expr: Expr, ctx) -> Literal:
121    """
122    http://www.w3.org/TR/sparql11-query/#idp2133952
123    """
124
125    return Literal(random.random())
126
127
128def Builtin_UUID(expr: Expr, ctx) -> URIRef:
129    """
130    http://www.w3.org/TR/sparql11-query/#func-strdt
131    """
132
133    return URIRef(uuid.uuid4().urn)
134
135
136def Builtin_STRUUID(expr, ctx) -> Literal:
137    """
138    http://www.w3.org/TR/sparql11-query/#func-strdt
139    """
140
141    return Literal(str(uuid.uuid4()))
142
143
144def Builtin_MD5(expr: Expr, ctx) -> Literal:
145    s = string(expr.arg).encode("utf-8")
146    return Literal(hashlib.md5(s).hexdigest())
147
148
149def Builtin_SHA1(expr: Expr, ctx) -> Literal:
150    s = string(expr.arg).encode("utf-8")
151    return Literal(hashlib.sha1(s).hexdigest())
152
153
154def Builtin_SHA256(expr: Expr, ctx) -> Literal:
155    s = string(expr.arg).encode("utf-8")
156    return Literal(hashlib.sha256(s).hexdigest())
157
158
159def Builtin_SHA384(expr: Expr, ctx) -> Literal:
160    s = string(expr.arg).encode("utf-8")
161    return Literal(hashlib.sha384(s).hexdigest())
162
163
164def Builtin_SHA512(expr: Expr, ctx) -> Literal:
165    s = string(expr.arg).encode("utf-8")
166    return Literal(hashlib.sha512(s).hexdigest())
167
168
169def Builtin_COALESCE(expr: Expr, ctx):
170    """
171    http://www.w3.org/TR/sparql11-query/#func-coalesce
172    """
173    for x in expr.get("arg", variables=True):
174        if x is not None and not isinstance(x, (SPARQLError, Variable)):
175            return x
176    raise SPARQLError("COALESCE got no arguments that did not evaluate to an error")
177
178
179def Builtin_CEIL(expr: Expr, ctx) -> Literal:
180    """
181    http://www.w3.org/TR/sparql11-query/#func-ceil
182    """
183
184    l_ = expr.arg
185    return Literal(int(math.ceil(numeric(l_))), datatype=l_.datatype)
186
187
188def Builtin_FLOOR(expr: Expr, ctx) -> Literal:
189    """
190    http://www.w3.org/TR/sparql11-query/#func-floor
191    """
192    l_ = expr.arg
193    return Literal(int(math.floor(numeric(l_))), datatype=l_.datatype)
194
195
196def Builtin_ROUND(expr: Expr, ctx) -> Literal:
197    """
198    http://www.w3.org/TR/sparql11-query/#func-round
199    """
200
201    # This used to be just math.bound
202    # but in py3k bound was changed to
203    # "round-to-even" behaviour
204    # this is an ugly work-around
205    l_ = expr.arg
206    v = numeric(l_)
207    v = int(Decimal(v).quantize(1, ROUND_HALF_UP if v > 0 else ROUND_HALF_DOWN))
208    return Literal(v, datatype=l_.datatype)
209
210
211def Builtin_REGEX(expr: Expr, ctx) -> Literal:
212    """
213    http://www.w3.org/TR/sparql11-query/#func-regex
214    Invokes the XPath fn:matches function to match text against a regular
215    expression pattern.
216    The regular expression language is defined in XQuery 1.0 and XPath 2.0
217    Functions and Operators section 7.6.1 Regular Expression Syntax
218    """
219
220    text = string(expr.text)
221    pattern = string(expr.pattern)
222    flags = expr.flags
223
224    cFlag = 0
225    if flags:
226        # Maps XPath REGEX flags (http://www.w3.org/TR/xpath-functions/#flags)
227        # to Python's re flags
228        flagMap = dict([("i", re.IGNORECASE), ("s", re.DOTALL), ("m", re.MULTILINE)])
229        cFlag = reduce(pyop.or_, [flagMap.get(f, 0) for f in flags])
230
231    return Literal(bool(re.search(str(pattern), text, cFlag)))
232
233
234def Builtin_REPLACE(expr: Expr, ctx) -> Literal:
235    """
236    http://www.w3.org/TR/sparql11-query/#func-substr
237    """
238    text = string(expr.arg)
239    pattern = string(expr.pattern)
240    replacement = string(expr.replacement)
241    flags = expr.flags
242
243    # python uses \1, xpath/sparql uses $1
244    # type error: Incompatible types in assignment (expression has type "str", variable has type "Literal")
245    replacement = re.sub("\\$([0-9]*)", r"\\\1", replacement)  # type: ignore[assignment]
246
247    cFlag = 0
248    if flags:
249        # Maps XPath REGEX flags (http://www.w3.org/TR/xpath-functions/#flags)
250        # to Python's re flags
251        flagMap = dict([("i", re.IGNORECASE), ("s", re.DOTALL), ("m", re.MULTILINE)])
252        cFlag = reduce(pyop.or_, [flagMap.get(f, 0) for f in flags])
253
254        # @@FIXME@@ either datatype OR lang, NOT both
255
256    return Literal(
257        re.sub(str(pattern), replacement, text, cFlag),
258        datatype=text.datatype,
259        lang=text.language,
260    )
261
262
263def Builtin_STRDT(expr: Expr, ctx) -> Literal:
264    """
265    http://www.w3.org/TR/sparql11-query/#func-strdt
266    """
267
268    return Literal(str(expr.arg1), datatype=expr.arg2)
269
270
271def Builtin_STRLANG(expr: Expr, ctx) -> Literal:
272    """
273    http://www.w3.org/TR/sparql11-query/#func-strlang
274    """
275
276    s = string(expr.arg1)
277    if s.language or s.datatype:
278        raise SPARQLError("STRLANG expects a simple literal")
279
280    # TODO: normalisation of lang tag to lower-case
281    # should probably happen in literal __init__
282    return Literal(str(s), lang=str(expr.arg2).lower())
283
284
285def Builtin_CONCAT(expr: Expr, ctx) -> Literal:
286    """
287    http://www.w3.org/TR/sparql11-query/#func-concat
288    """
289
290    # dt/lang passed on only if they all match
291
292    dt = set(x.datatype for x in expr.arg if isinstance(x, Literal))
293    # type error: Incompatible types in assignment (expression has type "Optional[str]", variable has type "Set[Optional[str]]")
294    dt = dt.pop() if len(dt) == 1 else None  # type: ignore[assignment]
295
296    lang = set(x.language for x in expr.arg if isinstance(x, Literal))
297    # type error: error: Incompatible types in assignment (expression has type "Optional[str]", variable has type "Set[Optional[str]]")
298    lang = lang.pop() if len(lang) == 1 else None  # type: ignore[assignment]
299
300    # NOTE on type errors: this is because same variable is used for two incompatibel types
301    # type error: Argument "datatype" to "Literal" has incompatible type "Set[Any]"; expected "Optional[str]"  [arg-type]
302    # type error: Argument "lang" to "Literal" has incompatible type "Set[Any]"; expected "Optional[str]"
303    return Literal("".join(string(x) for x in expr.arg), datatype=dt, lang=lang)  # type: ignore[arg-type]
304
305
306def _compatibleStrings(a: Literal, b: Literal) -> None:
307    string(a)
308    string(b)
309
310    if b.language and a.language != b.language:
311        raise SPARQLError("incompatible arguments to str functions")
312
313
314def Builtin_STRSTARTS(expr: Expr, ctx) -> Literal:
315    """
316    http://www.w3.org/TR/sparql11-query/#func-strstarts
317    """
318
319    a = expr.arg1
320    b = expr.arg2
321    _compatibleStrings(a, b)
322
323    return Literal(a.startswith(b))
324
325
326def Builtin_STRENDS(expr: Expr, ctx) -> Literal:
327    """
328    http://www.w3.org/TR/sparql11-query/#func-strends
329    """
330    a = expr.arg1
331    b = expr.arg2
332
333    _compatibleStrings(a, b)
334
335    return Literal(a.endswith(b))
336
337
338def Builtin_STRBEFORE(expr: Expr, ctx) -> Literal:
339    """
340    http://www.w3.org/TR/sparql11-query/#func-strbefore
341    """
342
343    a = expr.arg1
344    b = expr.arg2
345    _compatibleStrings(a, b)
346
347    i = a.find(b)
348    if i == -1:
349        return Literal("")
350    else:
351        return Literal(a[:i], lang=a.language, datatype=a.datatype)
352
353
354def Builtin_STRAFTER(expr: Expr, ctx) -> Literal:
355    """
356    http://www.w3.org/TR/sparql11-query/#func-strafter
357    """
358
359    a = expr.arg1
360    b = expr.arg2
361    _compatibleStrings(a, b)
362
363    i = a.find(b)
364    if i == -1:
365        return Literal("")
366    else:
367        return Literal(a[i + len(b) :], lang=a.language, datatype=a.datatype)
368
369
370def Builtin_CONTAINS(expr: Expr, ctx) -> Literal:
371    """
372    http://www.w3.org/TR/sparql11-query/#func-strcontains
373    """
374
375    a = expr.arg1
376    b = expr.arg2
377    _compatibleStrings(a, b)
378
379    return Literal(b in a)
380
381
382def Builtin_ENCODE_FOR_URI(expr: Expr, ctx) -> Literal:
383    return Literal(quote(string(expr.arg).encode("utf-8"), safe=""))
384
385
386def Builtin_SUBSTR(expr: Expr, ctx) -> Literal:
387    """
388    http://www.w3.org/TR/sparql11-query/#func-substr
389    """
390
391    a = string(expr.arg)
392
393    start = numeric(expr.start) - 1
394
395    length = expr.length
396    if length is not None:
397        length = numeric(length) + start
398
399    return Literal(a[start:length], lang=a.language, datatype=a.datatype)
400
401
402def Builtin_STRLEN(e: Expr, ctx) -> Literal:
403    l_ = string(e.arg)
404
405    return Literal(len(l_))
406
407
408def Builtin_STR(e: Expr, ctx) -> Literal:
409    arg = e.arg
410    if isinstance(arg, SPARQLError):
411        raise arg
412    return Literal(str(arg))  # plain literal
413
414
415def Builtin_LCASE(e: Expr, ctx) -> Literal:
416    l_ = string(e.arg)
417
418    return Literal(l_.lower(), datatype=l_.datatype, lang=l_.language)
419
420
421def Builtin_LANGMATCHES(e: Expr, ctx) -> Literal:
422    """
423    http://www.w3.org/TR/sparql11-query/#func-langMatches
424
425
426    """
427    langTag = string(e.arg1)
428    langRange = string(e.arg2)
429
430    if str(langTag) == "":
431        return Literal(False)  # nothing matches empty!
432
433    return Literal(_lang_range_check(langRange, langTag))
434
435
436def Builtin_NOW(e: Expr, ctx) -> Literal:
437    """
438    http://www.w3.org/TR/sparql11-query/#func-now
439    """
440    return Literal(ctx.now)
441
442
443def Builtin_YEAR(e: Expr, ctx) -> Literal:
444    d = date(e.arg)
445    return Literal(d.year)
446
447
448def Builtin_MONTH(e: Expr, ctx) -> Literal:
449    d = date(e.arg)
450    return Literal(d.month)
451
452
453def Builtin_DAY(e: Expr, ctx) -> Literal:
454    d = date(e.arg)
455    return Literal(d.day)
456
457
458def Builtin_HOURS(e: Expr, ctx) -> Literal:
459    d = datetime(e.arg)
460    return Literal(d.hour)
461
462
463def Builtin_MINUTES(e: Expr, ctx) -> Literal:
464    d = datetime(e.arg)
465    return Literal(d.minute)
466
467
468def Builtin_SECONDS(e: Expr, ctx) -> Literal:
469    """
470    http://www.w3.org/TR/sparql11-query/#func-seconds
471    """
472    d = datetime(e.arg)
473    result_value = Decimal(d.second)
474    if d.microsecond:
475        result_value += Decimal(d.microsecond) / Decimal(1000000)
476    return Literal(result_value, datatype=XSD.decimal)
477
478
479def Builtin_TIMEZONE(e: Expr, ctx) -> Literal:
480    """
481    http://www.w3.org/TR/sparql11-query/#func-timezone
482
483    Returns:
484        The timezone part of arg as an xsd:dayTimeDuration.
485
486    Raises:
487        An error if there is no timezone.
488    """
489    dt = datetime(e.arg)
490    if not dt.tzinfo:
491        raise SPARQLError("datatime has no timezone: %r" % dt)
492
493    delta = dt.utcoffset()
494
495    # type error: Item "None" of "Optional[timedelta]" has no attribute "days"
496    d = delta.days  # type: ignore[union-attr]
497    # type error: Item "None" of "Optional[timedelta]" has no attribute "seconds"
498    s = delta.seconds  # type: ignore[union-attr]
499    neg = ""
500
501    if d < 0:
502        s = -24 * 60 * 60 * d - s
503        d = 0
504        neg = "-"
505
506    h = s / (60 * 60)
507    m = (s - h * 60 * 60) / 60
508    s = s - h * 60 * 60 - m * 60
509
510    tzdelta = "%sP%sT%s%s%s" % (
511        neg,
512        "%dD" % d if d else "",
513        "%dH" % h if h else "",
514        "%dM" % m if m else "",
515        "%dS" % s if not d and not h and not m else "",
516    )
517
518    return Literal(tzdelta, datatype=XSD.dayTimeDuration)
519
520
521def Builtin_TZ(e: Expr, ctx) -> Literal:
522    d = datetime(e.arg)
523    if not d.tzinfo:
524        return Literal("")
525    n = d.tzinfo.tzname(d)
526    if n is None:
527        n = ""
528    elif n == "UTC":
529        n = "Z"
530    elif n.startswith("UTC"):
531        # Replace tzname like "UTC-05:00" with simply "-05:00" to match Jena tz fn
532        n = n[3:]
533    return Literal(n)
534
535
536def Builtin_UCASE(e: Expr, ctx) -> Literal:
537    l_ = string(e.arg)
538
539    return Literal(l_.upper(), datatype=l_.datatype, lang=l_.language)
540
541
542def Builtin_LANG(e: Expr, ctx) -> Literal:
543    """http://www.w3.org/TR/sparql11-query/#func-lang
544
545    Returns the language tag of ltrl, if it has one. It returns "" if ltrl has
546    no language tag. Note that the RDF data model does not include literals
547    with an empty language tag.
548    """
549
550    l_ = literal(e.arg)
551    return Literal(l_.language or "")
552
553
554def Builtin_DATATYPE(e: Expr, ctx) -> Optional[str]:
555    l_ = e.arg
556    if not isinstance(l_, Literal):
557        raise SPARQLError("Can only get datatype of literal: %r" % l_)
558    if l_.language:
559        return RDF.langString
560    if not l_.datatype and not l_.language:
561        return XSD.string
562    return l_.datatype
563
564
565def Builtin_sameTerm(e: Expr, ctx) -> Literal:
566    a = e.arg1
567    b = e.arg2
568    return Literal(a == b)
569
570
571def Builtin_BOUND(e: Expr, ctx) -> Literal:
572    """
573    http://www.w3.org/TR/sparql11-query/#func-bound
574    """
575    n = e.get("arg", variables=True)
576
577    return Literal(not isinstance(n, Variable))
578
579
580def Builtin_EXISTS(e: Expr, ctx: FrozenBindings) -> Literal:
581    # damn...
582    from rdflib.plugins.sparql.evaluate import evalPart
583
584    exists = e.name == "Builtin_EXISTS"
585
586    # type error: Incompatible types in assignment (expression has type "QueryContext", variable has type "FrozenBindings")
587    ctx = ctx.ctx.thaw(ctx)  # type: ignore[assignment] # hmm
588    # type error: Argument 1 to "evalPart" has incompatible type "FrozenBindings"; expected "QueryContext"
589    for x in evalPart(ctx, e.graph):  # type: ignore[arg-type]
590        return Literal(exists)
591    return Literal(not exists)
592
593
594_CustomFunction = Callable[[Expr, FrozenBindings], Node]
595
596_CUSTOM_FUNCTIONS: Dict[URIRef, Tuple[_CustomFunction, bool]] = {}
597
598
599def register_custom_function(
600    uri: URIRef, func: _CustomFunction, override: bool = False, raw: bool = False
601) -> None:
602    """Register a custom SPARQL function.
603
604    By default, the function will be passed the RDF terms in the argument list.
605    If raw is True, the function will be passed an Expression and a Context.
606
607    The function must return an RDF term, or raise a SparqlError.
608    """
609    if not override and uri in _CUSTOM_FUNCTIONS:
610        raise ValueError("A function is already registered as %s" % uri.n3())
611    _CUSTOM_FUNCTIONS[uri] = (func, raw)
612
613
614def custom_function(
615    uri: URIRef, override: bool = False, raw: bool = False
616) -> Callable[[_CustomFunction], _CustomFunction]:
617    """
618    Decorator version of :func:`register_custom_function`.
619    """
620
621    def decorator(func: _CustomFunction) -> _CustomFunction:
622        register_custom_function(uri, func, override=override, raw=raw)
623        return func
624
625    return decorator
626
627
628def unregister_custom_function(
629    uri: URIRef, func: Optional[Callable[..., Any]] = None
630) -> None:
631    """
632    The 'func' argument is included for compatibility with existing code.
633    A previous implementation checked that the function associated with
634    the given uri was actually 'func', but this is not necessary as the
635    uri should uniquely identify the function.
636    """
637    if _CUSTOM_FUNCTIONS.get(uri):
638        del _CUSTOM_FUNCTIONS[uri]
639    else:
640        warnings.warn("This function is not registered as %s" % uri.n3())
641
642
643def Function(e: Expr, ctx: FrozenBindings) -> Node:
644    """
645    Custom functions and casts
646    """
647    pair = _CUSTOM_FUNCTIONS.get(e.iri)
648    if pair is None:
649        # no such function is registered
650        raise SPARQLError("Unknown function %r" % e.iri)
651    func, raw = pair
652    if raw:
653        # function expects expression and context
654        return func(e, ctx)
655    else:
656        # function expects the argument list
657        try:
658            return func(*e.expr)
659        except TypeError as ex:
660            # wrong argument number
661            raise SPARQLError(*ex.args)
662
663
664@custom_function(XSD.string, raw=True)
665@custom_function(XSD.dateTime, raw=True)
666@custom_function(XSD.float, raw=True)
667@custom_function(XSD.double, raw=True)
668@custom_function(XSD.decimal, raw=True)
669@custom_function(XSD.integer, raw=True)
670@custom_function(XSD.boolean, raw=True)
671def default_cast(e: Expr, ctx: FrozenBindings) -> Literal:  # type: ignore[return]
672    if not e.expr:
673        raise SPARQLError("Nothing given to cast.")
674    if len(e.expr) > 1:
675        raise SPARQLError("Cannot cast more than one thing!")
676
677    x = e.expr[0]
678
679    if e.iri == XSD.string:
680        if isinstance(x, (URIRef, Literal)):
681            return Literal(x, datatype=XSD.string)
682        else:
683            raise SPARQLError("Cannot cast term %r of type %r" % (x, type(x)))
684
685    if not isinstance(x, Literal):
686        raise SPARQLError("Can only cast Literals to non-string data-types")
687
688    if x.datatype and not x.datatype in XSD_DTs:  # noqa: E713
689        raise SPARQLError("Cannot cast literal with unknown datatype: %r" % x.datatype)
690
691    if e.iri == XSD.dateTime:
692        if x.datatype and x.datatype not in (XSD.dateTime, XSD.string):
693            raise SPARQLError("Cannot cast %r to XSD:dateTime" % x.datatype)
694        try:
695            return Literal(parse_datetime(x), datatype=e.iri)
696        except:  # noqa: E722
697            raise SPARQLError("Cannot interpret '%r' as datetime" % x)
698
699    if x.datatype == XSD.dateTime:
700        raise SPARQLError("Cannot cast XSD.dateTime to %r" % e.iri)
701
702    if e.iri in (XSD.float, XSD.double):
703        try:
704            return Literal(float(x), datatype=e.iri)
705        except:  # noqa: E722
706            raise SPARQLError("Cannot interpret '%r' as float" % x)
707
708    elif e.iri == XSD.decimal:
709        if "e" in x or "E" in x:  # SPARQL/XSD does not allow exponents in decimals
710            raise SPARQLError("Cannot interpret '%r' as decimal" % x)
711        try:
712            return Literal(Decimal(x), datatype=e.iri)
713        except:  # noqa: E722
714            raise SPARQLError("Cannot interpret '%r' as decimal" % x)
715
716    elif e.iri == XSD.integer:
717        try:
718            return Literal(int(x), datatype=XSD.integer)
719        except:  # noqa: E722
720            raise SPARQLError("Cannot interpret '%r' as int" % x)
721
722    elif e.iri == XSD.boolean:
723        # # I would argue that any number is True...
724        # try:
725        #     return Literal(bool(int(x)), datatype=XSD.boolean)
726        # except:
727        if x.lower() in ("1", "true"):
728            return Literal(True)
729        if x.lower() in ("0", "false"):
730            return Literal(False)
731        raise SPARQLError("Cannot interpret '%r' as bool" % x)
732
733
734def UnaryNot(expr: Expr, ctx: FrozenBindings) -> Literal:
735    return Literal(not EBV(expr.expr))
736
737
738def UnaryMinus(expr: Expr, ctx: FrozenBindings) -> Literal:
739    return Literal(-numeric(expr.expr))
740
741
742def UnaryPlus(expr: Expr, ctx: FrozenBindings) -> Literal:
743    return Literal(+numeric(expr.expr))
744
745
746def MultiplicativeExpression(
747    e: Expr, ctx: Union[QueryContext, FrozenBindings]
748) -> Literal:
749    expr = e.expr
750    other = e.other
751
752    # because of the way the mul-expr production handled operator precedence
753    # we sometimes have nothing to do
754    if other is None:
755        return expr
756    try:
757        res: Union[Decimal, float]
758        res = Decimal(numeric(expr))
759        for op, f in zip(e.op, other):
760            f = numeric(f)
761
762            if type(f) == float:  # noqa: E721
763                res = float(res)
764
765            if op == "*":
766                res *= f
767            else:
768                res /= f
769    except (InvalidOperation, ZeroDivisionError):
770        raise SPARQLError("divide by 0")
771
772    return Literal(res)
773
774
775# type error: Missing return statement
776def AdditiveExpression(e: Expr, ctx: Union[QueryContext, FrozenBindings]) -> Literal:  # type: ignore[return]
777    expr = e.expr
778    other = e.other
779
780    # because of the way the add-expr production handled operator precedence
781    # we sometimes have nothing to do
782    if other is None:
783        return expr
784
785    # handling arithmetic(addition/subtraction) of dateTime, date, time
786    # and duration datatypes (if any)
787    if hasattr(expr, "datatype") and (
788        expr.datatype in XSD_DateTime_DTs or expr.datatype in XSD_Duration_DTs
789    ):
790        res = dateTimeObjects(expr)
791        dt = expr.datatype
792
793        for op, term in zip(e.op, other):
794            # check if operation is datetime,date,time operation over
795            # another datetime,date,time datatype
796            if dt in XSD_DateTime_DTs and dt == term.datatype and op == "-":
797                # checking if there are more than one datetime operands -
798                # in that case it doesn't make sense for example
799                # ( dateTime1 - dateTime2 - dateTime3 ) is an invalid operation
800                if len(other) > 1:
801                    error_message = "Can't evaluate multiple %r arguments"
802                    # type error: Too many arguments for "SPARQLError"
803                    raise SPARQLError(error_message, dt.datatype)  # type: ignore[call-arg]
804                else:
805                    n = dateTimeObjects(term)
806                    res = calculateDuration(res, n)
807                    return res
808
809            # datetime,date,time +/- duration,dayTimeDuration,yearMonthDuration
810            elif dt in XSD_DateTime_DTs and term.datatype in XSD_Duration_DTs:
811                n = dateTimeObjects(term)
812                res = calculateFinalDateTime(res, dt, n, term.datatype, op)
813                return res
814
815            # duration,dayTimeDuration,yearMonthDuration + datetime,date,time
816            elif dt in XSD_Duration_DTs and term.datatype in XSD_DateTime_DTs:
817                if op == "+":
818                    n = dateTimeObjects(term)
819                    res = calculateFinalDateTime(res, dt, n, term.datatype, op)
820                    return res
821
822            # rest are invalid types
823            else:
824                raise SPARQLError("Invalid DateTime Operations")
825
826    # handling arithmetic(addition/subtraction) of numeric datatypes (if any)
827    else:
828        res = numeric(expr)
829
830        dt = expr.datatype
831
832        for op, term in zip(e.op, other):
833            n = numeric(term)
834            if isinstance(n, Decimal) and isinstance(res, float):
835                n = float(n)
836            if isinstance(n, float) and isinstance(res, Decimal):
837                res = float(res)
838
839            dt = type_promotion(dt, term.datatype)
840
841            if op == "+":
842                res += n
843            else:
844                res -= n
845
846        return Literal(res, datatype=dt)
847
848
849def RelationalExpression(e: Expr, ctx: Union[QueryContext, FrozenBindings]) -> Literal:
850    expr = e.expr
851    other = e.other
852    op = e.op
853
854    # because of the way the add-expr production handled operator precedence
855    # we sometimes have nothing to do
856    if other is None:
857        return expr
858
859    ops = dict(
860        [
861            (">", lambda x, y: x.__gt__(y)),
862            ("<", lambda x, y: x.__lt__(y)),
863            ("=", lambda x, y: x.eq(y)),
864            ("!=", lambda x, y: x.neq(y)),
865            (">=", lambda x, y: x.__ge__(y)),
866            ("<=", lambda x, y: x.__le__(y)),
867            ("IN", pyop.contains),
868            ("NOT IN", lambda x, y: not pyop.contains(x, y)),
869        ]
870    )
871
872    if op in ("IN", "NOT IN"):
873        res = op == "NOT IN"
874
875        error: Union[bool, SPARQLError] = False
876
877        if other == RDF.nil:
878            other = []
879
880        for x in other:
881            try:
882                if x == expr:
883                    return Literal(True ^ res)
884            except SPARQLError as e:
885                error = e
886        if not error:
887            return Literal(False ^ res)
888        else:
889            # Note on type error: this is because variable is Union[bool, SPARQLError]
890            # type error: Exception must be derived from BaseException
891            raise error  # type: ignore[misc]
892
893    if op not in ("=", "!=", "IN", "NOT IN"):
894        if not isinstance(expr, Literal):
895            raise SPARQLError(
896                "Compare other than =, != of non-literals is an error: %r" % expr
897            )
898        if not isinstance(other, Literal):
899            raise SPARQLError(
900                "Compare other than =, != of non-literals is an error: %r" % other
901            )
902    else:
903        if not isinstance(expr, Node):
904            raise SPARQLError("I cannot compare this non-node: %r" % expr)
905        if not isinstance(other, Node):
906            raise SPARQLError("I cannot compare this non-node: %r" % other)
907
908    if isinstance(expr, Literal) and isinstance(other, Literal):
909        if (
910            expr.datatype is not None
911            and expr.datatype not in XSD_DTs
912            and other.datatype is not None
913            and other.datatype not in XSD_DTs
914        ):
915            # in SPARQL for non-XSD DT Literals we can only do =,!=
916            if op not in ("=", "!="):
917                raise SPARQLError("Can only do =,!= comparisons of non-XSD Literals")
918
919    try:
920        r = ops[op](expr, other)
921        if r == NotImplemented:
922            raise SPARQLError("Error when comparing")
923    except TypeError as te:
924        raise SPARQLError(*te.args)
925    return Literal(r)
926
927
928def ConditionalAndExpression(
929    e: Expr, ctx: Union[QueryContext, FrozenBindings]
930) -> Literal:
931    # TODO: handle returned errors
932
933    expr = e.expr
934    other = e.other
935
936    # because of the way the add-expr production handled operator precedence
937    # we sometimes have nothing to do
938    if other is None:
939        return expr
940
941    return Literal(all(EBV(x) for x in [expr] + other))
942
943
944def ConditionalOrExpression(
945    e: Expr, ctx: Union[QueryContext, FrozenBindings]
946) -> Literal:
947    # TODO: handle errors
948
949    expr = e.expr
950    other = e.other
951
952    # because of the way the add-expr production handled operator precedence
953    # we sometimes have nothing to do
954    if other is None:
955        return expr
956    # A logical-or that encounters an error on only one branch
957    # will return TRUE if the other branch is TRUE and an error
958    # if the other branch is FALSE.
959    error = None
960    for x in [expr] + other:
961        try:
962            if EBV(x):
963                return Literal(True)
964        except SPARQLError as e:
965            error = e
966    if error:
967        raise error
968    return Literal(False)
969
970
971def not_(arg) -> Expr:
972    return Expr("UnaryNot", UnaryNot, expr=arg)
973
974
975def and_(*args: Expr) -> Expr:
976    if len(args) == 1:
977        return args[0]
978
979    return Expr(
980        "ConditionalAndExpression",
981        ConditionalAndExpression,
982        expr=args[0],
983        other=list(args[1:]),
984    )
985
986
987TrueFilter = Expr("TrueFilter", lambda _1, _2: Literal(True))
988
989
990def simplify(expr: Any) -> Any:
991    if isinstance(expr, ParseResults) and len(expr) == 1:
992        return simplify(expr[0])
993
994    if isinstance(expr, (list, ParseResults)):
995        return list(map(simplify, expr))
996    if not isinstance(expr, CompValue):
997        return expr
998    if expr.name.endswith("Expression"):
999        if expr.other is None:
1000            return simplify(expr.expr)
1001
1002    for k in expr.keys():
1003        expr[k] = simplify(expr[k])
1004        # expr['expr']=simplify(expr.expr)
1005        #    expr['other']=simplify(expr.other)
1006
1007    return expr
1008
1009
1010def literal(s: Literal) -> Literal:
1011    if not isinstance(s, Literal):
1012        raise SPARQLError("Non-literal passed as string: %r" % s)
1013    return s
1014
1015
1016def datetime(e: Literal) -> py_datetime.datetime:
1017    if not isinstance(e, Literal):
1018        raise SPARQLError("Non-literal passed as datetime: %r" % e)
1019    if not e.datatype == XSD.dateTime:
1020        raise SPARQLError("Literal with wrong datatype passed as datetime: %r" % e)
1021    return e.toPython()
1022
1023
1024def date(e: Literal) -> py_datetime.date:
1025    if not isinstance(e, Literal):
1026        raise SPARQLError("Non-literal passed as date: %r" % e)
1027    if e.datatype not in (XSD.date, XSD.dateTime):
1028        raise SPARQLError("Literal with wrong datatype passed as date: %r" % e)
1029    result = e.toPython()
1030    if isinstance(result, py_datetime.datetime):
1031        return result.date()
1032    return result
1033
1034
1035def string(s: Literal) -> Literal:
1036    """
1037    Make sure the passed thing is a string literal
1038    i.e. plain literal, xsd:string literal or lang-tagged literal
1039    """
1040    if not isinstance(s, Literal):
1041        raise SPARQLError("Non-literal passes as string: %r" % s)
1042    if s.datatype and s.datatype != XSD.string:
1043        raise SPARQLError("Non-string datatype-literal passes as string: %r" % s)
1044    return s
1045
1046
1047def numeric(expr: Literal) -> Any:
1048    """
1049    return a number from a literal
1050    http://www.w3.org/TR/xpath20/#promotion
1051
1052    or TypeError
1053    """
1054
1055    if not isinstance(expr, Literal):
1056        raise SPARQLTypeError("%r is not a literal!" % expr)
1057
1058    if expr.datatype not in (
1059        XSD.float,
1060        XSD.double,
1061        XSD.decimal,
1062        XSD.integer,
1063        XSD.nonPositiveInteger,
1064        XSD.negativeInteger,
1065        XSD.nonNegativeInteger,
1066        XSD.positiveInteger,
1067        XSD.unsignedLong,
1068        XSD.unsignedInt,
1069        XSD.unsignedShort,
1070        XSD.unsignedByte,
1071        XSD.long,
1072        XSD.int,
1073        XSD.short,
1074        XSD.byte,
1075    ):
1076        raise SPARQLTypeError("%r does not have a numeric datatype!" % expr)
1077
1078    return expr.toPython()
1079
1080
1081def dateTimeObjects(expr: Literal) -> Any:
1082    """
1083    return a dataTime/date/time/duration/dayTimeDuration/yearMonthDuration python objects from a literal
1084    """
1085    return expr.toPython()
1086
1087
1088# type error: Missing return statement
1089def isCompatibleDateTimeDatatype(  # type: ignore[return]
1090    obj1: Union[py_datetime.date, py_datetime.datetime],
1091    dt1: URIRef,
1092    obj2: Union[Duration, py_datetime.timedelta],
1093    dt2: URIRef,
1094) -> bool:
1095    """
1096    Returns a boolean indicating if first object is compatible
1097    with operation(+/-) over second object.
1098    """
1099    if dt1 == XSD.date:
1100        if dt2 == XSD.yearMonthDuration:
1101            return True
1102        elif dt2 == XSD.dayTimeDuration or dt2 == XSD.Duration:
1103            # checking if the dayTimeDuration has no Time Component
1104            # else it won't be compatible with Date Literal
1105            if "T" in str(obj2):
1106                return False
1107            else:
1108                return True
1109
1110    if dt1 == XSD.time:
1111        if dt2 == XSD.yearMonthDuration:
1112            return False
1113        elif dt2 == XSD.dayTimeDuration or dt2 == XSD.Duration:
1114            # checking if the dayTimeDuration has no Date Component
1115            # (by checking if the format is "PT...." )
1116            # else it won't be compatible with Time Literal
1117            if "T" == str(obj2)[1]:
1118                return True
1119            else:
1120                return False
1121
1122    if dt1 == XSD.dateTime:
1123        # compatible with all
1124        return True
1125
1126
1127def calculateDuration(
1128    obj1: Union[py_datetime.date, py_datetime.datetime],
1129    obj2: Union[py_datetime.date, py_datetime.datetime],
1130) -> Literal:
1131    """
1132    returns the duration Literal between two datetime
1133    """
1134    date1 = obj1
1135    date2 = obj2
1136    # type error: No overload variant of "__sub__" of "datetime" matches argument type "date"
1137    difference = date1 - date2  # type: ignore[operator]
1138    return Literal(difference, datatype=XSD.duration)
1139
1140
1141def calculateFinalDateTime(
1142    obj1: Union[py_datetime.date, py_datetime.datetime],
1143    dt1: URIRef,
1144    obj2: Union[Duration, py_datetime.timedelta],
1145    dt2: URIRef,
1146    operation: str,
1147) -> Literal:
1148    """
1149    Calculates the final dateTime/date/time resultant after addition/
1150    subtraction of duration/dayTimeDuration/yearMonthDuration
1151    """
1152
1153    # checking compatibility of datatypes (duration types and date/time/dateTime)
1154    if isCompatibleDateTimeDatatype(obj1, dt1, obj2, dt2):
1155        # proceed
1156        if operation == "-":
1157            ans = obj1 - obj2
1158            return Literal(ans, datatype=dt1)
1159        else:
1160            ans = obj1 + obj2
1161            return Literal(ans, datatype=dt1)
1162
1163    else:
1164        raise SPARQLError("Incompatible Data types to DateTime Operations")
1165
1166
1167@overload
1168def EBV(rt: Literal) -> bool: ...
1169
1170
1171@overload
1172def EBV(rt: Union[Variable, IdentifiedNode, SPARQLError, Expr]) -> NoReturn: ...
1173
1174
1175@overload
1176def EBV(rt: Union[Identifier, SPARQLError, Expr]) -> Union[bool, NoReturn]: ...
1177
1178
1179def EBV(rt: Union[Identifier, SPARQLError, Expr]) -> bool:
1180    """Effective Boolean Value (EBV)
1181
1182    * If the argument is a typed literal with a datatype of xsd:boolean,
1183      the EBV is the value of that argument.
1184    * If the argument is a plain literal or a typed literal with a
1185      datatype of xsd:string, the EBV is false if the operand value
1186      has zero length; otherwise the EBV is true.
1187    * If the argument is a numeric type or a typed literal with a datatype
1188      derived from a numeric type, the EBV is false if the operand value is
1189      NaN or is numerically equal to zero; otherwise the EBV is true.
1190    * All other arguments, including unbound arguments, produce a type error.
1191    """
1192
1193    if isinstance(rt, Literal):
1194        if rt.datatype == XSD.boolean:
1195            return rt.toPython()
1196
1197        elif rt.datatype == XSD.string or rt.datatype is None:
1198            return len(rt) > 0
1199
1200        else:
1201            pyRT = rt.toPython()
1202
1203            if isinstance(pyRT, Literal):
1204                # Type error, see: http://www.w3.org/TR/rdf-sparql-query/#ebv
1205                raise SPARQLTypeError(
1206                    "http://www.w3.org/TR/rdf-sparql-query/#ebv - ' + \
1207                    'Could not determine the EBV for : %r"
1208                    % rt
1209                )
1210            else:
1211                return bool(pyRT)
1212
1213    else:
1214        raise SPARQLTypeError(
1215            "http://www.w3.org/TR/rdf-sparql-query/#ebv - ' + \
1216            'Only literals have Boolean values! %r"
1217            % rt
1218        )
1219
1220
1221def _lang_range_check(range: Literal, lang: Literal) -> bool:
1222    """
1223    Implementation of the extended filtering algorithm, as defined in point
1224    3.3.2, of [RFC 4647](http://www.rfc-editor.org/rfc/rfc4647.txt), on
1225    matching language ranges and language tags.
1226    Needed to handle the `rdf:PlainLiteral` datatype.
1227
1228    Args:
1229        range: language range
1230        lang: language tag
1231
1232    Author: [Ivan Herman](http://www.w3.org/People/Ivan/)
1233
1234    Taken from [`RDFClosure/RestrictedDatatype.py`](http://dev.w3.org/2004/PythonLib-IH/RDFClosure/RestrictedDatatype.py)
1235    """
1236
1237    def _match(r: str, l_: str) -> bool:
1238        """
1239        Matching of a range and language item: either range is a wildcard
1240        or the two are equal
1241
1242        Args:
1243            r: language range item
1244            l_: language tag item
1245        """
1246        return r == "*" or r == l_
1247
1248    rangeList = range.strip().lower().split("-")
1249    langList = lang.strip().lower().split("-")
1250    if not _match(rangeList[0], langList[0]):
1251        return False
1252    if len(rangeList) > len(langList):
1253        return False
1254
1255    return all(_match(*x) for x in zip(rangeList, langList))