You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "SCHJonathan (via GitHub)" <gi...@apache.org> on 2025/12/03 06:56:51 UTC
[PR] [SPARK-54562] Block eager analysis / execution inside flow function from the server side [spark]
SCHJonathan opened a new pull request, #53303:
URL: https://github.com/apache/spark/pull/53303
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'common/utils/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
Eager analysis / execution inside the SDP flow function is not supported. Previously, we enforce such restriction by intercepting the `AnalyzePlan` / `ExecutePlan` RPC from the Python Spark Connect Client side, which is fragile. It's mostly because during that time, we don't have a good way for the SC server to know whether it is serving a requests that made inside a flow function.
Now, we have `PipelineAnalysisContext`. If the request comes from the flow function, SC client automatically add a `PipelineAnalysisContext` UserContext extension and SC server can based on its existence to know whether the request comes from flow function. Thereby, we're now able to move the eager analysis / execution validation from the client to the server.
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
Our previous way of performing such validation is very fragile and this PR serves as an code improvement.
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
No
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
New unit tests in PythonPipelineSuite
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
No
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-54562] Block eager analysis / execution inside flow function from the server side [spark]
Posted by "SCHJonathan (via GitHub)" <gi...@apache.org>.
SCHJonathan commented on PR #53303:
URL: https://github.com/apache/spark/pull/53303#issuecomment-3609910024
@sryza CI should be all green now, ready to be merged!
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-54562] Block eager analysis / execution inside flow function from the server side [spark]
Posted by "cookiedough77 (via GitHub)" <gi...@apache.org>.
cookiedough77 commented on code in PR #53303:
URL: https://github.com/apache/spark/pull/53303#discussion_r2586797382
##########
sql/connect/server/src/main/scala/org/apache/spark/sql/connect/utils/PipelineAnalysisContextUtils.scala:
##########
@@ -0,0 +1,84 @@
+/*
+ * 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.connect.utils
+
+import scala.collection.mutable
+import scala.jdk.CollectionConverters._
+import scala.reflect.ClassTag
+
+import com.google.protobuf.{Any => ProtoAny, Message}
+
+import org.apache.spark.connect.proto
+import org.apache.spark.connect.proto.{PipelineAnalysisContext, UserContext}
+
+/**
+ * Utilities for working with PipelineAnalysisContext in the user context.
+ */
+object PipelineAnalysisContextUtils {
+
+ /** Get a list of extensions from the user context. */
+ private def getExtensionList[T <: Message: ClassTag](
+ extensions: mutable.Buffer[ProtoAny]): Seq[T] = {
+ val cls = implicitly[ClassTag[T]].runtimeClass
+ .asInstanceOf[Class[_ <: Message]]
+ extensions.collect {
+ case any if any.is(cls) => any.unpack(cls).asInstanceOf[T]
+ }.toSeq
+ }
+
+ /** Get a list of PipelineAnalysisContext extensions from the user context. */
+ private def getPipelineAnalysisContextList(
+ userContext: UserContext): Seq[PipelineAnalysisContext] = {
+ val userContextExtensions = userContext.getExtensionsList.asScala
+ getExtensionList[PipelineAnalysisContext](userContextExtensions)
+ }
+
+ /** Return whether the execution / analysis is inside a pipeline flow function. */
+ def hasPipelineAnalysisContext(userContext: UserContext): Boolean = {
+ getPipelineAnalysisContextList(userContext).nonEmpty
+ }
+
+ /** Return whether the execution / analysis is inside a pipeline flow function. */
+ def isInsidePipelineFlowFunction(userContext: UserContext): Boolean = {
+ getPipelineAnalysisContextList(userContext).exists(_.hasFlowName)
+ }
+
+ /**
+ * Return whether the eager execution is triggered inside pipeline flow function but the eager
+ * execution type is not allowed.
+ */
+ def isUnsupportedEagerExecutionInsideFlowFunction(
+ userContext: UserContext,
+ plan: proto.Plan): Boolean = {
+ // if the eager execution is not triggered inside pipeline flow function,
+ // don't block it.
+ if (!isInsidePipelineFlowFunction(userContext)) {
+ return false
+ }
+
+ plan.getOpTypeCase match {
+ // Root plan (e.g., df.collect(), df.first()) are not allowed inside flow function
+ case proto.Plan.OpTypeCase.ROOT => true
+ case proto.Plan.OpTypeCase.COMMAND =>
+ // only spark.sql() command is allowed inside flow function.
+ val commandAllowList = Set(proto.Command.CommandTypeCase.SQL_COMMAND)
+ !commandAllowList.contains(plan.getCommand.getCommandTypeCase)
+ case other => false
Review Comment:
why should default value for case other be false?
##########
sql/connect/server/src/test/scala/org/apache/spark/sql/connect/pipelines/PythonPipelineSuite.scala:
##########
@@ -1105,6 +1105,70 @@ class PythonPipelineSuite
|""".stripMargin)
}
+ private val eagerExecutionPythonCommands = Seq(
+ "df.collect()",
+ "df.first()",
+ "df.head(0)",
+ "df.toPandas()",
+ "spark.readStream.format(\"rate\").load().writeStream" +
+ ".format(\"memory\").queryName(\"test_query_name\").start()")
+
+ gridTest("unsupported eager execution inside flow function is blocked")(
+ eagerExecutionPythonCommands) { unsupportedEagerExecutionCommand =>
+ val ex = intercept[RuntimeException] {
+ buildGraph(s"""
+ |@dp.materialized_view()
Review Comment:
duplicate code as the eagerAnalysisPythonCommands case.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-54562] Block eager analysis / execution inside flow function from the server side [spark]
Posted by "sryza (via GitHub)" <gi...@apache.org>.
sryza commented on PR #53303:
URL: https://github.com/apache/spark/pull/53303#issuecomment-3609049422
It looks like there are some test failures – are you able to investigate?
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-54562] Block eager analysis / execution inside flow function from the server side [spark]
Posted by "SCHJonathan (via GitHub)" <gi...@apache.org>.
SCHJonathan commented on code in PR #53303:
URL: https://github.com/apache/spark/pull/53303#discussion_r2586887426
##########
sql/connect/server/src/test/scala/org/apache/spark/sql/connect/pipelines/PythonPipelineSuite.scala:
##########
@@ -1105,6 +1105,70 @@ class PythonPipelineSuite
|""".stripMargin)
}
+ private val eagerExecutionPythonCommands = Seq(
+ "df.collect()",
+ "df.first()",
+ "df.head(0)",
+ "df.toPandas()",
+ "spark.readStream.format(\"rate\").load().writeStream" +
+ ".format(\"memory\").queryName(\"test_query_name\").start()")
+
+ gridTest("unsupported eager execution inside flow function is blocked")(
+ eagerExecutionPythonCommands) { unsupportedEagerExecutionCommand =>
+ val ex = intercept[RuntimeException] {
+ buildGraph(s"""
+ |@dp.materialized_view()
Review Comment:
Intentionally to keep them into separate test as one is eager exeuction and one is eager analysis.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-54562] Block eager analysis / execution inside flow function from the server side [spark]
Posted by "sryza (via GitHub)" <gi...@apache.org>.
sryza commented on PR #53303:
URL: https://github.com/apache/spark/pull/53303#issuecomment-3612525974
Thanks for this! Merged to master.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-54562] Block eager analysis / execution inside flow function from the server side [spark]
Posted by "sryza (via GitHub)" <gi...@apache.org>.
sryza closed pull request #53303: [SPARK-54562] Block eager analysis / execution inside flow function from the server side
URL: https://github.com/apache/spark/pull/53303
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org