You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@kyuubi.apache.org by GitBox <gi...@apache.org> on 2023/01/10 07:03:53 UTC

[GitHub] [kyuubi] yaooqinn commented on a diff in pull request #3699: [KYUUBI #3698][Subtask] Set table owner for v1 create table commands

yaooqinn commented on code in PR #3699:
URL: https://github.com/apache/kyuubi/pull/3699#discussion_r1065396638


##########
extensions/spark/kyuubi-spark-authz/src/main/scala/org/apache/kyuubi/plugin/spark/authz/serde/tableOwnerRewriters.scala:
##########
@@ -0,0 +1,156 @@
+/*
+ * 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.spark.authz.serde
+
+import java.util.ServiceLoader
+
+import scala.collection.JavaConverters._
+
+import org.apache.spark.sql.{Row, SparkSession}
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.catalyst.expressions.Attribute
+import org.apache.spark.sql.catalyst.plans.logical.{LeafNode, LogicalPlan}
+import org.apache.spark.sql.execution.SparkPlan
+import org.apache.spark.sql.execution.command.{DataWritingCommand, DataWritingCommandExec, ExecutedCommandExec, RunnableCommand}
+import org.apache.spark.sql.execution.metric.SQLMetric
+
+import org.apache.kyuubi.plugin.spark.authz.util.AuthZUtils.{getAuthzUgi, getFieldVal}
+import org.apache.kyuubi.plugin.spark.authz.util.WithInternalChild
+
+trait TableOwnerRewriter {
+
+  def key: String = getClass.getSimpleName
+
+  def rewritePlan(spark: SparkSession, sparkPlan: SparkPlan, table: Table): SparkPlan
+}
+
+object TableOwnerRewriter {
+  val tableOwnerRewriters: Map[String, TableOwnerRewriter] = {
+    ServiceLoader.load(classOf[TableOwnerRewriter])
+      .iterator()
+      .asScala
+      .map(e => (e.key, e))
+      .toMap
+  }
+}
+
+class RunnableCommandTableOwnerRewriter extends TableOwnerRewriter {
+
+  protected def shouldRewrite(sparkPlan: SparkPlan, tableExists: Boolean): Boolean = {
+    !tableExists
+  }
+
+  override def rewritePlan(spark: SparkSession, sparkPlan: SparkPlan, table: Table): SparkPlan = {
+    val catalog = spark.sessionState.catalog
+    val tableId = TableIdentifier(table.table, table.database)
+    if (shouldRewrite(sparkPlan, catalog.tableExists(tableId))) {
+      val exec = sparkPlan.asInstanceOf[ExecutedCommandExec]
+      val newCmd =
+        RewriteTableOwnerRunnableCommand(exec.cmd, tableId)
+      sparkPlan.asInstanceOf[ExecutedCommandExec].copy(cmd = newCmd)
+    } else {
+      sparkPlan
+    }
+  }
+}
+
+class CreateViewCommandTableOwnerRewriter extends RunnableCommandTableOwnerRewriter {
+
+  override protected def shouldRewrite(sparkPlan: SparkPlan, tableExists: Boolean): Boolean = {
+    val exec = sparkPlan.asInstanceOf[ExecutedCommandExec]
+    val isPermView = new ViewTypeTableTypeExtractor().apply(
+      getFieldVal(exec.cmd, "viewType"),
+      null) == TableType.PERMANENT_VIEW
+    isPermView && (getFieldVal[Boolean](exec.cmd, "replace") || !tableExists)
+  }
+}
+
+case class RewriteTableOwnerRunnableCommand(delegate: RunnableCommand, tableId: TableIdentifier)
+  extends RunnableCommand with LeafNode {
+
+  override lazy val metrics: Map[String, SQLMetric] = delegate.metrics
+
+  override val output: Seq[Attribute] = delegate.output
+
+  override def nodeName: String = delegate.nodeName
+
+  override def run(sparkSession: SparkSession): Seq[Row] = {
+    val ret = delegate.run(sparkSession)
+
+    val catalog = sparkSession.sessionState.catalog
+    val metadata = catalog.getTableMetadata(tableId)
+    val authzUser = getAuthzUgi(sparkSession.sparkContext).getShortUserName
+    if (metadata.owner != authzUser) {
+      catalog.alterTable(metadata.copy(owner = authzUser))
+    }
+    ret
+  }
+}
+
+class DataWritingCommandTableOwnerRewriter extends TableOwnerRewriter {
+
+  override def rewritePlan(spark: SparkSession, sparkPlan: SparkPlan, table: Table): SparkPlan = {
+    val catalog = spark.sessionState.catalog
+    val tableId = TableIdentifier(table.table, table.database)
+    if (!catalog.tableExists(tableId)) {
+      val exec = sparkPlan.asInstanceOf[DataWritingCommandExec]
+      val newCmd =
+        RewriteTableOwnerDataWritingCommand(exec.cmd, tableId)
+      sparkPlan.asInstanceOf[DataWritingCommandExec].copy(cmd = newCmd)
+    } else {
+      sparkPlan
+    }
+  }
+}
+
+case class RewriteTableOwnerDataWritingCommand(
+    delegate: DataWritingCommand,

Review Comment:
   Avoiding import DataWritingCommand



-- 
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: notifications-unsubscribe@kyuubi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@kyuubi.apache.org
For additional commands, e-mail: notifications-help@kyuubi.apache.org