airflow icon indicating copy to clipboard operation
airflow copied to clipboard

MsSqlHook: implement _generate_insert_sql

Open jplauri opened this issue 2 years ago • 21 comments

Related: https://github.com/apache/airflow/discussions/19583

For prepared SQL statements, MSSQL uses ? rather than %s for parameter markers. Currently, MsSqlHook does not provide an implementation of _generate_insert_sql used by insert_rows making it impossible to use for instance GenericTransfer with the said hooks.

As written, the PR implements _generate_insert_sql for MsSqlHook but in a limited way. That is, it will only allow us to do INSERTs (so when replace=False) but not UPSERTs (when replace=True). I would love to get some feedback and ideas for implementing the UPSERT even though it is not useful for my current use-case.

Note that MSSQL does not support anything like "REPLACE INTO" for UPSERT. Instead, there seem to be two alternatives:

  • Try to do an update first and check @@ROWCOUNT: in case nothing was updated, do an insert. This is explained here by Aaron Bertrand.

  • Use MERGE, many hits on StackOverflow such this one explaining the syntax.

It seems that Aaron B. argues for the pattern in the first link because of several issues with MERGE (see list of issues).

I spent a bit of time trying to implement either approach here, but got stuck after realizing it's not that straightforward to re-use parameter markers ? from values from pyodbc cursor.execute(sql, values). In any case, I was attempting to draw some inspiration from _generate_insert_sql of PostgresHook. Thus, this might need some work beyond just implementing _generate_insert_sql unless I'm missing something.


^ Add meaningful description above

Read the Pull Request Guidelines for more information. In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed. In case of a new dependency, check compliance with the ASF 3rd Party License Policy. In case of backwards incompatible changes please leave a note in UPDATING.md.

jplauri avatar Nov 15 '21 16:11 jplauri

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 (flake8, 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: [email protected] Slack: https://s.apache.org/airflow-slack

boring-cyborg[bot] avatar Nov 15 '21 16:11 boring-cyborg[bot]

@jplauri regarding your question about merge vs insert, i think merge is the right call. That list of issues is pretty ancient now. Merge was implemented in 2008 and by now, I think most of the meaningful kinks have been ironed out. I would bet that the rowcount approach would be slow since it limits you to row-by-row (where as with merge you could, e.g. load into a temp table and merge into target with deduping logic too)

But I don't think you need to tackle that in this PR. One thing at a time. Can do this limited case first.

dstandish avatar Nov 16 '21 17:11 dstandish

@jplauri regarding your question about merge vs insert, i think merge is the right call. That list of issues is pretty ancient now. Merge was implemented in 2008 and by now, I think most of the meaningful kinks have been ironed out. I would bet that the rowcount approach would be slow since it limits you to row-by-row (where as with merge you could, e.g. load into a temp table and merge into target with deduping logic too)

But I don't think you need to tackle that in this PR. One thing at a time. Can do this limited case first.

Thanks! And yes, I would be fine with this limited solution too. I mean, GenericTransfer is meant to be used for small datasets anyway as it works in-mem, so I can imagine there are cases where doing say truncate & insert all is just fine instead of replace. Perhaps there are other use cases where replace is useful, but anyway.

jplauri avatar Nov 16 '21 17:11 jplauri

I mean, GenericTransfer is meant to be used for small datasets anyway as it works in-mem

Yeah I wasn't aware of that, this is my first look at generic transfer.

so I can imagine there are cases where doing say truncate & insert all is just fine instead of replace

Yeah this is one of the challenges with trying to build an interface like this. Replace can mean "replace rows (i.e. when collision by PK)" or "replace table". And in the base implementation, it means by row. But you are talking by table. So there's ambiguity.

dstandish avatar Nov 16 '21 17:11 dstandish

Oh... _generate_insert_sql already is row-by-row. I assumed it is row-based. Yeah in that case def small datasets / limited use cases.

dstandish avatar Nov 16 '21 17:11 dstandish

@dstandish Would you be able to advise me further here, i.e., is there something more the PR should have? Should we just wait for a review/merge, or what's the process? I'm in no rush, but I believe this addition would be helpful for some people.

jplauri avatar Nov 25 '21 17:11 jplauri

@dstandish Would you be able to advise me further here

i'll review today

dstandish avatar Nov 30 '21 14:11 dstandish

@dstandish Not really sure if I got it right - I basically copied from TestPostgres with only superficial understanding. But intuitively it makes sense - running test_mssql_to_mssql will fail unless _generate_insert_sql is at least syntactically correct. What do you think?

jplauri avatar Dec 01 '21 18:12 jplauri

Let’s see if tests can run now.

uranusjr avatar Jan 14 '22 07:01 uranusjr

I merged main for #21221.

uranusjr avatar Jan 31 '22 08:01 uranusjr

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Apr 06 '22 00:04 github-actions[bot]

tests /static checks failing

potiuk avatar Apr 06 '22 16:04 potiuk

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar May 22 '22 00:05 github-actions[bot]

Please add a test for this

Should this be good to go now?

jplauri avatar May 22 '22 15:05 jplauri

I rebased it - let's see. @dstandish ?

potiuk avatar May 22 '22 22:05 potiuk

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Jul 24 '22 00:07 github-actions[bot]

Just to avoid this going stale as I think it could be helpful. @dstandish do you think some work is still required?

jplauri avatar Jul 25 '22 08:07 jplauri

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Sep 18 '22 00:09 github-actions[bot]

Please add a test for this

Would you have a chance to revisit this PR? Thanks!

jplauri avatar Sep 18 '22 07:09 jplauri

mm in Sqlite we removed _generate_insert_sql in the hook see https://github.com/apache/airflow/pull/25939 could same approch be relvant here? cc @alexandermalyga

eladkal avatar Sep 18 '22 07:09 eladkal

very sorry this slipped off the radar @jplauri. please do check out @eladkal's commant and i'll watch for the update

dstandish avatar Sep 19 '22 16:09 dstandish

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Nov 04 '22 00:11 github-actions[bot]