SQL Interface Overview
RaisinDB provides a SQL interface for querying and manipulating data stored in the hierarchical document database. The SQL implementation extends standard SQL with specialized features for hierarchical paths, graph queries, JSON documents, vector search, and version control.
In RaisinDB, the workspace name is used as the table name in SQL queries. When you write SELECT * FROM products, products is the workspace name.
Supported SQL Features
Data Definition Language (DDL)
CREATE NODETYPE- Define node type schemasCREATE ARCHETYPE- Define archetype schemasCREATE ELEMENTTYPE- Define element type schemasALTERstatements for schema modificationsDROPstatements for schema removal
Branch Operations
CREATE BRANCH- Create a new branch (with options: FROM, AT REVISION, DESCRIPTION, PROTECTED, UPSTREAM, WITH HISTORY)DROP BRANCH- Delete a branchALTER BRANCH- Modify branch settings (RENAME, SET/UNSET UPSTREAM, SET PROTECTED)MERGE BRANCH ... INTO ...- Merge branches (with strategy selection)USE BRANCH/CHECKOUT BRANCH- Switch active branchUSE LOCAL BRANCH- Switch branch for current session onlyBEGIN/COMMIT- Transaction control with message and actor metadataSET validate_schema- Configure schema validation
Data Manipulation Language (DML)
SELECT- Query data with filtering, joins, aggregationINSERT- Add new nodesUPDATE- Modify existing nodesDELETE- Remove nodesEXPLAIN- Show query execution plan
Graph DML
RELATE- Create relationships between nodesUNRELATE- Remove relationshipsMOVE- Move nodes in hierarchyCOPY- Copy nodes (with optional recursion)ORDER- Order nodes within a parentRESTORE- Restore nodes from previous revisionsTRANSLATE- Multi-language translation of node content
Query Capabilities
- Filtering:
WHEREclauses with complex predicates - Joins: INNER, LEFT, RIGHT, FULL, CROSS
- Aggregation:
GROUP BYwith aggregate functions - Sorting:
ORDER BYwith multiple columns - Limiting:
LIMITandOFFSETfor pagination - Common Table Expressions:
WITHclause for CTEs - Window Functions:
OVERclause for analytical queries - Subqueries: Correlated and non-correlated subqueries
Special Features
Hierarchical Path Functions
RaisinDB provides specialized functions for working with hierarchical paths:
DEPTH(path)- Calculate hierarchy depthPARENT(path)- Get parent pathANCESTOR(path, depth)- Get ancestor at specific depthPATH_STARTS_WITH(path, prefix)- Check path prefixCHILD_OF(path, parent)- Check direct parent relationshipDESCENDANT_OF(path, ancestor)- Check descendant relationshipREFERENCES(target)- Check if node references targetNEIGHBORS(node_id, direction, relation_type)- Get neighboring nodes in the graph
Working with Properties
All node data is stored in a properties JSONB column. Use JSONB operators to access fields:
-- Extract as text
SELECT properties->>'title' AS title FROM default;
-- Extract as JSONB
SELECT properties->'tags' AS tags FROM default;
-- Nested access
SELECT properties->'author'->>'name' AS author_name FROM default;
-- Cast for comparisons
SELECT * FROM default WHERE (properties->>'price')::numeric > 100;
-- Containment check
SELECT * FROM default WHERE properties @> '{"featured": true}';
JSON Support
Native JSON operations using JSONPath expressions:
JSON_VALUE(json, path)- Extract scalar valuesJSON_QUERY(json, path)- Extract JSON objects/arraysJSON_EXISTS(json, path)- Check path existenceJSONB_SET(json, path, value)- Set values in JSON- JSON operators:
->,->>,@>,<@,?,#>,#>>,#-,@?
All of these operate on the properties JSONB column when querying nodes.
Full-Text Search
PostgreSQL-compatible full-text search:
TO_TSVECTOR([language,] text)- Create search documentTO_TSQUERY([language,] text)- Create search queryTS_RANK(tsvector, tsquery)- Calculate relevance scoreFULLTEXT_MATCH(query, language)- Match with language supportfulltext_search(workspace, query)- Search a workspace@@operator for matching
Vector Search
Embedding-based similarity search:
EMBEDDING(text)- Generate vector embeddingVECTOR_L2_DISTANCE(vec1, vec2)- Euclidean distanceVECTOR_COSINE_DISTANCE(vec1, vec2)- Cosine distanceVECTOR_INNER_PRODUCT(vec1, vec2)- Inner product- Operators:
<->(L2),<=>(cosine),<#>(inner product)
Geospatial Queries
PostGIS-compatible spatial operations:
ST_POINT(lon, lat)- Create point geometryST_DISTANCE(geom1, geom2)- Calculate distanceST_CONTAINS(geom1, geom2)- Check containmentST_INTERSECTS(geom1, geom2)- Check intersection
Graph Queries (SQL/PGQ)
SQL:2023 standard property graph queries using GRAPH_TABLE syntax:
- Pattern matching with node labels and relationship types
- Multi-label support:
(n:User|Admin) - Variable-length path patterns with quantifiers
- Inline WHERE predicates in node patterns
- Default graph name:
NODES_GRAPH
System Functions
VERSION(),CURRENT_SCHEMA(),CURRENT_DATABASE()CURRENT_USER(),SESSION_USER(),CURRENT_CATALOG()- RaisinDB-specific:
RAISIN_CURRENT_USER(),RAISIN_AUTH_*()functions
Numeric Functions
ROUND(number [, decimals])- Round to nearest integer or N decimal places
Node Columns
Every node has these columns:
| Column | Type | Description |
|---|---|---|
id | TEXT | Unique node identifier (ULID) |
node_type | TEXT | NodeType name |
path | TEXT | Hierarchical path |
workspace | TEXT | Workspace name |
properties | JSONB | All node properties |
created_at | TIMESTAMP | Creation time |
updated_at | TIMESTAMP | Last update time |
version | INTEGER | Version number |
All user-defined data lives in the properties JSONB column and is accessed with JSONB operators (properties->>'field').
System Aliases
The following double-underscore aliases are also available:
__id→id__path→path__node_type→node_type__created_at→created_at__updated_at→updated_at__revision→version__branch→ current branch name
Data Types
Supported SQL data types:
- Numeric: INT, BIGINT, DOUBLE (NUMERIC/DECIMAL are accepted but convert to DOUBLE internally)
- Text: TEXT, UUID
- Boolean: BOOLEAN
- Temporal: TIMESTAMPTZ, INTERVAL
- Specialized: PATH, JSONB, GEOMETRY, VECTOR(n)
- Full-Text: TSVECTOR, TSQUERY
- Collections: ARRAY[T]
- Nullable: Any type with NULL support
Type Coercion
RaisinDB supports implicit type coercion:
- Numeric ladder: INT → BIGINT → DOUBLE
- TEXT → PATH for path comparisons
- Non-nullable → Nullable wrapping
Explicit casting uses CAST(expr AS type) syntax.
Case Sensitivity
- SQL keywords are case-insensitive
- Function names are case-insensitive
- Column names are case-sensitive
- String comparisons are case-sensitive by default
Standards Compliance
RaisinDB SQL implementation follows:
- SQL:2016 standard for core features
- SQL:2023 PGQ for property graph queries
- PostgreSQL conventions for extensions
- PostGIS for geospatial functions