Skip to content

Commit

Permalink
Escape backslashes in array values
Browse files Browse the repository at this point in the history
  • Loading branch information
kinghuang committed Jun 28, 2024
1 parent 20cdb55 commit 534a38a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion target_postgres/sinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ def bulk_insert_records( # type: ignore[override]
column.type.bind_processor(connection.dialect) or str for column in columns
]

# Make translation table for escaping in array values.
array_translate_table = str.maketrans(
{
'"': '\\""',
"\\": "\\\\",
}
)

def process_column_value(data: Any, proc: Callable) -> str:
# If the data is null, return an unquoted, empty value.
# Unquoted is important here, for PostgreSQL to interpret as null.
Expand All @@ -200,7 +208,9 @@ def process_column_value(data: Any, proc: Callable) -> str:
# for each member of value, escape double quotes as \".
return (
'"{'
+ ",".join('""' + v.replace('"', r'\""') + '""' for v in value)
+ ",".join(
'""' + v.translate(array_translate_table) + '""' for v in value
)
+ '}"'
)

Expand Down

0 comments on commit 534a38a

Please sign in to comment.