|
19 | 19 |
|
20 | 20 | package org.apache.comet.serde |
21 | 21 |
|
| 22 | +import org.apache.spark.api.java.function.FilterFunction |
22 | 23 | import org.apache.spark.sql.catalyst.expressions.{ApplyFunctionExpression, Attribute, Base64, Expression, ExpressionImplUtils, Literal, StringDecode, TryEval, UrlCodec} |
23 | 24 | import org.apache.spark.sql.catalyst.expressions.objects.{Invoke, StaticInvoke} |
24 | 25 | import org.apache.spark.sql.catalyst.util.CharVarcharCodegenUtils |
25 | | -import org.apache.spark.sql.types.StringType |
| 26 | +import org.apache.spark.sql.connector.catalog.functions.ScalarFunction |
| 27 | +import org.apache.spark.sql.types.{BooleanType, ObjectType, StringType} |
26 | 28 |
|
| 29 | +import org.apache.comet.CometExplainInfo |
27 | 30 | import org.apache.comet.CometSparkSessionExtensions.withFallbackReason |
28 | 31 | import org.apache.comet.serde.QueryPlanSerde.{exprToProtoInternal, scalarFunctionExprToProto, scalarFunctionExprToProtoWithReturnType} |
29 | 32 |
|
@@ -84,7 +87,8 @@ object CometStaticInvoke extends CometExpressionSerde[StaticInvoke] { |
84 | 87 | * Arrow `Decimal128(precision, scale)` vector just like a native kernel does, so it produces |
85 | 88 | * the out-of-range value instead of a null. The mixin's contract ("the case must be something |
86 | 89 | * `doGenCode` can compile") does not cover a limit that lives at the Arrow output boundary, so |
87 | | - * enrollment stays with the individual handlers. |
| 90 | + * enrollment stays with the individual handlers. [[CometInvokeTargets]] now keeps Iceberg's |
| 91 | + * classes out of the dispatcher as well. |
88 | 92 | */ |
89 | 93 | override def getSupportLevel(expr: StaticInvoke): SupportLevel = |
90 | 94 | handlerFor(expr) |
@@ -119,7 +123,9 @@ object CometStaticInvoke extends CometExpressionSerde[StaticInvoke] { |
119 | 123 | // `StaticInvoke.doGenCode` emits a static method call, so the kernel matches Spark by |
120 | 124 | // construction. Spark 4.x keeps lowering more `RuntimeReplaceable` functions this way |
121 | 125 | // (`encode`, `is_valid_utf8`, the `TIME` family, ...) and `lpad` / `rpad` on binary |
122 | | - // has lowered to `StaticInvoke(ByteArray, ...)` since Spark 3.4. |
| 126 | + // has lowered to `StaticInvoke(ByteArray, ...)` since Spark 3.4. The dispatcher |
| 127 | + // declines a call into any class that is not Spark's, such as a DataSource V2 |
| 128 | + // function, so that falls back (see [[CometInvokeTargets]]). |
123 | 129 | // |
124 | 130 | // The encoder and deserializer trees that make up most `StaticInvoke` usage in typed |
125 | 131 | // Dataset operations are unaffected: their arguments are `ObjectType`, which |
@@ -167,17 +173,92 @@ object CometApplyFunctionExpression extends CometExpressionSerde[ApplyFunctionEx |
167 | 173 | } |
168 | 174 |
|
169 | 175 | /** |
170 | | - * Catch-all for `Invoke`, which has no allowlist at all. Spark 4.x lowers a growing number of |
| 176 | + * Catch-all for `Invoke`, which has no allowlist of its own. Spark 4.x lowers a growing number of |
171 | 177 | * `RuntimeReplaceable` expressions to evaluator-backed `Invoke` nodes; the Spark 4.x shim |
172 | 178 | * reconstructs the handful it recognizes and everything else lands here. `Invoke.doGenCode` emits |
173 | 179 | * a method call on the target object, so the dispatcher runs Spark's own implementation inside |
174 | 180 | * the Comet pipeline rather than failing the operator back to Spark. |
175 | 181 | * |
| 182 | + * A DataSource V2 function with an instance `invoke` method lowers to `Invoke` too. The |
| 183 | + * dispatcher declines that, and any other call into a class that is not Spark's, so the operator |
| 184 | + * falls back (see [[CometInvokeTargets]]). |
| 185 | + * |
176 | 186 | * As with [[CometStaticInvoke]], the object-typed `Invoke` nodes in encoder / deserializer trees |
177 | 187 | * are rejected by `CometBatchKernelCodegen.canHandle` and fall back as before. |
178 | 188 | */ |
179 | 189 | object CometInvoke extends CometCodegenDispatch[Invoke] |
180 | 190 |
|
| 191 | +/** |
| 192 | + * The classes the codegen dispatcher may call into from a `StaticInvoke` or an `Invoke`: Spark's |
| 193 | + * own, other than a DataSource V2 function, plus the predicate of a typed `Dataset.filter`. |
| 194 | + * [[CometScalaUDF.emitJvmCodegenDispatch]] checks every node of a dispatched tree. |
| 195 | + * |
| 196 | + * Spark's helpers return values that already have their declared type. Other code need not, and |
| 197 | + * Spark corrects such a value only when it writes a row. A DataSource V2 function can return a |
| 198 | + * `Decimal` at a different scale from the type it declares, or one that does not fit it: Spark |
| 199 | + * rescales it, or writes null, when it writes the row, and an expression around the call reads |
| 200 | + * the value as returned (#6425). The kernel has to write an Arrow vector of the declared type, so |
| 201 | + * a native consumer of its output would read something else, and where Spark writes rows depends |
| 202 | + * on whole-stage codegen, so Comet cannot reproduce it. Any other call, including every |
| 203 | + * `ApplyFunctionExpression`, makes the operator fall back to Spark instead, as it did before |
| 204 | + * #5692 routed these calls through the dispatcher. |
| 205 | + */ |
| 206 | +object CometInvokeTargets { |
| 207 | + |
| 208 | + /** The packages of Spark's expression helpers and evaluators, and of its internal values. */ |
| 209 | + private val SparkPackages = Seq("org.apache.spark.sql.catalyst.", "org.apache.spark.unsafe.") |
| 210 | + |
| 211 | + /** |
| 212 | + * The types Spark's `TypedFilter` calls the predicate of a typed `Dataset.filter` through. The |
| 213 | + * predicate is user code, but it returns a boolean, which a row write has nothing to correct |
| 214 | + * in. |
| 215 | + */ |
| 216 | + private val TypedFilterPredicates: Set[Class[_]] = |
| 217 | + Set(classOf[scala.Function1[_, _]], classOf[FilterFunction[_]]) |
| 218 | + |
| 219 | + /** |
| 220 | + * Why the dispatcher must not run `expr`, naming the first call in it into a class the |
| 221 | + * dispatcher does not allow, or `None` when it allows them all. |
| 222 | + */ |
| 223 | + def declineReason(expr: Expression): Option[String] = |
| 224 | + expr.find(checkedCallee(_).exists(!isSparkCode(_))).map { node => |
| 225 | + val cls = checkedCallee(node).get |
| 226 | + val target = |
| 227 | + if (isV2Function(cls)) { |
| 228 | + s"the DataSource V2 function ${cls.getName}" |
| 229 | + } else { |
| 230 | + s"${cls.getName}, which is not part of Spark" |
| 231 | + } |
| 232 | + s"${CometExplainInfo.exprDisplayName(node)} calls $target" |
| 233 | + } |
| 234 | + |
| 235 | + private def isV2Function(cls: Class[_]): Boolean = |
| 236 | + classOf[ScalarFunction[_]].isAssignableFrom(cls) |
| 237 | + |
| 238 | + private def isSparkCode(cls: Class[_]): Boolean = |
| 239 | + !isV2Function(cls) && SparkPackages.exists(p => cls.getName.startsWith(p)) |
| 240 | + |
| 241 | + /** |
| 242 | + * The class whose code `expr` calls, unless it calls a method of a Catalyst value or is the |
| 243 | + * predicate of a typed filter. |
| 244 | + */ |
| 245 | + private def checkedCallee(expr: Expression): Option[Class[_]] = expr match { |
| 246 | + case s: StaticInvoke => Some(s.staticObject) |
| 247 | + case i: Invoke => |
| 248 | + i.targetObject.dataType match { |
| 249 | + case ObjectType(cls) |
| 250 | + if TypedFilterPredicates.contains(cls) && i.dataType == BooleanType => |
| 251 | + None |
| 252 | + case ObjectType(cls) => Some(cls) |
| 253 | + // Any other target is a Catalyst value, such as the `UTF8String` that Spark 4 lowers |
| 254 | + // `is_valid_utf8` to call `isValid` on. |
| 255 | + case _ => None |
| 256 | + } |
| 257 | + case a: ApplyFunctionExpression => Some(a.function.getClass) |
| 258 | + case _ => None |
| 259 | + } |
| 260 | +} |
| 261 | + |
181 | 262 | object CometUrlEncodeStaticInvoke extends CometExpressionSerde[StaticInvoke] { |
182 | 263 | override def convert( |
183 | 264 | expr: StaticInvoke, |
|
0 commit comments