matanper opened a new issue, #71153: URL: https://github.com/apache/doris/issues/71153
### 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 master (`be/src/exprs/function/cast/variant_v2/cast_variant_to_array.cpp`, `cast_variant_to_scalar.cpp`); the same code is in branch-4.1. ### What's Wrong? `CAST(<variant> AS ARRAY<VARIANT>)` (and `ARRAY<STRING>`, `ARRAY<JSONB>`, `ARRAY<INT>`, ...) with Variant V2 can return an `ARRAY` whose elements are not `Nullable`, while the declared type is `Array(Nullable(...))`. When the cast result is materialized as a projected column, the type check rejects it: ``` [INTERNAL_ERROR]ColumnWithTypeAndName check column type failed, column name: (CAST event.authorizations(Nullable(Variant(max subcolumns count = 2048, enable doc mode = false))) TO Nullable(Array(Nullable(Variant(max subcolumns count = 2048, enable doc mode = false))))), type: Nullable(Array(Nullable(Variant(...)))), column: Nullable(Array(variant_v2)), error: [INTERNAL_ERROR]Column type variant_v2 is not compatible with data type Nullable(Variant(...)) DataTypeNullable::check_column data_type_nullable.cpp DataTypeArray::check_column data_type_array.cpp ColumnWithTypeAndName::check_type_and_column_match VExprContext::execute Scanner::_do_projections ``` Cause: - `cast_variant_to_array.cpp:93-102` (`finalize_collected_node`) builds the array's element column from the leaf node and puts it straight into `ColumnArray::create`. - For an element type of VARIANT, the leaf is `variant_column_from_refs(...)` followed by `apply_forced_nulls(...)` (`:115-116`). The string, JSONB and scalar leaves go through the same helper. - `apply_forced_nulls` (`cast_variant_to_scalar.cpp:569`) returns the column unwrapped when the null map is empty (`:573-575`). - The leaf's null map has one entry per array element, so it is empty exactly when the block holds no array elements: every row is NULL, an empty array, or not an array. Such a block yields `Array(variant_v2)` instead of `Array(Nullable(variant_v2))`. - Blocks with at least one element get a non-empty null map and come out right, which makes the failure depend on how rows fall into blocks. ### What You Expected? The cast returns `Nullable(Array(Nullable(T)))` for every block, including blocks without any array elements. ### How to Reproduce? A table with a VARIANT column `v` where some rows have an array at `v['arr']` and others do not. Use the cast twice, so the planner materializes it as a projected column: ```sql SELECT sum(array_count(x -> x IS NOT NULL, CAST(v['arr'] AS ARRAY<VARIANT>))), sum(array_size(CAST(v['arr'] AS ARRAY<VARIANT>))) FROM t WHERE v['arr'] IS NULL; -- every scanned block has no array elements ``` This fails with the error above. The same query with `WHERE v['arr'] IS NOT NULL` succeeds, as does a query that uses the cast once. A unit-level reproduction: cast a Variant V2 batch of `NULL`, `[]` and `7` to `ARRAY<Nullable(VARIANT)>` (or `ARRAY<Nullable(STRING)>`), then check `make_nullable(target)->check_column(*result)`. The check fails because the array's data column is not a `ColumnNullable`. ### Anything Else? Wrapping the child in `finalize_collected_node` before building the array fixes every element type at once. `make_nullable` keeps a column that is already nullable: ```cpp RETURN_IF_ERROR(finalize_collected_node(state, *node.child, &child)); child = make_nullable(child); ``` We have a patch with a unit test and can open a PR. ### Are you willing to submit PR? - [x] 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]
