|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.paimon.spark.commands |
| 20 | + |
| 21 | +import org.apache.spark.sql.catalyst.expressions.Expression |
| 22 | +import org.apache.spark.sql.catalyst.plans.logical.MergeRows.{Instruction, Keep} |
| 23 | + |
| 24 | +import scala.util.Try |
| 25 | + |
| 26 | +/** |
| 27 | + * Helper object for creating MergeRows.Keep instructions compatible with different Spark versions. |
| 28 | + * |
| 29 | + * Different Spark versions have different Keep signatures: |
| 30 | + * - Spark 4.0: Keep(condition, output) |
| 31 | + * - Spark 4.1+: Keep(context, condition, output) |
| 32 | + * |
| 33 | + * This object uses reflection to determine which signature to use. |
| 34 | + */ |
| 35 | +object KeepHelper { |
| 36 | + |
| 37 | + // Check if Keep has 3-parameter constructor (Spark 4.1+) |
| 38 | + private lazy val keepHas3Params: Boolean = { |
| 39 | + Try { |
| 40 | + classOf[Keep].getConstructors.exists(_.getParameterCount == 3) |
| 41 | + }.getOrElse(false) |
| 42 | + } |
| 43 | + |
| 44 | + // Context objects for Spark 4.1+ |
| 45 | + private lazy val updateContext: Option[AnyRef] = getContextObject("Update") |
| 46 | + private lazy val copyContext: Option[AnyRef] = getContextObject("Copy") |
| 47 | + private lazy val insertContext: Option[AnyRef] = getContextObject("Insert") |
| 48 | + |
| 49 | + private def getContextObject(name: String): Option[AnyRef] = { |
| 50 | + Try { |
| 51 | + val contextClass = |
| 52 | + Class.forName(s"org.apache.spark.sql.catalyst.plans.logical.MergeRows$$$name$$") |
| 53 | + contextClass.getField("MODULE$").get(null).asInstanceOf[AnyRef] |
| 54 | + }.toOption |
| 55 | + } |
| 56 | + |
| 57 | + private def createKeepWithContext( |
| 58 | + context: AnyRef, |
| 59 | + condition: Expression, |
| 60 | + output: Seq[Expression]): Instruction = { |
| 61 | + val constructor = classOf[Keep].getConstructors.find(_.getParameterCount == 3).get |
| 62 | + constructor.newInstance(context, condition, output).asInstanceOf[Instruction] |
| 63 | + } |
| 64 | + |
| 65 | + /** Creates a Keep instruction for Update context (matched rows being updated). */ |
| 66 | + def createKeepForUpdate(condition: Expression, output: Seq[Expression]): Instruction = { |
| 67 | + if (keepHas3Params && updateContext.isDefined) { |
| 68 | + createKeepWithContext(updateContext.get, condition, output) |
| 69 | + } else { |
| 70 | + Keep(condition, output) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** Creates a Keep instruction for Copy context (matched rows being copied as-is). */ |
| 75 | + def createKeepForCopy(condition: Expression, output: Seq[Expression]): Instruction = { |
| 76 | + if (keepHas3Params && copyContext.isDefined) { |
| 77 | + createKeepWithContext(copyContext.get, condition, output) |
| 78 | + } else { |
| 79 | + Keep(condition, output) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** Creates a Keep instruction for Insert context (not matched rows being inserted). */ |
| 84 | + def createKeepForInsert(condition: Expression, output: Seq[Expression]): Instruction = { |
| 85 | + if (keepHas3Params && insertContext.isDefined) { |
| 86 | + createKeepWithContext(insertContext.get, condition, output) |
| 87 | + } else { |
| 88 | + Keep(condition, output) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments