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 2 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
62 changes: 62 additions & 0 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -7531,6 +7531,68 @@ 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"},
},
},
{
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"},
},
},
{
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"},
},
},
},
},
{
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
6 changes: 6 additions & 0 deletions sql/expression/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ func combinedCaseBranchType(left, right sql.Type) sql.Type {
if right == types.Null {
return left
}
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