Skip to content

Commit

Permalink
chore: slight perf improvments for pg_arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas ESTRADA committed Jan 15, 2025
1 parent d377423 commit acdf446
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions sources/pg_legacy_replication/schema_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,30 +183,26 @@ def _to_dlt_val(
return data_type_handlers[data_type](raw_value)

raw_type = _DATUM_RAW_TYPES[datum]
if _is_scalar_pg_array(data_type, raw_type, raw_value):
raw_type, raw_value = "text", _pg_array_to_json_str(raw_value)
if raw_type == "binary" and _is_scalar_pg_array(data_type, raw_value):
raw_type = "text"
raw_value = _pg_array_to_json_str(raw_value)

return coerce_value(data_type, raw_type, raw_value)


def _is_scalar_pg_array(
data_type: TDataType, raw_type: TDataType, raw_value: Any
) -> bool:
def _is_scalar_pg_array(data_type: TDataType, raw_value: bytes) -> bool:
return (
data_type == "json"
and raw_type == "binary"
and raw_value.startswith(b"{")
and raw_value.endswith(b"}")
len(raw_value) > 1
and data_type == "json"
and raw_value[0] == ord("{")
and raw_value[-1] == ord("}")
)


def _pg_array_to_json_str(raw_value: bytes) -> str:
"""
Decode the byte string to a regular string and strip the curly braces
"""
content = raw_value[1:-1].decode()
csv = ",".join(
f'"{element}"' if element.isalpha() else element
for element in content.split(",")
)
without_braces = raw_value[1:-1].decode()
csv = ",".join(f'"{x}"' if x.isalpha() else x for x in without_braces.split(","))
return f"[{csv}]"

0 comments on commit acdf446

Please sign in to comment.