> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ocient.com/llms.txt
> Use this file to discover all available pages before exploring further.

# OCGraph Java Library

export const Spark = "Apache® Spark™";

export const Ocient = "Ocient®";

export const Java = "Java®";

The `OCGraph` {Java} class in the {Ocient} System implements a set of graph operations (similar to {Spark} GraphX) directly on tables using JDBC SQL. The overall structure of a graph comprises at least two database tables: one for vertices (nodes) and one for edges (directed links). The OCGraph library runs graph algorithms and transformations by executing SQL over these tables.

## Getting Started

To use the OCGraph library of methods, you must have access to an installed Ocient System and the JDBC driver. For details, see [JDBC Manual](/jdbc-manual).

Use JDBC for connectivity and the `OCGraph` class for graph APIs. The Java library depends on a valid `java.sql.Statement` class to connect to the Ocient System. The class builds and executes SQL using that statement and does not manage connections itself.

**Import the** `OCGraph` **Class**

Specify the `com.ocient.jdbc.graph.OCGraph` class in the `import` declaration.

```java Java theme={null}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import com.ocient.jdbc.graph.OCGraph;
```

### Data Model Requirements

Database tables that use the `OCGraph` library must adhere to a GraphX table structure. In addition to the listed requirements, tables can include other columns.

| **Table**      | **Description**                                                                                                                                                                 | **Requirements**                                                                                             |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ |
| Vertices table | A table with one row per vertex (node). This table typically represents the anchor for graph algorithms and transforms. Many methods join edges to vertices by the `id` column. | The table must contain the<br />`id BIGINT NOT NULL` column definition as the unique vertex identifier.      |
| Edges table    | A table with one row per directed edge (relationship). Each row is a directed edge from a source vertex to a destination vertex.                                                | The table must contain these column definitions:<br />`srcid BIGINT NOT NULL`,<br />`destid BIGINT NOT NULL` |

## Subgraph and Filtering

Use a subgraph or various filters to restrict a graph to relevant vertices and edges. These methods create filtered copies or masked intersections, preserving schema and optional indexes for performance.

### subgraph

Creates filtered vertex and edge tables using vertex and triplet predicates, retaining only edges with endpoints that remain after vertex filtering. The method creates the requested indexes and performs best-effort cleanup in the event of failure.

**Syntax**

```java Java theme={null}
subgraph(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    resultSchema,
    resultVerticesTable,
    resultEdgesTable,
    vertexFilter,
    edgeFilter,
    [ resultVerticesIndexes [ , ... ] ],
    [ resultEdgesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**            | **Data Type**      | **Description**                                                                                                                                                                                   |
| ----------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`           | String             | A non-empty schema containing the input tables.                                                                                                                                                   |
| `inputVerticesTable`    | String             | Input vertices table (must have an `id` column).                                                                                                                                                  |
| `inputEdgesTable`       | String             | Input edges table (must have `srcid` and `destid` columns).                                                                                                                                       |
| `resultSchema`          | String             | A writable schema to create the result tables.                                                                                                                                                    |
| `resultVerticesTable`   | String             | Name of the filtered vertices table to create.                                                                                                                                                    |
| `resultEdgesTable`      | String             | Name of the filtered edges table to create.                                                                                                                                                       |
| `vertexFilter`          | String             | SQL predicate to filter the vertices (without the `WHERE` keyword). Example: `status = 'ACTIVE' AND score > 0`                                                                                    |
| `edgeFilter`            | String             | SQL predicate that the system evaluates in a triplet context using the aliases `a` (source vertex), `b` (edge), and `c` (destination vertex). Example: `b.weight > 0.5 AND a.country = c.country` |
| `resultVerticesIndexes` | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none.                                                                                             |
| `resultEdgesIndexes`    | ArrayList          | Optional. Columns to index in the result edges table (e.g., `srcid` and `destid` columns). Specify an empty list for none.                                                                        |
| `stmt`                  | java.sql.Statement | JDBC statement for executing SQL.                                                                                                                                                                 |

**Example**

Create an active customer subgraph that includes only purchases exceeding \$50 where the source and destination share a region.

```java Java theme={null}
OCGraph.subgraph(
    "sales",
    "customers",
    "purchases",
    "sales",
    "customers_active",
    "purchases_active",
    "status = 'ACTIVE' AND score > 0",
    "b.amount > 50 AND a.region = c.region",
    new ArrayList<>(List.of("id","region")),
    new ArrayList<>(List.of("srcid","destid")),
    stmt
);
```

### filterVertices

Creates a filtered subgraph by selecting vertices that match a predicate while retaining only edges with endpoints that are in the filtered vertex set.

**Syntax**

```java Java theme={null}
filterVertices(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    resultSchema,
    resultVerticesTable,
    resultEdgesTable,
    vertexFilter,
    [ resultVerticesIndexes [ , ... ] ],
    [ resultEdgesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**            | **Data Type**      | **Description**                                                                                                            |
| ----------------------- | ------------------ | -------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`           | String             | A non-empty schema containing the input tables.                                                                            |
| `inputVerticesTable`    | String             | Input vertices table (must have an `id` column).                                                                           |
| `inputEdgesTable`       | String             | Input edges table (must have `srcid` and `destid` columns).                                                                |
| `resultSchema`          | String             | A writable schema to create the result tables.                                                                             |
| `resultVerticesTable`   | String             | Name of the result vertices table. The name must not conflict with the names of input tables.                              |
| `resultEdgesTable`      | String             | Name of the edges table to create.                                                                                         |
| `vertexFilter`          | String             | SQL predicate for vertices (without the `WHERE` keyword). Example: `status = 'ACTIVE'`                                     |
| `resultVerticesIndexes` | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none.                      |
| `resultEdgesIndexes`    | ArrayList          | Optional. Columns to index in the result edges table (e.g., `srcid` and `destid` columns). Specify an empty list for none. |
| `stmt`                  | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                               |

**Example**

Filter US customers and retain edges with endpoints that remain in the filtered vertex set.

```java Java theme={null}
OCGraph.filterVertices(
    "sales", "customers", "purchases",
    "sales", "customers_us", "purchases_us",
    "country = 'US'",
    new ArrayList<>(List.of("id")),
    new ArrayList<>(List.of("srcid","destid")),
    stmt
);
```

### filterEdges

Creates a filtered edges table by selecting edges that match a predicate.

**Syntax**

```java Java theme={null}
filterEdges(
    inputSchema,
    inputEdgesTable,
    resultSchema,
    resultEdgesTable,
    edgeFilter,
    [ resultEdgesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**         | **Data Type**      | **Description**                                                                                                            |
| -------------------- | ------------------ | -------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`        | String             | A non-empty schema containing the input tables.                                                                            |
| `inputEdgesTable`    | String             | Input edges table (must have `srcid` and `destid` columns).                                                                |
| `resultSchema`       | String             | A writable schema to create the result tables.                                                                             |
| `resultEdgesTable`   | String             | Name of the edges table to create.                                                                                         |
| `edgeFilter`         | String             | SQL predicate on edges (without the `WHERE` keyword). Example: `weight > 0.5 AND type = 'ACTIVE'`                          |
| `resultEdgesIndexes` | ArrayList          | Optional. Columns to index in the result edges table (e.g., `srcid` and `destid` columns). Specify an empty list for none. |
| `stmt`               | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and database connection.                                 |

**Example**

This example demonstrates how to create a filtered edges table from an existing `purchases` edge set by keeping only edges that meet a business rule (`weight > 0.5` and `ACTIVE`). Then, the `filterEdges` method indexes the result on the `srcid` and `destid` columns for faster lookups.

```java Java theme={null}
OCGraph.filterEdges(
    "sales", "purchases",
    "sales", "purchases_filtered",
    "weight > 0.5 AND type = 'ACTIVE'",
    new ArrayList<>(List.of("srcid","destid")),
    stmt
);
```

### mask

Creates a masked subgraph by intersecting two graphs. Vertices intersect if the vertex identifier is present in both graphs. Edges intersect when the `srcid` and `destid` values are present in both graphs.

The method copies rows that intersect from the graph defined by the arguments `inputVerticesTable` and `inputEdgesTable`, including any attributes.

You can optionally create indexes on the result subgraph tables.

**Syntax**

```java Java theme={null}
mask(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    otherSchema,
    otherVerticesTable,
    otherEdgesTable,
    resultSchema,
    resultVerticesTable,
    resultEdgesTable,
    [ resultVerticesIndexes [ , ... ] ],
    [ resultEdgesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**            | **Data Type**      | **Description**                                                                                                                                                                  |
| ----------------------- | ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`           | String             | A non-empty schema containing the primary input tables.                                                                                                                          |
| `inputVerticesTable`    | String             | The primary vertices table to intersect (must have an `id` column).<br /><br />The masked subgraph created by this method copies rows that intersect from this table.            |
| `inputEdgesTable`       | String             | The primary edges table to intersect (must have `srcid` and `destid` columns).<br /><br />The masked subgraph created by this method copies rows that intersect from this table. |
| `otherSchema`           | String             | Schema containing the second graph.                                                                                                                                              |
| `otherVerticesTable`    | String             | The second vertices table to intersect.                                                                                                                                          |
| `otherEdgesTable`       | String             | The second edges table to intersect.                                                                                                                                             |
| `resultSchema`          | String             | A writable schema to create the result tables.                                                                                                                                   |
| `resultVerticesTable`   | String             | Name of the vertices table to create.                                                                                                                                            |
| `resultEdgesTable`      | String             | Name of the edges table to create.                                                                                                                                               |
| `resultVerticesIndexes` | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none.                                                                            |
| `resultEdgesIndexes`    | ArrayList          | Optional. Columns to index in the result edges table (e.g., `srcid` and `destid` columns). Specify an empty list for none.                                                       |
| `stmt`                  | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                                                                                     |

**Example**

Create a masked subgraph by intersecting two graphs. The example copies vertices and edges that are present in both graphs, along with the remaining endpoints.

```java Java theme={null}
OCGraph.mask(
    "sales", "customers", "purchases",
    "ref",   "customers_ref", "purchases_ref",
    "sales", "customers_masked", "purchases_masked",
    new ArrayList<>(List.of("id")),
    new ArrayList<>(List.of("srcid","destid")),
    stmt
);
```

## Transformations

Construct new vertex or edge tables by computing derived columns, reversing direction, or aggregating duplicates. These methods do not change the original inputs. Instead, the methods materialize new results.

### mapVertices

Creates a new vertices table with the identifier `id` and computed columns. Use the `resultColumnExpressions` argument to calculate additional columns. This method can also add indexes before inserting data.

**Syntax**

```java Java theme={null}
mapVertices(
    inputSchema,
    inputVerticesTable,
    resultSchema,
    resultVerticesTable,
    resultColumnExpressions [ , ... ],
    [ resultVerticesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**              | **Data Type**      | **Description**                                                                                                    |
| ------------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------ |
| `inputSchema`             | String             | A non-empty schema containing the input tables.                                                                    |
| `inputVerticesTable`      | String             | Input vertices table (must have an `id` column).                                                                   |
| `resultSchema`            | String             | A writable schema to create the result tables.                                                                     |
| `resultVerticesTable`     | String             | Name of the vertices table to create.                                                                              |
| `resultColumnExpressions` | ArrayList          | One or more SQL expressions defining result columns beyond `id`. Use the `AS alias_name` keyword for stable names. |
| `resultVerticesIndexes`   | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none.              |
| `stmt`                    | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                       |

**Example**

Create vertices by adding derived columns, `name_upper` and `is_vip`, and generate indexes for the `id` and `name_upper` columns.

```java Java theme={null}
OCGraph.mapVertices(
    "sales", "customers",
    "sales", "customers_enriched",
    new ArrayList<>(List.of(
        "UPPER(name) AS name_upper",
        "CASE WHEN score > 1000 THEN true ELSE false END AS is_vip"
    )),
    new ArrayList<>(List.of("id","name_upper")),
    stmt
);
```

### mapEdges

Creates a new edges table with `srcid`, `destid`, and computed columns. Expressions should refer to input edge columns by their original names, and each computed expression should include an `AS alias` keyword.

**Syntax**

```java Java theme={null}
mapEdges(
    inputSchema,
    inputEdgesTable,
    resultSchema,
    resultEdgesTable,
    [ resultColumnExpressions [ , ... ] ],
    [ resultEdgesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**              | **Data Type**      | **Description**                                                                                                                                     |
| ------------------------- | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`             | String             | A non-empty schema containing the input tables.                                                                                                     |
| `inputEdgesTable`         | String             | Input edges table (must have `srcid` and `destid` columns).                                                                                         |
| `resultSchema`            | String             | A writable schema to create the result tables.                                                                                                      |
| `resultEdgesTable`        | String             | Name of the edges table to create.                                                                                                                  |
| `resultColumnExpressions` | ArrayList          | Optional. SQL expressions for additional edge columns (besides the `srcid` and `destid` columns). Use the `AS alias_name` keyword for stable names. |
| `resultEdgesIndexes`      | ArrayList          | Optional. Columns to index in the result edges table (e.g., `srcid` and `destid` columns). Specify an empty list for none.                          |
| `stmt`                    | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                                                        |

**Example**

Create a new edges table with two columns, `discounted_amount` and `big_txn`, and generate indexes for the `srcid` and `destid` columns.

```java Java theme={null}
OCGraph.mapEdges(
    "sales", "purchases",
    "sales", "purchases_enriched",
    new ArrayList<>(List.of(
        "amount * 0.9 AS discounted_amount",
        "CASE WHEN amount > 100 THEN 1 ELSE 0 END AS big_txn"
    )),
    new ArrayList<>(List.of("srcid","destid")),
    stmt
);
```

### mapTriplets

Creates a new edges table with computed columns that reference a triplet made of: `a` (source vertex), `b` (edge), and `c` (destination vertex). The output automatically includes `b.srcid` and `b.destid` columns.

**Syntax**

```java Java theme={null}
mapTriplets(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    resultSchema,
    resultEdgesTable,
    [ resultColumnExpressions [ , ... ] ],
    [ resultEdgesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**              | **Data Type**      | **Description**                                                                                                            |
| ------------------------- | ------------------ | -------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`             | String             | A non-empty schema containing the input tables.                                                                            |
| `inputVerticesTable`      | String             | Input vertices table (must have an `id` column). Used as the `a` and `c` vertices.                                         |
| `inputEdgesTable`         | String             | Input edges table (must have the `srcid` and `destid` columns). This method uses this argument as the `b` edge.            |
| `resultSchema`            | String             | A writable schema to create the result edges table.                                                                        |
| `resultEdgesTable`        | String             | Name of the edges table to create.                                                                                         |
| `resultColumnExpressions` | ArrayList          | Optional. SQL expressions that can reference `a`, `b`, or `c`. Use the `AS alias_name` keyword for stable names.           |
| `resultEdgesIndexes`      | ArrayList          | Optional. Columns to index in the result edges table (e.g., `srcid` and `destid` columns). Specify an empty list for none. |
| `stmt`                    | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                               |

**Example**

Create a new triplet table from the vertices and edges tables with the `amount` and `same_country` columns, and generate indexes for the `src` and `destid` columns.

```java Java theme={null}
OCGraph.mapTriplets(
    "sales", "customers", "purchases",
    "sales", "purchases_triplets",
    new ArrayList<>(List.of(
        "b.amount AS amount",
        "CASE WHEN a.country = c.country THEN 1 ELSE 0 END AS same_country"
    )),
    new ArrayList<>(List.of("srcid","destid")),
    stmt
);
```

### reverseEdges

Creates a new edges table with the `srcid` and `destid` columns reversed, preserving other columns. Use this method to traverse a graph in the opposite direction.

**Syntax**

```java Java theme={null}
reverseEdges(
    inputSchema,
    inputEdgesTable,
    resultSchema,
    resultEdgesTable,
    [ resultEdgesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**         | **Data Type**      | **Description**                                                                                                            |
| -------------------- | ------------------ | -------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`        | String             | A non-empty schema containing the input tables.                                                                            |
| `inputEdgesTable`    | String             | Input edges table (must have the `srcid` and `destid` columns).                                                            |
| `resultSchema`       | String             | A writable schema to create the result tables.                                                                             |
| `resultEdgesTable`   | String             | Name of the reversed edges table to create.                                                                                |
| `resultEdgesIndexes` | ArrayList          | Optional. Columns to index in the result edges table (e.g., `srcid` and `destid` columns). Specify an empty list for none. |
| `stmt`               | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                               |

**Example**

Transform edge direction by reversing the `srcid` and `destid` columns. The example also creates indexes for these columns.

```java Java theme={null}
OCGraph.reverseEdges(
    "sales", "purchases",
    "sales", "purchases_reversed",
    new ArrayList<>(List.of("srcid","destid")),
    stmt
);
```

### groupEdges

Groups duplicate rows of the `srcid` and `destid` columns, producing one row for each unique pair of values in a new edges table. This method performs aggregations based on one or more SQL expressions.

**Syntax**

```java Java theme={null}
groupEdges(
    inputSchema,
    inputEdgesTable,
    resultSchema,
    resultEdgesTable,
    resultColumnExpressions [ , ... ],
    [ resultEdgesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**              | **Data Type**      | **Description**                                                                                                                                                                                                           |
| ------------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`             | String             | A non-empty schema containing the input tables.                                                                                                                                                                           |
| `inputEdgesTable`         | String             | Input edges table (must have the `srcid` and `destid` columns).                                                                                                                                                           |
| `resultSchema`            | String             | A writable schema to create the grouped tables.                                                                                                                                                                           |
| `resultEdgesTable`        | String             | Name of the grouped edges table to create.                                                                                                                                                                                |
| `resultColumnExpressions` | ArrayList          | One or more aggregate expressions for the `srcid` and `destid` columns. For details about SQL aggregations, see [Aggregate Functions](/aggregate-functions).<br /><br />Use the `AS alias_name` keyword for stable names. |
| `resultEdgesIndexes`      | ArrayList          | Optional. Columns to index in the result edges table (e.g., `srcid` and `destid` columns). Specify an empty list for none.                                                                                                |
| `stmt`                    | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                                                                                                                              |

**Example**

Create a new edge table that includes SQL aggregations for counting unique transactions `txn_count` and total sums `total_amount`. Also, this method generates indexes for the `src` and `destid` columns.

```java Java theme={null}
OCGraph.groupEdges(
    "sales", "purchases",
    "sales", "purchases_grouped",
    new ArrayList<>(List.of(
        "COUNT(*) AS txn_count",
        "SUM(amount) AS total_amount"
    )),
    new ArrayList<>(List.of("srcid","destid")),
    stmt
);
```

## Triplets

Produce triplet representations that are made of `a` (source vertex), `b` (edge), and `c` (destination vertex), either as a logical view or a materialized table for downstream queries.

### createTripletsView

Creates a view that combines the edge table with the source and destination vertex attributes. This view is useful for analyzing relationships without having to repeatedly join tables. The view includes these columns:

* All original edge columns (including the `srcid` and `destid` columns).
* All source-vertex columns except `id`. Source-vertex column names have the `src_` prefix.
* All destination-vertex columns except `id`. Destination-vertex column names have the  `dest_` prefix.

Use the [createTripletsTable](#createtripletstable) method if you want to create a materialized table with indexes instead of a view.

**Syntax**

```java Java theme={null}
createTripletsView(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    resultSchema,
    resultTripletsView,
    stmt
)
```

| **Argument**         | **Data Type**      | **Description**                                                                                 |
| -------------------- | ------------------ | ----------------------------------------------------------------------------------------------- |
| `inputSchema`        | String             | A non-empty schema containing the input tables.                                                 |
| `inputVerticesTable` | String             | Input vertices table (must have an `id` column).                                                |
| `inputEdgesTable`    | String             | Input edges table (must have the `srcid` and `destid` columns).                                 |
| `resultSchema`       | String             | A schema to create the view.                                                                    |
| `resultTripletsView` | String             | Name of the triplets view to create. The name must not conflict with the names of input tables. |
| `stmt`               | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.    |

**Example**

Create a triplets view to inspect edges with joined source and destination vertex attributes.

```java Java theme={null}
OCGraph.createTripletsView(
    "sales", "customers", "purchases",
    "sales", "triplets_v",
    stmt
);
```

### createTripletsTable

Creates a materialized table that combines the edge table with the source and destination vertex attributes. This table is useful for analyzing relationships without having to repeatedly join tables. The created table includes these columns:

* All original edge columns (including the `srcid` and `destid` columns).
* All source-vertex columns except `id`. Source-vertex column names have the `src_` prefix.
* All destination-vertex columns except `id`. Destination-vertex column names have the `dest_` prefix.

Use the [createTripletsView](#createtripletsview) method if you want to create a view instead of a new table.

**Syntax**

```java Java theme={null}
createTripletsTable(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    resultSchema,
    resultTripletsTable,
    [ resultTripletsIndexes [ , ... ] ],
    stmt
)
```

| **Argument**            | **Data Type**      | **Description**                                                                                  |
| ----------------------- | ------------------ | ------------------------------------------------------------------------------------------------ |
| `inputSchema`           | String             | A non-empty schema containing the input tables.                                                  |
| `inputVerticesTable`    | String             | Input vertices table (must have an `id` column).                                                 |
| `inputEdgesTable`       | String             | Input edges table (must have the `srcid` and `destid` columns).                                  |
| `resultSchema`          | String             | A schema to create the triplets table.                                                           |
| `resultTripletsTable`   | String             | Name of the triplets table to create. The name must not conflict with the names of input tables. |
| `resultTripletsIndexes` | ArrayList          | Optional. Columns to index in the result table. Specify an empty list for none.                  |
| `stmt`                  | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.     |

**Example**

Create a new table for triplets. Generate indexes for the `src_id` and `dest_id` columns.

```java Java theme={null}
OCGraph.createTripletsTable(
    "sales", "customers", "purchases",
    "sales", "triplets_t",
    new ArrayList<>(List.of("src_id","dest_id")),
    stmt
);
```

## Degrees

Compute degree metrics for each vertex from the edges table. These methods produce small vertex tables suitable for joins and analytics.

### inDegrees

Computes how many edges point to each vertex in an edge table by counting how many times each unique `destid` value appears. The result table has two columns: `id` (the destination vertex) and `in_degree` (the count).

**Syntax**

```java Java theme={null}
inDegrees(
    inputSchema,
    inputEdgesTable,
    resultSchema,
    resultVerticesTable,
    [ resultVerticesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**            | **Data Type**      | **Description**                                                                                       |
| ----------------------- | ------------------ | ----------------------------------------------------------------------------------------------------- |
| `inputSchema`           | String             | A non-empty schema containing the input table.                                                        |
| `inputEdgesTable`       | String             | Input edges table (must have the `srcid` and `destid` columns).                                       |
| `resultSchema`          | String             | A schema to create the in-degree table.                                                               |
| `resultVerticesTable`   | String             | Name of the vertices table to create (columns include `id` and  `in_degree`).                         |
| `resultVerticesIndexes` | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none. |
| `stmt`                  | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.          |

**Example**
Compute in-degrees per vertex and generate an index on the `id` column.

```java Java theme={null}
OCGraph.inDegrees(
    "sales", "purchases",
    "sales", "customers_in_degree",
    new ArrayList<>(List.of("id")),
    stmt
);
```

### outDegrees

Computes how many edges originate from each vertex in an edge table by counting how many times each unique `srcid` value appears. The result table has two columns: `id` (the source vertex) and `out_degree` (the count).

**Syntax**

```java Java theme={null}
outDegrees(
    inputSchema,
    inputEdgesTable,
    resultSchema,
    resultVerticesTable,
    [ resultVerticesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**            | **Data Type**      | **Description**                                                                                       |
| ----------------------- | ------------------ | ----------------------------------------------------------------------------------------------------- |
| `inputSchema`           | String             | A non-empty schema containing the input table.                                                        |
| `inputEdgesTable`       | String             | Input edges table (must have the `srcid` and `destid` columns).                                       |
| `resultSchema`          | String             | Schema to create the out-degree table.                                                                |
| `resultVerticesTable`   | String             | Name of the vertices table to create (columns include `id` and  `out_degree`).                        |
| `resultVerticesIndexes` | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none. |
| `stmt`                  | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.          |

**Example**

Compute the out-degree count for each vertex and generate an index on the `id` column.

```java Java theme={null}
OCGraph.outDegrees(
    "sales", "purchases",
    "sales", "customers_out_degree",
    new ArrayList<>(List.of("id")),
    stmt
);
```

### degrees

Computes the total degrees (in-degrees and out-degrees) for each vertex in an edge table by counting how many times each unique `srcid` and `destid` value appears. The result table has two columns: `id` (the destination or source vertex) and `degree` (the count).

**Syntax**

```java Java theme={null}
degrees(
    inputSchema,
    inputEdgesTable,
    resultSchema,
    resultVerticesTable,
    [ resultVerticesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**            | **Data Type**      | **Description**                                                                                       |
| ----------------------- | ------------------ | ----------------------------------------------------------------------------------------------------- |
| `inputSchema`           | String             | A non-empty schema containing the input table.                                                        |
| `inputEdgesTable`       | String             | Input edges table (must have the `srcid` and `destid` columns).                                       |
| `resultSchema`          | String             | A schema to create the degree table.                                                                  |
| `resultVerticesTable`   | String             | Name of the vertices table to create (columns include `id` and `degree`).                             |
| `resultVerticesIndexes` | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none. |
| `stmt`                  | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.          |

**Example**
Compute total degrees for each vertex and generate an index on the `id` column.

```java Java theme={null}
OCGraph.degrees(
    "sales", "purchases",
    "sales", "customers_degree",
    new ArrayList<>(List.of("id")),
    stmt
);
```

## Vertex Extraction and Joins

Build vertex sets from edges and combine vertex attributes across tables. These methods are useful for shaping vertex properties and consolidating features.

### fromEdges

Builds a vertices table from an edges table by extracting the unique source and destination identifiers.

This method can optionally compute additional columns using SQL expressions by referencing the unique identifier as `ids.id`.

The created table always contains the `id` column with one additional column per expression.

**Syntax**

```java Java theme={null}
fromEdges(
    inputSchema,
    inputEdgesTable,
    resultSchema,
    resultVerticesTable,
    [ resultColumnExpressions [ , ... ] ],
    [ resultVerticesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**              | **Data Type**      | **Description**                                                                                        |
| ------------------------- | ------------------ | ------------------------------------------------------------------------------------------------------ |
| `inputSchema`             | String             | A non-empty schema containing the input table.                                                         |
| `inputEdgesTable`         | String             | Input edges table (must have the `srcid` and `destid` columns).                                        |
| `resultSchema`            | String             | A schema to create the result vertices table.                                                          |
| `resultVerticesTable`     | String             | Name of the result vertices table (always includes `id`).                                              |
| `resultColumnExpressions` | ArrayList          | Optional. Specify a list of SQL expressions that reference `ids.id` to add additional columns.         |
| `resultVerticesIndexes`   | ArrayList          | Optional. Columns to index in the result vertices table  (e.g., `id`). Specify an empty list for none. |
| `stmt`                    | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.           |

**Example**
Create a vertices table from edge endpoints and add a `bucket` column that assigns each vertex to one of 10 buckets. Generate an index for the `id` and `bucket` columns.

```java Java theme={null}
OCGraph.fromEdges(
    "sales", "purchases",
    "sales", "customers_from_edges",
    new ArrayList<>(List.of(
        "ids.id % 10 AS bucket"
    )),
    new ArrayList<>(List.of("id","bucket")),
    stmt
);
```

### joinVertices

Merges two vertices tables by retaining every row from a primary table (`inputVerticesTable`) and selectively updating rows that also appear in the modification table (`modificationVerticesTable`). The merged table includes all vertices from the primary table that do not appear in the modification table.

For vertices that appear in both tables, the method must include a list of expressions (`resultAttributeExpressions`) in the same column order for every non-identifier column in the merged result table. These SQL expressions can add computations to columns or simply add aliases if no changes are needed. Each expression can reference columns from the primary table (using alias `a`) or from the modification table (using alias `b`).

**Syntax**

```java Java theme={null}
joinVertices(
    inputSchema,
    inputVerticesTable,
    modificationSchema,
    modificationVerticesTable,
    resultSchema,
    resultVerticesTable,
    resultAttributeExpressions [ , ... ],
    [ resultVerticesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**                 | **Data Type**      | **Description**                                                                                                                                                                                                                                                                                                                                                       |
| ---------------------------- | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`                | String             | A non-empty schema containing the primary input table.                                                                                                                                                                                                                                                                                                                |
| `inputVerticesTable`         | String             | Input vertices table (with the alias `a`). This table must have an `id` column.                                                                                                                                                                                                                                                                                       |
| `modificationSchema`         | String             | A schema for the modification vertices table.                                                                                                                                                                                                                                                                                                                         |
| `modificationVerticesTable`  | String             | Modification vertices table (with the alias `b`). This table must have an `id` column.                                                                                                                                                                                                                                                                                |
| `resultSchema`               | String             | A writable schema to create the result tables.                                                                                                                                                                                                                                                                                                                        |
| `resultVerticesTable`        | String             | Name of the vertices table to create.                                                                                                                                                                                                                                                                                                                                 |
| `resultAttributeExpressions` | ArrayList          | A list of SQL expressions that define the non-identifier columns of the joined result.<br /><br />Each expression can reference the left (`inputVerticesTable`) vertex as `a` and the right (`modificationVerticesTable`) vertex as `b`.<br /><br />You must end every expression with an explicit alias using `AS alias_name` so the output column names are stable. |
| `resultVerticesIndexes`      | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none.                                                                                                                                                                                                                                                                 |
| `stmt`                       | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                                                                                                                                                                                                                                                                          |

**Example**
Merge vertex attributes and generate indexes for the `id` and `status` columns. This example includes two SQL expressions to update the `status` and `score` columns based on the modification vertex table using the `COALESCE` SQL reference function.

```java Java theme={null}
OCGraph.joinVertices(
    "sales", "customers",
    "sales", "customers_updates",
    "sales", "customers_merged",
    new ArrayList<>(List.of(
        "COALESCE(b.new_status, a.status) AS status",
        "COALESCE(b.score_delta + a.score, a.score) AS score"
    )),
    new ArrayList<>(List.of("id","status")),
  stmt
);
```

### innerJoinVertices

Performs an inner join on two vertex tables using an equality comparison `a.id = b.id`. The result table automatically includes the `id` column from the first table.

The method must include a list of SQL expressions (`resultAttributeExpressions`) in the same column order for every non-identifier column in the merged result table. These SQL expressions can add computations to columns, or simply add aliases if no changes are needed. Each expression can reference columns from the primary table using the alias `a` or from the modification table using the alias `b`.

**Syntax**

```java Java theme={null}
innerJoinVertices(
    inputSchema,
    inputVerticesTable,
    otherSchema,
    otherVerticesTable,
    resultSchema,
    resultVerticesTable,
    resultAttributeExpressions [ , ... ],
    [ resultVerticesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**                 | **Data Type**      | **Description**                                                                                                                                                                                                                                                                                                                                                       |
| ---------------------------- | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`                | String             | A schema for the left vertices table.                                                                                                                                                                                                                                                                                                                                 |
| `inputVerticesTable`         | String             | Input vertices table for the left side of the join (must have an `id` column).                                                                                                                                                                                                                                                                                        |
| `otherSchema`                | String             | A schema for the right vertices table.                                                                                                                                                                                                                                                                                                                                |
| `otherVerticesTable`         | String             | The other vertices table for the right side of the join (must have an `id` column).                                                                                                                                                                                                                                                                                   |
| `resultSchema`               | String             | A writable schema to create the result tables.                                                                                                                                                                                                                                                                                                                        |
| `resultVerticesTable`        | String             | Name of the vertices table to create.                                                                                                                                                                                                                                                                                                                                 |
| `resultAttributeExpressions` | ArrayList          | A list of SQL expressions that define the non-identifier columns of the joined result.<br /><br />Each expression can reference the left (`inputVerticesTable`) vertex as `a` and the right (`otherVerticesTable`) vertex as `b`.<br /><br />You must end every expression with an explicit alias using `AS alias_name` to ensure the output column names are stable. |
| `resultVerticesIndexes`      | ArrayList          | Optional. Columns to index in the result vertices table  (e.g., `id`). Specify an empty list for none.                                                                                                                                                                                                                                                                |
| `stmt`                       | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                                                                                                                                                                                                                                                                          |

**Example**
Create an inner join between two vertex tables and generate an index on the `id` column.

```java Java theme={null}
OCGraph.innerJoinVertices(
    "sales", "customers",
    "sales", "profiles",
    "sales", "customers_joined",
    new ArrayList<>(List.of(
        "a.id AS id",
        "a.status AS status",
        "b.tier AS tier"
    )),
    new ArrayList<>(List.of("id")),
    stmt
);
```

### outerJoinVertices

Performs a left outer join between two vertices tables using an equality comparison `a.id = b.id`. The result table includes all rows from the left table. For left-table rows that have no match in the right table, any expression that reads columns from the right table with the alias `b` evaluates to NULL (while expressions that only read the table with the alias `a` remain non-NULL as usual).

The method must include a list of SQL expressions (`resultAttributeExpressions`) in the same column order for every non-identifier column in the merged result table. These SQL expressions can add computations to columns or simply add aliases if no changes are needed. Each expression can reference columns from the primary table using the alias `a` or from the modification table using the alias `b`.

**Syntax**

```java Java theme={null}
outerJoinVertices(
    inputSchema,
    inputVerticesTable,
    otherSchema,
    otherVerticesTable,
    resultSchema,
    resultVerticesTable,
    resultAttributeExpressions [ , ... ],
    [ resultVerticesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**                 | **Data Type**      | **Description**                                                                                                                                                                                                                                                                                                                                               |
| ---------------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`                | String             | A non-empty schema containing the input tables (aliased as `a`).                                                                                                                                                                                                                                                                                              |
| `inputVerticesTable`         | String             | Left vertices table (must have an `id` column).                                                                                                                                                                                                                                                                                                               |
| `otherSchema`                | String             | Schema of the right vertices table (with the alias `b`).                                                                                                                                                                                                                                                                                                      |
| `otherVerticesTable`         | String             | Right vertices table (must have an `id` column).                                                                                                                                                                                                                                                                                                              |
| `resultSchema`               | String             | A writable schema to create the result tables.                                                                                                                                                                                                                                                                                                                |
| `resultVerticesTable`        | String             | Name of the vertices table to create.                                                                                                                                                                                                                                                                                                                         |
| `resultAttributeExpressions` | ArrayList          | A list of SQL expressions that define the non-ID columns of the joined result.<br /><br />Each expression can reference the left (`inputVerticesTable`) vertex as `a` and the right (`otherVerticesTable`) vertex as `b`.<br /><br />You must end every expression with an explicit alias using `AS alias_name` to ensure the output column names are stable. |
| `resultVerticesIndexes`      | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none.                                                                                                                                                                                                                                                         |
| `stmt`                       | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                                                                                                                                                                                                                                                                  |

**Example**
Perform a left outer join on two vertices tables and generate an index on the `id` column.

```java Java theme={null}
OCGraph.outerJoinVertices(
    "sales", "customers",
    "sales", "profiles",
    "sales", "customers_joined",
    new ArrayList<>(List.of(
        "a.id AS id",
        "a.status AS status",
        "b.tier AS tier"
    )),
    new ArrayList<>(List.of("id")),
    stmt
);
```

### collectNeighbors

For each vertex in a table, this method collects information on neighbors (identifier and any attributes) as an array of tuples. For a specified direction (`IN`, `OUT`, or `BOTH`), the method aggregates tuples representing each neighboring vertex into an array.

The direction types are:

* `IN` — Neighbors with edges pointing to the vertex (edges where `destid = id`).
* `OUT` — Neighbors that the vertex points to (edges where `srcid = id`).
* `BOTH` — Union of `IN` and `OUT` with neighbors from incoming (`destid = id`) and outgoing (`srcid = id`) edges.

The result table has the columns `id` (the vertex identifier) and `neighbors` (an array of tuples representing each neighbor).

If an error occurs after table creation, the method drops the result table.

**Syntax**

```java Java theme={null}
collectNeighbors(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    resultSchema,
    resultTable,
    direction,
    [ resultIndexes [ , ... ] ],
    stmt
)
```

| **Argument**         | **Data Type**      | **Description**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| -------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `inputSchema`        | String             | A non-empty schema containing the input tables.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| `inputVerticesTable` | String             | Input vertices table (must have an `id` column).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| `inputEdgesTable`    | String             | Input edges table (must have the `srcid` and `destid` columns).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| `resultSchema`       | String             | A writable schema to create the result tables.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `resultTable`        | String             | Name of the neighbors collection table (with the `id` and `neighbors` columns).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| `direction`          | EdgeDirection      | Specifies the direction of traversal. Supported values are:<br />`IN` — Neighbors that have edges pointing to the vertex (edges where `destid = id`).<br />For example, if an edge `5` points to `10`, then for `id=10`, neighbor `5` is included. <br /><br />`OUT` — Neighbors that the vertex points to (edges where `srcid = id`). For example, if an edge `5` points to `10`, then for `id=5`, neighbor `10` is included.<br /><br />`BOTH` — The union of `IN` and `OUT`. This traversal includes neighbors from edges pointing to `id` and edges from `id`. |
| `resultIndexes`      | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none.                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| `stmt`               | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |

**Example**
Collect incoming neighbors for each vertex and generate an index on the `id` column. The `direction` argument set to `IN` collects neighbors pointing to `id`.

```java Java theme={null}
OCGraph.collectNeighbors(
    "sales", "customers", "purchases",
    "sales", "neighbors_in",
    OCGraph.EdgeDirection.IN,
    new ArrayList<>(List.of("id")),
    stmt
);
```

### collectEdges

For each vertex in a table, this method collects an array of adjacent edge rows based on the specified direction. The result table has two columns: `id` (the vertex identifier) and `edges` (an array of tuples, each tuple containing all columns from the edges table for a connected edge).

The `direction` types are:

* `IN` — Edges pointing to the vertex (edges where `destid = id`).
* `OUT` — Edges originating from the vertex (edges where `srcid = id`).
* `BOTH` — Union of `IN` and `OUT` that includes edges from incoming (`destid = id`) and outgoing (`srcid = id`) directions. This direction retains duplicates.

If an error occurs after table creation, the method drops the result table.

**Syntax**

```java Java theme={null}
collectEdges(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    resultSchema,
    resultTable,
    direction,
    [ resultIndexes [ , ... ] ],
    stmt
)
```

| **Argument**         | **Data Type**      | **Description**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| -------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `inputSchema`        | String             | A non-empty schema containing the input tables.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| `inputVerticesTable` | String             | Input vertices table (must have an `id` column).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| `inputEdgesTable`    | String             | Input edges table (must have the `srcid` and `destid` columns).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| `resultSchema`       | String             | A writable schema to create the result tables.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| `resultTable`        | String             | Name of the edge collection table (with the `id` and `edges` columns).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| `direction`          | EdgeDirection      | Specifies the direction of traversal. Supported values are:<br />`IN` — Neighbors that have edges pointing to the vertex (edges where `destid = id`).<br />For example, if an edge `5` points to `10`, then for `id=10`, neighbor `5` is included. <br /><br />`OUT` — Neighbors that the vertex points to (edges where `srcid = id`). For example, if an edge `5` points to `10`, then for `id=5`, neighbor `10` is included.<br /><br />`BOTH` — The union of `IN` and `OUT`. This traversal includes neighbors from edges pointing to `id` and edges from `id`. |
| `resultIndexes`      | ArrayList          | Optional. Columns to index. Specify an empty list for none.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
| `stmt`               | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |

**Example**
Collect outgoing edges for each vertex and generate an index for the `id` column. The example sets the `direction` to `OUT` to collect edges from `id`.

```java Java theme={null}
OCGraph.collectEdges(
    "sales", "customers", "purchases",
    "sales", "outgoing_edges",
    OCGraph.EdgeDirection.OUT,
    new ArrayList<>(List.of("id")),
    stmt
);
```

## Algorithms

High-level graph algorithms that iterate over the graph structure to produce labels, components, or counts.

### labelPropagation

Executes the [Label Propagation Algorithm](https://en.wikipedia.org/wiki/Label_propagation_algorithm) (LPA) to assign community labels to vertices.

Each vertex starts with its own identifier as its label. For a number set by the `maxIterations` argument, each vertex updates its label to the most frequent label among its neighbors. The algorithm determines ties by choosing the smallest label. The algorithm uses temporary tables for intermediate results and drops these tables when the process completes or if it fails. Isolated vertices retain their initial label. The final table stores `id` and `label` columns and can include indexes.

**Syntax**

```java Java theme={null}
labelPropagation(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    resultSchema,
    resultVerticesTable,
    maxIterations,
    [ resultVerticesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**            | **Data Type**      | **Description**                                                                                       |
| ----------------------- | ------------------ | ----------------------------------------------------------------------------------------------------- |
| `inputSchema`           | String             | A non-empty schema containing the input tables.                                                       |
| `inputVerticesTable`    | String             | Input vertices table (must have an `id` column).                                                      |
| `inputEdgesTable`       | String             | Input edges table (must have the `srcid` and `destid` columns).                                       |
| `resultSchema`          | String             | A writable schema to create the result tables.                                                        |
| `resultVerticesTable`   | String             | Name of the vertices table to create (columns include `id` and `label`).                              |
| `maxIterations`         | numeric            | Maximum number of iterations (must be `1` or greater).                                                |
| `resultVerticesIndexes` | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none. |
| `stmt`                  | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.          |

**Example**
Run label propagation for 10 iterations and assign labels to vertices. Generate an index on the `id` column.

```java Java theme={null}
OCGraph.labelPropagation(
    "sales", "customers", "purchases",
    "sales", "lpa_labels",
    10,
    new ArrayList<>(List.of("id")),
    stmt
);
```

### connectedComponents

Identifies the connected components of an undirected graph. This algorithm configures a Pregel computation in which each vertex initially sets its component label equal to its own identifier `id`.

In each iteration, vertices send their component label to neighbors. Each vertex updates based on the aggregated minimum value of its current component label and any received values. The process repeats until no more updates occur.

The result table maps each vertex `id` to its final `component` label.

**Syntax**

```java Java theme={null}
connectedComponents(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    resultSchema,
    resultVerticesTable,
    [ resultVerticesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**            | **Data Type**      | **Description**                                                                                       |
| ----------------------- | ------------------ | ----------------------------------------------------------------------------------------------------- |
| `inputSchema`           | String             | A non-empty schema containing the input tables.                                                       |
| `inputVerticesTable`    | String             | Input vertices table (must have an `id` column).                                                      |
| `inputEdgesTable`       | String             | Input edges table (must have the `srcid` and `destid` columns).                                       |
| `resultSchema`          | String             | A writable schema to create the result tables.                                                        |
| `resultVerticesTable`   | String             | Name of the vertices table to create.                                                                 |
| `resultVerticesIndexes` | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none. |
| `stmt`                  | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.          |

**Example**
Compute connected components for a maximum of 20 iterations and generate an index on the `id` column.

```java Java theme={null}
OCGraph.connectedComponents(
    "sales", "customers", "purchases",
    "sales", "components",
    20,
    new ArrayList<>(List.of("id")),
    stmt
);
```

### stronglyConnectedComponents

Computes strongly connected components (SCC) in a directed graph. This method runs a recursive algorithm that partitions vertices into subsets where every vertex is reachable from other vertices in the same subset.

This method uses recursive partitioning. The algorithm selects a pivot (typically the minimum identifier `id`), computes its predecessor set (vertices that can reach the pivot), and its descendant sets (vertices reachable from the pivot). Then, the method identifies the SCC as their intersection, removes that SCC from the graph, and recurses on the remainder until all vertices have been assigned to an SCC. The output contains columns for the `id` and `component` identifiers (the minimum `id` in the SCC).

The method creates temporary tables in the result schema to store intermediate results. This method drops these tables when the computation completes or fails. The final result table contains two columns: `id` (vertex identifier) and `component` (the minimum vertex identifier in its SCC subset).

**Syntax**

```java Java theme={null}
stronglyConnectedComponents(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    resultSchema,
    resultVerticesTable,
    [ resultVerticesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**            | **Data Type**      | **Description**                                                                                       |
| ----------------------- | ------------------ | ----------------------------------------------------------------------------------------------------- |
| `inputSchema`           | String             | A non-empty schema containing the input tables.                                                       |
| `inputVerticesTable`    | String             | Input vertices table (must have an `id` column).                                                      |
| `inputEdgesTable`       | String             | Input edges table (must have the `srcid` and `destid` columns).                                       |
| `resultSchema`          | String             | A writable schema to create the result tables.                                                        |
| `resultVerticesTable`   | String             | Name of the vertices table to create (columns include `id` and `component`).                          |
| `resultVerticesIndexes` | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none. |
| `stmt`                  | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.          |

**Example**

Compute the SCC and generate an index on the `id` column.

```java Java theme={null}
OCGraph.stronglyConnectedComponents(
    "sales", "customers", "purchases",
    "sales", "scc",
    new ArrayList<>(List.of("id")),
    stmt
);
```

### TriangleCount

TriangleCount identifies all 3-cycles (triangles) in the graph and counts how many distinct triangles each vertex participates in.

The algorithm first builds a canonical, undirected edge set by ensuring `srcid < destid` and removing duplicates to prevent double-counting. If your input edges are already canonicalized and deduplicated, use `TriangleCount.runPreCanonicalized` to skip preprocessing for faster performance.

The method then counts triangles (`a`, `b`, `c`) where `a < b < c` by intersecting neighbor lists and aggregates per-vertex participation to produce a result table with the `id` and `triangle_count` columns.

`run` **syntax**

```java Java theme={null}
TriangleCount.run(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    resultSchema,
    resultVerticesTable,
    [ resultVerticesIndexes [ , ... ] ],
    stmt
)
```

`runPreCanonicalized` **syntax**

```java Java theme={null}
TriangleCount.runPreCanonicalized(
    inputSchema,
    inputVerticesTable,
    canonicalEdgesTable,
    resultSchema,
    resultVerticesTable,
    [ resultVerticesIndexes [ , ... ] ],
    stmt
)
```

| **Argument**            | **Data Type**      | **Description**                                                                                                                                                                                                   |
| ----------------------- | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`           | String             | A non-empty schema containing the input tables.                                                                                                                                                                   |
| `inputVerticesTable`    | String             | Input vertices table (must have an `id` column).                                                                                                                                                                  |
| `inputEdgesTable`       | String             | Input edges table (must have the `srcid` and `destid` columns).<br /><br />For `runPreCanonicalized `execution, the method assumes this table is already canonicalized (e.g., `srcid < destid`) and deduplicated. |
| `resultSchema`          | String             | A writable schema to create the result tables.                                                                                                                                                                    |
| `resultVerticesTable`   | String             | Name of the vertices table to create (columns include `id` and `triangle_count`).                                                                                                                                 |
| `resultVerticesIndexes` | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none.                                                                                                             |
| `stmt`                  | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                                                                                                                      |

**Examples**

**Count Triangles Using** `run`

Canonicalize the raw edges internally, count unique triangles, and write per-vertex triangle counts with an index on the `id` column.

```java Java theme={null}
OCGraph.TriangleCount.run(
    "sales",
    "customers",
    "purchases",
    "sales",
    "triangle_counts",
    new ArrayList<>(List.of("id")),
    stmt
);
```

**Count Triangles Using** `runPreCanonicalized`

Use a pre-canonicalized, deduplicated edge table to count triangles and write per-vertex triangle counts with an index on the `id` column.

```java Java theme={null}
OCGraph.TriangleCount.runPreCanonicalized(
    "sales",
    "customers",
    "purchases_canonical",
    "sales",
    "triangle_counts",
    new ArrayList<>(List.of("id")),
    stmt
);
```

### pregel

Provides a generic vertex‑centered iteration framework for custom graph algorithms, similar to the [Pregel model](https://research.google/pubs/pregel-a-system-for-large-scale-graph-processing/).

Each iteration updates vertex states by sending messages along edges and then aggregating these messages to compute new states. The algorithm continues iterating until it reaches convergence (no state changed or no messages produced) or a specified iteration cap.

The algorithm uses multiple specified SQL expressions.

**Syntax**

```java Java theme={null}
pregel(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    resultSchema,
    resultVerticesTable,
    initializerExpr,
    [ sendToSourceExpr ],
    [ sendToDestExpr ],
    aggregateExpr,
    updaterExpr,
    maxIterations,
    [ resultVerticesIndexes [ , ... ] ],
   stmt
)
```

| **Argument**            | **Data Type**      | **Description**                                                                                                                                                                                                                                                                                                                                |
| ----------------------- | ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`           | String             | A non-empty schema containing the input tables.                                                                                                                                                                                                                                                                                                |
| `inputVerticesTable`    | String             | Input vertices table (must have an `id` column).                                                                                                                                                                                                                                                                                               |
| `inputEdgesTable`       | String             | Input edges table (must have the `srcid` and `destid` columns).                                                                                                                                                                                                                                                                                |
| `resultSchema`          | String             | A writable schema to create the result tables.                                                                                                                                                                                                                                                                                                 |
| `resultVerticesTable`   | String             | Name of the vertices table to create (columns include `id` and `result`).                                                                                                                                                                                                                                                                      |
| `initializerExpr`       | String             | A SQL expression to compute the initial state for each vertex (e.g., `CASE WHEN type = 'seed' THEN 1.0 ELSE 0.0 END`).                                                                                                                                                                                                                         |
| `sendToSourceExpr`      | String             | Optional. Defines the message sent to the source vertex of an edge, referencing the current vertex states `a.state` (source) and `c.state` (destination), and any edge attributes in `b`.<br />Example: `CASE WHEN a.country = c.country THEN 1 ELSE 0 END`                                                                                    |
| `sendToDestExpr`        | String             | Optional. Defines the message sent to the destination vertex of an edge, referencing the current vertex states `a.state` (source) and `c.state` (destination), and any edge attributes in `b`. <br />Example: `CASE WHEN a.country = c.country THEN 1 ELSE 0 END`                                                                              |
| `aggregateExpr`         | String             | A SQL aggregation to combine messages per vertex (e.g., `SUM(msg)`).<br />For a list of supported aggregations, see [Aggregate Functions](/aggregate-functions).                                                                                                                                                                               |
| `updaterExpr`           | String             | A SQL expression to compute the next state from the current state and aggregated messages. <br /><br />For example, this code updates the state to the minimum current state and the aggregated message (similar to the [connectedComponent](#connectedcomponents) method): `LEAST(a.state, COALESCE(m.aggregated_message, a.state)) AS state` |
| `maxIterations`         | numeric            | Maximum number of iterations (must be either `-1` or a value of `1` or greater). <br /><br />If this value is `-1`, the Pregel algorithm has no limit, and it runs until convergence. <br /><br />A value of `0` causes the algorithm to return an error.                                                                                      |
| `resultVerticesIndexes` | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none.                                                                                                                                                                                                                                          |
| `stmt`                  | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                                                                                                                                                                                                                                                   |

**Example**

Run a simple Pregel computation summing incoming edge amounts into the vertex state for 10 iterations at most, and generate an index on the `id` column.

```java Java theme={null}
OCGraph.pregel(
    "sales", "customers", "purchases",
    "sales", "pregel_result",
    "0 AS state",
    "b.amount",
    null,
    "SUM(msg) AS aggregated_message",
    "state + COALESCE(aggregated_message, 0) AS state",
    10,
    new ArrayList<>(List.of("id")),
    stmt
);
```

## Paths and Ranking

These methods include the shortest-path and PageRank algorithms.

### shortestPaths

Computes the shortest distance from every vertex to each set of landmark vertices using an iterative relaxation algorithm. The algorithm resembles [Bellman–Ford](https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm) but simultaneously handles multiple destinations.

Each landmark starts at distance `0` and all others at positive infinity. On each iteration, the algorithm examines every edge and checks whether traveling through the connected neighbor would yield a shorter route to a landmark. If a shorter route exists, the algorithm updates the distance of the source vertex. The process stops when no distances improve, or the algorithm reaches the maximum number of iterations.

After the process finishes, the algorithm writes a result table with the `srcid`, `destid`, and `distance` columns.

**Syntax**

```java Java theme={null}
shortestPaths(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    resultSchema,
    resultTable,
    landmarks [ , ... ],
    [ edgeWeightColumn ],
    maxIterations,
    [ resultIndexes [ , ... ] ],
    stmt
)
```

| **Argument**         | **Data Type**      | **Description**                                                                                                                                     |
| -------------------- | ------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`        | String             | A non-empty schema containing the input tables.                                                                                                     |
| `inputVerticesTable` | String             | Input vertices table (must have an `id` column).                                                                                                    |
| `inputEdgesTable`    | String             | Input edges table (must have the `srcid` and `destid` columns).                                                                                     |
| `resultSchema`       | String             | A writable schema to create the result table.                                                                                                       |
| `resultTable`        | String             | Name of the distances result table. This table has the `srcid`, `destid`, and `distance` columns.                                                   |
| `landmarks`          | ArrayList          | One or more landmark vertex identifiers. This list must be non-empty and contain no NULLs.                                                          |
| `edgeWeightColumn`   | String             | Optional. The name of an edge weight column. <br />If you do not specify this argument, the default value is `1.0`.                                 |
| `maxIterations`      | numeric            | Maximum number of relaxation iterations. This value must be `1` or greater.                                                                         |
| `resultIndexes`      | ArrayList          | Optional. The list of columns to index in the result table (`srcid`, `destid`, and `distance` columns). <br /><br />Specify an empty list for none. |
| `stmt`               | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                                                        |

**Example**

Compute distances from landmarks and generate indexes on the `src` and `dest` columns.

```java Java theme={null}
OCGraph.shortestPaths(
    "sales", "customers", "purchases",
    "sales", "distances",
    new ArrayList<>(List.of(1L, 42L)),
    null, // unweighted
    10,
    new ArrayList<>(List.of("srcid","destid")),
    stmt
);
```

### staticPageRank

Computes [PageRank](https://en.wikipedia.org/wiki/PageRank) scores for each vertex over a fixed number of iterations.

The algorithm follows the standard PageRank formula with a reset probability (`resetProb`) and uses common table expressions to calculate contributions from incoming edges and redistribute rank from dangling nodes.

The algorithm supports two variants:

* Standard PageRank — All vertices start with rank `1.0/N`, where `N` is the number of vertices. Specify this variant if `personalizationSrcId` is `null`.
* Personalized PageRank — The specified vertex starts with a rank of `1.0`, while others start with a rank of `0.0`. Specify this variant if `personalizationSrcId` is a vertex identifier.

After running PageRank for a fixed number of iterations, the method writes a result vertices table containing all original vertex columns with a new PageRank scoring column.

**Syntax**

```java Java theme={null}
staticPageRank(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    resultSchema,
    resultVerticesTable,
    numIterations,
    resetProb,
    [ resultVerticesIndexes [ , ... ] ],
    [ personalizationSrcId ],
    stmt
)
```

| **Argument**            | **Data Type**      | **Description**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| ----------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`           | String             | A non-empty schema containing the input tables.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| `inputVerticesTable`    | String             | Input vertices table (must have an `id` column).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| `inputEdgesTable`       | String             | Input edges table (must have the `srcid` and `destid` columns).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| `resultSchema`          | String             | A writable schema to create the result tables.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| `resultVerticesTable`   | String             | Name of the vertices table to create (columns include all vertex columns and a `pagerank` column).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| `numIterations`         | numeric            | Number of iterations to run. This value must be 1 or greater.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| `resetProb`             | numeric            | A value between `0.0` and `1.0` that controls the damping factor for how PageRank random surfer moves across vertices. <br /><br />A high value (e.g., `0.85`) puts more emphasis on link structure, encouraging the algorithm to move from each vertex to a neighbor. This value means PageRank scores tend to concentrate around well-linked regions.<br /><br />A lower score (e.g., `0.50`) allows the algorithm to ignore edges and instead jump to a vertex chosen from a base distribution. For standard PageRank, this base is uniform over all vertices. For personalized PageRank, the base is biased toward the specified vertex. This behavior creates more uniform scoring with less sensitivity to link topology. |
| `resultVerticesIndexes` | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `personalizationSrcId`  | Long               | Determines whether PageRank uses the standard or the personalized variant.  <br /><br />For personalized mode, specify a vertex identifier.  PageRank scoring starts with this vertex set at `1.0`, while all other vertices start at `0.0`. <br /><br />For standard mode, specify `null`. All vertices start with rank `1.0/N`, where `N` is the number of vertices.                                                                                                                                                                                                                                                                                                                                                          |
| `stmt`                  | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |

**Example**

Run fixed-iteration PageRank and generate an index on the `id` column. This example uses a high reset probability `resetProb` of `0.85` to ensure the ranking concentrates on highly linked regions.

```java Java theme={null}
OCGraph.staticPageRank(
    "sales", "customers", "purchases",
    "sales", "pagerank_static",
    10,
    0.85,
    new ArrayList<>(List.of("id")),
    null,
    stmt
);
```

### dynamicPageRank

Computes PageRank scores until convergence based on a specified threshold value (`tolerance`). Unlike the [staticPageRank](#staticpagerank) method, this algorithm runs iterations until the sum of absolute differences between ranks in successive iterations is less than or equal to the `tolerance` value. The algorithm handles personalization similarly to `staticPageRank`. At each iteration, the method uses the PageRank formula, collects rank values, and redistributes them.

The algorithm supports two variants:

* Standard PageRank — All vertices start with rank `1.0/N`, where `N` is the number of vertices. Specify this variant if `personalizationSrcId` is `null`.
* Personalized PageRank — The specified vertex starts with a rank of `1.0`, while others start with a rank of `0.0`. Specify this variant if `personalizationSrcId` is a vertex identifier.

After running PageRank until the system reaches the `tolerance` threshold, the method writes a vertices table containing all the original vertex columns with a new PageRank scoring column.

**Syntax**

```java Java theme={null}
dynamicPageRank(
    inputSchema,
    inputVerticesTable,
    inputEdgesTable,
    resultSchema,
    resultVerticesTable,
    tolerance,
    resetProb,
    [ resultVerticesIndexes [ , ... ] ],
    [ personalizationSrcId ],
    stmt
)
```

| **Argument**            | **Data Type**      | **Description**                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| ----------------------- | ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `inputSchema`           | String             | A non-empty schema containing the input tables.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| `inputVerticesTable`    | String             | Input vertices table (must have an `id` column).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| `inputEdgesTable`       | String             | Input edges table (must have the `srcid` and `destid` columns).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| `resultSchema`          | String             | A writable schema to create the result tables.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `resultVerticesTable`   | String             | Name of the vertices table to create (columns include all vertex columns and `pagerank`).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| `tolerance`             | numeric            | The convergence threshold that stops iterations when the rank changes become negligible. <br /><br />After each iteration, the algorithm measures the maximum change in any rank of a vertex. If that change is below the specified tolerance, the algorithm considers the computation converged and stops early. <br /><br />A high tolerance value (e.g., `0.001`) is good for quick exploratory runs that generate an approximate ranking. <br /><br />A low tolerance value (e.g., `0.000001`) generates a precise ranking, but requires a higher compute cost.                                                                                                                                                                      |
| `resetProb`             | numeric            | A value between `0.0` and `1.0` that controls the damping factor for how the PageRank random surfer moves across vertices. <br /><br />A high value (e.g., `0.85`) puts more emphasis on link structure, encouraging the algorithm to move from each vertex to a neighbor.  The high value means PageRank scores tend to concentrate around well-linked regions.<br /><br />A lower score (e.g., `0.50`) allows the algorithm to ignore edges and instead jump to a vertex chosen from a base distribution. For standard PageRank, this base is uniform over all vertices. For personalized PageRank, the base is biased toward the specified vertex. This behavior creates more uniform scoring with less sensitivity to link topology. |
| `resultVerticesIndexes` | ArrayList          | Optional. Columns to index in the result vertices table (e.g., `id`). Specify an empty list for none.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
| `personalizationSrcId`  | Long               | Determines whether PageRank uses the standard or the personalized variant.  <br /><br />For personalized mode, specify a vertex identifier. PageRank scoring starts with this vertex set at `1.0`, while all other vertices start at `0.0`. <br /><br />For standard mode, specify `null`. All vertices start with rank `1.0/N`, where `N` is the number of vertices.                                                                                                                                                                                                                                                                                                                                                                    |
| `stmt`                  | java.sql.Statement | JDBC statement for executing SQL. You must have a valid statement and a database connection.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |

**Example**

Run dynamic PageRank to convergence. This example uses a low `tolerance` value of `1.0e-6`, which generates high-precision rankings but requires more computing resources.

```java Java theme={null}
OCGraph.dynamicPageRank(
    "sales", "customers", "purchases",
    "sales", "pagerank_dynamic",
    1.0e-6,
    0.85,
    new ArrayList<>(List.of("id")),
    null,
    stmt
);
```

## Bibliography

Pregel: A System for Large-Scale Graph Processing.” Accessed November 18, 2025. [https://research.google/pubs/pregel-a-system-for-large-scale-graph-processing/](https://research.google/pubs/pregel-a-system-for-large-scale-graph-processing/).

## Related Links

[JDBC Manual](/jdbc-manual)

[OCGraph Python Library](/ocgraph-python-library)
