Skip to content

Commit

Permalink
builtins: Implement unaccent function
Browse files Browse the repository at this point in the history
Unaccent function should work the same as in PostgreSQL.

Release note (sql change): Implement the string builtin `unaccent`.
  • Loading branch information
mknycha committed Sep 21, 2020
1 parent d17d32b commit 546cf70
Show file tree
Hide file tree
Showing 4 changed files with 1,559 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/generated/sql/functions.md
Expand Up @@ -2430,6 +2430,8 @@ The swap_ordinate_string parameter is a 2-character string naming the ordinates
<tr><td><a name="translate"></a><code>translate(input: <a href="string.html">string</a>, find: <a href="string.html">string</a>, replace: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>In <code>input</code>, replaces the first character from <code>find</code> with the first character in <code>replace</code>; repeat for each character in <code>find</code>.</p>
<p>For example, <code>translate('doggie', 'dog', '123');</code> returns <code>1233ie</code>.</p>
</span></td></tr>
<tr><td><a name="unaccent"></a><code>unaccent(val: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Removes accents (diacritic signs) from the text provided in <code>val</code>.</p>
</span></td></tr>
<tr><td><a name="upper"></a><code>upper(val: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Converts all characters in <code>val</code> to their to their upper-case equivalents.</p>
</span></td></tr></tbody>
</table>
Expand Down
20 changes: 20 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/builtin_function
Expand Up @@ -102,6 +102,26 @@ SELECT upper('roacH7')
upper
ROACH7

query T
SELECT unaccent('no_special_CHARACTERS1!')
----
no_special_CHARACTERS1!

query T
SELECT unaccent('Żółć')
----
Zolc

query T
SELECT unaccent('⃞h̀ELLO`̀́⃞')
----
hELLO`

query T
SELECT unaccent('some­thing')
----
some-thing

statement error unknown signature: upper\(decimal\)
SELECT upper(2.2)

Expand Down
18 changes: 18 additions & 0 deletions pkg/sql/sem/builtins/builtins.go
Expand Up @@ -260,6 +260,24 @@ var builtins = map[string]builtinDefinition{
),
),

"unaccent": makeBuiltin(tree.FunctionProperties{Category: categoryString},
stringOverload1(
func(evalCtx *tree.EvalContext, s string) (tree.Datum, error) {
separatedStrings := strings.Split(s, "")
for i, ss := range separatedStrings {
v, ok := unaccentDictionary[ss]
if ok {
separatedStrings[i] = v
}
}
return tree.NewDString(strings.Join(separatedStrings, "")), nil
},
types.String,
"Removes accents (diacritic signs) from the text provided in `val`.",
tree.VolatilityImmutable,
),
),

"upper": makeBuiltin(tree.FunctionProperties{Category: categoryString},
stringOverload1(
func(evalCtx *tree.EvalContext, s string) (tree.Datum, error) {
Expand Down

0 comments on commit 546cf70

Please sign in to comment.