Skip to content

# `` algebra

Converting the ‘parse-tree’ output of pyparsing to a SPARQL Algebra expression

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

Classes:

- `ExpressionNotCoveredException` –
- `StopTraversal` –

Functions:

- `BGP` –
- `Extend` –
- `Filter` –
- `Graph` –
- `Group` –
- `Join` –
- `LeftJoin` –
- `Minus` –
- `OrderBy` –
- `Project` –
- `ToMultiSet` –
- `Union` –
- `Values` –
- `analyse` –

  Some things can be lazily joined.

- `collectAndRemoveFilters` –

  FILTER expressions apply to the whole group graph pattern in which

- `pprintAlgebra` –
- `reorderTriples` –

  Reorder triple patterns so that we execute the

- `simplify` –

  Remove joins to empty BGPs

- `translate` –

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

- `translateAggregates` –
- `translateAlgebra` –

  Translates a SPARQL 1.1 algebra tree into the corresponding query string.

- `translateExists` –

  Translate the graph pattern used by EXISTS and NOT EXISTS

- `translateGraphGraphPattern` –
- `translateGroupGraphPattern` –

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

- `translateGroupOrUnionGraphPattern` –
- `translateInlineData` –
- `translatePName` –

  Expand prefixed/relative URIs

- `translatePath` –

  Translate PropertyPath expressions

- `translatePrologue` –
- `translateQuads` –
- `translateQuery` –

  Translate a query-parsetree to a SPARQL Algebra Expression

- `translateUpdate` –

  Returns a list of SPARQL Update Algebra expressions

- `translateUpdate1` –
- `translateValues` –
- `traverse` –

  Traverse tree, visit each node with visit function

- `triples` –

## `` ExpressionNotCoveredException

Bases: `Exception`

## `` StopTraversal

    StopTraversal(rv: bool)

Bases: `Exception`

Attributes:

- `rv` –

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

    396
    397

    def __init__(self, rv: bool):
        self.rv = rv

### `` rv `instance-attribute`

    rv = rv

## `` BGP

    BGP(triples: Optional[List[Tuple[Identifier, Identifier, Identifier]]] = None) -> CompValue

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

    72
    73
    74
    75

    def BGP(
        triples: Optional[List[Tuple[Identifier, Identifier, Identifier]]] = None
    ) -> CompValue:
        return CompValue("BGP", triples=triples or [])

## `` Extend

    Extend(p: CompValue, expr: Union[Identifier, Expr], var: Variable) -> CompValue

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

    86
    87
    88
    89

    def Extend(
        p: CompValue, expr: typing.Union[Identifier, Expr], var: Variable
    ) -> CompValue:
        return CompValue("Extend", p=p, expr=expr, var=var)

## `` Filter

    Filter(expr: Expr, p: CompValue) -> CompValue

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

    82
    83

    def Filter(expr: Expr, p: CompValue) -> CompValue:
        return CompValue("Filter", expr=expr, p=p)

## `` Graph

    Graph(term: Identifier, graph: CompValue) -> CompValue

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

    68
    69

    def Graph(term: Identifier, graph: CompValue) -> CompValue:
        return CompValue("Graph", term=term, p=graph)

## `` Group

    Group(p: CompValue, expr: Optional[List[Variable]] = None) -> CompValue

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

    100
    101

    def Group(p: CompValue, expr: Optional[List[Variable]] = None) -> CompValue:
        return CompValue("Group", p=p, expr=expr)

## `` Join

    Join(p1: CompValue, p2: Optional[CompValue]) -> CompValue

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

    60
    61

    def Join(p1: CompValue, p2: Optional[CompValue]) -> CompValue:
        return CompValue("Join", p1=p1, p2=p2)

## `` LeftJoin

    LeftJoin(p1: CompValue, p2: CompValue, expr) -> CompValue

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

    78
    79

    def LeftJoin(p1: CompValue, p2: CompValue, expr) -> CompValue:
        return CompValue("LeftJoin", p1=p1, p2=p2, expr=expr)

## `` Minus

    Minus(p1: CompValue, p2: CompValue) -> CompValue

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

    64
    65

    def Minus(p1: CompValue, p2: CompValue) -> CompValue:
        return CompValue("Minus", p1=p1, p2=p2)

## `` OrderBy

    OrderBy(p: CompValue, expr: List[CompValue]) -> CompValue

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

    48
    49

    def OrderBy(p: CompValue, expr: List[CompValue]) -> CompValue:
        return CompValue("OrderBy", p=p, expr=expr)

## `` Project

    Project(p: CompValue, PV: List[Variable]) -> CompValue

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

    96
    97

    def Project(p: CompValue, PV: List[Variable]) -> CompValue:
        return CompValue("Project", p=p, PV=PV)

## `` ToMultiSet

    ToMultiSet(p: Union[List[Dict[Variable, str]], CompValue]) -> CompValue

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

    52
    53

    def ToMultiSet(p: typing.Union[List[Dict[Variable, str]], CompValue]) -> CompValue:
        return CompValue("ToMultiSet", p=p)

## `` Union

    Union(p1: CompValue, p2: CompValue) -> CompValue

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

    56
    57

    def Union(p1: CompValue, p2: CompValue) -> CompValue:
        return CompValue("Union", p1=p1, p2=p2)

## `` Values

    Values(res: List[Dict[Variable, str]]) -> CompValue

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

    92
    93

    def Values(res: List[Dict[Variable, str]]) -> CompValue:
        return CompValue("values", res=res)

## `` analyse

    analyse(n: Any, children: Any) -> bool

Some things can be lazily joined. This propegates whether they can up the tree and sets lazy flags for all joins

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

    792
    793
    794
    795
    796
    797
    798
    799
    800
    801
    802
    803
    804
    805
    806
    807
    808

    def analyse(n: Any, children: Any) -> bool:
        """
        Some things can be lazily joined.
        This propegates whether they can up the tree
        and sets lazy flags for all joins
        """

        if isinstance(n, CompValue):
            if n.name == "Join":
                n["lazy"] = all(children)
                return False
            elif n.name in ("Slice", "Distinct"):
                return False
            else:
                return all(children)
        else:
            return True

## `` collectAndRemoveFilters

    collectAndRemoveFilters(parts: List[CompValue]) -> Optional[Expr]

FILTER expressions apply to the whole group graph pattern in which they appear.

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

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

    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300

    def collectAndRemoveFilters(parts: List[CompValue]) -> Optional[Expr]:
        """FILTER expressions apply to the whole group graph pattern in which
        they appear.

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

        filters = []

        i = 0
        while i < len(parts):
            p = parts[i]
            if p.name == "Filter":
                filters.append(translateExists(p.expr))
                parts.pop(i)
            else:
                i += 1

        if filters:
            # type error: Argument 1 to "and_" has incompatible type "*List[Union[Expr, Literal, Variable]]"; expected "Expr"
            return and_(*filters)  # type: ignore[arg-type]

        return None

## `` pprintAlgebra

    pprintAlgebra(q) -> None

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

    1660
    1661
    1662
    1663
    1664
    1665
    1666
    1667
    1668
    1669
    1670
    1671
    1672
    1673
    1674
    1675
    1676
    1677
    1678
    1679
    1680
    1681
    1682
    1683
    1684
    1685
    1686
    1687
    1688

    def pprintAlgebra(q) -> None:
        def pp(p, ind="    "):
            # if isinstance(p, list):
            #     print "[ "
            #     for x in p: pp(x,ind)
            #     print "%s ]"%ind
            #     return
            if not isinstance(p, CompValue):
                print(p)
                return
            print("%s(" % (p.name,))
            for k in p:
                print(
                    "%s%s ="
                    % (
                        ind,
                        k,
                    ),
                    end=" ",
                )
                pp(p[k], ind + "    ")
            print("%s)" % ind)

        try:
            pp(q.algebra)
        except AttributeError:
            # it's update, just a list
            for x in q:
                pp(x)

## `` reorderTriples

    reorderTriples(l_: Iterable[Tuple[Identifier, Identifier, Identifier]]) -> List[Tuple[Identifier, Identifier, Identifier]]

Reorder triple patterns so that we execute the ones with most bindings first

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

    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168

    def reorderTriples(
        l_: Iterable[Tuple[Identifier, Identifier, Identifier]]
    ) -> List[Tuple[Identifier, Identifier, Identifier]]:
        """
        Reorder triple patterns so that we execute the
        ones with most bindings first
        """

        def _addvar(term: str, varsknown: Set[typing.Union[Variable, BNode]]):
            if isinstance(term, (Variable, BNode)):
                varsknown.add(term)

        # NOTE on type errors: most of these are because the same variable is used
        # for different types.

        # type error: List comprehension has incompatible type List[Tuple[None, Tuple[Identifier, Identifier, Identifier]]]; expected List[Tuple[Identifier, Identifier, Identifier]]
        l_ = [(None, x) for x in l_]  # type: ignore[misc]
        varsknown: Set[typing.Union[BNode, Variable]] = set()
        varscount: Dict[Identifier, int] = collections.defaultdict(int)
        for t in l_:
            for c in t[1]:
                if isinstance(c, (Variable, BNode)):
                    varscount[c] += 1
        i = 0

        # Done in steps, sort by number of bound terms
        # the top block of patterns with the most bound terms is kept
        # the rest is resorted based on the vars bound after the first
        # block is evaluated

        # we sort by decorate/undecorate, since we need the value of the sort keys

        while i < len(l_):
            # type error: Generator has incompatible item type "Tuple[Any, Identifier]"; expected "Tuple[Identifier, Identifier, Identifier]"
            # type error: Argument 1 to "_knownTerms" has incompatible type "Identifier"; expected "Tuple[Identifier, Identifier, Identifier]"
            l_[i:] = sorted((_knownTerms(x[1], varsknown, varscount), x[1]) for x in l_[i:])  # type: ignore[misc,arg-type]
            # type error: Incompatible types in assignment (expression has type "str", variable has type "Tuple[Identifier, Identifier, Identifier]")
            t = l_[i][0][0]  # type: ignore[assignment] # top block has this many terms bound
            j = 0
            while i + j < len(l_) and l_[i + j][0][0] == t:
                for c in l_[i + j][1]:
                    _addvar(c, varsknown)
                j += 1
            i += 1

        # type error: List comprehension has incompatible type List[Identifier]; expected List[Tuple[Identifier, Identifier, Identifier]]
        return [x[1] for x in l_]  # type: ignore[misc]

## `` simplify

    simplify(n: Any) -> Optional[CompValue]

Remove joins to empty BGPs

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

    779
    780
    781
    782
    783
    784
    785
    786
    787
    788
    789

    def simplify(n: Any) -> Optional[CompValue]:  # type: ignore[return]
        """Remove joins to empty BGPs"""
        if isinstance(n, CompValue):
            if n.name == "Join":
                if n.p1.name == "BGP" and len(n.p1.triples) == 0:
                    return n.p2
                if n.p2.name == "BGP" and len(n.p2.triples) == 0:
                    return n.p1
            elif n.name == "BGP":
                n["triples"] = reorderTriples(n.triples)
                return n

## `` translate

    translate(q: CompValue) -> Tuple[Optional[CompValue], List[Variable]]

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

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

    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
    651
    652
    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
    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
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760

    def translate(q: CompValue) -> Tuple[Optional[CompValue], List[Variable]]:
        """
        http://www.w3.org/TR/sparql11-query/#convertSolMod
        """

        _traverse(q, _simplifyFilters)

        q.where = traverse(q.where, visitPost=translatePath)

        # TODO: Var scope test
        VS: Set[Variable] = set()

        # All query types have a WHERE clause EXCEPT some DESCRIBE queries
        # where only explicit IRIs are provided.
        if q.name == "DescribeQuery":
            # For DESCRIBE queries, use the vars provided in q.var.
            # If there is no WHERE clause, vars should be explicit IRIs to describe.
            # If there is a WHERE clause, vars can be any combination of explicit IRIs
            # and variables.
            VS = set(q.var)

            # If there is no WHERE clause, just return the vars projected
            if q.where is None:
                return None, list(VS)

            # Otherwise, evaluate the WHERE clause like SELECT DISTINCT
            else:
                q.modifier = "DISTINCT"

        else:
            traverse(q.where, functools.partial(_findVars, res=VS))

        # depth-first recursive generation of mapped query tree
        M = translateGroupGraphPattern(q.where)

        aggregate = False
        if q.groupby:
            conditions = []
            # convert "GROUP BY (?expr as ?var)" to an Extend
            for c in q.groupby.condition:
                if isinstance(c, CompValue) and c.name == "GroupAs":
                    M = Extend(M, c.expr, c.var)
                    c = c.var
                conditions.append(c)

            M = Group(p=M, expr=conditions)
            aggregate = True
        elif (
            traverse(q.having, _hasAggregate, complete=False)
            or traverse(q.orderby, _hasAggregate, complete=False)
            or any(
                traverse(x.expr, _hasAggregate, complete=False)
                for x in q.projection or []
                if x.evar
            )
        ):
            # if any aggregate is used, implicit group by
            M = Group(p=M)
            aggregate = True

        if aggregate:
            M, aggregateAliases = translateAggregates(q, M)
        else:
            aggregateAliases = []

        # Need to remove the aggregate var aliases before joining to VALUES;
        # else the variable names won't match up correctly when aggregating.
        for alias, var in aggregateAliases:
            M = Extend(M, alias, var)

        # HAVING
        if q.having:
            M = Filter(expr=and_(*q.having.condition), p=M)

        # VALUES
        if q.valuesClause:
            M = Join(p1=M, p2=ToMultiSet(translateValues(q.valuesClause)))

        if not q.projection:
            # select *

            # Find the first child projection in each branch of the mapped query tree,
            # then include the variables it projects out in our projected variables.
            for child_projection in _find_first_child_projections(M):
                VS |= set(child_projection.PV)

            PV = list(VS)
        else:
            E = list()
            PV = list()
            for v in q.projection:
                if v.var:
                    if v not in PV:
                        PV.append(v.var)
                elif v.evar:
                    if v not in PV:
                        PV.append(v.evar)

                    E.append((v.expr, v.evar))
                else:
                    raise Exception("I expected a var or evar here!")

            for e, v in E:
                M = Extend(M, e, v)

        # ORDER BY
        if q.orderby:
            M = OrderBy(
                M,
                [
                    CompValue("OrderCondition", expr=c.expr, order=c.order)
                    for c in q.orderby.condition
                ],
            )

        # PROJECT
        M = Project(M, PV)

        if q.modifier:
            if q.modifier == "DISTINCT":
                M = CompValue("Distinct", p=M)
            elif q.modifier == "REDUCED":
                M = CompValue("Reduced", p=M)

        if q.limitoffset:
            offset = 0
            if q.limitoffset.offset is not None:
                offset = q.limitoffset.offset.toPython()

            if q.limitoffset.limit is not None:
                M = CompValue(
                    "Slice", p=M, start=offset, length=q.limitoffset.limit.toPython()
                )
            else:
                M = CompValue("Slice", p=M, start=offset)

        return M, PV

## `` translateAggregates

    translateAggregates(q: CompValue, M: CompValue) -> Tuple[CompValue, List[Tuple[Variable, Variable]]]

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

    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600

    def translateAggregates(
        q: CompValue, M: CompValue
    ) -> Tuple[CompValue, List[Tuple[Variable, Variable]]]:
        E: List[Tuple[Variable, Variable]] = []
        A: List[CompValue] = []

        # collect/replace aggs in :
        #    select expr as ?var
        if q.projection:
            for v in q.projection:
                if v.evar:
                    v.expr = traverse(v.expr, functools.partial(_sample, v=v.evar))
                    v.expr = traverse(v.expr, functools.partial(_aggs, A=A))

        # having clause
        if traverse(q.having, _hasAggregate, complete=True):
            q.having = traverse(q.having, _sample)
            traverse(q.having, functools.partial(_aggs, A=A))

        # order by
        if traverse(q.orderby, _hasAggregate, complete=False):
            q.orderby = traverse(q.orderby, _sample)
            traverse(q.orderby, functools.partial(_aggs, A=A))

        # sample all other select vars
        # TODO: only allowed for vars in group-by?
        if q.projection:
            for v in q.projection:
                if v.var:
                    rv = Variable("__agg_%d__" % (len(A) + 1))
                    A.append(CompValue("Aggregate_Sample", vars=v.var, res=rv))
                    E.append((rv, v.var))

        return CompValue("AggregateJoin", A=A, p=M), E

## `` translateAlgebra

    translateAlgebra(query_algebra: Query) -> str

Translates a SPARQL 1.1 algebra tree into the corresponding query string.

Parameters:

- ### `query_algebra`

  (`Query`) –

  An algebra returned by `translateQuery`.

Returns:

- `str` –

  The query form generated from the SPARQL 1.1 algebra tree for SELECT queries.

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

    1643
    1644
    1645
    1646
    1647
    1648
    1649
    1650
    1651
    1652
    1653
    1654
    1655
    1656
    1657

    def translateAlgebra(query_algebra: Query) -> str:
        """
        Translates a SPARQL 1.1 algebra tree into the corresponding query string.

        Args:
            query_algebra: An algebra returned by `translateQuery`.

        Returns:
            The query form generated from the SPARQL 1.1 algebra tree for
                SELECT queries.
        """
        query_from_algebra = _AlgebraTranslator(
            query_algebra=query_algebra
        ).translateAlgebra()
        return query_from_algebra

## `` translateExists

    translateExists(e: Union[Expr, Literal, Variable, URIRef]) -> Union[Expr, Literal, Variable, URIRef]

Translate the graph pattern used by EXISTS and NOT EXISTS http://www.w3.org/TR/sparql11-query/#sparqlCollectFilters

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

    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275

    def translateExists(
        e: typing.Union[Expr, Literal, Variable, URIRef]
    ) -> typing.Union[Expr, Literal, Variable, URIRef]:
        """
        Translate the graph pattern used by EXISTS and NOT EXISTS
        http://www.w3.org/TR/sparql11-query/#sparqlCollectFilters
        """

        def _c(n):
            if isinstance(n, CompValue):
                if n.name in ("Builtin_EXISTS", "Builtin_NOTEXISTS"):
                    n.graph = translateGroupGraphPattern(n.graph)
                    if n.graph.name == "Filter":
                        # filters inside (NOT) EXISTS can see vars bound outside
                        n.graph.no_isolated_scope = True

        e = traverse(e, visitPost=_c)

        return e

## `` translateGraphGraphPattern

    translateGraphGraphPattern(graphPattern: CompValue) -> CompValue

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

    315
    316

    def translateGraphGraphPattern(graphPattern: CompValue) -> CompValue:
        return Graph(graphPattern.term, translateGroupGraphPattern(graphPattern.graph))

## `` translateGroupGraphPattern

    translateGroupGraphPattern(graphPattern: CompValue) -> CompValue

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

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

    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392

    def translateGroupGraphPattern(graphPattern: CompValue) -> CompValue:
        """
        http://www.w3.org/TR/sparql11-query/#convertGraphPattern
        """

        if graphPattern.translated:
            # This occurs if it is attempted to translate a group graph pattern twice,
            # which occurs with nested (NOT) EXISTS filters. Simply return the already
            # translated pattern instead.
            return graphPattern
        if graphPattern.name == "SubSelect":
            # The first output from translate cannot be None for a subselect query
            # as it can only be None for certain DESCRIBE queries.
            # type error: Argument 1 to "ToMultiSet" has incompatible type "Optional[CompValue]";
            #   expected "Union[List[Dict[Variable, str]], CompValue]"
            return ToMultiSet(translate(graphPattern)[0])  # type: ignore[arg-type]

        if not graphPattern.part:
            graphPattern.part = []  # empty { }

        filters = collectAndRemoveFilters(graphPattern.part)

        g: List[CompValue] = []
        for p in graphPattern.part:
            if p.name == "TriplesBlock":
                # merge adjacent TripleBlocks
                if not (g and g[-1].name == "BGP"):
                    g.append(BGP())
                g[-1]["triples"] += triples(p.triples)
            else:
                g.append(p)

        G = BGP()
        for p in g:
            if p.name == "OptionalGraphPattern":
                A = translateGroupGraphPattern(p.graph)
                if A.name == "Filter":
                    G = LeftJoin(G, A.p, A.expr)
                else:
                    G = LeftJoin(G, A, TrueFilter)
            elif p.name == "MinusGraphPattern":
                G = Minus(p1=G, p2=translateGroupGraphPattern(p.graph))
            elif p.name == "GroupOrUnionGraphPattern":
                G = Join(p1=G, p2=translateGroupOrUnionGraphPattern(p))
            elif p.name == "GraphGraphPattern":
                G = Join(p1=G, p2=translateGraphGraphPattern(p))
            elif p.name == "InlineData":
                G = Join(p1=G, p2=translateInlineData(p))
            elif p.name == "ServiceGraphPattern":
                G = Join(p1=G, p2=p)
            elif p.name in ("BGP", "Extend"):
                G = Join(p1=G, p2=p)
            elif p.name == "Bind":
                # translateExists will translate the expression if it is EXISTS, and otherwise return
                # the expression as is. This is needed because EXISTS has a graph pattern
                # which must be translated to work properly during evaluation.
                G = Extend(G, translateExists(p.expr), p.var)

            else:
                raise Exception(
                    "Unknown part in GroupGraphPattern: %s - %s" % (type(p), p.name)
                )

        if filters:
            G = Filter(expr=filters, p=G)

        # Mark this graph pattern as translated
        G.translated = True

        return G

## `` translateGroupOrUnionGraphPattern

    translateGroupOrUnionGraphPattern(graphPattern: CompValue) -> Optional[CompValue]

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

    303
    304
    305
    306
    307
    308
    309
    310
    311
    312

    def translateGroupOrUnionGraphPattern(graphPattern: CompValue) -> Optional[CompValue]:
        A: Optional[CompValue] = None

        for g in graphPattern.graph:
            g = translateGroupGraphPattern(g)
            if not A:
                A = g
            else:
                A = Union(A, g)
        return A

## `` translateInlineData

    translateInlineData(graphPattern: CompValue) -> CompValue

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

    319
    320

    def translateInlineData(graphPattern: CompValue) -> CompValue:
        return ToMultiSet(translateValues(graphPattern))

## `` translatePName

    translatePName(p: Union[CompValue, str], prologue: Prologue) -> Optional[Identifier]

Expand prefixed/relative URIs

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

    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200

    def translatePName(  # type: ignore[return]
        p: typing.Union[CompValue, str], prologue: Prologue
    ) -> Optional[Identifier]:
        """
        Expand prefixed/relative URIs
        """
        if isinstance(p, CompValue):
            if p.name == "pname":
                # type error: Incompatible return value type (got "Union[CompValue, str, None]", expected "Optional[Identifier]")
                return prologue.absolutize(p)  # type: ignore[return-value]
            if p.name == "literal":
                # type error: Argument "datatype" to "Literal" has incompatible type "Union[CompValue, str, None]"; expected "Optional[str]"
                return Literal(
                    p.string, lang=p.lang, datatype=prologue.absolutize(p.datatype)  # type: ignore[arg-type]
                )
        elif isinstance(p, URIRef):
            # type error: Incompatible return value type (got "Union[CompValue, str, None]", expected "Optional[Identifier]")
            return prologue.absolutize(p)  # type: ignore[return-value]

## `` translatePath

    translatePath(p: Union[CompValue, URIRef]) -> Optional[Path]

Translate PropertyPath expressions

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

    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254

    def translatePath(p: typing.Union[CompValue, URIRef]) -> Optional[Path]:  # type: ignore[return]
        """
        Translate PropertyPath expressions
        """

        if isinstance(p, CompValue):
            if p.name == "PathAlternative":
                if len(p.part) == 1:
                    return p.part[0]
                else:
                    return AlternativePath(*p.part)

            elif p.name == "PathSequence":
                if len(p.part) == 1:
                    return p.part[0]
                else:
                    return SequencePath(*p.part)

            elif p.name == "PathElt":
                if not p.mod:
                    return p.part
                else:
                    if isinstance(p.part, list):
                        if len(p.part) != 1:
                            raise Exception("Denkfehler!")

                        return MulPath(p.part[0], p.mod)
                    else:
                        return MulPath(p.part, p.mod)

            elif p.name == "PathEltOrInverse":
                if isinstance(p.part, list):
                    if len(p.part) != 1:
                        raise Exception("Denkfehler!")
                    return InvPath(p.part[0])
                else:
                    return InvPath(p.part)

            elif p.name == "PathNegatedPropertySet":
                if isinstance(p.part, list):
                    return NegatedPath(AlternativePath(*p.part))
                else:
                    return NegatedPath(p.part)

## `` translatePrologue

    translatePrologue(p: ParseResults, base: Optional[str], initNs: Optional[Mapping[str, Any]] = None, prologue: Optional[Prologue] = None) -> Prologue

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

    811
    812
    813
    814
    815
    816
    817
    818
    819
    820
    821
    822
    823
    824
    825
    826
    827
    828
    829
    830
    831
    832
    833

    def translatePrologue(
        p: ParseResults,
        base: Optional[str],
        initNs: Optional[Mapping[str, Any]] = None,
        prologue: Optional[Prologue] = None,
    ) -> Prologue:
        if prologue is None:
            prologue = Prologue()
            prologue.base = ""
        if base:
            prologue.base = base
        if initNs:
            for k, v in initNs.items():
                prologue.bind(k, v)

        x: CompValue
        for x in p:
            if x.name == "Base":
                prologue.base = x.iri
            elif x.name == "PrefixDecl":
                prologue.bind(x.prefix, prologue.absolutize(x.iri))

        return prologue

## `` translateQuads

    translateQuads(quads: CompValue) -> Tuple[List[Tuple[Identifier, Identifier, Identifier]], DefaultDict[str, List[Tuple[Identifier, Identifier, Identifier]]]]

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

    836
    837
    838
    839
    840
    841
    842
    843
    844
    845
    846
    847
    848
    849
    850
    851
    852
    853
    854
    855
    856

    def translateQuads(
        quads: CompValue,
    ) -> Tuple[
        List[Tuple[Identifier, Identifier, Identifier]],
        DefaultDict[str, List[Tuple[Identifier, Identifier, Identifier]]],
    ]:
        if quads.triples:
            alltriples = triples(quads.triples)
        else:
            alltriples = []

        allquads: DefaultDict[str, List[Tuple[Identifier, Identifier, Identifier]]] = (
            collections.defaultdict(list)
        )

        if quads.quadsNotTriples:
            for q in quads.quadsNotTriples:
                if q.triples:
                    allquads[q.term] += triples(q.triples)

        return alltriples, allquads

## `` translateQuery

    translateQuery(q: ParseResults, base: Optional[str] = None, initNs: Optional[Mapping[str, Any]] = None) -> Query

Translate a query-parsetree to a SPARQL Algebra Expression

Return a rdflib.plugins.sparql.sparql.Query object

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

    912
    913
    914
    915
    916
    917
    918
    919
    920
    921
    922
    923
    924
    925
    926
    927
    928
    929
    930
    931
    932
    933
    934
    935
    936
    937
    938
    939
    940
    941
    942
    943
    944
    945

    def translateQuery(
        q: ParseResults,
        base: Optional[str] = None,
        initNs: Optional[Mapping[str, Any]] = None,
    ) -> Query:
        """
        Translate a query-parsetree to a SPARQL Algebra Expression

        Return a rdflib.plugins.sparql.sparql.Query object
        """

        # We get in: (prologue, query)

        prologue = translatePrologue(q[0], base, initNs)

        # absolutize/resolve prefixes
        q[1] = traverse(
            q[1], visitPost=functools.partial(translatePName, prologue=prologue)
        )

        P, PV = translate(q[1])
        datasetClause = q[1].datasetClause
        if q[1].name == "ConstructQuery":
            template = triples(q[1].template) if q[1].template else None

            res = CompValue(q[1].name, p=P, template=template, datasetClause=datasetClause)
        else:
            res = CompValue(q[1].name, p=P, datasetClause=datasetClause, PV=PV)

        res = traverse(res, visitPost=simplify)
        _traverseAgg(res, visitor=analyse)
        _traverseAgg(res, _addVars)

        return Query(prologue, res)

## `` translateUpdate

    translateUpdate(q: CompValue, base: Optional[str] = None, initNs: Optional[Mapping[str, Any]] = None) -> Update

Returns a list of SPARQL Update Algebra expressions

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

    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

    def translateUpdate(
        q: CompValue,
        base: Optional[str] = None,
        initNs: Optional[Mapping[str, Any]] = None,
    ) -> Update:
        """
        Returns a list of SPARQL Update Algebra expressions
        """

        res: List[CompValue] = []
        prologue = None
        if not q.request:
            # type error: Incompatible return value type (got "List[CompValue]", expected "Update")
            return res  # type: ignore[return-value]
        for p, u in zip(q.prologue, q.request):
            prologue = translatePrologue(p, base, initNs, prologue)

            # absolutize/resolve prefixes
            u = traverse(u, visitPost=functools.partial(translatePName, prologue=prologue))
            u = _traverse(u, _simplifyFilters)

            u = traverse(u, visitPost=translatePath)

            res.append(translateUpdate1(u, prologue))

        # type error: Argument 1 to "Update" has incompatible type "Optional[Any]"; expected "Prologue"
        return Update(prologue, res)  # type: ignore[arg-type]

## `` translateUpdate1

    translateUpdate1(u: CompValue, prologue: Prologue) -> CompValue

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

    859
    860
    861
    862
    863
    864
    865
    866
    867
    868
    869
    870
    871
    872
    873
    874
    875
    876
    877
    878
    879
    880

    def translateUpdate1(u: CompValue, prologue: Prologue) -> CompValue:
        if u.name in ("Load", "Clear", "Drop", "Create"):
            pass  # no translation needed
        elif u.name in ("Add", "Move", "Copy"):
            pass
        elif u.name in ("InsertData", "DeleteData", "DeleteWhere"):
            t, q = translateQuads(u.quads)
            u["quads"] = q
            u["triples"] = t
            if u.name in ("DeleteWhere", "DeleteData"):
                pass  # TODO: check for bnodes in triples
        elif u.name == "Modify":
            if u.delete:
                u.delete["triples"], u.delete["quads"] = translateQuads(u.delete.quads)
            if u.insert:
                u.insert["triples"], u.insert["quads"] = translateQuads(u.insert.quads)
            u["where"] = translateGroupGraphPattern(u.where)
        else:
            raise Exception("Unknown type of update operation: %s" % u)

        u.prologue = prologue
        return u

## `` translateValues

    translateValues(v: CompValue) -> Union[List[Dict[Variable, str]], CompValue]

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

    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621

    def translateValues(
        v: CompValue,
    ) -> typing.Union[List[Dict[Variable, str]], CompValue]:
        # if len(v.var)!=len(v.value):
        #     raise Exception("Unmatched vars and values in ValueClause: "+str(v))

        res: List[Dict[Variable, str]] = []
        if not v.var:
            return res
        if not v.value:
            return res
        if not isinstance(v.value[0], list):
            for val in v.value:
                res.append({v.var[0]: val})
        else:
            for vals in v.value:
                res.append(dict(zip(v.var, vals)))

        return Values(res)

## `` traverse

    traverse(tree, visitPre: Callable[[Any], Any] = lambda n: None, visitPost: Callable[[Any], Any] = lambda n: None, complete: Optional[bool] = None) -> Any

Traverse tree, visit each node with visit function visit function may raise StopTraversal to stop traversal if complete!=None, it is returned on complete traversal, otherwise the transformed tree is returned

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

    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469

    def traverse(
        tree,
        visitPre: Callable[[Any], Any] = lambda n: None,
        visitPost: Callable[[Any], Any] = lambda n: None,
        complete: Optional[bool] = None,
    ) -> Any:
        """
        Traverse tree, visit each node with visit function
        visit function may raise StopTraversal to stop traversal
        if complete!=None, it is returned on complete traversal,
        otherwise the transformed tree is returned
        """
        try:
            r = _traverse(tree, visitPre, visitPost)
            if complete is not None:
                return complete
            return r
        except StopTraversal as st:
            return st.rv

## `` triples

    triples(l: Union[List[List[Identifier]], List[Tuple[Identifier, Identifier, Identifier]]]) -> List[Tuple[Identifier, Identifier, Identifier]]

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

    171
    172
    173
    174
    175
    176
    177
    178
    179

    def triples(
        l: typing.Union[  # noqa: E741
            List[List[Identifier]], List[Tuple[Identifier, Identifier, Identifier]]
        ]
    ) -> List[Tuple[Identifier, Identifier, Identifier]]:
        _l = reduce(lambda x, y: x + y, l)
        if (len(_l) % 3) != 0:
            raise Exception("these aint triples")
        return reorderTriples((_l[x], _l[x + 1], _l[x + 2]) for x in range(0, len(_l), 3))