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
Feature/lpad rpad #522
Merged
Merged
Feature/lpad rpad #522
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Next
Next commit
RPAD, LPAD
Signed-off-by: Theo Despoudis <thdespou@hotmail.com>
- Loading branch information
commit a131d366e47f6290eb75ea695faed72ca564c73f
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
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,165 @@ | ||
package function | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
"gopkg.in/src-d/go-mysql-server.v0/sql" | ||
) | ||
|
||
type padType int | ||
|
||
const ( | ||
leftPadType = iota | ||
theodesp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rightPadType | ||
) | ||
|
||
// MakePadder returns a Pad creator functions with a specific padType. | ||
theodesp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func MakePadder(pType padType) func(e ...sql.Expression) (sql.Expression, error) { | ||
return func(e ...sql.Expression) (sql.Expression, error) { | ||
return NewPad(pType, e...) | ||
} | ||
} | ||
|
||
// NewLogBase creates a new LogBase expression. | ||
theodesp marked this conversation as resolved.
Show resolved
Hide resolved
theodesp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func NewPad(pType padType, args ...sql.Expression) (sql.Expression, error) { | ||
argLen := len(args) | ||
if argLen != 3 { | ||
return nil, sql.ErrInvalidArgumentNumber.New("3", argLen) | ||
} | ||
|
||
return &Pad{args[0], args[1], args[2], pType}, nil | ||
} | ||
|
||
// Pad is a function that pads a string with another string. | ||
type Pad struct { | ||
str sql.Expression | ||
len sql.Expression | ||
theodesp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
padStr sql.Expression | ||
padType padType | ||
} | ||
|
||
// Children implements the Expression interface. | ||
func (p *Pad) Children() []sql.Expression { | ||
return []sql.Expression{p.str, p.len, p.padStr} | ||
} | ||
|
||
// Resolved implements the Expression interface. | ||
func (p *Pad) Resolved() bool { | ||
return p.str.Resolved() && p.len.Resolved() && (p.padStr.Resolved()) | ||
} | ||
|
||
// IsNullable implements the Expression interface. | ||
func (p *Pad) IsNullable() bool { | ||
return p.str.IsNullable() || p.len.IsNullable() || p.padStr.IsNullable() | ||
} | ||
|
||
// Type implements the Expression interface. | ||
func (p *Pad) Type() sql.Type { return sql.Text } | ||
|
||
func (p *Pad) String() string { | ||
if p.padType == leftPadType { | ||
return fmt.Sprintf("lpad(%s, %s, %s)", p.str, p.len, p.padStr) | ||
} | ||
return fmt.Sprintf("rpad(%s, %s, %s)", p.str, p.len, p.padStr) | ||
} | ||
|
||
// TransformUp implements the Expression interface. | ||
func (p *Pad) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) { | ||
str, err := p.str.TransformUp(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
len, err := p.len.TransformUp(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
padStr, err := p.padStr.TransformUp(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
padded, _ := NewPad(p.padType, str, len, padStr) | ||
return f(padded) | ||
} | ||
|
||
// Eval implements the Expression interface. | ||
func (p *Pad) Eval( | ||
ctx *sql.Context, | ||
row sql.Row, | ||
) (interface{}, error) { | ||
str, err := p.str.Eval(ctx, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if str == nil { | ||
return nil, nil | ||
} | ||
|
||
str, err = sql.Text.Convert(str) | ||
if err != nil { | ||
return nil, sql.ErrInvalidType.New(reflect.TypeOf(str)) | ||
} | ||
|
||
length, err := p.len.Eval(ctx, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if length == nil { | ||
return nil, nil | ||
} | ||
|
||
length, err = sql.Int64.Convert(length) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
padStr, err := p.padStr.Eval(ctx, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if padStr == nil { | ||
return nil, nil | ||
} | ||
|
||
padStr, err = sql.Text.Convert(padStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return padString(str.(string), length.(int64), padStr.(string), p.padType) | ||
} | ||
|
||
func padString(str string, length int64, padStr string, padType padType) (string, error) { | ||
if length <= 0 { | ||
return "", nil | ||
} | ||
if int64(len(str)) >= length { | ||
return str[:length], nil | ||
} | ||
if len(padStr) == 0 { | ||
return "", nil | ||
} | ||
|
||
padLen := int(length - int64(len(str))) | ||
quo, rem := divmod(int64(padLen), int64(len(padStr))) | ||
|
||
if padType == leftPadType { | ||
result := strings.Repeat(padStr, int(quo)) + padStr[:rem] + str | ||
return result[:length], nil | ||
} else { | ||
theodesp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result := str + strings.Repeat(padStr, int(quo)) + padStr[:rem] | ||
return result[(int64(len(result)) - length):], nil | ||
} | ||
} | ||
|
||
func divmod(a, b int64) (quotient, remainder int64) { | ||
quotient = a / b | ||
theodesp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
remainder = a % b | ||
return | ||
} |
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,109 @@ | ||
package function | ||
|
||
import ( | ||
"testing" | ||
|
||
"gopkg.in/src-d/go-mysql-server.v0/sql" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql/expression" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLPad(t *testing.T) { | ||
f, err := NewPad( | ||
leftPadType, | ||
expression.NewGetField(0, sql.Text, "str", false), | ||
expression.NewGetField(1, sql.Int64, "len", false), | ||
expression.NewGetField(2, sql.Text, "padStr", false), | ||
) | ||
require.NoError(t, err) | ||
testCases := []struct { | ||
name string | ||
row sql.Row | ||
expected interface{} | ||
err bool | ||
}{ | ||
{"null string", sql.NewRow(nil, 1, "bar"), nil, false}, | ||
{"null len", sql.NewRow("foo", nil, "bar"), nil, false}, | ||
{"null padStr", sql.NewRow("foo", 1, nil), nil, false}, | ||
|
||
{"negative length", sql.NewRow("foo", -1, "bar"), "", false}, | ||
{"length 0", sql.NewRow("foo", 0, "bar"), "", false}, | ||
{"invalid length", sql.NewRow("foo", "a", "bar"), "", true}, | ||
|
||
{"empty padStr and len < len(str)", sql.NewRow("foo", 1, ""), "f", false}, | ||
{"empty padStr and len > len(str)", sql.NewRow("foo", 4, ""), "", false}, | ||
{"empty padStr and len == len(str)", sql.NewRow("foo", 3, ""), "foo", false}, | ||
|
||
{"non empty padStr and len < len(str)", sql.NewRow("foo", 1, "abcd"), "f", false}, | ||
{"non empty padStr and len == len(str)", sql.NewRow("foo", 3, "abcd"), "foo", false}, | ||
|
||
{"padStr repeats exactly once", sql.NewRow("foo", 6, "abc"), "abcfoo", false}, | ||
{"padStr does not repeat once", sql.NewRow("foo", 5, "abc"), "abfoo", false}, | ||
{"padStr repeats many times", sql.NewRow("foo", 10, "abc"), "abcabcafoo", false}, | ||
} | ||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Helper() | ||
require := require.New(t) | ||
ctx := sql.NewEmptyContext() | ||
|
||
v, err := f.Eval(ctx, tt.row) | ||
if tt.err { | ||
require.Error(err) | ||
} else { | ||
require.NoError(err) | ||
require.Equal(tt.expected, v) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRPad(t *testing.T) { | ||
f, err := NewPad( | ||
rightPadType, | ||
expression.NewGetField(0, sql.Text, "str", false), | ||
expression.NewGetField(1, sql.Int64, "len", false), | ||
expression.NewGetField(2, sql.Text, "padStr", false), | ||
) | ||
require.NoError(t, err) | ||
testCases := []struct { | ||
name string | ||
row sql.Row | ||
expected interface{} | ||
err bool | ||
}{ | ||
{"null string", sql.NewRow(nil, 1, "bar"), nil, false}, | ||
{"null len", sql.NewRow("foo", nil, "bar"), nil, false}, | ||
{"null padStr", sql.NewRow("foo", 1, nil), nil, false}, | ||
|
||
{"negative length", sql.NewRow("foo", -1, "bar"), "", false}, | ||
{"length 0", sql.NewRow("foo", 0, "bar"), "", false}, | ||
{"invalid length", sql.NewRow("foo", "a", "bar"), "", true}, | ||
|
||
{"empty padStr and len < len(str)", sql.NewRow("foo", 1, ""), "f", false}, | ||
{"empty padStr and len > len(str)", sql.NewRow("foo", 4, ""), "", false}, | ||
{"empty padStr and len == len(str)", sql.NewRow("foo", 3, ""), "foo", false}, | ||
|
||
{"non empty padStr and len < len(str)", sql.NewRow("foo", 1, "abcd"), "f", false}, | ||
{"non empty padStr and len == len(str)", sql.NewRow("foo", 3, "abcd"), "foo", false}, | ||
|
||
{"padStr repeats exactly once", sql.NewRow("foo", 6, "abc"), "fooabc", false}, | ||
{"padStr does not repeat once", sql.NewRow("foo", 5, "abc"), "fooab", false}, | ||
{"padStr repeats many times", sql.NewRow("foo", 10, "abc"), "fooabcabca", false}, | ||
} | ||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Helper() | ||
require := require.New(t) | ||
ctx := sql.NewEmptyContext() | ||
|
||
v, err := f.Eval(ctx, tt.row) | ||
if tt.err { | ||
require.Error(err) | ||
} else { | ||
require.NoError(err) | ||
require.Equal(tt.expected, v) | ||
} | ||
}) | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.