Skip to content

Commit

Permalink
fix: sql unsigned settings (#10673)
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj authored and MyaLongmire committed Jul 6, 2022
1 parent b10860c commit 501311a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 23 deletions.
9 changes: 8 additions & 1 deletion plugins/outputs/sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ through the convert settings.
# defaultvalue = "TEXT"
# unsigned = "UNSIGNED"
# bool = "BOOL"

## This setting controls the behavior of the unsigned value. By default the
## setting will take the integer value and append the unsigned value to it. The other
## option is "literal", which will use the actual value the user provides to
## the unsigned option. This is useful for a database like ClickHouse where
## the unsigned value should use a value like "uint64".
# conversion_style = "unsigned_suffix"
```

## Driver-specific information
Expand Down Expand Up @@ -158,7 +165,7 @@ Use this metric type to SQL type conversion:
timestamp = "DateTime"
defaultvalue = "String"
unsigned = "UInt64"
bool = "Uint8"
bool = "UInt8"
```

See [ClickHouse data types](https://clickhouse.com/docs/en/sql-reference/data-types/) for more info.
Expand Down
46 changes: 31 additions & 15 deletions plugins/outputs/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import (
)

type ConvertStruct struct {
Integer string
Real string
Text string
Timestamp string
Defaultvalue string
Unsigned string
Bool string
Integer string
Real string
Text string
Timestamp string
Defaultvalue string
Unsigned string
Bool string
ConversionStyle string
}

type SQL struct {
Expand Down Expand Up @@ -100,7 +101,13 @@ func (p *SQL) deriveDatatype(value interface{}) string {
case int64:
datatype = p.Convert.Integer
case uint64:
datatype = fmt.Sprintf("%s %s", p.Convert.Integer, p.Convert.Unsigned)
if p.Convert.ConversionStyle == "unsigned_suffix" {
datatype = fmt.Sprintf("%s %s", p.Convert.Integer, p.Convert.Unsigned)
} else if p.Convert.ConversionStyle == "literal" {
datatype = p.Convert.Unsigned
} else {
p.Log.Errorf("unknown converstaion style: %s", p.Convert.ConversionStyle)
}
case float64:
datatype = p.Convert.Real
case string:
Expand Down Expand Up @@ -158,6 +165,14 @@ var sampleConfig = `
# timestamp = "TIMESTAMP"
# defaultvalue = "TEXT"
# unsigned = "UNSIGNED"
# bool = "BOOL"
## This setting controls the behavior of the unsigned value. By default the
## setting will take the integer value and append the unsigned value to it. The other
## option is "literal", which will use the actual value the user provides to
## the unsigned option. This is useful for a database like ClickHouse where
## the unsigned value should use a value like "uint64".
# conversion_style = "unsigned_suffix"
`

func (p *SQL) SampleConfig() string { return sampleConfig }
Expand Down Expand Up @@ -300,13 +315,14 @@ func newSQL() *SQL {
TableExistsTemplate: "SELECT 1 FROM {TABLE} LIMIT 1",
TimestampColumn: "timestamp",
Convert: ConvertStruct{
Integer: "INT",
Real: "DOUBLE",
Text: "TEXT",
Timestamp: "TIMESTAMP",
Defaultvalue: "TEXT",
Unsigned: "UNSIGNED",
Bool: "BOOL",
Integer: "INT",
Real: "DOUBLE",
Text: "TEXT",
Timestamp: "TIMESTAMP",
Defaultvalue: "TEXT",
Unsigned: "UNSIGNED",
Bool: "BOOL",
ConversionStyle: "unsigned_suffix",
},
}
}
12 changes: 12 additions & 0 deletions plugins/outputs/sql/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ var (
Key: "bool_two",
Value: false,
},
{
Key: "uint64_one",
Value: uint64(1000000000),
},
{
Key: "float64_one",
Value: float64(3.1415),
},
},
ts,
),
Expand Down Expand Up @@ -295,6 +303,9 @@ func TestPostgresIntegration(t *testing.T) {
p.Log = testutil.Logger{}
p.Driver = "pgx"
p.DataSourceName = address
p.Convert.Real = "double precision"
p.Convert.Unsigned = "bigint"
p.Convert.ConversionStyle = "literal"

require.NoError(t, p.Connect())
require.NoError(t, p.Write(
Expand Down Expand Up @@ -396,6 +407,7 @@ func TestClickHouseIntegration(t *testing.T) {
p.Convert.Defaultvalue = "String"
p.Convert.Unsigned = "UInt64"
p.Convert.Bool = "UInt8"
p.Convert.ConversionStyle = "literal"

require.NoError(t, p.Connect())

Expand Down
6 changes: 4 additions & 2 deletions plugins/outputs/sql/testdata/clickhouse/expected.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2021-05-17 22:04:45 tag1 tag2 1234 2345 1 0
2021-05-17 22:04:45 tag1 tag2 1234 2345 1 0 1000000000 3.1415
CREATE TABLE foo.metric_one
(
`timestamp` DateTime,
Expand All @@ -7,7 +7,9 @@ CREATE TABLE foo.metric_one
`int64_one` Int64,
`int64_two` Int64,
`bool_one` UInt8,
`bool_two` UInt8
`bool_two` UInt8,
`uint64_one` UInt64,
`float64_one` Float64
)
ENGINE = MergeTree
ORDER BY timestamp
Expand Down
6 changes: 4 additions & 2 deletions plugins/outputs/sql/testdata/mariadb/expected.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ CREATE TABLE `metric_one` (
`int64_one` int(11) DEFAULT NULL,
`int64_two` int(11) DEFAULT NULL,
`bool_one` tinyint(1) DEFAULT NULL,
`bool_two` tinyint(1) DEFAULT NULL
`bool_two` tinyint(1) DEFAULT NULL,
`uint64_one` int(10) unsigned DEFAULT NULL,
`float64_one` double DEFAULT NULL
);
/*!40101 SET character_set_client = @saved_cs_client */;
INSERT INTO `metric_one` VALUES ('2021-05-17 22:04:45','tag1','tag2',1234,2345,1,0);
INSERT INTO `metric_one` VALUES ('2021-05-17 22:04:45','tag1','tag2',1234,2345,1,0,1000000000,3.1415);
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `metric_two` (
Expand Down
8 changes: 5 additions & 3 deletions plugins/outputs/sql/testdata/postgres/expected.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ CREATE TABLE public.metric_one (
int64_one integer,
int64_two integer,
bool_one boolean,
bool_two boolean
bool_two boolean,
uint64_one bigint,
float64_one double precision
);
ALTER TABLE public.metric_one OWNER TO postgres;
CREATE TABLE public.metric_two (
Expand All @@ -35,8 +37,8 @@ ALTER TABLE public.metric_two OWNER TO postgres;
COPY public."metric three" ("timestamp", "tag four", "string two") FROM stdin;
2021-05-17 22:04:45 tag4 string2
\.
COPY public.metric_one ("timestamp", tag_one, tag_two, int64_one, int64_two, bool_one, bool_two) FROM stdin;
2021-05-17 22:04:45 tag1 tag2 1234 2345 t f
COPY public.metric_one ("timestamp", tag_one, tag_two, int64_one, int64_two, bool_one, bool_two, uint64_one, float64_one) FROM stdin;
2021-05-17 22:04:45 tag1 tag2 1234 2345 t f 1000000000 3.1415
\.
COPY public.metric_two ("timestamp", tag_three, string_one) FROM stdin;
2021-05-17 22:04:45 tag3 string1
Expand Down

0 comments on commit 501311a

Please sign in to comment.