johanl-db commented on code in PR #40885:
URL: https://github.com/apache/spark/pull/40885#discussion_r1173430612


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileFormat.scala:
##########
@@ -241,47 +256,74 @@ object FileFormat {
     FileSourceConstantMetadataStructField(FILE_BLOCK_LENGTH, LongType, 
nullable = false),
     FileSourceConstantMetadataStructField(FILE_MODIFICATION_TIME, 
TimestampType, nullable = false))
 
+  /**
+   * All [[BASE_METADATA_FIELDS]] require custom extractors because they are 
derived directly from
+   * fields of the [[PartitionedFile]], and do have entries in the file's 
metadata map.
+   */
+  val BASE_METADATA_EXTRACTORS: Map[String, PartitionedFile => Any] = Map(
+    FILE_PATH -> { pf: PartitionedFile => pf.toPath.toString },
+    FILE_NAME -> { pf: PartitionedFile => pf.toPath.getName },
+    FILE_SIZE -> { pf: PartitionedFile => pf.fileSize },
+    FILE_BLOCK_START -> { pf: PartitionedFile => pf.start },
+    FILE_BLOCK_LENGTH -> { pf: PartitionedFile => pf.length },
+    // The modificationTime from the file has millisecond granularity, but the 
TimestampType for
+    // `file_modification_time` has microsecond granularity.
+    FILE_MODIFICATION_TIME -> { pf: PartitionedFile => pf.modificationTime * 
1000 }
+  )
+
+  /**
+   * Extracts the [[Literal]] value of a file-constant metadata column from a 
[[PartitionedFile]].
+   *
+   * If an extractor is available, use it. Otherwise, attempt to fetch the 
value directly from the
+   * file's metadata map, returning null if not found.
+   *
+   * Raw values (including null) are automatically converted to literals as a 
courtesy.
+   */
+  def getFileConstantMetadataColumnValue(
+      name: String,
+      file: PartitionedFile,
+      metadataExtractors: Map[String, PartitionedFile => Any]): Literal = {
+    val extractor = metadataExtractors.get(name).getOrElse {
+      (_: PartitionedFile).otherConstantMetadataColumnValues.get(name).orNull

Review Comment:
   Note: the syntax
   ```
   (_: PartitionedFile).otherConstantMetadataColumnValues.get(name).orNull
   ```
   to define the default extractor threw me off at first, the following would 
be easier to understand:
   ```
   pf: PartitionedFile => pf.otherConstantMetadataColumnValues.get(name).orNull
   ```



##########
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.
+   *
+   * A scanner must derive each file-constant metadata field's value from each 
[[PartitionedFile]]
+   * it processes. By default, the value is obtained by a direct lookup of the 
column's name on
+   * [[PartitionedFile.otherConstantMetadataColumnValues]] (see
+   * [[FileFormat.getFileConstantMetadataColumnValue]]). However, 
implementations can override this
+   * method in order to provide more sophisticated lazy extractors (e.g. in 
case the column value is
+   * complicated or expensive to compute).

Review Comment:
   Someone adding a new constant metadata column in the future will need to 
choose between the two alternatives to provide values, this choice could be 
described more explicitly here:
   
   - extractor: anytime the value can be directly derived from 
`PartitionedFile` information.
   - `PartitionedFile.otherConstantMetadataColumnValues`: all other cases, e.g. 
the value is arbitrary or is coming from some other place



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