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

Fix creation of replicated tables when using legacy strategy and unique_key #208

Conversation

StevenReitsma
Copy link
Contributor

@StevenReitsma StevenReitsma commented Nov 14, 2023

Problem

Table creation fails in the situation where:

  • you are not using distributed materializations
  • you are using ReplicatedMergeTree table engines
  • you are using cluster_mode: true
  • you have defined a unique_key for a model
  • you are using legacy incremental model strategy

Cause

The cause is twofold:

  1. create table [table1] as [table2] on cluster [cluster] is invalid SQL for ClickHouse (at least for OSS). The on cluster [cluster] clause is not supported for create table .. as statements.
  2. When removing the on_cluster_clause, table creation fails on Code: 36. DB::Exception: Received from localhost:9000. DB::Exception: Macro 'uuid' and empty arguments of ReplicatedMergeTree are supported only for ON CLUSTER queries with Atomic database engine. (BAD_ARGUMENTS).

So in this case, on cluster is both required and not supported at the same time.

Solution

This PR changes create table [table1] as [table2] to create table [table1] empty as ([sql for table1]). This lets dbt supply ClickHouse with the SQL that was used to create [table1] for [table2], instead of letting ClickHouse look at [table2] itself to copy the schema.

Tests

All tests pass locally.

@CLAassistant
Copy link

CLAassistant commented Nov 14, 2023

CLA assistant check
All committers have signed the CLA.

@genzgd
Copy link
Contributor

genzgd commented Nov 23, 2023

Thanks for the PR. I have mixed feelings about this because ON CLUSTER is actually supported, it's just the order is wrong in that macro. CREATE TABLE <table> ON CLUSTER <cluster> AS is the right order, and I'd be inclined to let ClickHouse do this instead of a Jinja macro.

@StevenReitsma
Copy link
Contributor Author

Ah seems like you're right, it's just the ON CLUSTER being in the wrong place. Strange that there is no mention of this in the official documentation.

However, moving the ON CLUSTER clause doesn't solve the problem yet, as we get the following error:

DB::Exception: Replica /clickhouse/tables/0/db_foo/tbl_bar/e5f54f75-b9fc-436c-a5a9-59c7f781ee40/replicas/service-dwh-0-0 already exists.

The engine for this model is set to ReplicatedMergeTree('/clickhouse/tables/{shard}/{database}/{table}/{uuid}', '{replica}'). Shouldn't ClickHouse generate a new UUID when running a CTAS instead of copying the one from the source?

@genzgd
Copy link
Contributor

genzgd commented Nov 27, 2023

What you're seeing might be a result of the behavior described here: ClickHouse/ClickHouse#12135. If you are running the model repeatedly within 8 minutes the old table path might still be in zookeeper. I'm fairly certain the UUID for "CREATE TABLE AS" would be based on either a timestamp, the "different" table name, or both, so I don't think it's reusing the source table one.

It's likely we should be applying the SYNC modifier to dropping any Replicated tables.

Unfortunately this sort of behavior is one of the reasons that dbt is problematic on ClickHouse clusters.

@StevenReitsma StevenReitsma force-pushed the fix/incremental_unique_key_table_generation branch from c91f961 to a75eeb9 Compare November 28, 2023 10:58
@StevenReitsma
Copy link
Contributor Author

You're absolutely right, not sure why I didn't catch that before. The UUID does change when doing a CTAS, but somehow the UUID was hardcoded for some of my tables. I set up a fresh instance and using CTAS with ON CLUSTER works just fine there. Of course, the clause is still in the wrong place in the query, so I've changed this PR to just move the clause to the correct position.

@genzgd genzgd merged commit 9997b82 into ClickHouse:main Nov 28, 2023
16 checks passed
Savid pushed a commit to Savid/dbt-clickhouse that referenced this pull request Jan 17, 2024
Solovechik added a commit to tekliner/dbt-clickhouse that referenced this pull request May 27, 2024
* fix: SYSTEM SYNC REPLICA for on_cluster_clause (ClickHouse#156)

* fix SYSTEM SYNC REPLICA

* add schema

* Update version and pypi job

* Fix incompatible return type (ClickHouse#162)

* Distributed table materialization (ClickHouse#163)

* distributed table materialization

* fix rebase

* PR fixes

* Bump version

* Tweak PyPI build Python release

* Add space to exchange_tables_atomic macro (ClickHouse#168)

* Add space to exchange_tables_atomic macro

This changes the SYSTEM SYNC REPLICA query to have a space between the
ON CLUSTER clause and the table name.

* Move whitespace to on_cluster_clause

* Fix bad logging/error handling (ClickHouse#170)

* Distributed incremental materialization (ClickHouse#172)

* distributed table materialization

* fix rebase

* PR fixes

* distributed incremental materialization

* fix

* fix

* add insert_distributed_sync to README.md

* add checks on  insert_distributed_sync

* add checks on  insert_distributed_sync

* review fixes

* Update version and tweak docs

* Lw delete set fix (ClickHouse#174)

* Move lightweight delete settings to per query for HTTP stickiness fix

* Minor cleanup and doc updates

* Fix legacy incremental materialization (ClickHouse#178)

* fix: distributed_table materialization issue (ClickHouse#184)

* Bump version and changelog (ClickHouse#185)

* cluster names containing dash characters (ClickHouse#198) (ClickHouse#200)

Co-authored-by: the4thamigo-uk <the4thamigo-uk>

* Add basic error test, fix minor merge conflict (ClickHouse#202)

* Cluster setting and Distributed Table tests (ClickHouse#186)

* added can_on_cluster var in ClickhouseRelation

* add tests for cluster

* fix lint issue

* debug set cluster env variable

* debug test

* debug and add tests

* skip distributed table grant test

* debug workflow

* debug workflow

* debug test

* add tests fro distributed_incremental

* fix zk path error

* fix wrong alias for distributed materializations

update aliase test

* update base on review

* Update version and CHANGELOG, incorporate cluster name fix (ClickHouse#203)

* Release 1 5 0 (ClickHouse#210)

* Initial 1.5.0 commit

* Reorganize basic tests

* Fix lint

* Add case sensitive cache

* Fix s3 bucket bug

* Checkpoint for constraints/contracts

* Fix native column query

* Loosen replication test

* Checkpoint for constraints tests

* Checkpoint for constraints tests

* Add rendering of model level CHECK constraints

* Fix lint

* Reorganize test files

* Add one hooks test

* Fix lint

* Update test and dependency versions. (ClickHouse#211)

* Adjust the wrapper parenthesis around the table materialization sql code (ClickHouse#212)

* Update for 1.5.1 bug fix

* Fix creation of replicated tables when using legacy materialization (ClickHouse#208)

* On cluster sync cleanup

* Bug fixes related to model settings. (ClickHouse#214)

* Add materialization macro for materialized view (ClickHouse#207)

* Add materialization macro for materialized view

* fix isort issues in materialized view test

* Release 1 6 0 (ClickHouse#215)

* Initial dbt 1.6 update

* Add skipped clone test

* Clean up MV PR

* Release 1 6 1 (ClickHouse#217)

* Identifier quoting checkpoint

* Identifier quoting checkpoint

* Fix distributed table local quoting

* Fix issues with deduplication settings

* Release 1 6 2 (ClickHouse#219)

* Limited fix to completely broken `on_schema_change`

* Tweak changelog

* Release 1 7 0 (ClickHouse#220)

* Initial dependency updates for 1.7.x

* Initial dependency updates for 1.7.x

* Correctly warn or error if light weight deletes not available

* Wrap columns_in_query query in select statement (ClickHouse#222)

* Wrap columns_in_query query in select statement

* formatting

* Update changelog

* allows to add a comment in table's or view's metadata

* add settings_section flag as comment for code using settings

* override test sql macro and add limit-placer macro

* update CHANGELOG.md

* fix: use correct schema for MV target tables (ClickHouse#244)

* fix: use correct schema when updating MVs

The existing implementation passes just the name for `target_table`,
which ultimately means that the target schema is not included when the
final SQL is generated. By passing the entire relation object, the
correct target schema will be present in the final SQL.

* update MV tests

Provide a custom schema to make sure that the full target table
name (schema + relation name) is included in the CREATE MATERIALIZED
VIEW statement

* Update changelog

* rename end of query flag

* Bug/223 relationship test with limit (ClickHouse#245)

* add settings_section flag as comment for code using settings

* override test sql macro and add limit-placer macro

* update CHANGELOG.md

* rename end of query flag

* Revert "Bug/223 relationship test with limit (ClickHouse#245)" (ClickHouse#247)

This reverts commit d8afb93.

* always return --end_of_sql when asking for settings

* Add model settings based on materialization type

* support setting clause on view creation

* edit CHANGELOG.md

* Bump version and tweak changelog

* change list syntax to satisfy lint test

* change list syntax to satisfy lint test

* change imports order to satisfy lint test

* Add typing to satisfy lint

* Add snapshot materialization to default settings

* Fix tests - add distributed_table and distributed_incremental materializations

* Fix tests - make sure to call the get_model_settings only when materialization is view

* clean up recent changelog

* Add materialization macro for dictionaries

* address lint issue in dictionary test

* address lint issue with enum

* Fix model settings with custom materialization

* Release 1.7.4 housekeeping (ClickHouse#261)

* Bump black from 23.11.0 to 24.3.0 (ClickHouse#259)

Bumps [black](https://github.com/psf/black) from 23.11.0 to 24.3.0.
- [Release notes](https://github.com/psf/black/releases)
- [Changelog](https://github.com/psf/black/blob/main/CHANGES.md)
- [Commits](psf/black@23.11.0...24.3.0)

---
updated-dependencies:
- dependency-name: black
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Release 1 7 5 (ClickHouse#265)

* Release 1.7.5 housekeeping

* Upgrade setuptools requirement for clickhouse_driver install

* Remove flake8 checks for the moment

* Update workflow actions

* Fix black comma

* fix(clients): add newlines around subquery when retrieving columns to avoid a syntax error (ClickHouse#262)

* Fix lint

* lazy load agate (ClickHouse#263)

* feat: add TTL support (ClickHouse#254)

* Fix lint

* Update table relation after exchange command (ClickHouse#230)

Related to ClickHouse#226

* feat: allow to add connection overrides for dictionaries (ClickHouse#267)

* Housekeeping for 1.7.6 release (ClickHouse#268)

* Revert "allows to add a comment in table's or view's metadata"

* Fix bool_or behavior (ClickHouse#270)

* feat: support column codecs

* Use Column.data_type in ClickHouseAdapter.format_columns

* Always apply query_settings in clickhouse__insert_into macro

* Add ClickHouseColumn.is_low_cardinality

* Update column type test cases for LowCardinality

* Omit empty dictionary connection_overrides from materialization DDL

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Schum <68906108+Schum-io@users.noreply.github.com>
Co-authored-by: Geoff Genz <geoff@clickhouse.com>
Co-authored-by: Sergey Reshetnikov <resh.sersh@gmail.com>
Co-authored-by: gladkikhtutu <88535677+gladkikhtutu@users.noreply.github.com>
Co-authored-by: Damir Basic Knezevic <damirbasicknezevic@gmail.com>
Co-authored-by: Zhenbang <122523068+zli06160@users.noreply.github.com>
Co-authored-by: Andy <email@elevatesystems.co.uk>
Co-authored-by: gfunc <fcjchaojian@gmail.com>
Co-authored-by: Kristof Szaloki <szalokikristof@gmail.com>
Co-authored-by: Steven Reitsma <4895139+StevenReitsma@users.noreply.github.com>
Co-authored-by: Rory Sawyer <rory@sawyer.dev>
Co-authored-by: ptemarvelde <45282601+ptemarvelde@users.noreply.github.com>
Co-authored-by: Dmitrii Tcimokha <dstsimokha@gmail.com>
Co-authored-by: bentsileviav <bentsi.leviav@clickhouse.com>
Co-authored-by: Dmitriy Sokolov <silentsokolov@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: triou <teva.riou@gmail.com>
Co-authored-by: Daniel Reeves <31971762+dwreeves@users.noreply.github.com>
Co-authored-by: Cristhian Garcia <crisgarta8@gmail.com>
Co-authored-by: Thomas Schmidt <somtom91@gmail.com>
Co-authored-by: scrawfor <scrawfor@users.noreply.github.com>
Co-authored-by: Robin Norgren <68205730+rjoelnorgren@users.noreply.github.com>
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.

3 participants