Skip to content

Commit dac3fdc

Browse files
authoredAug 26, 2024··
Merge pull request #1387 from ClickHouse/gg/recognize_empty_enum_key
Recognize empty strings as a valid enum key
2 parents 5a3d28a + 4e02b95 commit dac3fdc

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed
 

‎lib/column/enum.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ func extractEnumNamedValues(chType Type) (typ string, values []string, indexes [
7878
var foundValueLen int
7979
var skippedValueTokens []int
8080
var indexFound bool
81+
var valueFound bool
8182
var valueIndex = 0
8283

8384
for c := 0; c < len(src); c++ {
@@ -104,6 +105,7 @@ func extractEnumNamedValues(chType Type) (typ string, values []string, indexes [
104105
case token == '\'' && stringOpen:
105106
stringOpen = false
106107
foundValueLen = c - foundValueOffset
108+
valueFound = true
107109
break
108110
// escape character, skip the next character
109111
case token == '\\' && stringOpen:
@@ -112,10 +114,9 @@ func extractEnumNamedValues(chType Type) (typ string, values []string, indexes [
112114
break
113115
// capture optional index. `=` token is followed with an integer index
114116
case token == '=' && !stringOpen:
115-
if foundValueLen == 0 {
117+
if !valueFound {
116118
return
117119
}
118-
119120
indexStart := c + 1
120121
// find the end of the index, it's either a comma or a closing bracket
121122
for _, token := range src[indexStart:] {
@@ -134,10 +135,9 @@ func extractEnumNamedValues(chType Type) (typ string, values []string, indexes [
134135
break
135136
// capture the value and index when a comma or closing bracket is found
136137
case (token == ',' || token == ')') && !stringOpen:
137-
if foundValueLen == 0 {
138+
if !valueFound {
138139
return
139140
}
140-
141141
// if no index was found for current value, increment the value index
142142
// e.g. Enum8('a','b') is equivalent to Enum8('a'=1,'b'=2)
143143
// or Enum8('a'=3,'b') is equivalent to Enum8('a'=3,'b'=4)
@@ -160,6 +160,7 @@ func extractEnumNamedValues(chType Type) (typ string, values []string, indexes [
160160
indexes = append(indexes, valueIndex)
161161
values = append(values, string(foundName))
162162
indexFound = false
163+
valueFound = false
163164
break
164165
}
165166
}

‎lib/column/enum_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ func TestExtractEnumNamedValues(t *testing.T) {
124124
chType: "Enum8",
125125
isNotValid: true,
126126
},
127+
{
128+
name: "Enum8 with empty key",
129+
chType: "Enum8('a'=1, ''=2)",
130+
expectedType: "Enum8",
131+
expectedValues: map[int]string{
132+
1: "a",
133+
2: "",
134+
},
135+
},
127136
}
128137
for _, tt := range tests {
129138
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)
Please sign in to comment.