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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugins/outputs/sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## {TABLE} - table name as a quoted identifier
## {TABLELITERAL} - table name as a quoted string literal
## {COLUMNS} - column definitions (list of quoted identifiers and types)
## {TAG_COLUMN_NAMES} - tag column definitions (list of quoted identifiers)
# table_template = "CREATE TABLE {TABLE}({COLUMNS})"

## Table existence check template
Expand Down
1 change: 1 addition & 0 deletions plugins/outputs/sql/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
## {TABLE} - table name as a quoted identifier
## {TABLELITERAL} - table name as a quoted string literal
## {COLUMNS} - column definitions (list of quoted identifiers and types)
## {TAG_COLUMN_NAMES} - tag column definitions (list of quoted identifiers)
# table_template = "CREATE TABLE {TABLE}({COLUMNS})"

## Table existence check template
Expand Down
16 changes: 15 additions & 1 deletion plugins/outputs/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,15 @@ func (p *SQL) deriveDatatype(value interface{}) string {

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

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

for _, tag := range metric.TagList() {
columns = append(columns, fmt.Sprintf("%s %s", quoteIdent(tag.Key), p.Convert.Text))
tagColumnNames = append(tagColumnNames, quoteIdent(tag.Key))
}

var datatype string
Expand All @@ -172,6 +174,7 @@ func (p *SQL) generateCreateTable(metric telegraf.Metric) string {
query = strings.ReplaceAll(query, "{TABLE}", quoteIdent(metric.Name()))
query = strings.ReplaceAll(query, "{TABLELITERAL}", quoteStr(metric.Name()))
query = strings.ReplaceAll(query, "{COLUMNS}", strings.Join(columns, ","))
query = strings.ReplaceAll(query, "{TAG_COLUMN_NAMES}", strings.Join(tagColumnNames, ","))

return query
}
Expand Down Expand Up @@ -274,13 +277,24 @@ func (p *SQL) Write(metrics []telegraf.Metric) error {
return nil
}

func (p *SQL) Init() error {
if p.TableTemplate == "" {
if p.Driver == "clickhouse" {
p.TableTemplate = "CREATE TABLE {TABLE}({COLUMNS}) ORDER BY ({TAG_COLUMN_NAMES}, " + p.TimestampColumn + ")"
} else {
p.TableTemplate = "CREATE TABLE {TABLE}({COLUMNS})"
}
}

return nil
}

func init() {
outputs.Add("sql", func() telegraf.Output { return newSQL() })
}

func newSQL() *SQL {
return &SQL{
TableTemplate: "CREATE TABLE {TABLE}({COLUMNS})",
TableExistsTemplate: "SELECT 1 FROM {TABLE} LIMIT 1",
TimestampColumn: "timestamp",
Convert: ConvertStruct{
Expand Down
4 changes: 3 additions & 1 deletion plugins/outputs/sql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func TestMysqlIntegration(t *testing.T) {
p.Driver = "mysql"
p.DataSourceName = address
p.InitSQL = "SET sql_mode='ANSI_QUOTES';"
require.NoError(t, p.Init())

require.NoError(t, p.Connect())
require.NoError(t, p.Write(
Expand Down Expand Up @@ -287,6 +288,7 @@ func TestPostgresIntegration(t *testing.T) {
p.Convert.Real = "double precision"
p.Convert.Unsigned = "bigint"
p.Convert.ConversionStyle = "literal"
require.NoError(t, p.Init())

require.NoError(t, p.Connect())
defer p.Close()
Expand Down Expand Up @@ -372,14 +374,14 @@ func TestClickHouseIntegration(t *testing.T) {
p.Log = testutil.Logger{}
p.Driver = "clickhouse"
p.DataSourceName = address
p.TableTemplate = "CREATE TABLE {TABLE}({COLUMNS}) ENGINE MergeTree() ORDER by timestamp"
p.Convert.Integer = "Int64"
p.Convert.Text = "String"
p.Convert.Timestamp = "DateTime"
p.Convert.Defaultvalue = "String"
p.Convert.Unsigned = "UInt64"
p.Convert.Bool = "UInt8"
p.Convert.ConversionStyle = "literal"
require.NoError(t, p.Init())

require.NoError(t, p.Connect())
require.NoError(t, p.Write(testMetrics))
Expand Down
1 change: 1 addition & 0 deletions plugins/outputs/sql/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestSqlite(t *testing.T) {
p.Log = testutil.Logger{}
p.Driver = "sqlite"
p.DataSourceName = address
require.NoError(t, p.Init())

require.NoError(t, p.Connect())
defer p.Close()
Expand Down
Loading