Skip to content

# `` evaluate

These method recursively evaluate the SPARQL Algebra

evalQuery is the entry-point, it will setup context and return the SPARQLResult object

evalPart is called on each level and will delegate to the right method

A `rdflib.plugins.sparql.sparql.QueryContext` is passed along, keeping information needed for evaluation

A list of dicts (solution mappings) is returned, apart from GroupBy which may also return a dict of list of dicts

Functions:

- `evalAggregateJoin` –
- `evalAskQuery` –
- `evalBGP` –

  A basic graph pattern

- `evalConstructQuery` –
- `evalDescribeQuery` –
- `evalDistinct` –
- `evalExtend` –
- `evalFilter` –
- `evalGraph` –
- `evalGroup` –

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

- `evalJoin` –
- `evalLazyJoin` –

  A lazy join will push the variables bound

- `evalLeftJoin` –
- `evalMinus` –
- `evalMultiset` –
- `evalOrderBy` –
- `evalPart` –
- `evalProject` –
- `evalQuery` –

  Evaluate a SPARQL query against a graph.

- `evalReduced` –

  apply REDUCED to result

- `evalSelectQuery` –
- `evalServiceQuery` –
- `evalSlice` –
- `evalUnion` –
- `evalValues` –

## `` evalAggregateJoin

    evalAggregateJoin(ctx: QueryContext, agg: CompValue) -> Generator[FrozenBindings, None, None]

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

    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492

    def evalAggregateJoin(
        ctx: QueryContext, agg: CompValue
    ) -> Generator[FrozenBindings, None, None]:
        # import pdb ; pdb.set_trace()
        p = evalPart(ctx, agg.p)
        # p is always a Group, we always get a dict back

        group_expr = agg.p.expr
        res: Dict[Any, Any] = collections.defaultdict(
            lambda: Aggregator(aggregations=agg.A)
        )

        if group_expr is None:
            # no grouping, just COUNT in SELECT clause
            # get 1 aggregator for counting
            aggregator = res[True]
            for row in p:
                aggregator.update(row)
        else:
            for row in p:
                # determine right group aggregator for row
                k = tuple(_eval(e, row, False) for e in group_expr)
                res[k].update(row)

        # all rows are done; yield aggregated values
        for aggregator in res.values():
            yield FrozenBindings(ctx, aggregator.get_bindings())

        # there were no matches
        if len(res) == 0:
            yield FrozenBindings(ctx)

## `` evalAskQuery

    evalAskQuery(ctx: QueryContext, query: CompValue) -> Mapping[str, Union[str, bool]]

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

    586
    587
    588
    589
    590
    591
    592
    593
    594

    def evalAskQuery(ctx: QueryContext, query: CompValue) -> Mapping[str, Union[str, bool]]:
        res: Dict[str, Union[bool, str]] = {}
        res["type_"] = "ASK"
        res["askAnswer"] = False
        for x in evalPart(ctx, query.p):
            res["askAnswer"] = True
            break

        return res

## `` evalBGP

    evalBGP(ctx: QueryContext, bgp: List[_Triple]) -> Generator[FrozenBindings, None, None]

A basic graph pattern

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

     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121

    def evalBGP(
        ctx: QueryContext, bgp: List[_Triple]
    ) -> Generator[FrozenBindings, None, None]:
        """
        A basic graph pattern
        """

        if not bgp:
            yield ctx.solution()
            return

        s, p, o = bgp[0]

        _s = ctx[s]
        _p = ctx[p]
        _o = ctx[o]

        # type error: Item "None" of "Optional[Graph]" has no attribute "triples"
        # type Argument 1 to "triples" of "Graph" has incompatible type "Tuple[Union[str, Path, None], Union[str, Path, None], Union[str, Path, None]]"; expected "Tuple[Optional[Node], Optional[Node], Optional[Node]]"
        for ss, sp, so in ctx.graph.triples((_s, _p, _o)):  # type: ignore[union-attr, arg-type]
            if None in (_s, _p, _o):
                c = ctx.push()
            else:
                c = ctx

            if _s is None:
                # type error: Incompatible types in assignment (expression has type "Union[Node, Any]", target has type "Identifier")
                c[s] = ss  # type: ignore[assignment]

            try:
                if _p is None:
                    # type error: Incompatible types in assignment (expression has type "Union[Node, Any]", target has type "Identifier")
                    c[p] = sp  # type: ignore[assignment]
            except AlreadyBound:
                continue

            try:
                if _o is None:
                    # type error: Incompatible types in assignment (expression has type "Union[Node, Any]", target has type "Identifier")
                    c[o] = so  # type: ignore[assignment]
            except AlreadyBound:
                continue

            for x in evalBGP(c, bgp[1:]):
                yield x

## `` evalConstructQuery

    evalConstructQuery(ctx: QueryContext, query: CompValue) -> Mapping[str, Union[str, Graph]]

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

    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615

    def evalConstructQuery(
        ctx: QueryContext, query: CompValue
    ) -> Mapping[str, Union[str, Graph]]:
        template = query.template

        if not template:
            # a construct-where query
            template = query.p.p.triples  # query->project->bgp ...

        graph = Graph()

        for c in evalPart(ctx, query.p):
            graph += _fillTemplate(template, c)

        res: Dict[str, Union[str, Graph]] = {}
        res["type_"] = "CONSTRUCT"
        res["graph"] = graph

        return res

## `` evalDescribeQuery

    evalDescribeQuery(ctx: QueryContext, query) -> Dict[str, Union[str, Graph]]

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

    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650

    def evalDescribeQuery(ctx: QueryContext, query) -> Dict[str, Union[str, Graph]]:
        # Create a result graph and bind namespaces from the graph being queried
        graph = Graph()
        # type error: Item "None" of "Optional[Graph]" has no attribute "namespaces"
        for pfx, ns in ctx.graph.namespaces():  # type: ignore[union-attr]
            graph.bind(pfx, ns)

        to_describe = set()

        # Explicit IRIs may be provided to a DESCRIBE query.
        # If there is a WHERE clause, explicit IRIs may be provided in
        # addition to projected variables. Find those explicit IRIs and
        # prepare to describe them.
        for iri in query.PV:
            if isinstance(iri, URIRef):
                to_describe.add(iri)

        # If there is a WHERE clause, evaluate it then find the unique set of
        # resources to describe across all bindings and projected variables
        if query.p is not None:
            bindings = evalPart(ctx, query.p)
            to_describe.update(*(set(binding.values()) for binding in bindings))

        # Get a CBD for all resources identified to describe
        for resource in to_describe:
            # type error: Item "None" of "Optional[Graph]" has no attribute "cbd"
            ctx.graph.cbd(resource, target_graph=graph)  # type: ignore[union-attr]

        res: Dict[str, Union[str, Graph]] = {}
        res["type_"] = "DESCRIBE"
        res["graph"] = graph

        return res

## `` evalDistinct

    evalDistinct(ctx: QueryContext, part: CompValue) -> Generator[FrozenBindings, None, None]

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

    559
    560
    561
    562
    563
    564
    565
    566
    567
    568

    def evalDistinct(
        ctx: QueryContext, part: CompValue
    ) -> Generator[FrozenBindings, None, None]:
        res = evalPart(ctx, part.p)

        done = set()
        for x in res:
            if x not in done:
                yield x
                done.add(x)

## `` evalExtend

    evalExtend(ctx: QueryContext, extend: CompValue) -> Generator[FrozenBindings, None, None]

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

    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138

    def evalExtend(
        ctx: QueryContext, extend: CompValue
    ) -> Generator[FrozenBindings, None, None]:
        # TODO: Deal with dict returned from evalPart from GROUP BY

        for c in evalPart(ctx, extend.p):
            try:
                e = _eval(extend.expr, c.forget(ctx, _except=extend._vars))
                if isinstance(e, SPARQLError):
                    raise e

                yield c.merge({extend.var: e})

            except SPARQLError:
                yield c

## `` evalFilter

    evalFilter(ctx: QueryContext, part: CompValue) -> Generator[FrozenBindings, None, None]

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

    208
    209
    210
    211
    212
    213
    214
    215
    216
    217

    def evalFilter(
        ctx: QueryContext, part: CompValue
    ) -> Generator[FrozenBindings, None, None]:
        # TODO: Deal with dict returned from evalPart!
        for c in evalPart(ctx, part.p):
            if _ebv(
                part.expr,
                c.forget(ctx, _except=part._vars) if not part.no_isolated_scope else c,
            ):
                yield c

## `` evalGraph

    evalGraph(ctx: QueryContext, part: CompValue) -> Generator[FrozenBindings, None, None]

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

    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252

    def evalGraph(
        ctx: QueryContext, part: CompValue
    ) -> Generator[FrozenBindings, None, None]:
        if ctx.dataset is None:
            raise Exception(
                "Non-conjunctive-graph doesn't know about "
                + "graphs. Try a query without GRAPH."
            )

        ctx = ctx.clone()
        graph: Union[str, Path, None, Graph] = ctx[part.term]
        prev_graph = ctx.graph
        if graph is None:
            for graph in ctx.dataset.contexts():
                # in SPARQL the default graph is NOT a named graph
                if graph == ctx.dataset.default_context:
                    continue

                c = ctx.pushGraph(graph)
                c = c.push()
                graphSolution = [{part.term: graph.identifier}]
                for x in _join(evalPart(c, part.p), graphSolution):
                    x.ctx.graph = prev_graph
                    yield x

        else:
            if TYPE_CHECKING:
                assert not isinstance(graph, Graph)
            # type error: Argument 1 to "get_context" of "ConjunctiveGraph" has incompatible type "Union[str, Path]"; expected "Union[Node, str, None]"
            c = ctx.pushGraph(ctx.dataset.get_context(graph))  # type: ignore[arg-type]
            for x in evalPart(c, part.p):
                x.ctx.graph = prev_graph
                yield x

## `` evalGroup

    evalGroup(ctx: QueryContext, group: CompValue)

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

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

    454
    455
    456
    457
    458
    459

    def evalGroup(ctx: QueryContext, group: CompValue):
        """
        http://www.w3.org/TR/sparql11-query/#defn_algGroup
        """
        # grouping should be implemented by evalAggregateJoin
        return evalPart(ctx, group.p)

## `` evalJoin

    evalJoin(ctx: QueryContext, join: CompValue) -> Generator[FrozenDict, None, None]

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

    156
    157
    158
    159
    160
    161
    162
    163
    164
    165

    def evalJoin(ctx: QueryContext, join: CompValue) -> Generator[FrozenDict, None, None]:
        # TODO: Deal with dict returned from evalPart from GROUP BY
        # only ever for join.p1

        if join.lazy:
            return evalLazyJoin(ctx, join)
        else:
            a = evalPart(ctx, join.p1)
            b = set(evalPart(ctx, join.p2))
            return _join(a, b)

## `` evalLazyJoin

    evalLazyJoin(ctx: QueryContext, join: CompValue) -> Generator[FrozenBindings, None, None]

A lazy join will push the variables bound in the first part to the second part, essentially doing the join implicitly hopefully evaluating much fewer triples

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

    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153

    def evalLazyJoin(
        ctx: QueryContext, join: CompValue
    ) -> Generator[FrozenBindings, None, None]:
        """
        A lazy join will push the variables bound
        in the first part to the second part,
        essentially doing the join implicitly
        hopefully evaluating much fewer triples
        """
        for a in evalPart(ctx, join.p1):
            c = ctx.thaw(a)
            for b in evalPart(c, join.p2):
                yield b.merge(a)  # merge, as some bindings may have been forgotten

## `` evalLeftJoin

    evalLeftJoin(ctx: QueryContext, join: CompValue) -> Generator[FrozenBindings, None, None]

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

    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205

    def evalLeftJoin(
        ctx: QueryContext, join: CompValue
    ) -> Generator[FrozenBindings, None, None]:
        # import pdb; pdb.set_trace()
        for a in evalPart(ctx, join.p1):
            ok = False
            c = ctx.thaw(a)
            for b in evalPart(c, join.p2):
                if _ebv(join.expr, b.forget(ctx)):
                    ok = True
                    yield b.merge(a)
            if not ok:
                # we've cheated, the ctx above may contain
                # vars bound outside our scope
                # before we yield a solution without the OPTIONAL part
                # check that we would have had no OPTIONAL matches
                # even without prior bindings...
                p1_vars = join.p1._vars
                if p1_vars is None or not any(
                    _ebv(join.expr, b)
                    for b in evalPart(ctx.thaw(a.remember(p1_vars)), join.p2)
                ):
                    yield a

## `` evalMinus

    evalMinus(ctx: QueryContext, minus: CompValue) -> Generator[FrozenDict, None, None]

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

    177
    178
    179
    180

    def evalMinus(ctx: QueryContext, minus: CompValue) -> Generator[FrozenDict, None, None]:
        a = evalPart(ctx, minus.p1)
        b = set(evalPart(ctx, minus.p2))
        return _minus(a, b)

## `` evalMultiset

    evalMultiset(ctx: QueryContext, part: CompValue)

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

    270
    271
    272
    273
    274

    def evalMultiset(ctx: QueryContext, part: CompValue):
        if part.p.name == "values":
            return evalValues(ctx, part)

        return evalPart(ctx, part.p)

## `` evalOrderBy

    evalOrderBy(ctx: QueryContext, part: CompValue) -> Generator[FrozenBindings, None, None]

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

    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506

    def evalOrderBy(
        ctx: QueryContext, part: CompValue
    ) -> Generator[FrozenBindings, None, None]:
        res = evalPart(ctx, part.p)

        for e in reversed(part.expr):
            reverse = bool(e.order and e.order == "DESC")
            res = sorted(
                res, key=lambda x: _val(value(x, e.expr, variables=True)), reverse=reverse
            )

        return res

## `` evalPart

    evalPart(ctx: QueryContext, part: CompValue) -> Any

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

    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340

    def evalPart(ctx: QueryContext, part: CompValue) -> Any:
        # try custom evaluation functions
        for name, c in CUSTOM_EVALS.items():
            try:
                return c(ctx, part)
            except NotImplementedError:
                pass  # the given custome-function did not handle this part

        if part.name == "BGP":
            # Reorder triples patterns by number of bound nodes in the current ctx
            # Do patterns with more bound nodes first
            triples = sorted(
                part.triples, key=lambda t: len([n for n in t if ctx[n] is None])
            )

            return evalBGP(ctx, triples)
        elif part.name == "Filter":
            return evalFilter(ctx, part)
        elif part.name == "Join":
            return evalJoin(ctx, part)
        elif part.name == "LeftJoin":
            return evalLeftJoin(ctx, part)
        elif part.name == "Graph":
            return evalGraph(ctx, part)
        elif part.name == "Union":
            return evalUnion(ctx, part)
        elif part.name == "ToMultiSet":
            return evalMultiset(ctx, part)
        elif part.name == "Extend":
            return evalExtend(ctx, part)
        elif part.name == "Minus":
            return evalMinus(ctx, part)

        elif part.name == "Project":
            return evalProject(ctx, part)
        elif part.name == "Slice":
            return evalSlice(ctx, part)
        elif part.name == "Distinct":
            return evalDistinct(ctx, part)
        elif part.name == "Reduced":
            return evalReduced(ctx, part)

        elif part.name == "OrderBy":
            return evalOrderBy(ctx, part)
        elif part.name == "Group":
            return evalGroup(ctx, part)
        elif part.name == "AggregateJoin":
            return evalAggregateJoin(ctx, part)

        elif part.name == "SelectQuery":
            return evalSelectQuery(ctx, part)
        elif part.name == "AskQuery":
            return evalAskQuery(ctx, part)
        elif part.name == "ConstructQuery":
            return evalConstructQuery(ctx, part)

        elif part.name == "ServiceGraphPattern":
            return evalServiceQuery(ctx, part)

        elif part.name == "DescribeQuery":
            return evalDescribeQuery(ctx, part)

        else:
            raise Exception("I dont know: %s" % part.name)

## `` evalProject

    evalProject(ctx: QueryContext, project: CompValue)

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

    571
    572
    573

    def evalProject(ctx: QueryContext, project: CompValue):
        res = evalPart(ctx, project.p)
        return (row.project(project.PV) for row in res)

## `` evalQuery

    evalQuery(graph: Graph, query: Query, initBindings: Optional[Mapping[str, Identifier]] = None, base: Optional[str] = None) -> Mapping[Any, Any]

Evaluate a SPARQL query against a graph.

Caution

This method can access indirectly requested network endpoints, for example, query processing will attempt to access network endpoints specified in `SERVICE` directives.

When processing untrusted or potentially malicious queries, measures should be taken to restrict network and file access.

For information on available security measures, see the RDFLib Security Considerations documentation.

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

    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684

    def evalQuery(
        graph: Graph,
        query: Query,
        initBindings: Optional[Mapping[str, Identifier]] = None,
        base: Optional[str] = None,
    ) -> Mapping[Any, Any]:
        """Evaluate a SPARQL query against a graph.

        !!! warning "Caution"

            This method can access indirectly requested network endpoints, for
            example, query processing will attempt to access network endpoints
            specified in `SERVICE` directives.

            When processing untrusted or potentially malicious queries, measures
            should be taken to restrict network and file access.

            For information on available security measures, see the RDFLib
            [Security Considerations](../security_considerations.md)
            documentation.
        """
        main = query.algebra

        initBindings = dict((Variable(k), v) for k, v in (initBindings or {}).items())

        ctx = QueryContext(
            graph, initBindings=initBindings, datasetClause=main.datasetClause
        )

        ctx.prologue = query.prologue

        return evalPart(ctx, main)

## `` evalReduced

    evalReduced(ctx: QueryContext, part: CompValue) -> Generator[FrozenBindings, None, None]

apply REDUCED to result

REDUCED is not as strict as DISTINCT, but if the incoming rows were sorted it should produce the same result with limited extra memory and time per incoming row.

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

    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556

    def evalReduced(
        ctx: QueryContext, part: CompValue
    ) -> Generator[FrozenBindings, None, None]:
        """apply REDUCED to result

        REDUCED is not as strict as DISTINCT, but if the incoming rows were sorted
        it should produce the same result with limited extra memory and time per
        incoming row.
        """

        # This implementation uses a most recently used strategy and a limited
        # buffer size. It relates to a LRU caching algorithm:
        # https://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used_.28LRU.29
        MAX = 1
        # TODO: add configuration or determine "best" size for most use cases
        # 0: No reduction
        # 1: compare only with the last row, almost no reduction with
        #    unordered incoming rows
        # N: The greater the buffer size the greater the reduction but more
        #    memory and time are needed

        # mixed data structure: set for lookup, deque for append/pop/remove
        mru_set = set()
        mru_queue: Deque[Any] = collections.deque()

        for row in evalPart(ctx, part.p):
            if row in mru_set:
                # forget last position of row
                mru_queue.remove(row)
            else:
                # row seems to be new
                yield row
                mru_set.add(row)
                if len(mru_set) > MAX:
                    # drop the least recently used row from buffer
                    mru_set.remove(mru_queue.pop())
            # put row to the front
            mru_queue.appendleft(row)

## `` evalSelectQuery

    evalSelectQuery(ctx: QueryContext, query: CompValue) -> Mapping[str, Union[str, List[Variable], Iterable[FrozenDict]]]

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

    576
    577
    578
    579
    580
    581
    582
    583

    def evalSelectQuery(
        ctx: QueryContext, query: CompValue
    ) -> Mapping[str, Union[str, List[Variable], Iterable[FrozenDict]]]:
        res: Dict[str, Union[str, List[Variable], Iterable[FrozenDict]]] = {}
        res["type_"] = "SELECT"
        res["bindings"] = evalPart(ctx, query.p)
        res["vars_"] = query.PV
        return res

## `` evalServiceQuery

    evalServiceQuery(ctx: QueryContext, part: CompValue)

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

    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390

    def evalServiceQuery(ctx: QueryContext, part: CompValue):
        res = {}
        match = re.match(
            "^service <(.*)>[ \n]*{(.*)}[ \n]*$",
            # type error: Argument 2 to "get" of "CompValue" has incompatible type "str"; expected "bool"  [arg-type]
            part.get("service_string", ""),  # type: ignore[arg-type]
            re.DOTALL | re.I,
        )

        if match:
            service_url = match.group(1)
            service_query = _buildQueryStringForServiceCall(ctx, match.group(2))

            query_settings = {"query": service_query, "output": "json"}
            headers = {
                "accept": "application/sparql-results+json",
                "user-agent": "rdflibForAnUser",
            }
            # GET is easier to cache so prefer that if the query is not to long
            if len(service_query) < 600:
                response = urlopen(
                    Request(service_url + "?" + urlencode(query_settings), headers=headers)
                )
            else:
                response = urlopen(
                    Request(
                        service_url,
                        data=urlencode(query_settings).encode(),
                        headers=headers,
                    )
                )
            if response.status == 200:
                if _HAS_ORJSON:
                    json_dict = orjson.loads(response.read())
                else:
                    json_dict = json.loads(response.read())
                variables = res["vars_"] = json_dict["head"]["vars"]
                # or just return the bindings?
                res = json_dict["results"]["bindings"]
                if len(res) > 0:
                    for r in res:
                        # type error: Argument 2 to "_yieldBindingsFromServiceCallResult" has incompatible type "str"; expected "Dict[str, Dict[str, str]]"
                        for bound in _yieldBindingsFromServiceCallResult(ctx, r, variables):  # type: ignore[arg-type]
                            yield bound
            else:
                raise Exception(
                    "Service: %s responded with code: %s", service_url, response.status
                )

## `` evalSlice

    evalSlice(ctx: QueryContext, slice: CompValue)

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

    509
    510
    511
    512
    513
    514
    515
    516

    def evalSlice(ctx: QueryContext, slice: CompValue):
        res = evalPart(ctx, slice.p)

        return itertools.islice(
            res,
            slice.start,
            slice.start + slice.length if slice.length is not None else None,
        )

## `` evalUnion

    evalUnion(ctx: QueryContext, union: CompValue) -> List[Any]

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

    168
    169
    170
    171
    172
    173
    174

    def evalUnion(ctx: QueryContext, union: CompValue) -> List[Any]:
        branch1_branch2 = []
        for x in evalPart(ctx, union.p1):
            branch1_branch2.append(x)
        for x in evalPart(ctx, union.p2):
            branch1_branch2.append(x)
        return branch1_branch2

## `` evalValues

    evalValues(ctx: QueryContext, part: CompValue) -> Generator[FrozenBindings, None, None]

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

    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267

    def evalValues(
        ctx: QueryContext, part: CompValue
    ) -> Generator[FrozenBindings, None, None]:
        for r in part.p.res:
            c = ctx.push()
            try:
                for k, v in r.items():
                    if v != "UNDEF":
                        c[k] = v
            except AlreadyBound:
                continue

            yield c.solution()