Skip to content

# `` operators

This contains evaluation functions for expressions

They get bound as instances-methods to the CompValue objects from parserutils using setEvalFn

Functions:

- `AdditiveExpression` –
- `Builtin_ABS` –

  http://www.w3.org/TR/sparql11-query/#func-abs

- `Builtin_BNODE` –

  http://www.w3.org/TR/sparql11-query/#func-bnode

- `Builtin_BOUND` –

  http://www.w3.org/TR/sparql11-query/#func-bound

- `Builtin_CEIL` –

  http://www.w3.org/TR/sparql11-query/#func-ceil

- `Builtin_COALESCE` –

  http://www.w3.org/TR/sparql11-query/#func-coalesce

- `Builtin_CONCAT` –

  http://www.w3.org/TR/sparql11-query/#func-concat

- `Builtin_CONTAINS` –

  http://www.w3.org/TR/sparql11-query/#func-strcontains

- `Builtin_DATATYPE` –
- `Builtin_DAY` –
- `Builtin_ENCODE_FOR_URI` –
- `Builtin_EXISTS` –
- `Builtin_FLOOR` –

  http://www.w3.org/TR/sparql11-query/#func-floor

- `Builtin_HOURS` –
- `Builtin_IF` –

  http://www.w3.org/TR/sparql11-query/#func-if

- `Builtin_IRI` –

  http://www.w3.org/TR/sparql11-query/#func-iri

- `Builtin_LANG` –

  http://www.w3.org/TR/sparql11-query/#func-lang

- `Builtin_LANGMATCHES` –

  http://www.w3.org/TR/sparql11-query/#func-langMatches

- `Builtin_LCASE` –
- `Builtin_MD5` –
- `Builtin_MINUTES` –
- `Builtin_MONTH` –
- `Builtin_NOW` –

  http://www.w3.org/TR/sparql11-query/#func-now

- `Builtin_RAND` –

  http://www.w3.org/TR/sparql11-query/#idp2133952

- `Builtin_REGEX` –

  http://www.w3.org/TR/sparql11-query/#func-regex

- `Builtin_REPLACE` –

  http://www.w3.org/TR/sparql11-query/#func-substr

- `Builtin_ROUND` –

  http://www.w3.org/TR/sparql11-query/#func-round

- `Builtin_SECONDS` –

  http://www.w3.org/TR/sparql11-query/#func-seconds

- `Builtin_SHA1` –
- `Builtin_SHA256` –
- `Builtin_SHA384` –
- `Builtin_SHA512` –
- `Builtin_STR` –
- `Builtin_STRAFTER` –

  http://www.w3.org/TR/sparql11-query/#func-strafter

- `Builtin_STRBEFORE` –

  http://www.w3.org/TR/sparql11-query/#func-strbefore

- `Builtin_STRDT` –

  http://www.w3.org/TR/sparql11-query/#func-strdt

- `Builtin_STRENDS` –

  http://www.w3.org/TR/sparql11-query/#func-strends

- `Builtin_STRLANG` –

  http://www.w3.org/TR/sparql11-query/#func-strlang

- `Builtin_STRLEN` –
- `Builtin_STRSTARTS` –

  http://www.w3.org/TR/sparql11-query/#func-strstarts

- `Builtin_STRUUID` –

  http://www.w3.org/TR/sparql11-query/#func-strdt

- `Builtin_SUBSTR` –

  http://www.w3.org/TR/sparql11-query/#func-substr

- `Builtin_TIMEZONE` –

  http://www.w3.org/TR/sparql11-query/#func-timezone

- `Builtin_TZ` –
- `Builtin_UCASE` –
- `Builtin_UUID` –

  http://www.w3.org/TR/sparql11-query/#func-strdt

- `Builtin_YEAR` –
- `Builtin_isBLANK` –
- `Builtin_isIRI` –
- `Builtin_isLITERAL` –
- `Builtin_isNUMERIC` –
- `Builtin_sameTerm` –
- `ConditionalAndExpression` –
- `ConditionalOrExpression` –
- `EBV` –

  Effective Boolean Value (EBV)

- `Function` –

  Custom functions and casts

- `MultiplicativeExpression` –
- `RelationalExpression` –
- `UnaryMinus` –
- `UnaryNot` –
- `UnaryPlus` –
- `and_` –
- `calculateDuration` –

  returns the duration Literal between two datetime

- `calculateFinalDateTime` –

  Calculates the final dateTime/date/time resultant after addition/

- `custom_function` –

  Decorator version of :func:`register_custom_function`.

- `date` –
- `dateTimeObjects` –

  return a dataTime/date/time/duration/dayTimeDuration/yearMonthDuration python objects from a literal

- `datetime` –
- `default_cast` –
- `isCompatibleDateTimeDatatype` –

  Returns a boolean indicating if first object is compatible

- `literal` –
- `not_` –
- `numeric` –

  return a number from a literal

- `register_custom_function` –

  Register a custom SPARQL function.

- `simplify` –
- `string` –

  Make sure the passed thing is a string literal

- `unregister_custom_function` –

  The ‘func’ argument is included for compatibility with existing code.

Attributes:

- `TrueFilter` –

## `` TrueFilter `module-attribute`

    TrueFilter = Expr('TrueFilter', lambda _1, _2: Literal(True))

## `` AdditiveExpression

    AdditiveExpression(e: Expr, ctx: Union[QueryContext, FrozenBindings]) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    776
    777
    778
    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789
    790
    791
    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808
    809
    810
    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833
    834
    835
    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846

    def AdditiveExpression(e: Expr, ctx: Union[QueryContext, FrozenBindings]) -> Literal:  # type: ignore[return]
        expr = e.expr
        other = e.other

        # because of the way the add-expr production handled operator precedence
        # we sometimes have nothing to do
        if other is None:
            return expr

        # handling arithmetic(addition/subtraction) of dateTime, date, time
        # and duration datatypes (if any)
        if hasattr(expr, "datatype") and (
            expr.datatype in XSD_DateTime_DTs or expr.datatype in XSD_Duration_DTs
        ):
            res = dateTimeObjects(expr)
            dt = expr.datatype

            for op, term in zip(e.op, other):
                # check if operation is datetime,date,time operation over
                # another datetime,date,time datatype
                if dt in XSD_DateTime_DTs and dt == term.datatype and op == "-":
                    # checking if there are more than one datetime operands -
                    # in that case it doesn't make sense for example
                    # ( dateTime1 - dateTime2 - dateTime3 ) is an invalid operation
                    if len(other) > 1:
                        error_message = "Can't evaluate multiple %r arguments"
                        # type error: Too many arguments for "SPARQLError"
                        raise SPARQLError(error_message, dt.datatype)  # type: ignore[call-arg]
                    else:
                        n = dateTimeObjects(term)
                        res = calculateDuration(res, n)
                        return res

                # datetime,date,time +/- duration,dayTimeDuration,yearMonthDuration
                elif dt in XSD_DateTime_DTs and term.datatype in XSD_Duration_DTs:
                    n = dateTimeObjects(term)
                    res = calculateFinalDateTime(res, dt, n, term.datatype, op)
                    return res

                # duration,dayTimeDuration,yearMonthDuration + datetime,date,time
                elif dt in XSD_Duration_DTs and term.datatype in XSD_DateTime_DTs:
                    if op == "+":
                        n = dateTimeObjects(term)
                        res = calculateFinalDateTime(res, dt, n, term.datatype, op)
                        return res

                # rest are invalid types
                else:
                    raise SPARQLError("Invalid DateTime Operations")

        # handling arithmetic(addition/subtraction) of numeric datatypes (if any)
        else:
            res = numeric(expr)

            dt = expr.datatype

            for op, term in zip(e.op, other):
                n = numeric(term)
                if isinstance(n, Decimal) and isinstance(res, float):
                    n = float(n)
                if isinstance(n, float) and isinstance(res, Decimal):
                    res = float(res)

                dt = type_promotion(dt, term.datatype)

                if op == "+":
                    res += n
                else:
                    res -= n

            return Literal(res, datatype=dt)

## `` Builtin_ABS

    Builtin_ABS(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-abs

Source code in `rdflib/plugins/sparql/operators.py`

    104
    105
    106
    107
    108
    109

    def Builtin_ABS(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-abs
        """

        return Literal(abs(numeric(expr.arg)))

## `` Builtin_BNODE

    Builtin_BNODE(expr, ctx) -> BNode

http://www.w3.org/TR/sparql11-query/#func-bnode

Source code in `rdflib/plugins/sparql/operators.py`

     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101

    def Builtin_BNODE(expr, ctx) -> BNode:
        """
        http://www.w3.org/TR/sparql11-query/#func-bnode
        """

        a = expr.arg

        if a is None:
            return BNode()

        if isinstance(a, Literal):
            return ctx.bnodes[a]  # defaultdict does the right thing

        raise SPARQLError("BNode function only accepts no argument or literal/string")

## `` Builtin_BOUND

    Builtin_BOUND(e: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-bound

Source code in `rdflib/plugins/sparql/operators.py`

    571
    572
    573
    574
    575
    576
    577

    def Builtin_BOUND(e: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-bound
        """
        n = e.get("arg", variables=True)

        return Literal(not isinstance(n, Variable))

## `` Builtin_CEIL

    Builtin_CEIL(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-ceil

Source code in `rdflib/plugins/sparql/operators.py`

    179
    180
    181
    182
    183
    184
    185

    def Builtin_CEIL(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-ceil
        """

        l_ = expr.arg
        return Literal(int(math.ceil(numeric(l_))), datatype=l_.datatype)

## `` Builtin_COALESCE

    Builtin_COALESCE(expr: Expr, ctx)

http://www.w3.org/TR/sparql11-query/#func-coalesce

Source code in `rdflib/plugins/sparql/operators.py`

    169
    170
    171
    172
    173
    174
    175
    176

    def Builtin_COALESCE(expr: Expr, ctx):
        """
        http://www.w3.org/TR/sparql11-query/#func-coalesce
        """
        for x in expr.get("arg", variables=True):
            if x is not None and not isinstance(x, (SPARQLError, Variable)):
                return x
        raise SPARQLError("COALESCE got no arguments that did not evaluate to an error")

## `` Builtin_CONCAT

    Builtin_CONCAT(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-concat

Source code in `rdflib/plugins/sparql/operators.py`

    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303

    def Builtin_CONCAT(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-concat
        """

        # dt/lang passed on only if they all match

        dt = set(x.datatype for x in expr.arg if isinstance(x, Literal))
        # type error: Incompatible types in assignment (expression has type "Optional[str]", variable has type "Set[Optional[str]]")
        dt = dt.pop() if len(dt) == 1 else None  # type: ignore[assignment]

        lang = set(x.language for x in expr.arg if isinstance(x, Literal))
        # type error: error: Incompatible types in assignment (expression has type "Optional[str]", variable has type "Set[Optional[str]]")
        lang = lang.pop() if len(lang) == 1 else None  # type: ignore[assignment]

        # NOTE on type errors: this is because same variable is used for two incompatibel types
        # type error: Argument "datatype" to "Literal" has incompatible type "Set[Any]"; expected "Optional[str]"  [arg-type]
        # type error: Argument "lang" to "Literal" has incompatible type "Set[Any]"; expected "Optional[str]"
        return Literal("".join(string(x) for x in expr.arg), datatype=dt, lang=lang)  # type: ignore[arg-type]

## `` Builtin_CONTAINS

    Builtin_CONTAINS(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-strcontains

Source code in `rdflib/plugins/sparql/operators.py`

    370
    371
    372
    373
    374
    375
    376
    377
    378
    379

    def Builtin_CONTAINS(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-strcontains
        """

        a = expr.arg1
        b = expr.arg2
        _compatibleStrings(a, b)

        return Literal(b in a)

## `` Builtin_DATATYPE

    Builtin_DATATYPE(e: Expr, ctx) -> Optional[str]

Source code in `rdflib/plugins/sparql/operators.py`

    554
    555
    556
    557
    558
    559
    560
    561
    562

    def Builtin_DATATYPE(e: Expr, ctx) -> Optional[str]:
        l_ = e.arg
        if not isinstance(l_, Literal):
            raise SPARQLError("Can only get datatype of literal: %r" % l_)
        if l_.language:
            return RDF.langString
        if not l_.datatype and not l_.language:
            return XSD.string
        return l_.datatype

## `` Builtin_DAY

    Builtin_DAY(e: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    453
    454
    455

    def Builtin_DAY(e: Expr, ctx) -> Literal:
        d = date(e.arg)
        return Literal(d.day)

## `` Builtin_ENCODE_FOR_URI

    Builtin_ENCODE_FOR_URI(expr: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    382
    383

    def Builtin_ENCODE_FOR_URI(expr: Expr, ctx) -> Literal:
        return Literal(quote(string(expr.arg).encode("utf-8"), safe=""))

## `` Builtin_EXISTS

    Builtin_EXISTS(e: Expr, ctx: FrozenBindings) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591

    def Builtin_EXISTS(e: Expr, ctx: FrozenBindings) -> Literal:
        # damn...
        from rdflib.plugins.sparql.evaluate import evalPart

        exists = e.name == "Builtin_EXISTS"

        # type error: Incompatible types in assignment (expression has type "QueryContext", variable has type "FrozenBindings")
        ctx = ctx.ctx.thaw(ctx)  # type: ignore[assignment] # hmm
        # type error: Argument 1 to "evalPart" has incompatible type "FrozenBindings"; expected "QueryContext"
        for x in evalPart(ctx, e.graph):  # type: ignore[arg-type]
            return Literal(exists)
        return Literal(not exists)

## `` Builtin_FLOOR

    Builtin_FLOOR(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-floor

Source code in `rdflib/plugins/sparql/operators.py`

    188
    189
    190
    191
    192
    193

    def Builtin_FLOOR(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-floor
        """
        l_ = expr.arg
        return Literal(int(math.floor(numeric(l_))), datatype=l_.datatype)

## `` Builtin_HOURS

    Builtin_HOURS(e: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    458
    459
    460

    def Builtin_HOURS(e: Expr, ctx) -> Literal:
        d = datetime(e.arg)
        return Literal(d.hour)

## `` Builtin_IF

    Builtin_IF(expr: Expr, ctx)

http://www.w3.org/TR/sparql11-query/#func-if

Source code in `rdflib/plugins/sparql/operators.py`

    112
    113
    114
    115
    116
    117

    def Builtin_IF(expr: Expr, ctx):
        """
        http://www.w3.org/TR/sparql11-query/#func-if
        """

        return expr.arg2 if EBV(expr.arg1) else expr.arg3

## `` Builtin_IRI

    Builtin_IRI(expr: Expr, ctx: FrozenBindings) -> URIRef

http://www.w3.org/TR/sparql11-query/#func-iri

Source code in `rdflib/plugins/sparql/operators.py`

    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65

    def Builtin_IRI(expr: Expr, ctx: FrozenBindings) -> URIRef:
        """
        http://www.w3.org/TR/sparql11-query/#func-iri
        """

        a = expr.arg

        if isinstance(a, URIRef):
            return a
        if isinstance(a, Literal):
            # type error: Item "None" of "Optional[Prologue]" has no attribute "absolutize"
            # type error: Incompatible return value type (got "Union[CompValue, str, None, Any]", expected "URIRef")
            return ctx.prologue.absolutize(URIRef(a))  # type: ignore[union-attr,return-value]

        raise SPARQLError("IRI function only accepts URIRefs or Literals/Strings!")

## `` Builtin_LANG

    Builtin_LANG(e: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-lang

Returns the language tag of ltrl, if it has one. It returns “” if ltrl has no language tag. Note that the RDF data model does not include literals with an empty language tag.

Source code in `rdflib/plugins/sparql/operators.py`

    542
    543
    544
    545
    546
    547
    548
    549
    550
    551

    def Builtin_LANG(e: Expr, ctx) -> Literal:
        """http://www.w3.org/TR/sparql11-query/#func-lang

        Returns the language tag of ltrl, if it has one. It returns "" if ltrl has
        no language tag. Note that the RDF data model does not include literals
        with an empty language tag.
        """

        l_ = literal(e.arg)
        return Literal(l_.language or "")

## `` Builtin_LANGMATCHES

    Builtin_LANGMATCHES(e: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-langMatches

Source code in `rdflib/plugins/sparql/operators.py`

    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433

    def Builtin_LANGMATCHES(e: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-langMatches

        """
        langTag = string(e.arg1)
        langRange = string(e.arg2)

        if str(langTag) == "":
            return Literal(False)  # nothing matches empty!

        return Literal(_lang_range_check(langRange, langTag))

## `` Builtin_LCASE

    Builtin_LCASE(e: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    415
    416
    417
    418

    def Builtin_LCASE(e: Expr, ctx) -> Literal:
        l_ = string(e.arg)

        return Literal(l_.lower(), datatype=l_.datatype, lang=l_.language)

## `` Builtin_MD5

    Builtin_MD5(expr: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    144
    145
    146

    def Builtin_MD5(expr: Expr, ctx) -> Literal:
        s = string(expr.arg).encode("utf-8")
        return Literal(hashlib.md5(s).hexdigest())

## `` Builtin_MINUTES

    Builtin_MINUTES(e: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    463
    464
    465

    def Builtin_MINUTES(e: Expr, ctx) -> Literal:
        d = datetime(e.arg)
        return Literal(d.minute)

## `` Builtin_MONTH

    Builtin_MONTH(e: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    448
    449
    450

    def Builtin_MONTH(e: Expr, ctx) -> Literal:
        d = date(e.arg)
        return Literal(d.month)

## `` Builtin_NOW

    Builtin_NOW(e: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-now

Source code in `rdflib/plugins/sparql/operators.py`

    436
    437
    438
    439
    440

    def Builtin_NOW(e: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-now
        """
        return Literal(ctx.now)

## `` Builtin_RAND

    Builtin_RAND(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#idp2133952

Source code in `rdflib/plugins/sparql/operators.py`

    120
    121
    122
    123
    124
    125

    def Builtin_RAND(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#idp2133952
        """

        return Literal(random.random())

## `` Builtin_REGEX

    Builtin_REGEX(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-regex Invokes the XPath fn:matches function to match text against a regular expression pattern. The regular expression language is defined in XQuery 1.0 and XPath 2.0 Functions and Operators section 7.6.1 Regular Expression Syntax

Source code in `rdflib/plugins/sparql/operators.py`

    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231

    def Builtin_REGEX(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-regex
        Invokes the XPath fn:matches function to match text against a regular
        expression pattern.
        The regular expression language is defined in XQuery 1.0 and XPath 2.0
        Functions and Operators section 7.6.1 Regular Expression Syntax
        """

        text = string(expr.text)
        pattern = string(expr.pattern)
        flags = expr.flags

        cFlag = 0
        if flags:
            # Maps XPath REGEX flags (http://www.w3.org/TR/xpath-functions/#flags)
            # to Python's re flags
            flagMap = dict([("i", re.IGNORECASE), ("s", re.DOTALL), ("m", re.MULTILINE)])
            cFlag = reduce(pyop.or_, [flagMap.get(f, 0) for f in flags])

        return Literal(bool(re.search(str(pattern), text, cFlag)))

## `` Builtin_REPLACE

    Builtin_REPLACE(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-substr

Source code in `rdflib/plugins/sparql/operators.py`

    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

    def Builtin_REPLACE(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-substr
        """
        text = string(expr.arg)
        pattern = string(expr.pattern)
        replacement = string(expr.replacement)
        flags = expr.flags

        # python uses \1, xpath/sparql uses $1
        # type error: Incompatible types in assignment (expression has type "str", variable has type "Literal")
        replacement = re.sub("\\$([0-9]*)", r"\\\1", replacement)  # type: ignore[assignment]

        cFlag = 0
        if flags:
            # Maps XPath REGEX flags (http://www.w3.org/TR/xpath-functions/#flags)
            # to Python's re flags
            flagMap = dict([("i", re.IGNORECASE), ("s", re.DOTALL), ("m", re.MULTILINE)])
            cFlag = reduce(pyop.or_, [flagMap.get(f, 0) for f in flags])

            # @@FIXME@@ either datatype OR lang, NOT both

        return Literal(
            re.sub(str(pattern), replacement, text, cFlag),
            datatype=text.datatype,
            lang=text.language,
        )

## `` Builtin_ROUND

    Builtin_ROUND(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-round

Source code in `rdflib/plugins/sparql/operators.py`

    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208

    def Builtin_ROUND(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-round
        """

        # This used to be just math.bound
        # but in py3k bound was changed to
        # "round-to-even" behaviour
        # this is an ugly work-around
        l_ = expr.arg
        v = numeric(l_)
        v = int(Decimal(v).quantize(1, ROUND_HALF_UP if v > 0 else ROUND_HALF_DOWN))
        return Literal(v, datatype=l_.datatype)

## `` Builtin_SECONDS

    Builtin_SECONDS(e: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-seconds

Source code in `rdflib/plugins/sparql/operators.py`

    468
    469
    470
    471
    472
    473
    474
    475
    476

    def Builtin_SECONDS(e: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-seconds
        """
        d = datetime(e.arg)
        result_value = Decimal(d.second)
        if d.microsecond:
            result_value += Decimal(d.microsecond) / Decimal(1000000)
        return Literal(result_value, datatype=XSD.decimal)

## `` Builtin_SHA1

    Builtin_SHA1(expr: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    149
    150
    151

    def Builtin_SHA1(expr: Expr, ctx) -> Literal:
        s = string(expr.arg).encode("utf-8")
        return Literal(hashlib.sha1(s).hexdigest())

## `` Builtin_SHA256

    Builtin_SHA256(expr: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    154
    155
    156

    def Builtin_SHA256(expr: Expr, ctx) -> Literal:
        s = string(expr.arg).encode("utf-8")
        return Literal(hashlib.sha256(s).hexdigest())

## `` Builtin_SHA384

    Builtin_SHA384(expr: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    159
    160
    161

    def Builtin_SHA384(expr: Expr, ctx) -> Literal:
        s = string(expr.arg).encode("utf-8")
        return Literal(hashlib.sha384(s).hexdigest())

## `` Builtin_SHA512

    Builtin_SHA512(expr: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    164
    165
    166

    def Builtin_SHA512(expr: Expr, ctx) -> Literal:
        s = string(expr.arg).encode("utf-8")
        return Literal(hashlib.sha512(s).hexdigest())

## `` Builtin_STR

    Builtin_STR(e: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    408
    409
    410
    411
    412

    def Builtin_STR(e: Expr, ctx) -> Literal:
        arg = e.arg
        if isinstance(arg, SPARQLError):
            raise arg
        return Literal(str(arg))  # plain literal

## `` Builtin_STRAFTER

    Builtin_STRAFTER(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-strafter

Source code in `rdflib/plugins/sparql/operators.py`

    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367

    def Builtin_STRAFTER(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-strafter
        """

        a = expr.arg1
        b = expr.arg2
        _compatibleStrings(a, b)

        i = a.find(b)
        if i == -1:
            return Literal("")
        else:
            return Literal(a[i + len(b) :], lang=a.language, datatype=a.datatype)

## `` Builtin_STRBEFORE

    Builtin_STRBEFORE(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-strbefore

Source code in `rdflib/plugins/sparql/operators.py`

    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351

    def Builtin_STRBEFORE(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-strbefore
        """

        a = expr.arg1
        b = expr.arg2
        _compatibleStrings(a, b)

        i = a.find(b)
        if i == -1:
            return Literal("")
        else:
            return Literal(a[:i], lang=a.language, datatype=a.datatype)

## `` Builtin_STRDT

    Builtin_STRDT(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-strdt

Source code in `rdflib/plugins/sparql/operators.py`

    263
    264
    265
    266
    267
    268

    def Builtin_STRDT(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-strdt
        """

        return Literal(str(expr.arg1), datatype=expr.arg2)

## `` Builtin_STRENDS

    Builtin_STRENDS(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-strends

Source code in `rdflib/plugins/sparql/operators.py`

    326
    327
    328
    329
    330
    331
    332
    333
    334
    335

    def Builtin_STRENDS(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-strends
        """
        a = expr.arg1
        b = expr.arg2

        _compatibleStrings(a, b)

        return Literal(a.endswith(b))

## `` Builtin_STRLANG

    Builtin_STRLANG(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-strlang

Source code in `rdflib/plugins/sparql/operators.py`

    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282

    def Builtin_STRLANG(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-strlang
        """

        s = string(expr.arg1)
        if s.language or s.datatype:
            raise SPARQLError("STRLANG expects a simple literal")

        # TODO: normalisation of lang tag to lower-case
        # should probably happen in literal __init__
        return Literal(str(s), lang=str(expr.arg2).lower())

## `` Builtin_STRLEN

    Builtin_STRLEN(e: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    402
    403
    404
    405

    def Builtin_STRLEN(e: Expr, ctx) -> Literal:
        l_ = string(e.arg)

        return Literal(len(l_))

## `` Builtin_STRSTARTS

    Builtin_STRSTARTS(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-strstarts

Source code in `rdflib/plugins/sparql/operators.py`

    314
    315
    316
    317
    318
    319
    320
    321
    322
    323

    def Builtin_STRSTARTS(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-strstarts
        """

        a = expr.arg1
        b = expr.arg2
        _compatibleStrings(a, b)

        return Literal(a.startswith(b))

## `` Builtin_STRUUID

    Builtin_STRUUID(expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-strdt

Source code in `rdflib/plugins/sparql/operators.py`

    136
    137
    138
    139
    140
    141

    def Builtin_STRUUID(expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-strdt
        """

        return Literal(str(uuid.uuid4()))

## `` Builtin_SUBSTR

    Builtin_SUBSTR(expr: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-substr

Source code in `rdflib/plugins/sparql/operators.py`

    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399

    def Builtin_SUBSTR(expr: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-substr
        """

        a = string(expr.arg)

        start = numeric(expr.start) - 1

        length = expr.length
        if length is not None:
            length = numeric(length) + start

        return Literal(a[start:length], lang=a.language, datatype=a.datatype)

## `` Builtin_TIMEZONE

    Builtin_TIMEZONE(e: Expr, ctx) -> Literal

http://www.w3.org/TR/sparql11-query/#func-timezone

Returns:

- `Literal` –

  The timezone part of arg as an xsd:dayTimeDuration.

Source code in `rdflib/plugins/sparql/operators.py`

    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

    def Builtin_TIMEZONE(e: Expr, ctx) -> Literal:
        """
        http://www.w3.org/TR/sparql11-query/#func-timezone

        Returns:
            The timezone part of arg as an xsd:dayTimeDuration.

        Raises:
            An error if there is no timezone.
        """
        dt = datetime(e.arg)
        if not dt.tzinfo:
            raise SPARQLError("datatime has no timezone: %r" % dt)

        delta = dt.utcoffset()

        # type error: Item "None" of "Optional[timedelta]" has no attribute "days"
        d = delta.days  # type: ignore[union-attr]
        # type error: Item "None" of "Optional[timedelta]" has no attribute "seconds"
        s = delta.seconds  # type: ignore[union-attr]
        neg = ""

        if d < 0:
            s = -24 * 60 * 60 * d - s
            d = 0
            neg = "-"

        h = s / (60 * 60)
        m = (s - h * 60 * 60) / 60
        s = s - h * 60 * 60 - m * 60

        tzdelta = "%sP%sT%s%s%s" % (
            neg,
            "%dD" % d if d else "",
            "%dH" % h if h else "",
            "%dM" % m if m else "",
            "%dS" % s if not d and not h and not m else "",
        )

        return Literal(tzdelta, datatype=XSD.dayTimeDuration)

## `` Builtin_TZ

    Builtin_TZ(e: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533

    def Builtin_TZ(e: Expr, ctx) -> Literal:
        d = datetime(e.arg)
        if not d.tzinfo:
            return Literal("")
        n = d.tzinfo.tzname(d)
        if n is None:
            n = ""
        elif n == "UTC":
            n = "Z"
        elif n.startswith("UTC"):
            # Replace tzname like "UTC-05:00" with simply "-05:00" to match Jena tz fn
            n = n[3:]
        return Literal(n)

## `` Builtin_UCASE

    Builtin_UCASE(e: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    536
    537
    538
    539

    def Builtin_UCASE(e: Expr, ctx) -> Literal:
        l_ = string(e.arg)

        return Literal(l_.upper(), datatype=l_.datatype, lang=l_.language)

## `` Builtin_UUID

    Builtin_UUID(expr: Expr, ctx) -> URIRef

http://www.w3.org/TR/sparql11-query/#func-strdt

Source code in `rdflib/plugins/sparql/operators.py`

    128
    129
    130
    131
    132
    133

    def Builtin_UUID(expr: Expr, ctx) -> URIRef:
        """
        http://www.w3.org/TR/sparql11-query/#func-strdt
        """

        return URIRef(uuid.uuid4().urn)

## `` Builtin_YEAR

    Builtin_YEAR(e: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    443
    444
    445

    def Builtin_YEAR(e: Expr, ctx) -> Literal:
        d = date(e.arg)
        return Literal(d.year)

## `` Builtin_isBLANK

    Builtin_isBLANK(expr: Expr, ctx: FrozenBindings) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    68
    69

    def Builtin_isBLANK(expr: Expr, ctx: FrozenBindings) -> Literal:
        return Literal(isinstance(expr.arg, BNode))

## `` Builtin_isIRI

    Builtin_isIRI(expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    76
    77

    def Builtin_isIRI(expr, ctx) -> Literal:
        return Literal(isinstance(expr.arg, URIRef))

## `` Builtin_isLITERAL

    Builtin_isLITERAL(expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    72
    73

    def Builtin_isLITERAL(expr, ctx) -> Literal:
        return Literal(isinstance(expr.arg, Literal))

## `` Builtin_isNUMERIC

    Builtin_isNUMERIC(expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    80
    81
    82
    83
    84
    85

    def Builtin_isNUMERIC(expr, ctx) -> Literal:
        try:
            numeric(expr.arg)
            return Literal(True)
        except:  # noqa: E722
            return Literal(False)

## `` Builtin_sameTerm

    Builtin_sameTerm(e: Expr, ctx) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    565
    566
    567
    568

    def Builtin_sameTerm(e: Expr, ctx) -> Literal:
        a = e.arg1
        b = e.arg2
        return Literal(a == b)

## `` ConditionalAndExpression

    ConditionalAndExpression(e: Expr, ctx: Union[QueryContext, FrozenBindings]) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    928
    929
    930
    931
    932
    933
    934
    935
    936
    937
    938
    939
    940
    941

    def ConditionalAndExpression(
        e: Expr, ctx: Union[QueryContext, FrozenBindings]
    ) -> Literal:
        # TODO: handle returned errors

        expr = e.expr
        other = e.other

        # because of the way the add-expr production handled operator precedence
        # we sometimes have nothing to do
        if other is None:
            return expr

        return Literal(all(EBV(x) for x in [expr] + other))

## `` ConditionalOrExpression

    ConditionalOrExpression(e: Expr, ctx: Union[QueryContext, FrozenBindings]) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    944
    945
    946
    947
    948
    949
    950
    951
    952
    953
    954
    955
    956
    957
    958
    959
    960
    961
    962
    963
    964
    965
    966
    967
    968

    def ConditionalOrExpression(
        e: Expr, ctx: Union[QueryContext, FrozenBindings]
    ) -> Literal:
        # TODO: handle errors

        expr = e.expr
        other = e.other

        # because of the way the add-expr production handled operator precedence
        # we sometimes have nothing to do
        if other is None:
            return expr
        # A logical-or that encounters an error on only one branch
        # will return TRUE if the other branch is TRUE and an error
        # if the other branch is FALSE.
        error = None
        for x in [expr] + other:
            try:
                if EBV(x):
                    return Literal(True)
            except SPARQLError as e:
                error = e
        if error:
            raise error
        return Literal(False)

## `` EBV

    EBV(rt: Union[Identifier, SPARQLError, Expr]) -> bool

Effective Boolean Value (EBV)

- If the argument is a typed literal with a datatype of xsd:boolean, the EBV is the value of that argument.
- If the argument is a plain literal or a typed literal with a datatype of xsd:string, the EBV is false if the operand value has zero length; otherwise the EBV is true.
- If the argument is a numeric type or a typed literal with a datatype derived from a numeric type, the EBV is false if the operand value is NaN or is numerically equal to zero; otherwise the EBV is true.
- All other arguments, including unbound arguments, produce a type error.

Source code in `rdflib/plugins/sparql/operators.py`

    1179
    1180
    1181
    1182
    1183
    1184
    1185
    1186
    1187
    1188
    1189
    1190
    1191
    1192
    1193
    1194
    1195
    1196
    1197
    1198
    1199
    1200
    1201
    1202
    1203
    1204
    1205
    1206
    1207
    1208
    1209
    1210
    1211
    1212
    1213
    1214
    1215
    1216
    1217
    1218

    def EBV(rt: Union[Identifier, SPARQLError, Expr]) -> bool:
        """Effective Boolean Value (EBV)

        * If the argument is a typed literal with a datatype of xsd:boolean,
          the EBV is the value of that argument.
        * If the argument is a plain literal or a typed literal with a
          datatype of xsd:string, the EBV is false if the operand value
          has zero length; otherwise the EBV is true.
        * If the argument is a numeric type or a typed literal with a datatype
          derived from a numeric type, the EBV is false if the operand value is
          NaN or is numerically equal to zero; otherwise the EBV is true.
        * All other arguments, including unbound arguments, produce a type error.
        """

        if isinstance(rt, Literal):
            if rt.datatype == XSD.boolean:
                return rt.toPython()

            elif rt.datatype == XSD.string or rt.datatype is None:
                return len(rt) > 0

            else:
                pyRT = rt.toPython()

                if isinstance(pyRT, Literal):
                    # Type error, see: http://www.w3.org/TR/rdf-sparql-query/#ebv
                    raise SPARQLTypeError(
                        "http://www.w3.org/TR/rdf-sparql-query/#ebv - ' + \
                        'Could not determine the EBV for : %r"
                        % rt
                    )
                else:
                    return bool(pyRT)

        else:
            raise SPARQLTypeError(
                "http://www.w3.org/TR/rdf-sparql-query/#ebv - ' + \
                'Only literals have Boolean values! %r"
                % rt
            )

## `` Function

    Function(e: Expr, ctx: FrozenBindings) -> Node

Custom functions and casts

Source code in `rdflib/plugins/sparql/operators.py`

    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661

    def Function(e: Expr, ctx: FrozenBindings) -> Node:
        """
        Custom functions and casts
        """
        pair = _CUSTOM_FUNCTIONS.get(e.iri)
        if pair is None:
            # no such function is registered
            raise SPARQLError("Unknown function %r" % e.iri)
        func, raw = pair
        if raw:
            # function expects expression and context
            return func(e, ctx)
        else:
            # function expects the argument list
            try:
                return func(*e.expr)
            except TypeError as ex:
                # wrong argument number
                raise SPARQLError(*ex.args)

## `` MultiplicativeExpression

    MultiplicativeExpression(e: Expr, ctx: Union[QueryContext, FrozenBindings]) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    761
    762
    763
    764
    765
    766
    767
    768
    769
    770
    771
    772

    def MultiplicativeExpression(
        e: Expr, ctx: Union[QueryContext, FrozenBindings]
    ) -> Literal:
        expr = e.expr
        other = e.other

        # because of the way the mul-expr production handled operator precedence
        # we sometimes have nothing to do
        if other is None:
            return expr
        try:
            res: Union[Decimal, float]
            res = Decimal(numeric(expr))
            for op, f in zip(e.op, other):
                f = numeric(f)

                if type(f) == float:  # noqa: E721
                    res = float(res)

                if op == "*":
                    res *= f
                else:
                    res /= f
        except (InvalidOperation, ZeroDivisionError):
            raise SPARQLError("divide by 0")

        return Literal(res)

## `` RelationalExpression

    RelationalExpression(e: Expr, ctx: Union[QueryContext, FrozenBindings]) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    849
    850
    851
    852
    853
    854
    855
    856
    857
    858
    859
    860
    861
    862
    863
    864
    865
    866
    867
    868
    869
    870
    871
    872
    873
    874
    875
    876
    877
    878
    879
    880
    881
    882
    883
    884
    885
    886
    887
    888
    889
    890
    891
    892
    893
    894
    895
    896
    897
    898
    899
    900
    901
    902
    903
    904
    905
    906
    907
    908
    909
    910
    911
    912
    913
    914
    915
    916
    917
    918
    919
    920
    921
    922
    923
    924
    925

    def RelationalExpression(e: Expr, ctx: Union[QueryContext, FrozenBindings]) -> Literal:
        expr = e.expr
        other = e.other
        op = e.op

        # because of the way the add-expr production handled operator precedence
        # we sometimes have nothing to do
        if other is None:
            return expr

        ops = dict(
            [
                (">", lambda x, y: x.__gt__(y)),
                ("<", lambda x, y: x.__lt__(y)),
                ("=", lambda x, y: x.eq(y)),
                ("!=", lambda x, y: x.neq(y)),
                (">=", lambda x, y: x.__ge__(y)),
                ("<=", lambda x, y: x.__le__(y)),
                ("IN", pyop.contains),
                ("NOT IN", lambda x, y: not pyop.contains(x, y)),
            ]
        )

        if op in ("IN", "NOT IN"):
            res = op == "NOT IN"

            error: Union[bool, SPARQLError] = False

            if other == RDF.nil:
                other = []

            for x in other:
                try:
                    if x == expr:
                        return Literal(True ^ res)
                except SPARQLError as e:
                    error = e
            if not error:
                return Literal(False ^ res)
            else:
                # Note on type error: this is because variable is Union[bool, SPARQLError]
                # type error: Exception must be derived from BaseException
                raise error  # type: ignore[misc]

        if op not in ("=", "!=", "IN", "NOT IN"):
            if not isinstance(expr, Literal):
                raise SPARQLError(
                    "Compare other than =, != of non-literals is an error: %r" % expr
                )
            if not isinstance(other, Literal):
                raise SPARQLError(
                    "Compare other than =, != of non-literals is an error: %r" % other
                )
        else:
            if not isinstance(expr, Node):
                raise SPARQLError("I cannot compare this non-node: %r" % expr)
            if not isinstance(other, Node):
                raise SPARQLError("I cannot compare this non-node: %r" % other)

        if isinstance(expr, Literal) and isinstance(other, Literal):
            if (
                expr.datatype is not None
                and expr.datatype not in XSD_DTs
                and other.datatype is not None
                and other.datatype not in XSD_DTs
            ):
                # in SPARQL for non-XSD DT Literals we can only do =,!=
                if op not in ("=", "!="):
                    raise SPARQLError("Can only do =,!= comparisons of non-XSD Literals")

        try:
            r = ops[op](expr, other)
            if r == NotImplemented:
                raise SPARQLError("Error when comparing")
        except TypeError as te:
            raise SPARQLError(*te.args)
        return Literal(r)

## `` UnaryMinus

    UnaryMinus(expr: Expr, ctx: FrozenBindings) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    738
    739

    def UnaryMinus(expr: Expr, ctx: FrozenBindings) -> Literal:
        return Literal(-numeric(expr.expr))

## `` UnaryNot

    UnaryNot(expr: Expr, ctx: FrozenBindings) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    734
    735

    def UnaryNot(expr: Expr, ctx: FrozenBindings) -> Literal:
        return Literal(not EBV(expr.expr))

## `` UnaryPlus

    UnaryPlus(expr: Expr, ctx: FrozenBindings) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    742
    743

    def UnaryPlus(expr: Expr, ctx: FrozenBindings) -> Literal:
        return Literal(+numeric(expr.expr))

## `` and_

    and_(*args: Expr) -> Expr

Source code in `rdflib/plugins/sparql/operators.py`

    975
    976
    977
    978
    979
    980
    981
    982
    983
    984

    def and_(*args: Expr) -> Expr:
        if len(args) == 1:
            return args[0]

        return Expr(
            "ConditionalAndExpression",
            ConditionalAndExpression,
            expr=args[0],
            other=list(args[1:]),
        )

## `` calculateDuration

    calculateDuration(obj1: Union[date, datetime], obj2: Union[date, datetime]) -> Literal

returns the duration Literal between two datetime

Source code in `rdflib/plugins/sparql/operators.py`

    1127
    1128
    1129
    1130
    1131
    1132
    1133
    1134
    1135
    1136
    1137
    1138

    def calculateDuration(
        obj1: Union[py_datetime.date, py_datetime.datetime],
        obj2: Union[py_datetime.date, py_datetime.datetime],
    ) -> Literal:
        """
        returns the duration Literal between two datetime
        """
        date1 = obj1
        date2 = obj2
        # type error: No overload variant of "__sub__" of "datetime" matches argument type "date"
        difference = date1 - date2  # type: ignore[operator]
        return Literal(difference, datatype=XSD.duration)

## `` calculateFinalDateTime

    calculateFinalDateTime(obj1: Union[date, datetime], dt1: URIRef, obj2: Union[Duration, timedelta], dt2: URIRef, operation: str) -> Literal

Calculates the final dateTime/date/time resultant after addition/ subtraction of duration/dayTimeDuration/yearMonthDuration

Source code in `rdflib/plugins/sparql/operators.py`

    1141
    1142
    1143
    1144
    1145
    1146
    1147
    1148
    1149
    1150
    1151
    1152
    1153
    1154
    1155
    1156
    1157
    1158
    1159
    1160
    1161
    1162
    1163
    1164

    def calculateFinalDateTime(
        obj1: Union[py_datetime.date, py_datetime.datetime],
        dt1: URIRef,
        obj2: Union[Duration, py_datetime.timedelta],
        dt2: URIRef,
        operation: str,
    ) -> Literal:
        """
        Calculates the final dateTime/date/time resultant after addition/
        subtraction of duration/dayTimeDuration/yearMonthDuration
        """

        # checking compatibility of datatypes (duration types and date/time/dateTime)
        if isCompatibleDateTimeDatatype(obj1, dt1, obj2, dt2):
            # proceed
            if operation == "-":
                ans = obj1 - obj2
                return Literal(ans, datatype=dt1)
            else:
                ans = obj1 + obj2
                return Literal(ans, datatype=dt1)

        else:
            raise SPARQLError("Incompatible Data types to DateTime Operations")

## `` custom_function

    custom_function(uri: URIRef, override: bool = False, raw: bool = False) -> Callable[[_CustomFunction], _CustomFunction]

Decorator version of :func:`register_custom_function`.

Source code in `rdflib/plugins/sparql/operators.py`

    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625

    def custom_function(
        uri: URIRef, override: bool = False, raw: bool = False
    ) -> Callable[[_CustomFunction], _CustomFunction]:
        """
        Decorator version of :func:`register_custom_function`.
        """

        def decorator(func: _CustomFunction) -> _CustomFunction:
            register_custom_function(uri, func, override=override, raw=raw)
            return func

        return decorator

## `` date

    date(e: Literal) -> date

Source code in `rdflib/plugins/sparql/operators.py`

    1024
    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032

    def date(e: Literal) -> py_datetime.date:
        if not isinstance(e, Literal):
            raise SPARQLError("Non-literal passed as date: %r" % e)
        if e.datatype not in (XSD.date, XSD.dateTime):
            raise SPARQLError("Literal with wrong datatype passed as date: %r" % e)
        result = e.toPython()
        if isinstance(result, py_datetime.datetime):
            return result.date()
        return result

## `` dateTimeObjects

    dateTimeObjects(expr: Literal) -> Any

return a dataTime/date/time/duration/dayTimeDuration/yearMonthDuration python objects from a literal

Source code in `rdflib/plugins/sparql/operators.py`

    1081
    1082
    1083
    1084
    1085

    def dateTimeObjects(expr: Literal) -> Any:
        """
        return a dataTime/date/time/duration/dayTimeDuration/yearMonthDuration python objects from a literal
        """
        return expr.toPython()

## `` datetime

    datetime(e: Literal) -> datetime

Source code in `rdflib/plugins/sparql/operators.py`

    1016
    1017
    1018
    1019
    1020
    1021

    def datetime(e: Literal) -> py_datetime.datetime:
        if not isinstance(e, Literal):
            raise SPARQLError("Non-literal passed as datetime: %r" % e)
        if not e.datatype == XSD.dateTime:
            raise SPARQLError("Literal with wrong datatype passed as datetime: %r" % e)
        return e.toPython()

## `` default_cast

    default_cast(e: Expr, ctx: FrozenBindings) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731

    @custom_function(XSD.string, raw=True)
    @custom_function(XSD.dateTime, raw=True)
    @custom_function(XSD.float, raw=True)
    @custom_function(XSD.double, raw=True)
    @custom_function(XSD.decimal, raw=True)
    @custom_function(XSD.integer, raw=True)
    @custom_function(XSD.boolean, raw=True)
    def default_cast(e: Expr, ctx: FrozenBindings) -> Literal:  # type: ignore[return]
        if not e.expr:
            raise SPARQLError("Nothing given to cast.")
        if len(e.expr) > 1:
            raise SPARQLError("Cannot cast more than one thing!")

        x = e.expr[0]

        if e.iri == XSD.string:
            if isinstance(x, (URIRef, Literal)):
                return Literal(x, datatype=XSD.string)
            else:
                raise SPARQLError("Cannot cast term %r of type %r" % (x, type(x)))

        if not isinstance(x, Literal):
            raise SPARQLError("Can only cast Literals to non-string data-types")

        if x.datatype and not x.datatype in XSD_DTs:  # noqa: E713
            raise SPARQLError("Cannot cast literal with unknown datatype: %r" % x.datatype)

        if e.iri == XSD.dateTime:
            if x.datatype and x.datatype not in (XSD.dateTime, XSD.string):
                raise SPARQLError("Cannot cast %r to XSD:dateTime" % x.datatype)
            try:
                return Literal(parse_datetime(x), datatype=e.iri)
            except:  # noqa: E722
                raise SPARQLError("Cannot interpret '%r' as datetime" % x)

        if x.datatype == XSD.dateTime:
            raise SPARQLError("Cannot cast XSD.dateTime to %r" % e.iri)

        if e.iri in (XSD.float, XSD.double):
            try:
                return Literal(float(x), datatype=e.iri)
            except:  # noqa: E722
                raise SPARQLError("Cannot interpret '%r' as float" % x)

        elif e.iri == XSD.decimal:
            if "e" in x or "E" in x:  # SPARQL/XSD does not allow exponents in decimals
                raise SPARQLError("Cannot interpret '%r' as decimal" % x)
            try:
                return Literal(Decimal(x), datatype=e.iri)
            except:  # noqa: E722
                raise SPARQLError("Cannot interpret '%r' as decimal" % x)

        elif e.iri == XSD.integer:
            try:
                return Literal(int(x), datatype=XSD.integer)
            except:  # noqa: E722
                raise SPARQLError("Cannot interpret '%r' as int" % x)

        elif e.iri == XSD.boolean:
            # # I would argue that any number is True...
            # try:
            #     return Literal(bool(int(x)), datatype=XSD.boolean)
            # except:
            if x.lower() in ("1", "true"):
                return Literal(True)
            if x.lower() in ("0", "false"):
                return Literal(False)
            raise SPARQLError("Cannot interpret '%r' as bool" % x)

## `` isCompatibleDateTimeDatatype

    isCompatibleDateTimeDatatype(obj1: Union[date, datetime], dt1: URIRef, obj2: Union[Duration, timedelta], dt2: URIRef) -> bool

Returns a boolean indicating if first object is compatible with operation(+/-) over second object.

Source code in `rdflib/plugins/sparql/operators.py`

    1089
    1090
    1091
    1092
    1093
    1094
    1095
    1096
    1097
    1098
    1099
    1100
    1101
    1102
    1103
    1104
    1105
    1106
    1107
    1108
    1109
    1110
    1111
    1112
    1113
    1114
    1115
    1116
    1117
    1118
    1119
    1120
    1121
    1122
    1123
    1124

    def isCompatibleDateTimeDatatype(  # type: ignore[return]
        obj1: Union[py_datetime.date, py_datetime.datetime],
        dt1: URIRef,
        obj2: Union[Duration, py_datetime.timedelta],
        dt2: URIRef,
    ) -> bool:
        """
        Returns a boolean indicating if first object is compatible
        with operation(+/-) over second object.
        """
        if dt1 == XSD.date:
            if dt2 == XSD.yearMonthDuration:
                return True
            elif dt2 == XSD.dayTimeDuration or dt2 == XSD.Duration:
                # checking if the dayTimeDuration has no Time Component
                # else it won't be compatible with Date Literal
                if "T" in str(obj2):
                    return False
                else:
                    return True

        if dt1 == XSD.time:
            if dt2 == XSD.yearMonthDuration:
                return False
            elif dt2 == XSD.dayTimeDuration or dt2 == XSD.Duration:
                # checking if the dayTimeDuration has no Date Component
                # (by checking if the format is "PT...." )
                # else it won't be compatible with Time Literal
                if "T" == str(obj2)[1]:
                    return True
                else:
                    return False

        if dt1 == XSD.dateTime:
            # compatible with all
            return True

## `` literal

    literal(s: Literal) -> Literal

Source code in `rdflib/plugins/sparql/operators.py`

    1010
    1011
    1012
    1013

    def literal(s: Literal) -> Literal:
        if not isinstance(s, Literal):
            raise SPARQLError("Non-literal passed as string: %r" % s)
        return s

## `` not_

    not_(arg) -> Expr

Source code in `rdflib/plugins/sparql/operators.py`

    971
    972

    def not_(arg) -> Expr:
        return Expr("UnaryNot", UnaryNot, expr=arg)

## `` numeric

    numeric(expr: Literal) -> Any

return a number from a literal http://www.w3.org/TR/xpath20/#promotion

or TypeError

Source code in `rdflib/plugins/sparql/operators.py`

    1047
    1048
    1049
    1050
    1051
    1052
    1053
    1054
    1055
    1056
    1057
    1058
    1059
    1060
    1061
    1062
    1063
    1064
    1065
    1066
    1067
    1068
    1069
    1070
    1071
    1072
    1073
    1074
    1075
    1076
    1077
    1078

    def numeric(expr: Literal) -> Any:
        """
        return a number from a literal
        http://www.w3.org/TR/xpath20/#promotion

        or TypeError
        """

        if not isinstance(expr, Literal):
            raise SPARQLTypeError("%r is not a literal!" % expr)

        if expr.datatype not in (
            XSD.float,
            XSD.double,
            XSD.decimal,
            XSD.integer,
            XSD.nonPositiveInteger,
            XSD.negativeInteger,
            XSD.nonNegativeInteger,
            XSD.positiveInteger,
            XSD.unsignedLong,
            XSD.unsignedInt,
            XSD.unsignedShort,
            XSD.unsignedByte,
            XSD.long,
            XSD.int,
            XSD.short,
            XSD.byte,
        ):
            raise SPARQLTypeError("%r does not have a numeric datatype!" % expr)

        return expr.toPython()

## `` register_custom_function

    register_custom_function(uri: URIRef, func: _CustomFunction, override: bool = False, raw: bool = False) -> None

Register a custom SPARQL function.

By default, the function will be passed the RDF terms in the argument list. If raw is True, the function will be passed an Expression and a Context.

The function must return an RDF term, or raise a SparqlError.

Source code in `rdflib/plugins/sparql/operators.py`

    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611

    def register_custom_function(
        uri: URIRef, func: _CustomFunction, override: bool = False, raw: bool = False
    ) -> None:
        """Register a custom SPARQL function.

        By default, the function will be passed the RDF terms in the argument list.
        If raw is True, the function will be passed an Expression and a Context.

        The function must return an RDF term, or raise a SparqlError.
        """
        if not override and uri in _CUSTOM_FUNCTIONS:
            raise ValueError("A function is already registered as %s" % uri.n3())
        _CUSTOM_FUNCTIONS[uri] = (func, raw)

## `` simplify

    simplify(expr: Any) -> Any

Source code in `rdflib/plugins/sparql/operators.py`

     990
     991
     992
     993
     994
     995
     996
     997
     998
     999
    1000
    1001
    1002
    1003
    1004
    1005
    1006
    1007

    def simplify(expr: Any) -> Any:
        if isinstance(expr, ParseResults) and len(expr) == 1:
            return simplify(expr[0])

        if isinstance(expr, (list, ParseResults)):
            return list(map(simplify, expr))
        if not isinstance(expr, CompValue):
            return expr
        if expr.name.endswith("Expression"):
            if expr.other is None:
                return simplify(expr.expr)

        for k in expr.keys():
            expr[k] = simplify(expr[k])
            # expr['expr']=simplify(expr.expr)
            #    expr['other']=simplify(expr.other)

        return expr

## `` string

    string(s: Literal) -> Literal

Make sure the passed thing is a string literal i.e. plain literal, xsd:string literal or lang-tagged literal

Source code in `rdflib/plugins/sparql/operators.py`

    1035
    1036
    1037
    1038
    1039
    1040
    1041
    1042
    1043
    1044

    def string(s: Literal) -> Literal:
        """
        Make sure the passed thing is a string literal
        i.e. plain literal, xsd:string literal or lang-tagged literal
        """
        if not isinstance(s, Literal):
            raise SPARQLError("Non-literal passes as string: %r" % s)
        if s.datatype and s.datatype != XSD.string:
            raise SPARQLError("Non-string datatype-literal passes as string: %r" % s)
        return s

## `` unregister_custom_function

    unregister_custom_function(uri: URIRef, func: Optional[Callable[..., Any]] = None) -> None

The ‘func’ argument is included for compatibility with existing code. A previous implementation checked that the function associated with the given uri was actually ‘func’, but this is not necessary as the uri should uniquely identify the function.

Source code in `rdflib/plugins/sparql/operators.py`

    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640

    def unregister_custom_function(
        uri: URIRef, func: Optional[Callable[..., Any]] = None
    ) -> None:
        """
        The 'func' argument is included for compatibility with existing code.
        A previous implementation checked that the function associated with
        the given uri was actually 'func', but this is not necessary as the
        uri should uniquely identify the function.
        """
        if _CUSTOM_FUNCTIONS.get(uri):
            del _CUSTOM_FUNCTIONS[uri]
        else:
            warnings.warn("This function is not registered as %s" % uri.n3())