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

Commit

Permalink
Change return type after eval to the right type
Browse files Browse the repository at this point in the history
Signed-off-by: Juanjo Alvarez <juanjo@sourced.tech>
  • Loading branch information
Juanjo Alvarez committed May 7, 2019
1 parent 99416f2 commit b8660ef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
16 changes: 12 additions & 4 deletions sql/expression/function/greatest_least.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// are mixed with non numerically convertible strings, those are ignored.
type Greatest struct {
Args []sql.Expression
returnType sql.Type
}

// ErrUnsupportedType is returned when an argument to Greatest or Latest is not numeric or string
Expand All @@ -33,11 +34,11 @@ func NewGreatest(args ...sql.Expression) (sql.Expression, error) {
}
}

return &Greatest{args}, nil
return &Greatest{args, sql.Float64}, nil
}

// Type implements the Expression interface.
func (f *Greatest) Type() sql.Type { return sql.Float64 }
func (f *Greatest) Type() sql.Type { return f.returnType }

// IsNullable implements the Expression interface.
func (f *Greatest) IsNullable() bool {
Expand Down Expand Up @@ -154,12 +155,15 @@ func (f *Greatest) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
}

if allInt {
f.returnType = sql.Int64
return int64(greatestNum), nil
} else if allString {
f.returnType = sql.Text
return greatestString, nil
}

// float64
f.returnType = sql.Float64
return greatestNum, nil
}

Expand All @@ -170,6 +174,7 @@ func (f *Greatest) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
// are mixed with non numerically convertible strings, those are ignored.
type Least struct {
Args []sql.Expression
returnType sql.Type
}

// NewLeast creates a new Least UDF
Expand All @@ -188,11 +193,11 @@ func NewLeast(args ...sql.Expression) (sql.Expression, error) {
}
}

return &Least{args}, nil
return &Least{args, sql.Float64}, nil
}

// Type implements the Expression interface.
func (f *Least) Type() sql.Type { return sql.Float64 }
func (f *Least) Type() sql.Type { return f.returnType }

// IsNullable implements the Expression interface.
func (f *Least) IsNullable() bool {
Expand Down Expand Up @@ -309,11 +314,14 @@ func (f *Least) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
}

if allInt {
f.returnType = sql.Int64
return int64(leastNum), nil
} else if allString {
f.returnType = sql.Text
return leastString, nil
}

// float
f.returnType = sql.Float64
return leastNum, nil
}
12 changes: 12 additions & 0 deletions sql/expression/function/greatest_least_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ import (
)

func TestGreatest(t *testing.T) {

// XXX
//table := mem.NewTable("foo", sql.Schema{
// {Name: "str_no_num", Type: sql.Text, Source: "foo"},
// {Name: "str_num1", Type: sql.Text, Source: "foo"},
// {Name: "str_num9", Type: sql.Text, Source: "foo"},
// {Name: "int1", Type: sql.Int64, Source: "foo"},
// {Name: "int5", Type: sql.Int64, Source: "foo"},
// {Name: "float2", Type: sql.Float64, Source: "foo"},
// {Name: "float9", Type: sql.Float64, Source: "foo"},
//})

testCases := []struct {
name string
args []sql.Expression
Expand Down

0 comments on commit b8660ef

Please sign in to comment.