rich7420 commented on code in PR #6179:
URL: https://github.com/apache/datafusion-comet/pull/6179#discussion_r4706975165


##########
spark/src/test/scala/org/apache/comet/CometNativeCastSuite.scala:
##########
@@ -2683,6 +2683,71 @@ class CometNativeCastSuite
     }
   }
 
+  test("TRY cast of a map key that can fail is Unsupported") {
+    // https://github.com/apache/datafusion-comet/issues/5995
+    def level(from: DataType, to: DataType, mode: EvalMode.Value = 
EvalMode.TRY) =
+      CometCast.getSupportLevel(Cast(AttributeReference("m", from)(), to, 
None, mode))
+    val narrowing = (MapType(LongType, IntegerType), MapType(IntegerType, 
IntegerType))
+    val expected = Unsupported(Some(CometCast.tryCastNullMapKeyReason))
+    assert(level(narrowing._1, narrowing._2) == expected)
+    // Only TRY nulls a failing key. Legacy wraps and ANSI raises.
+    Seq(EvalMode.LEGACY, EvalMode.ANSI).foreach { mode =>
+      assert(level(narrowing._1, narrowing._2, mode).isInstanceOf[Compatible])
+    }
+    // A widening key cast cannot fail, and neither can these.
+    assert(
+      level(MapType(IntegerType, IntegerType), MapType(LongType, LongType))
+        .isInstanceOf[Compatible])
+    Seq(
+      BooleanType -> IntegerType,
+      IntegerType -> BooleanType,
+      TimestampType -> LongType,
+      StringType -> BinaryType,
+      ArrayType(LongType) -> ArrayType(IntegerType)).foreach { case (from, to) 
=>
+      assert(
+        level(MapType(from, IntegerType), MapType(to, 
IntegerType)).isInstanceOf[Compatible],
+        s"$from")
+    }
+    val structKey = (
+      StructType(Seq(StructField("a", LongType))),
+      StructType(Seq(StructField("a", IntegerType))))
+    assert(
+      level(MapType(structKey._1, IntegerType), MapType(structKey._2, 
IntegerType))
+        .isInstanceOf[Compatible])
+    // These have no native path, but are no longer refused for a null key.
+    Seq(BooleanType -> DecimalType(2, 0), IntegerType -> BinaryType, 
TimestampType -> BooleanType)
+      .foreach { case (from, to) =>
+        assert(level(MapType(from, IntegerType), MapType(to, IntegerType)) != 
expected, s"$from")

Review Comment:
   These three now use codegen dispatch instead of falling back. Could the SQL 
file pin them with `expect_dispatch(try_cast)`, since `!= expected` accepts any 
other level?



##########
spark/src/test/resources/sql-tests/expressions/cast/try_cast_map_null_key.sql:
##########
@@ -0,0 +1,94 @@
+-- Licensed to the Apache Software Foundation (ASF) under one
+-- or more contributor license agreements.  See the NOTICE file
+-- distributed with this work for additional information
+-- regarding copyright ownership.  The ASF licenses this file
+-- to you under the Apache License, Version 2.0 (the
+-- "License"); you may not use this file except in compliance
+-- with the License.  You may obtain a copy of the License at
+--
+--   http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing,
+-- software distributed under the License is distributed on an
+-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+-- KIND, either express or implied.  See the License for the
+-- specific language governing permissions and limitations
+-- under the License.
+-- A TRY cast that narrows a map key turns an out-of-range key into a null 
key, and Spark keeps the
+-- row. Arrow's map format cannot hold a null key, so these casts must fall 
back to Spark rather
+-- than fail. See #5995. What Spark returns is itself inconsistent between 
readers: its map holds a
+-- null key internally, map_keys shows the null, and the other readers see 0.
+
+statement
+CREATE TABLE test_try_cast_map_key(id int, m map<bigint, int>) USING parquet
+
+statement
+INSERT INTO test_try_cast_map_key VALUES
+  (1, map(1, 10, 9999999999, 20)),
+  (2, map(2, 30)),
+  (3, map()),
+  (4, NULL)
+
+query expect_fallback(map key that can fail)
+SELECT id, try_cast(m AS map<int, int>) FROM test_try_cast_map_key ORDER BY id
+
+query expect_fallback(map key that can fail)
+SELECT id, map_keys(try_cast(m AS map<int, int>)) FROM test_try_cast_map_key 
ORDER BY id
+
+query expect_fallback(map key that can fail)
+SELECT id, element_at(try_cast(m AS map<int, int>), 0) FROM 
test_try_cast_map_key ORDER BY id
+
+-- A plain cast wraps the key instead of nulling it, so it stays native
+query expect_native(cast)
+SELECT id, cast(m AS map<int, int>) FROM test_try_cast_map_key ORDER BY id
+
+statement
+CREATE TABLE test_try_cast_map_key_widen(id int, m map<int, int>) USING parquet
+
+statement
+INSERT INTO test_try_cast_map_key_widen VALUES (1, map(1, 10, 2147483647, 
20)), (2, NULL)
+
+-- A widening key cast cannot fail, so it cannot produce a null key and stays 
native
+query expect_native(try_cast)
+SELECT id, try_cast(m AS map<bigint, bigint>) FROM test_try_cast_map_key_widen 
ORDER BY id

Review Comment:
   Could we add a control where only the value cast fails, casting 
`map<int,bigint>` to `map<bigint,int>` with value `9999999999`? It stays native 
with a NULL value for me.



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