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

chore(outputs.sql): Make default create table template suitable for ClickHouse #16464

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AndreKR
Copy link
Contributor

@AndreKR AndreKR commented Feb 2, 2025

Summary

The SQL output plugin automatically creates the required table in the target database. For this it uses a default create table template that currently has the value CREATE TABLE {TABLE}({COLUMNS}) where {TABLE} is the metric name and {COLUMNS} is a list of column name/type pairs. The plugin has a setting table_template to override this template.

ClickHouse requires that in a CREATE TABLE statement a primary key or a sort key is given, thus the default template above does not work with the ClickHouse database.

There is a section about ClickHouse-specific configuration in the README, but it doesn't mention a mandatory table_template.

The integration test solves this issue by hardcoding an override for the template that reads: CREATE TABLE {TABLE}({COLUMNS}) ENGINE MergeTree() ORDER by timestamp. In other words, it specifies the timestamp as the sort key.

Specifying only the timestamp as sort key is a valid approach, but it is not recommended in the ClickHouse world because the timestamp has a high cardinality.

This PR makes the following changes:

  • Change the default table template from CREATE TABLE {TABLE}({COLUMNS}) to CREATE TABLE {TABLE}({COLUMNS}) {SORT_KEY_CLAUSE}. The placeholder {SORT_KEY_CLAUSE} will contain a list of all tag columns when the database is ClickHouse. It will be removed (replaced with an empty string) for any other database.
  • Provide another placeholder {TAG_COLUMNS} that contains the list of tag columns. You can use this in your table_template for example to build a CREATE TABLE statement with extra columns, like this: CREATE TABLE {TABLE} ({COLUMNS}, my_extra_column int) ORDER BY ({TAG_COLUMNS}, my_extra_column)
  • Remove the hardcoded table_template override from the integration test because it is no longer needed to make the test work.

Checklist

  • No AI generated code was used in this PR

Related issues

resolves #16463

…lickHouse

This changes the default TableTemplate of the SQL output plugin so that
it can be used with ClickHouse. Previously it was invalid for ClickHouse
because it didn't include a PRIMARY KEY or ORDER BY clause, which is
required for ClickHouse.

This then also allows to remove the custom TableTemplate from the
ClickHouse unit test.

This also adds another placeholder {TAG_COLUMNS} that allows to get a
list of all tag columns in a TableTemplate. This can then be used to use
the tag columns as a primary or sort key, especially in ClickHouse.
@telegraf-tiger telegraf-tiger bot added area/sql chore plugin/output 1. Request for new output plugins 2. Issues/PRs that are related to out plugins labels Feb 2, 2025
@AndreKR AndreKR changed the title chore(outputs.sql): Make default create table template suitable for ClcikHouse chore(outputs.sql): Make default create table template suitable for ClickHouse Feb 2, 2025
@telegraf-tiger
Copy link
Contributor

telegraf-tiger bot commented Feb 2, 2025

Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

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

@AndreKR thanks for your effort! Why don't we simple modify the default table_template for Clickhouse to

  ## Note: The default template for clickhouse is
  ## table_template = "CREATE TABLE {TABLE}({COLUMNS}) ORDER BY (<tag columns>, <time column>)"
  ## for all other drivers the default is
  # table_template = "CREATE TABLE {TABLE}({COLUMNS})"

@srebhan srebhan self-assigned this Feb 4, 2025
@AndreKR
Copy link
Contributor Author

AndreKR commented Feb 5, 2025

Why don't we simple modify the default table_template for Clickhouse

That's because I didn't find a good way to make the default table template dependent on the driver. The default table template comes from newSQL() which is called from init() and unlike in the other functions I don't have the p *SQL struct with the plugin settings available there. (I guess because at that point the concrete plugin instance doesn't exist yet.)

@AndreKR AndreKR marked this pull request as draft February 6, 2025 19:06
@AndreKR
Copy link
Contributor Author

AndreKR commented Feb 6, 2025

By the way, this (the test to be precise) depends on #16462, so marking this as a draft for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/sql chore plugin/output 1. Request for new output plugins 2. Issues/PRs that are related to out plugins
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make SQL output plugin work with ClickHouse out of the box
2 participants