andygrove commented on code in PR #2018: URL: https://github.com/apache/datafusion-comet/pull/2018#discussion_r2202060728
########## spark/src/main/scala/org/apache/comet/serde/arithmetic.scala: ########## @@ -0,0 +1,293 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.comet.serde + +import scala.math.min + +import org.apache.spark.sql.catalyst.expressions.{Add, Attribute, Divide, EqualTo, EvalMode, Expression, If, IntegralDivide, Literal, Multiply, Remainder, Subtract} +import org.apache.spark.sql.types.{BinaryType, BooleanType, ByteType, DataType, DateType, DecimalType, DoubleType, FloatType, IntegerType, LongType, NullType, ShortType, StringType, TimestampNTZType, TimestampType} + +import org.apache.comet.CometSparkSessionExtensions.withInfo +import org.apache.comet.expressions.CometEvalMode +import org.apache.comet.serde.QueryPlanSerde.{castToProto, evalModeToProto, exprToProtoInternal, serializeDataType} +import org.apache.comet.shims.CometEvalModeUtil + +trait MathBase { + def createMathExpression( + expr: Expression, + left: Expression, + right: Expression, + inputs: Seq[Attribute], + binding: Boolean, + dataType: DataType, + evalMode: EvalMode.Value, + f: (ExprOuterClass.Expr.Builder, ExprOuterClass.MathExpr) => ExprOuterClass.Expr.Builder) + : Option[ExprOuterClass.Expr] = { + val leftExpr = exprToProtoInternal(left, inputs, binding) + val rightExpr = exprToProtoInternal(right, inputs, binding) + + if (leftExpr.isDefined && rightExpr.isDefined) { + // create the generic MathExpr message + val builder = ExprOuterClass.MathExpr.newBuilder() + builder.setLeft(leftExpr.get) + builder.setRight(rightExpr.get) + builder.setEvalMode(evalModeToProto(CometEvalModeUtil.fromSparkEvalMode(evalMode))) + serializeDataType(dataType).foreach { t => + builder.setReturnType(t) + } + val inner = builder.build() + // call the user-supplied function to wrap MathExpr in a top-level Expr + // such as Expr.Add or Expr.Divide + Some( + f( + ExprOuterClass.Expr + .newBuilder(), + inner).build()) + } else { + withInfo(expr, left, right) + None + } + } + + def nullIfWhenPrimitive(expression: Expression): Expression = + if (isPrimitive(expression)) { + val zero = Literal.default(expression.dataType) + expression match { + case _: Literal if expression != zero => expression + case _ => + If(EqualTo(expression, zero), Literal.create(null, expression.dataType), expression) + } + } else { + expression + } + + private def isPrimitive(expression: Expression): Boolean = expression.dataType match { + case _: ByteType | _: ShortType | _: IntegerType | _: LongType | _: FloatType | + _: DoubleType | _: TimestampType | _: DateType | _: BooleanType | _: DecimalType => + true + case _ => false + } Review Comment: This is moved without modification from QueryPlanSerde. I am not sure why this method is needed. I'm pretty sure that we can only perform arithmetic on primitive types? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org