File size: 1,397 Bytes
381fcf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# cypher4sql

Neo4j-enhanced context engineering for SQL generation.

## Overview

cypher4sql uses Neo4j schema graphs to provide rich context for text-to-SQL models:

- **Semantic table retrieval** via vector embeddings
- **FK statistics** (fanout, cardinality, risk tags)
- **Join path finding** for multi-hop queries
- **Planner hints** for complex joins

## Architecture

```
User Question
     ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Neo4j Schema Graph (cypher4sql)    β”‚
β”‚  - Vector search β†’ relevant tables  β”‚
β”‚  - FK stats β†’ join risk/hints       β”‚
β”‚  - Path finding β†’ join options      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
     ↓
Enhanced Context + FK Stats
     ↓
SQL Model β†’ Query
```

## Usage

### Training
- FK stats for reward shaping
- Mutation validation (Phase 3.5)
- Context engineering for training data

### Production
- Live schema retrieval
- Semantic table ranking
- FK stats for query planning

## FK Stats Format (v4)

```
Join Options:
  1. orders.customer_id -> customers.id [1:N, fk_u=0.85, n=50000, fanout=3.2, risk=MED]
  2. order_items.order_id -> orders.id [1:N, fk_u=1.00, n=150000, fanout=2.8, risk=LOW]

Safe (no-dup) joins: order_items->orders
```

## License

MIT