You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by we...@apache.org on 2024/11/15 01:17:28 UTC

(spark) branch master updated: [SPARK-50318][SQL] Add IntervalUtils.makeYearMonthInterval to deduplicate code between interpreted and codegen

This is an automated email from the ASF dual-hosted git repository.

wenchen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new c90efae2b4aa [SPARK-50318][SQL] Add IntervalUtils.makeYearMonthInterval to deduplicate code between interpreted and codegen
c90efae2b4aa is described below

commit c90efae2b4aaafd453db72404254dec55e797e9c
Author: Ruzel Ibragimov <ru...@databricks.com>
AuthorDate: Fri Nov 15 09:17:13 2024 +0800

    [SPARK-50318][SQL] Add IntervalUtils.makeYearMonthInterval to deduplicate code between interpreted and codegen
    
    ### What changes were proposed in this pull request?
    
    It's refactoring of existing code. Add `makeYearMonthInterval` and remove duplicated code from `MakeDTInterval`
    
    ### Why are the changes needed?
    
    Better code quality.
    
    ### Does this PR introduce _any_ user-facing change?
    
    No.
    
    ### How was this patch tested?
    
    Using existing tests.
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Yes, Copilot was used.
    
    Closes #48848 from gotocoding-DB/deduplicate-code.
    
    Authored-by: Ruzel Ibragimov <ru...@databricks.com>
    Signed-off-by: Wenchen Fan <we...@databricks.com>
---
 .../catalyst/expressions/intervalExpressions.scala | 23 ++++------------------
 .../spark/sql/catalyst/util/IntervalUtils.scala    |  9 +++++++++
 2 files changed, 13 insertions(+), 19 deletions(-)

diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/intervalExpressions.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/intervalExpressions.scala
index a7b67f55d8cd..653ee9f836ed 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/intervalExpressions.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/intervalExpressions.scala
@@ -24,7 +24,6 @@ import com.google.common.math.{DoubleMath, IntMath, LongMath}
 
 import org.apache.spark.QueryContext
 import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, CodeGenerator, ExprCode}
-import org.apache.spark.sql.catalyst.util.DateTimeConstants.MONTHS_PER_YEAR
 import org.apache.spark.sql.catalyst.util.IntervalUtils
 import org.apache.spark.sql.catalyst.util.IntervalUtils._
 import org.apache.spark.sql.errors.{QueryCompilationErrors, QueryExecutionErrors}
@@ -572,28 +571,14 @@ case class MakeYMInterval(years: Expression, months: Expression)
   override def dataType: DataType = YearMonthIntervalType()
 
   override def nullSafeEval(year: Any, month: Any): Any = {
-    try {
-      Math.toIntExact(
-        Math.addExact(month.asInstanceOf[Int],
-          Math.multiplyExact(year.asInstanceOf[Int], MONTHS_PER_YEAR)))
-    } catch {
-      case _: ArithmeticException =>
-        throw QueryExecutionErrors.withoutSuggestionIntervalArithmeticOverflowError(origin.context)
-    }
+    makeYearMonthInterval(year.asInstanceOf[Int], month.asInstanceOf[Int], origin.context)
   }
 
   override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
-    nullSafeCodeGen(ctx, ev, (years, months) => {
-      val math = classOf[Math].getName.stripSuffix("$")
+    defineCodeGen(ctx, ev, (years, months) => {
       val errorContext = getContextOrNullCode(ctx)
-      // scalastyle:off line.size.limit
-      s"""
-         |try {
-         |  ${ev.value} = $math.toIntExact($math.addExact($months, $math.multiplyExact($years, $MONTHS_PER_YEAR)));
-         |} catch (java.lang.ArithmeticException e) {
-         |  throw QueryExecutionErrors.withoutSuggestionIntervalArithmeticOverflowError($errorContext);
-         |}""".stripMargin
-      // scalastyle:on line.size.limit
+      val iu = IntervalUtils.getClass.getName.stripSuffix("$")
+      s"$iu.makeYearMonthInterval($years, $months, $errorContext)"
     })
   }
 
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala
index 39a07990dea3..8793c0407a9b 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/IntervalUtils.scala
@@ -797,6 +797,15 @@ object IntervalUtils extends SparkIntervalUtils {
     }
   }
 
+  def makeYearMonthInterval(year: Int, month: Int, context: QueryContext): Int = {
+    try {
+      Math.toIntExact(Math.addExact(month, Math.multiplyExact(year, MONTHS_PER_YEAR)))
+    } catch {
+      case _: ArithmeticException =>
+        throw QueryExecutionErrors.withoutSuggestionIntervalArithmeticOverflowError(context)
+    }
+  }
+
   def intToYearMonthInterval(v: Int, startField: Byte, endField: Byte): Int = {
     endField match {
       case YEAR =>


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org