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
Suggest similar table/column/indexes names on missing errors #685
Merged
Merged
Changes from all commits
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
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package similartext | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
func min(a, b int) int { | ||
if a < b { | ||
return a | ||
} | ||
return b | ||
} | ||
|
||
// DistanceForStrings returns the edit distance between source and target. | ||
// It has a runtime proportional to len(source) * len(target) and memory use | ||
// proportional to len(target). | ||
// Taken (simplified, for strings and with default options) from: | ||
// https://github.com/texttheater/golang-levenshtein | ||
func distanceForStrings(source, target string) int { | ||
height := len(source) + 1 | ||
width := len(target) + 1 | ||
matrix := make([][]int, 2) | ||
|
||
for i := 0; i < 2; i++ { | ||
matrix[i] = make([]int, width) | ||
matrix[i][0] = i | ||
} | ||
for j := 1; j < width; j++ { | ||
matrix[0][j] = j | ||
} | ||
|
||
for i := 1; i < height; i++ { | ||
cur := matrix[i%2] | ||
prev := matrix[(i-1)%2] | ||
cur[0] = i | ||
for j := 1; j < width; j++ { | ||
delCost := prev[j] + 1 | ||
matchSubCost := prev[j-1] | ||
if source[i-1] != target[j-1] { | ||
matchSubCost += 2 | ||
} | ||
insCost := cur[j-1] + 1 | ||
cur[j] = min(delCost, min(matchSubCost, insCost)) | ||
} | ||
} | ||
return matrix[(height-1)%2][width-1] | ||
} | ||
|
||
// MaxDistanceIgnored is the maximum Levenshtein distance from which | ||
// we won't consider a string similar at all and thus will be ignored. | ||
var DistanceSkipped = 3 | ||
|
||
// Find returns a string with suggestions for name(s) in `names` | ||
// similar to the string `src` until a max distance of `DistanceSkipped`. | ||
func Find(names []string, src string) string { | ||
if len(src) == 0 { | ||
return "" | ||
} | ||
|
||
minDistance := -1 | ||
matchMap := make(map[int][]string) | ||
|
||
for _, name := range names { | ||
dist := distanceForStrings(name, src) | ||
if dist >= DistanceSkipped { | ||
continue | ||
} | ||
|
||
if minDistance == -1 || dist < minDistance { | ||
minDistance = dist | ||
} | ||
|
||
matchMap[dist] = append(matchMap[dist], name) | ||
} | ||
|
||
if len(matchMap) == 0 { | ||
return "" | ||
} | ||
|
||
return fmt.Sprintf(", maybe you mean %s?", | ||
strings.Join(matchMap[minDistance], " or ")) | ||
} | ||
|
||
// FindFromMap does the same as Find but taking a map instead | ||
// of a string array as first argument. | ||
func FindFromMap(names interface{}, src string) string { | ||
rnames := reflect.ValueOf(names) | ||
if rnames.Kind() != reflect.Map { | ||
panic("Implementation error: non map used as first argument " + | ||
"to FindFromMap") | ||
} | ||
|
||
t := rnames.Type() | ||
if t.Key().Kind() != reflect.String { | ||
panic("Implementation error: non string key for map used as " + | ||
"first argument to FindFromMap") | ||
} | ||
|
||
var namesList []string | ||
for _, kv := range rnames.MapKeys() { | ||
namesList = append(namesList, kv.String()) | ||
} | ||
|
||
return Find(namesList, src) | ||
} |
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,52 @@ | ||
package similartext | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFind(t *testing.T) { | ||
require := require.New(t) | ||
|
||
var names []string | ||
res := Find(names, "") | ||
require.Empty(res) | ||
|
||
names = []string{"foo", "bar", "aka", "ake"} | ||
res = Find(names, "baz") | ||
require.Equal(", maybe you mean bar?", res) | ||
|
||
res = Find(names, "") | ||
require.Empty(res) | ||
|
||
res = Find(names, "foo") | ||
require.Equal(", maybe you mean foo?", res) | ||
|
||
res = Find(names, "willBeTooDifferent") | ||
require.Empty(res) | ||
|
||
res = Find(names, "aki") | ||
require.Equal(", maybe you mean aka or ake?", res) | ||
} | ||
|
||
func TestFindFromMap(t *testing.T) { | ||
require := require.New(t) | ||
|
||
var names map[string]int | ||
res := FindFromMap(names, "") | ||
require.Empty(res) | ||
|
||
names = map[string]int { | ||
"foo": 1, | ||
"bar": 2, | ||
} | ||
res = FindFromMap(names, "baz") | ||
require.Equal(", maybe you mean bar?", res) | ||
|
||
res = FindFromMap(names, "") | ||
require.Empty(res) | ||
|
||
res = FindFromMap(names, "foo") | ||
require.Equal(", maybe you mean foo?", res) | ||
} |
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
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
Oops, something went wrong.
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.