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