ryan-johnson-databricks commented on code in PR #40885:
URL: https://github.com/apache/spark/pull/40885#discussion_r1174397988


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileFormat.scala:
##########
@@ -203,6 +203,21 @@ trait FileFormat {
    * method. Technically, a file format could choose suppress them, but that 
is not recommended.
    */
   def metadataSchemaFields: Seq[StructField] = FileFormat.BASE_METADATA_FIELDS
+
+  /**
+   * The extractors to use when deriving file-constant metadata columns for 
this file format.
+   *
+   * By default, the value of a file-constant metadata column is obtained by 
looking up the column's
+   * name in the file's metadata column value map. However, implementations 
can override this method
+   * in order to provide an extractor that has access to the entire 
[[PartitionedFile]] when
+   * deriving the column's value.
+   *
+   * NOTE: Extractors are lazy, invoked only if the query actually selects 
their column at runtime.
+   *
+   * See also [[FileFormat.getFileConstantMetadataColumnValue]].
+   */
+  def fileConstantMetadataExtractors: Map[String, PartitionedFile => Any] =

Review Comment:
   > Shall we have a single API to define constant metadata columns?
   
   Maybe? I couldn't figure out a good way to do it:
   
   > I originally wanted to just embed the extractor in the attribute 
definition directly, but attribute/struct metadata cannot store code and IMO 
that's a wise design decision we shouldn't change
   
   I guess we could store pairs or a triple, but (a) the extractors are 
optional (we'd be forcing every column to either use one or opt out); and (b) 
the uses for the attribute/struct definitions are rather separated from the 
uses of the extractors (result: a lot of `x.map` and `x.filter` type calls 
clutter up the code)
   
   > Should the extractor return a `Literal` instead of `Any`?
   
   I thought about it? But:
   * it complicates the definitions of all extractors with boilerplate, which 
IMO indicates a flawed design
   * we anyway have to wrap it in a literal again to handle the `null` vs. 
`lit(null)` case (or require every extractor to think carefully about nulls). 
   * Currently, `getFileConstantMetadataColumnValue` takes care of converting 
to literal, and that's what everyone uses (nobody calls the extractor directly).
   * It would be non-symmetric with the default case, that pulls `Any` from the 
column value map.
   
   Note that nothing prevents an extractor from returning a literal, because 
`Literal.apply` will detect and return it unchanged.
   
   Overall, I worry the above changes would only give the code a bigger surface 
area with little/no gain in safety or functionality?
   



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