Skip to content
Shell

Files and storage

Ursa reads files where they are: a file is a table. This page covers reading and writing files, files in S3, GCS and Azure, the formats ursa prints, and the .ursa database file that keeps your work between sessions.

Name a file in FROM, with its path or URL in quotes. A relative path is relative to the current directory. The format comes from the extension:

Extension Format
.parquet Parquet
.csv CSV, with a header line
.json, .jsonl, .ndjson JSON, one object per line (NDJSON)
.arrow Arrow IPC
SELECT count(*) AS ties FROM 'karate_edges.csv';
┌───────┐
│ ties │
│ Int64 │
├───────┤
│ 78 │
└───────┘

CSV and JSON files may be compressed: .csv.gz, .jsonl.zst, and .bz2 and .xz too. A directory or a glob ('edges/*.parquet') reads every file in it, and a year=2024/ directory becomes a year column. To use a file in several queries, name it as a view:

CREATE VIEW ties AS SELECT * FROM 'karate_edges.csv';

A view reads the file each time a query uses it, so it always sees the file’s current contents.

read_parquet, read_csv and read_json read a file in the format they name, whatever its extension, with options, as in DuckDB. They take a path, a glob, a directory, a URL, or a list of them:

SELECT count(*) AS ties FROM read_csv('karate_edges.csv', header => true, delim => ',');
┌───────┐
│ ties │
│ Int64 │
├───────┤
│ 78 │
└───────┘
Function Options
read_parquet none
read_csv header (true or false), delim (one character), quote, compression
read_json compression; format: 'newline_delimited' (the default) or 'array', for a file that holds one JSON array of objects

compression is 'auto' (from the extension, the default), 'none', 'gzip', 'bzip2', 'xz' or 'zstd'. Options are named with =>, or with = as DuckDB writes them.

COPY … TO writes a query’s rows to a file, in the format its extension names, and prints how many rows it wrote:

COPY (SELECT * FROM ties) TO 'ties.jsonl';
┌────────┐
│ count │
│ UInt64 │
├────────┤
│ 78 │
└────────┘

STORED AS CSV (or PARQUET, JSON, ARROW) names the format when the extension does not. DuckDB’s option list works too: COPY … TO 'ties.txt' (FORMAT csv, HEADER false, DELIMITER '|').

In Ursa Shell in the browser, which has no disk, COPY … TO 'ties.parquet' keeps the file in the session: FROM 'ties.parquet' reads it and .files lists it, until the page is closed.

Anything that works with a local path works with an s3://, gs://, az:// or https:// URL:

CREATE VIEW follows AS
SELECT * FROM 's3://my-bucket/social/follows/*.parquet';
CREATE PROPERTY GRAPH social
EDGE TABLES (follows SOURCE KEY (follower) DESTINATION KEY (followee));
SELECT * FROM pagerank(social) ORDER BY pagerank DESC LIMIT 10;

Ursa reads only the columns a query needs, and stores the graph index in the database file, so the second query does not read the edges again.

Ursa finds credentials where each cloud’s own tools do, and uses the first place that has them. Nothing is stored in the database file.

S3, in this order:

  1. AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, plus AWS_SESSION_TOKEN for temporary credentials.
  2. An AWS profile in ~/.aws/credentials and ~/.aws/config: the one AWS_PROFILE names, or default. It can hold access keys, an SSO login (run aws sso login first), a role to assume, or a credential_process.
  3. Web identity (AWS_WEB_IDENTITY_TOKEN_FILE and AWS_ROLE_ARN, as on EKS), container credentials (as on ECS), or the EC2 instance’s role.
AWS_PROFILE=analytics ursa -c "SELECT count(*) FROM 's3://my-bucket/follows/*.parquet'"

The region comes from AWS_REGION or AWS_DEFAULT_REGION, or else Ursa asks S3 where the bucket is. For an S3-compatible store (MinIO, R2), set AWS_ENDPOINT. For a public bucket, set AWS_SKIP_SIGNATURE=true.

Google Cloud Storage: a service account key (GOOGLE_SERVICE_ACCOUNT, or its JSON in GOOGLE_SERVICE_ACCOUNT_KEY), application default credentials (gcloud auth application-default login), or the instance’s service account.

Azure Blob Storage: set AZURE_STORAGE_ACCOUNT_NAME, then an account key (AZURE_STORAGE_ACCOUNT_KEY), workload identity, a service principal, a SAS token, the Azure CLI’s login (AZURE_USE_AZURE_CLI=true), or a managed identity.

HTTPS URLs are read without credentials.

The shell prints tables. --format sets the format on the command line, and .mode in the shell or a script: table (the default), csv, tsv, json, ndjson, markdown, line and arrow. DuckDB’s names duckbox, box and jsonlines work too.

ursa club.ursa --format json -c "SELECT * FROM degree(karate) ORDER BY id LIMIT 2"
[{"id":0,"degree":16},{"id":1,"degree":8}]

A table shows at most 40 rows. The other formats write every row and never cut a value.

  • For a program, or an agent, read json or ndjson. Every row object has every column, with null for a null; a list is a JSON array and a struct a JSON object.
  • For files and spreadsheets, csv and tsv write as DuckDB does. .headers off leaves out the header row.
  • To paste into an issue or a document, markdown writes a Markdown table.
  • For a wide row, line writes one column = value line per column.
  • For another program, --format arrow writes an Arrow IPC stream. It works only with -c or -f, for one query that returns rows.

A script can hold dot-commands, one a line, as at the prompt:

.mode csv
SELECT count(*) AS edges FROM 'karate_edges.csv';
ursa -f q.sql
edges
78

Errors go to stderr, and so do notices (lines starting ursa: ), so stdout holds only results. With --format json or ndjson, an error is one line of JSON on stderr, for a program to read.

A .ursa file is a database. It keeps two things between sessions: the views, external tables and graphs you define, and the graph indexes Ursa builds from them. Name the file when you start ursa:

ursa club.ursa # the shell
ursa club.ursa -c "SHOW GRAPHS" # one command

The file is created if it does not exist, and its name must end in .ursa. Without a file, ursa runs in memory and keeps nothing. In an in-memory session that has not defined anything yet, ATTACH 'club.ursa' (or the shell’s .open) opens one.

The file stores statements, not rows. Opening the database runs them again, so a view over a file always reads the file’s current contents. DROP removes an object’s statement, and CREATE OR REPLACE replaces it. Statements that create tables with data, or change them, are refused in a database session.

The first query over a graph builds its index: the graph’s edges, laid out for the algorithms. Ursa writes it next to the database, and later queries, in this process or another, load it instead of building it again. An index is stored under a key made from the plan that produces the edges and the identity of every file it reads (its location, size and modification time). So:

  • if a source file changes, Ursa builds a new index;
  • a weight is not part of the key, so trying several weightings reuses one index;
  • a database moved together with its data still finds its indexes.

Edges that are not read from files, such as an in-memory table, have no stable key. Their index is built once per session and not stored.

One process at a time may have a database open for writing. A second one waits two seconds, then fails with an error that names the process that has it. --readonly opens a database without taking that lock, to query it meanwhile. It refuses any statement that defines or drops something:

ursa club.ursa --readonly -c "SELECT * FROM degree(karate) LIMIT 3"