From Writing SQL to Speaking Human Language
Data analysis is a capability every enterprise needs, but traditional data analysis workflows have a huge bottleneck: business users have analytical needs but cannot write SQL, while data engineers can write SQL but lack business context. As a result, a simple question—"Which product line had the highest profit margin last month?"—may require multiple rounds of communication among business users, product managers, data analysts, and data engineers, taking days. AI-driven data analysis pipelines (NL2SQL + Auto Visualization + Insight Generation) are designed to solve this bottleneck: business users can directly ask questions in natural language, and the AI automatically converts them into SQL queries, executes them, generates visualizations, and extracts data insights.
This is not just an improvement in efficiency, but a democratization of data analysis capabilities. When every business user can directly converse with data, data-driven decision-making becomes more than just a slogan.
System Architecture
Our data analysis pipeline consists of four core stages: Schema Understanding (AI automatically reads the database schema, understanding table structures, field meanings, and relationships between tables), NL2SQL (converts natural language questions into executable SQL queries, supporting complex operations like multi-table JOINs, aggregations, and subqueries), Automatic Visualization (based on the data characteristics of query results, automatically selects the most appropriate chart type—line chart, bar chart, pie chart, scatter plot, etc.), and Insight Generation (based on query results, generates data insight summaries—trend analysis, anomaly detection, comparative analysis, recommended actions).
from openai import OpenAI
import json, sqlite3
client = OpenAI(api_key="your-deepseek-api-key", base_url="https://api.deepseek.com")
class AIDataPipeline:
def __init__(self, db_path):
self.conn = sqlite3.connect(db_path)
self.schema = self._get_schema()
def _get_schema(self):
"""Automatically extract database schema"""
cursor = self.conn.cursor()
tables = cursor.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall()
schema = {}
for (table,) in tables:
cols = cursor.execute(f"PRAGMA table_info({table})").fetchall()
schema[table] = [{"name":c[1], "type":c[2]} for c in cols]
return schema
def nl2sql(self, question):
"""Natural language to SQL"""
prompt = f"""You are an SQL expert. Based on the following database schema and user question, generate an SQL query.
Database Schema:
{json.dumps(self.schema, ensure_ascii=False, indent=2)}
User question: {question}
Requirements:
1. Output only the SQL statement, no explanations
2. Use SQLite-compatible syntax
3. If the question cannot be answered with SQL, output "ERROR: Cannot generate SQL"
4. Only SELECT queries, no INSERT/UPDATE/DELETE
5. Add Chinese aliases (AS) for each field"""
response = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role":"user","content":prompt}],
temperature=0.1
)
sql = response.choices[0].message.content.strip()
if sql.startswith("ERROR"):
raise ValueError(sql)
return sql.replace("```sql","").replace("```","").strip()
def execute_sql(self, sql):
"""Safely execute SQL"""
if not sql.upper().strip().startswith("SELECT"):
raise ValueError("Only SELECT queries allowed")
cursor = self.conn.cursor()
cursor.execute(sql)
columns = [d[0] for d in cursor.description]
rows = cursor.fetchall()
return [dict(zip(columns, row)) for row in rows]
def generate_insights(self, question, data):
"""Generate data insights"""
if not data:
return "Query returned no results"
sample = json.dumps(data[:10], ensure_ascii=False)
prompt = f"""Analyze the following data and generate an insight report.
Original question: {question}
Query results (total {len(data)} rows, showing first 10):
{sample}
Please provide:
1. Data overview (key numbers)
2. Main findings (2-3 items)
3. Anomalies (if any)
4. Recommended actions (1-2 items)
Within 150 characters."""
response = client.chat.completions.create(
model="deepseek-chat",
messages=[{"role":"user","content":prompt}],
temperature=0.3
)
return response.choices[0].message.content
def run(self, question):
print(f"User question: {question}")
print("[1/3] Generating SQL...")
sql = self.nl2sql(question)
print(f"SQL: {sql}")
print("[2/3] Executing query...")
data = self.execute_sql(sql)
print(f"Query result: {len(data)} rows")
print("[3/3] Generating insights...")
insights = self.generate_insights(question, data)
return {"sql": sql, "row_count": len(data), "data": data[:5], "insights": insights}
# Usage
pipeline = AIDataPipeline("sales.db")
result = pipeline.run("Which product line had the highest profit margin last month? TOP5")
print(f"\nInsights: {result['insights']}")Security and Performance Considerations
The security of NL2SQL is critical—user input could be maliciously exploited to execute dangerous operations. Our security measures include: only allowing SELECT statements (rejecting INSERT/UPDATE/DELETE/DROP), query timeout limits (automatically terminating after 30 seconds), result row limits (maximum 1000 rows returned), and sensitive table filtering (certain tables are not allowed to be queried). In terms of performance: automatically adding LIMIT for queries on large tables; caching results for common queries (e.g., "this month's sales"); performing Explain analysis for complex JOIN queries, and rejecting queries that exceed thresholds with suggestions for optimization.
From Prototype to Production
To deploy this pipeline to a production environment, additional considerations include: integrating with enterprise-grade data warehouses (Hive/ClickHouse/Snowflake), integrating with BI platforms (Tableau/PowerBI/Superset), supporting multi-turn conversational data exploration ("break it down by region", "compare with the same period last year"), adding permission control (different users see different data scopes), and establishing query logs for auditing and optimization. When the barrier to data analysis is lowered sufficiently, the true value of data can be released.
Intelligent Chart Recommendation
After the AI retrieves data from the database, choosing the right visualization method is key to the readability of the analysis report. Our pipeline includes an intelligent chart recommender—automatically selecting the most appropriate chart type based on data characteristics: time series data (date + value) → line chart; categorical comparison data (category + value) → bar chart; proportion data → pie or donut chart; bivariate relationship → scatter plot; distribution data → histogram or box plot. The recommendation logic is based on automatic analysis of the data schema and data distribution, requiring no manual specification by the user.Security Audit and Compliance: Security auditing is particularly important in NL2SQL scenarios—it is necessary to record who queried what data at what time, what SQL was generated, and how many rows were returned. For queries involving sensitive fields (such as user phone numbers, ID card numbers), an additional approval process is required. In the financial and healthcare industries, these audit logs are essential for compliance reviews. It is recommended to store audit logs in an immutable logging system and retain them for at least 6 months.
Deep Integration with BI Tools
The AI data analysis pipeline should not be a standalone tool, but should be deeply integrated into the enterprise's existing BI ecosystem. Recommended integration methods: export the AI-generated SQL and query results as datasets for BI platforms (such as Superset, Metabase) so users can further explore on the BI platform; automatically publish AI-generated insights as annotations on the BI platform, marked on corresponding charts; support a "follow-up" mode—if users are not satisfied with the AI-generated results, they can ask follow-ups like "break down by region" or "only look at the most recent month's data", and the AI automatically adjusts the SQL and re-queries. Through this deep integration, AI data analysis transforms from "replacing BI" to "enhancing BI", better aligning with actual enterprise usage habits.
In summary, the core value of an AI-driven data analysis pipeline is not to replace data analysts, but to free them from tedious SQL writing and basic chart creation, allowing them to focus on more valuable activities like "asking the right questions" and "deeply interpreting the business meaning behind the data". When business users can self-serve 80% of routine data answers, data analysts can concentrate their efforts on the most complex 20% of problems, achieving an optimal human-machine collaboration.
With the explosive growth of enterprise data and the continuous improvement of large model capabilities like DeepSeek, AI data analysis pipelines are evolving from "assistive tools" to "decision partners". Future data analysis will no longer be "humans ask questions → AI answers", but rather "AI proactively discovers issues → reports anomalies to humans → humans confirm or adjust → AI conducts in-depth analysis". This shift from passive response to proactive insight will fundamentally change the way and efficiency of data-driven decision-making in enterprises.
Want to orchestrate this skill chain yourself?
Open in Skill Chain →