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

[Feature] Adding WHERE codintion during MERGE update #56

Open
philiphagglund opened this issue Nov 29, 2022 · 4 comments
Open

[Feature] Adding WHERE codintion during MERGE update #56

philiphagglund opened this issue Nov 29, 2022 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@philiphagglund
Copy link

philiphagglund commented Nov 29, 2022

Hi,

It would be nice to be able to controll the incremental merge strategy by adding a WHERE clause to the WHEN MATCHED THEN part of the merge statement. As i see on dbt-oracle github it seems the adapter doesn't support it.

ON (src.id = tgt.id AND src.date = tgt.date)
WHEN MATCHED THEN
    UPDATE SET
        tgt.end_date = src.end_date
    WHERE
        src.end_date <> tgt.end_date
WHEN NOT MATCHED THEN
    INSERT (tgt.id, tgt.date, tgt.end_date)
    VALUES (src.id, src.date, src.end_date)

I got recommended by #db-oracle on dbt slack to raise a feature request so it gets tracked.

Describe alternatives you've considered

This is the repons i got from #db-oracle slack channel from @aosingh:

dbt-oracle implements the functionality conforming to the standard defined by dbt.
However, I see that you need support for an arbitrary where clause condition during MERGE update. We will need to think about the interface to implement this. One way could be to introduce a dbt-oracle specific parameter. The challenge is to make this a generic config which can work for any where condition. The where condition can be on the target data or source data or both.
You can also raise a feature request here - https://github.com/oracle/dbt-oracle/issues so that this gets tracked

Who will this benefit?

No response

Anything else?

No response

@philiphagglund philiphagglund added the enhancement New feature or request label Nov 29, 2022
@aosingh aosingh self-assigned this Nov 29, 2022
@aosingh
Copy link
Member

aosingh commented Feb 10, 2023

@philiphagglund

We are working on dbt-oracle 1.4 release which will support incremental predicates

We will support incremental predicates in dbt-oracle conforming to rules defined by dbt Labs.

I think, this will solve your feature request. Please read about it and I will let you know when the first cut is released on PyPI.

Let me know if you have any questions

@aosingh
Copy link
Member

aosingh commented Mar 9, 2023

@philiphagglund

Did you try incremental predicates ? Does it solve your usecase.

@philiphagglund
Copy link
Author

Hi @aosingh

Unfortunately is this not going to solve the usecase i have. I have now tried the new incremental predicates functionality and it works fine, except is doesn't accept a column in the ON statement to be updated:

[0m07:00:02.502344 [info ] [Thread-1 (]: oracle adapter: Oracle error: ORA-38104: Columns referenced in the ON Clause cannot be updated: "DBT_INTERNAL_DEST"."END_DATE"

Below is the query generated by DBT using incremental predicates.

yaml:

version: 2

models:
  - name: tst
    config:
      materialized: incremental
      tags: cas
      unique_key: ['id', 'date_key']
      incremental_strategy: merge
      incremental_predicates: ["DBT_INTERNAL_SOURCE.end_date<> DBT_INTERNAL_DEST.end_date"]

Sql:

ON (src.id = tgt.id AND src.date = tgt.date AND DBT_INTERNAL_SOURCE.end_date <> DBT_INTERNAL_DEST.end_date )
WHEN MATCHED THEN
    UPDATE SET
        tgt.end_date = src.end_date
WHEN NOT MATCHED THEN
    INSERT (tgt.id, tgt.date, tgt.end_date)
    VALUES (src.id, src.date, src.end_date)

The result I was expecting would be:

ON (src.id = tgt.id AND src.date = tgt.date)
WHEN MATCHED THEN
    UPDATE SET
        tgt.end_date = src.end_date
    **WHERE**
        **DBT_INTERNAL_SOURCE.end_date <> DBT_INTERNAL_DEST.end_date**
WHEN NOT MATCHED THEN
    INSERT (tgt.id, tgt.date, tgt.end_date)
    VALUES (src.id, src.date, src.end_date)

@aosingh
Copy link
Member

aosingh commented Jun 28, 2023

@philiphagglund understood the problem. I guess i need to think about how to support this conditional update clause during MERGE. Probably, a parameter which can be supplied by the user during incremental materialization config.

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

No branches or pull requests

2 participants