spark-test commented on code in PR #60224: URL: https://github.com/apache/spark/pull/60224#discussion_r4510452568
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ml/VectorToArray.scala: ########## @@ -0,0 +1,86 @@ +/* + * 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.ml + +import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.DataTypeMismatch +import org.apache.spark.sql.catalyst.expressions.{Expression, ExprUtils, UnaryExpression} +import org.apache.spark.sql.catalyst.expressions.Cast._ +import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode, FalseLiteral} +import org.apache.spark.sql.catalyst.expressions.codegen.Block._ +import org.apache.spark.sql.catalyst.types.DataTypeUtils +import org.apache.spark.sql.types._ + +/** + * Converts an MLlib vector to an array of doubles or floats. This expression is dedicated only + * for Spark ML and accepts either a vector's SQL struct representation or a UDT with the same + * underlying SQL type. Null inputs are rejected, matching the vector_to_array API. + */ +case class VectorToArray(child: Expression, elementType: DataType) extends UnaryExpression { + require(elementType == DoubleType || elementType == FloatType, + "VectorToArray requires a double or float element type.") + + def this(child: Expression, elementType: Expression) = { + this(child, ExprUtils.evalTypeExpr(elementType)) + } + + override def prettyName: String = "ml_vector_to_array" + + override def dataType: DataType = ArrayType(elementType, containsNull = false) + + override def nullable: Boolean = false + + override def checkInputDataTypes(): TypeCheckResult = { + val inputType = child.dataType match { + case udt: UserDefinedType[_] => udt.sqlType + case other => other + } + if (DataTypeUtils.sameType(inputType, VectorScaleShift.vectorSqlType)) { Review Comment: **Blocking (P1):** `sameType` ignores `StructField.nullable` and `ArrayType.containsNull`, so this check admits any vector-shaped struct, including one whose `values` array can hold nulls. Before this PR such structs were rejected, and vector UDT values went through `VectorUDT.deserialize` and `SparseVector` validation. `MLExpressionUtils.vectorToArray` now reads the admitted struct positionally and checks only for a null row and an unknown type tag. For example, take `vector_to_array(expr("named_struct('type', 1Y, 'size', CAST(NULL AS INT), 'indices', CAST(NULL AS ARRAY<INT>), 'values', array(x, y))"))`, where `y` is null on some row: - With `float64`, the input `values` array is returned unchanged, so the row carries a null element in an `array<double>` declared `containsNull = false`. Generated projections trust that declaration, so the same element shows up as null or as 0.0 depending on how the array is stored. - With `float32`, the null is read through `getDouble` and silently becomes 0.0. A sparse row with null indices has its values placed at positions 0..n-1. - A null `values` field, a null size, or out-of-range or mismatched indices throw raw JVM exceptions, such as `NullPointerException`, `ArrayIndexOutOfBoundsException`, or `NegativeArraySizeException`, instead of a defined error. The same applies to vector UDT values built with `wrap_udt` from such structs, on every evaluation path, and through `ml_vector_to_array`. Well-formed vector UDT columns and their `unwrap_udt` output are unaffected. See **Shared repair plan 1** in the review body. ########## sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/ml/MLExpressionUtils.java: ########## @@ -77,6 +77,35 @@ public static double dotProduct( } } + public static ArrayData vectorToArray(InternalRow vector, boolean toFloat) { + if (vector == null) { + throw new IllegalArgumentException( Review Comment: **Non-blocking (P2):** A null vector reaches this bare `IllegalArgumentException`. Before this PR, `ScalaUDF` reported the same rejection as a `SparkException` with condition `FAILED_EXECUTE_UDF`. The error README also asks new code not to add uncategorized errors. The failure now carries no error condition or SQLSTATE, so callers that match on `getCondition` or `getSqlState`, or on PySpark and Connect error classes, no longer match. Its shape also depends on where the plan runs: - Over local data, `ConvertToLocalRelation` evaluates on the driver and the raw exception surfaces. The rewritten `FunctionsSuite` assertion exercises only this path. - In tasks, `DAGScheduler` passes only categorized errors through unwrapped, so the caller gets a generic "Job aborted due to stage failure" `SparkException`. The message also still says the input must be an ML or MLlib `Vector`, although structs and other UDTs are now accepted. See **Shared repair plan 1** in the review body. -- 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]
