展示 HN:SQLite 图 – 使用 Cypher 查询的图数据库(alpha 版,但可用)
Show HN: SQLite Graph Ext – Graph database with Cypher queries (alpha)

原始链接: https://github.com/agentflare-ai/sqlite-graph

## SQLite-Graph:SQLite 的图数据库扩展(Alpha) SQLite-Graph 是一个处于 Alpha 阶段的 SQLite 扩展,它带来了图数据库功能,并提供**完整的、基本的 Cypher 查询支持**。作为 AgentFlare AI 生态系统的一部分开发,它允许利用 SQLite 的可靠性和 Cypher 的表达力来构建复杂的图应用程序。 **主要特性:**核心图操作(通过 SQL 创建、读取、更新、删除节点/边)、端到端 Cypher 执行(包括 `CREATE`、`MATCH`、`WHERE` 和 `RETURN`)、图虚拟表以及基本的图算法(连通性、密度、中心性)。它拥有令人印象深刻的性能——**30 万+ 节点/秒和 39 万+ 边/秒**——并包含 **Python 3.6+ 绑定**。 **目前,由于其 Alpha 状态和潜在的 API 变更,不建议将此扩展用于生产环境**。开发重点是实现完全的 openCypher 兼容性、高级查询功能和性能优化,路线图一直延伸到 2027 年。 **安装**涉及下载预构建的二进制文件或从源代码构建。基本用法示例演示了 Cypher 查询和基于 SQL 函数的图操作。

## SQLite 图形扩展:摘要 一个名为 SQLite Graph Ext 的新 alpha 项目,使用 Cypher 查询语言为 SQLite 带来了图形数据库功能。由 Agentflare AI 开发,它允许用户在现有的 SQLite 数据库中创建和查询图形,而无需单独的图形数据库系统。 该扩展拥有令人印象深刻的性能——高达 340K 节点/秒的插入速度和 390K 边/秒的创建速度——并利用完全的执行流水线(词法分析器、解析器、计划器、执行器),用 C99 构建,没有外部依赖。目前,它支持节点和关系创建、基本过滤以及结果的 JSON 序列化。 虽然仍处于早期阶段,但其局限性包括仅支持正向关系、不支持关系属性过滤以及缺乏聚合等高级功能。开发者计划在 2026 年第一季度实现对 Cypher 的完全支持,优先考虑双向关系和属性投影。该项目采用 MIT 许可,并欢迎反馈,定位为现有 SQLite 用户的一个轻量级、集成的图形解决方案——类似于 SQLite 的 FTS5 扩展,用于全文搜索。 [GitHub 仓库](https://github.com/agentflare-ai/sqlite-graph)
相关文章

原文

Release Build Status License: MIT Platform C99

A powerful SQLite extension that adds graph database capabilities with full Cypher query support. Part of the AgentFlare AI ecosystem. Build sophisticated graph applications with the reliability of SQLite and the expressiveness of Cypher.

⚠️ ALPHA RELEASE: This is an alpha release (v0.1.0-alpha.0) intended for testing and feedback. The API may change in future releases. Not recommended for production use.

  • Core Graph Operations: Create, read, update, delete nodes and edges via SQL functions
  • Cypher Query Execution (NEW!): Full basic Cypher support working end-to-end
    • CREATE (n) - Create anonymous nodes ✅
    • CREATE (p:Person {name: "Alice"}) - Create nodes with labels and properties ✅
    • CREATE (a)-[:TYPE]->(b) - Create relationships ✅
    • CREATE (a)-[:TYPE {props}]->(b) - Create relationships with properties ✅
    • MATCH (n) RETURN n - Basic pattern matching ✅
    • MATCH (n:Label) RETURN n - Label-based matching ✅
    • MATCH (a)-[r:TYPE]->(b) RETURN a, r, b - Relationship matching ✅
    • MATCH (n) WHERE n.prop > value RETURN n - Property filtering ✅
    • WHERE clause with all comparison operators: =, >, <, >=, <=, <>
    • Full execution pipeline: parser → logical planner → physical planner → executor ✅
    • 70/70 CREATE TCK tests passing (100% openCypher compliance for CREATE)
  • Graph Virtual Tables: SQLite virtual table integration for graph data
  • Basic Graph Algorithms: Connectivity checks, density calculations, degree centrality
  • SQL API: graph_node_add(), graph_edge_add(), graph_count_nodes(), graph_count_edges()
  • Performance: 300K+ nodes/sec, 390K+ edges/sec
  • Python Bindings: Full Python 3.6+ support with examples
  • Thread Safety: Fixed critical thread-safety issues for production use
  • Security: Buffer overflow protections, SQL injection prevention
  • Advanced Cypher Features:
    • Bidirectional/reverse relationship matching (<-[r]-, -[r]-)
    • Variable-length paths ([r*1..3])
    • Complex expressions in WHERE (AND, OR, NOT)
    • Property expressions in RETURN (n.property)
    • Aggregations (COUNT, SUM, etc.)
    • ORDER BY, SKIP, LIMIT
  • Graph Algorithms: Shortest path, PageRank (implementation incomplete)
  • Property Indexing: Basic support, optimization ongoing

📋 Roadmap (Not Yet Implemented)

  • Full openCypher Compliance: 100% TCK test suite passing
  • Advanced Write Operations: MERGE, SET, DELETE with complex patterns
  • Advanced Pattern Matching: Multi-hop paths, variable-length patterns
  • Advanced Algorithms: Betweenness centrality, community detection
  • Query Optimization: Cost-based query planner with statistics
  • Distributed Queries: Multi-database graph queries

See ROADMAP.md for detailed feature timeline.

Option 1: Download Pre-built Binary (Linux x86_64)

# Download from releases page
wget https://github.com/agentflare-ai/sqlite-graph/releases/latest/download/libgraph.so

# Verify checksum (recommended)
wget https://github.com/agentflare-ai/sqlite-graph/releases/latest/download/checksums.txt
sha256sum -c checksums.txt

# Ready to use!

Option 2: Build from Source

# Clone the repository
git clone https://github.com/agentflare-ai/sqlite-graph.git
cd sqlite-graph

# Build the extension
make

# The extension will be built as build/libgraph.so

See the Installation Guide for detailed instructions and platform-specific guidance.

Basic Usage (Alpha v0.1.0)

import sqlite3
import json

# Load the extension
conn = sqlite3.connect(":memory:")
conn.enable_load_extension(True)
conn.load_extension("./build/libgraph.so")

# Create a graph virtual table
conn.execute("CREATE VIRTUAL TABLE graph USING graph()")

# Option 1: Use Cypher queries (NEW!)
# Create nodes with properties
conn.execute("SELECT cypher_execute('CREATE (p:Person {name: \"Alice\", age: 30})')")
conn.execute("SELECT cypher_execute('CREATE (c:Company {name: \"Acme Inc\"})')")

# Create relationships with properties
conn.execute("SELECT cypher_execute('CREATE (a:Person {name: \"Bob\"})-[:KNOWS {since: 2020}]->(b:Person {name: \"Charlie\"})')")

# Query with MATCH...RETURN
cursor = conn.execute("SELECT cypher_execute('MATCH (n:Person) RETURN n')")
results = json.loads(cursor.fetchone()[0])
print(results)  # [{"n": Node(1)}, {"n": Node(2)}, ...]

# Query relationships
cursor = conn.execute("SELECT cypher_execute('MATCH (a)-[r:KNOWS]->(b) RETURN a, r, b')")
results = json.loads(cursor.fetchone()[0])
print(results)  # Returns matching relationships with nodes

# Filter with WHERE clause
cursor = conn.execute("SELECT cypher_execute('MATCH (p:Person) WHERE p.age > 25 RETURN p')")
results = json.loads(cursor.fetchone()[0])
print(results)  # Returns nodes where age > 25

# Option 2: Add nodes using SQL functions
conn.execute("SELECT graph_node_add(1, ?) as id", (json.dumps({"name": "Alice", "age": 30}),))
conn.execute("SELECT graph_node_add(2, ?) as id", (json.dumps({"name": "Bob", "age": 25}),))

# Add edges
conn.execute("SELECT graph_edge_add(1, 2, 'KNOWS', ?) as id", (json.dumps({"since": "2020"}),))

# Query the graph
node_count = conn.execute("SELECT graph_count_nodes()").fetchone()[0]
edge_count = conn.execute("SELECT graph_count_edges()").fetchone()[0]
print(f"Nodes: {node_count}, Edges: {edge_count}")  # Nodes: 4, Edges: 1

# Check graph properties
is_connected = conn.execute("SELECT graph_is_connected()").fetchone()[0]
density = conn.execute("SELECT graph_density()").fetchone()[0]
centrality = conn.execute("SELECT graph_degree_centrality(1)").fetchone()[0]
print(f"Connected: {bool(is_connected)}, Density: {density:.3f}, Alice centrality: {centrality:.3f}")

# Verify Cypher-created nodes
cypher_nodes = conn.execute("SELECT id, labels FROM graph_nodes WHERE labels != ''").fetchall()
print(f"Cypher nodes: {cypher_nodes}")  # [(node_id, 'Person'), (node_id, 'Company')]

See the examples/ directory for fully functional demonstrations:

  • simple_graph_example.py - Complete working example with nodes, edges, and algorithms
  • python_examples.py - 6 comprehensive examples showcasing all working features
  • cypher_demo.py - NEW! Cypher CREATE query examples

Note: Basic Cypher queries fully work! CREATE, MATCH, WHERE, and RETURN operations are functional, including relationship creation and matching. You can now build complete graph applications using only Cypher. Advanced features like bidirectional matching, variable-length paths, and aggregations are in development for v0.2.0. The alpha version also provides SQL function-based graph operations for advanced use cases.

  • FEATURES.md - ⭐ Start here! Complete feature status and API reference
  • ROADMAP.md - Development timeline and planned features
  • Installation Guide - Detailed build and installation instructions
  • Examples - Working code examples (simple_graph_example.py, python_examples.py)

Project Status (Alpha v0.1.0)

What Works: Node/edge creation, full Cypher CREATE/MATCH/WHERE/RETURN, relationships with properties, basic algorithms, Python bindings 🚧 In Progress: Advanced MATCH features (bidirectional, variable-length paths), property projection in RETURN, aggregations 📋 Planned: Full Cypher compliance (Q1 2026), advanced algorithms (Q2 2026), query optimization

Architecture (Alpha Implementation)

The extension currently consists of:

  • Virtual Table Interface: SQLite virtual table implementation for graph operations
  • Storage Engine: Efficient node/edge storage with JSON properties
  • Algorithm Library: Basic graph algorithms (connectivity, density, centrality)
  • Cypher Execution Engine: Parser → Planner → Iterator-based Executor
    • ✅ Lexer and Parser (AST generation)
    • ✅ Logical Plan generation
    • ✅ Physical Plan optimization
    • ✅ Volcano-model iterators
    • ✅ Result serialization
  • Cypher Operators: CREATE, MATCH (with relationships), WHERE, RETURN all working
    • ✅ AllNodesScan, LabelIndexScan iterators
    • ✅ Expand iterator (relationship traversal)
    • ✅ Filter iterator (WHERE clause)
    • ✅ Create iterator (nodes and relationships)
  • 📋 Query Optimizer: Cost-based optimization planned for v0.2.0

Performance (Alpha Benchmarks)

  • Node Creation: 300,000+ nodes/second (tested up to 1,000 nodes)
  • Edge Creation: 390,000+ edges/second (tested up to 999 edges)
  • Connectivity Check: <1ms for 1,000 node graphs
  • Scalability: Currently tested up to 1,000 nodes/edges
  • Memory: In-memory storage, persistence via SQLite database file

Social Network Analysis (Working Alpha Code)

import json

# Create person nodes
alice_id = 1
bob_id = 2
carol_id = 3

conn.execute("SELECT graph_node_add(?, ?)",
            (alice_id, json.dumps({"name": "Alice", "city": "NYC"})))
conn.execute("SELECT graph_node_add(?, ?)",
            (bob_id, json.dumps({"name": "Bob", "city": "LA"})))
conn.execute("SELECT graph_node_add(?, ?)",
            (carol_id, json.dumps({"name": "Carol", "city": "NYC"})))

# Add friendships
conn.execute("SELECT graph_edge_add(?, ?, 'FRIENDS', ?)",
            (alice_id, bob_id, json.dumps({})))
conn.execute("SELECT graph_edge_add(?, ?, 'FRIENDS', ?)",
            (alice_id, carol_id, json.dumps({})))

# Query the graph with SQL
mutual_friends = conn.execute("""
    SELECT DISTINCT n.id, n.properties
    FROM graph_nodes n
    JOIN graph_edges e1 ON e1.target = n.id
    JOIN graph_edges e2 ON e2.target = n.id
    WHERE e1.source = ? AND e2.source = ?
      AND e1.edge_type = 'FRIENDS' AND e2.edge_type = 'FRIENDS'
""", (alice_id, bob_id)).fetchall()

# Check connectivity
is_connected = conn.execute("SELECT graph_is_connected()").fetchone()[0]
print(f"Network is connected: {bool(is_connected)}")

See the examples/ directory for complete, tested code:

  • simple_graph_example.py - Basic operations walkthrough
  • python_examples.py - 6 comprehensive examples with output
# Build the extension
make

# Run Python examples (all tests should pass)
cd examples
python3 simple_graph_example.py
python3 python_examples.py

# Expected output: All examples pass with ✅ indicators

This project is licensed under the MIT License - see the LICENSE file for details.

We welcome contributions! Please check:

  • Bug Reports: GitHub Issues
  • Feature Requests: Check ROADMAP.md first, then open an issue with [Feature Request] tag
  • Questions: See FEATURES.md for detailed API documentation

Alpha v0.1.0 (Current): Core graph operations, basic algorithms v0.2.0 (Q1 2026): Full Cypher query execution v0.3.0 (Q2 2026): Advanced graph algorithms v0.4.0 (Q3 2026): Performance optimization & scale v1.0.0 (2027): Production ready with full openCypher compliance

See ROADMAP.md for detailed feature timeline.


Part of the AgentFlare AI ecosystem • Built with SQLite Alpha Release: v0.1.0-alpha.0 • Not for production use

联系我们 contact @ memedata.com