Semantic SQL Generation
Production - Ontology-driven SQL generation with SHACL validation
Semantic SQL Generation uses ontology mappings to generate SQL that respects the business domain model. Instead of relying purely on schema matching, it leverages concept hierarchies and relationship definitions to produce more accurate and semantically meaningful queries.
12.5.8.1Semantic SQL Pipeline
Natural Language Question
|
v
Concept Extraction (from question)
|
v
Ontology Lookup (map concepts to entities)
|
v
SQL Mapping (entities to tables/columns via OntologySQLMapper)
|
v
SQL Generation (with ontology-aware context)
|
v
SHACL Validation (validate query against shapes)
|
v
Semantic SQL Result# Generate semantic SQL
curl -X POST http://localhost:8000/api/v1/context-graph/semantic-sql/generate \
-H "Content-Type: application/json" \
-H "X-Tenant-ID: acme-corp" \
-d '{
"question": "What is the total value of premium customer orders?",
"ontology_id": "sales-domain"
}'{
"sql": "SELECT SUM(o.total_amount) AS total_value FROM orders o JOIN customers c ON o.customer_id = c.id WHERE c.tier = 'premium'",
"concepts_used": ["premium_customer", "order_value"],
"ontology_mappings": {
"premium_customer": "customers WHERE tier = 'premium'",
"order_value": "orders.total_amount"
},
"confidence": 0.91,
"shacl_valid": true
}