spark-test commented on code in PR #60535:
URL: https://github.com/apache/spark/pull/60535#discussion_r4582093159
##########
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))
Review Comment:
**Nit (P3):** This walk descends into every struct before it knows whether a
FILE without EXTERNAL is present. For each struct it copies the field array
(`fields.toSeq` copies under Scala 2.13), builds a path per field and a
`flatMap` result, only to return an empty `Seq` in the common case. Nothing
gates the callers on `spark.sql.fileType.enabled`, so every V1/V2 CREATE TABLE,
CTAS/RTAS, CREATE TABLE LIKE, schema-evolving write and construction of an
ALTER TABLE ADD/REPLACE COLUMNS or ALTER COLUMN TYPE node pays for it, with the
flag on or off. The base branch decided the same check with one
`existsRecursively` that builds nothing. The cost is small and analysis-only,
but only the error message needs the paths.
Returning `Seq.empty` before the `StructType` case when `dataType` holds no
FILE without EXTERNAL (the same `existsRecursively` predicate the base branch
used) keeps the reported paths and their order, because a struct without such a
FILE yields no path either way. The `StructType` overloads at lines 208-209 and
227-228 can then pass the schema itself with an empty path instead of first
building a `(Seq(name), type)` pair per top-level column.
**Verification:**
- **Regression:** For each type shape the FILE errors report (a top-level
FILE, a FILE in an array or a map, a FILE in a struct field, and a FILE in an
array two structs deep), FILE_TYPE_MISSING_QUALIFIER and
FILE_TYPE_UNKNOWN_WRITE still report the same column path, type and order as
before the change. This holds on CREATE TABLE, REPLACE TABLE, CREATE TABLE AS
SELECT, CREATE TABLE LIKE, schema evolution and ALTER TABLE.
- **Regression:** Tables, CREATE TABLE AS SELECT queries and ALTER TABLE ADD
COLUMNS statements whose schema has struct and array-of-struct columns but no
FILE without a qualifier still pass analysis, with spark.sql.fileType.enabled
on and off.
- **Inspection:** The FILE path walk tests a type for a FILE without a
qualifier before it descends into a struct, and the schema-level checks no
longer build a (name, type) pair for every top-level column. So a schema
without such a FILE is decided in one existence pass.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/TypeUtils.scala:
##########
@@ -242,37 +251,37 @@ object TypeUtils extends QueryErrorsBase {
schema: StructType,
clusterBySpec: ClusterBySpec,
resolver: Resolver): Unit = {
- def find(schema: StructType, path: Seq[String]): Option[StructField] =
+ // Returns the path of the column, with the names of the schema, and its
field.
+ def find(schema: StructType, path: Seq[String]): Option[(Seq[String],
StructField)] =
schema.fields.find(f => resolver(f.name, path.head)).flatMap { field =>
(path.tail, field.dataType) match {
- case (Seq(), _) => Some(field)
- case (tail, s: StructType) => find(s, tail)
- case (tail, ArrayType(s: StructType, _)) => find(s, tail)
+ case (Seq(), _) => Some(Seq(field.name) -> field)
+ case (tail, s: StructType) => find(s, tail).map { case (p, f) =>
(field.name +: p, f) }
Review Comment:
**Nit (P3):** No test reaches this struct branch with a FILE: the suite's
FILE clustering cases use a top-level `f` or `arr.f`, which take the leaf and
array branches. Reverting this line to the old `find(s, tail)` still compiles,
and it would make `CREATE TABLE t (id INT, s STRUCT<f: FILE EXTERNAL>) USING
parquet CLUSTER BY (s.f)` and `ALTER TABLE testcat.t CLUSTER BY (s.f)` report
`` `f` `` instead of `` `s`.`f` `` while the suite stays green. The `ORDERED BY
s.f` case does not cover this lookup, because write ordering, like V2 CREATE
TABLE ... CLUSTER BY, resolves through `findNestedField`. Struct-field cases
for V1 CREATE TABLE and ALTER TABLE CLUSTER BY, expecting `` `s`.`f` ``, would
pin it.
See **Shared repair plan 1** in the review body.
##########
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:
**Nit (P3):** This line has no failure signal. The suite's only
sorted-bucket row, `CLUSTERED BY (id) SORTED BY (f) INTO 4 BUCKETS`, buckets by
an INT, so this check runs but never throws in a test, and nothing else on the
V2 path checks a sorted spec's bucket columns. If this line were dropped or
passed `BucketSortColumn`, `CREATE TABLE testcat.t (id INT, f FILE EXTERNAL)
CLUSTERED BY (f) SORTED BY (id) INTO 4 BUCKETS` would pass analysis or report
the wrong subclass, and the suite would stay green. A `("CLUSTERED BY (f)
SORTED BY (id) INTO 4 BUCKETS", "BUCKET_COLUMN")` row in the existing clause
table of `FileTypeRestrictionsSuite` would cover it on V1 and V2 tables, with
and without a query.
See **Shared repair plan 1** in the review body.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/v2AlterTableCommands.scala:
##########
@@ -113,7 +113,7 @@ case class AddColumns(
columnsToAdd.foreach { c =>
TypeUtils.failWithIntervalType(c.dataType)
TypeUtils.failUnsupportedDataType(c.dataType, conf)
- TypeUtils.checkFileTypeHasQualifier(c.dataType)
+ TypeUtils.checkFileTypeHasQualifier(Seq(c.name -> c.dataType))
Review Comment:
**Nit (P3):** Every ADD COLUMN, REPLACE COLUMNS and ALTER COLUMN case in the
suite targets a top-level column, where `c.name` and `Seq(c.colName)` are the
same path. If this line passed `Seq(c.colName)`, or line 278 passed only the
last part of `spec.column.name`, `ALTER TABLE testcat.t ADD COLUMN s.x FILE`
and `ALTER TABLE testcat.t ALTER COLUMN s.x TYPE FILE` would report `` `x` ``
instead of `` `s`.`x` ``, and the suite would stay green. Nested ADD COLUMN and
ALTER COLUMN cases expecting `` `s`.`x` `` would pin both sites; REPLACE
COLUMNS needs none, because the parser rejects nested targets there.
See **Shared repair plan 1** in the review body.
##########
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.
Review Comment:
**Nit (P3):** The list leaves out bucket sort columns, which line 91 rejects
with `BUCKET_SORT_COLUMN` and which the V1 comment in `PreprocessTableCreation`
names. A reader comparing the two comments could conclude that V2 tables accept
a FILE sort column.
```suggestion
// FILE is not comparable, so it cannot be a partition, bucket, bucket
sort, clustering or
// write-ordering column.
```
**Verification:**
- **Inspection:** The comment above the V2 FILE column-usage checks lists
partition, bucket, bucket sort, clustering and write-ordering columns, the five
usages those checks raise, and names bucket sort columns as the V1 comment in
PreprocessTableCreation does.
##########
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:
**Nit (P3):** This listing is never exercised with more than one path: every
`FILE_TYPE_UNKNOWN_WRITE` expectation in the suite is `` [`f`] `` or ``
[`s`.`f`] ``. An implementation that kept only the first path, for example by
reusing the `take(1)` of `checkFileTypeHasQualifier`, would still pass. `CREATE
TABLE t USING parquet AS SELECT named_struct('a', CAST(NULL AS FILE), 'b',
CAST(NULL AS FILE)) AS s` would then report only `` `s`.`a` ``, and users would
fix one field per attempt. A CREATE TABLE AS SELECT case with two FILE fields
in one struct plus another FILE column, expecting all three paths in field
order, would pin it.
See **Shared repair plan 1** in the review body.
--
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]