Skip to content

Commit c94e3f5

Browse files
committed
Escape backslashes in array values
1 parent 5e61763 commit c94e3f5

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

target_postgres/sinks.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ def bulk_insert_records( # type: ignore[override]
179179
column.type.bind_processor(connection.dialect) or str for column in columns
180180
]
181181

182+
# Make translation table for escaping in array values.
183+
array_translate_table = str.maketrans(
184+
{
185+
'"': '\\""',
186+
"\\": "\\\\",
187+
}
188+
)
189+
182190
def process_column_value(data: Any, proc: Callable) -> str:
183191
# If the data is null, return an unquoted, empty value.
184192
# Unquoted is important here, for PostgreSQL to interpret as null.
@@ -197,10 +205,15 @@ def process_column_value(data: Any, proc: Callable) -> str:
197205
# If the value is a list (for ARRAY), escape double-quotes as \" and return
198206
# a quoted value in literal array format.
199207
if isinstance(value, list):
208+
self.logger.info(f"{value.__class__=}")
209+
self.logger.info(f"{value=}")
210+
200211
# for each member of value, escape double quotes as \".
201212
return (
202213
'"{'
203-
+ ",".join('""' + v.replace('"', r'\""') + '""' for v in value)
214+
+ ",".join(
215+
'""' + v.translate(array_translate_table) + '""' for v in value
216+
)
204217
+ '}"'
205218
)
206219

0 commit comments

Comments
 (0)