You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/04/06 16:02:04 UTC

[GitHub] [spark] gengliangwang commented on a diff in pull request #36077: [SPARK-38795][SQL] Support INSERT INTO user specified column lists with DEFAULT values

gengliangwang commented on code in PR #36077:
URL: https://github.com/apache/spark/pull/36077#discussion_r844123690


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveDefaultColumns.scala:
##########
@@ -239,12 +263,40 @@ case class ResolveDefaultColumns(
       val lookup = catalog.lookupRelation(tableName)
       lookup match {
         case SubqueryAlias(_, r: UnresolvedCatalogRelation) =>
-          Some(StructType(r.tableMeta.schema.fields.dropRight(
-            enclosingInsert.get.partitionSpec.size)))
-        case _ => None
+          StructType(r.tableMeta.schema.fields.dropRight(
+            enclosingInsert.get.partitionSpec.size))
+        case _ => return None
       }
     } catch {
-      case _: NoSuchTableException => None
+      case _: AnalysisException => return None
+    }
+    // Rearrange the columns in the result schema to match the order of the explicit column list,
+    // if any.
+    val userSpecifiedCols: Seq[String] = enclosingInsert.get.userSpecifiedCols
+    if (userSpecifiedCols.isEmpty) {
+      return Some(schema)
+    }
+    val colNamesToFields: Map[String, StructField] =
+      schema.fields.map {
+        field: StructField => field.name -> field
+      }.toMap
+    val userSpecifiedFields: Seq[StructField] =
+      userSpecifiedCols.map {
+        name: String => colNamesToFields.getOrElse(name, return Some(schema))
+      }
+    val userSpecifiedColNames: Set[String] = userSpecifiedCols.toSet
+    val nonUserSpecifiedFields: Seq[StructField] =
+      schema.fields.filter {
+        field => !userSpecifiedColNames.contains(field.name)
+      }
+    if (SQLConf.get.useNullsForMissingDefaultColumnValues) {

Review Comment:
   this seems duplicated with `getDefaultExpressions`



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org