# tobymao/sqlglot

**Attribution required: if you use, quote, or summarise this content, you must credit and link back to [awesome-repositories.com](https://awesome-repositories.com/repository/tobymao-sqlglot).**

9,336 stars · 1,176 forks · Python · MIT

## Links

- GitHub: https://github.com/tobymao/sqlglot
- Homepage: https://sqlglot.com/
- awesome-repositories: https://awesome-repositories.com/repository/tobymao-sqlglot.md

## Description

sqlglot is a SQL parser and transpiler that represents queries as abstract syntax trees to enable structural analysis, modification, and semantic transformation. It functions as a dialect translator and query optimizer, converting SQL code between different database engines and simplifying syntax trees through rule-based normalization.

The project provides a framework for defining custom SQL dialects by overriding tokenizers, parsers, and generators. It includes a lineage analyzer to track data flow from source tables through complex queries to identify the origin of specific columns.

Additional capabilities cover programmatic SQL generation, syntax validation, and schema-aware type inference. The system can generate logical plans for query planning, extract metadata, and execute queries in-memory using Python dictionaries as data sources.

## Tags

### Data & Databases

- [SQL Abstract Syntax Tree Parsing](https://awesome-repositories.com/f/data-databases/sql-abstract-syntax-tree-parsing.md) — Parses raw SQL strings into structured abstract syntax trees that capture the semantics of the statement. ([source](https://github.com/tobymao/sqlglot/blob/main/AGENTS.md))
- [SQL Dialect Adapters](https://awesome-repositories.com/f/data-databases/sql-dialect-adapters.md) — Translates SQL queries between different database dialects by mapping functions, types, and delimiters.
- [Column-Level Lineage Extraction](https://awesome-repositories.com/f/data-databases/column-mappings/column-level-lineage-extraction.md) — Tracks the flow of data from source tables through transformations to identify column origins.
- [Recursive Descent Parsers](https://awesome-repositories.com/f/data-databases/data-transformation-functions/recursive-processors/recursive-logic-implementations/recursive-descent-parsers.md) — Uses a recursive descent parsing engine to translate SQL text into structured syntax trees.
- [Lexical Tokenizers](https://awesome-repositories.com/f/data-databases/database-dialects/lexical-tokenizers.md) — Implements specialized lexers to handle unique keywords and delimiters for various database engines.
- [Query Optimizers](https://awesome-repositories.com/f/data-databases/database-management-systems/database-systems-management/database-operations/sql-query-execution/query-optimizers.md) — Provides automated query optimization by rewriting boolean logic and normalizing predicates to improve execution speed. ([source](https://github.com/tobymao/sqlglot#readme))
- [Dynamic SQL Generators](https://awesome-repositories.com/f/data-databases/dynamic-sql-generators.md) — Provides a Python API for programmatically building and modifying dynamic SQL queries.
- [SQL Syntax Tree Construction](https://awesome-repositories.com/f/data-databases/expression-based-data-querying/fluent-query-apis/sql-syntax-tree-construction.md) — Provides a programmatic way to build or modify SQL queries by manipulating the abstract syntax tree. ([source](https://github.com/tobymao/sqlglot#readme))
- [AST-Based Query Construction](https://awesome-repositories.com/f/data-databases/raw-sql-execution/advanced-sql-construct-execution/ast-based-query-construction.md) — Constructs SQL statements programmatically by manipulating a structured syntax tree via a builder pattern. ([source](https://github.com/tobymao/sqlglot/blob/main/AGENTS.md))
- [Dialect Specification Frameworks](https://awesome-repositories.com/f/data-databases/sql-dialect-adapters/dialect-specification-frameworks.md) — Provides a framework for creating new SQL engine specifications by overriding core parsing and generation components. ([source](https://github.com/tobymao/sqlglot/blob/main/sqlglot/dialects/__init__.py))
- [SQL Generators](https://awesome-repositories.com/f/data-databases/sql-generators.md) — Converts a syntax tree back into a SQL string with support for custom formatting and dialect rules. ([source](https://github.com/tobymao/sqlglot/blob/main/AGENTS.md))
- [SQL Parsers](https://awesome-repositories.com/f/data-databases/sql-parsers.md) — Parses and transpiles SQL code between different database dialects using structured expression trees.
- [Semantic Analysis](https://awesome-repositories.com/f/data-databases/query-analyzers/semantic-analysis.md) — Resolves semantic context and relationships, such as references within common table expressions. ([source](https://github.com/tobymao/sqlglot/blob/main/posts/ast_primer.md))
- [Query Planning](https://awesome-repositories.com/f/data-databases/query-planning.md) — Converts optimized syntax trees into directed acyclic graphs of execution steps for query planning. ([source](https://github.com/tobymao/sqlglot/blob/main/posts/python_sql_engine.md))
- [Schema Modelers](https://awesome-repositories.com/f/data-databases/schema-modelers.md) — Represents the structure of tables, views, and columns to enrich the analysis of syntax trees. ([source](https://github.com/tobymao/sqlglot/blob/main/posts/onboarding.md))
- [Syntax Validation](https://awesome-repositories.com/f/data-databases/sql-abstract-syntax-tree-parsing/syntax-validation.md) — Parses SQL strings to identify syntax violations and return structured error messages with precise locations. ([source](https://github.com/tobymao/sqlglot/blob/main/README.md))
- [SQL Metadata Extraction](https://awesome-repositories.com/f/data-databases/sql-metadata-extraction.md) — Extracts structural information from queries by identifying the specific columns and tables being referenced. ([source](https://github.com/tobymao/sqlglot#readme))
- [SQL Formatters](https://awesome-repositories.com/f/data-databases/sql-parsers/sql-formatters.md) — Generates consistently styled SQL from a syntax tree with options for pretty printing and indentation. ([source](https://github.com/tobymao/sqlglot/blob/main/posts/onboarding.md))
- [SQL Query Optimizations](https://awesome-repositories.com/f/data-databases/sql-query-optimizations.md) — Simplifies the syntax tree by rewriting boolean predicates and normalizing expressions through a set of predefined transformation rules.
- [Visitor Pattern Traversers](https://awesome-repositories.com/f/data-databases/visitor-pattern-traversers.md) — Implements the visitor pattern to identify and modify specific nodes within SQL expression trees.

### Artificial Intelligence & ML

- [SQL Lineage Analyzers](https://awesome-repositories.com/f/artificial-intelligence-ml/data-lineage/automated-lineage-capturers/sql-lineage-analyzers.md) — Compiles complex SQL statements to trace column-level data lineage and dependencies.

### Development Tools & Productivity

- [AST Transformation Tools](https://awesome-repositories.com/f/development-tools-productivity/ast-transformation-tools.md) — Enables structural modification of SQL queries by searching for and transforming specific nodes within the syntax tree. ([source](https://github.com/tobymao/sqlglot/blob/main/AGENTS.md))
- [Language Support Extensions](https://awesome-repositories.com/f/development-tools-productivity/language-support-extensions.md) — Enables the addition of new database system support through subclassing and plugin registration. ([source](https://github.com/tobymao/sqlglot#readme))

### Programming Languages & Runtimes

- [Intermediate Representations](https://awesome-repositories.com/f/programming-languages-runtimes/compiler-interpreter-internals/compiler-infrastructure/intermediate-representations.md) — Uses a generic internal representation to translate SQL between different database dialects.

### Software Engineering & Architecture

- [Expression Transformers](https://awesome-repositories.com/f/software-engineering-architecture/expression-transformers.md) — Provides utilities for modifying query logic by traversing and mapping specific node types within the syntax tree.
- [Query Result Type Inference](https://awesome-repositories.com/f/software-engineering-architecture/typescript-type-definitions/dynamic-return-type-inference/query-result-type-inference.md) — Assigns data types to expression nodes by referencing schema information and function signature metadata. ([source](https://github.com/tobymao/sqlglot/blob/main/posts/onboarding.md))
- [Schema-Driven Type Inference](https://awesome-repositories.com/f/software-engineering-architecture/typescript-type-definitions/schema-driven-type-inference.md) — Infers data types for expression nodes using external table definitions and function metadata.

### Part of an Awesome List

- [Database Tools](https://awesome-repositories.com/f/awesome-lists/data/database-tools.md) — No-dependency SQL parser, transpiler, and optimizer.
- [SQL Development Tools](https://awesome-repositories.com/f/awesome-lists/devtools/sql-development-tools.md) — Transpiler for translating between multiple SQL dialects.
- [SQL Parsers](https://awesome-repositories.com/f/awesome-lists/devtools/sql-parsers.md) — Pure Python SQL parser, transpiler, and builder.
