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
Show file tree
Hide file tree
Changes from all 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 @@ -201,9 +201,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 +221,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 +295,16 @@ trait TransactionalWrite extends DeltaLogging { self: OptimisticTransactionImpl
statsTrackers.append(basicWriteJobStatsTracker)
}

// Retain only a minimal selection of 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")
).toMap
}

try {
FileFormatWriter.write(
sparkSession = spark,
Expand All @@ -309,7 +319,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
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
package org.apache.spark.sql.delta

import org.apache.spark.sql.delta.actions.{Action, FileAction}
import org.apache.spark.sql.delta.test.DeltaSQLCommandTest
import org.apache.spark.sql.delta.util.FileNames

import org.apache.commons.io.FileUtils

import org.apache.spark.sql.{AnalysisException, QueryTest}
import org.apache.spark.sql.test.SharedSparkSession
import org.apache.spark.util.Utils

class DeltaOptionSuite extends QueryTest
with SharedSparkSession {
with SharedSparkSession with DeltaSQLCommandTest {

import testImplicits._

Expand Down Expand Up @@ -131,4 +134,47 @@ class DeltaOptionSuite extends QueryTest
}
}

test("support the maxRecordsPerFile write option: path") {
withTempDir { tempDir =>
val path = tempDir.getCanonicalPath
withTable("maxRecordsPerFile") {
spark.range(100)
.write
.format("delta")
.option("maxRecordsPerFile", 5)
.save(path)
assert(FileUtils.listFiles(tempDir, Array("parquet"), false).size === 20)
}
}
}

test("support the maxRecordsPerFile write option: external table") {
withTempDir { tempDir =>
val path = tempDir.getCanonicalPath
withTable("maxRecordsPerFile") {
spark.range(100)
.write
.format("delta")
.option("maxRecordsPerFile", 5)
.option("path", path)
.saveAsTable("maxRecordsPerFile")
assert(FileUtils.listFiles(tempDir, Array("parquet"), false).size === 20)
}
}
}

test("support the maxRecordsPerFile write option: v2 write") {
withTempDir { tempDir =>
val path = tempDir.getCanonicalPath
withTable("maxRecordsPerFile") {
spark.range(100)
.writeTo(s"maxRecordsPerFile")
.using("delta")
.option("maxRecordsPerFile", 5)
.tableProperty("location", path)
.create()
assert(FileUtils.listFiles(tempDir, Array("parquet"), false).size === 20)
}
}
}
}