Skip to content

Commit

Permalink
fix: sql unsigned settings
Browse files Browse the repository at this point in the history
This more accurately refelcts the correct naming for unsigned datatypes.

Additionally, this actually adds tests for the uint64 and float64
datatypes across clickhouse, mariadb, and postgres. Because postgres
does not have support for unsigned values, the number is treated as a
bigint.

Fixes: #10671
  • Loading branch information
powersj committed Feb 17, 2022
1 parent 3c600cd commit 9b3805d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
4 changes: 2 additions & 2 deletions plugins/outputs/sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ through the convert settings.
# text = "TEXT"
# timestamp = "TIMESTAMP"
# defaultvalue = "TEXT"
# unsigned = "UNSIGNED"
# unsigned = "INT UNSIGNED"
# bool = "BOOL"
```

Expand Down Expand Up @@ -158,7 +158,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
6 changes: 3 additions & 3 deletions plugins/outputs/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ 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)
datatype = p.Convert.Unsigned
case float64:
datatype = p.Convert.Real
case string:
Expand Down Expand Up @@ -157,7 +157,7 @@ var sampleConfig = `
# text = "TEXT"
# timestamp = "TIMESTAMP"
# defaultvalue = "TEXT"
# unsigned = "UNSIGNED"
# unsigned = "INT UNSIGNED"
`

func (p *SQL) SampleConfig() string { return sampleConfig }
Expand Down Expand Up @@ -305,7 +305,7 @@ func newSQL() *SQL {
Text: "TEXT",
Timestamp: "TIMESTAMP",
Defaultvalue: "TEXT",
Unsigned: "UNSIGNED",
Unsigned: "INT UNSIGNED",
Bool: "BOOL",
},
}
Expand Down
10 changes: 10 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,8 @@ func TestPostgresIntegration(t *testing.T) {
p.Log = testutil.Logger{}
p.Driver = "pgx"
p.DataSourceName = address
p.Convert.Real = "double precision"
p.Convert.Unsigned = "bigint"

require.NoError(t, p.Connect())
require.NoError(t, p.Write(
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 9b3805d

Please sign in to comment.