Created
May 2, 2024 23:01
-
-
Save maxjustus/85c856e2989f30887140bde4d92f8458 to your computer and use it in GitHub Desktop.
simple example of using transform with sqlglot to implement custom rewrite rules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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