Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#781] Add support for Spark DataFrameWriter maxRecordsPerFile option #1017

Closed
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.apache.spark.SparkException
import org.apache.spark.sql.{Dataset, SparkSession}
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.logical.LocalRelation
import org.apache.spark.sql.catalyst.util.DateTimeUtils
import org.apache.spark.sql.execution._
import org.apache.spark.sql.execution.datasources.{BasicWriteJobStatsTracker, FileFormatWriter, WriteJobStatsTracker}
import org.apache.spark.sql.functions.to_json
Expand Down Expand Up @@ -201,9 +202,8 @@ trait TransactionalWrite extends DeltaLogging { self: OptimisticTransactionImpl

def writeFiles(
data: Dataset[_],
writeOptions: Option[DeltaOptions],
additionalConstraints: Seq[Constraint]): Seq[FileAction] = {
writeFiles(data, additionalConstraints)
writeFiles(data, None, additionalConstraints)
}

def writeFiles(
Expand All @@ -222,6 +222,7 @@ trait TransactionalWrite extends DeltaLogging { self: OptimisticTransactionImpl
*/
def writeFiles(
data: Dataset[_],
writeOptions: Option[DeltaOptions],
additionalConstraints: Seq[Constraint]): Seq[FileAction] = {
if (DeltaConfigs.CHANGE_DATA_FEED.fromMetaData(metadata)) {
throw DeltaErrors.cdcWriteNotAllowedInThisVersion()
Expand Down Expand Up @@ -295,6 +296,16 @@ trait TransactionalWrite extends DeltaLogging { self: OptimisticTransactionImpl
statsTrackers.append(basicWriteJobStatsTracker)
}

// Retain only known Spark writer options to avoid any potential compatibility issues
val options = writeOptions match {
case None => Map.empty[String, String]
case Some(writeOptions) =>
writeOptions.options.filterKeys(key =>
key.equalsIgnoreCase("maxRecordsPerFile") ||
key.equalsIgnoreCase(DateTimeUtils.TIMEZONE_OPTION)
zsxwing marked this conversation as resolved.
Show resolved Hide resolved
).toMap
}

try {
FileFormatWriter.write(
sparkSession = spark,
Expand All @@ -309,7 +320,7 @@ trait TransactionalWrite extends DeltaLogging { self: OptimisticTransactionImpl
partitionColumns = partitioningColumns,
bucketSpec = None,
statsTrackers = optionalStatsTracker.toSeq ++ statsTrackers,
options = Map.empty)
options = options)
} catch {
case s: SparkException =>
// Pull an InvariantViolationException up to the top level if it was the root cause.
Expand Down