Skip to content

Commit d5eeeb2

Browse files
committed
Only traverse the base mapping once
1 parent 6b9a000 commit d5eeeb2

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

sqlmesh/core/macros.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,15 @@ def _macro_str_replace(text: str) -> str:
128128
return f"self.template({text}, locals())"
129129

130130

131-
class CaseInsensitiveMapping(dict):
131+
class CaseInsensitiveMapping(t.Dict[str, t.Any]):
132132
def __init__(self, data: t.Dict[str, t.Any]) -> None:
133133
super().__init__(data)
134134

135-
self._lower = {k.lower(): v for k, v in data.items()}
136-
137135
def __getitem__(self, key: str) -> t.Any:
138-
if key in self:
139-
return super().__getitem__(key)
140-
return self._lower[key.lower()]
136+
return super().__getitem__(key.lower())
141137

142-
def get(self, key: str, default: t.Any = None) -> t.Any:
143-
if key in self:
144-
return super().get(key, default)
145-
return self._lower.get(key.lower(), default)
138+
def get(self, key: str, default: t.Any = None, /) -> t.Any:
139+
return super().get(key.lower(), default)
146140

147141

148142
class MacroDialect(Python):
@@ -335,7 +329,7 @@ def template(self, text: t.Any, local_variables: t.Dict[str, t.Any]) -> str:
335329
# We try to convert all variables into sqlglot expressions because they're going to be converted
336330
# into strings; in sql we don't convert strings because that would result in adding quotes
337331
base_mapping = {
338-
k: convert_sql(v, self.dialect)
332+
k.lower(): convert_sql(v, self.dialect)
339333
for k, v in chain(self.variables.items(), self.locals.items(), local_variables.items())
340334
}
341335
return MacroStrTemplate(str(text)).safe_substitute(CaseInsensitiveMapping(base_mapping))

0 commit comments

Comments
 (0)