{"id":711,"library":"sqlglot","title":"SQLGlot: SQL Parser, Transpiler, and Optimizer","description":"SQLGlot is a powerful Python library for parsing, transpiling, optimizing, and even executing SQL. It supports translating SQL across over 30 different database dialects, enabling cross-dialect compatibility, query parsing into Abstract Syntax Trees (ASTs), programmatic query rewriting, and optimization. It's known for being a fast, pure-Python solution with no external dependencies and is regularly among the top Python package downloads. The library is currently at version 30.1.0 and follows a versioning strategy where MINOR version increments can introduce backwards-incompatible changes.","status":"active","version":"30.1.0","language":"python","source_language":"en","source_url":"https://github.com/tobymao/sqlglot","tags":["SQL","parser","transpiler","optimizer","AST","database","dialect"],"install":[{"cmd":"pip install sqlglot","lang":"bash","label":"Core library"},{"cmd":"pip install \"sqlglot[c]\"","lang":"bash","label":"With C extensions (if available)"},{"cmd":"pip install \"sqlglot[rs]\"","lang":"bash","label":"With Rust tokenizer (for performance)"}],"dependencies":[{"reason":"Optional performance improvement","package":"c-extensions","optional":true},{"reason":"Optional performance improvement","package":"rust-tokenizer","optional":true}],"imports":[{"note":"Commonly imported directly for convenience; the root module also provides it.","wrong":"sqlglot.parse_one(sql)","symbol":"parse_one","correct":"from sqlglot import parse_one"},{"note":"Commonly imported directly for convenience; the root module also provides it.","wrong":"sqlglot.transpile(sql)","symbol":"transpile","correct":"from sqlglot import transpile"},{"note":"The `exp` submodule contains all expression types for AST manipulation and is typically imported directly from the root `sqlglot` module now.","wrong":"sqlglot.expressions as exp","symbol":"exp","correct":"from sqlglot import exp"}],"quickstart":{"code":"from sqlglot import parse_one, transpile\n\n# Parse a SQL query (defaults to SQLGlot dialect if 'read' is not specified)\nsql_query = \"SELECT id, name FROM users WHERE age > 18\"\nexpression = parse_one(sql_query)\nprint(f\"Parsed Expression: {expression}\")\n\n# Transpile from MySQL to BigQuery\nmysql_query = \"SELECT IFNULL(employee_name, 'Unknown') AS employee_status FROM employees;\"\nbigquery_query = transpile(mysql_query, read=\"mysql\", write=\"bigquery\")[0]\nprint(f\"Transpiled to BigQuery: {bigquery_query}\")\n\n# Further manipulation of the AST is possible with `expression` object\n# E.g., print(expression.find_all(exp.Column))","lang":"python","description":"This quickstart demonstrates basic SQL parsing and cross-dialect transpilation. The `parse_one` function converts a SQL string into an Abstract Syntax Tree (AST), while `transpile` converts SQL between specified dialects. It also shows a common pattern for transforming MySQL-specific functions to BigQuery equivalents."},"warnings":[{"fix":"Always specify `read='your_source_dialect'` when parsing and `write='your_target_dialect'` when transpiling for accurate results (e.g., `parse_one(sql, read='spark')`).","message":"Failing to specify the `read` (source) or `write` (target) dialect during `parse_one` or `transpile` calls can lead to `ParseError` or incorrect output. SQLGlot defaults to its 'SQLGlot dialect', which is a superset, if not specified.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Pre-render or interpolate any non-SQL templating variables before passing the SQL string to SQLGlot. Ensure all SQL literals, especially timestamps, are correctly quoted according to SQL standards.","message":"SQLGlot strictly parses valid SQL. It will raise a `ParseError` if the SQL string contains non-SQL constructs like templating variables (e.g., `{{parameter}}`) or invalid syntax (e.g., unquoted timestamps in some contexts).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Refer to the official changelog or release notes for each minor version upgrade to identify potential breaking changes and necessary code adjustments. Test thoroughly before deploying new minor versions.","message":"SQLGlot's versioning strategy indicates that MINOR version increments (e.g., 29.x to 30.x) can introduce backwards-incompatible fixes or feature additions. Be mindful of minor version bumps during upgrades.","severity":"breaking","affected_versions":"All versions (since project inception)"},{"fix":"If encountering unexpected transpilation behavior for specific dialect features, consult the SQLGlot documentation, raise an issue on GitHub, or consider contributing a fix.","message":"While SQLGlot is comprehensive, transpilation across all possible dialect pairs and inputs is an 'incremental' problem. Some specific dialect conversions may have limitations or ongoing improvements.","severity":"gotcha","affected_versions":"All versions"},{"fix":"As a workaround, replace non-standard cross-quotes with standard double quotes (`\"`) before parsing, or ensure the input SQL conforms to a supported dialect's quoting conventions.","message":"Parsing SQL queries with column names or identifiers using non-standard quoting (e.g., backticks as often used by LLMs) can lead to `ParseError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Simplify complex queries, break down very large SQL statements, or review known performance limitations for specific SQL constructs if encountering consistent timeouts. Consider setting a parsing timeout if the environment supports it to catch such cases gracefully.","message":"SQLGlot parsing or transpilation can, in rare cases, encounter performance bottlenecks or infinite loops when processing extremely complex or malformed SQL, leading to a 'TIMEOUT' rather than an explicit `ParseError`. This is more likely with very large queries, deeply nested expressions, or highly unusual syntax patterns.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'30':27 '30.1.0':78 'abstract':39 'across':25 'among':66 'ast':42,98 'backward':91 'backwards-incompat':90 'chang':93 'compat':35 'cross':33 'cross-dialect':32 'current':75 'databas':29,99 'depend':62 'dialect':30,34,100 'differ':28 'download':71 'enabl':31 'even':18 'execut':19 'extern':61 'fast':54 'follow':80 'incompat':92 'increment':87 'introduc':89 'known':50 'librari':12,73 'minor':85 'optim':6,16,47,97 'packag':70 'pars':14,37 'parser':3,95 'power':10 'programmat':43 'pure':56 'pure-python':55 'python':11,57,69 'queri':36,44 'regular':65 'rewrit':45 'solut':58 'sql':2,20,24,94 'sqlglot':1,7 'strategi':83 'support':22 'syntax':40 'top':68 'translat':23 'transpil':4,15,96 'tree':41 'version':77,82,86","created_at":"2026-03-28T17:11:34.413110+00:00","updated_at":"2026-04-15T17:46:02.236499+00:00","problems":[{"fix":"Install the 'sqlglot' library using pip: 'pip install sqlglot'.","cause":"The 'sqlglot' library is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'sqlglot'"},{"fix":"Update your import statement to: 'from sqlglot import transpile'.","cause":"The 'transpile' function has been moved or renamed in recent versions of 'sqlglot'.","error":"ImportError: cannot import name 'transpile' from 'sqlglot'"},{"fix":"Use the 'sqlglot.parse_one' function instead: 'from sqlglot import parse_one'.","cause":"The 'parse' function has been deprecated or removed in the current version of 'sqlglot'.","error":"AttributeError: module 'sqlglot' has no attribute 'parse'"},{"fix":"Refer to the latest 'sqlglot' documentation for the correct usage of the 'transpile' function.","cause":"The 'transpile' function's signature has changed, and it no longer accepts a 'dialect' keyword argument.","error":"TypeError: transpile() got an unexpected keyword argument 'dialect'"},{"fix":"Ensure you are using a supported dialect; refer to the 'sqlglot' documentation for a list of supported dialects.","cause":"The specified SQL dialect is not supported by the current version of 'sqlglot'.","error":"ValueError: Unsupported dialect 'mysql'"}],"ecosystem":"pypi","meta_description":null,"install_score":50,"quickstart_score":80,"quickstart_tag":"verified","pypi_latest":"30.17.0","cli_name":"sqlglot","cli_version":"sh: 1: sqlglot: not found","type":"library","homepage":"https://sqlglot.com/","github":"https://github.com/tobymao/sqlglot","docs":"https://sqlglot.com/sqlglot.html","changelog":null,"pypi":"https://pypi.org/project/sqlglot/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["data","database","serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":"draft"}}