GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing sqlglot.com — sqlglot API documentation SQLGlot is a no-dependency SQL parser, transpiler, optimizer, and engine. It can be used to format SQL or translate between over 30 dialects like DuckDB, Presto / Trino, Spark / Databricks, Snowflake, and BigQuery. It aims to read a wide variety of SQL inputs and output syntactically and semantically correct SQL in the targeted dialects. It is a very comprehensive generic SQL parser with a robust test suite. It is also quite performant, while being written purely in Python. You can easily customize the parser, analyze queries, traverse expression trees, and programmatically build SQL. SQLGlot can detect a variety of syntax errors, such as unbalanced parentheses, incorrect usage of reserved keywords, and so on. These errors are highlighted and dialect incompatibilities can warn or raise depending on configurations. Learn more about SQLGlot in the API documentation and the expression tree primer. Contributions are very welcome in SQLGlot; read the contribution guide and the onboarding document to get started! Table of Contents
InstallFrom PyPI:
On Python 3.9, Or with a local checkout:
Requirements for development (optional):
VersioningGiven a version number
Get in TouchWe'd love to hear from you. Join our community Slack channel! FAQI tried to parse SQL that should be valid, but it failed. Why? You probably didn't specify the source dialect. Without it, The generated SQL is not in the correct dialect! The target dialect must also be specified explicitly. For example, to transpile a query from Spark SQL to DuckDB, do Why does SQLGlot parse my invalid SQL without complaining? The parser is intentionally lenient, so it can accept queries that a real engine would reject. SQLGlot is a transpiler, not a validator. A query that parses successfully may still fail at execution time. Why doesn't the output preserve my formatting, casing or quoting? SQLGlot parses queries into an AST and generates SQL back from it, so it preserves the meaning of a query rather than its exact text. Cosmetic details can change in the process. Use How can I make parsing faster? Install the version compiled with mypyc: My dialect isn't supported. What are my options? You can subclass an existing dialect (Custom Dialects), or ship one as a separate package (Creating a Dialect Plugin). Keep in mind that subclassing may not work properly with ExamplesFormatting and TranspilingEasily translate from one dialect to another. For example, date/time functions vary between dialects and can be hard to deal with:
SQLGlot can even translate custom time formats:
Identifier delimiters and data types can be translated as well:
Comments are also preserved on a best-effort basis:
MetadataYou can explore SQL with expression helpers to do things like find columns and tables in a query:
Read the ast primer to learn more about SQLGlot's internals. Parser ErrorsWhen the parser detects an error in the syntax, it raises a
Structured syntax errors are accessible for programmatic use:
Unsupported ErrorsIt may not be possible to translate some queries between certain dialects. For these cases, SQLGlot may emit a warning and will proceed to do a best-effort translation by default:
This behavior can be changed by setting the
Some queries need additional information to be transpiled accurately, such as the schemas of the referenced tables. This is because certain transformations are type-sensitive and depend on type inference. The Transpilation is a hard problem, so SQLGlot solves it incrementally. Some dialect pairs may lack support for certain inputs, but coverage improves over time. We highly appreciate well-documented and tested issues or PRs, so feel free to reach out if you need guidance! Build and Modify SQLSQLGlot supports incrementally building SQL expressions:
It's possible to modify a parsed tree:
Parsed expressions can also be transformed recursively by applying a mapping function to each node in the tree:
SQL OptimizerSQLGlot can rewrite queries into an "optimized" form. It performs a variety of techniques to create a new canonical AST. This AST can be used to standardize queries or provide the foundations for implementing an actual engine. For example:
AST IntrospectionYou can see the AST version of the parsed SQL by calling
AST DiffSQLGlot can calculate the semantic difference between two expressions and output changes in a form of a sequence of actions needed to transform a source expression into a target one:
The sequence consists of See also: Semantic Diff for SQL. Custom DialectsDialects can be added by subclassing
Note: when SQL ExecutionSQLGlot is able to interpret SQL queries, where the tables are represented as Python dictionaries. The engine is not supposed to be fast, but it can be useful for unit testing and running SQL natively across Python objects. Additionally, the foundation can be easily integrated with fast compute kernels, such as Arrow and Pandas. The example below showcases the execution of a query that involves aggregations and joins:
See also: Writing a Python SQL engine from scratch. Used ByDocumentationSQLGlot uses pdoc to serve its API documentation. A hosted version is on the SQLGlot website, or you can build locally with:
Run Tests and Lint
BenchmarksBenchmarks run on Python 3.14.3 in seconds. sqlglot, sqltree, sqlparse, and sqlfluff are python based whereas sqloxide and polyglot-sql are rust bindings.
Optional DependenciesSQLGlot uses dateutil to simplify constant date and interval arithmetic. With dateutil installed, the optimizer simplifies:
to:
Without dateutil, the optimizer leaves this arithmetic unevaluated. Supported Dialects
Official Dialects are maintained by the core SQLGlot team with higher priority for bug fixes and feature additions. Community Dialects are developed and maintained primarily through community contributions. These are fully functional but may receive lower priority for issue resolution compared to officially supported dialects. We welcome and encourage community contributions to improve these dialects. Plugin Dialects (supported since v28.6.0) are third-party dialects developed and maintained in external repositories by independent contributors. These dialects are not part of the SQLGlot codebase and are distributed as separate packages. The SQLGlot team does not provide support or maintenance for plugin dialects — please direct any issues or feature requests to their respective repositories. See Creating a Dialect Plugin below for information on how to build your own. Creating a Dialect PluginIf your database isn't supported, you can create a plugin that registers a custom dialect via entry points. Create a package with your dialect class and register it in
The dialect will be automatically discovered and can be used like any built-in dialect:
See the Custom Dialects section for implementation details. 1# ruff: noqa: F401 2""" 3.. include:: ../README.md 4 5---- 6""" 7 8from __future__ import annotations 9 10from collections.abc import Collection 11from builtins import type as Type 12 13import logging 14import typing as t 15 16from sqlglot import expressions as exp 17from sqlglot.dialects.dialect import Dialect as Dialect, Dialects as Dialects 18from sqlglot.diff import diff as diff 19from sqlglot.errors import ( 20 ErrorLevel as ErrorLevel, 21 ParseError as ParseError, 22 TokenError as TokenError, 23 UnsupportedError as UnsupportedError, 24) 25from sqlglot.expressions import ( 26 Expr as Expr, 27 alias_ as alias, 28 and_ as and_, 29 case as case, 30 cast as cast, 31 column as column, 32 condition as condition, 33 delete as delete, 34 except_ as except_, 35 find_tables as find_tables, 36 from_ as from_, 37 func as func, 38 insert as insert, 39 intersect as intersect, 40 maybe_parse as maybe_parse, 41 merge as merge, 42 not_ as not_, 43 or_ as or_, 44 select as select, 45 subquery as subquery, 46 table_ as table, 47 to_column as to_column, 48 to_identifier as to_identifier, 49 to_table as to_table, 50 union as union, 51) 52from sqlglot.generator import Generator as Generator 53from sqlglot.parser import Parser as Parser 54from sqlglot.schema import MappingSchema as MappingSchema, Schema as Schema 55from sqlglot.tokens import Token as Token, Tokenizer as Tokenizer, TokenType as TokenType 56 57if t.TYPE_CHECKING: 58 from sqlglot._typing import E, GeneratorArgs, ParserNoDialectArgs 59 from typing_extensions import Unpack 60 from sqlglot.dialects.dialect import DialectType as DialectType 61 62logger = logging.getLogger("sqlglot") 63 64 65try: 66 from sqlglot._version import __version__, __version_tuple__ # type: ignore[import-not-found] 67except ImportError: 68 logger.error( 69 "Unable to set __version__, run `pip install -e .` or `python setup.py develop` first." 70 ) 71 72 73pretty = False 74"""Whether to format generated SQL by default.""" 75 76 77def tokenize(sql: str, read: DialectType = None, dialect: DialectType = None) -> list[Token]: 78 """ 79 Tokenizes the given SQL string. 80 81 Args: 82 sql: the SQL code string to tokenize. 83 read: the SQL dialect to apply during tokenizing (eg. "spark", "hive", "presto", "mysql"). 84 See `sqlglot.dialects.Dialects` for options. 85 dialect: the SQL dialect (alias for read). 86 87 Returns: 88 The resulting list of tokens. 89 """ 90 return Dialect.get_or_raise(read or dialect).tokenize(sql) 91 92 93def parse( 94 sql: str, 95 read: DialectType = None, 96 dialect: DialectType = None, 97 **opts: Unpack[ParserNoDialectArgs], 98) -> list[Expr | None]: 99 """ 100 Parses the given SQL string into a collection of syntax trees, one per parsed SQL statement. 101 102 Args: 103 sql: the SQL code string to parse. 104 read: the SQL dialect to apply during parsing (eg. "spark", "hive", "presto", "mysql"). 105 See `sqlglot.dialects.Dialects` for options. 106 dialect: the SQL dialect (alias for read). 107 **opts: other `sqlglot.parser.Parser` options. 108 109 Returns: 110 The resulting syntax tree collection. 111 """ 112 return Dialect.get_or_raise(read or dialect).parse(sql, **opts) 113 114 115@t.overload 116def parse_one( 117 sql: str, 118 *, 119 read: DialectType = ..., 120 dialect: DialectType = ..., 121 into: Type[E], 122 **opts: Unpack[ParserNoDialectArgs], 123) -> E: ... 124 125 126@t.overload 127def parse_one( 128 sql: str, 129 read: DialectType = ..., 130 dialect: DialectType = ..., 131 into: exp.IntoType | None = ..., 132 **opts: Unpack[ParserNoDialectArgs], 133) -> Expr: ... 134 135 136def parse_one( 137 sql: str, 138 read: DialectType = None, 139 dialect: DialectType = None, 140 into: exp.IntoType | None = None, 141 **opts: Unpack[ParserNoDialectArgs], 142) -> Expr: 143 """ 144 Parses the given SQL string and returns a syntax tree. 145 146 Args: 147 sql: the SQL code string to parse. 148 read: the SQL dialect to apply during parsing (eg. "spark", "hive", "presto", "mysql"). 149 See `sqlglot.dialects.Dialects` for options. 150 dialect: the SQL dialect (alias for read) 151 into: the SQLGlot Expr to parse into. 152 **opts: other `sqlglot.parser.Parser` options. 153 154 Returns: 155 A single syntax tree if one statement is parsed, otherwise a Block expression containing all parsed syntax trees. 156 """ 157 158 dialect = Dialect.get_or_raise(read or dialect) 159 160 if into: 161 result = dialect.parse_into(into, sql, **opts) 162 else: 163 result = dialect.parse(sql, **opts) 164 165 if not result or result[0] is None: 166 raise ParseError(f"No expression was parsed from '{sql}'") 167 168 return exp.Block(expressions=result) if len(result) > 1 else result[0] 169 170 171def transpile( 172 sql: str, 173 read: DialectType = None, 174 write: DialectType = None, 175 identity: bool = True, 176 error_level: ErrorLevel | None = None, 177 **opts: Unpack[GeneratorArgs], 178) -> list[str]: 179 """ 180 Parses the given SQL string in accordance with the source dialect and returns a list of SQL strings transformed 181 to conform to the target dialect. Each string in the returned list represents a single transformed SQL statement. 182 183 Args: 184 sql: the SQL code string to transpile. 185 read: the source dialect used to parse the input string (eg. "spark", "hive", "presto", "mysql"). 186 See `sqlglot.dialects.Dialects` for options. 187 write: the target dialect into which the input should be transformed (eg. "spark", "hive", "presto", "mysql"). 188 See `sqlglot.dialects.Dialects` for options. 189 identity: if set to `True` and if the target dialect is not specified the source dialect will be used as both: 190 the source and the target dialect. 191 error_level: the desired error level of the parser. 192 **opts: other `sqlglot.generator.Generator` options. 193 194 Returns: 195 The list of transpiled SQL statements. 196 """ 197 write = (read if write is None else write) if identity else write 198 write = Dialect.get_or_raise(write) 199 return [ 200 write.generate(expression, copy=False, **opts) if expression else "" 201 for expression in parse(sql, read, error_level=error_level) 202 ]
logger =
<Logger sqlglot (WARNING)>
pretty =
False
Whether to format generated SQL by default.
def
tokenize( sql: str, read: Union[str, sqlglot.dialects.Dialect, type[sqlglot.dialects.Dialect], NoneType] = None, dialect: Union[str, sqlglot.dialects.Dialect, type[sqlglot.dialects.Dialect], NoneType] = None) -> list[sqlglot.tokenizer_core.Token]:
View Source
78def tokenize(sql: str, read: DialectType = None, dialect: DialectType = None) -> list[Token]: 79 """ 80 Tokenizes the given SQL string. 81 82 Args: 83 sql: the SQL code string to tokenize. 84 read: the SQL dialect to apply during tokenizing (eg. "spark", "hive", "presto", "mysql"). 85 See `sqlglot.dialects.Dialects` for options. 86 dialect: the SQL dialect (alias for read). 87 88 Returns: 89 The resulting list of tokens. 90 """ 91 return Dialect.get_or_raise(read or dialect).tokenize(sql) Tokenizes the given SQL string. Arguments:
Returns:
def
parse( sql: str, read: Union[str, sqlglot.dialects.Dialect, type[sqlglot.dialects.Dialect], NoneType] = None, dialect: Union[str, sqlglot.dialects.Dialect, type[sqlglot.dialects.Dialect], NoneType] = None, **opts: typing_extensions.Unpack[sqlglot._typing.ParserNoDialectArgs]) -> list[sqlglot.expressions.core.Expr | None]:
View Source
94def parse( 95 sql: str, 96 read: DialectType = None, 97 dialect: DialectType = None, 98 **opts: Unpack[ParserNoDialectArgs], 99) -> list[Expr | None]: 100 """ 101 Parses the given SQL string into a collection of syntax trees, one per parsed SQL statement. 102 103 Args: 104 sql: the SQL code string to parse. 105 read: the SQL dialect to apply during parsing (eg. "spark", "hive", "presto", "mysql"). 106 See `sqlglot.dialects.Dialects` for options. 107 dialect: the SQL dialect (alias for read). 108 **opts: other `sqlglot.parser.Parser` options. 109 110 Returns: 111 The resulting syntax tree collection. 112 """ 113 return Dialect.get_or_raise(read or dialect).parse(sql, **opts) Parses the given SQL string into a collection of syntax trees, one per parsed SQL statement. Arguments:
Returns:
def
parse_one( sql: str, read: Union[str, sqlglot.dialects.Dialect, type[sqlglot.dialects.Dialect], NoneType] = None, dialect: Union[str, sqlglot.dialects.Dialect, type[sqlglot.dialects.Dialect], NoneType] = None, into: Union[type[sqlglot.expressions.core.Expr], Collection[type[sqlglot.expressions.core.Expr]], NoneType] = None, **opts: typing_extensions.Unpack[sqlglot._typing.ParserNoDialectArgs]) -> sqlglot.expressions.core.Expr:
View Source
137def parse_one( 138 sql: str, 139 read: DialectType = None, 140 dialect: DialectType = None, 141 into: exp.IntoType | None = None, 142 **opts: Unpack[ParserNoDialectArgs], 143) -> Expr: 144 """ 145 Parses the given SQL string and returns a syntax tree. 146 147 Args: 148 sql: the SQL code string to parse. 149 read: the SQL dialect to apply during parsing (eg. "spark", "hive", "presto", "mysql"). 150 See `sqlglot.dialects.Dialects` for options. 151 dialect: the SQL dialect (alias for read) 152 into: the SQLGlot Expr to parse into. 153 **opts: other `sqlglot.parser.Parser` options. 154 155 Returns: 156 A single syntax tree if one statement is parsed, otherwise a Block expression containing all parsed syntax trees. 157 """ 158 159 dialect = Dialect.get_or_raise(read or dialect) 160 161 if into: 162 result = dialect.parse_into(into, sql, **opts) 163 else: 164 result = dialect.parse(sql, **opts) 165 166 if not result or result[0] is None: 167 raise ParseError(f"No expression was parsed from '{sql}'") 168 169 return exp.Block(expressions=result) if len(result) > 1 else result[0] Parses the given SQL string and returns a syntax tree. Arguments:
Returns:
def
transpile( sql: str, read: Union[str, sqlglot.dialects.Dialect, type[sqlglot.dialects.Dialect], NoneType] = None, write: Union[str, sqlglot.dialects.Dialect, type[sqlglot.dialects.Dialect], NoneType] = None, identity: bool = True, error_level: sqlglot.errors.ErrorLevel | None = None, **opts: typing_extensions.Unpack[sqlglot._typing.GeneratorArgs]) -> list[str]:
View Source
172def transpile( 173 sql: str, 174 read: DialectType = None, 175 write: DialectType = None, 176 identity: bool = True, 177 error_level: ErrorLevel | None = None, 178 **opts: Unpack[GeneratorArgs], 179) -> list[str]: 180 """ 181 Parses the given SQL string in accordance with the source dialect and returns a list of SQL strings transformed 182 to conform to the target dialect. Each string in the returned list represents a single transformed SQL statement. 183 184 Args: 185 sql: the SQL code string to transpile. 186 read: the source dialect used to parse the input string (eg. "spark", "hive", "presto", "mysql"). 187 See `sqlglot.dialects.Dialects` for options. 188 write: the target dialect into which the input should be transformed (eg. "spark", "hive", "presto", "mysql"). 189 See `sqlglot.dialects.Dialects` for options. 190 identity: if set to `True` and if the target dialect is not specified the source dialect will be used as both: 191 the source and the target dialect. 192 error_level: the desired error level of the parser. 193 **opts: other `sqlglot.generator.Generator` options. 194 195 Returns: 196 The list of transpiled SQL statements. 197 """ 198 write = (read if write is None else write) if identity else write 199 write = Dialect.get_or_raise(write) 200 return [ 201 write.generate(expression, copy=False, **opts) if expression else "" 202 for expression in parse(sql, read, error_level=error_level) 203 ] Parses the given SQL string in accordance with the source dialect and returns a list of SQL strings transformed to conform to the target dialect. Each string in the returned list represents a single transformed SQL statement. Arguments:
Returns:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |