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: function, added soundex #486
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,7 @@ | |
- ROUND | ||
- COALESCE | ||
- CONNECTION_ID | ||
- SOUNDEX | ||
|
||
## Time functions | ||
- DAY | ||
|
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,102 @@ | ||
package function | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"unicode" | ||
|
||
"gopkg.in/src-d/go-mysql-server.v0/sql" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql/expression" | ||
) | ||
|
||
// Soundex is a function that returns the soundex of a string. Two strings that | ||
// sound almost the same should have identical soundex strings. A standard | ||
// soundex string is four characters long, but the SOUNDEX() function returns | ||
// an arbitrarily long string. | ||
type Soundex struct { | ||
expression.UnaryExpression | ||
} | ||
|
||
// NewSoundex creates a new Soundex expression. | ||
func NewSoundex(e sql.Expression) sql.Expression { | ||
return &Soundex{expression.UnaryExpression{Child: e}} | ||
} | ||
|
||
// Eval implements the Expression interface. | ||
func (s *Soundex) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { | ||
v, err := s.Child.Eval(ctx, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if v == nil { | ||
return nil, nil | ||
} | ||
|
||
v, err = sql.Text.Convert(v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var b strings.Builder | ||
var last rune | ||
for _, c := range strings.ToUpper(v.(string)) { | ||
if last == 0 && !unicode.IsLetter(c) { | ||
continue | ||
} | ||
code := s.code(c) | ||
if last == 0 { | ||
b.WriteRune(c) | ||
last = code | ||
continue | ||
} | ||
if code == '0' || code == last { | ||
continue | ||
} | ||
b.WriteRune(code) | ||
last = code | ||
} | ||
if b.Len() == 0 { | ||
return "", nil | ||
} | ||
for i := len([]rune(b.String())); i < 4; i++ { | ||
bake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
b.WriteRune('0') | ||
} | ||
return b.String(), nil | ||
} | ||
|
||
func (s *Soundex) code(c rune) rune { | ||
switch c { | ||
case 'B', 'F', 'P', 'V': | ||
return '1' | ||
case 'C', 'G', 'J', 'K', 'Q', 'S', 'X', 'Z': | ||
return '2' | ||
case 'D', 'T': | ||
return '3' | ||
case 'L': | ||
return '4' | ||
case 'M', 'N': | ||
return '5' | ||
case 'R': | ||
return '6' | ||
} | ||
return '0' | ||
} | ||
|
||
func (s *Soundex) String() string { | ||
return fmt.Sprintf("SOUNDEX(%s)", s.Child) | ||
} | ||
|
||
// TransformUp implements the Expression interface. | ||
func (s *Soundex) TransformUp(f sql.TransformExprFunc) (sql.Expression, error) { | ||
child, err := s.Child.TransformUp(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return f(NewSoundex(child)) | ||
} | ||
|
||
// Type implements the Expression interface. | ||
func (s *Soundex) Type() sql.Type { | ||
return s.Child.Type() | ||
} |
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,49 @@ | ||
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 TestSoundex(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
rowType sql.Type | ||
row sql.Row | ||
expected interface{} | ||
}{ | ||
{"text nil", sql.Text, sql.NewRow(nil), nil}, | ||
{"text empty", sql.Text, sql.NewRow(""), ""}, | ||
{"text ignored character", sql.Text, sql.NewRow("-"), ""}, | ||
{"text runes", sql.Text, sql.NewRow("日本語"), "日000"}, | ||
{"text Hello ok", sql.Text, sql.NewRow("Hello"), "H400"}, | ||
{"text Quadratically ok", sql.Text, sql.NewRow("Quadratically"), "Q36324"}, | ||
{"text Lee ok", sql.Text, sql.NewRow("Lee"), "L000"}, | ||
{"text McKnockitter ok", sql.Text, sql.NewRow("McKnockitter"), "M25236"}, | ||
{"text Honeyman ok", sql.Text, sql.NewRow("Honeyman"), "H500"}, | ||
{"text Munn ok", sql.Text, sql.NewRow("Munn"), "M000"}, | ||
{"text Poppett ok", sql.Text, sql.NewRow("Poppett"), "P300"}, | ||
{"text Peachman ok", sql.Text, sql.NewRow("Peachman"), "P250"}, | ||
{"text Cochrane ok", sql.Text, sql.NewRow("Cochrane"), "C650"}, | ||
{"text Chesley ok", sql.Text, sql.NewRow("Chesley"), "C400"}, | ||
{"text Tachenion ok", sql.Text, sql.NewRow("Tachenion"), "T250"}, | ||
{"text Wilcox ok", sql.Text, sql.NewRow("Wilcox"), "W420"}, | ||
{"binary ok", sql.Blob, sql.NewRow([]byte("Harvey")), "H610"}, | ||
{"other type", sql.Int32, sql.NewRow(int32(1)), ""}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
f := NewSoundex(expression.NewGetField(0, tt.rowType, "", true)) | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
require.Equal(t, tt.expected, eval(t, f, tt.row)) | ||
}) | ||
|
||
req := require.New(t) | ||
req.True(f.IsNullable()) | ||
req.Equal(tt.rowType, f.Type()) | ||
} | ||
} |
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.
why 4 (because type rune = int32)?
Uh oh!
There was an error while loading. Please reload this page.
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.
Even though the reference doesn't say so, MySQL returns at least four characters, if one of them is a letter:
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.
I am not sure how close this should follow MySQL, since it is not a replacement. Always returning a string of 0 or 4 characters would be a possibility, too.