[
https://issues.apache.org/jira/browse/SPARK-62332?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Andreas Neumann updated SPARK-62332:
------------------------------------
Priority: Critical (was: Major)
> Transform the relation of a pipeline flow when the flow is analyzed
> -------------------------------------------------------------------
>
> Key: SPARK-62332
> URL: https://issues.apache.org/jira/browse/SPARK-62332
> Project: Spark
> Issue Type: Bug
> Components: Declarative Pipelines
> Affects Versions: 5.0.0
> Reporter: Andreas Neumann
> Priority: Critical
> Fix For: 4.4.0
>
>
> h3. Problem
> When {{DefineFlow}} registers a pipeline flow over Spark Connect,
> {{PipelinesHandler}} transforms the flow's relation with
> {{SparkConnectPlanner.transformRelation}} right away. Several transformations
> analyze their input while they are transformed, because they need its schema:
> {{withColumn}}, {{withColumnRenamed}}, {{na.fill}} / {{na.drop}} /
> {{na.replace}}, {{to}}, {{withWatermark}}, and others. Such a transformation
> resolves a read of another dataset of the pipeline against the catalog, not
> against the pipeline.
> Flow analysis ({{FlowAnalysis}}) records a flow's dependencies by resolving
> the {{UnresolvedRelation}}s in the flow's plan. Once a read was resolved
> during the transformation, flow analysis no longer sees it. This causes
> several problems:
> * *Lost dependency on reruns.* On the first run of a pipeline, the upstream
> dataset does not exist in the catalog yet, eager analysis of {{withColumn}}
> fails, and it falls back to an unresolved plan, so the dependency is
> recorded. On every later run, the upstream table exists, the read is resolved
> against it, and no dependency is recorded. The flow is then scheduled
> concurrently with its upstream and can read it while it is being refreshed,
> for example writing no rows. The run still completes successfully, and a dry
> run does not detect it.
> * *Stale schema.* Even when the dependency is recovered, the transformation
> was analyzed against the schema of the table from the previous run. A column
> added to the upstream dataset reaches the downstream flow only on the
> following run.
> * *Flows that cannot be defined.* Transformations without a fallback to an
> unresolved plan, such as {{na.fill}}, {{to}}, and {{withWatermark}}, fail
> when the flow is defined if the upstream dataset does not exist yet, for
> example on the first run, with {{TABLE_OR_VIEW_NOT_FOUND}}.
> * *Stale reads of external tables.* A read of an external table through such
> a transformation captures the table as of when the flow was defined.
> Example (reported on the dev list:
> https://lists.apache.org/thread/or2hs36558lnkq971mdy5nj0vltkzto2):
> {code:python}
> @dp.materialized_view
> def a():
> return spark.sql("SELECT * FROM VALUES (1, 'north'), (2, 'south') AS
> t(id, name)")
> @dp.materialized_view
> def b():
> return spark.read.table("a").withColumn("m", F.upper(F.col("name")))
> {code}
> The first run is correct. On the second run, {{b}} starts at the same time as
> {{a}}, reads {{a}} while it is refreshed, and writes no rows. With {{select}}
> instead of {{withColumn}}, the dependency is kept, because {{select}} does
> not analyze its input when it is transformed.
> h3. Existing fixes for individual cases
> * SPARK-60494 (fixed in 4.3.0 and 4.2.2) defers {{spark.sql}} with DataFrame
> arguments in a flow to flow analysis, by binding the references as CTE
> relations instead of executing the SQL. Its description lists the same lost
> dependency as one of the problems it fixes, for that one transformation.
> * SPARK-60555 (open, PR https://github.com/apache/spark/pull/59796) recovers
> the dependency in {{FlowAnalysis}}: it recovers the {{UnresolvedRelation}}
> that an already resolved catalog table read was resolved from, and reads it
> through the pipeline. It fixes the lost dependency and the stale reads for
> the relation types it recognizes, but not the stale schema, since the
> transformations above the read were already analyzed against the old schema,
> and not the flows that cannot be defined. Its correctness also depends on
> recognizing every relation type a catalog table read can resolve to.
> Both fix one symptom, or one transformation, of the same cause: the flow's
> relation is transformed, and partially analyzed, before flow analysis.
> h3. Proposed solution: transform the relation of a flow when the flow is
> analyzed
> Transform a flow's relation during flow analysis instead of when the flow is
> defined, and resolve the reads of datasets while the relation is transformed:
> * {{PipelinesHandler.defineFlow}} keeps the flow's relation and registers a
> flow function that transforms it each time the flow is analyzed.
> * {{FlowAnalysis}} passes the planner a function that resolves the reads of
> datasets in a plan, as flow analysis does today.
> * While it transforms a pipeline flow's relation, the planner applies that
> function to each transformed relation before the relation is used to
> transform its parent, and does not use the session's plan cache for these
> plans. A transformation that analyzes its input then analyzes it against the
> datasets of the pipeline, and flow analysis records them as inputs of the
> flow.
> This is a more general fix: it does not depend on which transformation
> analyzes its input or on which relation type a read resolves to, because a
> read is never resolved against the catalog before flow analysis. It fixes all
> four problems above, for {{withColumn}} as well as {{na.fill}}, {{to}},
> {{withWatermark}}, and transformations added in the future. It would subsume
> SPARK-60555, and could replace the special handling of SPARK-60494.
> Trade-offs:
> * Analysis errors of a flow's relation are reported when the flow is analyzed
> instead of when it is defined, as SPARK-60494 already does for {{spark.sql}}
> with DataFrame arguments.
> * Transformations that execute a query when they are transformed (a pivot
> without values, {{crosstab}}, {{approxQuantile}}, {{showString}},
> {{htmlString}}) would run against placeholder inputs without data, so they
> must fail the flow, e.g. with
> {{ATTEMPT_ANALYSIS_IN_PIPELINE_QUERY_FUNCTION}}. Today they run against the
> catalog when the flow is defined.
> * A flow's relation is transformed on each analysis of the flow, including
> retries and the re-analysis before execution.
> A prototype of this exists. It passes tests that cover each transformation
> listed above across two runs, a column added to the upstream dataset, an
> external table, the real server path through {{StartRun}}, and the new
> failure of a pivot without values, as well as the existing {{sql/pipelines}},
> Spark Connect pipelines, planner, and PySpark pipelines tests.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]