Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize query template input in sqlite task #1359

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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