-
Notifications
You must be signed in to change notification settings - Fork 14.3k
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
Allow json_result_force_utf8_encoding
specification in providers.snowflake.hooks.SnowflakeHook
extra dict
#44264
Open
ttzhou
wants to merge
7
commits into
apache:main
Choose a base branch
from
ttzhou:ttzhou/allow-force-json-default-encoding-snowflake-connector-argument
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+38
−1
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fbd76ac
Allow json_result_force_utf8_encoding specification in SnowflakeHook …
ttzhou c0f5434
Merge branch 'main' into ttzhou/allow-force-json-default-encoding-sno…
ttzhou fd67837
Use a set for the not in
ttzhou 27ac5bb
Merge branch 'main' into ttzhou/allow-force-json-default-encoding-sno…
ttzhou 02c79cd
Merge branch 'main' into ttzhou/allow-force-json-default-encoding-sno…
ttzhou 2cea900
Merge branch 'main' into ttzhou/allow-force-json-default-encoding-sno…
ttzhou 34904d9
Merge branch 'main' into ttzhou/allow-force-json-default-encoding-sno…
ttzhou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -200,6 +200,9 @@ def _get_conn_params(self) -> dict[str, str | None]: | |
region = self._get_field(extra_dict, "region") or "" | ||
role = self._get_field(extra_dict, "role") or "" | ||
insecure_mode = _try_to_boolean(self._get_field(extra_dict, "insecure_mode")) | ||
json_result_force_utf8_decoding = _try_to_boolean( | ||
self._get_field(extra_dict, "json_result_force_utf8_decoding") | ||
) | ||
schema = conn.schema or "" | ||
client_request_mfa_token = _try_to_boolean(self._get_field(extra_dict, "client_request_mfa_token")) | ||
|
||
|
@@ -224,6 +227,9 @@ def _get_conn_params(self) -> dict[str, str | None]: | |
if insecure_mode: | ||
conn_config["insecure_mode"] = insecure_mode | ||
|
||
if json_result_force_utf8_decoding: | ||
conn_config["json_result_force_utf8_decoding"] = json_result_force_utf8_decoding | ||
|
||
if client_request_mfa_token: | ||
conn_config["client_request_mfa_token"] = client_request_mfa_token | ||
|
||
|
@@ -301,7 +307,13 @@ def _conn_params_to_sqlalchemy_uri(self, conn_params: dict) -> str: | |
for k, v in conn_params.items() | ||
if v | ||
and k | ||
not in ["session_parameters", "insecure_mode", "private_key", "client_request_mfa_token"] | ||
not in { | ||
"session_parameters", | ||
"insecure_mode", | ||
"private_key", | ||
"client_request_mfa_token", | ||
"json_result_force_utf8_decoding", | ||
} | ||
} | ||
) | ||
|
||
|
@@ -323,6 +335,9 @@ def get_sqlalchemy_engine(self, engine_kwargs=None): | |
if "insecure_mode" in conn_params: | ||
engine_kwargs.setdefault("connect_args", {}) | ||
engine_kwargs["connect_args"]["insecure_mode"] = True | ||
if "json_result_force_utf8_decoding" in conn_params: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about lumping this together into the same branch on line 335 above, but wanted to reduce diff count for now. Happy to make that change as well if you prefer, since it's the same type of value. |
||
engine_kwargs.setdefault("connect_args", {}) | ||
engine_kwargs["connect_args"]["json_result_force_utf8_decoding"] = True | ||
for key in ["session_parameters", "private_key"]: | ||
if conn_params.get(key): | ||
engine_kwargs.setdefault("connect_args", {}) | ||
|
This file contains 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 |
---|---|---|
|
@@ -138,6 +138,7 @@ class TestPytestSnowflakeHook: | |
"extra__snowflake__region": "af_region", | ||
"extra__snowflake__role": "af_role", | ||
"extra__snowflake__insecure_mode": "True", | ||
"extra__snowflake__json_result_force_utf8_decoding": "True", | ||
"extra__snowflake__client_request_mfa_token": "True", | ||
}, | ||
}, | ||
|
@@ -158,6 +159,7 @@ class TestPytestSnowflakeHook: | |
"user": "user", | ||
"warehouse": "af_wh", | ||
"insecure_mode": True, | ||
"json_result_force_utf8_decoding": True, | ||
"client_request_mfa_token": True, | ||
}, | ||
), | ||
|
@@ -171,6 +173,7 @@ class TestPytestSnowflakeHook: | |
"extra__snowflake__region": "af_region", | ||
"extra__snowflake__role": "af_role", | ||
"extra__snowflake__insecure_mode": "False", | ||
"extra__snowflake__json_result_force_utf8_decoding": "False", | ||
"extra__snowflake__client_request_mfa_token": "False", | ||
}, | ||
}, | ||
|
@@ -247,6 +250,7 @@ class TestPytestSnowflakeHook: | |
"extra": { | ||
**BASE_CONNECTION_KWARGS["extra"], | ||
"extra__snowflake__insecure_mode": False, | ||
"extra__snowflake__json_result_force_utf8_decoding": True, | ||
"extra__snowflake__client_request_mfa_token": False, | ||
}, | ||
}, | ||
|
@@ -266,6 +270,7 @@ class TestPytestSnowflakeHook: | |
"session_parameters": None, | ||
"user": "user", | ||
"warehouse": "af_wh", | ||
"json_result_force_utf8_decoding": True, | ||
}, | ||
), | ||
], | ||
|
@@ -473,6 +478,23 @@ def test_get_sqlalchemy_engine_should_support_insecure_mode(self): | |
) | ||
assert mock_create_engine.return_value == conn | ||
|
||
def test_get_sqlalchemy_engine_should_support_json_result_force_utf8_decoding(self): | ||
connection_kwargs = deepcopy(BASE_CONNECTION_KWARGS) | ||
connection_kwargs["extra"]["extra__snowflake__json_result_force_utf8_decoding"] = "True" | ||
|
||
with ( | ||
mock.patch.dict("os.environ", AIRFLOW_CONN_TEST_CONN=Connection(**connection_kwargs).get_uri()), | ||
mock.patch("airflow.providers.snowflake.hooks.snowflake.create_engine") as mock_create_engine, | ||
): | ||
hook = SnowflakeHook(snowflake_conn_id="test_conn") | ||
conn = hook.get_sqlalchemy_engine() | ||
mock_create_engine.assert_called_once_with( | ||
"snowflake://user:[email protected]_region/db/public" | ||
"?application=AIRFLOW&authenticator=snowflake&role=af_role&warehouse=af_wh", | ||
connect_args={"json_result_force_utf8_decoding": True}, | ||
) | ||
assert mock_create_engine.return_value == conn | ||
|
||
def test_get_sqlalchemy_engine_should_support_session_parameters(self): | ||
connection_kwargs = deepcopy(BASE_CONNECTION_KWARGS) | ||
connection_kwargs["extra"]["session_parameters"] = {"TEST_PARAM": "AA", "TEST_PARAM_B": 123} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed to a set; we're not iterating, may as well make it O(1)