{"id":5734,"library":"tree-sitter-sql","title":"Tree-sitter Grammar for SQL","description":"The `tree-sitter-sql` Python package (version 0.3.11) provides a SQL grammar for `tree-sitter`, an incremental parsing library. It aims to offer a permissive and general SQL syntax parsing experience, with an initial focus on the PostgreSQL dialect. The package facilitates the use of the SQL grammar with `py-tree-sitter`, the official Python bindings for Tree-sitter. This library is actively maintained, with updates typically coinciding with grammar enhancements and core `tree-sitter` library developments.","status":"active","version":"0.3.11","language":"en","source_language":"en","source_url":"https://github.com/derekstride/tree-sitter-sql.git","tags":["sql","parsing","tree-sitter","grammar","ast"],"install":[{"cmd":"pip install tree-sitter tree-sitter-sql","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Provides the core Tree-sitter Python bindings necessary to use the SQL grammar.","package":"tree-sitter","optional":false}],"imports":[{"symbol":"Language","correct":"from tree_sitter import Language"},{"symbol":"Parser","correct":"from tree_sitter import Parser"},{"symbol":"tree_sitter_sql","correct":"import tree_sitter_sql"}],"quickstart":{"code":"import tree_sitter_sql\nfrom tree_sitter import Language, Parser\n\n# Initialize the SQL language from the installed grammar package\nSQL_LANGUAGE = Language(tree_sitter_sql.language())\n\n# Create a parser and set its language\nparser = Parser()\nparser.set_language(SQL_LANGUAGE)\n\n# SQL code to parse (must be bytes)\nsql_code = b\"\"\"\nSELECT id, name FROM users WHERE age > 30 ORDER BY name ASC;\n\"\"\"\n\n# Parse the SQL code\ntree = parser.parse(sql_code)\n\n# Get the root node of the syntax tree\nroot_node = tree.root_node\n\n# Print a basic representation of the tree (for demonstration)\n# In a real application, you would traverse the tree or use queries.\nprint(f\"Root Node Type: {root_node.type}\")\nprint(f\"Root Node Text: {root_node.text.decode('utf8')}\")\n\n# Example of finding a specific node type (e.g., 'select_statement')\n# This is a basic traversal; for complex patterns, Tree-sitter queries are used.\nselect_statement_node = None\nfor child in root_node.children:\n    if child.type == 'select_statement':\n        select_statement_node = child\n        break\n\nif select_statement_node:\n    print(f\"Found 'select_statement' node. Text: {select_statement_node.text.decode('utf8')}\")\nelse:\n    print(\"No 'select_statement' node found.\")\n","lang":"python","description":"This quickstart demonstrates how to initialize the `tree-sitter-sql` grammar with the `tree-sitter` Python bindings, create a parser, and parse a sample SQL string. It then shows how to access the root node and perform a basic traversal to find a specific type of statement."},"warnings":[{"fix":"Regularly check the `tree-sitter-sql` GitHub repository for grammar updates and adjust Tree-sitter queries to match the latest grammar structure. Utilize Tree-sitter's query debugger for validation.","message":"Grammar definitions (node names, structure) in `tree-sitter-sql` can evolve. Updates to the grammar may introduce breaking changes to existing Tree-sitter queries that rely on specific node types or structures, requiring query adjustments.","severity":"breaking","affected_versions":"All versions, as grammars are under active development."},{"fix":"Stay informed about `tree-sitter` Python binding releases and their change logs. Update your `tree-sitter` dependency and adapt code as necessary.","message":"The underlying `tree-sitter` Python library (on which `tree-sitter-sql` depends) may introduce breaking changes. For example, the behavior of `iter_matches` was updated to fix incorrect behavior, requiring changes for code relying on the old behavior.","severity":"breaking","affected_versions":"All versions, as `tree-sitter` core evolves."},{"fix":"If strict parsing for a particular SQL dialect is required, evaluate the grammar's fidelity to that dialect. Consider contributing to the `tree-sitter-sql` project or exploring dedicated grammars for specific dialects if available (e.g., `tree-sitter-sqlite`, `tree-sitter-sql-bigquery`).","message":"The `tree-sitter-sql` grammar aims for permissiveness and initially focuses on the PostgreSQL dialect. While general, it may not strictly conform to all nuances of other specific SQL dialects (e.g., SQLite, MySQL, BigQuery).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}