This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
sql/expression/function: implement char_length and length functions #724
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package function | ||
|
||
import ( | ||
"fmt" | ||
"unicode/utf8" | ||
|
||
"gopkg.in/src-d/go-mysql-server.v0/sql" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql/expression" | ||
) | ||
|
||
// Length returns the length of a string or binary content, either in bytes | ||
// or characters. | ||
type Length struct { | ||
expression.UnaryExpression | ||
CountType CountType | ||
} | ||
|
||
// CountType is the kind of length count. | ||
type CountType bool | ||
|
||
const ( | ||
// NumBytes counts the number of bytes in a string or binary content. | ||
NumBytes = CountType(false) | ||
// NumChars counts the number of characters in a string or binary content. | ||
NumChars = CountType(true) | ||
) | ||
|
||
// NewLength returns a new LENGTH function. | ||
func NewLength(e sql.Expression) sql.Expression { | ||
return &Length{expression.UnaryExpression{Child: e}, NumBytes} | ||
} | ||
|
||
// NewCharLength returns a new CHAR_LENGTH function. | ||
func NewCharLength(e sql.Expression) sql.Expression { | ||
return &Length{expression.UnaryExpression{Child: e}, NumChars} | ||
} | ||
|
||
// TransformUp implements the sql.Expression interface. | ||
func (l *Length) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) { | ||
child, err := l.Child.TransformUp(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
nl := *l | ||
nl.Child = child | ||
return &nl, nil | ||
} | ||
|
||
// Type implements the sql.Expression interface. | ||
func (l *Length) Type() sql.Type { return sql.Int32 } | ||
|
||
func (l *Length) String() string { | ||
if l.CountType == NumBytes { | ||
return fmt.Sprintf("LENGTH(%s)", l.Child) | ||
} | ||
return fmt.Sprintf("CHAR_LENGTH(%s)", l.Child) | ||
} | ||
|
||
// Eval implements the sql.Expression interface. | ||
func (l *Length) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { | ||
val, err := l.Child.Eval(ctx, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if val == nil { | ||
return nil, nil | ||
} | ||
|
||
var content string | ||
switch l.Child.Type() { | ||
case sql.Blob: | ||
val, err = sql.Blob.Convert(val) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
content = string(val.([]byte)) | ||
default: | ||
val, err = sql.Text.Convert(val) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
content = string(val.(string)) | ||
} | ||
|
||
if l.CountType == NumBytes { | ||
return int32(len(content)), nil | ||
} | ||
|
||
return int32(utf8.RuneCountInString(content)), nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package function | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql/expression" | ||
) | ||
|
||
func TestLength(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
input interface{} | ||
inputType sql.Type | ||
fn func(sql.Expression) sql.Expression | ||
expected interface{} | ||
}{ | ||
{ | ||
"length string", | ||
"fóo", | ||
sql.Text, | ||
NewLength, | ||
int32(4), | ||
}, | ||
{ | ||
"length binary", | ||
[]byte("fóo"), | ||
sql.Blob, | ||
NewLength, | ||
int32(4), | ||
}, | ||
{ | ||
"length empty", | ||
"", | ||
sql.Blob, | ||
NewLength, | ||
int32(0), | ||
}, | ||
{ | ||
"length empty binary", | ||
[]byte{}, | ||
sql.Blob, | ||
NewLength, | ||
int32(0), | ||
}, | ||
{ | ||
"length nil", | ||
nil, | ||
sql.Blob, | ||
NewLength, | ||
nil, | ||
}, | ||
{ | ||
"char_length string", | ||
"fóo", | ||
sql.Text, | ||
NewCharLength, | ||
int32(3), | ||
}, | ||
{ | ||
"char_length binary", | ||
[]byte("fóo"), | ||
sql.Blob, | ||
NewCharLength, | ||
int32(3), | ||
}, | ||
{ | ||
"char_length empty", | ||
"", | ||
sql.Blob, | ||
NewCharLength, | ||
int32(0), | ||
}, | ||
{ | ||
"char_length empty binary", | ||
[]byte{}, | ||
sql.Blob, | ||
NewCharLength, | ||
int32(0), | ||
}, | ||
{ | ||
"char_length nil", | ||
nil, | ||
sql.Blob, | ||
NewCharLength, | ||
nil, | ||
}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require := require.New(t) | ||
|
||
result, err := tt.fn(expression.NewGetField(0, tt.inputType, "foo", false)).Eval( | ||
sql.NewEmptyContext(), | ||
sql.Row{tt.input}, | ||
) | ||
|
||
require.NoError(err) | ||
require.Equal(tt.expected, result) | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also add a test with an empty string.