Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ case class Abs(child: Expression, failOnError: Boolean = SQLConf.get.ansiEnabled
abstract class BinaryArithmetic extends BinaryOperator
with NullIntolerant with SupportQueryContext {

protected val evalMode: EvalMode.Value
protected[sql] val evalMode: EvalMode.Value
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to make sure we can access evalMode in tests.


private lazy val internalDataType: DataType = (left.dataType, right.dataType) match {
case (DecimalType.Fixed(p1, s1), DecimalType.Fixed(p2, s2)) =>
Expand Down Expand Up @@ -237,6 +237,8 @@ abstract class BinaryArithmetic extends BinaryOperator
}
}

override def otherCopyArgs: Seq[AnyRef] = evalMode :: Nil

final override val nodePatterns: Seq[TreePattern] = Seq(BINARY_ARITHMETIC)

override def initQueryContext(): Option[QueryContext] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import org.apache.spark.sql.types._
case class BitwiseAnd(left: Expression, right: Expression) extends BinaryArithmetic
with CommutativeExpression {

protected override val evalMode: EvalMode.Value = EvalMode.LEGACY
protected[sql] override val evalMode: EvalMode.Value = EvalMode.LEGACY

override def inputType: AbstractDataType = IntegralType

Expand Down Expand Up @@ -86,7 +86,7 @@ case class BitwiseAnd(left: Expression, right: Expression) extends BinaryArithme
case class BitwiseOr(left: Expression, right: Expression) extends BinaryArithmetic
with CommutativeExpression {

protected override val evalMode: EvalMode.Value = EvalMode.LEGACY
protected[sql] override val evalMode: EvalMode.Value = EvalMode.LEGACY

override def inputType: AbstractDataType = IntegralType

Expand Down Expand Up @@ -133,7 +133,7 @@ case class BitwiseOr(left: Expression, right: Expression) extends BinaryArithmet
case class BitwiseXor(left: Expression, right: Expression) extends BinaryArithmetic
with CommutativeExpression {

protected override val evalMode: EvalMode.Value = EvalMode.LEGACY
protected[sql] override val evalMode: EvalMode.Value = EvalMode.LEGACY

override def inputType: AbstractDataType = IntegralType

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1086,4 +1086,23 @@ class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper
checkEvaluation(IntegralDivide(Literal(Duration.ofDays(1)),
Literal(Duration.ofHours(-5))), -4L)
}

test("SPARK-48016: makeCopy should include the evalMode") {
val originalLeft = Literal(1)
val originalRight = Literal(0)
val newLeft = Literal(1.0)
val newRight = Literal(0.0)
Seq(
(left: Expression, right: Expression) => Divide(left, right),
(left: Expression, right: Expression) => Add(left, right),
(left: Expression, right: Expression) => Subtract(left, right),
(left: Expression, right: Expression) => Multiply(left, right)
).foreach { binaryOp =>
val original = binaryOp(originalLeft, originalRight)
val copy = original.makeCopy(Array(newLeft, newRight)).asInstanceOf[BinaryArithmetic]
assert(copy.evalMode === original.evalMode)
assert(copy.left == newLeft)
assert(copy.right == newRight)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ Project [try_divide(1, (1.0 / 0.0)) AS try_divide(1, (1.0 / 0.0))#x]
+- OneRowRelation


-- !query
SELECT try_divide(1, decimal(0))
-- !query analysis
Project [try_divide(1, cast(0 as decimal(10,0))) AS try_divide(1, 0)#x]
+- OneRowRelation


-- !query
SELECT try_divide(interval 2 year, 2)
-- !query analysis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ Project [try_divide(1, (1.0 / 0.0)) AS try_divide(1, (1.0 / 0.0))#x]
+- OneRowRelation


-- !query
SELECT try_divide(1, decimal(0))
-- !query analysis
Project [try_divide(1, cast(0 as decimal(10,0))) AS try_divide(1, 0)#x]
+- OneRowRelation


-- !query
SELECT try_divide(interval 2 year, 2)
-- !query analysis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ SELECT try_divide(0, 0);
SELECT try_divide(1, (2147483647 + 1));
SELECT try_divide(1L, (9223372036854775807L + 1L));
SELECT try_divide(1, 1.0 / 0.0);
SELECT try_divide(1, decimal(0));

-- Interval / Numeric
SELECT try_divide(interval 2 year, 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,14 @@ org.apache.spark.SparkArithmeticException
}


-- !query
SELECT try_divide(1, decimal(0))
-- !query schema
struct<try_divide(1, 0):decimal(12,11)>
-- !query output
NULL


-- !query
SELECT try_divide(interval 2 year, 2)
-- !query schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ struct<try_divide(1, (1.0 / 0.0)):decimal(16,9)>
NULL


-- !query
SELECT try_divide(1, decimal(0))
-- !query schema
struct<try_divide(1, 0):decimal(12,11)>
-- !query output
NULL


-- !query
SELECT try_divide(interval 2 year, 2)
-- !query schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ object V2FunctionBenchmark extends SqlBasedBenchmark {
left: Expression,
right: Expression,
override val nullable: Boolean) extends BinaryArithmetic {
protected override val evalMode: EvalMode.Value = EvalMode.LEGACY
protected[sql] override val evalMode: EvalMode.Value = EvalMode.LEGACY
override def inputType: AbstractDataType = NumericType
override def symbol: String = "+"

Expand Down