Skip to content

Commit 80069ac

Browse files
committed
chore(outputs.sql): Make default create table template suitable for ClickHouse
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.
1 parent 2f3e25f commit 80069ac

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

plugins/outputs/sql/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
9999
## {TABLE} - table name as a quoted identifier
100100
## {TABLELITERAL} - table name as a quoted string literal
101101
## {COLUMNS} - column definitions (list of quoted identifiers and types)
102+
## {TAG_COLUMNS} - tag column definitions (list of quoted identifiers)
102103
# table_template = "CREATE TABLE {TABLE}({COLUMNS})"
103104

104105
## Table existence check template

plugins/outputs/sql/sample.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
## {TABLE} - table name as a quoted identifier
1919
## {TABLELITERAL} - table name as a quoted string literal
2020
## {COLUMNS} - column definitions (list of quoted identifiers and types)
21+
## {TAG_COLUMNS} - tag column definitions (list of quoted identifiers)
2122
# table_template = "CREATE TABLE {TABLE}({COLUMNS})"
2223

2324
## Table existence check template

plugins/outputs/sql/sql.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,15 @@ func (p *SQL) deriveDatatype(value interface{}) string {
146146

147147
func (p *SQL) generateCreateTable(metric telegraf.Metric) string {
148148
columns := make([]string, 0, len(metric.TagList())+len(metric.FieldList())+1)
149+
tagColumns := make([]string, 0, len(metric.TagList()))
149150

150151
if p.TimestampColumn != "" {
151152
columns = append(columns, fmt.Sprintf("%s %s", quoteIdent(p.TimestampColumn), p.Convert.Timestamp))
152153
}
153154

154155
for _, tag := range metric.TagList() {
155156
columns = append(columns, fmt.Sprintf("%s %s", quoteIdent(tag.Key), p.Convert.Text))
157+
tagColumns = append(tagColumns, quoteIdent(tag.Key))
156158
}
157159

158160
var datatype string
@@ -165,6 +167,13 @@ func (p *SQL) generateCreateTable(metric telegraf.Metric) string {
165167
query = strings.ReplaceAll(query, "{TABLE}", quoteIdent(metric.Name()))
166168
query = strings.ReplaceAll(query, "{TABLELITERAL}", quoteStr(metric.Name()))
167169
query = strings.ReplaceAll(query, "{COLUMNS}", strings.Join(columns, ","))
170+
query = strings.ReplaceAll(query, "{TAG_COLUMNS}", strings.Join(tagColumns, ","))
171+
172+
if p.Driver == "clickhouse" {
173+
query = strings.ReplaceAll(query, "{SORT_KEY_CLAUSE}", fmt.Sprintf("ORDER BY (%s, %s)", strings.Join(tagColumns, ","), p.TimestampColumn))
174+
} else {
175+
query = strings.ReplaceAll(query, "{SORT_KEY_CLAUSE}", "")
176+
}
168177

169178
return query
170179
}
@@ -273,7 +282,7 @@ func init() {
273282

274283
func newSQL() *SQL {
275284
return &SQL{
276-
TableTemplate: "CREATE TABLE {TABLE}({COLUMNS})",
285+
TableTemplate: "CREATE TABLE {TABLE}({COLUMNS}) {SORT_KEY_CLAUSE}",
277286
TableExistsTemplate: "SELECT 1 FROM {TABLE} LIMIT 1",
278287
TimestampColumn: "timestamp",
279288
Convert: ConvertStruct{

plugins/outputs/sql/sql_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ func TestClickHouseIntegration(t *testing.T) {
368368
p.Log = testutil.Logger{}
369369
p.Driver = "clickhouse"
370370
p.DataSourceName = address
371-
p.TableTemplate = "CREATE TABLE {TABLE}({COLUMNS}) ENGINE MergeTree() ORDER by timestamp"
372371
p.Convert.Integer = "Int64"
373372
p.Convert.Text = "String"
374373
p.Convert.Timestamp = "DateTime"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy