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

Properly cast ENUMs to TEXT for CASE and CONVERT statements #2791

Merged
merged 5 commits into from
Dec 12, 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
38 changes: 35 additions & 3 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -7641,14 +7641,14 @@ where
},
{
// https://github.com/dolthub/dolt/issues/8598
Name: "enum cast to int and string",
Name: "enum cast to int and string",
Dialect: "mysql",
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},
Expand All @@ -7657,14 +7657,46 @@ where
},
},
{
Skip: true,
Query: "select i, cast(e as char) from t;",
Expected: []sql.Row{
{1, "abc"},
{2, "def"},
{3, "ghi"},
},
},
{
Query: "select i, cast(e as binary) from t;",
Expected: []sql.Row{
{1, []uint8("abc")},
{2, []uint8("def")},
{3, []uint8("ghi")},
},
},
{
Query: "select case when e = 'abc' then 'abc' when e = 'def' then 123 else e end from t",
Expected: []sql.Row{
{"abc"},
{"123"},
{"ghi"},
},
},
},
},
{
Name: "enum errors",
Dialect: "mysql",
SetUpScript: []string{
"create table t (i int primary key, e enum('abc', 'def', 'ghi'));",
},
Assertions: []ScriptTestAssertion{
{
Query: "insert into t values (1, 500)",
ExpectedErrStr: "value 500 is not valid for this Enum",
},
{
Query: "insert into t values (1, -1)",
ExpectedErrStr: "value -1 is not valid for this Enum",
},
},
},
}
Expand Down
93 changes: 93 additions & 0 deletions sql/expression/enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2024 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package expression

import (
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/types"
)

// EnumToString is an expression that converts an enum value to a string.
type EnumToString struct {
Enum sql.Expression
}

var _ sql.Expression = (*EnumToString)(nil)
var _ sql.CollationCoercible = (*EnumToString)(nil)

func NewEnumToString(enum sql.Expression) *EnumToString {
return &EnumToString{Enum: enum}
}

// Type implements the sql.Expression interface.
func (e *EnumToString) Type() sql.Type {
return types.Text
}

// CollationCoercibility implements the interface sql.CollationCoercible.
func (e *EnumToString) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return e.Type().CollationCoercibility(ctx)
}

// IsNullable implements the sql.Expression interface.
func (e *EnumToString) IsNullable() bool {
return e.Enum.IsNullable()
}

// Resolved implements the sql.Expression interface.
func (e *EnumToString) Resolved() bool {
return e.Enum.Resolved()
}

// Children implements the sql.Expression interface.
func (e *EnumToString) Children() []sql.Expression {
return []sql.Expression{e.Enum}
}

// Eval implements the sql.Expression interface.
func (e *EnumToString) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
span, ctx := ctx.Span("expression.EnumToString")
defer span.End()

val, err := e.Enum.Eval(ctx, row)
if err != nil {
return nil, err
}
if val == nil {
return nil, nil
}

enumType := e.Enum.Type().(types.EnumType)
str, _ := enumType.At(int(val.(uint16)))
return str, nil
}

// WithChildren implements the Expression interface.
func (e *EnumToString) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(e, len(children), 1)
}

return NewEnumToString(children[0]), nil
}

// String implements the sql.Expression interface.
func (e *EnumToString) String() string {
return e.Enum.String()
}

// DebugString implements the sql.Expression interface.
func (e *EnumToString) DebugString() string {
return sql.DebugString(e.Enum)
}
9 changes: 9 additions & 0 deletions sql/planbuilder/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package planbuilder
import (
"strings"

"github.com/dolthub/go-mysql-server/sql/types"

"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/expression"
"github.com/dolthub/go-mysql-server/sql/plan"
Expand Down Expand Up @@ -132,6 +134,13 @@ func (f *factory) buildConvert(expr sql.Expression, castToType string, typeLengt
return expr, nil
}
}
if types.IsText(n.Type()) && types.IsEnum(expr.Type()) {
newNode, err := n.WithChildren(expression.NewEnumToString(expr))
if err != nil {
return nil, err
}
return newNode, nil
}
return n, nil
}

Expand Down
15 changes: 14 additions & 1 deletion sql/planbuilder/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,20 @@ func (b *Builder) caseExprToExpression(inScope *scope, e *ast.CaseExpr) (sql.Exp
elseExpr = b.buildScalar(inScope, e.Else)
}

return expression.NewCase(expr, branches, elseExpr), nil
newCase := expression.NewCase(expr, branches, elseExpr)
if types.IsText(newCase.Type()) {
for _, branch := range branches {
if types.IsEnum(branch.Value.Type()) {
branch.Value = expression.NewEnumToString(branch.Value)
}
}
if elseExpr != nil && types.IsEnum(elseExpr.Type()) {
elseExpr = expression.NewEnumToString(elseExpr)
}
newCase = expression.NewCase(expr, branches, elseExpr)
}

return newCase, nil
}

func (b *Builder) intervalExprToExpression(inScope *scope, e *ast.IntervalExpr) *expression.Interval {
Expand Down
6 changes: 3 additions & 3 deletions sql/types/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,11 @@ func TypesEqual(a, b sql.Type) bool {
case EnumType:
aEnumType := at
bEnumType := b.(EnumType)
if len(aEnumType.indexToVal) != len(bEnumType.indexToVal) {
if len(aEnumType.idxToVal) != len(bEnumType.idxToVal) {
return false
}
for i := 0; i < len(aEnumType.indexToVal); i++ {
if aEnumType.indexToVal[i] != bEnumType.indexToVal[i] {
for i := 0; i < len(aEnumType.idxToVal); i++ {
if aEnumType.idxToVal[i] != bEnumType.idxToVal[i] {
return false
}
}
Expand Down
43 changes: 23 additions & 20 deletions sql/types/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ var (

type EnumType struct {
collation sql.CollationID
hashedValToIndex map[uint64]int
hashedValToIdx map[uint64]int
valToIdx map[string]int
indexToVal []string
idxToVal []string
maxResponseByteLength uint32
}

Expand Down Expand Up @@ -98,8 +98,8 @@ func CreateEnumType(values []string, collation sql.CollationID) (sql.EnumType, e
}
return EnumType{
collation: collation,
hashedValToIndex: hashedValToIndex,
indexToVal: values,
hashedValToIdx: hashedValToIndex,
idxToVal: values,
valToIdx: valToIdx,
maxResponseByteLength: maxResponseByteLength,
}, nil
Expand Down Expand Up @@ -217,9 +217,9 @@ func (t EnumType) MustConvert(v interface{}) interface{} {

// Equals implements the Type interface.
func (t EnumType) Equals(otherType sql.Type) bool {
if ot, ok := otherType.(EnumType); ok && t.collation.Equals(ot.collation) && len(t.indexToVal) == len(ot.indexToVal) {
for i, val := range t.indexToVal {
if ot.indexToVal[i] != val {
if ot, ok := otherType.(EnumType); ok && t.collation.Equals(ot.collation) && len(t.idxToVal) == len(ot.idxToVal) {
for i, val := range t.idxToVal {
if ot.idxToVal[i] != val {
return false
}
}
Expand Down Expand Up @@ -289,16 +289,19 @@ func (t EnumType) Zero() interface{} {
}

// At implements EnumType interface.
func (t EnumType) At(index int) (string, bool) {
// The elements listed in the column specification are assigned index numbers, beginning with 1.
index -= 1
if index <= -1 {
// for index zero, the value is empty. It's used for insert ignore.
func (t EnumType) At(idx int) (string, bool) {
// for index zero, the value is empty. It's used for insert ignore.
if idx < 0 {
return "", false
}
if idx == 0 {
return "", true
} else if index >= len(t.indexToVal) {
}
if idx > len(t.idxToVal) {
return "", false
}
return t.indexToVal[index], true
// The elements listed in the column specification are assigned index numbers, beginning with 1.
return t.idxToVal[idx-1], true
}

// CharacterSet implements EnumType interface.
Expand All @@ -318,7 +321,7 @@ func (t EnumType) IndexOf(v string) int {
}
hashedVal, err := t.collation.HashToUint(v)
if err == nil {
if index, ok := t.hashedValToIndex[hashedVal]; ok {
if index, ok := t.hashedValToIdx[hashedVal]; ok {
return index
}
}
Expand All @@ -334,24 +337,24 @@ func (t EnumType) IndexOf(v string) int {

// NumberOfElements implements EnumType interface.
func (t EnumType) NumberOfElements() uint16 {
return uint16(len(t.indexToVal))
return uint16(len(t.idxToVal))
}

// Values implements EnumType interface.
func (t EnumType) Values() []string {
vals := make([]string, len(t.indexToVal))
copy(vals, t.indexToVal)
vals := make([]string, len(t.idxToVal))
copy(vals, t.idxToVal)
return vals
}

// WithNewCollation implements sql.TypeWithCollation interface.
func (t EnumType) WithNewCollation(collation sql.CollationID) (sql.Type, error) {
return CreateEnumType(t.indexToVal, collation)
return CreateEnumType(t.idxToVal, collation)
}

// StringWithTableCollation implements sql.TypeWithCollation interface.
func (t EnumType) StringWithTableCollation(tableCollation sql.CollationID) string {
s := fmt.Sprintf("enum('%v')", strings.Join(t.indexToVal, `','`))
s := fmt.Sprintf("enum('%v')", strings.Join(t.idxToVal, `','`))
if t.CharacterSet() != tableCollation.CharacterSet() {
s += " CHARACTER SET " + t.CharacterSet().String()
}
Expand Down
Loading