Skip to content

Commit

Permalink
feat: no recursion copy (#3103)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Mar 8, 2024
1 parent a38db01 commit 18fd079
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,38 @@ def meta(self) -> t.Dict[str, t.Any]:
return self._meta

def __deepcopy__(self, memo):
copy = self.__class__(**deepcopy(self.args))
if self.comments is not None:
copy.comments = deepcopy(self.comments)

if self._type is not None:
copy._type = self._type.copy()

if self._meta is not None:
copy._meta = deepcopy(self._meta)

return copy
root = self.__class__()
stack = [(self, root)]

while stack:
node, copy = stack.pop()

if node.comments is not None:
copy.comments = deepcopy(node.comments)
if node._type is not None:
copy._type = deepcopy(node._type)
if node._meta is not None:
copy._meta = deepcopy(node._meta)
if node._hash is not None:
copy._hash = node._hash

for k, vs in node.args.items():
if hasattr(vs, "parent"):
stack.append((vs, vs.__class__()))
copy.set(k, stack[-1][-1])
elif type(vs) is list:
copy.args[k] = []

for v in vs:
if hasattr(v, "parent"):
stack.append((v, v.__class__()))
copy.append(k, stack[-1][-1])
else:
copy.append(k, v)
else:
copy.set(k, vs)

return root

def copy(self):
"""
Expand Down Expand Up @@ -289,7 +310,7 @@ def append(self, arg_key: str, value: t.Any) -> None:
arg_key (str): name of the list expression arg
value (Any): value to append to the list
"""
if not isinstance(self.args.get(arg_key), list):
if type(self.args.get(arg_key)) is not list:
self.args[arg_key] = []
self.args[arg_key].append(value)
self._set_parent(arg_key, value)
Expand Down

0 comments on commit 18fd079

Please sign in to comment.