Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtins: Implement unaccent function #54628

Merged
merged 1 commit into from Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/generated/sql/functions.md
Expand Up @@ -2432,6 +2432,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
15 changes: 15 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/builtin_function
Expand Up @@ -102,6 +102,21 @@ SELECT upper('roacH7')
upper
ROACH7

query T
SELECT unaccent(str) FROM ( VALUES
('no_special_CHARACTERS1!'),
('Żółć'),
('⃞h̀ELLO`̀́⃞'),
('Softhyphen­separator'),
('some ̂thing')
) tbl(str)
----
no_special_CHARACTERS1!
Zolc
hELLO`
Softhyphen-separator
something

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

Expand Down
21 changes: 21 additions & 0 deletions pkg/sql/sem/builtins/builtins.go
Expand Up @@ -61,6 +61,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/timeofday"
"github.com/cockroachdb/cockroach/pkg/util/timetz"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/unaccent"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/errors"
"github.com/knz/strtime"
Expand Down Expand Up @@ -260,6 +261,26 @@ var builtins = map[string]builtinDefinition{
),
),

"unaccent": makeBuiltin(tree.FunctionProperties{Category: categoryString},
stringOverload1(
func(evalCtx *tree.EvalContext, s string) (tree.Datum, error) {
var b strings.Builder
for _, ch := range s {
v, ok := unaccent.Dictionary[ch]
if ok {
b.WriteString(v)
} else {
b.WriteRune(ch)
}
}
return tree.NewDString(b.String()), 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