🚀 Spark Notebook Converter
Transform SQL stored procedures into executable PySpark Jupyter notebooks in seconds.
from spark_notebook_converter import parse_stored_procedure
from notebook_generator import NotebookGenerator
sql = "SELECT dept, AVG(salary) FROM employees GROUP BY dept ORDER BY dept"
gen = NotebookGenerator(parse_stored_procedure(sql), title="Salary Analysis")
gen.save("salary_analysis.ipynb")
Result: A complete Jupyter notebook with PySpark code, validation, and export cells.
✨ Features
- ✅ Parse SQL - Extract 9 operation types (SELECT, FROM, JOIN, WHERE, GROUP BY, HAVING, ORDER BY, Window Functions, CTEs)
- ✅ Generate Notebooks - Create executable Jupyter notebooks (.ipynb)
- ✅ Generate Code - Produce PySpark DataFrame code from SQL
- ✅ Educational - Add explanations for SQL→PySpark transformations
- ✅ Zero Dependencies - Uses Python standard library only
- ✅ Production Ready - 57/57 tests passing (100% coverage)
📦 Installation
Local install from this repository
pip install .
Via PyPI (after publication)
pip install spark-notebook-converter
From source
git clone https://github.com/your-org/spark-notebook-converter
cd spark-notebook-converter
python -m pytest tests/ # Verify installation (57 tests pass)
🚀 Quick Start
1. Generate a Jupyter Notebook
from spark_notebook_converter import parse_stored_procedure
from notebook_generator import NotebookGenerator
sql = """
SELECT c.customer_id, c.name, COUNT(o.id) as order_count
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE c.status = 'ACTIVE'
GROUP BY c.customer_id, c.name
ORDER BY order_count DESC
"""
# Parse and generate
proc = parse_stored_procedure(sql)
gen = NotebookGenerator(proc, title="Customer Analysis")
# Save as .ipynb
gen.save("customer_analysis.ipynb")
Then open in Jupyter:
jupyter notebook customer_analysis.ipynb
2. Generate PySpark Code
from pyspark_code_generator import generate_pyspark_from_sql
sql = """
SELECT product_id, SUM(qty) as total_qty, AVG(price) as avg_price
FROM sales
WHERE region = 'North America'
GROUP BY product_id
ORDER BY total_qty DESC
"""
code = generate_pyspark_from_sql(sql)
print(code)
Output:
# Start with base table
result = sales_df # WHERE
# SQL: WHERE region = 'North America'
result = result.filter("region == 'North America'")
# SQL: GROUP BY product_id
result = result.groupBy("product_id").agg(
sum("qty").alias("total_qty"),
avg("price").alias("avg_price")
)
# SQL: ORDER BY total_qty
result = result.orderBy(desc("total_qty"))
3. Inspect Parsed SQL
from spark_notebook_converter import parse_stored_procedure
sql = "SELECT * FROM users WHERE status='ACTIVE' GROUP BY region"
proc = parse_stored_procedure(sql)
# Access parsed components
print(f"Tables: {[t.name for t in proc.input_tables]}")
print(f"Has WHERE: {proc.where_condition is not None}")
print(f"Grouped by: {proc.group_by.columns if proc.group_by else None}")
print(f"Columns: {[c.name for c in proc.select_columns]}")
📚 Supported SQL Operations
| Operation | Status | Example |
|---|---|---|
| SELECT | ✅ | SELECT id, name, COUNT(*) as cnt |
| FROM | ✅ | FROM customers c |
| JOIN | ✅ | LEFT JOIN orders ON c.id = o.customer_id |
| WHERE | ✅ | WHERE status = 'ACTIVE' |
| GROUP BY | ✅ | GROUP BY dept, region |
| HAVING | ✅ | HAVING COUNT(*) > 10 |
| ORDER BY | ✅ | ORDER BY salary DESC |
| Window Functions | ✅ | ROW_NUMBER() OVER (PARTITION BY...) |
| CTEs | ✅ | WITH temp AS (SELECT ...) |
🏗️ Architecture
SQL Input
↓
[Phase 2] SQLParser
├─ Extracts operations
├─ Builds data models
└─ Returns StoredProcedure object
↓
StoredProcedure
├─→ [Phase 3] NotebookGenerator → Jupyter Notebook (.ipynb)
└─→ [Phase 4] PySparkGenerator → PySpark code
Core Modules
- spark_notebook_converter.py - SQL parsing and data models
- notebook_generator.py - Jupyter notebook creation
- pyspark_code_generator.py - PySpark code generation
🧪 Testing
Run all tests:
python -m pytest tests/ -v
# Output: 57 passed in 0.40s ✅
Run specific test suite:
python tests/test_parser.py # Parser tests (7)
python tests/test_notebook_generator.py # Notebook tests (28)
python tests/test_pyspark_generator.py # PySpark tests (22)
📖 Examples
Example 1: Simple SELECT
from spark_notebook_converter import parse_stored_procedure
from pyspark_code_generator import generate_pyspark_from_sql
sql = "SELECT id, name, email FROM users WHERE status = 'ACTIVE'"
code = generate_pyspark_from_sql(sql)
print(code)
Example 2: Complex Query with Multiple Operations
sql = """
SELECT
dept_id,
COUNT(*) as emp_count,
AVG(salary) as avg_salary,
MAX(salary) as max_salary
FROM employees e
LEFT JOIN departments d ON e.dept_id = d.id
WHERE e.hire_date >= '2020-01-01'
GROUP BY dept_id
HAVING COUNT(*) > 5
ORDER BY avg_salary DESC
"""
from spark_notebook_converter import parse_stored_procedure
from notebook_generator import NotebookGenerator
proc = parse_stored_procedure(sql)
gen = NotebookGenerator(proc, title="Department Analysis")
gen.save("department_analysis.ipynb")
Example 3: Window Functions
sql = """
SELECT
employee_id,
salary,
ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC) as rank
FROM employees
"""
from pyspark_code_generator import generate_pyspark_from_sql
code = generate_pyspark_from_sql(sql)
print(code)
💻 Use Cases
📚 Education
Teach SQL developers PySpark by showing equivalent DataFrame operations
🔄 Migration
Convert legacy SQL procedures to modern PySpark code
📊 Documentation
Generate interactive Jupyter notebooks as query documentation
🚀 Automation
Batch convert multiple SQL procedures to notebooks
🎓 Training
Create training materials showing SQL→PySpark transformations
📊 Project Statistics
| Metric | Value |
|---|---|
| Lines of Code | ~2,400 |
| Lines of Tests | ~1,200 |
| Test Coverage | 100% (57/57 tests) |
| External Dependencies | 0 |
| Python Version | 3.8+ |
| License | MIT |
🔌 API Reference
Core Functions
parse_stored_procedure(sql: str) -> StoredProcedure
Parse SQL and extract operations.
proc = parse_stored_procedure("SELECT * FROM users WHERE active=1")
# Returns: StoredProcedure object with all extracted components
NotebookGenerator(procedure: StoredProcedure, title: str)
Create a Jupyter notebook generator.
gen = NotebookGenerator(proc, title="My Analysis")
notebook = gen.generate() # Get Notebook object
gen.save("output.ipynb") # Save to file
generate_pyspark_from_sql(sql: str) -> str
Generate PySpark code from SQL.
code = generate_pyspark_from_sql(sql)
print(code) # Print executable PySpark code
Data Models
- StoredProcedure - Root object with all parsed components
- Column - Represents a column with name, type, aggregations
- Table - Represents a table with name and alias
- JoinCondition - Represents a JOIN operation
- Aggregation - Represents aggregation functions (COUNT, SUM, etc)
- GroupByClause - GROUP BY with aggregations
- OrderByClause - ORDER BY with direction
- WindowFunction - Window functions (ROW_NUMBER, RANK, etc)
🐛 Troubleshooting
Issue: "ModuleNotFoundError: No module named 'spark_notebook_converter'"
Solution: Make sure you're in the correct directory or install the local package first:
pip install .
Issue: "SyntaxError" when generating code
Solution: The parser might not recognize your SQL syntax. Check:
- SQL is well-formed
- Parentheses are balanced
- Column/table names are valid
Issue: Generated notebook doesn't execute
Solution:
- Make sure your DataFrames match expected names (customers_df, orders_df, etc)
- Update data loading paths in Cell 3
- Install PySpark:
pip install pyspark
🤝 Contributing
Contributions welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/improvement) - Add tests for new functionality
- Ensure all tests pass (
pytest tests/) - Submit a pull request
📝 License
This project is licensed under the MIT License - see LICENSE file for details.
🙋 Support
- Documentation: See README.md and doc files in the repo
- Examples: Run
python demo.pyto see 5 working scenarios - Tests: Check
tests/directory for usage examples - Issues: Open an issue on GitHub
🎯 What's Next?
Future Features (Planned)
- Support for INSERT, UPDATE, DELETE
- Spark SQL output target
- Pandas DataFrame code generation
- Query optimization suggestions
- Web UI for interactive conversion
Want to Help?
- Report bugs
- Suggest features
- Improve documentation
- Share use cases
🌟 Show Your Support
If this project helped you, please consider:
- ⭐ Starring on GitHub
- 🔗 Sharing with colleagues
- 💬 Leaving feedback
- 🤝 Contributing improvements
Made with ❤️ for data engineers and SQL developers
Quick Links
Version: 1.0.0
Status: Production Ready ✅
Last Updated: 2026-04-16
Metadata
Release files for spark-notebook-converter 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| spark_notebook_converter-1.0.0.tar.gz | 25.1 kB | Without provenance | Details for spark_notebook_converter-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| spark_notebook_converter-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for spark_notebook_converter-1.0.0-py3-none-any.whl |
Total release size: 42.8 kB
Release files / spark_notebook_converter-1.0.0.tar.gz
| Download URL | spark_notebook_converter-1.0.0.tar.gz |
|---|---|
| Size | 25.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9b85638d633152e97cbb0f254931ac9e0f84028cff47891f63c6ffb869f6e937
|
|
BLAKE2b-256 checksum How to use checksums |
cfe9fcb74df0422cfad6e64883b6e43925b7326c90a9acf2238e4c2ffccaafa6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.11
|
Release files / spark_notebook_converter-1.0.0-py3-none-any.whl
| Download URL | spark_notebook_converter-1.0.0-py3-none-any.whl |
|---|---|
| Size | 17.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a134a855039854b724672a6e8abee5f7751a3d16548d63d95dda42c596200649
|
|
BLAKE2b-256 checksum How to use checksums |
cdc91f83a7fe491d45dc0aed535e9b50862d12eedcb354ea69e96f8eb0662f40
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.11
|
Security for spark-notebook-converter 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.