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

Fix Redshift unload query when select query contains single quotes #30300

Closed
Closed
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
12 changes: 11 additions & 1 deletion airflow/providers/amazon/aws/transfers/redshift_to_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""Transfers data from AWS Redshift into a S3 Bucket."""
from __future__ import annotations

import logging
from typing import TYPE_CHECKING, Iterable, Mapping, Sequence

from airflow.exceptions import AirflowException
Expand Down Expand Up @@ -143,8 +144,17 @@ def __init__(
def _build_unload_query(
self, credentials_block: str, select_query: str, s3_key: str, unload_options: str
) -> str:
# TODO: remove in provider 8.0.0
if "''" in select_query:
logging.warning(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you want to remove this check at the next major release? I dont understand. Users, at any point in time, can use this operator and specify a query with already double quoted strings

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1
Users may stumble on this in the future after you have removed the check.

Also is it possible the string contains some double quotes but not everywhere? Do we want to try handle these half working situations? This type of string sanitization code can often get very hairy very quick. Is there a thirdparty library we can use instead which specializes in this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont know any library doing that but that'd be cool. Otherwise, I am in favor of NOT managing these kind of use cases. These use cases (mix of quotes escaped and some unescaped) are not valid and users should fix it. The current implementation would not alter the string which, I think, is the correct behavior

"Skip adding escape characters for select query. This check will be removed soon, "
"consider deleting the '' and let Airflow add the escape characters"
)
escape = ""
else:
escape = "$$"
return f"""
UNLOAD ('{select_query}')
UNLOAD ({escape}\n{select_query}{escape}\n)
TO 's3://{self.s3_bucket}/{s3_key}'
credentials
'{credentials_block}'
Expand Down
38 changes: 38 additions & 0 deletions tests/providers/amazon/aws/transfers/test_redshift_to_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# under the License.
from __future__ import annotations

import io
from contextlib import redirect_stdout
from copy import deepcopy
from unittest import mock

Expand Down Expand Up @@ -395,3 +397,39 @@ def test_table_unloading_using_redshift_data_api(
)
# test sql arg
assert_equal_ignore_multiple_spaces(self, mock_rs.execute_statement.call_args[1]["Sql"], unload_query)

@pytest.mark.parametrize(
"select_query, expected_unload_query, warning",
[
("SELECT * FROM schema.table", "UNLOAD ($$\nSELECT * FROM schema.table$$\n)", False),
(
"SELECT * FROM schema.table WHERE foo='boo'",
"UNLOAD ($$\nSELECT * FROM schema.table WHERE foo='boo'$$\n)",
False,
),
(
"SELECT * FROM schema.table WHERE foo=''boo''",
"UNLOAD (\nSELECT * FROM schema.table WHERE foo=''boo''\n)",
True,
),
],
)
@mock.patch("boto3.session.Session")
def test_build_unload_query_single_quotes(
self, mock_session, select_query, expected_unload_query, warning
):
op = RedshiftToS3Operator(
s3_bucket="s3_bucket",
s3_key="s3_key",
select_query="select_query",
task_id="task_id",
dag=None,
)
credentials_block = build_credentials_block(mock_session.return_value)
with redirect_stdout(io.StringIO()) as stdout:
unload_query = op._build_unload_query(credentials_block, select_query, "s3_key", "HEADER")
stdout = stdout.getvalue()
assert expected_unload_query in unload_query
warning_msg = "Skip adding escape characters for select query. This check will be removed soon, "
"consider deleting the '' and let Airflow add the escape characters"
assert (warning_msg in stdout) == warning