Skip to content

Commit 23283ca

Browse files
authored
Fix: avoid concealing dialect module exception in _try_load (#4708)
1 parent 5f90307 commit 23283ca

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

sqlglot/dialects/dialect.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,15 @@ def classes(cls):
129129

130130
@classmethod
131131
def _try_load(cls, key: str | Dialects) -> None:
132-
try:
133-
if isinstance(key, Dialects):
134-
key = key.value
135-
136-
# This import will lead to a new dialect being loaded, and hence, registered.
137-
# We assert that the key is an actual module to avoid blindly importing files.
138-
assert key in DIALECT_MODULE_NAMES
132+
if isinstance(key, Dialects):
133+
key = key.value
134+
135+
# This import will lead to a new dialect being loaded, and hence, registered.
136+
# We check that the key is an actual sqlglot module to avoid blindly importing
137+
# files. Custom user dialects need to be imported at the top-level package, in
138+
# order for them to be registered as soon as possible.
139+
if key in DIALECT_MODULE_NAMES:
139140
importlib.import_module(f"sqlglot.dialects.{key}")
140-
except Exception:
141-
pass
142141

143142
@classmethod
144143
def __getitem__(cls, key: str) -> t.Type[Dialect]:

sqlglot/expressions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ def transform(self, fun: t.Callable, *args: t.Any, copy: bool = True, **kwargs)
637637

638638
if not root:
639639
root = new_node
640-
elif new_node is not node:
640+
elif parent and arg_key and new_node is not node:
641641
parent.set(arg_key, new_node, index)
642642

643643
assert root

sqlglot/parser.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3176,9 +3176,11 @@ def _parse_with(self, skip_with_token: bool = False) -> t.Optional[exp.With]:
31763176
last_comments = None
31773177
expressions = []
31783178
while True:
3179-
expressions.append(self._parse_cte())
3180-
if last_comments:
3181-
expressions[-1].add_comments(last_comments)
3179+
cte = self._parse_cte()
3180+
if isinstance(cte, exp.CTE):
3181+
expressions.append(cte)
3182+
if last_comments:
3183+
cte.add_comments(last_comments)
31823184

31833185
if not self._match(TokenType.COMMA) and not self._match(TokenType.WITH):
31843186
break

0 commit comments

Comments
 (0)