How to delete all columns from select and add Star to query? (modifying) #1357
-
I have some code that can delete columns and aliases from select and add asterisk. Like:
I have a code: import sqlglot
sql ="""
CREATE TABLE table2 AS
SELECT pos1,
pos2,
pos3,
pos4,
pos5,
pos6,
pos7
FROM
(SELECT pos3,
pos4
FROM table1 f
LEFT OUTER JOIN table3 t ON 1=1) test;
;
"""
parse = sqlglot.parse(sql, "tsql",)
print(parse)
print("------------------------------------------------")
def transformer(node):
if isinstance(node, sqlglot.exp.Select):
new = sqlglot.exp.select("*")
s = node.find_all(sqlglot.exp.Expression)
print(s)
for col in s:
if col.parent == node:
if isinstance(col, sqlglot.exp.Column) or isinstance(col, sqlglot.exp.Alias):
col.pop()
else:
print("-------------------col-----------------------------")
print(col)
new.append(col.text,col)
print("-------------------new-----------------------------")
print(new)
print("-------------------node-----------------------------")
print(node)
return node
i =0
for s in parse:
if s is not None:
parse[i]= s.transform(transformer)
i+=1
print(i)
print(parse)
print(parse[0].sql()) In loop I can find a child of select , but how to append it to new select? |
Beta Was this translation helpful? Give feedback.
Answered by
RedHotUnicorn
Mar 29, 2023
Replies: 1 comment
-
found solution
|
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
RedHotUnicorn
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
found solution