FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix inline list parameter rendering for IN clauses by fatkobra · Pull Request #773 · databricks/databricks-sql-python · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 9 additions & 3 deletions src/databricks/sql/utils.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -542,16 +542,22 @@ class ParamEscaper:
_TIME_FORMAT = "%H:%M:%S.%f %z"
_DATETIME_FORMAT = "{} {}".format(_DATE_FORMAT, _TIME_FORMAT)

def escape_args(self, parameters):
def escape_args(self, parameters):
if isinstance(parameters, dict):
return {k: self.escape_item(v) for k, v in parameters.items()}
return {k: self.escape_inline_arg(v) for k, v in parameters.items()}
elif isinstance(parameters, (list, tuple)):
return tuple(self.escape_item(x) for x in parameters)
return tuple(self.escape_inline_arg(x) for x in parameters)
else:
raise exc.ProgrammingError(
"Unsupported param format: {}".format(parameters)
)

def escape_inline_arg(self, item):
if isinstance(item, (list, tuple)):
escaped_items = list(map(str, map(self.escape_item, item)))
return "(" + ",".join(escaped_items) + ")"
return self.escape_item(item)

def escape_number(self, item):
return item

Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_param_escaper.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ def test_only_bind_in_where_clause(self):
with pytest.raises(Exception):
inject_parameters(INPUT, pe.escape_args(args))

def test_in_clause_with_positional_list_param(self):
query = "SELECT * FROM table WHERE something IN %s"
args = ([1, 2, 3],)

rendered = inject_parameters(query, pe.escape_args(args))

assert rendered == "SELECT * FROM table WHERE something IN (1,2,3)"

def test_in_clause_with_named_list_param(self):
query = "SELECT * FROM table WHERE something IN %(ids)s"
args = {"ids": [1, 2, 3]}

rendered = inject_parameters(query, pe.escape_args(args))

assert rendered == "SELECT * FROM table WHERE something IN (1,2,3)"

class TestInlineToNativeTransformer(object):
@pytest.mark.parametrize(
Expand Down
Loading

Back | FazBrowse Home | New Git URL