Skip to content

Instantly share code, notes, and snippets.

@maxjustus
Created May 2, 2024 23:01
Show Gist options
  • Save maxjustus/85c856e2989f30887140bde4d92f8458 to your computer and use it in GitHub Desktop.
Save maxjustus/85c856e2989f30887140bde4d92f8458 to your computer and use it in GitHub Desktop.
simple example of using transform with sqlglot to implement custom rewrite rules
from sqlglot import exp, parse_one
expression_tree = parse_one("SELECT a FROM x")
def transformer(node):
if isinstance(node, exp.Column) and node.name == "a":
return parse_one("FUN(a)")
return node
transformed_tree = expression_tree.transform(transformer)
transformed_tree.sql()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment