FrankYang0529 commented on code in PR #68983:
URL: https://github.com/apache/airflow/pull/68983#discussion_r3490093042


##########
java-sdk/example/src/java/org/apache/airflow/example/XComCastingExample.java:
##########
@@ -0,0 +1,65 @@
+/*
+ * 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.
+ */
+
+package org.apache.airflow.example;
+
+import static java.lang.System.Logger.Level.INFO;
+
+import org.apache.airflow.sdk.*;
+
+// Exercises numeric XCom handling end to end: a value flows int -> long -> 
double
+// across tasks (wire integers arrive as Long, so each hop widens via Number), 
and a
+// boxed parameter stays null when the upstream XCom is absent.

Review Comment:
   I check other examples in this folder. They don't have comment for test 
cases, so I remove this. Thank you.



##########
java-sdk/processor/src/main/kotlin/org/apache/airflow/sdk/BuilderProcessor.kt:
##########
@@ -203,6 +200,45 @@ private data class RequiredXCom(
   val taskId: String,
 )
 
+private val NUMBER_ACCESSORS: Map<TypeName, String> =
+  buildMap {
+    mapOf(
+      TypeName.BYTE to "byteValue",
+      TypeName.SHORT to "shortValue",
+      TypeName.INT to "intValue",
+      TypeName.LONG to "longValue",
+      TypeName.FLOAT to "floatValue",
+      TypeName.DOUBLE to "doubleValue",
+    ).forEach { (primitive, accessor) ->
+      put(primitive, accessor)
+      put(primitive.box(), accessor)
+    }
+  }
+
+private fun xcomAccess(xcom: RequiredXCom): CodeBlock {
+  val call = CodeBlock.of($$"client.getXCom($S)", xcom.taskId)
+  val type = TypeName.get(xcom.paramType)
+  val accessor = NUMBER_ACCESSORS[type]
+  val number = ClassName.get(Number::class.java)
+  // Wire integers decode to Long and floats to Double, so a direct 
(Integer)/(Float)
+  // cast throws ClassCastException; widen via Number instead.
+  return when {
+    accessor == null -> CodeBlock.of($$"($T) $L", if (type.isPrimitive) 
type.box() else type, call)
+    // A primitive target cannot hold null; an absent XCom fails fast on 
unboxing, as before.

Review Comment:
   Removed it.



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

Reply via email to