dtenedor commented on code in PR #40049:
URL: https://github.com/apache/spark/pull/40049#discussion_r1109091855


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statements.scala:
##########
@@ -135,6 +138,16 @@ case class QualifiedColType(
   def name: Seq[String] = path.map(_.name).getOrElse(Nil) :+ colName
 
   def resolved: Boolean = path.forall(_.resolved) && 
position.forall(_.resolved)
+
+  def getV2Default: ColumnDefaultValue = {
+    default.map { sql =>
+      val e = ResolveDefaultColumns.analyze(colName, dataType, sql, "ALTER 
TABLE")
+      assert(e.resolved && e.foldable,
+        "exist default must be simple SQL string that is resolved and 
foldable, " +

Review Comment:
   ```suggestion
           "The existence default value must be a simple SQL string that is 
resolved and foldable, " +
   ```



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Util.scala:
##########
@@ -431,4 +431,73 @@ private[sql] object CatalogV2Util {
       .getOrElse(catalogManager.v2SessionCatalog)
       .asTableCatalog
   }
+
+  def v2ColumnsToStructType(columns: Array[Column]): StructType = {
+    StructType(columns.map(v2ColumnToStructField))
+  }
+
+  private def v2ColumnToStructField(col: Column): StructField = {
+    val metadata = 
Option(col.metadataInJSON()).map(Metadata.fromJson).getOrElse(Metadata.empty)
+    var f = StructField(col.name(), col.dataType(), col.nullable(), metadata)
+    Option(col.comment()).foreach { comment =>
+      f = f.withComment(comment)
+    }
+    Option(col.defaultValue()).foreach { default =>
+      f = encodeDefaultValue(default, f)
+    }
+    f
+  }
+
+  // For built-in file sources, we encode the default value in StructField 
metadata. An analyzer
+  // rule will check the special metadata and change the DML input plan to 
fill the default value.
+  private def encodeDefaultValue(defaultValue: ColumnDefaultValue, f: 
StructField): StructField = {
+    Option(defaultValue).map { default =>
+      // The "exist default" is used to back-fill the existing data when new 
columns are added, and
+      // should be a fixed value which was evaluated at the definition time. 
For example, if the
+      // default value is `current_date()`, the "exist default" should be the 
value of
+      // `current_date()` when the column was defined/altered, instead of when 
back-fall happens.
+      // Note: the back-fill here is a logical concept. The data source can 
keep the existing
+      //       data unchanged and let the data reader to return "exist 
default" for missing
+      //       columns.
+      val existingDefault = Literal(default.getValue.value(), 
default.getValue.dataType()).sql
+      
f.withExistenceDefaultValue(existingDefault).withCurrentDefaultValue(default.getSql)
+    }.getOrElse(f)
+  }
+
+  def structTypeToV2Columns(schema: StructType): Array[Column] = {
+    schema.fields.map(structFieldToV2Column)
+  }
+
+  private def structFieldToV2Column(f: StructField): Column = {
+    def createV2Column(defaultValue: ColumnDefaultValue, metadata: Metadata): 
Column = {
+      val metadataJSON = if (metadata == Metadata.empty) {
+        null
+      } else {
+        metadata.json
+      }
+      Column.create(
+        f.name, f.dataType, f.nullable, f.getComment().orNull, defaultValue, 
metadataJSON)
+    }
+    if (f.getCurrentDefaultValue().isDefined && 
f.getExistenceDefaultValue().isDefined) {
+      val e = analyze(f, EXISTS_DEFAULT_COLUMN_METADATA_KEY)
+      assert(e.resolved && e.foldable,
+        "exist default must be simple SQL string that is resolved and 
foldable, " +

Review Comment:
   ```suggestion
           "The existence default value must be a simple SQL string that is 
resolved and foldable, " +
   ```



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/connector/catalog/CatalogV2Util.scala:
##########
@@ -431,4 +431,73 @@ private[sql] object CatalogV2Util {
       .getOrElse(catalogManager.v2SessionCatalog)
       .asTableCatalog
   }
+
+  def v2ColumnsToStructType(columns: Array[Column]): StructType = {

Review Comment:
   could we have method comments here the describe who is expected to call 
these methods, and what they convert to and from?



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -3108,7 +3108,7 @@ object SQLConf {
         "provided values when the corresponding fields are not present in 
storage.")
       .version("3.4.0")
       .stringConf
-      .createWithDefault("csv,json,orc,parquet")
+      .createWithDefault("csv,json,orc,parquet,hive")

Review Comment:
   Is this safe? What data source operator implements the `hive` provider? Does 
it support filling in the existence default values? Do we have any 
default-value test cases in this PR where the table is `using hive`?



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/connector/ColumnImpl.scala:
##########
@@ -0,0 +1,30 @@
+/*
+ * 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.internal.connector
+
+import org.apache.spark.sql.connector.catalog.{Column, ColumnDefaultValue}
+import org.apache.spark.sql.types.DataType
+
+// The default implementation of v2 column.

Review Comment:
   ```suggestion
   // The standard concrete implementation of data source V2 column.
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to