holdenk commented on code in PR #59730:
URL: https://github.com/apache/spark/pull/59730#discussion_r4585766504


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveTranspiledPythonUDFOptions.scala:
##########
@@ -117,3 +177,91 @@ object ResolveTranspiledPythonUDFOptions extends 
Rule[LogicalPlan] {
     }
   }
 }
+
+/**
+ * Drops any transpiled option that analysis left unresolved, so the call 
falls back to interpreted
+ * Python instead of failing the query.
+ *
+ * An option is a child of [[TranspiledPythonUDF]], so one that is still 
unresolved when analysis
+ * finishes reaches `CheckAnalysis`, which reports on an expression the user 
never wrote. For the
+ * common shape that is a type-check failure: an option of `cast(binary as 
bigint)` is measured as
+ * [[DATATYPE_MISMATCH.CAST_WITHOUT_SUGGESTION]], "cannot cast BINARY to 
BIGINT", naming a cast the
+ * transpiler invented. The honest answer is the one a category miss already 
gets: run the Python.
+ *
+ * Matching categories does not rule any of this out -- the transpiler picks a 
lowering per operator
+ * rather than per exact type, so `cast(binary as bigint)` matches "binary" 
and never resolves.
+ *
+ * Separate from [[ResolveTranspiledPythonUDFOptions]], and in a batch after 
the Resolution batch,
+ * because inside that batch `!resolved` does not mean unresolvable. Every 
option starts unresolved
+ * by design: its `_udf_param_N` references carry no type until 
ResolveTranspiledPythonUDFOptions
+ * reads one off each bound argument, and the node staying unresolved is what 
brings the analyzer
+ * back to coerce the body. Dropping on `!resolved` from inside the batch 
would throw away every
+ * option that reads a parameter and turn transpilation off without saying so. 
Once the batch is at
+ * a fixed point, nothing is going to resolve one.
+ *
+ * Dropping an option resolves the UDF, but the Resolution batch has already 
converged, so whatever
+ * was waiting on the UDF stays unresolved on its own: the enclosing 
`UnresolvedAlias` of
+ * `SELECT udf(a)`, or a parent held by name such as the `r` of `SELECT r + 1 
FROM (SELECT udf(a)
+ * AS r ...)`. `CheckAnalysis` would then report the fall-back itself as an 
error, so after
+ * dropping, this rule re-runs the resolution rules that finish those parents 
off -- and only on
+ * plans it changed, so queries without a dropped option never pay for this. 
The re-run is a single
+ * pass of each rule in Resolution-batch order; each walks bottom-up, which 
covers the parents of
+ * every dropped UDF at any nesting depth. `resolveAggregateFunctions` comes 
along because
+ * `resolveReferences` wraps ORDER BY/HAVING references over an Aggregate in 
`TempResolvedColumn`,
+ * and only `ResolveAggregateFunctions` marks the invalid ones `hasTried` for 
the later
+ * `RemoveTempResolvedColumn` batch to turn back into an unresolved attribute 
-- without it, an
+ * invalid ORDER BY in the same query as a dropped option would be unwrapped 
to a resolved
+ * attribute the Aggregate never provides, and reported as 
`MISSING_ATTRIBUTES` instead of the
+ * `UNRESOLVED_COLUMN` the same query gets without a UDF. The batch must 
therefore run before
+ * "Remove TempResolvedColumn", so that batch can still clean up behind this 
re-run. A shape
+ * needing a resolution rule not listed here fails `CheckAnalysis` as it did 
before the drop rule
+ * existed, rather than analyzing wrongly.
+ *
+ * @param resolveReferences the Analyzer's `ResolveReferences`, shared with 
the Resolution batch.
+ * @param resolveAliases the Analyzer's `ResolveAliases`.
+ * @param resolveAggregateFunctions the Analyzer's `ResolveAggregateFunctions`.
+ */
+class DropUnresolvedTranspiledPythonUDFOptions(
+    resolveReferences: Rule[LogicalPlan],
+    resolveAliases: Rule[LogicalPlan],
+    resolveAggregateFunctions: Rule[LogicalPlan]) extends Rule[LogicalPlan] {
+  def apply(plan: LogicalPlan): LogicalPlan = {
+    val dropped =
+      if (!plan.containsPattern(TRANSPILED_PYTHON_UDF)) {
+        plan
+      } else {
+        
plan.resolveOperatorsWithPruning(_.containsPattern(TRANSPILED_PYTHON_UDF)) {
+          case op if op.containsPattern(TRANSPILED_PYTHON_UDF) =>
+            
op.transformExpressionsUpWithPruning(_.containsPattern(TRANSPILED_PYTHON_UDF)) {
+              case t: TranspiledPythonUDF if t.arguments.forall(_.resolved) &&
+                  (t.optionInputCategories.nonEmpty || 
!t.transpiledOptions.forall(_.resolved)) =>
+                val typed = 
ResolveTranspiledPythonUDFOptions.pruneByCategoryAndTypeParameters(t)
+                val (kept, dropped) = 
typed.transpiledOptions.partition(_.resolved)
+                if (dropped.nonEmpty) {
+                  val first = Utils.redact(
+                    SQLConf.get.stringRedactionPattern, 
dropped.head.simpleString(maxFields = 100))
+                  // Only claim "falls back to interpreted Python" when every 
option was
+                  // dropped; with survivors, ConvertToCatalyst still picks 
the first kept one.
+                  if (kept.isEmpty) {
+                    logWarning(log"Dropping ${MDC(LogKeys.COUNT, 
dropped.length)} transpiled " +
+                      log"option(s) for Python UDF 
${MDC(LogKeys.FUNCTION_NAME, t.name)}; " +
+                      log"the call falls back to interpreted Python. First 
dropped: " +
+                      log"${MDC(LogKeys.EXPR, first)}")
+                  } else {
+                    logWarning(log"Dropping ${MDC(LogKeys.COUNT, 
dropped.length)} transpiled " +
+                      log"option(s) for Python UDF 
${MDC(LogKeys.FUNCTION_NAME, t.name)}; " +
+                      log"${MDC(LogKeys.COUNT, kept.length)} option(s) remain. 
First dropped: " +

Review Comment:
   Fixed in c40a0af7c62: the kept count now logs under `LogKeys.NUM_REMAINED`, 
so `count` keeps the dropped count and the kept count has its own field. 
Checked `MessageWithContext.+` (`putAll`) and the rendered message is 
unchanged. Added a suite test that runs full analysis with one dropped and one 
kept option plus a fully dropped call, and checks both warnings and the string 
redaction.
   
   ---
   <sub>🤖 Posted by an AI agent (Claude Code · skill `none`) on behalf of 
@holdenk.</sub>
   <!-- agent-attribution v1: agent=claude-code; skill=none; operator=holdenk 
-->



##########
.github/workflows/build_and_test.yml:
##########
@@ -627,6 +629,7 @@ jobs:
             pyspark-pandas-slow-connect
         exclude:
           - modules: ${{ fromJson(needs.precondition.outputs.required).pyspark 
!= 'true' && 'pyspark-sql, pyspark-resource, pyspark-testing, pyspark-core, 
pyspark-errors, pyspark-logger' }}
+          - modules: ${{ fromJson(needs.precondition.outputs.required).pyspark 
!= 'true' && 'pyspark-sql-slow' }}

Review Comment:
   Fixed in c40a0af7c62: the exclude now also drops `pyspark-sql-slow` when the 
precondition's `transpile` flag is not `'true'`. Confirmed the goal skips its 
differential class without `RUN_HYPOTHESIS`, and apache/spark's scheduled 
builds pass `transpile: true` (build_main.yml), so nothing loses coverage.
   
   ---
   <sub>🤖 Posted by an AI agent (Claude Code · skill `none`) on behalf of 
@holdenk.</sub>
   <!-- agent-attribution v1: agent=claude-code; skill=none; operator=holdenk 
-->



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