Skip to content

Commit

Permalink
Fix 26 - escaping colons (#27)
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Erlandson <[email protected]>
  • Loading branch information
erikerlandson authored Apr 20, 2022
1 parent 1fcc877 commit a90e466
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 3 additions & 0 deletions osc_ingest_trino/trino_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ def _sqlform(x):
if isinstance(x, str):
# escape any single quotes in the string
t = x.replace("'", "''")
# colons are mostly a problem for ':some_id_name', which is interpreted as
# a parameter requiring binding, but just escaping them all works
t = t.replace(":", "\\:")
# enclose string with single quotes
return f"'{t}'"
if isinstance(x, datetime):
Expand Down
7 changes: 5 additions & 2 deletions tests/test_trino_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_trino_batch_insert():
cxn = mock.MagicMock()
# tuple data, in form supplied to __call__ as specified in 'method' param docs:
# https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_sql.html
rows = [("a", 4.5), ("b'c", math.nan), (None, math.inf), ("d", -math.inf), ("e", datetime(2022, 1, 1))]
rows = [("a", 4.5), ("b'c", math.nan), (None, math.inf), ("d", -math.inf), ("e", datetime(2022, 1, 1)), (":f", 1.0)]
# invoke the __call__ method, simulating df.to_sql call
tbi = TrinoBatchInsert(catalog="test", schema="test", batch_size=2, verbose=True)
tbi(tbl, cxn, [], rows)
Expand All @@ -42,4 +42,7 @@ def test_trino_batch_insert():
xcalls = cxn.execute.call_args_list
assert xcalls[0].args[0].text == "insert into test.test.test values\n('a', 4.5),\n('b''c', nan())"
assert xcalls[1].args[0].text == "insert into test.test.test values\n(NULL, infinity()),\n('d', -infinity())"
assert xcalls[2].args[0].text == "insert into test.test.test values\n('e', TIMESTAMP '2022-01-01 00:00:00')"
assert (
xcalls[2].args[0].text
== "insert into test.test.test values\n('e', TIMESTAMP '2022-01-01 00:00:00'),\n('\\:f', 1.0)"
)

0 comments on commit a90e466

Please sign in to comment.