You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/09/16 09:39:28 UTC

[GitHub] [spark] beliefer opened a new pull request, #37915: [SPARK-40465][SQL] Refactor Decimal so as we can use other underlying implementation

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

   ### What changes were proposed in this pull request?
   Currently, Spark `Decimal` use the `java.math.BigDecimal` as the underlying implementation.
   As we know, the computing and storage efficiency of Spark `Decimal`  is very low.
   This PR let `Decimal` can supports other underlying implementation.
   
   The reconstruction design of Spark `Decimal` is mainly based on the proxy mode - that is, to create the abstraction class `DecimalOperation`, to lower the current implementation of Spark `Decimal` based on `java.math.BigDecimal` to `JDKDecimalOperation`. The API of Spark `Decimal` remains unchanged and proxies the corresponding interface methods of `DecimalOperation`. The whole design is shown in the figure below.
   ![UML 图](https://user-images.githubusercontent.com/8486025/190606990-19252d1b-8f9f-49e1-8244-408b02e77e73.jpg)
   
   Whether Spark `Decimal` will proxy `JDKDecimalOperation` or other subclasses of `DecimalOperation` depends on the configuration parameter `spark.sql.decimal.implementation`. The default value of `spark.sql.decimal.implementation` is `JDKBigDecimal`, that is, `Decimal` will proxy `JDKDecimalOperation`. `spark.sql.decimal.implementation` can also be set as other possible optimization implementations.
   
   
   ### Why are the changes needed?
   Refactor `Decimal` so as we can use other underlying implementation.
   
   
   ### Does this PR introduce _any_ user-facing change?
   'No'.
   The interface of `Decimal` is not changed.
   
   
   ### How was this patch tested?
   N/A
   


-- 
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] beliefer commented on a diff in pull request #37915: [SPARK-40465][SQL] Refactor Decimal so as we can use other underlying implementation

Posted by GitBox <gi...@apache.org>.
beliefer commented on code in PR #37915:
URL: https://github.com/apache/spark/pull/37915#discussion_r977133079


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala:
##########
@@ -28,52 +28,35 @@ import org.apache.spark.sql.internal.SQLConf
 import org.apache.spark.unsafe.types.UTF8String
 
 /**
- * A mutable implementation of BigDecimal that can hold a Long if values are small enough.
- *
- * The semantics of the fields are as follows:
- * - _precision and _scale represent the SQL precision and scale we are looking for
- * - If decimalVal is set, it represents the whole decimal value
- * - Otherwise, the decimal value is longVal / (10 ** _scale)
- *
- * Note, for values between -1.0 and 1.0, precision digits are only counted after dot.
+ * A mutable implementation of BigDecimal that hold a `DecimalOperation`.
  */
 @Unstable
-final class Decimal extends Ordered[Decimal] with Serializable {
+final class Decimal(initEnabled: Boolean = true) extends Ordered[Decimal] with Serializable {
   import org.apache.spark.sql.types.Decimal._
 
-  private var decimalVal: BigDecimal = null
-  private var longVal: Long = 0L
-  private var _precision: Int = 1
-  private var _scale: Int = 0
+  private var decimalOperation: DecimalOperation[_] = null

Review Comment:
   `decimalOperation` is not null in default. To reduce the overhead of operations (e.g. +), let it is null.
   I tested the benchmark, if give a default `DecimalOperation` value, these math operations (e.g. +) have 5x performance overhead.



-- 
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] beliefer commented on a diff in pull request #37915: [SPARK-40465][SQL] Refactor Decimal so as we can use other underlying implementation

Posted by GitBox <gi...@apache.org>.
beliefer commented on code in PR #37915:
URL: https://github.com/apache/spark/pull/37915#discussion_r977203717


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/DecimalBenchmark.scala:
##########
@@ -0,0 +1,157 @@
+/*
+ * 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.spark.sql
+
+import org.apache.spark.benchmark.{Benchmark, BenchmarkBase}
+import org.apache.spark.sql.catalyst.encoders.RowEncoder
+import org.apache.spark.sql.catalyst.expressions.codegen.GenerateSafeProjection
+import org.apache.spark.sql.types.{Decimal, DecimalType, StructType}
+
+/**
+ * Benchmark for the previous Decimal (without proxy mode) vs refactored Decimal (proxy mode).
+ * To run this benchmark:
+ * {{{
+ *   1. without sbt:
+ *      bin/spark-submit --class <this class> --jars <spark core test jar> <spark catalyst test jar>
+ *   2. build/sbt "catalyst/Test/runMain <this class>"
+ *   3. generate result:
+ *      SPARK_GENERATE_BENCHMARK_FILES=1 build/sbt "catalyst/Test/runMain <this class>"
+ *      Results will be written to "benchmarks/DecimalBenchmark-results.txt".

Review Comment:
   OK



-- 
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] github-actions[bot] commented on pull request #37915: [SPARK-40465][SQL] Refactor Decimal so as we can use other underlying implementation

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on PR #37915:
URL: https://github.com/apache/spark/pull/37915#issuecomment-1386275505

   We're closing this PR because it hasn't been updated in a while. This isn't a judgement on the merit of the PR in any way. It's just a way of keeping the PR queue manageable.
   If you'd like to revive this PR, please reopen it and ask a committer to remove the Stale tag!


-- 
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] baibaichen commented on pull request #37915: [SPARK-40465][SQL] Refactor Decimal so as we can use other underlying implementation

Posted by GitBox <gi...@apache.org>.
baibaichen commented on PR #37915:
URL: https://github.com/apache/spark/pull/37915#issuecomment-1251986118

   @beliefer thanks for bring this up!
   
   cc @cloud-fan would you pelease review this PR.  
   


-- 
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] beliefer commented on pull request #37915: [SPARK-40465][SQL] Refactor Decimal so as we can use other underlying implementation

Posted by GitBox <gi...@apache.org>.
beliefer commented on PR #37915:
URL: https://github.com/apache/spark/pull/37915#issuecomment-1272494104

   ping @cloud-fan would you pelease review this PR.


-- 
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] beliefer commented on a diff in pull request #37915: [SPARK-40465][SQL] Refactor Decimal so as we can use other underlying implementation

Posted by GitBox <gi...@apache.org>.
beliefer commented on code in PR #37915:
URL: https://github.com/apache/spark/pull/37915#discussion_r977133079


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala:
##########
@@ -28,52 +28,35 @@ import org.apache.spark.sql.internal.SQLConf
 import org.apache.spark.unsafe.types.UTF8String
 
 /**
- * A mutable implementation of BigDecimal that can hold a Long if values are small enough.
- *
- * The semantics of the fields are as follows:
- * - _precision and _scale represent the SQL precision and scale we are looking for
- * - If decimalVal is set, it represents the whole decimal value
- * - Otherwise, the decimal value is longVal / (10 ** _scale)
- *
- * Note, for values between -1.0 and 1.0, precision digits are only counted after dot.
+ * A mutable implementation of BigDecimal that hold a `DecimalOperation`.
  */
 @Unstable
-final class Decimal extends Ordered[Decimal] with Serializable {
+final class Decimal(initEnabled: Boolean = true) extends Ordered[Decimal] with Serializable {
   import org.apache.spark.sql.types.Decimal._
 
-  private var decimalVal: BigDecimal = null
-  private var longVal: Long = 0L
-  private var _precision: Int = 1
-  private var _scale: Int = 0
+  private var decimalOperation: DecimalOperation[_] = null

Review Comment:
   decimalOperation is not null in default. To reduce the overhead of operations (e.g. +), let it is null.
   I tested the benchmark, if give a default `DecimalOperation` value, these math operations (e.g. +) have 5x performance overhead.



-- 
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 #37915: [SPARK-40465][SQL] Refactor Decimal so as we can use other underlying implementation

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #37915:
URL: https://github.com/apache/spark/pull/37915#discussion_r977165972


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/DecimalBenchmark.scala:
##########
@@ -0,0 +1,157 @@
+/*
+ * 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.spark.sql
+
+import org.apache.spark.benchmark.{Benchmark, BenchmarkBase}
+import org.apache.spark.sql.catalyst.encoders.RowEncoder
+import org.apache.spark.sql.catalyst.expressions.codegen.GenerateSafeProjection
+import org.apache.spark.sql.types.{Decimal, DecimalType, StructType}
+
+/**
+ * Benchmark for the previous Decimal (without proxy mode) vs refactored Decimal (proxy mode).
+ * To run this benchmark:
+ * {{{
+ *   1. without sbt:
+ *      bin/spark-submit --class <this class> --jars <spark core test jar> <spark catalyst test jar>
+ *   2. build/sbt "catalyst/Test/runMain <this class>"
+ *   3. generate result:
+ *      SPARK_GENERATE_BENCHMARK_FILES=1 build/sbt "catalyst/Test/runMain <this class>"
+ *      Results will be written to "benchmarks/DecimalBenchmark-results.txt".

Review Comment:
   can you run it to generate result and commit the result?



-- 
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 #37915: [SPARK-40465][SQL] Refactor Decimal so as we can use other underlying implementation

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on PR #37915:
URL: https://github.com/apache/spark/pull/37915#issuecomment-1252350140

   The abstraction makes sense to me. The new `DecimalOperation` layer adds an indirection and may hurt performance, though I think the overhead is minor as Java decimal operation is already slow. Can we add a microbenchmark for `Decimal`? You can follow `HashBenchmark`.


-- 
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] beliefer commented on pull request #37915: [SPARK-40465][SQL] Refactor Decimal so as we can use other underlying implementation

Posted by GitBox <gi...@apache.org>.
beliefer commented on PR #37915:
URL: https://github.com/apache/spark/pull/37915#issuecomment-1253596565

   > The abstraction makes sense to me. The new `DecimalOperation` layer adds an indirection and may hurt performance, though I think the overhead is minor as Java decimal operation is already slow. Can we add a microbenchmark for `Decimal`? You can follow `HashBenchmark`.
   
   I followed `HashBenchmark` and create a new benchmark `DecimalBenchmark`.


-- 
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] beliefer commented on pull request #37915: [SPARK-40465][SQL] Refactor Decimal so as we can use other underlying implementation

Posted by GitBox <gi...@apache.org>.
beliefer commented on PR #37915:
URL: https://github.com/apache/spark/pull/37915#issuecomment-1252008481

   This PR can ensure that the existing underlying implementation of `Decimal` will not be changed, and it also provides an interface for us to develop new implementations through iteration. With this interface, no matter whether we can enter the community in the future, our development and maintenance costs will be greatly reduced.


-- 
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 #37915: [SPARK-40465][SQL] Refactor Decimal so as we can use other underlying implementation

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #37915:
URL: https://github.com/apache/spark/pull/37915#discussion_r977125557


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/types/Decimal.scala:
##########
@@ -28,52 +28,35 @@ import org.apache.spark.sql.internal.SQLConf
 import org.apache.spark.unsafe.types.UTF8String
 
 /**
- * A mutable implementation of BigDecimal that can hold a Long if values are small enough.
- *
- * The semantics of the fields are as follows:
- * - _precision and _scale represent the SQL precision and scale we are looking for
- * - If decimalVal is set, it represents the whole decimal value
- * - Otherwise, the decimal value is longVal / (10 ** _scale)
- *
- * Note, for values between -1.0 and 1.0, precision digits are only counted after dot.
+ * A mutable implementation of BigDecimal that hold a `DecimalOperation`.
  */
 @Unstable
-final class Decimal extends Ordered[Decimal] with Serializable {
+final class Decimal(initEnabled: Boolean = true) extends Ordered[Decimal] with Serializable {
   import org.apache.spark.sql.types.Decimal._
 
-  private var decimalVal: BigDecimal = null
-  private var longVal: Long = 0L
-  private var _precision: Int = 1
-  private var _scale: Int = 0
+  private var decimalOperation: DecimalOperation[_] = null

Review Comment:
   does it mean `decimalOperation` can be null sometimes? This seems fragile.



-- 
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] github-actions[bot] closed pull request #37915: [SPARK-40465][SQL] Refactor Decimal so as we can use other underlying implementation

Posted by GitBox <gi...@apache.org>.
github-actions[bot] closed pull request #37915: [SPARK-40465][SQL] Refactor Decimal so as we can use other underlying implementation
URL: https://github.com/apache/spark/pull/37915


-- 
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