From 71ae2e9637d233adbc3568d9a83f0b29fd2ed49f Mon Sep 17 00:00:00 2001 From: Angerszhuuuu Date: Thu, 18 Jan 2024 13:47:38 +0800 Subject: [PATCH] [KYUUBI #5937] PVM cause cache table not work MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # :mag: Description ## Issue References ๐Ÿ”— This pull request fixes #5937 ## Describe Your Solution ๐Ÿ”ง If we cache a table with persist view in the query, since cache table use analyzed plan, so in kyuubi authz we will use PVM to wrap the view, but cache table use canonicalized plan, so we need to implement the `doCanonicalize()` method to ignore the impact of PVM, or it will cache cached table can't be matched. ## Types of changes :bookmark: - [x] Bugfix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) ## Test Plan ๐Ÿงช #### Behavior Without This Pull Request :coffin: #### Behavior With This Pull Request :tada: #### Related Unit Tests --- # Checklist ๐Ÿ“ - [ ] This patch was not authored or co-authored using [Generative Tooling](https://www.apache.org/legal/generative-tooling.html) **Be nice. Be informative.** Closes #5982 from AngersZhuuuu/KYUUBI-5937. Closes #5937 e28275f32 [Angerszhuuuu] Update PermanentViewMarker.scala c504103d2 [Angerszhuuuu] Update PermanentViewMarker.scala 19102ff53 [Angerszhuuuu] [KYUUBI-5937][Bug] PVM cause cache table not work Authored-by: Angerszhuuuu Signed-off-by: Cheng Pan --- .../permanentview/PermanentViewMarker.scala | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/extensions/spark/kyuubi-spark-authz/src/main/scala/org/apache/kyuubi/plugin/spark/authz/rule/permanentview/PermanentViewMarker.scala b/extensions/spark/kyuubi-spark-authz/src/main/scala/org/apache/kyuubi/plugin/spark/authz/rule/permanentview/PermanentViewMarker.scala index fc52adc0458..cb233b4651f 100644 --- a/extensions/spark/kyuubi-spark-authz/src/main/scala/org/apache/kyuubi/plugin/spark/authz/rule/permanentview/PermanentViewMarker.scala +++ b/extensions/spark/kyuubi-spark-authz/src/main/scala/org/apache/kyuubi/plugin/spark/authz/rule/permanentview/PermanentViewMarker.scala @@ -21,11 +21,14 @@ import org.apache.spark.sql.catalyst.analysis.MultiInstanceRelation import org.apache.spark.sql.catalyst.catalog.CatalogTable import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, Cast} import org.apache.spark.sql.catalyst.plans.QueryPlan -import org.apache.spark.sql.catalyst.plans.logical.{LeafNode, LogicalPlan, Project, Statistics} +import org.apache.spark.sql.catalyst.plans.logical.{LeafNode, LogicalPlan, Project, Statistics, View} +import org.apache.spark.sql.catalyst.trees.TreeNodeTag case class PermanentViewMarker(child: LogicalPlan, catalogTable: CatalogTable) extends LeafNode with MultiInstanceRelation { + private val PVM_NEW_INSTANCE_TAG = TreeNodeTag[Unit]("__PVM_NEW_INSTANCE_TAG") + override def output: Seq[Attribute] = child.output override def argString(maxFields: Int): String = "" @@ -38,6 +41,18 @@ case class PermanentViewMarker(child: LogicalPlan, catalogTable: CatalogTable) val projectList = child.output.map { case attr => Alias(Cast(attr, attr.dataType), attr.name)(explicitMetadata = Some(attr.metadata)) } - this.copy(child = Project(projectList, child), catalogTable = catalogTable) + val newProj = Project(projectList, child) + newProj.setTagValue(PVM_NEW_INSTANCE_TAG, ()) + + this.copy(child = newProj, catalogTable = catalogTable) + } + + override def doCanonicalize(): LogicalPlan = { + child match { + case p @ Project(_, view: View) if p.getTagValue(PVM_NEW_INSTANCE_TAG).contains(true) => + view.canonicalized + case _ => + child.canonicalized + } } }