Skip to main content

Generate unique fingerprints for SQL queries

Project description

SQL Fingerprint

A Python library for generating normalized SQL fingerprints - useful for query identification, logging, and analysis.

Features

  • 🪄 Query Normalization - Produces consistent SQL representations
  • 🔍 Literal Replacement - Safely replaces values with placeholders
  • 📏 Whitespace Normalization - Removes insignificant spacing
  • 🧹 Syntax Cleaning - Standardizes SQL keywords and formatting
  • Select Clause Preservation - Maintains string literals in SELECT statements

Installation

pip install sqlfingerprint

Quick Start

from sqlfingerprint import SQLFingerprinter

fingerprinter = SQLFingerprinter()
# Basic example
query = "SELECT * FROM users WHERE id = 123"
print(fingerprinter.fingerprint(query))
# Output: select * from users where id = ?
# Complex example
complex_sql = """
    SELECT name, 'const' AS const_val FROM users 
    WHERE email LIKE '%@example.com' 
    AND created_at > '2024-01-01' 
    GROUP BY 1 ORDER BY 1 DESC
"""
print(fingerprinter.fingerprint(complex_sql))
# Output: select name, 'const' as const_val from users where email like ? 
#         and created_at > ? group by 1 order by 1 desc

Use Cases

  • 🕵️ Query deduplication in database logs
  • 📊 SQL performance analysis
  • 🔒 Sensitive data obfuscation
  • 🔄 Query pattern recognition
  • 📈 Query analytics aggregation

Normalization Rules

The fingerprinting process applies these transformations:

Original Element Normalized Form
String literals (WHERE) ?
Numeric literals ?
Boolean values ?
IN lists IN (?)
SQL keywords Lowercase
Whitespace Single space
Comments Removed
Backticks Removed
Parentheses spacing Standardized
SELECT clause string literals are preserved for result set identification

Limitations

  • Primarily tested with SELECT statements
  • May require tuning for complex CTE queries
  • Performance scales with query complexity
  • SQL dialect support limited to standard SQL

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/your-feature)
  3. Add tests for your changes
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin feature/your-feature)
  6. Open a Pull Request Run tests with:
pytest tests/

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project Homepage: https://github.com/yinhaox/sqlfingerprint

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

sqlfingerprint-0.1.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlfingerprint-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file sqlfingerprint-0.1.0.tar.gz.

File metadata

  • Download URL: sqlfingerprint-0.1.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for sqlfingerprint-0.1.0.tar.gz
Algorithm Hash digest
SHA256 df915ec2c2f195ada550f20d26b717ccf5839988b1523a1045dd945e7edcd0f7
MD5 d9db2283de8bd3ede9bceadc84b1e4c3
BLAKE2b-256 3e1830aac9fbb8884737ed60d69883c8494b8f0ee38e3798e217c5b8f2b825c1

See more details on using hashes here.

File details

Details for the file sqlfingerprint-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sqlfingerprint-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for sqlfingerprint-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 32abcbb0f62d91c08c5383324a8805b3ebda80f2b472f400ef46f7695bfdf494
MD5 5cd8bc47909cde6c65643ff95a8c62dd
BLAKE2b-256 388442286227d73b19abf4e1876de7154e0c2212e9ca16bf21256440dc41bf10

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page