Skip to content

Commit

Permalink
Sanitize query template input in sqlite task (#1359)
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Apolinario <[email protected]>

Signed-off-by: Eduardo Apolinario <[email protected]>
Co-authored-by: Eduardo Apolinario <[email protected]>
  • Loading branch information
eapolinario and eapolinario authored Dec 5, 2022
1 parent 7994f47 commit bc29749
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions flytekit/extras/sqlite3/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ def __init__(
outputs=outputs,
**kwargs,
)
# Sanitize query by removing the newlines at the end of the query. Keep in mind
# that the query can be a multiline string.
self._query_template = query_template.replace("\n", " ")

@property
def output_columns(self) -> typing.Optional[typing.List[str]]:
Expand Down
34 changes: 34 additions & 0 deletions tests/flytekit/unit/extras/sqlite3/test_task.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pandas
import pytest

from flytekit import kwtypes, task, workflow
from flytekit.configuration import DefaultImages
Expand Down Expand Up @@ -108,3 +109,36 @@ def test_task_serialization():
sql_task._container_image = image
tt = sql_task.serialize_to_model(sql_task.SERIALIZE_SETTINGS)
assert tt.container.image == image


@pytest.mark.parametrize(
"query_template, expected_query",
[
(
"""
select *
from tracks
limit {{.inputs.limit}}""",
" select * from tracks limit {{.inputs.limit}}",
),
(
""" \
select * \
from tracks \
limit {{.inputs.limit}}""",
" select * from tracks limit {{.inputs.limit}}",
),
("select * from abc", "select * from abc"),
],
)
def test_query_sanitization(query_template, expected_query):
sql_task = SQLite3Task(
"test",
query_template=query_template,
inputs=kwtypes(limit=int),
task_config=SQLite3Config(
uri=EXAMPLE_DB,
compressed=True,
),
)
assert sql_task.query_template == expected_query

0 comments on commit bc29749

Please sign in to comment.