Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add case for enum return types in multi-branch case statement #2766

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's generally useful to add some context if we're skipping a test. Could be what's missing to make the test pass or anything else that could be useful to someone looking at this brand new next year and trying to figure out why the test is skipped.

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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. it might be helpful to add a comment linking to the GitHub issue for these skipped tests. That would help provide more context for a future code reader.

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably worth adding a comment to explain what we're doing here. This seems pretty difficult to reverse engineer and figure out in a year or two when someone new looks at this code and tries to figure out why these cases are in here.

return right
}
if types.IsSet(left) && types.IsSet(right) {
return right
}
if types.IsTextOnly(left) && types.IsTextOnly(right) {
return types.LongText
}
Expand Down
Loading