|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.comet.rules |
| 21 | + |
| 22 | +import org.apache.spark.internal.Logging |
| 23 | +import org.apache.spark.sql.SparkSession |
| 24 | +import org.apache.spark.sql.catalyst.InternalRow |
| 25 | +import org.apache.spark.sql.catalyst.rules.Rule |
| 26 | +import org.apache.spark.sql.comet.{CometNativeCompaction, CometNativeCompactionExec} |
| 27 | +import org.apache.spark.sql.connector.catalog.{Identifier, TableCatalog} |
| 28 | +import org.apache.spark.sql.execution.SparkPlan |
| 29 | + |
| 30 | +import org.apache.comet.CometConf |
| 31 | + |
| 32 | +/** |
| 33 | + * Replaces Iceberg's CallExec targeting RewriteDataFilesProcedure with native Comet compaction. |
| 34 | + * |
| 35 | + * Uses reflection to detect CallExec (from Iceberg extensions) to avoid hard compile-time |
| 36 | + * dependency. Only active when spark.comet.iceberg.compaction.enabled is true and native |
| 37 | + * compaction is available. Currently supports Spark 3.x only (Spark 4.0 uses InvokeProcedures at |
| 38 | + * the analysis phase, handled separately via shim). |
| 39 | + */ |
| 40 | +case class CometCompactionRule(session: SparkSession) extends Rule[SparkPlan] with Logging { |
| 41 | + |
| 42 | + private val CALL_EXEC_CLASS = "org.apache.spark.sql.execution.datasources.v2.CallExec" |
| 43 | + private val REWRITE_PROCEDURE_NAME = "RewriteDataFilesProcedure" |
| 44 | + |
| 45 | + override def apply(plan: SparkPlan): SparkPlan = { |
| 46 | + if (!isEnabled) return plan |
| 47 | + |
| 48 | + plan.transformUp { |
| 49 | + case exec if isRewriteCallExec(exec) => |
| 50 | + replaceWithNative(exec).getOrElse(exec) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private def isEnabled: Boolean = |
| 55 | + CometConf.COMET_ICEBERG_COMPACTION_ENABLED.get(session.sessionState.conf) && |
| 56 | + CometNativeCompaction.isAvailable |
| 57 | + |
| 58 | + private def isRewriteCallExec(plan: SparkPlan): Boolean = { |
| 59 | + plan.getClass.getName == CALL_EXEC_CLASS && { |
| 60 | + try { |
| 61 | + val proc = plan.getClass.getMethod("procedure").invoke(plan) |
| 62 | + proc.getClass.getSimpleName == REWRITE_PROCEDURE_NAME |
| 63 | + } catch { case _: Exception => false } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private def replaceWithNative(exec: SparkPlan): Option[SparkPlan] = { |
| 68 | + try { |
| 69 | + val proc = exec.getClass.getMethod("procedure").invoke(exec) |
| 70 | + val input = exec.getClass.getMethod("input").invoke(exec).asInstanceOf[InternalRow] |
| 71 | + |
| 72 | + // Only intercept bin-pack strategy (default when strategy is null) |
| 73 | + if (!input.isNullAt(1)) { |
| 74 | + val strategy = input.getUTF8String(1).toString |
| 75 | + if (!strategy.equalsIgnoreCase("binpack")) { |
| 76 | + logInfo(s"Native compaction skipped: unsupported strategy '$strategy'") |
| 77 | + return None |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + val tableCatalog = extractTableCatalog(proc) |
| 82 | + val tableIdent = parseIdentifier(input.getUTF8String(0).toString) |
| 83 | + |
| 84 | + logInfo(s"Replacing CallExec with CometNativeCompactionExec for $tableIdent") |
| 85 | + Some(CometNativeCompactionExec(exec.output, tableCatalog, tableIdent)) |
| 86 | + } catch { |
| 87 | + case e: Exception => |
| 88 | + logWarning(s"Cannot replace with native compaction: ${e.getMessage}") |
| 89 | + None |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** Extract TableCatalog from BaseProcedure via reflection (field is private). */ |
| 94 | + private def extractTableCatalog(procedure: Any): TableCatalog = { |
| 95 | + val field = procedure.getClass.getSuperclass.getDeclaredField("tableCatalog") |
| 96 | + field.setAccessible(true) |
| 97 | + field.get(procedure).asInstanceOf[TableCatalog] |
| 98 | + } |
| 99 | + |
| 100 | + private def parseIdentifier(identStr: String): Identifier = { |
| 101 | + val parts = identStr.split("\\.") |
| 102 | + Identifier.of(parts.dropRight(1), parts.last) |
| 103 | + } |
| 104 | +} |
0 commit comments