morningman opened a new pull request, #69025:
URL: https://github.com/apache/doris/pull/69025
### What problem does this PR solve?
Issue Number: None
Related PR: #68968
Problem Summary:
**In short.** FE constant folding cast a VARBINARY constant to a string type
as the hex text of its bytes, while BE keeps the bytes. Every folded expression
that takes a VARBINARY constant through such a cast returned a different result
from the same expression evaluated by BE, including `hex()` and `to_base64()`
of a VARBINARY constant. This came up while testing #68968.
**Background.** `hex()` and `to_base64()` only have string signatures, so a
VARBINARY argument reaches them through an implicit `CAST(... AS VARCHAR)`. BE
implements `CAST(VARBINARY AS VARCHAR/STRING)` by keeping the bytes as they
are. When the argument is a constant, `FoldConstantRuleOnFE` folds that cast
with `Literal.checkedCastTo`, which builds the string from
`VarBinaryLiteral.castValueToString()`; that method returned `toString()`, the
base16 text of the bytes.
**The problem, and what it cost.**
| Query | folded on FE (master) | evaluated by BE
(`debug_skip_fold_constant=true`) / with this PR |
|---|---|---|
| `SELECT hex(cast('abc' AS varbinary(16)))` | `363136323633` | `616263` |
| `SELECT to_base64(cast('abc' AS varbinary(16)))` | `NjE2MjYz` | `YWJj` |
| `SELECT length(cast(cast('abc' AS varbinary(16)) AS string))` | 6 | 3 |
| `SELECT hex(X'FF')` | `4646` | `FF` |
The plan shows the difference: master sends `hex('616263')` to BE, while
with folding disabled it sends `hex(CAST(X'616263' AS varchar(65533)))`. The
same wrong value appears for a column read from a constant subquery or a view.
**How this PR fixes it.**
- `VarBinaryLiteral.castValueToString()` decodes the bytes as UTF-8
(`CodingErrorAction.REPORT`), so the folded string carries exactly the bytes BE
keeps; valid UTF-8 round-trips unchanged when the literal is sent to BE.
- Bytes that are not valid UTF-8 cannot be carried by an FE string literal,
so `castValueToString()` throws an `AnalysisException` for them.
`FoldConstantRuleOnFE.visitCast` turns only a `CastException` into NULL and
returns any other failing cast unfolded, so BE evaluates it (`hex(X'FF')` is
`FF`). A direct caller of `checkedCastTo` gets the exception.
**Results.** With this PR, every query in the table returns the BE result
whether or not constant folding is enabled, on a local cluster. The FE UT fails
when `castValueToString()` returns the hex text again (`expected: <'abc'> but
was: <'616263'>`). The existing `.out` of
`external_table_p0/dialect_compatible/sql/presto/scalar/TestVarbinaryFunctions`
already expects the BE values (`YWJj` for `to_base64(CAST('abc' AS
VARBINARY))`).
### Release note
Fix wrong results of constant expressions that cast a VARBINARY value to a
string, including `hex()` and `to_base64()` of a VARBINARY constant.
### Check List (For Author)
- Test <!-- At least one of them must be included. -->
- [x] Regression test
-
`query_p0/sql_functions/binary_functions/test_varbinary_cast_fold`: `hex()`,
`to_base64()`, `length()` and casts of VARBINARY constants (valid UTF-8,
multi-byte, `X'FF'`, empty), run with constant folding on and off; the two
result blocks match. `.out` generated by the script.
- [x] Unit Test
- `FoldConstantTest.testCastVarBinaryToStringFold`: the cast folds
to the bytes for ASCII and multi-byte UTF-8, and stays unfolded for `X'FF'`.
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No need to test or manual test. Explain why:
- [ ] This is a refactor/code format and no logic has been changed.
- [ ] Previous test can cover this change.
- [ ] No code files have been changed.
- [ ] Other reason <!-- Add your reason? -->
- Behavior changed:
- [ ] No.
- [x] Yes. <!-- Explain the behavior change -->
- A folded cast of a VARBINARY constant to a string type returns its
bytes instead of their hex text, as BE does.
- Does this need documentation?
- [x] No.
- [ ] Yes. <!-- Add document PR link here. eg:
https://github.com/apache/doris-website/pull/1214 -->
### Check List (For Reviewer who merge this PR)
- [ ] Confirm the release note
- [ ] Confirm test cases
--
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]