Commit aee5cd9
fix(functions): Make translate function postgres compatible (#19630)
## Which issue does this PR close?
- Closes #.
## Rationale for this change
In Postgres `translate` function implementation, the duplicates in the
`from` argument are ignored, and the first occurrence wins.
A similar implementation is also present in DuckDB.
If the character already exists, the index/mapping is not updated.
https://github.com/duckdb/duckdb/blob/4b7a6b7bd0f8c968bfecab08e801cdc1f0a5cdfd/extension/core_functions/scalar/string/translate.cpp#L45
Before the change in DataFusion
```
> SELECT translate('abcabc', 'aa', 'de');
+-------------------------------------------------+
| translate(Utf8("abcabc"),Utf8("aa"),Utf8("de")) |
+-------------------------------------------------+
| ebcebc |
+-------------------------------------------------+
1 row(s) fetched.
Elapsed 0.001 seconds.
```
While DuckDB returns
```
D SELECT translate('abcabc', 'aa', 'de');
┌─────────────────────────────────┐
│ translate('abcabc', 'aa', 'de') │
│ varchar │
├─────────────────────────────────┤
│ dbcdbc │
└─────────────────────────────────┘
```
Postgres returns
```
SELECT translate('abcabc', 'aa', 'de')
Output:
translate
-----------
dbcdbc
(1 row)
```
## What changes are included in this PR?
- If there are duplicate characters present in `from`, the first
occurrence wins.
- SLT Tests update. The earlier tests `from` value was `foo` which
contained duplicates and was mapping `o` -> `r` instead of Postgress
compatible `o` -> `a`
## Are these changes tested?
- New Unit Tests are added, which test this behaviour.
## Are there any user-facing changes?
This is a contract change in some sense. If someone has taken dependency
on this behaviour, they will encounter a change. However, I am unsure
and need help to properly categorize this.
---------
Co-authored-by: Devanshu <[email protected]>1 parent ff38480 commit aee5cd9
File tree
2 files changed
+19
-6
lines changed- datafusion
- functions/src/unicode
- sqllogictest/test_files/string
2 files changed
+19
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
173 | | - | |
| 173 | + | |
| 174 | + | |
174 | 175 | | |
175 | 176 | | |
176 | 177 | | |
| |||
259 | 260 | | |
260 | 261 | | |
261 | 262 | | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
262 | 275 | | |
263 | 276 | | |
264 | 277 | | |
| |||
Lines changed: 5 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
528 | 528 | | |
529 | 529 | | |
530 | 530 | | |
531 | | - | |
| 531 | + | |
532 | 532 | | |
533 | 533 | | |
534 | 534 | | |
| |||
542 | 542 | | |
543 | 543 | | |
544 | 544 | | |
545 | | - | |
546 | | - | |
547 | | - | |
548 | | - | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
549 | 549 | | |
550 | 550 | | |
551 | 551 | | |
| |||
0 commit comments