airflow
airflow copied to clipboard
MsSqlHook: implement _generate_insert_sql
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.
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
@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.
@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.
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.
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 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.
@dstandish Would you be able to advise me further here
i'll review today
@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?
Let’s see if tests can run now.
I merged main for #21221.
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.
tests /static checks failing
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.
Please add a test for this
Should this be good to go now?
I rebased it - let's see. @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.
Just to avoid this going stale as I think it could be helpful. @dstandish do you think some work is still required?
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.
Please add a test for this
Would you have a chance to revisit this PR? Thanks!
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
very sorry this slipped off the radar @jplauri. please do check out @eladkal's commant and i'll watch for the update
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.