Skip to content

Commit 6f2c308

Browse files
Escape bytes parameters as strings, not as ARRAY of ints
With inline parameters, ParamEscaper.escape_item checked for Sequence before bytes. Since bytes is a Sequence, a bytes value was escaped element by element (b"hi" became ARRAY(104,105)), and the bytes handling in escape_string, which decodes UTF-8 for older SQLAlchemy, was never reached. Route bytes to escape_string. Signed-off-by: Shubham Padkonde <shubhampadkonde12@gmail.com>
1 parent f0ef715 commit 6f2c308

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎src/databricks/sql/utils.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,8 @@ def escape_item(self, item):
603603
return "NULL"
604604
elif isinstance(item, (int, float)):
605605
return self.escape_number(item)
606-
elif isinstance(item, str):
606+
elif isinstance(item, (str, bytes)):
607+
# bytes are a Sequence too, but escape_string decodes them as a string
607608
return self.escape_string(item)
608609
elif isinstance(item, datetime.datetime):
609610
return self.escape_datetime(item, self._DATETIME_FORMAT)

‎tests/unit/test_param_escaper.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ def test_escape_date(self):
119119
OUTPUT = "'1991-08-03'"
120120
assert pe.escape_datetime(INPUT, FORMAT) == OUTPUT
121121

122+
def test_escape_item_bytes_as_string(self):
123+
"""bytes are escaped like the equivalent str, not as an ARRAY of ints"""
124+
assert pe.escape_item(b"golly bob howdy") == "'golly bob howdy'"
125+
assert pe.escape_item("golly bob howdy".encode("utf-8")) == pe.escape_item(
126+
"golly bob howdy"
127+
)
128+
assert pe.escape_item([b"his", b"name"]) == "ARRAY('his','name')"
129+
122130
def test_escape_sequence_integer(self):
123131
assert pe.escape_sequence([1, 2, 3, 4]) == "ARRAY(1,2,3,4)"
124132

0 commit comments

Comments
 (0)