Skip to content

Commit

Permalink
Merge pull request #72 from fschoell/fix/clickhouse_array_type
Browse files Browse the repository at this point in the history
fixed Clickhouse Array type
  • Loading branch information
maoueh authored Dec 21, 2024
2 parents 6eab99a + 78d6b77 commit 91d0240
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased

* Added support for the Clickhouse `Date` type.

* Fixed handling of the Clickhouse `Array` type.

* Removed the check for duplicate primary keys on the Clickhouse dialect. This allows inserting multiple rows with the same primary key.

## v4.3.0
Expand Down
12 changes: 11 additions & 1 deletion db/dialect_clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"context"
"encoding/json"
"fmt"
"math/big"
"reflect"
Expand Down Expand Up @@ -191,7 +192,16 @@ func convertToType(value string, valueType reflect.Type) (any, error) {
case reflect.String:
return value, nil
case reflect.Slice:
return value, nil
if valueType.Elem().Kind() == reflect.Struct || valueType.Elem().Kind() == reflect.Ptr {
return nil, fmt.Errorf("%q is not supported as Clickhouse Array type", valueType.Elem().Name())
}

res := reflect.New(reflect.SliceOf(valueType.Elem()))
if err := json.Unmarshal([]byte(value), res.Interface()); err != nil {
return "", fmt.Errorf("could not JSON unmarshal slice value %q: %w", value, err)
}

return res.Elem().Interface(), nil
case reflect.Bool:
return strconv.ParseBool(value)
case reflect.Int:
Expand Down
25 changes: 25 additions & 0 deletions db/dialect_clickhouse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ func Test_convertToType(t *testing.T) {
expectErr: nil,
valueType: reflect.TypeOf(time.Time{}),
},
{
name: "String Slice Double Quoted",
value: `["field1", "field2"]`,
expect: []string{"field1", "field2"},
expectErr: nil,
valueType: reflect.TypeOf([]string{}),
}, {
name: "Int Slice",
value: `[1, 2]`,
expect: []int{1, 2},
expectErr: nil,
valueType: reflect.TypeOf([]int{}),
}, {
name: "Float Slice",
value: `[1.0, 2.0]`,
expect: []float64{1, 2},
expectErr: nil,
valueType: reflect.TypeOf([]float64{}),
}, {
name: "Invalid Type Slice Struct",
value: `[""]`,
expect: nil,
expectErr: errors.New(`"Time" is not supported as Clickhouse Array type`),
valueType: reflect.TypeOf([]time.Time{}),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down

0 comments on commit 91d0240

Please sign in to comment.