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

   ### What changes were proposed in this pull request?
   `V2ExpressionUtils.resolveScalarFunction` looks up a `ScalarFunction`'s 
magic `invoke` method and its `produceResult` with `getDeclaredMethod` on the 
function's own class, so it misses a method the class inherits from a 
superclass. This PR looks the method up on the class and then along its 
superclass chain:
   - A method declared in a superclass counts only if it is public. Spark calls 
these methods from generated code or through 
`MethodUtils.getMatchingAccessibleMethod`, which reach only public methods. 
Without this rule, a function that inherits a non-public `invoke` and 
implements `produceResult` itself would stop working.
   - Interfaces are not searched, so the default `produceResult` of 
`ScalarFunction` still does not count as an implementation.
   - A method declared in the function's own class is found as before.
   
   The `ScalarFunction` javadoc, which says the magic method is looked up among 
the methods declared in the UDF class, is updated accordingly.
   
   ### Why are the changes needed?
   A function that implements `produceResult` or `invoke` in a base class is 
reported as not implementing it:
   
       abstract class StrLenBase extends ScalarFunction[Int] {
         override def produceResult(input: InternalRow): Int = 
input.getUTF8String(0).numChars()
       }
       class InheritedStrLen extends StrLenBase { ... }
   
   A SQL call of such a function fails analysis with 
`SCALAR_FUNCTION_NOT_FULLY_IMPLEMENTED`. The same lookup is used on the write 
path, which clusters rows by a partition transform, and by storage-partitioned 
joins, which then cannot evaluate such a partition transform and shuffle both 
sides.
   
   ### Does this PR introduce _any_ user-facing change?
   Yes. A V2 `ScalarFunction` that inherits a public `invoke` or 
`produceResult` from a superclass now resolves and runs instead of failing with 
`SCALAR_FUNCTION_NOT_FULLY_IMPLEMENTED`. In a storage-partitioned join, such a 
partition transform is now evaluated, so the join can reduce the other side 
onto it or shuffle only one side. A function that inherits a public magic 
method and overrides `produceResult` itself now uses the magic method, which 
takes precedence as documented.
   
   ### How was this patch tested?
   New tests in `DataSourceV2FunctionSuite`:
   - A Scala function that inherits `produceResult`, as in the JIRA.
   - Java functions that inherit `produceResult`, the magic method and the 
static magic method.
   - A Java function whose superclass declares a `protected` magic method and 
which implements `produceResult` itself; it must still use `produceResult`. 
This one passes without the fix too and guards the public-only rule.
   
   The first two fail without the fix with 
`SCALAR_FUNCTION_NOT_FULLY_IMPLEMENTED`.
   
   Updated the SPARK-60522 tests in `KeyGroupedPartitioningSuite`. They used 
`InheritedBucketFunction`, a `bucket` that inherits `produceResult`, as a 
transform Spark cannot evaluate. With this fix Spark evaluates it like 
`BucketFunction`, so the first test now expects that. As the transform Spark 
cannot evaluate, both tests now use a new Java `JavaBucketNoImpl`, which 
implements neither method; a Scala class cannot serve here, since it gets a 
forwarder to the default `produceResult`. The `TransformExpression` scaladoc 
that gave an inherited `produceResult` as an example of a resolution failure is 
updated too.
   
   Ran `DataSourceV2FunctionSuite`, `KeyGroupedPartitioningSuite` and 
`WriteDistributionAndOrderingSuite` locally.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   Co-authored-by: Claude Opus 5.5
   
   cc @metanil


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