Status: Completed portfolio implementation; synthetic data and mock integrations
Status: Completed portfolio implementation; synthetic data and mock integrations
Portfolio project demonstrating a safety-first SQL optimization assistant over a synthetic PostgreSQL database. It detects common performance risks, retrieves tuning guidance, proposes constrained rewrites, and clearly labels unmeasured benefits as theoretical.
SQL problems are often discovered after latency, cost, or concurrency has degraded. This service gives engineers a repeatable first review of projections, predicates, joins, sorting, grouping, and indexes without requiring production credentials or production queries.
flowchart LR
UI[Streamlit UI] --> API[FastAPI]
API --> V[SELECT validator]
V --> P[sqlglot parser]
P --> A[AST analyzer]
A --> R[RAG rule search]
A --> L[Mock or OpenAI-compatible LLM]
A --> O[Safe optimizer]
O --> C[Theoretical comparison]
C --> DB[(Synthetic PostgreSQL)]
sequenceDiagram
participant User
participant Agent
participant Tools
participant DB
User->>Agent: SQL query
Agent->>Tools: validate, parse, schema, indexes
Tools->>Agent: findings and relevant rules
Agent->>Agent: constrained rewrite and explanation
Agent-->>User: optimized SQL and trade-offs
User->>DB: optional validated local execution
The agent uses validate_sql, parse_sql, analyze_query, get_schema, get_indexes, search_optimization_rules, generate_optimized_sql, and compare_queries. Mock mode is deterministic and never invents execution percentages.
python -m venv .venv
.\\.venv\\Scripts\\Activate.ps1
pip install -r requirements.txt
uvicorn app.main:app --reloadOpen http://localhost:8000/docs for the API or run streamlit run streamlit_app/app.py in another terminal.
docker compose up --buildAPI: http://localhost:8000, UI: http://localhost:8501, PostgreSQL: localhost:5432.
Input:
SELECT * FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
WHERE YEAR(o.order_date) = 2026
ORDER BY o.order_date DESC;The optimizer identifies SELECT *, a function on a date column, and a possible sort. It rewrites the date condition to the half-open range order_date >= '2026-01-01' AND order_date < '2027-01-01'. This can preserve index range access, but the result must be measured with representative data.
POST /api/v1/analyze, /optimize, /validate, /compare; GET /api/v1/schema; GET /health.
app/rules/knowledge.py is a transparent retrieval corpus covering sargability, projections, joins, sorting, cardinality, indexes, and safe execution. LLM_MODE=mock is the default. Set LLM_MODE and OPENAI_API_KEY only when an OpenAI-compatible endpoint is intentionally configured; deterministic rules remain the source of truth.
The validator blocks write/DDL/admin keywords and parses SQL before execution. Optional execution wraps a validated query in a row-limited outer SELECT, reuses its SQLAlchemy engine, and uses a PostgreSQL read-only transaction with a statement timeout. Database failures return a generic message rather than raw SQL or driver details. Do not point this demo at production without authentication, a dedicated least-privilege database account, rate limiting, and operational controls. Observability records a short query fingerprint, event, latency, and error flag, never raw SQL.
evaluation/examples.json contains 40 synthetic examples spanning joins, aggregations, subqueries, CTEs, windows, indexes, date predicates, sorting, and filtering. Run python -m evaluation.runner. Accuracy is a labeled rule-detection metric; semantic equivalence and hallucination rate require a larger reviewed corpus and measured query pairs.
pytest -qTests cover parsing, analysis, safe rewriting, dangerous SQL detection, RAG, and API behavior. Limitations include heuristic index recommendations, no cost-based plan model, and no fabricated runtime comparison when execution statistics are unavailable.
Run the Streamlit app locally and capture the analysis view for a repository screenshot. The UI is intentionally generated from the live API rather than storing client or production artifacts.
Add PostgreSQL EXPLAIN (FORMAT JSON) ingestion, schema-aware type conversion checks, workload-level index analysis, a reviewed benchmark corpus, asynchronous execution cancellation, and an audited provider adapter for additional OpenAI-compatible models.
- Built a FastAPI and Streamlit SQL tuning assistant with sqlglot AST analysis and PostgreSQL synthetic data.
- Implemented safety controls for SELECT-only validation, row limits, local execution, and privacy-preserving observability.
- Designed transparent RAG and tool-calling orchestration so the LLM explains verified findings instead of inventing metrics.
- Why is a half-open date range safer than
BETWEENfor timestamps? - When is an index harmful despite improving a read query?
- How would you prove semantic equivalence between two SQL statements?
- How do cardinality estimates affect join and index choices?
- How would you safely expose
EXPLAIN ANALYZE?
- How would you ground an optimizer's recommendations in schema and plan evidence?
- How would you detect and measure LLM hallucinations in generated SQL?
- How would you isolate tenant data and secrets in a hosted version?
- What tool-call policy prevents the model from bypassing validation?
- How would you build a feedback loop from accepted recommendations?
Demonstrates Python, FastAPI, Streamlit, SQLGlot AST parsing, read-only SQL policy, PostgreSQL, RAG-style rules, Docker, testing, and evaluation without claiming unmeasured performance gains.
Sunil Javadi · GitHub · Portfolio · LinkedIn
git add .
git commit -m "Build AI SQL query optimizer"
git push origin main