You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "zhengruifeng (via GitHub)" <gi...@apache.org> on 2026/08/19 13:56:30 UTC

[PR] [SPARK-58875][ML][SQL] Add WrapUDT expression for vector UDT conversion [spark]

zhengruifeng opened a new pull request, #58124:
URL: https://github.com/apache/spark/pull/58124

   ### What changes were proposed in this pull request?
   
   This PR adds a Catalyst `WrapUDT` expression that wraps a column with a target UDT when the
   column data type matches the UDT's underlying SQL type.
   
   It also updates `MLUtils.convertVectorColumnsToML` and `MLUtils.convertVectorColumnsFromML` to
   use `WrapUDT(UnwrapUDT(...))` for `.ml` / `.mllib` vector UDT conversion instead of Scala UDFs.
   The matrix conversion paths are unchanged.
   
   ### Why are the changes needed?
   
   `.ml` and `.mllib` vector UDTs use the same Catalyst storage layout, but the current conversion
   helpers deserialize each vector and create a new vector object through `asML` / `Vectors.fromML`.
   
   `WrapUDT` allows these helpers to change the logical UDT of a column without copying the
   underlying Catalyst value, avoiding per-row UDF conversion overhead for vector columns.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. Existing vector conversion helper APIs keep the same behavior and return schemas, but use a
   more efficient internal implementation.
   
   ### How was this patch tested?
   
   Added tests for `WrapUDT` positive and negative type checking.
   
   Updated `MLUtils` vector conversion tests to verify converted DataFrames use `WrapUDT` and do not
   use `ScalaUDF`.
   
   Ran:
   
   ```
   build/sbt -java-home /usr/lib/jvm/java-17-openjdk-amd64 catalyst/Test/compile mllib/Test/compile
   ```
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: OpenAI Codex (GPT-5)
   


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-58875][SQL][ML][PYTHON][CONNECT] Add WrapUDT expression for zero-copy linalg conversion [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #58124:
URL: https://github.com/apache/spark/pull/58124#discussion_r3820988253


##########
sql/api/src/main/scala/org/apache/spark/sql/functions.scala:
##########
@@ -17723,6 +17723,32 @@ object functions {
    */
   def unwrap_udt(column: Column): Column = Column.internalFn("unwrap_udt", column)
 
+  /**
+   * Wrap a column as a user-defined type.
+   * @param column
+   *   the column to wrap. The column data type must match the UDT's underlying SQL type.
+   * @param udt
+   *   the target user-defined type.
+   * @group udf_funcs
+   * @since 4.4.0
+   */
+  def wrap_udt(column: Column, udt: UserDefinedType[_]): Column = {
+    wrap_udt(column, lit(udt.json))
+  }
+
+  /**
+   * Wrap a column as a user-defined type.
+   * @param column
+   *   the column to wrap. The column data type must match the UDT's underlying SQL type.
+   * @param udt
+   *   the target user-defined type as a constant JSON string column.
+   * @group udf_funcs
+   * @since 4.4.0
+   */
+  def wrap_udt(column: Column, udt: Column): Column = {

Review Comment:
   **Non-blocking:**
   
   Could we rename this parameter to `udtJson` or `udtJsonColumn`? Unlike the other overload, this `Column` carries the constant JSON type descriptor rather than a UDT value, and the current name makes that distinction easy to miss for named-argument callers.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-58875][SQL][ML][PYTHON][CONNECT] Add WrapUDT expression for zero-copy linalg conversion [spark]

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on PR #58124:
URL: https://github.com/apache/spark/pull/58124#issuecomment-5391845184

   **Merge Summary:**
   - merged into master https://github.com/apache/spark/commit/9299b130825deb4d80b4835001a0cb96f6a09094
   - merged into branch-4.x https://github.com/apache/spark/commit/0a4ab0d9130a51c1aecadf412a849474372582c3
   
   *Posted by `merge_spark_pr.py`*


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-58875][SQL][ML][PYTHON][CONNECT] Add WrapUDT expression for zero-copy linalg conversion [spark]

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #58124:
URL: https://github.com/apache/spark/pull/58124#discussion_r3821430135


##########
sql/api/src/main/scala/org/apache/spark/sql/functions.scala:
##########
@@ -17723,6 +17723,32 @@ object functions {
    */
   def unwrap_udt(column: Column): Column = Column.internalFn("unwrap_udt", column)
 
+  /**
+   * Wrap a column as a user-defined type.
+   * @param column
+   *   the column to wrap. The column data type must match the UDT's underlying SQL type.
+   * @param udt
+   *   the target user-defined type.
+   * @group udf_funcs
+   * @since 4.4.0
+   */
+  def wrap_udt(column: Column, udt: UserDefinedType[_]): Column = {
+    wrap_udt(column, lit(udt.json))
+  }
+
+  /**
+   * Wrap a column as a user-defined type.
+   * @param column
+   *   the column to wrap. The column data type must match the UDT's underlying SQL type.
+   * @param udt
+   *   the target user-defined type as a constant JSON string column.
+   * @group udf_funcs
+   * @since 4.4.0
+   */
+  def wrap_udt(column: Column, udt: Column): Column = {

Review Comment:
   Thanks for the suggestion. I agree `udtJsonColumn` is more explicit, but it seems conventional in this file to keep the semantic argument name stable across overloads when only the accepted representation changes.
   
   For example, `from_json` keeps the parameter name as `schema` across `StructType`, `DataType`, `String`, and `Column` overloads, and `from_csv` does the same for `StructType` and `Column` schema overloads. Following that convention, I would prefer to keep this overload as `udt` and clarify in the Scaladoc that the `Column` must be a constant UDT JSON string.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-58875][SQL][ML][PYTHON][CONNECT] Add WrapUDT expression for zero-copy linalg conversion [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #58124:
URL: https://github.com/apache/spark/pull/58124#discussion_r3826617490


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/WrapUDT.scala:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.catalyst.expressions
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
+import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.DataTypeMismatch
+import org.apache.spark.sql.catalyst.expressions.Cast._
+import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode}
+import org.apache.spark.sql.catalyst.types.DataTypeUtils
+import org.apache.spark.sql.catalyst.util.TypeUtils.ordinalNumber
+import org.apache.spark.sql.types.{DataType, UserDefinedType}
+
+/**
+ * Wrap a column with a UDT whose underlying SQL type matches the column data type.
+ *
+ * @see [[UnwrapUDT]] for converting a UDT column to its underlying SQL type.
+ */
+case class WrapUDT(child: Expression, udt: UserDefinedType[_])
+  extends UnaryExpression with NonSQLExpression {
+
+  def this(expressions: Seq[Expression]) = {

Review Comment:
   **Blocking:**
   
   Use a fixed `(Expression, Expression)` constructor so `FunctionRegistry` enforces the function's two-argument contract. A `Seq[Expression]` constructor selects the variadic builder, so Connect's public `call_udf` path can pass extra arguments that are silently ignored; too few arguments fail through sequence indexing instead of Spark's standard wrong-arity analysis error.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-58875][SQL][ML][PYTHON][CONNECT] Add WrapUDT expression for zero-copy linalg conversion [spark]

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng closed pull request #58124: [SPARK-58875][SQL][ML][PYTHON][CONNECT] Add WrapUDT expression for zero-copy linalg conversion
URL: https://github.com/apache/spark/pull/58124


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-58875][SQL][ML][PYTHON][CONNECT] Add WrapUDT expression for zero-copy linalg conversion [spark]

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #58124:
URL: https://github.com/apache/spark/pull/58124#discussion_r3823305500


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/WrapUDT.scala:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.catalyst.expressions
+
+import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
+import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.DataTypeMismatch
+import org.apache.spark.sql.catalyst.expressions.Cast._
+import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode}
+import org.apache.spark.sql.catalyst.types.DataTypeUtils
+import org.apache.spark.sql.catalyst.util.TypeUtils.ordinalNumber
+import org.apache.spark.sql.types.{DataType, StringType, UserDefinedType}
+import org.apache.spark.unsafe.types.UTF8String
+
+/**
+ * Wrap a column with a UDT whose underlying SQL type matches the column data type.
+ *
+ * @see [[UnwrapUDT]] for converting a UDT column to its underlying SQL type.
+ */
+case class WrapUDT(child: Expression, udt: UserDefinedType[_])
+  extends UnaryExpression with NonSQLExpression {
+
+  def this(expressions: Seq[Expression]) = {
+    this(expressions.head, WrapUDT.parseUDT(expressions(1)))
+  }
+
+  override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+    child.genCode(ctx)
+  }
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+    if (DataTypeUtils.sameType(child.dataType, udt.sqlType)) {
+      TypeCheckResult.TypeCheckSuccess
+    } else {
+      DataTypeMismatch(
+        errorSubClass = "UNEXPECTED_INPUT_TYPE",
+        messageParameters = Map(
+          "paramIndex" -> ordinalNumber(0),
+          "requiredType" -> toSQLType(udt.sqlType),
+          "inputSql" -> toSQLExpr(child),
+          "inputType" -> toSQLType(child.dataType)))
+    }
+  }
+
+  override def dataType: DataType = udt
+
+  override def nullSafeEval(input: Any): Any = input
+
+  override def prettyName: String = "wrap_udt"
+
+  override protected def withNewChildInternal(newChild: Expression): WrapUDT = {
+    copy(child = newChild)
+  }
+}
+
+object WrapUDT {
+  private def parseUDT(expression: Expression): UserDefinedType[_] = {
+    expression match {
+      case Literal(json: UTF8String, StringType) =>

Review Comment:
   **Blocking:**
   
   `WrapUDT` should use `ExprUtils.evalTypeExpr(expression)` here, then explicitly require the returned `DataType` to be a `UserDefinedType`. Matching only a `Literal` rejects other foldable constant Columns that peer schema APIs accept, and null, non-string, non-foldable, or non-UDT targets escape as a `MatchError` or `ClassCastException` and are wrapped as generic `FAILED_FUNCTION_CALL`. Reusing the established helper preserves the constant-expression and type-validation boundary; please add negative and non-literal foldable coverage as well.



##########
python/pyspark/sql/functions/builtin.py:
##########
@@ -29187,6 +29195,106 @@ def unwrap_udt(col: "ColumnOrName") -> Column:
     return _invoke_function("unwrap_udt", _to_java_column(col))
 
 
+@_try_remote_functions
+def wrap_udt(col: "ColumnOrName", udt: "Union[UserDefinedType, Column]") -> Column:
+    """
+    Wrap a column as a user-defined type.
+
+    .. versionadded:: 4.4.0
+
+    Parameters
+    ----------
+    col : :class:`~pyspark.sql.Column` or column name
+        The column to wrap. The column data type must match the UDT's underlying SQL type.
+    udt : :class:`~pyspark.sql.types.UserDefinedType` or :class:`~pyspark.sql.Column`
+        The target user-defined type, or a column containing its JSON representation.

Review Comment:
   **Non-blocking:**
   
   Please state that the Column form must be a constant string containing the UDT JSON. The current wording reads as though a row-valued string Column is supported, but Catalyst has to determine `WrapUDT.dataType` while constructing the expression, before row evaluation.



##########
sql/api/src/main/scala/org/apache/spark/sql/functions.scala:
##########
@@ -17723,6 +17723,32 @@ object functions {
    */
   def unwrap_udt(column: Column): Column = Column.internalFn("unwrap_udt", column)
 
+  /**
+   * Wrap a column as a user-defined type.
+   * @param column
+   *   the column to wrap. The column data type must match the UDT's underlying SQL type.
+   * @param udt
+   *   the target user-defined type.
+   * @group udf_funcs
+   * @since 4.4.0
+   */
+  def wrap_udt(column: Column, udt: UserDefinedType[_]): Column = {
+    wrap_udt(column, lit(udt.json))
+  }
+
+  /**
+   * Wrap a column as a user-defined type.
+   * @param column
+   *   the column to wrap. The column data type must match the UDT's underlying SQL type.
+   * @param udt
+   *   the target user-defined type as a constant JSON string column.
+   * @group udf_funcs
+   * @since 4.4.0
+   */
+  def wrap_udt(column: Column, udt: Column): Column = {

Review Comment:
   That convention makes sense, and the Scaladoc makes the constant JSON role explicit. Thanks for the explanation.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-58875][SQL][ML][PYTHON][CONNECT] Add WrapUDT expression for zero-copy linalg conversion [spark]

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #58124:
URL: https://github.com/apache/spark/pull/58124#discussion_r3829617192


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/WrapUDT.scala:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.catalyst.expressions
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
+import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.DataTypeMismatch
+import org.apache.spark.sql.catalyst.expressions.Cast._
+import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode}
+import org.apache.spark.sql.catalyst.types.DataTypeUtils
+import org.apache.spark.sql.catalyst.util.TypeUtils.ordinalNumber
+import org.apache.spark.sql.types.{DataType, UserDefinedType}
+
+/**
+ * Wrap a column with a UDT whose underlying SQL type matches the column data type.
+ *
+ * @see [[UnwrapUDT]] for converting a UDT column to its underlying SQL type.
+ */
+case class WrapUDT(child: Expression, udt: UserDefinedType[_])
+  extends UnaryExpression with NonSQLExpression {
+
+  def this(expressions: Seq[Expression]) = {

Review Comment:
   Thanks, fixed in the latest push. `WrapUDT` now uses a fixed `(Expression, Expression)` auxiliary constructor, so `FunctionRegistry` enforces the two-argument contract instead of selecting a variadic `Seq[Expression]` constructor. I also added arity coverage through `FunctionRegistry.internal` for too few and too many args.
   
   I added an ML e2e nullable-vector case in `FunctionsSuite` as well, and verified it locally with:
   
   `build/sbt 'mllib/testOnly org.apache.spark.ml.FunctionsSuite -- -z "wrap and unwrap nullable vector UDT columns"'`
   
   Could you please take another look?



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-58875][SQL][ML][PYTHON][CONNECT] Add WrapUDT expression for zero-copy linalg conversion [spark]

Posted by "zhengruifeng (via GitHub)" <gi...@apache.org>.
zhengruifeng commented on code in PR #58124:
URL: https://github.com/apache/spark/pull/58124#discussion_r3840363212


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/WrapUDT.scala:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.catalyst.expressions
+
+import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
+import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.DataTypeMismatch
+import org.apache.spark.sql.catalyst.expressions.Cast._
+import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode}
+import org.apache.spark.sql.catalyst.types.DataTypeUtils
+import org.apache.spark.sql.catalyst.util.TypeUtils.ordinalNumber
+import org.apache.spark.sql.types.{DataType, StringType, UserDefinedType}
+import org.apache.spark.unsafe.types.UTF8String
+
+/**
+ * Wrap a column with a UDT whose underlying SQL type matches the column data type.
+ *
+ * @see [[UnwrapUDT]] for converting a UDT column to its underlying SQL type.
+ */
+case class WrapUDT(child: Expression, udt: UserDefinedType[_])
+  extends UnaryExpression with NonSQLExpression {
+
+  def this(expressions: Seq[Expression]) = {
+    this(expressions.head, WrapUDT.parseUDT(expressions(1)))
+  }
+
+  override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
+    child.genCode(ctx)
+  }
+
+  override def checkInputDataTypes(): TypeCheckResult = {
+    if (DataTypeUtils.sameType(child.dataType, udt.sqlType)) {
+      TypeCheckResult.TypeCheckSuccess
+    } else {
+      DataTypeMismatch(
+        errorSubClass = "UNEXPECTED_INPUT_TYPE",
+        messageParameters = Map(
+          "paramIndex" -> ordinalNumber(0),
+          "requiredType" -> toSQLType(udt.sqlType),
+          "inputSql" -> toSQLExpr(child),
+          "inputType" -> toSQLType(child.dataType)))
+    }
+  }
+
+  override def dataType: DataType = udt
+
+  override def nullSafeEval(input: Any): Any = input
+
+  override def prettyName: String = "wrap_udt"
+
+  override protected def withNewChildInternal(newChild: Expression): WrapUDT = {
+    copy(child = newChild)
+  }
+}
+
+object WrapUDT {
+  private def parseUDT(expression: Expression): UserDefinedType[_] = {
+    expression match {
+      case Literal(json: UTF8String, StringType) =>

Review Comment:
   Addressed in the latest revision: `WrapUDT` now uses `ExprUtils.evalTypeExpr(...)` and explicitly validates that the parsed type is a `UserDefinedType`. I also added coverage for a non-literal foldable UDT expression, non-UDT target, and non-foldable target.



##########
python/pyspark/sql/functions/builtin.py:
##########
@@ -29187,6 +29195,106 @@ def unwrap_udt(col: "ColumnOrName") -> Column:
     return _invoke_function("unwrap_udt", _to_java_column(col))
 
 
+@_try_remote_functions
+def wrap_udt(col: "ColumnOrName", udt: "Union[UserDefinedType, Column]") -> Column:
+    """
+    Wrap a column as a user-defined type.
+
+    .. versionadded:: 4.4.0
+
+    Parameters
+    ----------
+    col : :class:`~pyspark.sql.Column` or column name
+        The column to wrap. The column data type must match the UDT's underlying SQL type.
+    udt : :class:`~pyspark.sql.types.UserDefinedType` or :class:`~pyspark.sql.Column`
+        The target user-defined type, or a column containing its JSON representation.

Review Comment:
   Addressed in the latest revision: the Python `wrap_udt` docstring now states that the Column form is a constant string column containing the UDT JSON representation.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org