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

LowCardinality type dataparser support. #77

Merged
merged 2 commits into from
Jun 28, 2019
Merged
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ http://user:password@host:8123/clicks?read_timeout=10&write_timeout=20
* Date
* DateTime
* Enum
* LowCardinality(T)
* [Array(T) (one-dimensional)](https://clickhouse.yandex/reference_en.html#Array(T))
* [Nested(Name1 Type1, Name2 Type2, ...)](https://clickhouse.yandex/docs/en/data_types/nested_data_structures/nested/)

Expand Down
21 changes: 21 additions & 0 deletions dataparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ func (p *arrayParser) Parse(s io.RuneScanner) (driver.Value, error) {
return slice.Interface(), nil
}

type lowCardinalityParser struct {
arg DataParser
}

func (p *lowCardinalityParser) Type() reflect.Type {
return p.arg.Type()
}

func (p *lowCardinalityParser) Parse(s io.RuneScanner) (driver.Value, error) {
return p.arg.Parse(s)
}

func newDateTimeParser(format, locname string, unquote bool) (DataParser, error) {
loc, err := time.LoadLocation(locname)
if err != nil {
Expand Down Expand Up @@ -431,6 +443,15 @@ func newDataParser(t *TypeDesc, unquote bool) (DataParser, error) {
subParsers[i] = subParser
}
return &tupleParser{subParsers}, nil
case "LowCardinality":
if len(t.Args) != 1 {
return nil, fmt.Errorf("element type not specified for LowCardinality")
}
subParser, err := newDataParser(t.Args[0], unquote)
if err != nil {
return nil, fmt.Errorf("failed to create parser for LowCardinality elements: %v", err)
}
return &lowCardinalityParser{subParser}, nil
default:
return nil, fmt.Errorf("type %s is not supported", t.Name)
}
Expand Down
24 changes: 24 additions & 0 deletions dataparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,30 @@ func TestParseData(t *testing.T) {
inputdata: "(1,2",
failParseData: true,
},
{
name: "low cardinality string",
inputtype: "LowCardinality(String)",
inputdata: "hello",
output: "hello",
},
{
name: "low cardinality string with escaping",
inputtype: "LowCardinality(String)",
inputdata: `hello \'world`,
output: "hello 'world",
},
{
name: "low cardinality string with incorrect escaping",
inputtype: "LowCardinality(String)",
inputdata: `hello world\`,
failParseData: true,
},
{
name: "low cardinality UInt64",
inputtype: "LowCardinality(UInt64)",
inputdata: "123",
output: uint64(123),
},
}

for _, tc := range testCases {
Expand Down