An intelligent AI-powered tool that converts PostgreSQL queries to Firebolt SQL with live testing and auto-correction.
- ๐ค AI-Powered Conversion: Uses OpenAI ChatGPT for intelligent query conversion
- ๐ฅ Live Firebolt Testing: Tests converted queries against your real Firebolt database
- ๐ Smart Auto-Correction: Intelligently fixes errors with context-aware retry logic
- ๐ง Error Learning: Tracks repeated errors and tries different approaches
- ๐พ Connection Management: Save and reuse Firebolt connection credentials
- ๐ Web Interface: Both Flask and Streamlit versions available
- ๐ Detailed Attempt Tracking: See exactly how queries are being fixed
Streamlit Cloud: [Coming Soon - Will be deployed after GitHub setup]
- Clone the repository:
git clone https://github.com/YOUR_USERNAME/postgresql-firebolt-converter.git
cd postgresql-firebolt-converter- Install dependencies:
pip install -r requirements.txt- Set up environment variables:
cp example.env .env
# Edit .env with your credentials:
# OPENAI_API_KEY=your_openai_api_key_here- Run the Flask app:
python run.py- Open your browser:
http://localhost:5000
-
Follow steps 1-3 above
-
Run the Streamlit app:
streamlit run streamlit_app.py- Open your browser:
http://localhost:8501
git init
git add .
git commit -m "Initial commit: PostgreSQL to Firebolt converter"
git branch -M main
git remote add origin https://github.com/YOUR_USERNAME/postgresql-firebolt-converter.git
git push -u origin main- Go to share.streamlit.io
- Click "New app"
- Connect your GitHub repository
- Set main file path:
streamlit_app.py - Add secrets in "Advanced settings":
OPENAI_API_KEY = "your_openai_api_key_here"
- Click "Deploy"
# OpenAI API key for AI-powered conversion
OPENAI_API_KEY=your_openai_api_key_here
# Optional: Firebolt credentials (can also be entered in UI)
FIREBOLT_CLIENT_ID=your_client_id
FIREBOLT_CLIENT_SECRET=your_client_secret
FIREBOLT_ACCOUNT=your_account
FIREBOLT_DATABASE=your_database
FIREBOLT_ENGINE=your_engine- The app will load
.envautomatically (viapython-dotenv). - By default,
OPENAI_API_KEYfrom.envis used if present. - You can overwrite the key in the Streamlit sidebar under โAI Settings โ OpenAI Optionsโ.
- Toggles in the sidebar:
- Enable OpenAI polish for conversion (applies AI after deterministic rule-based conversion)
- Enable OpenAI for auto-correction during live testing
Core deterministic conversions run even without an OpenAI key:
- Hoist scalar subqueries inside
EXTRACT(...)into derived subqueries in theFROMclause - Convert PostgreSQL JSON operators (
->,->>,#>,#>>) to FireboltJSON_VALUE(JSON_POINTER_EXTRACT_TEXT(...)) - Convert aggregate
FILTER (WHERE ...)toCASE WHEN - Normalize casting and data types
The tool supports two ways to connect to Firebolt:
- UI Form: Enter credentials directly in the web interface
- Environment Variables: Set credentials in
.envfile
- Sends your PostgreSQL query to OpenAI ChatGPT
- Gets Firebolt-compliant SQL as the first attempt
- Executes the converted query against your real Firebolt database
- Captures actual Firebolt error messages
- New Errors: Sends error + query to OpenAI for targeted fixes
- Repeated Errors: Tells OpenAI the previous attempts failed and asks for different approaches
- Intelligent Retry: Up to 15 attempts with learning from each failure
Attempt 1: JSONExtract error โ ๐ NEW ERROR โ Standard fix attempt
Attempt 2: Same JSONExtract error โ ๐ REPEATED ERROR โ "Previous fix FAILED, try different approach"
Attempt 3: Same error โ ๐ REPEATED ERROR โ "Failed in attempts [1,2], completely different method needed"
| PostgreSQL | Firebolt | Status |
|---|---|---|
column->>'path' |
JSON_POINTER_EXTRACT_TEXT(column, '/path') |
โ Auto-fixed |
column::json |
column (remove cast) |
โ Auto-fixed |
SUM(...) FILTER (WHERE ...) |
SUM(CASE WHEN ... THEN ... ELSE 0 END) |
โ Auto-fixed |
now() |
CURRENT_TIMESTAMP |
โ Auto-fixed |
EXTRACT(field FROM column) |
EXTRACT(field FROM CAST(column AS TIMESTAMP)) |
โ Auto-fixed |
column::type |
CAST(column AS type) |
โ Auto-fixed |
Input PostgreSQL:
select sa.applicationid as LAF,
sum(saf.totalfeeamount::decimal) filter (where sfm.feedesc = 'Login Fees')::text as IMD,
sum(saf.totalfeeamount::decimal) filter (where sfm.feedesc = 'Insurance Penetration Percentage')::text as Total_Insurance,
sum(saf.totalfeeamount::decimal) filter (where sfm.feedesc = 'Processing Fees')::text as Processing_Fees,
'skaleup' as source,
now() at TIME ZONE 'Asia/Kolkata' as edl_job_run
from skaleup_application sa
left join skaleup_application_fee saf on sa.applicationkey = saf.applicationkey and saf.isactive = TRUE
left join skaleup_fee_master sfm on saf.feetypecode = sfm.feecode and sfm.isactive = true
where sa.isactive = true
group by sa.applicationid
order by sa.applicationidAuto-Corrected Firebolt Output:
SELECT sa.applicationid AS LAF,
CASE WHEN sfm.feedesc = 'Login Fees' THEN CAST(saf.totalfeeamount AS DECIMAL) ELSE 0 END AS IMD,
CASE WHEN sfm.feedesc = 'Insurance Penetration Percentage' THEN CAST(saf.totalfeeamount AS DECIMAL) ELSE 0 END AS Total_Insurance,
CASE WHEN sfm.feedesc = 'Processing Fees' THEN CAST(saf.totalfeeamount AS DECIMAL) ELSE 0 END AS Processing_Fees,
'skaleup' AS source,
CURRENT_TIMESTAMP AT TIME ZONE 'Asia/Kolkata' AS edl_job_run
FROM skaleup_application sa
LEFT JOIN skaleup_application_fee saf ON sa.applicationkey = saf.applicationkey AND saf.isactive = TRUE
LEFT JOIN skaleup_fee_master sfm ON saf.feetypecode = sfm.feecode AND sfm.isactive = TRUE
WHERE sa.isactive = TRUE
GROUP BY sa.applicationid
ORDER BY sa.applicationidpostgresql-firebolt-converter/
โโโ app.py # Flask web application
โโโ streamlit_app.py # Streamlit web application
โโโ run.py # Flask runner
โโโ requirements.txt # Python dependencies
โโโ converter/ # Core conversion logic
โ โโโ query_converter.py # AI-powered query conversion
โ โโโ live_tester.py # Live testing & auto-correction
โ โโโ firebolt_client.py # Firebolt database client
โ โโโ error_analyzer.py # Error analysis & categorization
โ โโโ ...
โโโ templates/ # Flask HTML templates
โโโ README.md
# Test basic conversion
python test_converter.py
# Test with comparison
python comparison_test.py
# Run web comparison tool
python web_comparison.pyConvert from file to stdout (rule-based only):
python cli.py convert -i input.sqlConvert from stdin to file with AI polish and key override:
cat input.sql | python cli.py convert --enable-ai-polish --openai-key sk-... -o output.sqlShow method, warnings, and explanations:
python cli.py convert -i input.sql --print-method --print-warnings --print-explanations- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- OpenAI ChatGPT for intelligent query conversion
- Firebolt for the powerful analytics database
- Streamlit for easy deployment and beautiful UI
- All contributors who help improve this tool
If you encounter any issues:
- Check the Issues page
- Enable detailed logging by setting
DEBUG=Truein your.env - Review the attempt details in the web interface
- Create a new issue with your PostgreSQL query and error details
- Support for more PostgreSQL-specific functions
- Batch query conversion
- Query performance optimization suggestions
- Integration with more SQL databases
- API endpoints for programmatic access