pengzhiwei2018 commented on a change in pull request #2645:
URL: https://github.com/apache/hudi/pull/2645#discussion_r614665177



##########
File path: 
hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/analysis/HoodieAnalysis.scala
##########
@@ -0,0 +1,318 @@
+/*
+ * 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.hudi.analysis
+
+import org.apache.hudi.SparkSqlAdapterSupport
+
+import scala.collection.JavaConverters._
+import org.apache.hudi.common.model.HoodieRecord
+import org.apache.spark.SPARK_VERSION
+import org.apache.spark.sql.{AnalysisException, SparkSession}
+import org.apache.spark.sql.catalyst.analysis.UnresolvedStar
+import org.apache.spark.sql.catalyst.expressions.AttributeReference
+import org.apache.spark.sql.catalyst.analysis.UnresolvedAttribute
+import org.apache.spark.sql.catalyst.expressions.{Alias, Expression, Literal, 
NamedExpression}
+import org.apache.spark.sql.catalyst.plans.Inner
+import org.apache.spark.sql.catalyst.plans.logical.{Assignment, DeleteAction, 
DeleteFromTable, InsertAction, LogicalPlan, MergeIntoTable, Project, 
UpdateAction, UpdateTable}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.command.CreateDataSourceTableCommand
+import org.apache.spark.sql.execution.datasources.{CreateTable, 
LogicalRelation}
+import org.apache.spark.sql.hudi.HoodieSqlUtils._
+import org.apache.spark.sql.hudi.command.{CreateHoodieTableAsSelectCommand, 
CreateHoodieTableCommand, DeleteHoodieTableCommand, 
InsertIntoHoodieTableCommand, MergeIntoHoodieTableCommand, 
UpdateHoodieTableCommand}
+import org.apache.spark.sql.types.StringType
+
+object HoodieAnalysis {
+  def customResolutionRules(): Seq[SparkSession => Rule[LogicalPlan]] =
+    Seq(
+      session => HoodieResolveReferences(session),
+      session => HoodieAnalysis(session)
+    )
+
+  def customPostHocResolutionRules(): Seq[SparkSession => Rule[LogicalPlan]] =
+    Seq(
+      session => HoodiePostAnalysisRule(session)
+    )
+}
+
+/**
+  * Rule for convert the logical plan to command.
+  * @param sparkSession
+  */
+case class HoodieAnalysis(sparkSession: SparkSession) extends Rule[LogicalPlan]
+  with SparkSqlAdapterSupport {
+
+  override def apply(plan: LogicalPlan): LogicalPlan = {
+    plan match {
+      // Convert to MergeIntoHoodieTableCommand
+      case m @ MergeIntoTable(target, _, _, _, _)
+        if m.resolved && isHoodieTable(target, sparkSession) =>
+          MergeIntoHoodieTableCommand(m)
+
+      // Convert to UpdateHoodieTableCommand
+      case u @ UpdateTable(table, _, _)
+        if u.resolved && isHoodieTable(table, sparkSession) =>
+          UpdateHoodieTableCommand(u)
+
+      // Convert to DeleteHoodieTableCommand
+      case d @ DeleteFromTable(table, _)
+        if d.resolved && isHoodieTable(table, sparkSession) =>
+          DeleteHoodieTableCommand(d)
+
+      // Convert to InsertIntoHoodieTableCommand
+      case l if sparkSqlAdapter.isInsertInto(l) =>
+        val (table, partition, query, overwrite, _) = 
sparkSqlAdapter.getInsertIntoChildren(l).get
+        table match {
+          case relation: LogicalRelation if isHoodieTable(relation, 
sparkSession) =>
+            new InsertIntoHoodieTableCommand(relation, query, partition, 
overwrite)
+          case _ =>
+            l
+        }
+      // Convert to CreateHoodieTableAsSelectCommand
+      case CreateTable(table, mode, Some(query))
+        if query.resolved && isHoodieTable(table) =>
+          CreateHoodieTableAsSelectCommand(table, mode, query)
+      case _=> plan
+    }
+  }
+}
+
+/**
+  * Rule for resolve hoodie's extended syntax or rewrite some logical plan.
+  * @param sparkSession
+  */
+case class HoodieResolveReferences(sparkSession: SparkSession) extends 
Rule[LogicalPlan]
+  with SparkSqlAdapterSupport {
+  private lazy val analyzer = sparkSession.sessionState.analyzer
+
+  def apply(plan: LogicalPlan): LogicalPlan = {
+    plan match {
+      // Resolve merge into
+      case MergeIntoTable(target, source, mergeCondition, matchedActions, 
notMatchedActions)
+        if isHoodieTable(target, sparkSession) && target.resolved && 
source.resolved =>
+
+        def isEmptyAssignments(assignments: Seq[Assignment]): Boolean = {
+          if (assignments.isEmpty) {
+            true
+          } else {
+            // This is a Hack for test if it is "update set *" or "insert *" 
for spark3.
+            // As spark3's own ResolveReference will append first five columns 
of the target
+            // table(which is the hoodie meta fields) to the assignments for 
"update set *" and
+            // "insert *", so we test if the first five assignmentFieldNames 
is the meta fields
+            // to judge if it is "update set *" or "insert *".
+            // We can do this because under the normal case, we should not 
allow to update or set
+            // the hoodie's meta field in sql statement, it is a system field, 
cannot set the value
+            // by user.
+            if (SPARK_VERSION.startsWith("3.")) {

Review comment:
       good suggestion!




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

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


Reply via email to