spark-test commented on code in PR #60300:
URL: https://github.com/apache/spark/pull/60300#discussion_r4571899286


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ml/VectorDotProduct.scala:
##########
@@ -113,4 +113,14 @@ object VectorDotProduct {
     StructField("size", IntegerType, nullable = true),
     StructField("indices", ArrayType(IntegerType, containsNull = false), 
nullable = true),
     StructField("values", ArrayType(DoubleType, containsNull = false), 
nullable = true)))
+
+  private object VectorInputType extends AbstractDataType {

Review Comment:
   **Non-blocking (P2):** This check closes the nullability gap only for 
`ml_vector_dot_product`. `VectorScaleShift.inputTypes` still declares its 
vector operand with the `vectorSqlType` instance, whose `acceptsType` is 
`sameType`, and `sameType` ignores nullability. `MLExpressionUtils.scaleShift` 
reads `type`, `indices` and `values` as primitives, so for a struct with 
nullable elements it still reads the nulls as 0. Those nulls can also get past 
the check added here. `ml_vector_scale_shift` declares its output with the 
non-null vector schema, but it returns the input row unchanged when scale and 
shift are both null, and when shift is null it passes a sparse input's 
`indices` array through. Reading the code, take `s.values = [2.0, NULL]` and `w 
= [1.0, 1.0]`: `ml_vector_dot_product(ml_vector_scale_shift(s, NULL, NULL), w)` 
passes the new check and returns 2.0 instead of failing analysis the way 
`ml_vector_dot_product(s, w)` now does. This affects callers that build vector 
structs the
 mselves and call these internal functions by name through Spark Connect or 
PySpark's `InternalFunction`. The `org.apache.spark.ml.functions` helpers pass 
`unwrap_udt` operands with the exact schema and are not affected.
   
   **Recommended change:** Move the nullability-compatible vector input type 
out of the private VectorDotProduct companion into a definition in the same 
package that both ML vector expressions can use. Declare the vector operand of 
VectorScaleShift with it, so ml_vector_scale_shift rejects at analysis the same 
nullable vector structs that ml_vector_dot_product now rejects. Leave the scale 
and shift operand types and the evaluation code unchanged. Add catalyst ML 
vector expression tests for three cases: the new rejection, stricter 
nullability still being accepted, and vector structs with nullable elements 
passed through ml_vector_scale_shift into ml_vector_dot_product.
   
   **Why this works:** VectorScaleShift's analysis-time input check then 
applies the same compatible-nullability rule as VectorDotProduct. A vector 
operand whose declared type allows a null type field or null indices or values 
elements fails before MLExpressionUtils.scaleShift can read those nulls as 0. 
The accepted input is then at least as strict as the declared output type. So 
the unchanged row returned when scale and shift are null, and the input indices 
passed through in the sparse case without shift, honor the output's non-null 
declarations, and the dot product's check can rely on that output again.
   
   **Scope:** Share the vector operand check between ml_vector_dot_product and 
ml_vector_scale_shift, apply it to the scale-shift vector operand, and add 
catalyst tests for the new rejection and the composed path.
   
   **Compatibility:** For every vector operand that already matches the vector 
schema's nullability, including unwrap_udt of VectorUDT columns and the mllib 
helpers' constant scale and shift, ml_vector_scale_shift and 
ml_vector_dot_product analyze and evaluate as before.
   
   **Risks:** ml_vector_scale_shift will also reject vector structs whose field 
names differ from the schema only in case, which it accepted under the default 
case-insensitive analysis. The function has not shipped in a release, every 
producer in the repository emits the exact lower-case names, and 
ml_vector_dot_product and ml_vector_posexplode already match names exactly. 
Reverting the operand type restores the old acceptance. ml_vector_scale_shift's 
vector-operand errors will report the shared type's required-type text, which 
upper-cases field names, in place of its current schema text. Until the 
required-type text correction for the shared type lands, that text also drops 
the NOT NULL on the type field that the current text shows.
   
   **Constraints:** Keep VectorScaleShift.vectorSqlType as the declared output 
type and as the type a NULL vector argument is coerced to. The existing tests 
and the SparseVectorToArray expression on current master refer to it. Keep the 
scale and shift operand types, VectorPosExplode's exact schema check, and 
MLExpressionUtils unchanged.
   
   **Exclusions:**
   - Merging the identical vector schema definitions that VectorDotProduct, 
VectorScaleShift, and VectorPosExplode each declare is out of scope; this fix 
does not need it.
   - The text the shared check reports when it rejects an operand belongs to 
the fix for the misleading required type in the error message.
   
   **Success:** ml_vector_scale_shift fails analysis with 
DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE for its first parameter when the vector 
operand's declared type allows a null type field or null indices or values 
elements. ml_vector_scale_shift still accepts the exact vector schema, structs 
stricter than it, and a NULL vector argument, with unchanged results. 
ml_vector_dot_product over ml_vector_scale_shift of a vector whose declared 
type allows nulls fails analysis instead of reading those nulls as 0. A 
rejected vector operand of ml_vector_scale_shift reports the same required type 
as a rejected operand of ml_vector_dot_product.
   
   **Verification:**
   - **Behavior:** Show that ml_vector_scale_shift rejects at analysis a vector 
operand with nullable indices elements, one with nullable values elements, and 
one with a nullable type field. Also show that it accepts and evaluates, in 
both interpreted and generated code, a vector operand whose fields are all 
non-nullable.
   - **Regression:** Reproduce both composed routes: a dense vector struct 
whose values elements are declared nullable, passed through 
ml_vector_scale_shift with null scale and shift, and a sparse vector struct 
whose indices elements are declared nullable, passed through 
ml_vector_scale_shift with a scale and a null shift, each into 
ml_vector_dot_product. Both must now fail analysis instead of returning a dot 
product that reads a null element as 0.
   - **Compatibility:** Show that ml_vector_scale_shift still accepts at 
analysis a vector operand of exactly the vector schema, as unwrap_udt produces, 
and an untyped NULL vector argument, and that the NULL argument yields NULL.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ml/VectorDotProduct.scala:
##########
@@ -113,4 +113,14 @@ object VectorDotProduct {
     StructField("size", IntegerType, nullable = true),
     StructField("indices", ArrayType(IntegerType, containsNull = false), 
nullable = true),
     StructField("values", ArrayType(DoubleType, containsNull = false), 
nullable = true)))
+
+  private object VectorInputType extends AbstractDataType {
+    override private[sql] def defaultConcreteType: DataType = vectorSqlType
+
+    override private[sql] def acceptsType(other: DataType): Boolean = {
+      DataType.equalsIgnoreCompatibleNullability(other, vectorSqlType)
+    }
+
+    override private[spark] def simpleString: String = 
vectorSqlType.simpleString

Review Comment:
   **Nit (P3):** The required type here comes from 
`vectorSqlType.simpleString`. Because `VectorInputType` is not a `DataType`, 
`toSQLType` prints it upper-cased as 
`STRUCT<TYPE:TINYINT,SIZE:INT,INDICES:ARRAY<INT>,VALUES:ARRAY<DOUBLE>>`. 
`simpleString` drops field nullability, so the error no longer shows the `NOT 
NULL` on `type`, even though this check now enforces it. The base branch's 
message, rendered from `vectorSqlType.sql`, showed it, and 
`ml_vector_posexplode`'s message still does. Reading the code, the 
nullable-`type` case in the new test gets an error saying the parameter 
requires 
`STRUCT<TYPE:TINYINT,SIZE:INT,INDICES:ARRAY<INT>,VALUES:ARRAY<DOUBLE>>` but has 
the type `STRUCT<type: TINYINT, size: INT, indices: ARRAY<INT>, values: 
ARRAY<DOUBLE>>`. The two differ only in case and spacing, so the caller cannot 
tell why the operand was rejected. Rendering from `vectorSqlType.sql` brings 
the `NOT NULL` back:
   
   ```suggestion
       override private[spark] def simpleString: String = vectorSqlType.sql
   ```
   
   This does not cover `indices`/`values` element nullability. No Spark type 
text renders `containsNull`, and `ml_vector_posexplode` and the 
`ml_vector_scale_shift` scale/shift checks have the same limitation.
   
   **Verification:**
   - **Regression:** For an operand whose type field is declared nullable, 
assert the full type-check result: the UNEXPECTED_INPUT_TYPE subclass, the 
operand position, and a required type that shows NOT NULL on the type field.
   - **Inspection:** Confirm that the change touches only the vector input 
type's reported text, leaving its acceptance rule and its default concrete type 
unchanged.



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