dejankrak-db commented on code in PR #60535:
URL: https://github.com/apache/spark/pull/60535#discussion_r4588200070


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/TypeUtils.scala:
##########
@@ -175,60 +175,69 @@ object TypeUtils extends QueryErrorsBase {
     }
   }
 
-  private def containsUnknownFileType(dataType: DataType): Boolean = {
-    dataType.existsRecursively {
-      case ft: FileType => ft.mode == FileTypeMode.UNKNOWN
-      case _ => false
-    }
-  }
-
   /**
-   * Fails if a FILE in `dataType`, at any nesting depth, has no qualifier, as 
a FILE in a table
-   * column definition must be declared `FILE EXTERNAL`.
+   * Returns the paths of the columns that hold a FILE without a qualifier, 
for a column at `path`
+   * of type `dataType`. The path descends into struct fields, so a FILE in a 
field of a struct
+   * column is reported at that field. A FILE in an array or a map is reported 
at the column of
+   * the array or map.
    */
-  def checkFileTypeHasQualifier(dataType: DataType): Unit = {
-    if (containsUnknownFileType(dataType)) {
-      throw QueryCompilationErrors.fileTypeMissingQualifierError()
-    }
+  private def unknownFileTypePaths(
+      path: Seq[String],
+      dataType: DataType): Seq[(Seq[String], DataType)] = dataType match {
+    case st: StructType =>
+      st.fields.toSeq.flatMap(f => unknownFileTypePaths(path :+ f.name, 
f.dataType))
+    case dt if dt.existsRecursively {
+        case ft: FileType => ft.mode == FileTypeMode.UNKNOWN
+        case _ => false
+      } => Seq(path -> dt)
+    case _ => Seq.empty
   }
 
   /**
-   * Fails if any of `columns`, which a write such as CREATE TABLE AS SELECT 
stores in a table,
-   * holds a FILE without a qualifier, as a table cannot store one.
+   * Fails if a FILE in any of `columns`, given as the path of a column and 
its type, has no
+   * qualifier, as a FILE in a table column definition must be declared `FILE 
EXTERNAL`.
    */
-  def checkNoUnknownFileTypeWrite(columns: Seq[Attribute]): Unit = {
-    checkNoUnknownFileTypeInColumns(columns.map(c => Seq(c.name) -> 
c.dataType))
+  def checkFileTypeHasQualifier(columns: Seq[(Seq[String], DataType)]): Unit = 
{
+    columns.iterator.flatMap { case (path, dataType) => 
unknownFileTypePaths(path, dataType) }
+      .take(1).foreach { case (path, dataType) =>
+        throw QueryCompilationErrors.fileTypeMissingQualifierError(path, 
dataType)
+      }
   }
 
-  /**
-   * Fails if a column of `schema`, which a command such as CREATE TABLE LIKE 
stores in a new
-   * table, holds a FILE without a qualifier.
-   */
-  def checkNoUnknownFileTypeInTableSchema(schema: StructType): Unit = {
-    checkNoUnknownFileTypeInColumns(schema.fields.toSeq.map(f => Seq(f.name) 
-> f.dataType))
+  /** Fails if a FILE in a column of `schema` has no qualifier. */
+  def checkFileTypeHasQualifier(schema: StructType): Unit = {
+    checkFileTypeHasQualifier(schema.fields.toSeq.map(f => Seq(f.name) -> 
f.dataType))
   }
 
   /**
-   * Fails if any of `columns`, given as the path of a column and the type it 
would be stored
-   * with, holds a FILE without a qualifier.
+   * Fails if any of `columns`, which a write such as CREATE TABLE AS SELECT, 
CREATE TABLE LIKE or
+   * schema evolution stores in a table, holds a FILE without a qualifier, as 
a table cannot store
+   * one. A column is given as its path and the type it would be stored with.
    */
-  def checkNoUnknownFileTypeInColumns(columns: Seq[(Seq[String], DataType)]): 
Unit = {
-    val unknownCols = columns.collect {
-      case (path, dataType) if containsUnknownFileType(dataType) => path
+  def checkNoUnknownFileTypeWrite(columns: Seq[(Seq[String], DataType)]): Unit 
= {
+    val unknownPaths = columns.flatMap { case (path, dataType) =>
+      unknownFileTypePaths(path, dataType).map(_._1)

Review Comment:
   Done in 67e95fad0c1: added a CREATE TABLE AS SELECT with two FILE fields in 
a struct and a FILE column, expecting `[`s`.`a`, `s`.`b`, `f`]`. It fails if 
only the first path is kept.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CreateTableValidation.scala:
##########
@@ -68,27 +68,33 @@ private[sql] object CreateTableValidation {
     create match {
       // The columns of CREATE TABLE AS SELECT come from its query, not from 
DDL.
       case ctas: V2CreateTableAsSelectPlan =>
-        TypeUtils.checkNoUnknownFileTypeWrite(ctas.query.output)
+        TypeUtils.checkNoUnknownFileTypeWrite(ctas.query.schema)
       case _ =>
         TypeUtils.checkFileTypeHasQualifier(create.tableSchema)
     }
-    // FILE is not comparable, so it cannot be a partition, clustering or 
write-ordering column.
-    create.partitioning.foreach { transform =>
-      val usage = transform match {
-        case _: ClusterByTransform => "CLUSTERING_COLUMN"
-        case _ => "PARTITION_COLUMN"
-      }
-      transform.references.foreach { ref =>
+    // FILE is not comparable, so it cannot be a partition, bucket, clustering 
or write-ordering
+    // column.
+    def checkNoFileTypeInColumns(refs: Seq[NamedReference], usage: 
FileTypeColumnUsage): Unit =
+      refs.foreach { ref =>
         
create.tableSchema.findNestedField(ref.fieldNames.toImmutableArraySeq).foreach {
-          case (_, field) => TypeUtils.checkNoFileTypeInColumnUsage(field, 
usage)
+          case (parentPath, field) =>
+            TypeUtils.checkNoFileTypeInColumnUsage(parentPath :+ field.name, 
field.dataType, usage)
         }
       }
+    create.partitioning.foreach {
+      case clusterBy: ClusterByTransform =>
+        checkNoFileTypeInColumns(clusterBy.references.toSeq, 
FileTypeColumnUsage.ClusteringColumn)
+      case bucket: BucketTransform =>
+        checkNoFileTypeInColumns(bucket.columns, 
FileTypeColumnUsage.BucketColumn)
+      case bucket: SortedBucketTransform =>
+        checkNoFileTypeInColumns(bucket.columns, 
FileTypeColumnUsage.BucketColumn)

Review Comment:
   Done in 67e95fad0c1: added the `CLUSTERED BY (f) SORTED BY (id) INTO 4 
BUCKETS` row expecting `BUCKET_COLUMN`. It fails if this line reports 
`BucketSortColumn`.



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