leesf commented on a change in pull request #2761:
URL: https://github.com/apache/hudi/pull/2761#discussion_r614776987



##########
File path: 
hudi-spark-datasource/hudi-spark3-extensions_2.12/src/main/scala/org/apache/spark/sql/catalyst/analysis/HudiAnalysis.scala
##########
@@ -0,0 +1,240 @@
+/*
+ * 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.catalyst.analysis
+
+import org.apache.hudi.common.model.HoodieRecord
+import org.apache.hudi.execution.HudiSQLUtils
+import org.apache.hudi.execution.HudiSQLUtils.HoodieV1Relation
+import org.apache.hudi.spark3.internal.HoodieDataSourceInternalTable
+import org.apache.spark.sql.catalyst.catalog.{CatalogTable, HiveTableRelation}
+import org.apache.spark.sql.{AnalysisException, SparkSession}
+import org.apache.spark.sql.catalyst.expressions.{Alias, Literal}
+import org.apache.spark.sql.catalyst.merge.HudiMergeIntoUtils
+import org.apache.spark.sql.catalyst.plans.logical._
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.datasources.LogicalRelation
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation
+import org.apache.spark.sql.hudi.execution.InsertIntoHudiTable
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.types.{StructField, StructType}
+
+import scala.collection.mutable
+
+/**
+ * Analysis rules for hudi. deal with * in merge clause and insert into clause
+ */
+class HudiAnalysis(session: SparkSession, conf: SQLConf) extends 
Rule[LogicalPlan] {
+  override def apply(plan: LogicalPlan): LogicalPlan = 
plan.resolveOperatorsDown {
+    case dsv2 @ DataSourceV2Relation(d: HoodieDataSourceInternalTable, _, _, 
_, options) =>
+      HoodieV1Relation.fromV2Relation(dsv2, d, options)
+
+    // ResolveReferences rule will resolve * in merge, but the resolve result 
is not what we expected
+    // so if source is not resolved, we add a special placeholder to prevert 
ResolveReferences rule to resolve * in mergeIntoTable
+    case m @ MergeIntoTable(target, source, _, _, _) =>
+      if (source.resolved) {
+        dealWithStarAction(m)
+      } else {
+        placeHolderStarAction(m)
+      }
+    // we should deal with Meta columns in hudi, it will be safe to deal 
insertIntoStatement here.
+    case i @ InsertIntoStatement(table, _, query, overwrite, _)
+      if table.resolved && query.resolved && 
HudiSQLUtils.isHudiRelation(table) =>
+      table match {
+        case relation: HiveTableRelation =>
+          val metadata = relation.tableMeta
+          preprocess(i, metadata.identifier.quotedString, 
metadata.partitionSchema, Some(metadata), query, overwrite)
+        case dsv2 @ DataSourceV2Relation(d: HoodieDataSourceInternalTable, _, 
_, _, _) =>
+          preprocessV2(i, d, query)
+        case l: LogicalRelation =>
+          val catalogTable = l.catalogTable
+          val tblName = 
catalogTable.map(_.identifier.quotedString).getOrElse("unknown")
+          preprocess(i, tblName, catalogTable.get.partitionSchema, 
catalogTable, query, overwrite)
+        case _ => i
+      }
+  }
+
+  /**
+   * do align hoodie metacols, hoodie tablehave metaCols which are hidden by 
default, we try our best to fill those cols auto

Review comment:
       table has 




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