You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "amaliujia (via GitHub)" <gi...@apache.org> on 2023/04/07 02:48:31 UTC

[GitHub] [spark] amaliujia opened a new pull request, #40693: [SPARK-43058] Move Numeric to PhysicalDataType

amaliujia opened a new pull request, #40693:
URL: https://github.com/apache/spark/pull/40693

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'core/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   This PR proposes that we start to move Numeric to PhysicalDataType. This is to simplify the DataType class to make it become a simple interface without coupling too many internal representations.
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   To make DataType become a simpler interface, non-public code can be moved outside of the DataType class.
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   NO
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   UT


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] cloud-fan commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162318834


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/decimalExpressions.scala:
##########
@@ -273,7 +274,8 @@ case class DecimalDivideWithOverflowCheck(
       }
     } else {
       val value2 = right.eval(input)
-      dataType.fractional.asInstanceOf[Fractional[Any]].div(value1, value2).asInstanceOf[Decimal]
+      PhysicalDecimalType(dataType.precision, dataType.scale)
+        .fractional.asInstanceOf[Fractional[Any]].div(value1, value2).asInstanceOf[Decimal]

Review Comment:
   ```suggestion
           .fractional.div(value1, value2).asInstanceOf[Decimal]
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] amaliujia commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "amaliujia (via GitHub)" <gi...@apache.org>.
amaliujia commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162212156


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -902,152 +903,191 @@ case class Cast(
   }
 
   // LongConverter
-  private[this] def castToLong(from: DataType): Any => Any = from match {
-    case StringType if ansiEnabled =>
-      buildCast[UTF8String](_, v => UTF8StringUtils.toLongExact(v, getContextOrNull()))
-    case StringType =>
-      val result = new LongWrapper()
-      buildCast[UTF8String](_, s => if (s.toLong(result)) result.value else null)
-    case BooleanType =>
-      buildCast[Boolean](_, b => if (b) 1L else 0L)
-    case DateType =>
-      buildCast[Int](_, d => null)
-    case TimestampType =>
-      buildCast[Long](_, t => timestampToLong(t))
-    case x: NumericType if ansiEnabled =>
-      b => x.exactNumeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: NumericType =>
-      b => x.numeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: DayTimeIntervalType =>
-      buildCast[Long](_, i => dayTimeIntervalToLong(i, x.startField, x.endField))
-    case x: YearMonthIntervalType =>
-      buildCast[Int](_, i => yearMonthIntervalToInt(i, x.startField, x.endField).toLong)
+  private[this] def castToLong(from: DataType): Any => Any = {
+    var exactNumeric: Numeric[Any] = null
+    var numeric: Numeric[Any] = null

Review Comment:
   This won't work because there is a `PhysicalDataType(dt).asInstanceOf[PhysicalNumericType]` thus the code will break whenever the type is not numeric type. We will see exception like `xxx cannot be cast to PhysicalNumericType`.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] amaliujia commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "amaliujia (via GitHub)" <gi...@apache.org>.
amaliujia commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1161954353


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -915,9 +916,9 @@ case class Cast(
     case TimestampType =>
       buildCast[Long](_, t => timestampToLong(t))
     case x: NumericType if ansiEnabled =>
-      b => x.exactNumeric.asInstanceOf[Numeric[Any]].toLong(b)
+      b => PhysicalNumericType.exactNumeric(x).toLong(b)

Review Comment:
   Done



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] hvanhovell commented on a diff in pull request #40693: [SPARK-43058] Move Numeric to PhysicalDataType

Posted by "hvanhovell (via GitHub)" <gi...@apache.org>.
hvanhovell commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1160400552


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/types/PhysicalDataType.scala:
##########
@@ -23,7 +23,7 @@ import scala.reflect.runtime.universe.typeTag
 import org.apache.spark.sql.catalyst.expressions.{Ascending, BoundReference, InterpretedOrdering, SortOrder}
 import org.apache.spark.sql.catalyst.util.{ArrayData, SQLOrderingUtil}
 import org.apache.spark.sql.errors.QueryExecutionErrors
-import org.apache.spark.sql.types.{ArrayType, BinaryType, BooleanType, ByteType, DataType, DateType, DayTimeIntervalType, Decimal, DecimalType, DoubleType, FloatType, IntegerType, LongType, MapType, NullType, ShortType, StringType, StructField, StructType, TimestampNTZType, TimestampType, YearMonthIntervalType}
+import org.apache.spark.sql.types.{ArrayType, BinaryType, BooleanType, ByteExactNumeric, ByteType, DataType, DateType, DayTimeIntervalType, Decimal, DecimalExactNumeric, DecimalType, DoubleExactNumeric, DoubleType, FloatExactNumeric, FloatType, IntegerExactNumeric, IntegerType, LongExactNumeric, LongType, MapType, NullType, NumericType, ShortExactNumeric, ShortType, StringType, StructField, StructType, TimestampNTZType, TimestampType, YearMonthIntervalType}

Review Comment:
   We also have to move `*ExactNumeric` classes at some point.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] hvanhovell commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "hvanhovell (via GitHub)" <gi...@apache.org>.
hvanhovell commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1161097318


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -915,9 +916,9 @@ case class Cast(
     case TimestampType =>
       buildCast[Long](_, t => timestampToLong(t))
     case x: NumericType if ansiEnabled =>
-      b => x.exactNumeric.asInstanceOf[Numeric[Any]].toLong(b)
+      b => PhysicalNumericType.exactNumeric(x).toLong(b)

Review Comment:
   One small issue with all of these changes is that we are doing the same resolution over and over on the hot path, while we only need to do it once. Can you move the resolution to the `PhysicalNumericType.numeric` out of the function, and do this for all calls in here?
   
   For example:
   
   ``` scala
   val exactNumeric = PhysicalNumericType.exactNumeric(x)
   b => exactNumeric.toLong(b)
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] cloud-fan commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1161521386


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -915,9 +916,9 @@ case class Cast(
     case TimestampType =>
       buildCast[Long](_, t => timestampToLong(t))
     case x: NumericType if ansiEnabled =>
-      b => x.exactNumeric.asInstanceOf[Numeric[Any]].toLong(b)
+      b => PhysicalNumericType.exactNumeric(x).toLong(b)

Review Comment:
   let's not overcomplicate it. The code here matches a data type and returns a lambda. We just need to get the `numeric` ahead of the lambda function body.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] cloud-fan commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162225985


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -902,152 +903,191 @@ case class Cast(
   }
 
   // LongConverter
-  private[this] def castToLong(from: DataType): Any => Any = from match {
-    case StringType if ansiEnabled =>
-      buildCast[UTF8String](_, v => UTF8StringUtils.toLongExact(v, getContextOrNull()))
-    case StringType =>
-      val result = new LongWrapper()
-      buildCast[UTF8String](_, s => if (s.toLong(result)) result.value else null)
-    case BooleanType =>
-      buildCast[Boolean](_, b => if (b) 1L else 0L)
-    case DateType =>
-      buildCast[Int](_, d => null)
-    case TimestampType =>
-      buildCast[Long](_, t => timestampToLong(t))
-    case x: NumericType if ansiEnabled =>
-      b => x.exactNumeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: NumericType =>
-      b => x.numeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: DayTimeIntervalType =>
-      buildCast[Long](_, i => dayTimeIntervalToLong(i, x.startField, x.endField))
-    case x: YearMonthIntervalType =>
-      buildCast[Int](_, i => yearMonthIntervalToInt(i, x.startField, x.endField).toLong)
+  private[this] def castToLong(from: DataType): Any => Any = {
+    var exactNumeric: Numeric[Any] = null
+    var numeric: Numeric[Any] = null

Review Comment:
   Or we keep `PhysicalNumericType`, but its `numeric` takes `DataType` and throws an exception if the input is not `NumericType`. Then we can still simplify the code here.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] cloud-fan commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162342255


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -820,11 +821,13 @@ case class Divide(
   }
 
   private lazy val div: (Any, Any) => Any = dataType match {
-    case d @ DecimalType.Fixed(precision, scale) => (l, r) => {
-      val value = d.fractional.asInstanceOf[Fractional[Any]].div(l, r)
-      checkDecimalOverflow(value.asInstanceOf[Decimal], precision, scale)
-    }
-    case ft: FractionalType => ft.fractional.asInstanceOf[Fractional[Any]].div
+    case d @ DecimalType.Fixed(precision, scale) =>
+      val fractional = PhysicalDecimalType(precision, scale).fractional
+      (l, r) => {
+        val value = fractional.asInstanceOf[Fractional[Any]].div(l, r)

Review Comment:
   ah got it, no need to change then.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] cloud-fan closed pull request #40693: [SPARK-43058][SQL] Move Numeric and Fractional to PhysicalDataType

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #40693: [SPARK-43058][SQL] Move Numeric and Fractional to PhysicalDataType
URL: https://github.com/apache/spark/pull/40693


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] cloud-fan commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162309743


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -821,10 +822,11 @@ case class Divide(
 
   private lazy val div: (Any, Any) => Any = dataType match {
     case d @ DecimalType.Fixed(precision, scale) => (l, r) => {
-      val value = d.fractional.asInstanceOf[Fractional[Any]].div(l, r)
+      val value =
+        PhysicalDecimalType(precision, scale).fractional.asInstanceOf[Fractional[Any]].div(l, r)

Review Comment:
   actually we can just return `PhysicalDecimalType(precision, scale).fractional.asInstanceOf[Fractional[Any]].div`



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] amaliujia commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "amaliujia (via GitHub)" <gi...@apache.org>.
amaliujia commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162212156


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -902,152 +903,191 @@ case class Cast(
   }
 
   // LongConverter
-  private[this] def castToLong(from: DataType): Any => Any = from match {
-    case StringType if ansiEnabled =>
-      buildCast[UTF8String](_, v => UTF8StringUtils.toLongExact(v, getContextOrNull()))
-    case StringType =>
-      val result = new LongWrapper()
-      buildCast[UTF8String](_, s => if (s.toLong(result)) result.value else null)
-    case BooleanType =>
-      buildCast[Boolean](_, b => if (b) 1L else 0L)
-    case DateType =>
-      buildCast[Int](_, d => null)
-    case TimestampType =>
-      buildCast[Long](_, t => timestampToLong(t))
-    case x: NumericType if ansiEnabled =>
-      b => x.exactNumeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: NumericType =>
-      b => x.numeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: DayTimeIntervalType =>
-      buildCast[Long](_, i => dayTimeIntervalToLong(i, x.startField, x.endField))
-    case x: YearMonthIntervalType =>
-      buildCast[Int](_, i => yearMonthIntervalToInt(i, x.startField, x.endField).toLong)
+  private[this] def castToLong(from: DataType): Any => Any = {
+    var exactNumeric: Numeric[Any] = null
+    var numeric: Numeric[Any] = null

Review Comment:
   I see what you are suggesting. 
   
   Then probably we move every of those fields (ordering, fractional, numeric, exactnumeric, integral, etc.) to the top-level fields.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] amaliujia commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "amaliujia (via GitHub)" <gi...@apache.org>.
amaliujia commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162212156


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -902,152 +903,191 @@ case class Cast(
   }
 
   // LongConverter
-  private[this] def castToLong(from: DataType): Any => Any = from match {
-    case StringType if ansiEnabled =>
-      buildCast[UTF8String](_, v => UTF8StringUtils.toLongExact(v, getContextOrNull()))
-    case StringType =>
-      val result = new LongWrapper()
-      buildCast[UTF8String](_, s => if (s.toLong(result)) result.value else null)
-    case BooleanType =>
-      buildCast[Boolean](_, b => if (b) 1L else 0L)
-    case DateType =>
-      buildCast[Int](_, d => null)
-    case TimestampType =>
-      buildCast[Long](_, t => timestampToLong(t))
-    case x: NumericType if ansiEnabled =>
-      b => x.exactNumeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: NumericType =>
-      b => x.numeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: DayTimeIntervalType =>
-      buildCast[Long](_, i => dayTimeIntervalToLong(i, x.startField, x.endField))
-    case x: YearMonthIntervalType =>
-      buildCast[Int](_, i => yearMonthIntervalToInt(i, x.startField, x.endField).toLong)
+  private[this] def castToLong(from: DataType): Any => Any = {
+    var exactNumeric: Numeric[Any] = null
+    var numeric: Numeric[Any] = null

Review Comment:
   I see what you are suggesting. 
   
   Then probably we move every of those fields (ordering, fractional, numeric, exactnumeric, integral, etc.) to the top-level fields.
   
   This probably make PhysicalDataType more bigger as many PhysicalDataTypes need to throw.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] amaliujia commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "amaliujia (via GitHub)" <gi...@apache.org>.
amaliujia commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162330498


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -820,11 +821,13 @@ case class Divide(
   }
 
   private lazy val div: (Any, Any) => Any = dataType match {
-    case d @ DecimalType.Fixed(precision, scale) => (l, r) => {
-      val value = d.fractional.asInstanceOf[Fractional[Any]].div(l, r)
-      checkDecimalOverflow(value.asInstanceOf[Decimal], precision, scale)
-    }
-    case ft: FractionalType => ft.fractional.asInstanceOf[Fractional[Any]].div
+    case d @ DecimalType.Fixed(precision, scale) =>
+      val fractional = PhysicalDecimalType(precision, scale).fractional
+      (l, r) => {
+        val value = fractional.asInstanceOf[Fractional[Any]].div(l, r)

Review Comment:
   We actually needs this because the franctional here is from pyhsical data type directly which is fractional[InternalType]



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] amaliujia commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "amaliujia (via GitHub)" <gi...@apache.org>.
amaliujia commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162330656


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/decimalExpressions.scala:
##########
@@ -273,7 +274,8 @@ case class DecimalDivideWithOverflowCheck(
       }
     } else {
       val value2 = right.eval(input)
-      dataType.fractional.asInstanceOf[Fractional[Any]].div(value1, value2).asInstanceOf[Decimal]
+      PhysicalDecimalType(dataType.precision, dataType.scale)
+        .fractional.asInstanceOf[Fractional[Any]].div(value1, value2).asInstanceOf[Decimal]

Review Comment:
   same



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] cloud-fan commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162318609


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -820,11 +821,13 @@ case class Divide(
   }
 
   private lazy val div: (Any, Any) => Any = dataType match {
-    case d @ DecimalType.Fixed(precision, scale) => (l, r) => {
-      val value = d.fractional.asInstanceOf[Fractional[Any]].div(l, r)
-      checkDecimalOverflow(value.asInstanceOf[Decimal], precision, scale)
-    }
-    case ft: FractionalType => ft.fractional.asInstanceOf[Fractional[Any]].div
+    case d @ DecimalType.Fixed(precision, scale) =>
+      val fractional = PhysicalDecimalType(precision, scale).fractional
+      (l, r) => {
+        val value = fractional.asInstanceOf[Fractional[Any]].div(l, r)

Review Comment:
   ```suggestion
           val value = fractional.div(l, r)
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] cloud-fan commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162309432


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -821,10 +822,11 @@ case class Divide(
 
   private lazy val div: (Any, Any) => Any = dataType match {
     case d @ DecimalType.Fixed(precision, scale) => (l, r) => {
-      val value = d.fractional.asInstanceOf[Fractional[Any]].div(l, r)
+      val value =
+        PhysicalDecimalType(precision, scale).fractional.asInstanceOf[Fractional[Any]].div(l, r)

Review Comment:
   can we move it outside of the lambda?



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] amaliujia commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "amaliujia (via GitHub)" <gi...@apache.org>.
amaliujia commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1161169706


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -915,9 +916,9 @@ case class Cast(
     case TimestampType =>
       buildCast[Long](_, t => timestampToLong(t))
     case x: NumericType if ansiEnabled =>
-      b => x.exactNumeric.asInstanceOf[Numeric[Any]].toLong(b)
+      b => PhysicalNumericType.exactNumeric(x).toLong(b)

Review Comment:
   Essentially I think to achieve what you want to are doing something like this
   ```
   case FloatType: PhysicalFloatType. exactNumeric()
   case DoubleType: ....
   ...
   ```
   
   This is already the least code implementation.
   
   If we pull it out of the method it will even becomes this
   ```
   Map map; 
   case FloatType: map.put(FloatType, PhysicalFloatType. exactNumeric())
   
   within methods
   case FloatType: map,get(FloatType)
   ```
   @hvanhovell what do you think?
   
   



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] amaliujia commented on pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "amaliujia (via GitHub)" <gi...@apache.org>.
amaliujia commented on PR #40693:
URL: https://github.com/apache/spark/pull/40693#issuecomment-1499981247

   @hvanhovell @cloud-fan 


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] hvanhovell commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "hvanhovell (via GitHub)" <gi...@apache.org>.
hvanhovell commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1161097318


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -915,9 +916,9 @@ case class Cast(
     case TimestampType =>
       buildCast[Long](_, t => timestampToLong(t))
     case x: NumericType if ansiEnabled =>
-      b => x.exactNumeric.asInstanceOf[Numeric[Any]].toLong(b)
+      b => PhysicalNumericType.exactNumeric(x).toLong(b)

Review Comment:
   One small issue with all of these changes is that we are doing the same resolution over and over on the hot path, while we only need to do it once. Can you move the resolution to the `PhysicalNumericType.numeric` out of the function?
   
   For example:
   
   ``` scala
   val exactNumeric = PhysicalNumericType.exactNumeric(x)
   b => exactNumeric.toLong(b)
   ```
   
   The same applies for almost all other uses of numeric and exactNumeric.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] cloud-fan commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162208044


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -902,152 +903,191 @@ case class Cast(
   }
 
   // LongConverter
-  private[this] def castToLong(from: DataType): Any => Any = from match {
-    case StringType if ansiEnabled =>
-      buildCast[UTF8String](_, v => UTF8StringUtils.toLongExact(v, getContextOrNull()))
-    case StringType =>
-      val result = new LongWrapper()
-      buildCast[UTF8String](_, s => if (s.toLong(result)) result.value else null)
-    case BooleanType =>
-      buildCast[Boolean](_, b => if (b) 1L else 0L)
-    case DateType =>
-      buildCast[Int](_, d => null)
-    case TimestampType =>
-      buildCast[Long](_, t => timestampToLong(t))
-    case x: NumericType if ansiEnabled =>
-      b => x.exactNumeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: NumericType =>
-      b => x.numeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: DayTimeIntervalType =>
-      buildCast[Long](_, i => dayTimeIntervalToLong(i, x.startField, x.endField))
-    case x: YearMonthIntervalType =>
-      buildCast[Int](_, i => yearMonthIntervalToInt(i, x.startField, x.endField).toLong)
+  private[this] def castToLong(from: DataType): Any => Any = {
+    var exactNumeric: Numeric[Any] = null
+    var numeric: Numeric[Any] = null

Review Comment:
   this is really ugly... I'm wondering if we should just remove `PhysicalNumericType`, and make `numeric` a field in the top level `PhysicalDataType`, If a type doesn't support numeric, it should throw an exception.
   
   Then here we can just write
   ```
   lazy val numeric = PhysicalDataType.numeric(from)
   from match {
     ...
   }
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] cloud-fan commented on pull request #40693: [SPARK-43058][SQL] Move Numeric and Fractional to PhysicalDataType

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #40693:
URL: https://github.com/apache/spark/pull/40693#issuecomment-1503248755

   thanks, merging to master!


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] amaliujia commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "amaliujia (via GitHub)" <gi...@apache.org>.
amaliujia commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1161167873


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -915,9 +916,9 @@ case class Cast(
     case TimestampType =>
       buildCast[Long](_, t => timestampToLong(t))
     case x: NumericType if ansiEnabled =>
-      b => x.exactNumeric.asInstanceOf[Numeric[Any]].toLong(b)
+      b => PhysicalNumericType.exactNumeric(x).toLong(b)

Review Comment:
   Well after reading the code, I think we need to expland `NumericType` to all its sub-class and find the numeric directly to avoid `PhysicalNumericType.exactNumeric(x)`.
   
   This is because we do not know the running type in advance.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] amaliujia commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "amaliujia (via GitHub)" <gi...@apache.org>.
amaliujia commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162254891


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -902,152 +903,191 @@ case class Cast(
   }
 
   // LongConverter
-  private[this] def castToLong(from: DataType): Any => Any = from match {
-    case StringType if ansiEnabled =>
-      buildCast[UTF8String](_, v => UTF8StringUtils.toLongExact(v, getContextOrNull()))
-    case StringType =>
-      val result = new LongWrapper()
-      buildCast[UTF8String](_, s => if (s.toLong(result)) result.value else null)
-    case BooleanType =>
-      buildCast[Boolean](_, b => if (b) 1L else 0L)
-    case DateType =>
-      buildCast[Int](_, d => null)
-    case TimestampType =>
-      buildCast[Long](_, t => timestampToLong(t))
-    case x: NumericType if ansiEnabled =>
-      b => x.exactNumeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: NumericType =>
-      b => x.numeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: DayTimeIntervalType =>
-      buildCast[Long](_, i => dayTimeIntervalToLong(i, x.startField, x.endField))
-    case x: YearMonthIntervalType =>
-      buildCast[Int](_, i => yearMonthIntervalToInt(i, x.startField, x.endField).toLong)
+  private[this] def castToLong(from: DataType): Any => Any = {
+    var exactNumeric: Numeric[Any] = null
+    var numeric: Numeric[Any] = null

Review Comment:
   Code updated for simplification. 



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] cloud-fan commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162225985


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -902,152 +903,191 @@ case class Cast(
   }
 
   // LongConverter
-  private[this] def castToLong(from: DataType): Any => Any = from match {
-    case StringType if ansiEnabled =>
-      buildCast[UTF8String](_, v => UTF8StringUtils.toLongExact(v, getContextOrNull()))
-    case StringType =>
-      val result = new LongWrapper()
-      buildCast[UTF8String](_, s => if (s.toLong(result)) result.value else null)
-    case BooleanType =>
-      buildCast[Boolean](_, b => if (b) 1L else 0L)
-    case DateType =>
-      buildCast[Int](_, d => null)
-    case TimestampType =>
-      buildCast[Long](_, t => timestampToLong(t))
-    case x: NumericType if ansiEnabled =>
-      b => x.exactNumeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: NumericType =>
-      b => x.numeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: DayTimeIntervalType =>
-      buildCast[Long](_, i => dayTimeIntervalToLong(i, x.startField, x.endField))
-    case x: YearMonthIntervalType =>
-      buildCast[Int](_, i => yearMonthIntervalToInt(i, x.startField, x.endField).toLong)
+  private[this] def castToLong(from: DataType): Any => Any = {
+    var exactNumeric: Numeric[Any] = null
+    var numeric: Numeric[Any] = null

Review Comment:
   Or we keep `PhysicalNumericType`, but its `numeric` method takes `DataType` and throws an exception if the input is not `NumericType`. Then we can still simplify the code here.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] cloud-fan commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162310377


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -821,10 +822,11 @@ case class Divide(
 
   private lazy val div: (Any, Any) => Any = dataType match {
     case d @ DecimalType.Fixed(precision, scale) => (l, r) => {
-      val value = d.fractional.asInstanceOf[Fractional[Any]].div(l, r)
+      val value =
+        PhysicalDecimalType(precision, scale).fractional.asInstanceOf[Fractional[Any]].div(l, r)

Review Comment:
   and we can remove `.asInstanceOf[Fractional[Any]]`



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] cloud-fan commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162208044


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -902,152 +903,191 @@ case class Cast(
   }
 
   // LongConverter
-  private[this] def castToLong(from: DataType): Any => Any = from match {
-    case StringType if ansiEnabled =>
-      buildCast[UTF8String](_, v => UTF8StringUtils.toLongExact(v, getContextOrNull()))
-    case StringType =>
-      val result = new LongWrapper()
-      buildCast[UTF8String](_, s => if (s.toLong(result)) result.value else null)
-    case BooleanType =>
-      buildCast[Boolean](_, b => if (b) 1L else 0L)
-    case DateType =>
-      buildCast[Int](_, d => null)
-    case TimestampType =>
-      buildCast[Long](_, t => timestampToLong(t))
-    case x: NumericType if ansiEnabled =>
-      b => x.exactNumeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: NumericType =>
-      b => x.numeric.asInstanceOf[Numeric[Any]].toLong(b)
-    case x: DayTimeIntervalType =>
-      buildCast[Long](_, i => dayTimeIntervalToLong(i, x.startField, x.endField))
-    case x: YearMonthIntervalType =>
-      buildCast[Int](_, i => yearMonthIntervalToInt(i, x.startField, x.endField).toLong)
+  private[this] def castToLong(from: DataType): Any => Any = {
+    var exactNumeric: Numeric[Any] = null
+    var numeric: Numeric[Any] = null

Review Comment:
   this is really ugly... I'm wondering if we should just remove `PhysicalNumericType`, and make `numeric` a field in the top level `PhysicalDataType`, If a type doesn't support numeric, it should throw an exception.
   
   Then here we can just write
   ```
   lazy val numeric = PhysicalType.numeric(from)
   from match {
     ...
   }
   ```



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] amaliujia commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "amaliujia (via GitHub)" <gi...@apache.org>.
amaliujia commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162331907


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -820,11 +821,13 @@ case class Divide(
   }
 
   private lazy val div: (Any, Any) => Any = dataType match {
-    case d @ DecimalType.Fixed(precision, scale) => (l, r) => {
-      val value = d.fractional.asInstanceOf[Fractional[Any]].div(l, r)
-      checkDecimalOverflow(value.asInstanceOf[Decimal], precision, scale)
-    }
-    case ft: FractionalType => ft.fractional.asInstanceOf[Fractional[Any]].div
+    case d @ DecimalType.Fixed(precision, scale) =>
+      val fractional = PhysicalDecimalType(precision, scale).fractional
+      (l, r) => {
+        val value = fractional.asInstanceOf[Fractional[Any]].div(l, r)

Review Comment:
   If you think this is better, we can still does `PhysicalFrancitonalType.franctional(dt: Datatype)` which does a resolution but hide the `asInstanceOf[Fractional[Any]]`?



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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


[GitHub] [spark] amaliujia commented on a diff in pull request #40693: [SPARK-43058] Move Numeric and Fractional to PhysicalDataType

Posted by "amaliujia (via GitHub)" <gi...@apache.org>.
amaliujia commented on code in PR #40693:
URL: https://github.com/apache/spark/pull/40693#discussion_r1162330879


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -820,11 +821,13 @@ case class Divide(
   }
 
   private lazy val div: (Any, Any) => Any = dataType match {
-    case d @ DecimalType.Fixed(precision, scale) => (l, r) => {
-      val value = d.fractional.asInstanceOf[Fractional[Any]].div(l, r)
-      checkDecimalOverflow(value.asInstanceOf[Decimal], precision, scale)
-    }
-    case ft: FractionalType => ft.fractional.asInstanceOf[Fractional[Any]].div
+    case d @ DecimalType.Fixed(precision, scale) =>
+      val fractional = PhysicalDecimalType(precision, scale).fractional
+      (l, r) => {
+        val value = fractional.asInstanceOf[Fractional[Any]].div(l, r)

Review Comment:
   This is because we know it is decimal type thus just use `PhysicalDecimalType`



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


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