tanarda opened a new issue, #71208:
URL: https://github.com/apache/doris/issues/71208

   ### Search before asking
   
   - [x] I had searched in the 
[issues](https://github.com/apache/doris/issues?q=is%3Aissue) and found no 
similar issues.
   
   
   ### Version
   
   VeloDB Enterprise 4.1.4 (based on Apache Doris 4.1.x).
   Exact build: velodb version velodb-4.1.4-rc01-54d154ce8e8
   
   ### What's Wrong?
   
   `array_agg` (and `collect_list`) over a STRUCT with STRING fields fails in 
two-phase aggregation when a string value contains a single, unescaped double 
quote. A single quote is not required. A value ending with a backslash also 
triggers it.
   
   Error:
   ```
   [INTERNAL_ERROR]Failed to deserialize data for Struct(BIGINT, 
Nullable(String), ..., Nullable(String)) error: [INVALID_ARGUMENT]Actual struct 
field number 2 is less than schema field number 15.
   ```
   
   We hit this in a production ETL job (INSERT INTO ... SELECT ... GROUP BY 
with ARRAY_AGG(NAMED_STRUCT(...)) with 15 fields, about 3 billion source rows, 
run in chunks). The failure is intermittent: with agg_phase = 0 the planner 
picks one-phase or two-phase aggregation by cost, so the same data range passed 
in one run and failed in another.
   
   #69931 says that a value containing only a double quote works (example `x", 
"y`). That is true only when the number of double quotes is even. One odd 
double quote is enough to break deserialization.
   
   
   
   ### What You Expected?
   
   The aggregation succeeds and string values are preserved exactly, 
independent of the aggregation phase.
   
   ### How to Reproduce?
   
   <img width="1762" height="610" alt="image" 
src="https://github.com/user-attachments/assets/5c722444-1d83-4ccd-9ed4-ee8c1d58b18e";
 />
   
   <img width="1746" height="596" alt="image" 
src="https://github.com/user-attachments/assets/b130d30b-f7de-494d-8d95-3742535ba3f3";
 />
   
   <img width="1900" height="568" alt="image" 
src="https://github.com/user-attachments/assets/0117ae8e-2b75-4280-9d99-e4dc2ac99c84";
 />
   
   <img width="1790" height="522" alt="image" 
src="https://github.com/user-attachments/assets/5bcce0fd-b8de-408b-a762-325d32f11447";
 />
   
   ### Anything Else?
   
   Root cause (from reading the source, consistent with #69931):
   - The generic `AggregateFunctionCollectListData::write()/read()` serializes 
each element with `serialize_one_cell_to_json` / 
`deserialize_one_cell_from_json`, with default `FormatOptions` (`escape_char = 
0`).
   - `DataTypeStringSerDe` wraps nested strings in double quotes without 
escaping inner quotes or backslashes.
   - `DataTypeStructSerDe::deserialize_one_cell_from_json` toggles the quote 
state on every `"` or `'` and treats `\` as an escape that skips the next 
character. An odd number of `"` inside a value, or a trailing `\`, inverts the 
quote state for the rest of the struct, so the remaining field delimiters are 
never found.
   
   Observations:
   - Backslashes in the middle of a value (for example `A\B\C`) round-trip 
correctly. We compared thousands of such values with the source and found no 
differences.
   - Workaround: the statement hint `SELECT /*+ SET_VAR(agg_phase = 1) */ ...` 
works. In our ETL tool, a separate session-level `SET agg_phase = 1` did not 
take effect for the following INSERT, but the hint did.
   
   ### Are you willing to submit PR?
   
   - [ ] Yes I am willing to submit a PR!
   
   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   


-- 
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]

Reply via email to