Skip to content

How do you remove a node from the tree? #462

Closed Answered by georgesittas
g-u-t-h-m-a-n asked this question in Q&A
Discussion options

You must be logged in to vote

@g-u-t-h-m-a-n you can now also do the following (relevant commit):

from sqlglot import exp, parse_one

expression_tree = parse_one("SELECT a, b FROM x")

def fun(node):
    if isinstance(node, exp.Column) and node.name == "b":
        return None
    return node

transformed_tree = expression_tree.transform(fun)
transformed_tree.sql()

Which outputs:

SELECT a FROM x

Also, you can remove specific nodes by calling their pop method.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by tobymao
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants