|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.gluten.delta |
| 18 | + |
| 19 | +import org.apache.gluten.sql.shims.SparkShimLoader |
| 20 | + |
| 21 | +import org.apache.spark.sql.SparkSession |
| 22 | +import org.apache.spark.sql.delta.GlutenDeltaParquetFileFormat |
| 23 | +import org.apache.spark.sql.delta.actions.DeletionVectorDescriptor |
| 24 | +import org.apache.spark.sql.delta.deletionvectors.{RoaringBitmapArrayFormat, StoredBitmap} |
| 25 | +import org.apache.spark.sql.delta.storage.dv.HadoopFileSystemDVStore |
| 26 | +import org.apache.spark.sql.execution.datasources.PartitionedFile |
| 27 | + |
| 28 | +import org.apache.hadoop.fs.Path |
| 29 | + |
| 30 | +import java.util.{ArrayList => JArrayList} |
| 31 | + |
| 32 | +import scala.collection.JavaConverters._ |
| 33 | +import scala.util.control.NonFatal |
| 34 | + |
| 35 | +object DeltaDeletionVectorScanInfo { |
| 36 | + object RowIndexFilterType extends Enumeration { |
| 37 | + type RowIndexFilterType = Value |
| 38 | + val KEEP_ALL, IF_CONTAINED, IF_NOT_CONTAINED = Value |
| 39 | + } |
| 40 | + |
| 41 | + import RowIndexFilterType._ |
| 42 | + |
| 43 | + final case class DeletionVectorInfo( |
| 44 | + hasDeletionVector: Boolean, |
| 45 | + rowIndexFilterType: RowIndexFilterType, |
| 46 | + cardinality: Long, |
| 47 | + serializedDeletionVector: Array[Byte]) |
| 48 | + |
| 49 | + final case class PartitionFileScanInfo( |
| 50 | + normalizedOtherMetadataColumns: Map[String, Object], |
| 51 | + deletionVectorInfo: DeletionVectorInfo) |
| 52 | + |
| 53 | + private val RowIndexFilterIdEncoded = |
| 54 | + GlutenDeltaParquetFileFormat.FILE_ROW_INDEX_FILTER_ID_ENCODED |
| 55 | + private val RowIndexFilterTypeKey = |
| 56 | + GlutenDeltaParquetFileFormat.FILE_ROW_INDEX_FILTER_TYPE |
| 57 | + |
| 58 | + def extract( |
| 59 | + spark: SparkSession, |
| 60 | + partitionColumnCount: Int, |
| 61 | + file: PartitionedFile): PartitionFileScanInfo = { |
| 62 | + val metadata = otherMetadataColumns(file) |
| 63 | + val normalizedMetadata = metadata -- Seq(RowIndexFilterIdEncoded, RowIndexFilterTypeKey) |
| 64 | + val dvInfo = extractDeletionVectorInfo(spark, partitionColumnCount, file, metadata) |
| 65 | + PartitionFileScanInfo(normalizedMetadata, dvInfo) |
| 66 | + } |
| 67 | + |
| 68 | + def extractAll( |
| 69 | + spark: SparkSession, |
| 70 | + partitionColumnCount: Int, |
| 71 | + files: Seq[PartitionedFile]): Seq[PartitionFileScanInfo] = { |
| 72 | + files.map(extract(spark, partitionColumnCount, _)) |
| 73 | + } |
| 74 | + |
| 75 | + def extractAllFromJava( |
| 76 | + spark: SparkSession, |
| 77 | + partitionColumnCount: Int, |
| 78 | + files: java.util.List[PartitionedFile]): java.util.List[PartitionFileScanInfo] = { |
| 79 | + new JArrayList(extractAll(spark, partitionColumnCount, files.asScala.toSeq).asJava) |
| 80 | + } |
| 81 | + |
| 82 | + private def extractDeletionVectorInfo( |
| 83 | + spark: SparkSession, |
| 84 | + partitionColumnCount: Int, |
| 85 | + file: PartitionedFile, |
| 86 | + metadata: Map[String, Object]): DeletionVectorInfo = { |
| 87 | + val descriptorValue = metadata.get(RowIndexFilterIdEncoded) |
| 88 | + val filterTypeValue = metadata.get(RowIndexFilterTypeKey) |
| 89 | + |
| 90 | + (descriptorValue, filterTypeValue) match { |
| 91 | + case (None, None) => |
| 92 | + DeletionVectorInfo(false, KEEP_ALL, 0L, Array.emptyByteArray) |
| 93 | + case (Some(encodedDescriptor), Some(filterType)) => |
| 94 | + val descriptor = parseDescriptor(encodedDescriptor.toString) |
| 95 | + val serializedPayload = serializePayload(spark, partitionColumnCount, file, descriptor) |
| 96 | + DeletionVectorInfo( |
| 97 | + true, |
| 98 | + parseRowIndexFilterType(filterType.toString), |
| 99 | + descriptor.cardinality, |
| 100 | + serializedPayload) |
| 101 | + case _ => |
| 102 | + throw new IllegalStateException( |
| 103 | + s"Both $RowIndexFilterIdEncoded and $RowIndexFilterTypeKey must either be present or absent") |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private def otherMetadataColumns(file: PartitionedFile): Map[String, Object] = { |
| 108 | + val otherMetadata = |
| 109 | + SparkShimLoader.getSparkShims.getOtherConstantMetadataColumnValues(file) |
| 110 | + if (otherMetadata == null) { |
| 111 | + Map.empty |
| 112 | + } else { |
| 113 | + otherMetadata.asScala.toMap |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private def parseDescriptor(encodedDescriptor: String): DeletionVectorDescriptor = { |
| 118 | + try { |
| 119 | + DeletionVectorDescriptor.deserializeFromBase64(encodedDescriptor) |
| 120 | + } catch { |
| 121 | + case NonFatal(e) => |
| 122 | + throw new IllegalArgumentException("Unable to parse Delta deletion vector descriptor", e) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private def parseRowIndexFilterType(filterType: String): RowIndexFilterType = { |
| 127 | + filterType match { |
| 128 | + case "IF_CONTAINED" => IF_CONTAINED |
| 129 | + case "IF_NOT_CONTAINED" => IF_NOT_CONTAINED |
| 130 | + case "KEEP_ALL" => KEEP_ALL |
| 131 | + case unexpected => |
| 132 | + throw new IllegalStateException(s"Unexpected row index filter type: $unexpected") |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private def serializePayload( |
| 137 | + spark: SparkSession, |
| 138 | + partitionColumnCount: Int, |
| 139 | + file: PartitionedFile, |
| 140 | + descriptor: DeletionVectorDescriptor): Array[Byte] = { |
| 141 | + val tablePath = resolveTablePath(spark, partitionColumnCount, file) |
| 142 | + if (tablePath == null) { |
| 143 | + throw new IllegalStateException( |
| 144 | + "Unable to resolve Delta table path while materializing deletion vector payload") |
| 145 | + } |
| 146 | + val dvStore = new HadoopFileSystemDVStore(spark.sessionState.newHadoopConf()) |
| 147 | + StoredBitmap |
| 148 | + .create(descriptor, tablePath) |
| 149 | + .load(dvStore) |
| 150 | + .serializeAsByteArray(RoaringBitmapArrayFormat.Portable) |
| 151 | + } |
| 152 | + |
| 153 | + private def resolveTablePath( |
| 154 | + spark: SparkSession, |
| 155 | + partitionColumnCount: Int, |
| 156 | + file: PartitionedFile): Path = { |
| 157 | + val fileParent = new Path(unescapePathName(file.filePath.toString)).getParent |
| 158 | + var tablePath = fileParent |
| 159 | + for (_ <- 0 until partitionColumnCount) { |
| 160 | + tablePath = tablePath.getParent |
| 161 | + } |
| 162 | + if (tablePath != null && isDeltaTablePath(spark, tablePath)) { |
| 163 | + return tablePath |
| 164 | + } |
| 165 | + |
| 166 | + var candidate = fileParent |
| 167 | + while (candidate != null && !isDeltaTablePath(spark, candidate)) { |
| 168 | + candidate = candidate.getParent |
| 169 | + } |
| 170 | + if (candidate != null) candidate else tablePath |
| 171 | + } |
| 172 | + |
| 173 | + private def isDeltaTablePath(spark: SparkSession, tablePath: Path): Boolean = { |
| 174 | + val deltaLogPath = new Path(tablePath, "_delta_log") |
| 175 | + try { |
| 176 | + deltaLogPath.getFileSystem(spark.sessionState.newHadoopConf()).exists(deltaLogPath) |
| 177 | + } catch { |
| 178 | + case NonFatal(_) => false |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + private def unescapePathName(path: String): String = { |
| 183 | + if (path == null || path.indexOf('%') < 0) { |
| 184 | + path |
| 185 | + } else { |
| 186 | + val builder = new StringBuilder(path.length) |
| 187 | + var index = 0 |
| 188 | + while (index < path.length) { |
| 189 | + if (path.charAt(index) == '%' && index + 2 < path.length) { |
| 190 | + val high = Character.digit(path.charAt(index + 1), 16) |
| 191 | + val low = Character.digit(path.charAt(index + 2), 16) |
| 192 | + if (high >= 0 && low >= 0) { |
| 193 | + builder.append(((high << 4) | low).toChar) |
| 194 | + index += 3 |
| 195 | + } else { |
| 196 | + builder.append(path.charAt(index)) |
| 197 | + index += 1 |
| 198 | + } |
| 199 | + } else { |
| 200 | + builder.append(path.charAt(index)) |
| 201 | + index += 1 |
| 202 | + } |
| 203 | + } |
| 204 | + builder.toString() |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments