Home / Market / spanner relational to graph

spanner relational to graph

Analyzes existing Spanner relational schemas on Google Cloud (DDL, foreign keys, interleaved tables, and junction tables) and generates `CREATE PROPERTY GRAPH` DDL statements (`NODE TABLES`, `EDGE TABLES`) without modifying underlying relational tables. Use when mapping existing Spanner tables to a Spanner Graph, identifying node and edge candidates from relational tables, creating SQL bridge views for implicit `ARRAY`/`JSON` or type-mismatched relationships, mapping polymorphic or self-referent

skill★ 255720 dlgithubApache-2.0Install in CosmosUpstream Source

Documentation & Preview

--- name: spanner-relational-to-graph metadata: version: "1.0.0" category: Databases description: >- Analyzes existing Spanner relational schemas on Google Cloud (DDL, foreign keys, interleaved tables, and junction tables) and generates CREATE PROPERTY GRAPH DDL statements (NODE TABLES, EDGE TABLES) without modifying underlying relational tables. Use when mapping existing Spanner tables to a Spanner Graph, identifying node and edge candidates from relational tables, creating SQL bridge views for implicit ARRAY/JSON or type-mismatched relationships, mapping polymorphic or self-referential tables, or troubleshooting CREATE PROPERTY GRAPH DDL compiler errors. Don't use for greenfield physical table design (use spanner-graph-schema-best-practices) or tuning slow ISO GQL queries (use spanner-graph-query-tuning).

---

Spanner Relational-to-Property-Graph Schema Conversion

Convert an existing Spanner relational schema into a valid, production-ready CREATE OR REPLACE PROPERTY GRAPH DDL definition while keeping existing relational tables intact.

Usage Attribution

MUST READ: Before executing any gcloud, REST API, or MCP command, you MUST read and follow the instructions in Usage Attribution.

Prerequisites & Cloud Environment Setup

When inspecting a live Spanner instance or verifying DDL (see spanner-basics for general Spanner instance provisioning and MCP setup):

  1. Google Cloud SDK (gcloud): Ensure the CLI is installed (see

Install the Google Cloud SDK).

  1. Project, Billing & API: Verify an active Google Cloud project with

billing enabled (required even when provisioning a free trial instance) is configured (gcloud config set project {project_id}) and, after the user confirms, enable the Spanner API (gcloud services enable spanner.googleapis.com). Ask the user before changing the active project, billing, or enabled APIs, or creating or deleting instances, because these commands change the user's Google Cloud environment and can incur costs.

  1. Authentication & IAM Roles: Authenticate via gcloud auth login or

gcloud auth application-default login (or GOOGLE_APPLICATION_CREDENTIALS for service accounts). Listing instances/databases requires roles/spanner.viewer, inspecting DDL requires roles/spanner.databaseReader, and applying CREATE VIEW / CREATE PROPERTY GRAPH DDL requires roles/spanner.databaseAdmin.

  1. Instance Region & Pricing: For sandbox test instances, default to

regional-us-central1 unless specified otherwise (see Spanner instance configurations, Google Cloud locations, Spanner pricing, and Google Cloud pricing).

---

Core Principles

  1. Zero Data Duplication: Spanner Graph maps existing relational tables or

SQL views directly into graph nodes and edges via CREATE PROPERTY GRAPH metadata—without copying data or breaking existing SQL applications.

  1. Preserve Existing Tables: Treat all existing relational tables as

read-only inputs. Do not drop, rename, or alter existing columns or primary keys when adding a property graph view. If an implicit relationship requires key transformation (for example, unnesting an array or casting mismatched key types), define a new SQL bridge view (CREATE VIEW ... SQL SECURITY INVOKER AS ... by default, or SQL SECURITY DEFINER when row-level access control is needed) and bind the edge or node to that view.

  1. 100% Table Accounting: Account for every relational table in the input

schema—either mapped into NODE TABLES / EDGE TABLES or explicitly listed as excluded (such as pure system audit logs, schema migration state tables, or unlinked lookup tables).

---

Step-by-Step Conversion Workflow

Step 1: Discover or Parse the Relational Schema

CREATE TABLE, PRIMARY KEY, INTERLEAVE IN PARENT, and FOREIGN KEY definitions directly.

Cloud Console URL containing instances/{instance_id}/databases/{database_id}?project={project_id}), extract project, instance, and database and retrieve the live DDL using the read-only gcloud command:

  gcloud spanner databases ddl describe {database_id} \
    --instance={instance_id} \
    --project={project_id}

read-only and safe to run automatically, whereas applying CREATE VIEW or CREATE OR REPLACE PROPERTY GRAPH via gcloud spanner databases ddl update mutates the live database—always ask for explicit user confirmation before running gcloud spanner databases ddl update (and state this confirmation requirement clearly in your response even if ddl describe fails due to missing credentials or network access).

for environment discovery, emulator setup, and verification workflows.

Step 2: Classify Relational Tables into Nodes and Edges

Apply the structural classification rules in references/patterns.md:

  1. Core Entity Tables -> NODE TABLES:

Devices, Documents) become node tables.

  1. Pure or Attributed M:N Junction Tables -> EDGE TABLES:

ProjectAssignments(emp_id, project_id) or WireTransfers(transfer_id) with src_account_id and dst_account_id) become edge tables.

example, Orders is a child of Customers and a parent of OrderItems), map Orders as both a NODE TABLE (Orders AS OrderNode) and an EDGE TABLE (Orders AS PlacedOrder).

  1. **1:N Foreign Keys and Interleaved Parent-Child Tables -> EDGE TABLES

(Table AS Edge)**:

referencing A can simultaneously act as a NODE TABLE (B AS NodeB) and an EDGE TABLE (B AS EdgeBA) connecting B to A (or A to B).

Employees(emp_id, manager_id) maps to Employees AS Employee in NODE TABLES and Employees AS ReportsToEdge in EDGE TABLES (SOURCE KEY (emp_id) REFERENCES Employee(emp_id) and DESTINATION KEY (manager_id) REFERENCES Employee(emp_id)).

  1. Implicit Relationships (Arrays, JSON, Polymorphic Keys) -> Bridge Views:

type-mismatched keys (INT64 vs. STRING), create a SQL bridge view that flattens and projects (edge_key, src_key, dst_key) and register the view in EDGE TABLES with an explicit KEY (...).

SQL SECURITY clause. SQL SECURITY INVOKER is the common default when callers already hold permissions on the underlying tables, whereas SQL SECURITY DEFINER is supported when you need row-level / fine-grained access control so callers can query the graph view without direct base-table grants.

Spanner syntactically verifies that a view's declared KEY is unique (either a single base table's primary key or matched by SELECT DISTINCT / GROUP BY), failing otherwise with cannot verify the specified element key ... is unique. Use SELECT DISTINCT (or GROUP BY the key columns) when inputs can contain duplicate keys (such as duplicate elements inside an ARRAY); if you know the view's KEY is already semantically unique (for example, a key-preserving JOIN or UNNEST(...) WITH OFFSET), you can instead append OPTIONS (validate_element_key_uniqueness = false) to CREATE PROPERTY GRAPH to skip syntactic validation without extra DISTINCT / GROUP BY overhead (you remain responsible for ensuring keys are unique in data).

Step 3: Decide Graph Scoping (Unified vs. Multi-Graph)

completely disconnected business domains or exceeds ~12+ distinct node/edge types where domain-specific subgraphs improve query ergonomics.

Step 4: Author the CREATE OR REPLACE PROPERTY GRAPH DDL

Follow the exact syntax and structural invariants in references/spanner-graph-ddl-syntax.md and references/structural-invariants.md:

CREATE OR REPLACE PROPERTY GRAPH GraphName
  NODE TABLES (
    TableName AS NodeAlias
      KEY (pk_col1, pk_col2)
      LABEL NodeLabel
  )
  EDGE TABLES (
    EdgeTableName AS EdgeAlias
      KEY (edge_pk_col1, edge_pk_col2)
      SOURCE KEY (src_fk_col1) REFERENCES SourceNodeAlias(src_pk_col1)
      DESTINATION KEY (dst_fk_col1) REFERENCES DestNodeAlias(dst_pk_col1)
      LABEL EdgeLabel
  );

Critical DDL Invariants Checklist

entire CREATE PROPERTY GRAPH statement (if a table is used as both a node and an edge, give them distinct aliases, e.g., Orders AS OrderNode and Orders AS PlacedOrderEdge).

DESTINATION KEY)**:

(for example, KEY (transfer_id) or KEY (src_id, dst_id, transfer_ts)), while SOURCE KEY and DESTINATION KEY specify the endpoints. Spanner enforces uniqueness: on a table, a custom KEY must contain all primary key columns or match a UNIQUE index (so KEY (src_id, dst_id, transfer_ts) on a table whose primary key is transfer_id fails unless such an index exists). Rows where any KEY, SOURCE KEY, or DESTINATION KEY column is NULL are silently skipped by MATCH.

DESTINATION KEY (...) REFERENCES NodeAlias(...), the target after REFERENCES must be the NodeAlias (not the underlying relational table name if an alias was used, and not the LABEL name), and the column count and data types must match the referenced node's KEY 1-to-1. Spanner does not always enforce the count: with an explicit column list it accepts a partial composite key, and each edge then silently matches every node sharing that value.

NO PROPERTIES)**:

PROPERTIES ALL COLUMNS) exposes all columns of the input table and is concise when a table has its own label and non-conflicting column types.

PROPERTIES ALL COLUMNS EXCEPT (...), or NO PROPERTIES in four relational-conversion cases: (1) cross-table property type conflicts (aliasing colliding column names), (2) shared LABELs across tables, (3) dual Table AS Node + Table AS Edge or pure junction mappings (NO PROPERTIES or