MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / identifier

Function identifier

misc/python/materialize/mzexplore/sql.py:175–218  ·  view source on GitHub ↗

A version of pg8000.native.identifier (1) that is _ACTUALLY_ compatible with the Postgres code (2). 1. https://github.com/tlocke/pg8000/blob/017959e97751c35a3d58bc8bd5722cee5c10b656/pg8000/converters.py#L739-L761 2. https://github.com/postgres/postgres/blob/b0f7dd915bca6243f3daf52a

(s: str, force_quote: bool = False)

Source from the content-addressed store, hash-verified

173
174
175def identifier(s: str, force_quote: bool = False) -> str:
176 """
177 A version of pg8000.native.identifier (1) that is _ACTUALLY_ compatible with
178 the Postgres code (2).
179
180 1. https://github.com/tlocke/pg8000/blob/017959e97751c35a3d58bc8bd5722cee5c10b656/pg8000/converters.py#L739-L761
181 2. https://github.com/postgres/postgres/blob/b0f7dd915bca6243f3daf52a81b8d0682a38ee3b/src/backend/utils/adt/ruleutils.c#L11968-L12050
182 """
183 if not isinstance(s, str):
184 raise InterfaceError("identifier must be a str")
185
186 if len(s) == 0:
187 raise InterfaceError("identifier must be > 0 characters in length")
188
189 # Look for characters that require quotation.
190 def is_alpha(c: str) -> bool:
191 return ord(c) >= ord("a") and ord(c) <= ord("z") or c == "_"
192
193 def is_alphanum(c: str) -> bool:
194 return is_alpha(c) or ord(c) >= ord("0") and ord(c) <= ord("9")
195
196 quote = not (is_alpha(s[0]))
197
198 for c in s[1:]:
199 if not (is_alphanum(c)):
200 if c == "\u0000":
201 raise InterfaceError(
202 "identifier cannot contain the code zero character"
203 )
204 quote = True
205
206 if quote:
207 break
208
209 # Even if no speciall characters can be found we still want to quote
210 # keywords.
211 if s.upper() in keywords():
212 quote = True
213
214 if quote or force_quote:
215 s = s.replace('"', '""')
216 return f'"{s}"'
217 else:
218 return s
219
220
221@functools.lru_cache(maxsize=1)

Callers 15

update_environmentFunction · 0.85
createMethod · 0.85
createMethod · 0.85
runMethod · 0.85
createMethod · 0.85
createMethod · 0.85
runMethod · 0.85
createMethod · 0.85
runMethod · 0.85
__str__Method · 0.85
__str__Method · 0.85
__str__Method · 0.85

Calls 5

is_alphaFunction · 0.85
is_alphanumFunction · 0.85
keywordsFunction · 0.70
upperMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected