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

Impovements for RedshiftDataOperator #29434

Merged
merged 1 commit into from
Mar 2, 2023

Conversation

LMnet
Copy link
Contributor

@LMnet LMnet commented Feb 9, 2023

I added two improvements to the RedshiftDataOperator:

  1. Better error reporting. Before my changes, all you get in case of an error is just a status (FAILURE). It's not very helpful. I added the whole response in the exception, so it will be printed in the logs and it will give a possibility to troubleshoot problems easily.
  2. I added a return_sql_result optional parameter. By default it's False, so it's a backward-compatible change. If it's True operator will return a result of a SQL query from the execute method. It means that this result will be available through xcom variables. It may be handy if you need to get some small portions of data (or metadata) from Redshift. For example, I'm running show table query to get a DDL of some table and create a temporary copy of it.

@boring-cyborg boring-cyborg bot added area:providers provider:amazon-aws AWS/Amazon - related issues labels Feb 9, 2023
@boring-cyborg
Copy link

boring-cyborg bot commented Feb 9, 2023

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst)
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our pre-commits will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: dev@airflow.apache.org
    Slack: https://s.apache.org/airflow-slack

Copy link
Contributor

@ephraimbuddy ephraimbuddy left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 51 to 52
:param return_sql_result: if True will return the result of an SQL statement,
if False will return statement ID
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add that the default is False?

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@vincbeck vincbeck left a comment

Choose a reason for hiding this comment

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

Besides the comment left by @ephraimbuddy, LGTM

Copy link
Contributor

@Taragolis Taragolis left a comment

Choose a reason for hiding this comment

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

I would recommend to use pre-commit to fix most of the statics checks failures, see: Static code checks

Comment on lines 29 to 26
from mypy_boto3_redshift_data.type_defs import GetStatementResultResponseTypeDef

Copy link
Contributor

Choose a reason for hiding this comment

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

Please move it under the if TYPE_CHECKING: block

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@mock.patch("airflow.providers.amazon.aws.hooks.redshift_data.RedshiftDataHook.conn")
def test_return_sql_result(self, mock_conn):
expected_result = {"Result": True}
mock_conn.execute_statement.return_value = {"Id": STATEMENT_ID}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
mock_conn.execute_statement.return_value = {"Id": STATEMENT_ID}
mock_conn.batch_execute_statement.return_value = {"Id": STATEMENT_ID}

You need to mock batch_execute_statement here because you call

def execute_batch_query(self):
kwargs: dict[str, Any] = {
"ClusterIdentifier": self.cluster_identifier,
"Database": self.database,
"Sqls": self.sql,
"DbUser": self.db_user,
"Parameters": self.parameters,
"WithEvent": self.with_event,
"SecretArn": self.secret_arn,
"StatementName": self.statement_name,
}
resp = self.hook.conn.batch_execute_statement(**trim_none_values(kwargs))
return resp["Id"]

You could check it locally in Breeze or venv, for more detail see: Airflow Unit Tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Actually, I needed execute_statement, not batch_execute_statement. Looks like it was a copy-paste problem.

@LMnet
Copy link
Contributor Author

LMnet commented Feb 24, 2023

I rebased and squashed my changes.

@o-nikolas
Copy link
Contributor

Looks like tests are passing and changes have been made. @Taragolis your requested changes are blocking the merge. Does everything look acceptable from your end?

@Taragolis Taragolis dismissed their stale review March 2, 2023 09:34

Non-relevant anymore

@Taragolis Taragolis merged commit ea8ce21 into apache:main Mar 2, 2023
@boring-cyborg
Copy link

boring-cyborg bot commented Mar 2, 2023

Awesome work, congrats on your first merged pull request!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:providers provider:amazon-aws AWS/Amazon - related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants