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

Allow multiple materialized views to write to same target (#280) #364

Conversation

the4thamigo-uk
Copy link
Contributor

@the4thamigo-uk the4thamigo-uk commented Oct 8, 2024

Summary

Proposed mechanism to support having multiple materialized views pointing to the same target table.

In brief, we add tags to mark blocks of the sql which will form each of the materialized views to be created.

i.e. https://github.com/ClickHouse/dbt-clickhouse/pull/364/files#diff-b1cddf2e5c9b3d08ea2276e5cef4944b06b9744a4076c5970ff5a5edb0140f08R62.

The entire union of all such blocks is used to backfill the target table.

Checklist

Delete items not relevant to your PR:

  • Unit and integration tests covering the common scenarios were added
  • A human-readable description of the changes was provided to include in CHANGELOG
  • For significant changes, documentation in https://github.com/ClickHouse/clickhouse-docs was updated with further explanations or tutorials

@the4thamigo-uk
Copy link
Contributor Author

issue #280

@the4thamigo-uk the4thamigo-uk force-pushed the multiple-materialized-views_to_same_target branch from e4e9b6d to f932efb Compare October 8, 2024 13:14
@BentsiLeviav
Copy link
Contributor

Hi @the4thamigo-uk

Thank you for your contribution!
Before reviewing your PR, it is required to add a short description with a PR link to the changelog (please keep the current format we have in the Changelog file).
With this kind of feature, I think it is also important to add a note in the documentation (readme file).

looking forward to reviewing this

@the4thamigo-uk the4thamigo-uk force-pushed the multiple-materialized-views_to_same_target branch from f932efb to 798aca9 Compare November 13, 2024 09:25
the4thamigo-uk added a commit to the4thamigo-uk/dbt-clickhouse that referenced this pull request Nov 13, 2024
the4thamigo-uk added a commit to the4thamigo-uk/dbt-clickhouse that referenced this pull request Nov 13, 2024
@the4thamigo-uk the4thamigo-uk force-pushed the multiple-materialized-views_to_same_target branch from 3485793 to e661bfa Compare November 13, 2024 09:45
the4thamigo-uk added a commit to the4thamigo-uk/dbt-clickhouse that referenced this pull request Nov 13, 2024
@the4thamigo-uk the4thamigo-uk force-pushed the multiple-materialized-views_to_same_target branch from e661bfa to 277fa45 Compare November 13, 2024 09:46
@the4thamigo-uk the4thamigo-uk force-pushed the multiple-materialized-views_to_same_target branch from 277fa45 to 064ed53 Compare November 13, 2024 09:47
@the4thamigo-uk
Copy link
Contributor Author

@BentsiLeviav Ok, Ive done this.

"hackers.sql": MV_MODEL,
}

def test_create(self, project):
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also test for updates and full refresh?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I think if you change the model to add or rename MVs, then old ones will be left running, which is incorrect.

@BentsiLeviav
Copy link
Contributor

Thanks @the4thamigo-uk
We appreciate this contribution!
The code LGTM. I left a small comment regarding the tests.

@BentsiLeviav
Copy link
Contributor

I’ve added the tests through your branch.

Regarding the potential issue of unremoved materialized views (MVs): as you mentioned, this situation occurs when a user renames one (or more) of the MVs. What I’ve implemented for now is a query in ClickHouse to identify any MV that matches the pattern of the model name but is not listed in the model. If such an MV is found, a warning is raised to notify the user.

I’m hesitant about automatically dropping these MVs, as they could be valid and intentionally separate from the model.

Additionally, I’ve updated the README file to include this important information.

Let’s monitor this behavior for now. If anyone in the community has a better or safer solution, contributions are welcome!

@BentsiLeviav BentsiLeviav merged commit d3271c8 into ClickHouse:main Nov 19, 2024
21 checks passed
@the4thamigo-uk
Copy link
Contributor Author

the4thamigo-uk commented Nov 19, 2024

Amazing, thanks for finishing this off. I was actually working on this yesterday and implemented a similar thing to drop the dangling views, but as you say this might be dangerous.

However, I did change the naming convention for the views so that the view name is always appended after a _mv_. I think in this way it is a bit safer to drop anything that is prefixed with hackers_mv_. It also ensures that all materialized view that are created by the model start with this prefix, including the current behaviour.

hackers_mv1 -> hackers_mv_mv1

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.

2 participants