-
Notifications
You must be signed in to change notification settings - Fork 267
Chore: refactor Comparison out of QueryPlanSerde #2028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mbutrovich
merged 9 commits into
apache:main
from
CuteChuanChuan:raymond/refactor-expressions-in-QueryPlanSerde
Aug 5, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
202556d
chore: refactor GreaterThan out of QueryPlanSerde
CuteChuanChuan 2606d4f
Refactor: extract comparison expressions from QueryPlanSerde
CuteChuanChuan 3dc8eee
Remove duplicated code based on reviewer's
CuteChuanChuan b1213e6
Move in function into CometIn
CuteChuanChuan 48548a5
Merge branch 'main' into raymond/refactor-expressions-in-QueryPlanSerde
CuteChuanChuan fec7387
Merge remote-tracking branch 'upstream/main'
CuteChuanChuan dbb66bf
Merge remote-tracking branch origin into local
CuteChuanChuan ce81013
Removes repeated GetMapValue expression when solve conflict
CuteChuanChuan 2b5d058
Extract in function and handle notIn and inSet
CuteChuanChuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208 changes: 208 additions & 0 deletions
208
spark/src/main/scala/org/apache/comet/serde/comparisons.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| /* | ||
| * 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, InSet, IsNaN, IsNotNull, IsNull, LessThan, LessThanOrEqual, Literal, Not} | ||
| 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._ | ||
|
|
||
| object CometGreaterThan extends CometExpressionSerde { | ||
| 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 { | ||
| 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 { | ||
| 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 { | ||
| 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 { | ||
| 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 { | ||
| 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 { | ||
| 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 { | ||
| override def convert( | ||
| expr: Expression, | ||
| inputs: Seq[Attribute], | ||
| binding: Boolean): Option[ExprOuterClass.Expr] = { | ||
| val inExpr = expr.asInstanceOf[In] | ||
| ComparisonUtils.in(expr, inExpr.value, inExpr.list, inputs, binding, negate = false) | ||
| } | ||
| } | ||
|
|
||
| object CometNotIn extends CometExpressionSerde { | ||
| override def convert( | ||
| expr: Expression, | ||
| inputs: Seq[Attribute], | ||
| binding: Boolean): Option[ExprOuterClass.Expr] = { | ||
| val notExpr = expr.asInstanceOf[Not] | ||
| val inExpr = notExpr.child.asInstanceOf[In] | ||
| ComparisonUtils.in(expr, inExpr.value, inExpr.list, inputs, binding, negate = true) | ||
| } | ||
| } | ||
|
|
||
| object CometInSet extends CometExpressionSerde { | ||
| override def convert( | ||
| expr: Expression, | ||
| inputs: Seq[Attribute], | ||
| binding: Boolean): Option[ExprOuterClass.Expr] = { | ||
| val inSetExpr = expr.asInstanceOf[InSet] | ||
| val valueDataType = inSetExpr.child.dataType | ||
| val list = inSetExpr.hset.map { setVal => | ||
| Literal(setVal, valueDataType) | ||
| }.toSeq | ||
| // Change `InSet` to `In` expression | ||
| // We do Spark `InSet` optimization in native (DataFusion) side. | ||
| ComparisonUtils.in(expr, inSetExpr.child, list, inputs, binding, negate = false) | ||
| } | ||
| } | ||
|
|
||
| object ComparisonUtils { | ||
|
|
||
| def in( | ||
CuteChuanChuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| expr: Expression, | ||
| value: Expression, | ||
| list: Seq[Expression], | ||
| inputs: Seq[Attribute], | ||
| binding: Boolean, | ||
| negate: Boolean): Option[Expr] = { | ||
| val valueExpr = exprToProtoInternal(value, inputs, binding) | ||
| val listExprs = list.map(exprToProtoInternal(_, inputs, binding)) | ||
| if (valueExpr.isDefined && listExprs.forall(_.isDefined)) { | ||
| val builder = ExprOuterClass.In.newBuilder() | ||
| builder.setInValue(valueExpr.get) | ||
| builder.addAllLists(listExprs.map(_.get).asJava) | ||
| builder.setNegated(negate) | ||
| Some( | ||
| ExprOuterClass.Expr | ||
| .newBuilder() | ||
| .setIn(builder) | ||
| .build()) | ||
| } else { | ||
| val allExprs = list ++ Seq(value) | ||
| withInfo(expr, allExprs: _*) | ||
| None | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand now why you brought that
infunction over to comparisons.scala. It was used here, and inInSetandNot(In).I think we should handle those two expressions in this PR, and bring
inover to comparisons.scala so the logic isn't duplicated in two places.