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