Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

sql/expression: allow null literals in case branches #741

Merged
merged 1 commit into from
Jun 5, 2019
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
4 changes: 4 additions & 0 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,10 @@ var queries = []struct {
`SELECT SUBSTR(SUBSTRING('0123456789ABCDEF', 1, 10), -4)`,
[]sql.Row{{"6789"}},
},
{
`SELECT CASE i WHEN 1 THEN i ELSE NULL END FROM mytable`,
[]sql.Row{{int64(1)}, {nil}, {nil}},
},
}

func TestQueries(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions sql/analyzer/validation_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package analyzer
import (
"strings"

"gopkg.in/src-d/go-errors.v1"
"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/expression"
"github.com/src-d/go-mysql-server/sql/expression/function"
"github.com/src-d/go-mysql-server/sql/plan"
"gopkg.in/src-d/go-errors.v1"
)

const (
Expand Down Expand Up @@ -242,14 +242,14 @@ func validateCaseResultTypes(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Nod
case *expression.Case:
typ := e.Type()
for _, b := range e.Branches {
if b.Value.Type() != typ {
if b.Value.Type() != typ && b.Value.Type() != sql.Null {
err = ErrCaseResultType.New(typ, b.Value, b.Value.Type(), e)
return false
}
}

if e.Else != nil {
if e.Else.Type() != typ {
if e.Else.Type() != typ && e.Else.Type() != sql.Null {
err = ErrCaseResultType.New(typ, e.Else, e.Else.Type(), e)
return false
}
Expand Down
11 changes: 9 additions & 2 deletions sql/expression/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ func NewCase(expr sql.Expression, branches []CaseBranch, elseExpr sql.Expression
// Type implements the sql.Expression interface.
func (c *Case) Type() sql.Type {
for _, b := range c.Branches {
return b.Value.Type()
if b.Value.Type() != sql.Null {
return b.Value.Type()
}
}

if c.Else.Type() != sql.Null {
return c.Else.Type()
}
return c.Else.Type()

return sql.Null
}

// IsNullable implements the sql.Expression interface.
Expand Down
19 changes: 18 additions & 1 deletion sql/expression/case_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package expression
import (
"testing"

"github.com/stretchr/testify/require"
"github.com/src-d/go-mysql-server/sql"
"github.com/stretchr/testify/require"
)

func TestCase(t *testing.T) {
Expand Down Expand Up @@ -127,3 +127,20 @@ func TestCase(t *testing.T) {
})
}
}

func TestCaseNullBranch(t *testing.T) {
require := require.New(t)
f := NewCase(
NewGetField(0, sql.Int64, "x", false),
[]CaseBranch{
{
Cond: NewLiteral(int64(1), sql.Int64),
Value: NewLiteral(nil, sql.Null),
},
},
nil,
)
result, err := f.Eval(sql.NewEmptyContext(), sql.Row{int64(1)})
require.NoError(err)
require.Nil(result)
}