Zachgille opened a new issue, #27861:
URL: https://github.com/apache/datafusion/issues/27861

   ### Describe the bug
   
   A plain (type-only) `CAST` or `TRY_CAST` to the same underlying type strips 
Arrow extension metadata (`ARROW:extension:name` and 
`ARROW:extension:metadata`) when the source field carries 
`ARROW:extension:name`. However, if the source field carries 
`ARROW:extension:metadata` without `ARROW:extension:name`, both `CAST` and 
`TRY_CAST` leave `ARROW:extension:metadata` attached to the result instead of 
stripping it.
   
   This also leaks into `CREATE TABLE ... AS SELECT` from a constant 
expression, because constant folding evaluates the physical cast.
   
   ### To Reproduce
   
   Tested against `main` at `f2b60645e` (in `sqllogictest`):
   
   ```sql
   -- Constant-folded CAST and TRY_CAST leak ARROW:extension:metadata:
   SELECT
     arrow_metadata(CAST(with_metadata(arrow_cast(1, 'Int32'), 
'ARROW:extension:metadata', '{"stale": 1}') AS INT), 
'ARROW:extension:metadata') AS cast_meta,
     arrow_metadata(TRY_CAST(with_metadata(arrow_cast(1, 'Int32'), 
'ARROW:extension:metadata', '{"stale": 1}') AS INT), 
'ARROW:extension:metadata') AS try_cast_meta;
   -- Actual:   {"stale": 1}  {"stale": 1}
   -- Expected: NULL          NULL
   
   -- Table-backed CAST and TRY_CAST also leak ARROW:extension:metadata:
   CREATE TABLE src AS
   SELECT with_metadata(arrow_cast(1, 'Int32'), 'ARROW:extension:metadata', 
'{"stale": 1}') AS v;
   
   SELECT
     arrow_metadata(CAST(v AS INT), 'ARROW:extension:metadata') AS cast_meta,
     arrow_metadata(TRY_CAST(v AS INT), 'ARROW:extension:metadata') AS 
try_cast_meta
   FROM src;
   -- Actual:   {"stale": 1}  {"stale": 1}
   -- Expected: NULL          NULL
   
   -- CTAS from a constant keeps it too:
   CREATE TABLE t AS
   SELECT CAST(with_metadata(arrow_cast(1, 'Int32'), 
'ARROW:extension:metadata', '{"stale": 1}') AS INT) AS w;
   
   SELECT arrow_metadata(w, 'ARROW:extension:metadata') FROM t;
   -- Actual:   {"stale": 1}
   -- Expected: NULL
   
   -- Control: when ARROW:extension:name is also present, both keys are 
stripped as expected:
   SELECT arrow_metadata(
     CAST(with_metadata(with_metadata(arrow_cast(1, 'Int32'), 
'ARROW:extension:name', 'example.ext'), 'ARROW:extension:metadata', '{"stale": 
1}') AS INT),
     'ARROW:extension:metadata'
   );
   -- Actual: NULL
   ```
   
   ### Expected behavior
   
   All five values from the orphan `ARROW:extension:metadata` inputs should be 
`NULL`. That is, `ARROW:extension:metadata` should be stripped, as the logical 
plan and the control query do, while regular non-extension metadata keys are 
preserved.
   
   ### Additional context
   
   The same-type physical cast elision checks only for 
`EXTENSION_TYPE_NAME_KEY` before returning the input expression unchanged:
   
   - `cast_with_target_field`: 
`datafusion/physical-expr/src/expressions/cast.rs` 
[L858–866](https://github.com/apache/datafusion/blob/ff5f25375d5d6ed1553820817e0b435af96f012f/datafusion/physical-expr/src/expressions/cast.rs#L858-L866)
   - `try_cast_with_target_field`: 
`datafusion/physical-expr/src/expressions/try_cast.rs` 
[L331–339](https://github.com/apache/datafusion/blob/ff5f25375d5d6ed1553820817e0b435af96f012f/datafusion/physical-expr/src/expressions/try_cast.rs#L331-L339)
   
   Meanwhile, the logical cast (`datafusion/expr/src/expr_schema.rs` L94–96) 
and the physical type-only `return_field` paths (`cast.rs` L328–330, 
`try_cast.rs` L170–172) both strip `EXTENSION_TYPE_NAME_KEY` and 
`EXTENSION_TYPE_METADATA_KEY`. Checking both keys before eliding the cast 
aligns the physical skip with the rest of the cast paths.
   
   Related, and intentionally out of scope unless maintainers want them changed 
too: `ArrowCastFunc::simplify` (`datafusion/functions/src/core/arrow_cast.rs` 
L158–161) and `ArrowTryCastFunc::simplify` 
(`datafusion/functions/src/core/arrow_try_cast.rs` L131–134) have the same 
name-only check during logical simplification. There, the logical and physical 
plans still agree.
   
   AI assistance: I used AI tools to investigate these code paths and verify 
the repros. I reviewed the code paths myself.
   
   ### Willingness to contribute
   
   I plan to contribute a fix for this bug independently


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