|
| 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.backendsapi.velox |
| 18 | + |
| 19 | +import org.apache.gluten.substrait.rel.DeltaLocalFilesNode.{DeltaFileReadOptions, RowIndexFilterType} |
| 20 | + |
| 21 | +import org.apache.spark.sql.SparkSession |
| 22 | +import org.apache.spark.sql.execution.datasources.PartitionedFile |
| 23 | + |
| 24 | +import java.lang.{Long => JLong} |
| 25 | +import java.lang.reflect.Method |
| 26 | +import java.util.concurrent.ConcurrentHashMap |
| 27 | + |
| 28 | +import scala.collection.JavaConverters._ |
| 29 | + |
| 30 | +private[velox] object DeltaSplitMetadataExtractor { |
| 31 | + type NormalizedDeltaSplitMetadata = |
| 32 | + (Seq[java.util.Map[String, Object]], Seq[DeltaFileReadOptions]) |
| 33 | + |
| 34 | + private val DeltaScanInfoClassName = "org.apache.gluten.delta.DeltaDeletionVectorScanInfo$" |
| 35 | + private val scanInfoMethodsCache = new ConcurrentHashMap[Class[_], ScanInfoMethods]() |
| 36 | + private val deletionVectorInfoMethodsCache = |
| 37 | + new ConcurrentHashMap[Class[_], DeletionVectorInfoMethods]() |
| 38 | + |
| 39 | + private lazy val deltaScanInfoReflection: Option[DeltaScanInfoReflection] = { |
| 40 | + try { |
| 41 | + // DeltaDeletionVectorScanInfo is compiled only in Delta-enabled source profiles. Keep this |
| 42 | + // bridge isolated and cached; all directly linkable Gluten classes should be referenced |
| 43 | + // without reflection. |
| 44 | + // scalastyle:off classforname |
| 45 | + val moduleClass = Class.forName(DeltaScanInfoClassName) |
| 46 | + // scalastyle:on classforname |
| 47 | + val module = moduleClass.getField("MODULE$").get(null) |
| 48 | + val extractAllMethod = moduleClass.getMethod( |
| 49 | + "extractAllFromJava", |
| 50 | + classOf[SparkSession], |
| 51 | + classOf[Int], |
| 52 | + classOf[java.util.List[_]]) |
| 53 | + Some(DeltaScanInfoReflection(module, extractAllMethod)) |
| 54 | + } catch { |
| 55 | + case _: ClassNotFoundException | _: NoSuchMethodException => |
| 56 | + None |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + def normalize( |
| 61 | + partitionColumnCount: Int, |
| 62 | + partitionFiles: Seq[PartitionedFile]): Option[NormalizedDeltaSplitMetadata] = { |
| 63 | + deltaScanInfoReflection.flatMap { |
| 64 | + reflection => |
| 65 | + val scanInfos = reflection.extractAllFromJava |
| 66 | + .invoke( |
| 67 | + reflection.module, |
| 68 | + activeSparkSession, |
| 69 | + Int.box(partitionColumnCount), |
| 70 | + partitionFiles.asJava) |
| 71 | + .asInstanceOf[java.util.List[_]] |
| 72 | + .asScala |
| 73 | + .toSeq |
| 74 | + val splitMetadata = scanInfos.map(toDeltaSplitMetadata) |
| 75 | + if (splitMetadata.exists(_._2.hasDeletionVector())) { |
| 76 | + Some((splitMetadata.map(_._1), splitMetadata.map(_._2))) |
| 77 | + } else { |
| 78 | + None |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private def toDeltaSplitMetadata( |
| 84 | + scanInfo: Any): (java.util.Map[String, Object], DeltaFileReadOptions) = { |
| 85 | + val scanInfoMethods = methodsForScanInfo(scanInfo.getClass) |
| 86 | + val metadata = scanInfoMethods.normalizedOtherMetadataColumns |
| 87 | + .invoke(scanInfo) |
| 88 | + .asInstanceOf[scala.collection.Map[String, Object]] |
| 89 | + .asJava |
| 90 | + val deletionVectorInfo = scanInfoMethods.deletionVectorInfo.invoke(scanInfo) |
| 91 | + val deletionVectorInfoMethods = methodsForDeletionVectorInfo(deletionVectorInfo.getClass) |
| 92 | + val rowIndexFilterType = deletionVectorInfoMethods.rowIndexFilterType |
| 93 | + .invoke(deletionVectorInfo) |
| 94 | + .toString |
| 95 | + val hasDeletionVector = deletionVectorInfoMethods.hasDeletionVector |
| 96 | + .invoke(deletionVectorInfo) |
| 97 | + .asInstanceOf[Boolean] |
| 98 | + val cardinality = deletionVectorInfoMethods.cardinality |
| 99 | + .invoke(deletionVectorInfo) |
| 100 | + .asInstanceOf[JLong] |
| 101 | + .longValue() |
| 102 | + val serializedDeletionVector = deletionVectorInfoMethods.serializedDeletionVector |
| 103 | + .invoke(deletionVectorInfo) |
| 104 | + .asInstanceOf[Array[Byte]] |
| 105 | + |
| 106 | + ( |
| 107 | + metadata, |
| 108 | + new DeltaFileReadOptions( |
| 109 | + toDeltaRowIndexFilterType(rowIndexFilterType), |
| 110 | + hasDeletionVector, |
| 111 | + cardinality, |
| 112 | + serializedDeletionVector)) |
| 113 | + } |
| 114 | + |
| 115 | + private def toDeltaRowIndexFilterType(rowIndexFilterType: String): RowIndexFilterType = { |
| 116 | + rowIndexFilterType match { |
| 117 | + case "IF_CONTAINED" => RowIndexFilterType.IF_CONTAINED |
| 118 | + case "IF_NOT_CONTAINED" => RowIndexFilterType.IF_NOT_CONTAINED |
| 119 | + case _ => RowIndexFilterType.KEEP_ALL |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private def methodsForScanInfo(scanInfoClass: Class[_]): ScanInfoMethods = { |
| 124 | + val cached = scanInfoMethodsCache.get(scanInfoClass) |
| 125 | + if (cached != null) { |
| 126 | + return cached |
| 127 | + } |
| 128 | + |
| 129 | + val methods = ScanInfoMethods( |
| 130 | + scanInfoClass.getMethod("normalizedOtherMetadataColumns"), |
| 131 | + scanInfoClass.getMethod("deletionVectorInfo")) |
| 132 | + val previous = scanInfoMethodsCache.putIfAbsent(scanInfoClass, methods) |
| 133 | + if (previous != null) previous else methods |
| 134 | + } |
| 135 | + |
| 136 | + private def methodsForDeletionVectorInfo( |
| 137 | + deletionVectorInfoClass: Class[_]): DeletionVectorInfoMethods = { |
| 138 | + val cached = deletionVectorInfoMethodsCache.get(deletionVectorInfoClass) |
| 139 | + if (cached != null) { |
| 140 | + return cached |
| 141 | + } |
| 142 | + |
| 143 | + val methods = DeletionVectorInfoMethods( |
| 144 | + deletionVectorInfoClass.getMethod("rowIndexFilterType"), |
| 145 | + deletionVectorInfoClass.getMethod("hasDeletionVector"), |
| 146 | + deletionVectorInfoClass.getMethod("cardinality"), |
| 147 | + deletionVectorInfoClass.getMethod("serializedDeletionVector") |
| 148 | + ) |
| 149 | + val previous = deletionVectorInfoMethodsCache.putIfAbsent(deletionVectorInfoClass, methods) |
| 150 | + if (previous != null) previous else methods |
| 151 | + } |
| 152 | + |
| 153 | + private def activeSparkSession: SparkSession = { |
| 154 | + SparkSession.getActiveSession |
| 155 | + .orElse(SparkSession.getDefaultSession) |
| 156 | + .getOrElse { |
| 157 | + throw new IllegalStateException( |
| 158 | + "Active SparkSession is required to materialize Delta deletion vectors") |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private case class DeltaScanInfoReflection(module: AnyRef, extractAllFromJava: Method) |
| 163 | + |
| 164 | + private case class ScanInfoMethods( |
| 165 | + normalizedOtherMetadataColumns: Method, |
| 166 | + deletionVectorInfo: Method) |
| 167 | + |
| 168 | + private case class DeletionVectorInfoMethods( |
| 169 | + rowIndexFilterType: Method, |
| 170 | + hasDeletionVector: Method, |
| 171 | + cardinality: Method, |
| 172 | + serializedDeletionVector: Method) |
| 173 | +} |
0 commit comments