ConvertToDeltaCommand — Importing Parquet Table Into Delta Lake (Converting Parquet Table To Delta Format)

ConvertToDeltaCommand is a Delta command that converts a parquet table into delta format (imports it into Delta).

ConvertToDeltaCommand is created and executed using DeltaConvert utility.

ConvertToDeltaCommand requires that the partition schema matches the partitions of the parquet table (or an AnalysisException is thrown)

ConvertToDeltaCommandBase is the base of ConvertToDeltaCommand-like commands with the only known implementation being ConvertToDeltaCommand itself.

Creating ConvertToDeltaCommand Instance

ConvertToDeltaCommand takes the following to be created:

  • Parquet table (TableIdentifier)

  • Partition schema (Option[StructType])

  • Path (Option[String])

Running Command — run Method

run(spark: SparkSession): Seq[Row]
run is part of the RunnableCommand contract to…​FIXME.

run creates a ConvertProperties from the TableIdentifier (with the given SparkSession).

run makes sure that the (data source) provider (the database part of the TableIdentifier) is either delta or parquet. For all other data source providers, run throws an AnalysisException:

CONVERT TO DELTA only supports parquet tables, but you are trying to convert a [sourceName] source: [ident]

For delta data source provider, run simply prints out the following message to standard output and returns.

The table you are trying to convert is already a delta table

For parquet data source provider, run uses DeltaLog utility to create a DeltaLog. run then requests DeltaLog to update and start a new transaction. In the end, run performConvert.

In case the readVersion of the new transaction is greater than -1, run simply prints out the following message to standard output and returns.

The table you are trying to convert is already a delta table

performConvert Method

performConvert(
  spark: SparkSession,
  txn: OptimisticTransaction,
  convertProperties: ConvertProperties): Seq[Row]

performConvert makes sure that the directory exists (from the given ConvertProperties which is the table part of the TableIdentifier of the command).

performConvert requests the OptimisticTransaction for the DeltaLog that is then requested to ensureLogDirectoryExist.

performConvert creates a Dataset to recursively list directories and files in the directory and leaves only files (by filtering out directories using WHERE clause).

performConvert uses Dataset API to build a distributed computation to query files.

performConvert caches the Dataset of file names.

performConvert uses spark.databricks.delta.import.batchSize.schemaInference configuration property for the number of files per batch for schema inference. performConvert mergeSchemasInParallel for every batch of files and then mergeSchemas.

performConvert constructTableSchema using the inferred table schema and the partitionSchema (if specified).

performConvert creates a new Metadata using the table schema and the partitionSchema (if specified).

performConvert requests the OptimisticTransaction to update the metadata.

performConvert uses spark.databricks.delta.import.batchSize.statsCollection configuration property for the number of files per batch for stats collection. performConvert creates an AddFile (in the data path of the DeltaLog of the OptimisticTransaction) for every file in a batch.

In the end, performConvert streamWrite (with the OptimisticTransaction, the AddFiles, and Convert operation) and unpersists the Dataset of file names.

performConvert is used exclusively when ConvertToDeltaCommand is requested to run.

streamWrite Method

streamWrite(
  spark: SparkSession,
  txn: OptimisticTransaction,
  addFiles: Iterator[AddFile],
  op: DeltaOperations.Convert): Long

streamWrite…​FIXME

streamWrite is used exclusively when ConvertToDeltaCommand is requested to performConvert.

createAddFile Method

createAddFile(
  file: SerializableFileStatus,
  basePath: Path,
  fs: FileSystem,
  resolver: Resolver): AddFile

createAddFile creates an AddFile action.

Internally, createAddFile…​FIXME

createAddFile throws an AnalysisException if the number of fields in the given partition schema does not match the number of partitions found (at partition discovery phase):

Expecting [size] partition column(s): [expectedCols], but found [size] partition column(s): [parsedCols] from parsing the file name: [path]
createAddFile is used exclusively when ConvertToDeltaCommand is requested to performConvert (for every data file to import).

mergeSchemasInParallel Method

mergeSchemasInParallel(
  sparkSession: SparkSession,
  filesToTouch: Seq[FileStatus]): Option[StructType]

mergeSchemasInParallel…​FIXME

mergeSchemasInParallel is used exclusively when ConvertToDeltaCommand is requested to performConvert.

constructTableSchema Method

constructTableSchema(
  spark: SparkSession,
  dataSchema: StructType,
  partitionFields: Seq[StructField]): StructType

constructTableSchema…​FIXME

constructTableSchema is used exclusively when ConvertToDeltaCommand is requested to performConvert.

Creating ConvertProperties from TableIdentifier — getConvertProperties Method

getConvertProperties(
  spark: SparkSession,
  tableIdentifier: TableIdentifier): ConvertProperties

getConvertProperties simply creates a new ConvertProperties with the following:

  • Undefined CatalogTable (None)

  • Provider name as the database of the TableIdentifier

  • Target directory as the table of the TableIdentifier

  • No properties

getConvertProperties is used exclusively when ConvertToDeltaCommand is requested to run.