ClickHouse / clickhouse-go

Golang driver for ClickHouse
Apache License 2.0
2.92k stars 562 forks source link

invalid Enum error while parsing single value enum with zero value #1434

Open topofstack opened 1 week ago

topofstack commented 1 week ago

Observed

When parsing enum type with a single value, function extractEnumNamedValues works incorrectly, returning valid==false for single-value enum with zero value:

_, _, _, valid := extractEnumNamedValues("Enum8('FOO' = 0)")
fmt.Println(valid) // false

The output is correct if two or more enum values are passed:

_, _, _, valid := extractEnumNamedValues("Enum8('FOO' = 0, 'BAR' = 1")
fmt.Println(valid) // true

or value is not zero:

_, _, _, valid := extractEnumNamedValues("Enum8('FOO' = 1")
fmt.Println(valid) // true

Details

Looks like we need to add additional check here https://github.com/ClickHouse/clickhouse-go/blob/v2.30.0/lib/column/enum.go#L169

// Enum type must have at least one value
if valueIndex == 0 && !indexFound{
        return
}

Environment