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 2021/03/01 18:00:49 UTC

[GitHub] [spark] rwpenney commented on a change in pull request #30745: [SPARK-33678][SQL] Product aggregation function

rwpenney commented on a change in pull request #30745:
URL: https://github.com/apache/spark/pull/30745#discussion_r584932395



##########
File path: sql/core/src/test/scala/org/apache/spark/sql/ProductAggSuite.scala
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.sql.expressions.Window
+import org.apache.spark.sql.functions.{ col, lit, product }
+import org.apache.spark.sql.test.SharedSparkSession
+import org.apache.spark.sql.types.{ByteType, DoubleType, FloatType, IntegerType, ShortType}
+
+
+class ProductAggSuite extends QueryTest
+  with SharedSparkSession {
+
+  // Sequence of integers small enough that factorial is representable exactly as DoubleType:
+  private lazy val data16 = spark.range(1, 17).toDF("x")
+
+  private lazy val factorials = (1 to 16).scanLeft(1L) { case (f, x) => f * x }
+
+  test("bare factorial") {
+    checkAnswer(
+      data16.agg(product(col("x"))),
+      Row((1L to 16L).reduce { _ * _ }.toDouble)
+    )
+
+    checkAnswer(
+      data16.agg(product(col("x"))),
+      Row(factorials(16))
+    )
+  }
+
+  test("type flexibility") {
+    val bytes16 = spark.createDataset((1 to 16).map { _.toByte })(Encoders.scalaByte).toDF("x")
+
+    val variants = Map(
+      "int8" -> ByteType, "int16" -> ShortType, "int32" -> IntegerType,
+      "float32" -> FloatType, "float64" -> DoubleType)
+
+    val prods = variants.foldLeft(bytes16) { case (df, (id, typ)) =>
+      df.withColumn(id, df.col("x").cast(typ))
+    }.agg(
+      lit(1) as "dummy",
+      variants.keys.toSeq.map { id => product(col(id)) as id } : _*)
+
+    variants.keys.foreach { typ =>
+      checkAnswer(
+        prods.select(typ),
+        Row(factorials(16))
+      )
+    }
+  }
+
+  test("windowed factorials") {
+    val win = Window.partitionBy(lit(1)).orderBy("x")
+
+    val prodFactorials = data16.withColumn("f", product(col("x")).over(win))
+
+    assert(prodFactorials.count === 16)
+
+    checkAnswer(
+      prodFactorials.limit(5),

Review comment:
       Fair point - the `Window` doesn't guarantee that the output will be ordered, although that's almost certain to be the case on this tiny dataset.
   I've added a `.sortBy` elsewhere to make this safer, thanks.




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

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