Skip to content

Commit 6625ccc

Browse files
authored
add case for enum return types in multi-branch case statement (#2766)
1 parent 2e03797 commit 6625ccc

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

enginetest/queries/script_queries.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7531,6 +7531,71 @@ where
75317531
},
75327532
},
75337533
},
7534+
{
7535+
Name: "multi enum return types",
7536+
SetUpScript: []string{
7537+
"create table t (i int primary key, e enum('abc', 'def', 'ghi'));",
7538+
"insert into t values (1, 'abc'), (2, 'def'), (3, 'ghi');",
7539+
},
7540+
Assertions: []ScriptTestAssertion{
7541+
{
7542+
Query: "select i, (case e when 'abc' then e when 'def' then e when 'ghi' then e end) as e from t;",
7543+
Expected: []sql.Row{
7544+
{1, "abc"},
7545+
{2, "def"},
7546+
{3, "ghi"},
7547+
},
7548+
},
7549+
{
7550+
// https://github.com/dolthub/dolt/issues/8598
7551+
Skip: true,
7552+
Query: "select i, (case e when 'abc' then e when 'def' then e when 'ghi' then 'something' end) as e from t;",
7553+
Expected: []sql.Row{
7554+
{1, "abc"},
7555+
{2, "def"},
7556+
{3, "something"},
7557+
},
7558+
},
7559+
{
7560+
// https://github.com/dolthub/dolt/issues/8598
7561+
Skip: true,
7562+
Query: "select i, (case e when 'abc' then e when 'def' then e when 'ghi' then 123 end) as e from t;",
7563+
Expected: []sql.Row{
7564+
{1, "abc"},
7565+
{2, "def"},
7566+
{3, "123"},
7567+
},
7568+
},
7569+
},
7570+
},
7571+
{
7572+
// https://github.com/dolthub/dolt/issues/8598
7573+
Name: "enum cast to int and string",
7574+
SetUpScript: []string{
7575+
"create table t (i int primary key, e enum('abc', 'def', 'ghi'));",
7576+
"insert into t values (1, 'abc'), (2, 'def'), (3, 'ghi');",
7577+
},
7578+
Assertions: []ScriptTestAssertion{
7579+
{
7580+
Skip: true,
7581+
Query: "select i, cast(e as signed) from t;",
7582+
Expected: []sql.Row{
7583+
{1, 1},
7584+
{2, 2},
7585+
{3, 3},
7586+
},
7587+
},
7588+
{
7589+
Skip: true,
7590+
Query: "select i, cast(e as char) from t;",
7591+
Expected: []sql.Row{
7592+
{1, "abc"},
7593+
{2, "def"},
7594+
{3, "ghi"},
7595+
},
7596+
},
7597+
},
7598+
},
75347599
}
75357600

75367601
var SpatialScriptTests = []ScriptTest{

sql/expression/case.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ func combinedCaseBranchType(left, right sql.Type) sql.Type {
5252
if right == types.Null {
5353
return left
5454
}
55+
56+
// Our current implementation of StringType.Convert(enum), does not match MySQL's behavior.
57+
// So, we make sure to return Enums in this particular case.
58+
// More details: https://github.com/dolthub/dolt/issues/8598
59+
if types.IsEnum(left) && types.IsEnum(right) {
60+
return right
61+
}
62+
if types.IsSet(left) && types.IsSet(right) {
63+
return right
64+
}
5565
if types.IsTextOnly(left) && types.IsTextOnly(right) {
5666
return types.LongText
5767
}

0 commit comments

Comments
 (0)