|
| 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.shims |
| 21 | + |
| 22 | +import org.apache.spark.sql.catalyst.expressions._ |
| 23 | +import org.apache.spark.sql.catalyst.expressions.objects.StaticInvoke |
| 24 | +import org.apache.spark.sql.internal.SQLConf |
| 25 | +import org.apache.spark.sql.internal.types.StringTypeWithCollation |
| 26 | +import org.apache.spark.sql.types.{BinaryType, BooleanType, DataTypes, StringType} |
| 27 | + |
| 28 | +import org.apache.comet.CometSparkSessionExtensions.withInfo |
| 29 | +import org.apache.comet.expressions.{CometCast, CometEvalMode} |
| 30 | +import org.apache.comet.serde.{CommonStringExprs, Compatible, ExprOuterClass, Incompatible} |
| 31 | +import org.apache.comet.serde.ExprOuterClass.{BinaryOutputStyle, Expr} |
| 32 | +import org.apache.comet.serde.QueryPlanSerde.exprToProtoInternal |
| 33 | + |
| 34 | +/** |
| 35 | + * `CometExprShim` acts as a shim for parsing expressions from different Spark versions. |
| 36 | + */ |
| 37 | +trait CometExprShim extends CommonStringExprs { |
| 38 | + protected def evalMode(c: Cast): CometEvalMode.Value = |
| 39 | + CometEvalModeUtil.fromSparkEvalMode(c.evalMode) |
| 40 | + |
| 41 | + protected def binaryOutputStyle: BinaryOutputStyle = { |
| 42 | + SQLConf.get |
| 43 | + .getConf(SQLConf.BINARY_OUTPUT_STYLE) |
| 44 | + .map(SQLConf.BinaryOutputStyle.withName) match { |
| 45 | + case Some(SQLConf.BinaryOutputStyle.UTF8) => BinaryOutputStyle.UTF8 |
| 46 | + case Some(SQLConf.BinaryOutputStyle.BASIC) => BinaryOutputStyle.BASIC |
| 47 | + case Some(SQLConf.BinaryOutputStyle.BASE64) => BinaryOutputStyle.BASE64 |
| 48 | + case Some(SQLConf.BinaryOutputStyle.HEX) => BinaryOutputStyle.HEX |
| 49 | + case _ => BinaryOutputStyle.HEX_DISCRETE |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + def versionSpecificExprToProtoInternal( |
| 54 | + expr: Expression, |
| 55 | + inputs: Seq[Attribute], |
| 56 | + binding: Boolean): Option[Expr] = { |
| 57 | + expr match { |
| 58 | + case s: StaticInvoke |
| 59 | + if s.staticObject == classOf[StringDecode] && |
| 60 | + s.dataType.isInstanceOf[StringType] && |
| 61 | + s.functionName == "decode" && |
| 62 | + s.arguments.size == 4 && |
| 63 | + s.inputTypes == Seq( |
| 64 | + BinaryType, |
| 65 | + StringTypeWithCollation(supportsTrimCollation = true), |
| 66 | + BooleanType, |
| 67 | + BooleanType) => |
| 68 | + val Seq(bin, charset, _, _) = s.arguments |
| 69 | + stringDecode(expr, charset, bin, inputs, binding) |
| 70 | + |
| 71 | + case expr @ ToPrettyString(child, timeZoneId) => |
| 72 | + val castSupported = CometCast.isSupported( |
| 73 | + child.dataType, |
| 74 | + DataTypes.StringType, |
| 75 | + timeZoneId, |
| 76 | + CometEvalMode.TRY) |
| 77 | + |
| 78 | + val isCastSupported = castSupported match { |
| 79 | + case Compatible(_) => true |
| 80 | + case Incompatible(_) => true |
| 81 | + case _ => false |
| 82 | + } |
| 83 | + |
| 84 | + if (isCastSupported) { |
| 85 | + exprToProtoInternal(child, inputs, binding) match { |
| 86 | + case Some(p) => |
| 87 | + val toPrettyString = ExprOuterClass.ToPrettyString |
| 88 | + .newBuilder() |
| 89 | + .setChild(p) |
| 90 | + .setTimezone(timeZoneId.getOrElse("UTC")) |
| 91 | + .setBinaryOutputStyle(binaryOutputStyle) |
| 92 | + .build() |
| 93 | + Some( |
| 94 | + ExprOuterClass.Expr |
| 95 | + .newBuilder() |
| 96 | + .setToPrettyString(toPrettyString) |
| 97 | + .build()) |
| 98 | + case _ => |
| 99 | + withInfo(expr, child) |
| 100 | + None |
| 101 | + } |
| 102 | + } else { |
| 103 | + None |
| 104 | + } |
| 105 | + |
| 106 | + case _ => None |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +object CometEvalModeUtil { |
| 112 | + def fromSparkEvalMode(evalMode: EvalMode.Value): CometEvalMode.Value = evalMode match { |
| 113 | + case EvalMode.LEGACY => CometEvalMode.LEGACY |
| 114 | + case EvalMode.TRY => CometEvalMode.TRY |
| 115 | + case EvalMode.ANSI => CometEvalMode.ANSI |
| 116 | + } |
| 117 | +} |
0 commit comments