fix: sql unsigned settings (#10673)

This commit is contained in:
Joshua Powers 2022-03-03 08:21:39 -07:00 committed by GitHub
parent 4aa87229e6
commit 008e43b7c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 23 deletions

View File

@ -112,6 +112,13 @@ through the convert settings.
# defaultvalue = "TEXT" # defaultvalue = "TEXT"
# unsigned = "UNSIGNED" # unsigned = "UNSIGNED"
# bool = "BOOL" # 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 ## Driver-specific information
@ -158,7 +165,7 @@ Use this metric type to SQL type conversion:
timestamp = "DateTime" timestamp = "DateTime"
defaultvalue = "String" defaultvalue = "String"
unsigned = "UInt64" unsigned = "UInt64"
bool = "Uint8" bool = "UInt8"
``` ```
See [ClickHouse data types](https://clickhouse.com/docs/en/sql-reference/data-types/) for more info. See [ClickHouse data types](https://clickhouse.com/docs/en/sql-reference/data-types/) for more info.

View File

@ -17,13 +17,14 @@ import (
) )
type ConvertStruct struct { type ConvertStruct struct {
Integer string Integer string
Real string Real string
Text string Text string
Timestamp string Timestamp string
Defaultvalue string Defaultvalue string
Unsigned string Unsigned string
Bool string Bool string
ConversionStyle string
} }
type SQL struct { type SQL struct {
@ -100,7 +101,13 @@ func (p *SQL) deriveDatatype(value interface{}) string {
case int64: case int64:
datatype = p.Convert.Integer datatype = p.Convert.Integer
case uint64: 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: case float64:
datatype = p.Convert.Real datatype = p.Convert.Real
case string: case string:
@ -158,6 +165,14 @@ var sampleConfig = `
# timestamp = "TIMESTAMP" # timestamp = "TIMESTAMP"
# defaultvalue = "TEXT" # defaultvalue = "TEXT"
# unsigned = "UNSIGNED" # 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 } func (p *SQL) SampleConfig() string { return sampleConfig }
@ -300,13 +315,14 @@ func newSQL() *SQL {
TableExistsTemplate: "SELECT 1 FROM {TABLE} LIMIT 1", TableExistsTemplate: "SELECT 1 FROM {TABLE} LIMIT 1",
TimestampColumn: "timestamp", TimestampColumn: "timestamp",
Convert: ConvertStruct{ Convert: ConvertStruct{
Integer: "INT", Integer: "INT",
Real: "DOUBLE", Real: "DOUBLE",
Text: "TEXT", Text: "TEXT",
Timestamp: "TIMESTAMP", Timestamp: "TIMESTAMP",
Defaultvalue: "TEXT", Defaultvalue: "TEXT",
Unsigned: "UNSIGNED", Unsigned: "UNSIGNED",
Bool: "BOOL", Bool: "BOOL",
ConversionStyle: "unsigned_suffix",
}, },
} }
} }

View File

@ -107,6 +107,14 @@ var (
Key: "bool_two", Key: "bool_two",
Value: false, Value: false,
}, },
{
Key: "uint64_one",
Value: uint64(1000000000),
},
{
Key: "float64_one",
Value: float64(3.1415),
},
}, },
ts, ts,
), ),
@ -295,6 +303,9 @@ func TestPostgresIntegration(t *testing.T) {
p.Log = testutil.Logger{} p.Log = testutil.Logger{}
p.Driver = "pgx" p.Driver = "pgx"
p.DataSourceName = address 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.Connect())
require.NoError(t, p.Write( require.NoError(t, p.Write(
@ -396,6 +407,7 @@ func TestClickHouseIntegration(t *testing.T) {
p.Convert.Defaultvalue = "String" p.Convert.Defaultvalue = "String"
p.Convert.Unsigned = "UInt64" p.Convert.Unsigned = "UInt64"
p.Convert.Bool = "UInt8" p.Convert.Bool = "UInt8"
p.Convert.ConversionStyle = "literal"
require.NoError(t, p.Connect()) require.NoError(t, p.Connect())

View File

@ -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 CREATE TABLE foo.metric_one
( (
`timestamp` DateTime, `timestamp` DateTime,
@ -7,7 +7,9 @@ CREATE TABLE foo.metric_one
`int64_one` Int64, `int64_one` Int64,
`int64_two` Int64, `int64_two` Int64,
`bool_one` UInt8, `bool_one` UInt8,
`bool_two` UInt8 `bool_two` UInt8,
`uint64_one` UInt64,
`float64_one` Float64
) )
ENGINE = MergeTree ENGINE = MergeTree
ORDER BY timestamp ORDER BY timestamp

View File

@ -23,10 +23,12 @@ CREATE TABLE `metric_one` (
`int64_one` int(11) DEFAULT NULL, `int64_one` int(11) DEFAULT NULL,
`int64_two` int(11) DEFAULT NULL, `int64_two` int(11) DEFAULT NULL,
`bool_one` tinyint(1) 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 */; /*!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 @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */; /*!40101 SET character_set_client = utf8 */;
CREATE TABLE `metric_two` ( CREATE TABLE `metric_two` (

View File

@ -23,7 +23,9 @@ CREATE TABLE public.metric_one (
int64_one integer, int64_one integer,
int64_two integer, int64_two integer,
bool_one boolean, bool_one boolean,
bool_two boolean bool_two boolean,
uint64_one bigint,
float64_one double precision
); );
ALTER TABLE public.metric_one OWNER TO postgres; ALTER TABLE public.metric_one OWNER TO postgres;
CREATE TABLE public.metric_two ( CREATE TABLE public.metric_two (
@ -35,8 +37,8 @@ ALTER TABLE public.metric_two OWNER TO postgres;
COPY public."metric three" ("timestamp", "tag four", "string two") FROM stdin; COPY public."metric three" ("timestamp", "tag four", "string two") FROM stdin;
2021-05-17 22:04:45 tag4 string2 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; 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 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; COPY public.metric_two ("timestamp", tag_three, string_one) FROM stdin;
2021-05-17 22:04:45 tag3 string1 2021-05-17 22:04:45 tag3 string1