ulysses-you commented on code in PR #3185:
URL: https://github.com/apache/incubator-kyuubi/pull/3185#discussion_r951025424


##########
extensions/spark/kyuubi-spark-listener/pom.xml:
##########
@@ -0,0 +1,103 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0";
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+    <parent>
+        <artifactId>kyuubi-parent</artifactId>
+        <groupId>org.apache.kyuubi</groupId>
+        <version>1.7.0-SNAPSHOT</version>
+        <relativePath>../../../pom.xml</relativePath>
+
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>kyuubi-spark-listener_2.12</artifactId>

Review Comment:
   `kyuubi-spark-lineage_2.12`



##########
extensions/spark/kyuubi-spark-listener/src/main/scala/org/apache/kyuubi/plugin/lineage/helper/SparkListenerHelper.scala:
##########
@@ -0,0 +1,44 @@
+/*
+ * 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.kyuubi.plugin.lineage.helper
+
+import org.apache.spark.SPARK_VERSION
+
+import org.apache.kyuubi.Logging
+import org.apache.kyuubi.engine.SemanticVersion
+
+object SparkListenerHelper extends Logging {

Review Comment:
   unused: `Logging`



##########
extensions/spark/kyuubi-spark-listener/src/main/scala/org/apache/kyuubi/plugin/lineage/helper/SparkSQLLineageParseHelper.scala:
##########
@@ -0,0 +1,311 @@
+/*
+ * 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.kyuubi.plugin.lineage.helper
+
+import scala.collection.immutable.ListMap
+import scala.util.{Failure, Success, Try}
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, 
CatalogTable, HiveTableRelation}
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, 
AttributeSet, NamedExpression}
+import org.apache.spark.sql.catalyst.plans.logical._
+import org.apache.spark.sql.connector.catalog.{CatalogPlugin, Identifier, 
TableCatalog}
+import org.apache.spark.sql.execution.datasources.LogicalRelation
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanRelation
+
+import org.apache.kyuubi.Logging
+import org.apache.kyuubi.plugin.lineage.events.Lineage
+import 
org.apache.kyuubi.plugin.lineage.helper.SparkListenerHelper.isSparkVersionAtMost
+
+trait LineageParser {
+  def sparkSession: SparkSession
+
+  type AttributeMap[A] = ListMap[Attribute, A]
+
+  def parse(plan: LogicalPlan): Lineage = {
+    val columnsLineage =
+      extractColumnsLineage(plan, ListMap[Attribute, 
AttributeSet]()).toList.collect {
+        case (k, attrs) =>
+          k.name -> attrs.map(_.name).toSet
+      }
+    val (inputTables, outputTables) = columnsLineage.foldLeft((List[String](), 
List[String]())) {
+      case ((inputs, outputs), (out, in)) =>
+        val x = (inputs ++ 
in.map(_.split('.').init.mkString("."))).filter(_.nonEmpty)
+        val y = outputs ++ 
List(out.split('.').init.mkString(".")).filter(_.nonEmpty)
+        (x, y)
+    }
+    Lineage(inputTables.distinct, outputTables.distinct, columnsLineage)
+  }
+
+  private def mergeColumnsLineage(
+      left: AttributeMap[AttributeSet],
+      right: AttributeMap[AttributeSet]): AttributeMap[AttributeSet] = {
+    left ++ right.map {
+      case (k, attrs) =>
+        k -> (attrs ++ left.getOrElse(k, AttributeSet.empty))
+    }
+  }
+
+  private def joinColumnsLineage(
+      parent: AttributeMap[AttributeSet],
+      child: AttributeMap[AttributeSet]): AttributeMap[AttributeSet] = {
+    if (parent.isEmpty) child
+    else {
+      val childMap = child.map { case (k, attrs) => (k.exprId, attrs) }
+      parent.map { case (k, attrs) =>
+        k -> AttributeSet(attrs.flatMap(attr =>
+          childMap.getOrElse(attr.exprId, AttributeSet.empty)))
+      }
+    }
+  }
+
+  private def getSelectColumnLineage(
+      named: Seq[NamedExpression]): AttributeMap[AttributeSet] = {
+    val exps = named.map {
+      case a: Alias => a.toAttribute -> a.references
+      case a: Attribute => a -> a.references
+    }
+    ListMap(exps: _*)
+  }
+
+  private def joinRelationColumnLineage(
+      parent: AttributeMap[AttributeSet],
+      relationAttrs: Seq[Attribute],
+      tableName: String = ""): AttributeMap[AttributeSet] = {
+    val relationAttrSet = AttributeSet(relationAttrs)
+    if (parent.nonEmpty) {
+      parent.map { case (k, attrs) =>
+        k -> AttributeSet(attrs.collect {
+          case attr if relationAttrSet.contains(attr) =>
+            attr.withName(Seq(tableName, 
attr.name).filter(_.nonEmpty).mkString("."))
+        })
+      }
+    } else {
+      ListMap(relationAttrs.map { attr =>
+        (
+          attr,
+          AttributeSet(attr.withName(Seq(tableName, 
attr.name).filter(_.nonEmpty).mkString("."))))
+      }: _*)
+    }
+  }
+
+  private def extractColumnsLineage(
+      plan: LogicalPlan,
+      parentColumnsLineage: AttributeMap[AttributeSet]): 
AttributeMap[AttributeSet] = {
+
+    def getPlanField[T](field: String): T = {
+      getFieldVal[T](plan, field)
+    }
+
+    def getCurrentPlanField[T](curPlan: LogicalPlan, field: String): T = {
+      getFieldVal[T](curPlan, field)
+    }
+
+    def getPlanMethod[T](name: String): T = {
+      getMethod[T](plan, name)
+    }
+
+    def getQuery: LogicalPlan = {
+      getPlanField[LogicalPlan]("query")
+    }

Review Comment:
   better to move these methods out since we are inside recursion



##########
extensions/spark/kyuubi-spark-listener/src/main/scala/org/apache/kyuubi/plugin/lineage/events/OperationLineageEvent.scala:
##########
@@ -0,0 +1,37 @@
+/*
+ * 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.kyuubi.plugin.lineage.events
+
+import org.apache.kyuubi.Utils
+import org.apache.kyuubi.events.KyuubiEvent
+
+case class Lineage(

Review Comment:
   can we add some comments to list some examples of the `Lineage`, it may be 
used by developers.



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