Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(hogql): fix postgres identifier roundtrip PBT for '%' (#65517)

Robbie62f0e421a7ea
diff --git a/posthog/hogql/printer/test/test_printer_pbt.py b/posthog/hogql/printer/test/test_printer_pbt.py
index 1fc68724a2ac..ad8b9955b66c 100644
--- a/posthog/hogql/printer/test/test_printer_pbt.py
+++ b/posthog/hogql/printer/test/test_printer_pbt.py
@@ -191,9 +191,11 @@ class TestPostgresIdentifier:
unescaping that Postgres identifiers don't use).
"""
- @given(s=st.text(min_size=1, max_size=63))
+ @given(s=st.text(alphabet=st.characters(codec="utf-8", blacklist_characters="%"), min_size=1, max_size=63))
@settings(parent=_BASE, max_examples=1000)
def test_roundtrip(self, s: str) -> None:
+ # Exclude "%" (escape_postgres_identifier rejects it; covered by test_percent_always_rejected).
+ # codec="utf-8" matches the default st.text() alphabet so we don't generate lone surrogates.
escaped = escape_postgres_identifier(s)
if escaped.startswith('"'):
# Double-quoted: strip quotes and unescape "" → "
@@ -202,6 +204,18 @@ class TestPostgresIdentifier:
else:
assert escaped == s
+ @given(
+ s=st.builds(
+ # Keep total length ≤ 63 so inputs clear the length check (len > 63) and reach the '%' rejection.
+ lambda prefix, suffix: prefix + "%" + suffix,
+ prefix=st.text(max_size=31),
+ suffix=st.text(max_size=31),
+ )
+ )
+ def test_percent_always_rejected(self, s: str) -> None:
+ with pytest.raises(QueryError, match='is not permitted as it contains the "%" character'):
+ escape_postgres_identifier(s)
+
@given(s=st.from_regex(r"[a-z_][a-z0-9_$]*", fullmatch=True).filter(lambda s: len(s) <= 63))
def test_simple_identifiers_returned_bare(self, s: str) -> None:
assume(s.upper() not in POSTGRES_RESERVED_KEYWORDS)