Skip to content

Ursa

Graph analytics in SQL.

Ursa is a SQL engine for graphs. Point the ursa command-line tool at an edge list in Parquet, CSV or JSON, on your disk or in object storage, name it as a graph, and run PageRank, Louvain, shortest paths and the rest as table functions. It works like DuckDB: one binary, no server, and a database file that keeps your work. A Rust engine runs it: Apache Arrow for the data, DataFusion for the plan, and parallel, deterministic kernels for the algorithms.

The same engine is also a Python library, with a Polars-shaped API: see From Python below.

Try it in your browser

Ursa Shell is the ursa shell in a web page, with nothing to install. It runs the same engine, compiled to WebAssembly, in your browser. Pick a dataset from its menu, or import a file of your own: it stays on your machine.

Install

On Linux or macOS:

curl -fsSL https://cldixon.github.io/ursa/install.sh | sh

On Windows, in PowerShell: irm https://cldixon.github.io/ursa/install.ps1 | iex. Or cargo install ursa-cli. Getting started has the details.

First query

Download Zachary's karate club, 78 friendships between 34 members, and start the shell:

curl -LO https://raw.githubusercontent.com/cldixon/ursa/main/python/ursa/datasets/data/karate_edges.csv
ursa

A file is a table. Name it as a graph, then run an algorithm over it:

CREATE PROPERTY GRAPH club
  EDGE TABLES ('karate_edges.csv' SOURCE KEY (src) DESTINATION KEY (dst));

SELECT id, round(pagerank, 4) AS pagerank
FROM pagerank(club, direction => 'both')
ORDER BY pagerank DESC
LIMIT 5;
┌───────┬──────────┐
│  id   │ pagerank │
│ Int64 │ Float64  │
├───────┼──────────┤
│    33 │   0.1009 │
│     0 │    0.097 │
│    32 │   0.0717 │
│     2 │   0.0571 │
│     1 │   0.0529 │
└───────┴──────────┘

Getting started goes on from here: node attributes, several algorithms at once, weights, subgraphs, traversals and GRAPH_TABLE patterns. Ursa SQL is the overview.

From Python

pip install ursa-graph

The package is ursa-graph. The import name is ursa. Wheels include the compiled core, so you need no Rust toolchain. Python 3.10 or later. See Install for the optional extras.

There is no Graph object. An edge table is the graph. Every operation returns a table. Paste this into a Python session. It needs no files and no network.

import ursa as ur

edges = ur.datasets.load_karate()   # 34 nodes, 78 edges, bundled with the package

top = (
    edges.nodes()
    .with_columns(
        degree=ur.degree(edges, direction="both"),
        triangles=ur.triangle_count(edges),
        community=ur.louvain(edges, seed=1),
    )
    .sort("degree", descending=True)
    .head(5)
    .collect()
)
print(top)
shape: (5, 4)
┌───────┬────────┬───────────┬───────────┐
│ id    ┆ degree ┆ triangles ┆ community │
│ ---   ┆ ---    ┆ ---       ┆ ---       │
│ int64 ┆ uint32 ┆ uint32    ┆ uint32    │
╞═══════╪════════╪═══════════╪═══════════╡
│ 33    ┆ 17     ┆ 15        ┆ 2         │
│ 0     ┆ 16     ┆ 18        ┆ 0         │
│ 32    ┆ 12     ┆ 13        ┆ 2         │
│ 2     ┆ 10     ┆ 11        ┆ 0         │
│ 1     ┆ 9      ┆ 12        ┆ 0         │
└───────┴────────┴───────────┴───────────┘

What happened:

  1. edges.nodes() starts a lazy table of the node ids.
  2. with_columns(...) adds three columns. Each is a graph algorithm over edges.
  3. sort(...) and head(5) shape the rows.
  4. collect() runs all of it as one query and returns Arrow data.

To run the same query over your own data, change the first line:

edges = ur.scan_edges("edges.parquet", src="from_id", dst="to_id")

Nothing after that line changes.

What you get

  • SQL, like DuckDB


    Files are tables, a graph is an edge table, and algorithms are table functions you join on id. An interactive shell, ursa -c for scripts, and output as tables, CSV, JSON or Arrow.

    Ursa SQL

  • A dataframe API


    In Python: with_columns, filter, sort, group_by, join. If you know Polars, you know the shape. Lazy until collect().

    Concepts

  • The standard algorithms


    Degree, PageRank, components, triangles, clustering, closeness, betweenness, Louvain, label propagation. Each with a weighted form. Hops, shortest paths, random walks.

    Compute graph metrics

  • Your data, where it is


    Parquet and CSV, local or on S3, GCS, and Azure. Polars, pandas, pyarrow, NetworkX, NumPy, and SciPy in memory. Arrow in, Arrow out, no copies.

    Load data

  • Results you can check


    Every kernel is deterministic on any thread count. Each one is pinned to the NetworkX call it matches, and the test suite checks it.

    Semantics vs NetworkX

Next steps

The ursa CLI is version 0.9 and the Python library 0.5; both are in active development. The Limits page lists what does not run yet.