Skip to content

Commit

Permalink
add case for enum return types in multi-branch case statement (#2766)
Browse files Browse the repository at this point in the history
  • Loading branch information
jycor authored Nov 25, 2024
1 parent 2e03797 commit 6625ccc
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
65 changes: 65 additions & 0 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -7531,6 +7531,71 @@ where
},
},
},
{
Name: "multi enum return types",
SetUpScript: []string{
"create table t (i int primary key, e enum('abc', 'def', 'ghi'));",
"insert into t values (1, 'abc'), (2, 'def'), (3, 'ghi');",
},
Assertions: []ScriptTestAssertion{
{
Query: "select i, (case e when 'abc' then e when 'def' then e when 'ghi' then e end) as e from t;",
Expected: []sql.Row{
{1, "abc"},
{2, "def"},
{3, "ghi"},
},
},
{
// https://github.com/dolthub/dolt/issues/8598
Skip: true,
Query: "select i, (case e when 'abc' then e when 'def' then e when 'ghi' then 'something' end) as e from t;",
Expected: []sql.Row{
{1, "abc"},
{2, "def"},
{3, "something"},
},
},
{
// https://github.com/dolthub/dolt/issues/8598
Skip: true,
Query: "select i, (case e when 'abc' then e when 'def' then e when 'ghi' then 123 end) as e from t;",
Expected: []sql.Row{
{1, "abc"},
{2, "def"},
{3, "123"},
},
},
},
},
{
// https://github.com/dolthub/dolt/issues/8598
Name: "enum cast to int and string",
SetUpScript: []string{
"create table t (i int primary key, e enum('abc', 'def', 'ghi'));",
"insert into t values (1, 'abc'), (2, 'def'), (3, 'ghi');",
},
Assertions: []ScriptTestAssertion{
{
Skip: true,
Query: "select i, cast(e as signed) from t;",
Expected: []sql.Row{
{1, 1},
{2, 2},
{3, 3},
},
},
{
Skip: true,
Query: "select i, cast(e as char) from t;",
Expected: []sql.Row{
{1, "abc"},
{2, "def"},
{3, "ghi"},
},
},
},
},
}

var SpatialScriptTests = []ScriptTest{
Expand Down
10 changes: 10 additions & 0 deletions sql/expression/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ func combinedCaseBranchType(left, right sql.Type) sql.Type {
if right == types.Null {
return left
}

// Our current implementation of StringType.Convert(enum), does not match MySQL's behavior.
// So, we make sure to return Enums in this particular case.
// More details: https://github.com/dolthub/dolt/issues/8598
if types.IsEnum(left) && types.IsEnum(right) {
return right
}
if types.IsSet(left) && types.IsSet(right) {
return right
}
if types.IsTextOnly(left) && types.IsTextOnly(right) {
return types.LongText
}
Expand Down

0 comments on commit 6625ccc

Please sign in to comment.