linrrzqqq opened a new pull request, #71317:
URL: https://github.com/apache/doris/pull/71317
### Background
`U+FFFF` (`EF BF BF` in UTF-8) is a valid code point inside UTF-8 strings,
but `icu::CharacterIterator::DONE` (`0xFFFF`) in C++ and
`java.text.CharacterIterator.DONE` (`'\uFFFF'`) in Java use the same value as
their end-of-iteration sentinel.
### What problem does this PR solve?
Issue Number: None
Related PR: None
PR Type: Bugfix
Problem Summary:
On `master`, `INITCAP` (BE UTF-8 path) and `FunctionCallExpr.forJSON` (FE
SQL serialization for `json_*` functions) stop iterating when a character
equals `DONE`, silently truncating strings at `U+FFFF` and dropping all
trailing characters. This PR replaces both iterator loops with length-based
index traversal so `U+FFFF` and all trailing characters are preserved, and
removes the unused `Text.utf8Length` helper that relied on
`CharacterIterator.DONE`.
```text
1) BE INITCAP: "0a" + U+FFFF + "b" (HEX: 30 61 EFBFBF 62)
Before: iter != DONE (0xFFFF) --> stops at U+FFFF --> "0a"
(HEX: 3061)
After: i < length() --> keeps U+FFFF --> "0a" + U+FFFF + "B"
(HEX: 3061EFBFBF42)
2) FE EXPLAIN for json_*: "...'<U+FFFF>tail/end'"
Before: iter != DONE ('\uFFFF') --> stops at U+FFFF --> "...'"
After: i < str.length() --> keeps U+FFFF -->
"...'<U+FFFF>tail\/end'))"
```
### Details
#### 1. BE `INITCAP` UTF-8 path
```sql
-- Constructs "0a<U+FFFF>b" and "1a<U+FFFF>b" dynamically via numbers()
-- so FE constant folding does not bypass the BE INITCAP UTF-8 path.
SET enable_sql_cache = false;
SET enable_fold_constant_by_be = false;
SELECT
number,
HEX(CONCAT(CAST(number AS STRING), UNHEX('61EFBFBF62'))) AS input_hex,
HEX(INITCAP(CONCAT(CAST(number AS STRING), UNHEX('61EFBFBF62')))) AS
output_hex
FROM numbers('number'='2')
ORDER BY number;
```
Before:
```text
+--------+--------------+------------+
| number | input_hex | output_hex |
+--------+--------------+------------+
| 0 | 3061EFBFBF62 | 3061 |
| 1 | 3161EFBFBF62 | 3161 |
+--------+--------------+------------+
```
After:
```text
+--------+--------------+--------------+
| number | input_hex | output_hex |
+--------+--------------+--------------+
| 0 | 3061EFBFBF62 | 3061EFBFBF42 |
| 1 | 3161EFBFBF62 | 3161EFBFBF42 |
+--------+--------------+--------------+
```
#### 2. FE `FunctionCallExpr.forJSON` (`EXPLAIN` output for `json_*`
functions)
```sql
-- Literal string containing UTF-8 bytes EF BF BF (U+FFFF) followed by
"tail/end";
-- run via utf8mb4 client (e.g. python/mysql) so U+FFFF reaches FE
ExprToSqlVisitor.
EXPLAIN SELECT json_quote(concat(cast(number AS STRING),
'<U+FFFF>tail/end')) FROM numbers('number'='1');
```
Before (`final projections` drops `U+FFFF`, `tail/end'`, and closing `))`):
```text
OUTPUT EXPRS:
json_quote(concat(cast(number AS STRING), 'tail/end'))[#1]
final projections: json_quote(concat(CAST(_tvf_numbers.number[#0] AS
text), '
```
After (`U+FFFF` (`EF BF BF`) and escaped `tail\/end` are preserved):
```text
OUTPUT EXPRS:
json_quote(concat(cast(number AS STRING), 'tail/end'))[#1]
final projections: json_quote(concat(CAST(_tvf_numbers.number[#0] AS
text), 'tail\/end'))
```
### Testing
- BE UT (`run-be-ut.sh --run
--filter="function_string_test.function_initcap*"`), FE UT (`run-fe-ut.sh --run
org.apache.doris.analysis.ExprToSqlTest`: 119/119 passed), and Checkstyle
(`fe-common`, `fe-catalog`, `fe-core`: 0 violations) all passed.
- Regression suite `string_functions_all` (`test_string_all.groovy`) passed
on a live cluster (`Test 1 suites, failed 0 suites, fatal 0 scripts, skipped 0
scripts`).
### Release note
Fixed an issue where `INITCAP` and JSON function SQL serialization truncated
strings containing `U+FFFF`.
### Check List (For Author)
- Behavior changed: Yes (see Details)
- Does this need documentation: No
- Pre-submission status: Ready
### AI Disclosure
- Tool: Antigravity
- Generated: Code fix, unit/regression tests, and PR description
- Human review: Reviewed root cause, scope, cluster verification outputs,
and test coverage
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]