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

Improve DV path canonicalization #1829

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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 @@ -35,6 +35,7 @@ import org.apache.spark.sql.delta.util.DeltaFileOperations.absolutePath
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.Path

import org.apache.spark.paths.SparkPath
import org.apache.spark.sql.{Column, DataFrame, Dataset, Encoder, SparkSession}
import org.apache.spark.sql.catalyst.expressions.{AttributeReference, Expression, FileSourceMetadataAttribute}
import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, Project}
Expand Down Expand Up @@ -341,11 +342,16 @@ object DeletionVectorBitmapGenerator {
: Seq[DeletionVectorResult] = {
// TODO: fix this to work regardless of whether Spark encodes or doesn't encode
// _metadata.file_path. See https://github.com/delta-io/delta/issues/1725
val uriEncode = DeltaUDF.stringFromString(path => {
new Path(path).toUri.toString
})
// Build two maps, using Path or String as keys. The one with String keys is used in UDF.
val canonicalizedPathMap = buildCanonicalizedPathMap(txn.deltaLog, candidateFiles)
val canonicalizedPathStringMap =
canonicalizedPathMap.map { case (k, v) => (k.toString, v)}.toMap
xupefei marked this conversation as resolved.
Show resolved Hide resolved
val broadcastCanonicalizedPathStringMap =
sparkSession.sparkContext.broadcast(canonicalizedPathStringMap)

val lookupPathUdf = DeltaUDF.stringFromString(broadcastCanonicalizedPathStringMap.value(_))
val matchedRowsDf = targetDf
.withColumn(FILE_NAME_COL, uriEncode(col(s"${METADATA_NAME}.${FILE_PATH}")))
.withColumn(FILE_NAME_COL, lookupPathUdf(col(s"$METADATA_NAME.$FILE_PATH")))
xupefei marked this conversation as resolved.
Show resolved Hide resolved
// Filter after getting input file name as the filter might introduce a join and we
// cannot get input file name on join's output.
.filter(new Column(condition))
Expand All @@ -358,7 +364,7 @@ object DeletionVectorBitmapGenerator {
val filePathToDV = candidateFiles.map { add =>
val serializedDV = Option(add.deletionVector).map(dvd => JsonUtils.toJson(dvd))
// Paths in the metadata column are canonicalized. Thus we must canonicalize the DV path.
FileToDvDescriptor(absolutePath(basePath, add.path).toUri.toString, serializedDV)
FileToDvDescriptor(canonicalizedPathMap(absolutePath(basePath, add.path)), serializedDV)
}
val filePathToDVDf = sparkSession.createDataset(filePathToDV)

Expand All @@ -379,6 +385,16 @@ object DeletionVectorBitmapGenerator {

DeletionVectorBitmapGenerator.buildDeletionVectors(sparkSession, df, txn.deltaLog, txn)
}

private def buildCanonicalizedPathMap(
log: DeltaLog,
addFiles: Seq[AddFile]): Map[Path, String] = {
val basePath = log.dataPath.toString
addFiles.map { add =>
val absPath = absolutePath(basePath, add.path)
absPath -> SparkPath.fromPath(absPath).urlEncoded
}.toMap
}
}

/**
Expand Down