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

[AIRFLOW-3905] Allow using "parameters" in SqlSensor #4723

Merged
merged 2 commits into from
Feb 21, 2019

Conversation

XD-DENG
Copy link
Member

@XD-DENG XD-DENG commented Feb 17, 2019

Jira

Description

In most SQL-related operators/sensors, argument parameters is available to help render SQL command conveniently, like MySqlOperator (https://github.com/apache/airflow/blob/master/airflow/operators/mysql_operator.py#L25) and PostgresOperator (https://github.com/apache/airflow/blob/master/airflow/operators/postgres_operator.py#L24).

But this is not available in SqlSensor yet.

Tests

  • My PR adds the following unit tests OR does not need testing for this extremely good reason:

Tests passed.

Commits

  • My commits all reference Jira issues in their subject lines, and I have squashed multiple commits if they address the same issue. In addition, my commits follow the guidelines from "How to write a good git commit message":
    1. Subject is separated from body by a blank line
    2. Subject is limited to 50 characters (not including Jira issue reference)
    3. Subject does not end with a period
    4. Subject uses the imperative mood ("add", not "adding")
    5. Body wraps at 72 characters
    6. Body explains "what" and "why", not "how"

Documentation

  • In case of new functionality, my PR adds documentation that describes how to use it.
    • When adding new operators/hooks/sensors, the autoclass documentation generation needs to be added.
    • All the public functions and the classes in the PR contain docstrings that explain what it does

Code Quality

  • Passes flake8

@codecov-io
Copy link

codecov-io commented Feb 17, 2019

Codecov Report

Merging #4723 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4723      +/-   ##
==========================================
+ Coverage   74.65%   74.65%   +<.01%     
==========================================
  Files         430      430              
  Lines       27990    27992       +2     
==========================================
+ Hits        20896    20898       +2     
  Misses       7094     7094
Impacted Files Coverage Δ
airflow/sensors/sql_sensor.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b71a4b6...c63f008. Read the comment docs.

@XD-DENG XD-DENG force-pushed the AIRFLOW-3905 branch 2 times, most recently from d607f52 to 472310c Compare February 18, 2019 00:01
Copy link
Member

@zhongjiajie zhongjiajie left a comment

Choose a reason for hiding this comment

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

Please find my two cents.

airflow/sensors/sql_sensor.py Show resolved Hide resolved
@XD-DENG
Copy link
Member Author

XD-DENG commented Feb 18, 2019

@feng-tao @ashb PTAL. Thanks.

self.log.info('Poking: %s', self.sql)
records = hook.get_records(self.sql)
self.log.info('Poking: %s (with parameters %s)', self.sql, self.parameters)
records = hook.get_records(self.sql, self.parameters)
Copy link
Member

Choose a reason for hiding this comment

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

Do all the sql type hooks(druid, mysql, postgres etc) are all inherited from dbapi hook? If that's case, the change LGTM

Copy link
Member

Choose a reason for hiding this comment

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

If not, we should change that hook to inherit from dbapi hook.

Copy link
Member Author

@XD-DENG XD-DENG Feb 18, 2019

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

But I checkCloudSqlDatabaseHook (

class CloudSqlDatabaseHook(BaseHook):
) which doesn't seem to inherit from dbapi hook.

But the I assume the sqlSensor could use CloudSqlDatabasehook? Given there are many connection hooks defined, a safer approach would be check if the hook is an instance of dbapi hook, use the records = hook.get_records(self.sql, self.parameters), otherwise fall back to records = hook.get_records(self.sql). what you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually SqlSensor can not support CloudSqlDatabaseHook.

Eventually SqlSensor uses get_records method to retrieve the records from DB, while get_records method is not available in CloudSqlDatabaseHook at all.

Copy link
Member

Choose a reason for hiding this comment

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

I see, but would a check on hook type be safer?

Copy link
Member Author

Choose a reason for hiding this comment

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

Get what you mean.

Given the limitation in https://github.com/apache/airflow/blob/master/airflow/models/connection.py#L184 and the implementation of each hook, only the connection types below are supported by SqlSensor:

  • 'google_cloud_platform'
  • 'jdbc'
  • 'mssql'
  • 'mysql'
  • 'oracle'
  • 'postgres'
  • 'presto'
  • 'sqlite'
  • 'vertica'

I will add a check.

@XD-DENG XD-DENG changed the title [AIRFLOW-3905] Allow using "parameters" in SqlSensor [WIP][AIRFLOW-3905] Allow using "parameters" in SqlSensor Feb 20, 2019
@XD-DENG XD-DENG changed the title [WIP][AIRFLOW-3905] Allow using "parameters" in SqlSensor [AIRFLOW-3905] Allow using "parameters" in SqlSensor Feb 20, 2019
Not all SQL-related connections are supported by SqlSensor,
due to limitation in Connection model and hook implementation.
@feng-tao
Copy link
Member

LGTM

@feng-tao feng-tao merged commit c7d6c1a into apache:master Feb 21, 2019
@XD-DENG
Copy link
Member Author

XD-DENG commented Feb 21, 2019

Thanks @feng-tao

@XD-DENG XD-DENG deleted the AIRFLOW-3905 branch February 21, 2019 01:09
antonimaciej pushed a commit to PolideaInternal/airflow that referenced this pull request Feb 26, 2019
* [AIRFLOW-3905] Allow 'parameters' in SqlSensor

* Add check on conn_type & add test

Not all SQL-related connections are supported by SqlSensor,
due to limitation in Connection model and hook implementation.
ashb pushed a commit that referenced this pull request Mar 7, 2019
* [AIRFLOW-3905] Allow 'parameters' in SqlSensor

* Add check on conn_type & add test

Not all SQL-related connections are supported by SqlSensor,
due to limitation in Connection model and hook implementation.
wmorris75 pushed a commit to modmed/incubator-airflow that referenced this pull request Jul 29, 2019
* [AIRFLOW-3905] Allow 'parameters' in SqlSensor

* Add check on conn_type & add test

Not all SQL-related connections are supported by SqlSensor,
due to limitation in Connection model and hook implementation.
@pingzh
Copy link
Contributor

pingzh commented Aug 22, 2019

@XD-DENG @feng-tao it breaks the MetastorePartitionSensor

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/airflow/models/taskinstance.py", line 913, in _run_raw_task
    result = task_copy.execute(context=context)
  File "/usr/local/lib/python2.7/dist-packages/airflow/sensors/base_sensor_operator.py", line 107, in execute
    while not self.poke(context):
  File "/usr/local/lib/python2.7/dist-packages/airflow/sensors/metastore_partition_sensor.py", line 83, in poke
    return super(MetastorePartitionSensor, self).poke(context)
  File "/usr/local/lib/python2.7/dist-packages/airflow/sensors/sql_sensor.py", line 82, in poke
    self.log.info('Poking: %s (with parameters %s)', self.sql, self.parameters)
AttributeError: 'MetastorePartitionSensor' object has no attribute 'parameters

As thecontructor of the sqlSensor is not called in the MetastorePartitionSensor

@malthe
Copy link
Contributor

malthe commented Jun 10, 2022

Shouldn't the parameters be templated?

Or perhaps templated_parameters due to the native vs string templating issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants