ClickHouse / clickhouse-go

Golang driver for ClickHouse
Apache License 2.0
2.83k stars 544 forks source link

Append does not honor Valuer interface for int32-backed enum. #997

Open todor-mutafov opened 1 year ago

todor-mutafov commented 1 year ago

Describe the bug

I'm using Gorm, and I have an enum backed by a int32. I'm trying to store it to a Nullable(Int32) by using a pointer to the enum value. When trying to store the row, I get the error attached below. I tried implementing driver.Valuer, and it didn't help.

From debugging a bit I notice that clickhouse-go uses the valuer to determine the nil-or-not-nil state, but not the actual value of the field.

Expected behaviour

The library should either automatically handle the conversion between the enum and the backing type, or at the very least it should use the driver.Valuer implementation properly to determine the value.

Code example

type MyEnum int32

// ...

func (m *MyEum) Value() (driver.Value, error) {
    converter := driver.Null{Converter: driver.Int32}
    if m == nil {
        return converter.ConvertValue(nil)
    }
    return converter.ConvertValue(int32(*m))
}

type MyEntity struct {
    Timestamp time.Time `gorm:"type:DateTime('UTC')"`
    MyEnumValue *MyEnum `gorm:"type:Nullable(Int32)"`
    // ...
}

Error log

clickhouse [AppendRow]: MyEnumValue clickhouse [AppendRow]: converting *example.MyEnum to Int32 is unsupported

Configuration

Environment

ClickHouse server

jkaflik commented 1 year ago

Hi @todor-mutafov

To support for type alias, having driver.Valuer interface support is best. It already exists for string, but not numeric types. I will have look on this.