Vertica SQL dialect implementation for sqlglot
Project description
SQLGlot Vertica Dialect
A comprehensive Vertica dialect implementation for SQLGlot, a Python SQL parser and transpiler.
Features
This Vertica dialect provides full-featured support for Vertica SQL syntax, including:
Core Functionality
- Complete SQL parsing and generation
- Vertica-specific data types (TIMESTAMPTZ, BINARY, etc.)
- Identifier quoting with double quotes
- Heredoc string support
Date and Time Functions
DATEADD(unit, interval, timestamp)- Add intervals to dates/timestampsDATEDIFF(unit, start_date, end_date)- Calculate date differencesDATE_TRUNC(unit, timestamp)- Truncate timestamps to specified unitTO_CHAR(timestamp, format)- Format timestamps as stringsTO_DATE(string, format)- Parse strings as datesTO_TIMESTAMP(string, format)- Parse strings as timestampsCURRENT_DATE,CURRENT_TIME,CURRENT_TIMESTAMP,NOW()
String Functions
ILIKE/NOT ILIKE- Case-insensitive pattern matchingLIKE/NOT LIKE- Case-sensitive pattern matchingSUBSTRING(string, start, length)- Extract substringsTRIM()- Remove whitespace- Standard string functions (
LENGTH,UPPER,LOWER, etc.)
Mathematical Functions
RANDOM()- Generate random numbers- Standard math functions (
ABS,CEIL,FLOOR,ROUND,SQRT,POWER)
Hash Functions
MD5(string)- MD5 hashSHA1(string)- SHA1 hash
Array Support
ARRAY[...]syntax for array literals- Array operations and functions
Window Functions
- Complete support for window functions
ROW_NUMBER(),RANK(),LAG(),LEAD(), etc.OVERclauses withPARTITION BYandORDER BY
Advanced Features
- Common Table Expressions (CTEs)
- Subqueries and EXISTS clauses
- All JOIN types (INNER, LEFT, RIGHT, FULL OUTER, CROSS)
- Aggregate functions with GROUP BY and HAVING
- CASE WHEN expressions
- Complex data types and constraints
Installation
pip install sqlglot-vertica
For development:
pip install sqlglot-vertica[dev]
Usage
Basic Usage
from sqlglot import transpile
from sqlglot_vertica.vertica import Vertica
# Parse and generate Vertica SQL
sql = "SELECT DATEADD(DAY, 1, CURRENT_DATE)"
parsed = Vertica.parse(sql)[0]
generated = Vertica().generate(parsed)
print(generated) # SELECT DATEADD(DAY, 1, CURRENT_DATE)
# Transpile from other dialects to Vertica
postgres_sql = "SELECT NOW()"
vertica_sql = transpile(postgres_sql, read='postgres', write='vertica')[0]
print(vertica_sql) # SELECT CURRENT_TIMESTAMP
Advanced Examples
# Complex query with CTEs and window functions
complex_query = """
WITH sales_data AS (
SELECT
region,
DATE_TRUNC(MONTH, sale_date) AS month,
SUM(amount) AS total_sales,
ROW_NUMBER() OVER (PARTITION BY region ORDER BY SUM(amount) DESC) AS rank
FROM sales
WHERE sale_date >= CURRENT_DATE - INTERVAL '1' YEAR
GROUP BY region, DATE_TRUNC(MONTH, sale_date)
)
SELECT
region,
month,
total_sales,
CASE
WHEN rank = 1 THEN 'Top Month'
ELSE 'Other'
END AS category
FROM sales_data
WHERE rank <= 5
ORDER BY region, total_sales DESC
"""
parsed = Vertica.parse(complex_query)[0]
print(parsed)
Vertica-Specific Features
# Date functions
sql = "SELECT DATEDIFF(DAY, '2023-01-01', '2023-12-31')"
result = Vertica().generate(Vertica.parse(sql)[0])
# Array operations
sql = "SELECT ARRAY[1, 2, 3, 4, 5]"
result = Vertica().generate(Vertica.parse(sql)[0])
# Case-insensitive matching
sql = "SELECT * FROM users WHERE name ILIKE '%john%'"
result = Vertica().generate(Vertica.parse(sql)[0])
# Timestamp with timezone
sql = "CREATE TABLE events (id INTEGER, created_at TIMESTAMPTZ)"
result = Vertica().generate(Vertica.parse(sql)[0])
Development
Setup Development Environment
git clone https://github.com/luisdelatorre/sqlglot-vertica.git
cd sqlglot-vertica
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -e .[dev]
Running Tests
# Run all tests
pytest
# Run with coverage
pytest --cov=sqlglot_vertica
# Run specific test file
pytest tests/test_vertica.py
# Run specific test method
pytest tests/test_vertica.py::TestVertica::test_vertica_date_functions
Code Quality
# Format code
black sqlglot_vertica tests
# Sort imports
isort sqlglot_vertica tests
# Type checking
mypy sqlglot_vertica
Testing
The dialect includes comprehensive tests covering:
- Basic SQL operations (SELECT, INSERT, UPDATE, DELETE)
- All supported data types
- Date and time functions
- String and mathematical functions
- Window functions and aggregations
- JOIN operations
- Subqueries and CTEs
- Complex query patterns
- Transpilation from other dialects
- Edge cases and error conditions
The test suite follows the same patterns as other SQLGlot dialect tests, ensuring consistency and reliability.
Compatibility
- Python: 3.8+
- SQLGlot: 26.0.0+
- Vertica: Compatible with Vertica SQL syntax
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
License
MIT License - see LICENSE file for details.
📁 Examples
This package includes comprehensive examples demonstrating various use cases. See the examples/ directory for detailed demonstrations.
🚀 Quick Start Examples
# Run basic usage examples
python examples/basic_usage.py
# Run advanced transformation examples
python examples/advanced_transformations.py
# Run data migration examples
python examples/data_migration.py
# Run performance analysis examples
python examples/performance_analysis.py
# Run comprehensive demonstration
python examples/run_all_examples.py
📋 Example Categories
-
examples/basic_usage.py- Fundamental operations- Basic SQL parsing with Vertica syntax
- Cross-dialect transpilation
- AST inspection and manipulation
- Error handling for unsupported features
-
examples/advanced_transformations.py- AST manipulation- Custom query transformations
- Query optimization using SQLGlot
- Schema analysis and lineage tracking
- Advanced AST rewriting patterns
-
examples/data_migration.py- Database migration- DDL conversion between databases
- Function migration (date/time, string, hash)
- Data type mapping
- Batch script processing and validation
-
examples/performance_analysis.py- Query optimization- Query complexity analysis
- Anti-pattern detection
- Index recommendation
- Performance benchmarking
Common Use Cases
# Database Migration
from sqlglot import transpile
from sqlglot_vertica.vertica import Vertica
vertica_sql = "SELECT DATEDIFF('day', hire_date, CURRENT_DATE) FROM employees"
postgres_sql = transpile(vertica_sql, read=Vertica, write="postgres")[0]
# Query Analysis
from sqlglot import parse_one
ast = parse_one("SELECT MD5(email) FROM users WHERE active = true", read=Vertica)
tables = [table.name for table in ast.find_all(exp.Table)]
functions = [func.sql() for func in ast.find_all(exp.Func)]
# Error Handling
from sqlglot.errors import ParseError, UnsupportedError
try:
parse_one("SELECT $$invalid syntax$$", read=Vertica)
except ParseError as e:
print(f"Parse error: {e}")
Acknowledgments
- Built on top of the excellent SQLGlot library
- Inspired by existing SQLGlot dialect implementations, particularly Postgres
- Designed to be compatible with Vertica SQL syntax and semantics
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file vertica_sqlglot_dialect-0.1.0.tar.gz.
File metadata
- Download URL: vertica_sqlglot_dialect-0.1.0.tar.gz
- Upload date:
- Size: 17.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a60d6e59ec6a974ed332ae5d177108dc99f12f47c1e49061a514ac76da69b589
|
|
| MD5 |
b0c1d91f14cf7d1ff974d89ffadf40a0
|
|
| BLAKE2b-256 |
677b27b19ebdeff7ab6c7356e7f17509689499864262a444fcddfca4677fea85
|
File details
Details for the file vertica_sqlglot_dialect-0.1.0-py3-none-any.whl.
File metadata
- Download URL: vertica_sqlglot_dialect-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
77bfb833fb4c5691b8b919f2a648b4992debf55333e78cd7936dff1796876a02
|
|
| MD5 |
7e2d709106d58e0f8b62db09d6845eac
|
|
| BLAKE2b-256 |
d8be064e99acd7d747ab262ec7bbd4e1236707e34a944a14d2e4cf1c6db14aa8
|