Skip to content

Commit

Permalink
Merge pull request #164 from zhenghaoz/master
Browse files Browse the repository at this point in the history
support parsing bool type
  • Loading branch information
DoubleDi authored Nov 21, 2022
2 parents 0591d22 + 3c4c117 commit b259988
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.35.2
version: latest

test:
runs-on: ubuntu-latest
Expand All @@ -43,6 +43,6 @@ jobs:

services:
clickhouse:
image: yandex/clickhouse-server:latest
image: clickhouse/clickhouse-server:latest
ports:
- 8123:8123
7 changes: 4 additions & 3 deletions clickhouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var ddls = []string{
i64 Int64,
u64 UInt64,
f64 Float64,
b Boolean,
s String,
s2 String,
a16 Array(Int16),
Expand All @@ -39,9 +40,9 @@ var ddls = []string{
m Map(String, Array(Int64))
) ENGINE = Memory`,
`INSERT INTO data VALUES
(-1, 1, 1.0, '1', '1', [1], [10], '2011-03-06', '2011-03-06 06:20:00', 'one', '10.1111', '100.1111', '1000.1111', '1.1111', '127.0.0.1', '2001:db8:3333:4444:5555:6666:7777:8888', '12345678', 'one', {'key1':[1]}),
(-2, 2, 2.0, '2', '2', [2], [20], '2012-05-31', '2012-05-31 11:20:00', 'two', '30.1111', '300.1111', '2000.1111', '2.1111', '8.8.8.8', '2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF', '88888888', 'two', {'key2':[2]}),
(-3, 3, 3.0, '3', '2', [3], [30], '2016-04-04', '2016-04-04 11:30:00', 'three', '40.1111', '400.1111', '3000.1111', '3.1111', '255.255.255.255', '::1234:5678', '87654321', 'three', {'key3':[3]})
(-1, 1, 1.0, true, '1', '1', [1], [10], '2011-03-06', '2011-03-06 06:20:00', 'one', '10.1111', '100.1111', '1000.1111', '1.1111', '127.0.0.1', '2001:db8:3333:4444:5555:6666:7777:8888', '12345678', 'one', {'key1':[1]}),
(-2, 2, 2.0, false, '2', '2', [2], [20], '2012-05-31', '2012-05-31 11:20:00', 'two', '30.1111', '300.1111', '2000.1111', '2.1111', '8.8.8.8', '2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF', '88888888', 'two', {'key2':[2]}),
(-3, 3, 3.0, true, '3', '2', [3], [30], '2016-04-04', '2016-04-04 11:30:00', 'three', '40.1111', '400.1111', '3000.1111', '3.1111', '255.255.255.255', '::1234:5678', '87654321', 'three', {'key3':[3]})
`,
}

Expand Down
2 changes: 1 addition & 1 deletion conn_go18_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s *connSuite) TestColumnTypes() {
types, err := rows.ColumnTypes()
s.Require().NoError(err)
expected := []string{
"Int64", "UInt64", "Float64", "String", "String", "Array(Int16)", "Array(UInt8)", "Date", "DateTime",
"Int64", "UInt64", "Float64", "Bool", "String", "String", "Array(Int16)", "Array(UInt8)", "Date", "DateTime",
"Enum8('one' = 1, 'two' = 2, 'three' = 3)",
"Decimal(9, 4)", "Decimal(18, 4)", "Decimal(38, 4)", "Decimal(10, 4)", "IPv4", "IPv6", "FixedString(8)", "LowCardinality(String)",
"Map(String, Array(Int64))",
Expand Down
2 changes: 1 addition & 1 deletion conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (s *connSuite) TestQuery() {
[]interface{}{1},
[][]interface{}{
{
int64(-1), uint64(1), float64(1), "1", "1", []int16{1}, []uint8{10},
int64(-1), uint64(1), float64(1), true, "1", "1", []int16{1}, []uint8{10},
parseDate("2011-03-06"), parseDateTime("2011-03-06 06:20:00"), "one",
"10.1111", "100.1111", "1000.1111", "1.1111", "127.0.0.1",
"2001:db8:3333:4444:5555:6666:7777:8888", "12345678", "one",
Expand Down
21 changes: 21 additions & 0 deletions dataparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
reflectTypeString = reflect.TypeOf("")
reflectTypeTime = reflect.TypeOf(time.Time{})
reflectTypeEmptyStruct = reflect.TypeOf(struct{}{})
reflectTypeBool = reflect.TypeOf(bool(false))
reflectTypeInt8 = reflect.TypeOf(int8(0))
reflectTypeInt16 = reflect.TypeOf(int16(0))
reflectTypeInt32 = reflect.TypeOf(int32(0))
Expand Down Expand Up @@ -460,6 +461,24 @@ func newDateTimeParser(format string, loc *time.Location, precision int, unquote
}, nil
}

type boolParser struct{}

func (p *boolParser) Parse(s io.RuneScanner) (driver.Value, error) {
repr := readRaw(s).String()
switch repr {
case "true":
return true, nil
case "false":
return false, nil
default:
return nil, fmt.Errorf("incorrect bool value: %v", repr)
}
}

func (p *boolParser) Type() reflect.Type {
return reflectTypeBool
}

type intParser struct {
signed bool
bitSize int
Expand Down Expand Up @@ -646,6 +665,8 @@ func newDataParser(t *TypeDesc, unquote bool, opt *DataParserOptions) (DataParse
}

return newDateTimeParser(dateTime64Format, loc, precision, unquote)
case "Bool":
return &boolParser{}, nil
case "UInt8":
return &intParser{false, 8}, nil
case "UInt16":
Expand Down

0 comments on commit b259988

Please sign in to comment.