|
| 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.serde |
| 21 | + |
| 22 | +import org.apache.spark.sql.catalyst.expressions.{Attribute, Expression, Murmur3Hash, XxHash64} |
| 23 | +import org.apache.spark.sql.types.{DecimalType, IntegerType, LongType} |
| 24 | + |
| 25 | +import org.apache.comet.CometSparkSessionExtensions.withInfo |
| 26 | +import org.apache.comet.serde.QueryPlanSerde.{exprToProtoInternal, scalarExprToProtoWithReturnType, serializeDataType, supportedDataType} |
| 27 | + |
| 28 | +object CometXxHash64 extends CometExpressionSerde { |
| 29 | + override def convert( |
| 30 | + expr: Expression, |
| 31 | + inputs: Seq[Attribute], |
| 32 | + binding: Boolean): Option[ExprOuterClass.Expr] = { |
| 33 | + if (!HashUtils.isSupportedType(expr)) { |
| 34 | + return None |
| 35 | + } |
| 36 | + val hash = expr.asInstanceOf[XxHash64] |
| 37 | + val exprs = hash.children.map(exprToProtoInternal(_, inputs, binding)) |
| 38 | + val seedBuilder = ExprOuterClass.Literal |
| 39 | + .newBuilder() |
| 40 | + .setDatatype(serializeDataType(LongType).get) |
| 41 | + .setLongVal(hash.seed) |
| 42 | + val seedExpr = Some(ExprOuterClass.Expr.newBuilder().setLiteral(seedBuilder).build()) |
| 43 | + // the seed is put at the end of the arguments |
| 44 | + scalarExprToProtoWithReturnType("xxhash64", LongType, exprs :+ seedExpr: _*) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +object CometMurmur3Hash extends CometExpressionSerde { |
| 49 | + override def convert( |
| 50 | + expr: Expression, |
| 51 | + inputs: Seq[Attribute], |
| 52 | + binding: Boolean): Option[ExprOuterClass.Expr] = { |
| 53 | + if (!HashUtils.isSupportedType(expr)) { |
| 54 | + return None |
| 55 | + } |
| 56 | + val hash = expr.asInstanceOf[Murmur3Hash] |
| 57 | + val exprs = hash.children.map(exprToProtoInternal(_, inputs, binding)) |
| 58 | + val seedBuilder = ExprOuterClass.Literal |
| 59 | + .newBuilder() |
| 60 | + .setDatatype(serializeDataType(IntegerType).get) |
| 61 | + .setIntVal(hash.seed) |
| 62 | + val seedExpr = Some(ExprOuterClass.Expr.newBuilder().setLiteral(seedBuilder).build()) |
| 63 | + // the seed is put at the end of the arguments |
| 64 | + scalarExprToProtoWithReturnType("murmur3_hash", IntegerType, exprs :+ seedExpr: _*) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +private object HashUtils { |
| 69 | + def isSupportedType(expr: Expression): Boolean = { |
| 70 | + for (child <- expr.children) { |
| 71 | + child.dataType match { |
| 72 | + case dt: DecimalType if dt.precision > 18 => |
| 73 | + // Spark converts decimals with precision > 18 into |
| 74 | + // Java BigDecimal before hashing |
| 75 | + withInfo(expr, s"Unsupported datatype: $dt (precision > 18)") |
| 76 | + return false |
| 77 | + case dt if !supportedDataType(dt) => |
| 78 | + withInfo(expr, s"Unsupported datatype $dt") |
| 79 | + return false |
| 80 | + case _ => |
| 81 | + } |
| 82 | + } |
| 83 | + true |
| 84 | + } |
| 85 | +} |
0 commit comments