dejankrak-db commented on code in PR #59640:
URL: https://github.com/apache/spark/pull/59640#discussion_r4462755358
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala:
##########
@@ -1253,6 +1334,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) {
Review Comment:
No. The other checks only match when a plan contains a FILE, and the parser
produces a FILE only when `spark.sql.fileType.enabled` is true, so they cost
nothing when it is off. This one is different: it walks every expression of
every query to find UDFs, and a UDF's return type comes from the API rather
than the parser. The flag keeps that walk off the default path. The
registration-time check (`checkNoQualifierInRegisteredRoutineReturnType`) is
gated the same way.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/TypeUtils.scala:
##########
@@ -70,7 +72,8 @@ object TypeUtils extends QueryErrorsBase {
}
def checkForMapKeyType(keyType: DataType): TypeCheckResult = {
- if (keyType.existsRecursively(dt => dt.isInstanceOf[MapType] ||
dt.isInstanceOf[VariantType])) {
+ if (keyType.existsRecursively(dt => dt.isInstanceOf[MapType] ||
dt.isInstanceOf[VariantType] ||
Review Comment:
Agreed, as a follow-up. Sorting, and comparisons such as join conditions,
already go through the existing orderability check (`RowOrdering.isOrderable`),
which is how FILE is rejected there; this PR only adds tests for them. The
per-type lists are for operations that need equality rather than an ordering
(set operations, grouping, hashing, map keys). Spark has no "supports equality"
property today, which is why MAP and VARIANT are hard-coded there in the same
way. A property on the type, or deriving it as @holdenk suggests, would replace
these lists for all three types. @dtenedor also asked for a shared helper for
these checks, so I will file one follow-up covering both.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/TypeUtils.scala:
##########
@@ -162,4 +165,123 @@ 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))
+ }
Review Comment:
I kept them in `TypeUtils`, next to the VARIANT and interval checks they
mirror, to keep this PR small. Moving the FILE helpers into their own object
fits well with the follow-up for a shared helper for the types that are not
comparable (see the other thread), so I would do it there.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CheckAnalysis.scala:
##########
@@ -1170,6 +1234,13 @@ trait CheckAnalysis extends LookupCatalog with
QueryErrorsBase with PlanToString
origin = operator.origin
)
+ // FILE has no defined ordering, so it cannot be compared in a set
operation.
+ case o if fileColumnInSetOperation(o).isDefined =>
+ throw QueryCompilationErrors.unsupportedSetOperationOnFileType(
+ fileCol = fileColumnInSetOperation(o).get,
+ origin = operator.origin
+ )
+
Review Comment:
For FILE this is by design. The SPIP says: "FileType is not comparable... It
is not clear what equality of two files should mean, so we propose no ordering,
which means no joins, no grouping, no sorting and no partitioning on a FILE
column. Users who want those operations can extract the uri field." So unlike
VARIANT, which has the TODO above to support ordering, FILE has no planned
equality or hashing. If we later decide what equality of two files means (for
example by `uri`, or by `uri` and `checksum`), these checks are where it would
be relaxed, and I will capture that question in a follow-up JIRA. The comment
here wrongly put it down to the ordering; 494fb1ea4e6 now says FILE is not
comparable because it has no defined equality.
--
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]