rahil-c commented on code in PR #17904:
URL: https://github.com/apache/hudi/pull/17904#discussion_r2766137884


##########
hudi-client/hudi-spark-client/src/main/scala/org/apache/spark/sql/execution/datasources/SparkSchemaTransformUtils.scala:
##########
@@ -0,0 +1,426 @@
+/*
+ * 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.spark.sql.execution.datasources
+
+import org.apache.spark.sql.HoodieSchemaUtils
+import 
org.apache.spark.sql.catalyst.expressions.codegen.GenerateUnsafeProjection
+import org.apache.spark.sql.catalyst.expressions.{ArrayTransform, Attribute, 
AttributeReference, Cast, CreateNamedStruct, CreateStruct, Expression, 
GetStructField, LambdaFunction, Literal, MapEntries, MapFromEntries, 
NamedLambdaVariable, UnsafeProjection}
+import org.apache.spark.sql.types.{ArrayType, DataType, DateType, DecimalType, 
DoubleType, FloatType, IntegerType, LongType, MapType, StringType, StructField, 
StructType, TimestampNTZType}
+
+/**
+ * Format-agnostic utilities for Spark schema transformations including NULL 
padding
+ * and recursive type casting with workarounds for unsafe conversions.
+ *
+ * These utilities are used by file format readers that need to:
+ * - Pad missing columns with NULL literals (required for Lance)
+ * - Handle nested struct/array/map type conversions
+ * - Work around Spark unsafe cast issues (float->double, numeric->decimal)
+ *
+ * Note: The following functions were originally part of 
HoodieParquetFileFormatHelper
+ * and have been moved here to allow reuse across multiple file formats:
+ * - buildImplicitSchemaChangeInfo
+ * - isDataTypeEqual
+ * - generateUnsafeProjection
+ * - hasUnsupportedConversion
+ * - recursivelyCastExpressions
+ */
+object SparkSchemaTransformUtils {
+
+  /**
+   * Generate UnsafeProjection for type casting with special handling for 
unsupported conversions.
+   *
+   * @param fullSchema Complete schema including data and partition columns
+   * @param timeZoneId Session timezone for timestamp conversions
+   * @param typeChangeInfos Map of field index to (targetType, readerType) for 
fields needing casting
+   * @param requiredSchema Schema requested by the query (data columns only)
+   * @param partitionSchema Schema of partition columns
+   * @param schemaUtils Spark adapter schema utilities
+   * @return UnsafeProjection that applies type casting to rows
+   */
+  def generateUnsafeProjection(fullSchema: Seq[Attribute],
+                               timeZoneId: Option[String],
+                               typeChangeInfos: java.util.Map[Integer, 
org.apache.hudi.common.util.collection.Pair[DataType, DataType]],
+                               requiredSchema: StructType,
+                               partitionSchema: StructType,
+                               schemaUtils: HoodieSchemaUtils): 
UnsafeProjection = {
+    if (typeChangeInfos.isEmpty) {

Review Comment:
   In the original 
[impl](https://github.com/apache/hudi/blob/master/hudi-client/hudi-spark-client/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/HoodieParquetFileFormatHelper.scala#L110)
 this `addCastCache` was defined in `generateUnsafeProjection` but was only 
used by `hasUnsupportedConversion`function.
   
   In the current implementation the addedCastCache is now moved inside the 
`hasUnsupportedConversion`  
https://github.com/apache/hudi/pull/17904/changes#diff-56d3b110e2b04263ed60368227bddd9bef085799f4917701f936cbc9f7f71572R312
   <img width="1162" height="562" alt="Screenshot 2026-02-04 at 1 50 13 PM" 
src="https://github.com/user-attachments/assets/a4b5dd9d-ea67-4e18-9cea-38105094e775";
 />
   



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