cloud-fan commented on code in PR #51003: URL: https://github.com/apache/spark/pull/51003#discussion_r2113935804
########## sql/pipelines/src/main/scala/org/apache/spark/sql/pipelines/graph/DataflowGraph.scala: ########## @@ -0,0 +1,251 @@ +/* + * 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.pipelines.graph + +import scala.util.Try + +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.TableIdentifier +import org.apache.spark.sql.pipelines.graph.DataflowGraph.mapUnique +import org.apache.spark.sql.pipelines.util.SchemaMergingUtils +import org.apache.spark.sql.types.StructType + +/** + * DataflowGraph represents the core graph structure for Spark declarative pipelines. + * It manages the relationships between logical flows, tables, and views, providing + * operations for graph traversal, validation, and transformation. + */ +class DataflowGraph(val flows: Seq[Flow], val tables: Seq[Table], val views: Seq[View]) + extends GraphOperations + with GraphValidations { + + /** Returns a [[Output]] given its identifier */ + lazy val output: Map[TableIdentifier, Output] = mapUnique(tables, "output")(_.identifier) + + /** + * Returns a [[TableInput]], if one is available, that can be read from by downstream flows. + */ + def tableInput(identifier: TableIdentifier): Option[TableInput] = table.get(identifier) + + /** + * Returns [[Flow]]s in this graph that need to get planned and potentially executed when + * executing the graph. Flows that write to logical views are excluded. + */ + lazy val materializedFlows: Seq[ResolvedFlow] = { + resolvedFlows.filter( + f => output.contains(f.destinationIdentifier) + ) + } + + /** Returns the identifiers of [[materializedFlows]]. */ + val materializedFlowIdentifiers: Set[TableIdentifier] = materializedFlows.map(_.identifier).toSet + + /** Returns a [[Table]] given its identifier */ + lazy val table: Map[TableIdentifier, Table] = + mapUnique(tables, "table")(_.identifier) + + /** Returns a [[Flow]] given its identifier */ + lazy val flow: Map[TableIdentifier, Flow] = { + // Better error message than using mapUnique. + val flowsByIdentifier = flows.groupBy(_.identifier) + flowsByIdentifier + .find(_._2.size > 1) + .foreach { + case (flowIdentifier, flows) => + // We don't expect this to ever actually be hit, graph registration should validate for + // unique flow names. + throw new AnalysisException( + errorClass = "PIPELINE_DUPLICATE_IDENTIFIERS.FLOW", + messageParameters = Map( + "flowName" -> flowIdentifier.unquotedString, + "datasetNames" -> flows.map(_.destinationIdentifier).mkString(",") + ) + ) + } + // Flows with non-default names shouldn't conflict with table names + flows + .filterNot(f => f.identifier == f.destinationIdentifier) + .filter(f => table.contains(f.identifier)) + .foreach { f => + throw new AnalysisException( + "FLOW_NAME_CONFLICTS_WITH_TABLE", + Map( + "flowName" -> f.identifier.toString(), + "target" -> f.destinationIdentifier.toString(), + "tableName" -> f.identifier.toString() + ) + ) + } + flowsByIdentifier.view.mapValues(_.head).toMap + } + + /** Returns a [[View]] given its identifier */ + lazy val view: Map[TableIdentifier, View] = mapUnique(views, "view")(_.identifier) + + /** Returns the [[PersistedView]]s of the graph */ + lazy val persistedViews: Seq[PersistedView] = views.collect { + case v: PersistedView => v + } + + /** Returns all the [[Input]]s in the current DataflowGraph. */ + lazy val inputIdentifiers: Set[TableIdentifier] = { + (flows ++ tables).map(_.identifier).toSet + } + + /** Returns the [[Flow]]s that write to a given destination. */ + lazy val flowsTo: Map[TableIdentifier, Seq[Flow]] = flows.groupBy(_.destinationIdentifier) + + lazy val resolvedFlows: Seq[ResolvedFlow] = { + flows.collect { case f: ResolvedFlow => f } + } + + lazy val resolvedFlow: Map[TableIdentifier, ResolvedFlow] = { + resolvedFlows.map { f => + f.identifier -> f + }.toMap + } + + lazy val resolutionFailedFlows: Seq[ResolutionFailedFlow] = { + flows.collect { case f: ResolutionFailedFlow => f } + } + + lazy val resolutionFailedFlow: Map[TableIdentifier, ResolutionFailedFlow] = { + resolutionFailedFlows.map { f => + f.identifier -> f + }.toMap + } + + /** Returns a copy of this [[DataflowGraph]] with optionally replaced components. */ + def copy( Review Comment: turn `DataflowGraph` into a case class to get `def copy` automatically. -- 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-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org