Repository navigation
Description
Describe the bug
proto.ColEnum returns wrong names for Enum8 and Enum16 elements whose names contain a character that the server escapes in the type name (for example ', \, and a line feed). The read returns no error. An INSERT of such a name fails with unknown enum value, but the escaped text of the name is accepted and stored as that name.
A column whose Enum has a name with , or = cannot be read or inserted with proto.ColEnum: Infer fails with bad enum definition or bad right side of definition.
Every path that infers a ColEnum from the server type is affected: Query.Result, Query.Input, and Results.Auto. Names without these characters work. ColEnum8 and ColEnum16 (raw values) are not affected.
Steps to reproduce
- Start a ClickHouse server.
- Run the program below with
CH_ADDR=<host>:9000 go run ..
Expected behaviour
ColEnum returns the names that the server returns for CAST(v AS String) (the server column in the error log). An INSERT of a'b succeeds; an INSERT of a\'b fails with unknown enum value. Names with , or = decode.
The server sends the type with each name as a quoted string with escapes (toTypeName of such a column):
Enum8('a\'b' = 1, 'line\nbreak' = 2, 'back\\' = 3, 'q\'' = 4, 'x,y' = 5, 'p=q' = 6)
Code example
Reproduction program (Go, 72 lines)
package main
import (
"context"
"fmt"
"os"
"github.com/ClickHouse/ch-go"
"github.com/ClickHouse/ch-go/proto"
)
func main() {
ctx := context.Background()
// A failed query closes a ch.Client, so each step uses a new client.
do := func(q ch.Query) error {
c, err := ch.Dial(ctx, ch.Options{Address: os.Getenv("CH_ADDR")})
if err != nil {
panic(err)
}
defer c.Close()
return c.Do(ctx, q)
}
exec := func(body string) {
if err := do(ch.Query{Body: body}); err != nil {
panic(err)
}
}
exec(`DROP TABLE IF EXISTS enum_esc`)
exec(`CREATE TABLE enum_esc (id UInt8, v Enum8('a\'b' = 1, 'line\nbreak' = 2, 'back\\' = 3, 'q\'' = 4, 'plain' = 5)) ENGINE = MergeTree ORDER BY id`)
exec(`INSERT INTO enum_esc VALUES (1, 1), (2, 2), (3, 3), (4, 4), (5, 5)`)
// 1. SELECT: proto.ColEnum compared with the server's CAST(v AS String).
var v proto.ColEnum
var s proto.ColStr
if err := do(ch.Query{
Body: `SELECT v, CAST(v AS String) AS s FROM enum_esc ORDER BY id`,
Result: proto.Results{{Name: "v", Data: &v}, {Name: "s", Data: &s}},
}); err != nil {
panic(err)
}
for i := 0; i < v.Rows(); i++ {
fmt.Printf("SELECT ColEnum %-16q server %q\n", v.Row(i), s.Row(i))
}
// 2. INSERT the name, then the escaped text of the name.
for _, name := range []string{"a'b", `a\'b`} {
err := do(ch.Query{
Body: `INSERT INTO enum_esc (v) VALUES`,
Input: proto.Input{{Name: "v", Data: &proto.ColEnum{Values: []string{name}}}},
})
fmt.Printf("INSERT %-16q error: %v\n", name, err)
}
var stored proto.ColStr
if err := do(ch.Query{
Body: `SELECT CAST(v AS String) AS s FROM enum_esc WHERE id = 0`,
Result: proto.Results{{Name: "s", Data: &stored}},
}); err != nil {
panic(err)
}
fmt.Printf("stored by the INSERT of %q: %q\n", `a\'b`, stored.Row(0))
// 3. Names with ',' or '='.
for i, def := range []string{`Enum8('x,y' = 1, 'z' = 2)`, `Enum8('p=q' = 1, 'z' = 2)`} {
table := fmt.Sprintf("enum_sep%d", i)
exec(`DROP TABLE IF EXISTS ` + table)
exec(`CREATE TABLE ` + table + ` (v ` + def + `) ENGINE = MergeTree ORDER BY tuple()`)
exec(`INSERT INTO ` + table + ` VALUES (1)`)
var e proto.ColEnum
err := do(ch.Query{Body: `SELECT v FROM ` + table, Result: proto.Results{{Name: "v", Data: &e}}})
fmt.Printf("SELECT %s error: %v\n", def, err)
}
}Error log
SELECT ColEnum "a\\'b" server "a'b"
SELECT ColEnum "line\\nbreak" server "line\nbreak"
SELECT ColEnum "back\\\\" server "back\\"
SELECT ColEnum "q\\" server "q'"
SELECT ColEnum "plain" server "plain"
INSERT "a'b" error: send input: write block: prepare "v": unknown enum value "a'b"
INSERT "a\\'b" error: <nil>
stored by the INSERT of "a\\'b": "a'b"
SELECT Enum8('x,y' = 1, 'z' = 2) error: decode block: decode block: raw block: target: infer: parse type: bad enum definition "'x"
SELECT Enum8('p=q' = 1, 'z' = 2) error: decode block: decode block: raw block: target: infer: parse type: bad right side of definition "q' = 1"
Root cause
ColEnum.parse (proto/col_enum.go:63 and :66) splits the type at every , and cuts each element at the first =, so a , or = inside a quoted name breaks the parse. It then only trims ' characters from both ends of the name (proto/col_enum.go:76) and does not decode the escapes, so the name keeps \', \\, and \n, and a name that ends with ' also loses that quote ('q\'' gives q\).
The fix is a quote-aware split and an unescape step in ColEnum.parse.
Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Related
- fix(proto): infer Map types whose key or value type contains a comma #1238 (open PR): adds
ColumnType.elems(), a split that skips quoted strings.
Configuration
Environment
- Client version:
main(a53587d);v0.76.0has the same code - Language version: Go 1.26.11
- OS: Linux
ClickHouse server
- ClickHouse Server version: 26.10.4.207
- ClickHouse Server non-default settings, if any: none
CREATE TABLEstatements for tables involved: in the program
Classification: impact: Data integrity · area: Data types & formats (Enum8, Enum16)
Found by automated analysis (Polyglot AI) while working on ClickHouse/clickhouse-go#2120. Verified against a live server.
Activity