Skip to content

Commit

Permalink
builtins: add metaphone() sql built-in
Browse files Browse the repository at this point in the history
  • Loading branch information
charlespnh committed Sep 27, 2023
1 parent d773874 commit 0885b00
Show file tree
Hide file tree
Showing 7 changed files with 414 additions and 6 deletions.
2 changes: 2 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,8 @@ available replica will error.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="levenshtein"></a><code>levenshtein(source: <a href="string.html">string</a>, target: <a href="string.html">string</a>, ins_cost: <a href="int.html">int</a>, del_cost: <a href="int.html">int</a>, sub_cost: <a href="int.html">int</a>) &rarr; <a href="int.html">int</a></code></td><td><span class="funcdesc"><p>Calculates the Levenshtein distance between two strings. The cost parameters specify how much to charge for each edit operation. Maximum input length is 255 characters.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="metaphone"></a><code>metaphone(source: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Compute ‘sound-like’ string from the input string.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="soundex"></a><code>soundex(source: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Convert a string to its Soundex code.</p>
</span></td><td>Immutable</td></tr></tbody>
</table>
Expand Down
26 changes: 23 additions & 3 deletions pkg/sql/sem/builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -3915,10 +3915,30 @@ value if you rely on the HLC for accuracy.`,
Info: "Calculates the Levenshtein distance between two strings. The cost parameters specify how much to " +
"charge for each edit operation. Maximum input length is 255 characters.",
Volatility: volatility.Immutable,
}),
},
),
"levenshtein_less_equal": makeBuiltin(tree.FunctionProperties{UnsupportedWithIssue: 56820, Category: builtinconstants.CategoryFuzzyStringMatching}),
"metaphone": makeBuiltin(tree.FunctionProperties{UnsupportedWithIssue: 56820, Category: builtinconstants.CategoryFuzzyStringMatching}),
"dmetaphone_alt": makeBuiltin(tree.FunctionProperties{UnsupportedWithIssue: 56820, Category: builtinconstants.CategoryFuzzyStringMatching}),
"metaphone": makeBuiltin(
tree.FunctionProperties{Category: builtinconstants.CategoryFuzzyStringMatching},
tree.Overload{
Types: tree.ParamTypes{{Name: "source", Typ: types.String}},
ReturnType: tree.FixedReturnType(types.String),
Fn: func(_ context.Context, _ *eval.Context, args tree.Datums) (tree.Datum, error) {
const maxLen = 255
s := string(tree.MustBeDString(args[0]))
if (len(s) > maxLen) {
return nil, pgerror.Newf(pgcode.InvalidParameterValue,
"metaphone argument exceeds maximum length of %d characters", maxLen)
}
m := fuzzystrmatch.Metaphone(s)
return tree.NewDString(m), nil
},
Info: "Compute 'sound-like' string from the input string.",
Volatility: volatility.Immutable,
},
),
"dmetaphone": makeBuiltin(tree.FunctionProperties{UnsupportedWithIssue: 56820, Category: builtinconstants.CategoryFuzzyStringMatching}),
"dmetaphone_alt": makeBuiltin(tree.FunctionProperties{UnsupportedWithIssue: 56820, Category: builtinconstants.CategoryFuzzyStringMatching}),

// JSON functions.
// The behavior of both the JSON and JSONB data types in CockroachDB is
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/sem/builtins/fixed_oids.go
Original file line number Diff line number Diff line change
Expand Up @@ -2463,6 +2463,7 @@ var builtinOidsArray = []string{
2492: `make_timestamptz(year: int, month: int, day: int, hour: int, min: int, sec: float, timezone: string) -> timestamptz`,
2493: `date_trunc(element: string, input: timestamptz, timezone: string) -> timestamptz`,
2494: `make_date(year: int, month: int, day: int) -> date`,
2495: `metaphone(source: string) -> string`,
}

var builtinOidsBySignature map[string]oid.Oid
Expand Down
2 changes: 2 additions & 0 deletions pkg/util/fuzzystrmatch/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_library(
name = "fuzzystrmatch",
srcs = [
"leven.go",
"metaphone.go",
"soundex.go",
],
importpath = "github.com/cockroachdb/cockroach/pkg/util/fuzzystrmatch",
Expand All @@ -15,6 +16,7 @@ go_test(
size = "small",
srcs = [
"leven_test.go",
"metaphone_test.go",
"soundex_test.go",
],
args = ["-test.timeout=55s"],
Expand Down

0 comments on commit 0885b00

Please sign in to comment.