CuteChuanChuan commented on code in PR #2028: URL: https://github.com/apache/datafusion-comet/pull/2028#discussion_r2228741221
########## spark/src/main/scala/org/apache/comet/serde/comparisons.scala: ########## @@ -0,0 +1,265 @@ +/* + * 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.collection.JavaConverters._ + +import org.apache.spark.sql.catalyst.expressions.{Attribute, Expression, GreaterThan, GreaterThanOrEqual, In, IsNaN, IsNotNull, IsNull, LessThan, LessThanOrEqual} +import org.apache.spark.sql.types.BooleanType + +import org.apache.comet.CometSparkSessionExtensions.withInfo +import org.apache.comet.serde.ExprOuterClass.Expr +import org.apache.comet.serde.QueryPlanSerde.{exprToProtoInternal, scalarFunctionExprToProtoWithReturnType} + +object CometGreaterThan extends CometExpressionSerde with ComparisonBase { + override def convert( + expr: Expression, + inputs: Seq[Attribute], + binding: Boolean): Option[ExprOuterClass.Expr] = { + val greaterThan = expr.asInstanceOf[GreaterThan] + + createBinaryExpr( + expr, + greaterThan.left, + greaterThan.right, + inputs, + binding, + (builder, binaryExpr) => builder.setGt(binaryExpr)) + } +} + +object CometGreaterThanOrEqual extends CometExpressionSerde with ComparisonBase { + override def convert( + expr: Expression, + inputs: Seq[Attribute], + binding: Boolean): Option[ExprOuterClass.Expr] = { + val greaterThanOrEqual = expr.asInstanceOf[GreaterThanOrEqual] + + createBinaryExpr( + expr, + greaterThanOrEqual.left, + greaterThanOrEqual.right, + inputs, + binding, + (builder, binaryExpr) => builder.setGtEq(binaryExpr)) + } +} + +object CometLessThan extends CometExpressionSerde with ComparisonBase { + override def convert( + expr: Expression, + inputs: Seq[Attribute], + binding: Boolean): Option[ExprOuterClass.Expr] = { + val lessThan = expr.asInstanceOf[LessThan] + + createBinaryExpr( + expr, + lessThan.left, + lessThan.right, + inputs, + binding, + (builder, binaryExpr) => builder.setLt(binaryExpr)) + } +} + +object CometLessThanOrEqual extends CometExpressionSerde with ComparisonBase { + override def convert( + expr: Expression, + inputs: Seq[Attribute], + binding: Boolean): Option[ExprOuterClass.Expr] = { + val lessThanOrEqual = expr.asInstanceOf[LessThanOrEqual] + + createBinaryExpr( + expr, + lessThanOrEqual.left, + lessThanOrEqual.right, + inputs, + binding, + (builder, binaryExpr) => builder.setLtEq(binaryExpr)) + } +} + +object CometIsNull extends CometExpressionSerde with ComparisonBase { + override def convert( + expr: Expression, + inputs: Seq[Attribute], + binding: Boolean): Option[ExprOuterClass.Expr] = { + val isNull = expr.asInstanceOf[IsNull] + + createUnaryExpr( + expr, + isNull.child, + inputs, + binding, + (builder, unaryExpr) => builder.setIsNull(unaryExpr)) + } +} + +object CometIsNotNull extends CometExpressionSerde with ComparisonBase { + override def convert( + expr: Expression, + inputs: Seq[Attribute], + binding: Boolean): Option[ExprOuterClass.Expr] = { + val isNotNull = expr.asInstanceOf[IsNotNull] + + createUnaryExpr( + expr, + isNotNull.child, + inputs, + binding, + (builder, unaryExpr) => builder.setIsNotNull(unaryExpr)) + } +} + +object CometIsNaN extends CometExpressionSerde with ComparisonBase { + override def convert( + expr: Expression, + inputs: Seq[Attribute], + binding: Boolean): Option[ExprOuterClass.Expr] = { + val isNaN = expr.asInstanceOf[IsNaN] + val childExpr = exprToProtoInternal(isNaN.child, inputs, binding) + val optExpr = scalarFunctionExprToProtoWithReturnType("isnan", BooleanType, childExpr) + + optExprWithInfo(optExpr, expr, isNaN.child) + } +} + +object CometIn extends CometExpressionSerde with ComparisonBase { + override def convert( + expr: Expression, + inputs: Seq[Attribute], + binding: Boolean): Option[ExprOuterClass.Expr] = { + val inExpr = expr.asInstanceOf[In] + in(expr, inExpr.value, inExpr.list, inputs, binding, negate = false) + } +} + +sealed trait ComparisonBase { + + /** + * Creates a UnaryExpr by calling exprToProtoInternal for the provided child expression and then + * invokes the supplied function to wrap this UnaryExpr in a top-level Expr. + * + * @param child + * Spark expression + * @param inputs + * Inputs to the expression + * @param f + * Function that accepts an Expr.Builder and a UnaryExpr and builds the specific top-level + * Expr + * @return + * Some(Expr) or None if not supported + */ + def createUnaryExpr( Review Comment: Initially, I thought duplicating these methods here could keep flexibility if the logic of `createUnaryExpr` and `createBinaryExpr` for comparisons needed to be tailored in the future. Thanks for your review, which reminds me that these two are common utilities for other expression logic, so I can directly use them from `QueryPlanSerde` rather than duplicating, which would make maintenance harder due to code duplication. -- 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