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 5e61763 commit c94e3f5
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 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 @@ -197,10 +205,15 @@ def process_column_value(data: Any, proc: Callable) -> str:
# If the value is a list (for ARRAY), escape double-quotes as \" and return
# a quoted value in literal array format.
if isinstance(value, list):
self.logger.info(f"{value.__class__=}")
self.logger.info(f"{value=}")

# 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 c94e3f5

Please sign in to comment.