dejankrak-db commented on code in PR #59640:
URL: https://github.com/apache/spark/pull/59640#discussion_r4494290832
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/TypeUtils.scala:
##########
@@ -162,4 +165,124 @@ object TypeUtils extends QueryErrorsBase {
messageParameters = scala.collection.immutable.Map.empty)
}
}
+
+ 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`.
+ */
+ def checkFileTypeHasQualifier(dataType: DataType): Unit = {
+ if (containsUnknownFileType(dataType)) {
+ throw QueryCompilationErrors.fileTypeMissingQualifierError()
+ }
+ }
+
+ /**
+ * 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.
+ */
+ def checkNoUnknownFileTypeWrite(columns: Seq[Attribute]): Unit = {
+ checkNoUnknownFileTypeInColumns(columns.map(c => Seq(c.name) ->
c.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 any of `columns`, given as the path of a column and the type it
would be stored
+ * with, holds a FILE without a qualifier.
+ */
+ def checkNoUnknownFileTypeInColumns(columns: Seq[(Seq[String], DataType)]):
Unit = {
+ val unknownCols = columns.collect {
+ case (path, dataType) if containsUnknownFileType(dataType) => path
+ }
+ if (unknownCols.nonEmpty) {
+ throw QueryCompilationErrors.fileTypeUnknownWriteError(unknownCols)
+ }
+ }
+
+ /**
+ * Fails if `field` holds a FILE, as FILE is not comparable and so cannot be
a partition column
+ * (`usage` is `PARTITION_COLUMN`), a clustering column
(`CLUSTERING_COLUMN`) or a write-ordering
+ * column (`WRITE_ORDERING_COLUMN`).
+ */
+ def checkNoFileTypeInColumnUsage(field: StructField, usage: String): Unit = {
+ if (field.dataType.existsRecursively(_.isInstanceOf[FileType])) {
+ throw QueryCompilationErrors.fileTypeUnsupportedColumnUsageError(field,
usage)
+ }
+ }
+
+ /** Fails if a clustering column of `clusterBySpec` holds a FILE. */
+ def checkNoFileTypeClusteringColumn(
+ schema: StructType,
+ clusterBySpec: ClusterBySpec,
+ resolver: Resolver): Unit = {
+ clusterBySpec.columnNames.foreach { ref =>
+ schema.findNestedField(ref.fieldNames.toImmutableArraySeq, resolver =
resolver)
Review Comment:
Thanks, confirmed and fixed in 2c11bb0be93.
`checkNoFileTypeClusteringColumn` now returns early when the schema holds no
FILE. When it does, it looks the column up as `SchemaUtils.findColumnPosition`
does: the first match, descending into structs and the struct elements of
arrays, and skipping a path it cannot resolve, so the normalizer keeps
reporting that. The new test runs `ALTER TABLE ... CLUSTER BY (arr.x)` on V1
and V2 tables and V1 `CREATE TABLE ... CLUSTER BY (arr.x)` with the flag on and
off (it fails with `INVALID_FIELD_NAME` without the fix), and checks that a
FILE in an array struct element is still rejected in V1 `CREATE TABLE` and V2
`ALTER TABLE`. One correction: V1 CTAS is not affected by this PR.
`V2CreateTableAsSelectPlan.resolved` already rejects `CLUSTER BY (arr.x)` for
any CTAS, with or without a FILE, before this check runs.
##########
sql/api/src/main/scala/org/apache/spark/sql/types/FileType.scala:
##########
@@ -25,30 +25,33 @@ import org.apache.spark.annotation.Unstable
/**
* The data type representing a reference to a file, with metadata describing
the file.
*
- * In SQL the type is named `FILE`, and the `EXTERNAL` keyword is planned to
be required when
- * declaring a table column, as in `CREATE TABLE t (f FILE EXTERNAL)`; until
then a bare `FILE` is
- * accepted wherever a type is. Its JSON representation is `"file external"`
for
- * `FileType.external` and `"file unknown"` for `FileType()`, except that an
external file in a
- * struct field is the plain `"file"`. A bare `"file"` is read as external
inside a struct field
- * and as unknown elsewhere.
+ * In SQL the type is named `FILE`. A table column must be declared with the
`EXTERNAL` keyword,
+ * as in `CREATE TABLE t (f FILE EXTERNAL)`, while the signature of a function
declares a bare
+ * `FILE`. Its JSON representation is `"file external"` for
`FileType.external` and
+ * `"file unknown"` for `FileType()`, except that an external file in a struct
field is the plain
+ * `"file"`. A bare `"file"` is read as external inside a struct field and as
unknown elsewhere.
*
* FileType is planned to be physically stored as a struct with the following
fields (see
* `FileType.FIELDS`), but it deliberately does not extend [[StructType]]:
- * - uri (STRING): the opaque URI to the file
+ * - uri (STRING): the URI of the referenced file, which may be absolute or
relative
* - offset (BIGINT): the start of the referenced byte range
- * - size (BIGINT): the size of the file in bytes
- * - content_type (STRING): the MIME/content type of the file
- * - checksum (STRING): an algorithm-tagged integrity token for the file,
e.g. "ETAG:{etag}"
+ * - size (BIGINT): the length of the referenced byte range
+ * - content_type (STRING): the MIME/content type of the referenced bytes
+ * - checksum (STRING): an algorithm-tagged integrity token for the
referenced bytes, e.g.
+ * "MD5:{digest}", or "ETAG:{etag}" for the whole file
* - inline (BINARY): the referenced bytes stored inline in the value
*
* FileType may be EXTERNAL (simple references, lifecycle independent of the
table) or UNKNOWN
* (mode not yet determined, such as the result of a cast or of a UDF). Mode
is part of type
* identity - files with different modes are distinct under `equals`.
*
- * FileType is planned to be not comparable -- it cannot be used in join
expressions, grouping
- * expressions, or as a partition/clustering column. Users can extract the uri
field for such
- * operations. This type currently only guarantees the schema-level pieces
(DDL and JSON); the
- * physical representation and most of the restrictions are added by later
parts.
+ * FileType is not comparable: it defines neither equality nor an ordering, as
it is not clear
+ * what equality of two files should mean. So an operation that needs equality
fails at analysis,
Review Comment:
Rewrote it in 2c11bb0be93. It now lists the uses that fail at analysis today
(each backed by a check and a test), names the aggregates that are not rejected
yet (`count(DISTINCT f)`, `collect_set`, `approx_count_distinct`, `mode`), and
describes access to the fields of a FILE, such as `uri`, as planned together
with the physical representation.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala:
##########
@@ -1202,6 +1257,28 @@ trait CheckAnalysis extends LookupCatalog with
QueryErrorsBase with PlanToString
}
}
+ /**
+ * Checks that the [[UserDefinedExpression]]s in a query declare only a
plain FILE in their
+ * return type, as
[[TypeUtils.checkNoQualifierInRegisteredRoutineReturnType]] does when a
+ * routine is registered. This covers routines whose signature is not
checked when they are
+ * created, such as UDFs that are not registered and UDAF classes registered
with
+ * `CREATE FUNCTION ... AS`. SQL functions are checked by `CREATE FUNCTION`.
V2 catalog functions
+ * are covered only when they are bound as an [[ApplyFunctionExpression]]. A
non-scalar Python
+ * UDF is skipped, as its data type is the schema of the rows it returns.
+ */
+ private def checkFileTypeInUserDefinedRoutines(plan: LogicalPlan): Unit = {
+ if (SQLConf.get.isFileTypeEnabled) {
+ plan.foreach { operator =>
Review Comment:
Done in 2c11bb0be93. The walk now skips sub-plans that are already analyzed,
as `checkAnalysis0` does, and reads the flag once per pass. It checks a
`UserDefinedExpression` only when its return type holds a FILE, so routine
names are quoted only on that path. The errors, the quoted names and the
non-scalar Python UDF exemption are unchanged, and the existing tests for
unregistered Scala and Python UDFs and the UDAF class registered by name still
pass.
--
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]