Skip to content

Commit

Permalink
Merge pull request #1387 from ClickHouse/gg/recognize_empty_enum_key
Browse files Browse the repository at this point in the history
Recognize empty strings as a valid enum key
  • Loading branch information
genzgd authored Aug 26, 2024
2 parents 5a3d28a + 4e02b95 commit dac3fdc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/column/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func extractEnumNamedValues(chType Type) (typ string, values []string, indexes [
var foundValueLen int
var skippedValueTokens []int
var indexFound bool
var valueFound bool
var valueIndex = 0

for c := 0; c < len(src); c++ {
Expand All @@ -104,6 +105,7 @@ func extractEnumNamedValues(chType Type) (typ string, values []string, indexes [
case token == '\'' && stringOpen:
stringOpen = false
foundValueLen = c - foundValueOffset
valueFound = true
break
// escape character, skip the next character
case token == '\\' && stringOpen:
Expand All @@ -112,10 +114,9 @@ func extractEnumNamedValues(chType Type) (typ string, values []string, indexes [
break
// capture optional index. `=` token is followed with an integer index
case token == '=' && !stringOpen:
if foundValueLen == 0 {
if !valueFound {
return
}

indexStart := c + 1
// find the end of the index, it's either a comma or a closing bracket
for _, token := range src[indexStart:] {
Expand All @@ -134,10 +135,9 @@ func extractEnumNamedValues(chType Type) (typ string, values []string, indexes [
break
// capture the value and index when a comma or closing bracket is found
case (token == ',' || token == ')') && !stringOpen:
if foundValueLen == 0 {
if !valueFound {
return
}

// if no index was found for current value, increment the value index
// e.g. Enum8('a','b') is equivalent to Enum8('a'=1,'b'=2)
// or Enum8('a'=3,'b') is equivalent to Enum8('a'=3,'b'=4)
Expand All @@ -160,6 +160,7 @@ func extractEnumNamedValues(chType Type) (typ string, values []string, indexes [
indexes = append(indexes, valueIndex)
values = append(values, string(foundName))
indexFound = false
valueFound = false
break
}
}
Expand Down
9 changes: 9 additions & 0 deletions lib/column/enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ func TestExtractEnumNamedValues(t *testing.T) {
chType: "Enum8",
isNotValid: true,
},
{
name: "Enum8 with empty key",
chType: "Enum8('a'=1, ''=2)",
expectedType: "Enum8",
expectedValues: map[int]string{
1: "a",
2: "",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit dac3fdc

Please sign in to comment.