Skip to content

Commit 765fd78

Browse files
only remove null bytes
1 parent 6147f27 commit 765fd78

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

apps/bfd-pipeline/bfd-pipeline-idr/loader.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import logging
22
import os
3-
import string
43
from collections.abc import Iterator
54
from datetime import UTC, date, datetime
65

@@ -132,7 +131,7 @@ def load(
132131
with cur.copy(f"COPY {temp_table} ({cols_str}) FROM STDIN") as copy: # type: ignore
133132
for row in results:
134133
model_dump = row.model_dump()
135-
copy.write_row([_remove_non_printable(model_dump[k]) for k in insert_cols])
134+
copy.write_row([_remove_null_bytes(model_dump[k]) for k in insert_cols])
136135
copy_timer.stop()
137136

138137
if len(results) > 0:
@@ -206,10 +205,15 @@ def load(
206205
return data_loaded
207206

208207

209-
def _remove_non_printable(val: DbType) -> DbType:
210-
# Some IDR values have non-printable characters
208+
def _remove_null_bytes(val: DbType) -> DbType:
209+
# Some IDR strings have null bytes.
210+
# Postgres doesn't allow these in text fields.
211+
# We can't use a UTF-8 validator here since technically these are valid UTF-8
212+
# and we can't use string.printable because that only contains ASCII fields
213+
# so neither of those validation techniques will remove null bytes
214+
# and still allow other valid UTF-8 characters.
211215
if type(val) is str:
212-
return "".join(s for s in val if s in string.printable)
216+
return val.replace("\x00", "")
213217
return val
214218

215219

0 commit comments

Comments
 (0)