OQGRAPH examples: paths, reachability, leaves

Runnable OQGRAPH patterns: backing table, Dijkstra, BFS, leaves, weights.

OQGRAPH runs graph algorithms over an edge table you maintain. Mutations hit the backing table. Algorithms are selected with latch.

Schema

CREATE TABLE oq_backing (
  origid INT UNSIGNED NOT NULL,
  destid INT UNSIGNED NOT NULL,
  PRIMARY KEY (origid, destid),
  KEY (destid)
);

INSERT INTO oq_backing(origid, destid) VALUES
  (1,2),(2,3),(3,4),(4,5),(2,6),(5,6);

CREATE TABLE oq_graph (
  latch VARCHAR(32) NULL,
  origid BIGINT UNSIGNED NULL,
  destid BIGINT UNSIGNED NULL,
  weight DOUBLE NULL,
  seq BIGINT UNSIGNED NULL,
  linkid BIGINT UNSIGNED NULL,
  KEY (latch, origid, destid) USING HASH,
  KEY (latch, destid, origid) USING HASH
) ENGINE=OQGRAPH
  data_table='oq_backing' origid='origid' destid='destid';

Column layout must match engine expectations; wrong latch types fail create options.

Shortest path

SELECT GROUP_CONCAT(linkid ORDER BY seq) AS path
FROM oq_graph
WHERE latch='dijkstras' AND origid=1 AND destid=6;
-- 1,2,6

Edges are directed. Empty set for 6→1 unless reverse edges exist.

Reachability

SELECT GROUP_CONCAT(linkid) AS reachable
FROM oq_graph
WHERE latch='dijkstras' AND origid=2;

SELECT GROUP_CONCAT(linkid) AS bfs
FROM oq_graph
WHERE latch='breadth_first' AND origid=2;

Leaves

SELECT linkid, weight FROM oq_graph
WHERE latch='leaves' AND origid=2;

Terminal nodes in the reachable subgraph (incoming-only from that walk’s perspective).

Weights

Add weight DOUBLE NOT NULL on the backing table and weight='weight' in engine options. Dijkstra minimises cost; a high-weight direct edge can lose to a longer cheap path.

Full latch table: /graph/doc.