<a id="call-procedures"></a>

# Call procedures

Cypher has two `CALL` forms. The subquery form runs a finalised statement in its own scope and is documented on [Call subqueries](subqueries.md). The procedure form invokes a stored procedure and is documented here. A procedure call holds a procedure name, its arguments and the output columns it keeps, and the library checks it with the same scope rules as the rest of the statement.

<a id="procedure-call"></a>

<a id="call-a-procedure-and-read-its-output"></a>

## Call a procedure and read its output

`call_procedure` starts a statement on its own or continues one that already holds reading clauses. The procedure name is one dot-qualified string, such as `dbms.checkConfigValue`, split on dots at construction. The arguments are [expressions](patterns.md#expression): literals, parameters, variable names and any other expression, with a bare string staying a literal value, the same rule `fn` follows under [Call functions](reading.md#functions). The parentheses around the argument list always render, including the empty list of a procedure that takes no arguments.

```python
from cypher_dsl import Configuration, Renderer, call_procedure, name, param

statement = (
    call_procedure("dbms.checkConfigValue", param("setting"), "true")
    .yielding("valid")
    .returning(name("valid"))
)

renderer = Renderer.get(Configuration(server_version="5.26"))
print(renderer.render(statement).cypher)
```

```cypher
CALL dbms.checkConfigValue($setting, 'true') YIELD valid RETURN valid
```

`yielding` names the output columns the call keeps, and the clause after the call reads them under those names.

<a id="standalone-procedure-call"></a>

<a id="call-a-procedure-on-its-own"></a>

## Call a procedure on its own

A call without yield items finalises on its own: `build()` completes the statement the way it completes a write clause, and rendering accepts the builder directly. The server returns every column the procedure produces by default.

```python
from cypher_dsl import Configuration, Renderer, call_procedure

statement = call_procedure("db.labels")

renderer = Renderer.get(Configuration(server_version="5.26"))
print(renderer.render(statement).cypher)
```

```cypher
CALL db.labels()
```

The library does not know procedure signatures. It cannot name the output columns of a standalone call, so it checks nothing about them: the server decides which columns the result holds and what they mean.

<a id="procedure-yield"></a>

<a id="yield-and-rename-output-columns"></a>

## Yield and rename output columns

A standalone call leaves the columns to the server. Inside a larger query the kept columns must be named explicitly, one yield item per column. `yielding` accepts a bare name, a variable, or a variable under one alias: `name("versions").alias("installed")` renames the column `versions` to `installed`. The alias, or the plain name when there is no alias, is the binding the clauses after the call see. Repeated `yielding` calls append to the same list.

```python
from cypher_dsl import Configuration, Renderer, call_procedure, name

statement = (
    call_procedure("dbms.components")
    .yielding("edition", name("versions").alias("installed"))
    .returning(name("installed"))
)

renderer = Renderer.get(Configuration(server_version="5.26"))
print(renderer.render(statement).cypher)
```

```cypher
CALL dbms.components() YIELD edition, versions AS installed RETURN installed
```

A yield item that binds a name the query already holds renders as given, and the server decides whether the redeclaration is legal. Two items that bind the same output name raise `DslError`, even across repeated `yielding` calls, so give each yielded column a distinct name.

<a id="procedure-filter"></a>

<a id="filter-the-yielded-rows"></a>

## Filter the yielded rows

`where` after the yield attaches the filter to the `YIELD`: it renders after the yield items and keeps only the rows that satisfy it.

```python
from cypher_dsl import Configuration, Renderer, call_procedure, lit, name

statement = (
    call_procedure("db.labels")
    .yielding("label")
    .where(name("label") == lit("Person"))
    .returning(name("label"))
)

renderer = Renderer.get(Configuration(server_version="5.26"))
print(renderer.render(statement).cypher)
```

```cypher
CALL db.labels() YIELD `label` WHERE `label` = 'Person' RETURN `label`
```

`label` is a reserved word, so the renderer quotes it. The default escaping quotes only the names that need it. [Bind parameters](parameters.md) states the rule.

The predicate sees the names the outer query holds and the names the call yields:

```python
from cypher_dsl import (
    Configuration,
    Renderer,
    call_procedure,
    match,
    name,
    node,
)

person = node("Person", name="p")
statement = (
    match(person)
    .call_procedure("db.labels")
    .yielding("label")
    .where(name("label") != person["kind"])
    .returning(name("label"))
)

renderer = Renderer.get(Configuration(server_version="5.26"))
print(renderer.render(statement).cypher)
```

```cypher
MATCH (p:Person)
CALL db.labels() YIELD `label`
  WHERE `label` <> p.kind
RETURN `label`
```

A name that the outer query does not hold and the call does not yield is rejected. Repeated `where` calls combine with `AND`, because the call holds one filter position after `YIELD`:

```python
from cypher_dsl import (
    Configuration,
    Renderer,
    call_procedure,
    lit,
    name,
    param,
)

statement = (
    call_procedure(
        "dbms.checkConfigValue", param("setting"), param("expected")
    )
    .yielding(name("valid"), name("message").alias("observed"))
    .where(name("valid") == lit(True))
    .where(name("observed") != None)
    .returning(name("valid"), name("observed"))
)

renderer = Renderer.get(Configuration(server_version="5.26"))
print(renderer.render(statement).cypher)
```

```cypher
CALL dbms.checkConfigValue($setting, $expected)
  YIELD valid, message AS observed
  WHERE valid = true AND observed IS NOT NULL
RETURN valid, observed
```

<a id="optional-procedure-call"></a>

<a id="render-an-optional-procedure-call"></a>

## Render an optional procedure call

`optional_call_procedure` renders `OPTIONAL CALL`. It takes the procedure name and its arguments the way `call_procedure` does, and `yielding` names the output columns the call keeps. The [CALL procedure manual](https://neo4j.com/docs/operations-manual/current/procedures/call-procedures/#call-vs-optional-call) covers the row semantics of `OPTIONAL CALL`.

```python
from cypher_dsl import (
    Configuration,
    Renderer,
    match,
    name,
    node,
)

person = node("Person", name="p")
statement = (
    match(person)
    .optional_call_procedure("db.labels")
    .yielding("label")
    .returning(person["display_name"], name("label"))
)

renderer = Renderer.get(Configuration(server_version="5.26"))
print(renderer.render(statement).cypher)
```

```cypher
MATCH (p:Person)
OPTIONAL CALL db.labels() YIELD `label`
RETURN p.display_name, `label`
```

<a id="procedure-call-position"></a>

<a id="where-a-procedure-call-may-appear"></a>

## Where a procedure call may appear

A procedure call is a reading clause, like `MATCH` and `UNWIND`, and it is distinct from a `CALL` subquery. A call without yield items may follow an earlier clause and may end a statement. A call with `YIELD` must feed a following clause: `RETURN`, `WITH` or any other clause that consumes the yielded names. The [WITH manual](https://neo4j.com/docs/cypher-manual/current/clauses/with/) states when a query part needs a `WITH` boundary. The library renders the call in either position and leaves that sequencing rule to the server.

```python
from cypher_dsl import (
    Configuration,
    Renderer,
    assign,
    call_procedure,
    match,
    name,
    node,
)

person = node("Person", name="p")
statement = (
    match(person)
    .set_(assign(person["flags"], "seen"))
    .with_(person)
    .call_procedure("db.labels")
    .yielding("label")
    .returning(person, name("label"))
)

renderer = Renderer.get(Configuration(server_version="5.26"))
print(renderer.render(statement).cypher)
```

```cypher
MATCH (p:Person) SET p.flags = 'seen'
WITH p CALL db.labels() YIELD `label`
RETURN p, `label`
```

A call without yield items may end a standalone statement or follow an earlier clause, the way a write clause may. A call with yield items must feed a following clause.

<a id="what-is-not-available"></a>

## What is not available

- No `YIELD *`. Every kept column is named explicitly.
- No parenthesis omission. A procedure that takes no arguments still renders the empty argument list.
- No `SHOW PROCEDURES` or the other catalogue commands.
