rwpenney commented on a change in pull request #30745: URL: https://github.com/apache/spark/pull/30745#discussion_r582188477
########## File path: sql/core/src/test/scala/org/apache/spark/sql/ProductAggSuite.scala ########## @@ -0,0 +1,106 @@ +/* + * 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.functions.{ col, lit, product } +import org.apache.spark.sql.test.SharedSparkSession + + +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") { + implicit val enc = Encoders.scalaDouble + + val prod = data16.agg(product(col("x"))).as[Double].head + val expected = (1L to 16L).reduce { _ * _ }.toDouble + + assert(prod === expected) + assert(prod === factorials(16)) + } + + test("type flexibility") { + import org.apache.spark.sql.types.{ ByteType, DoubleType, FloatType, IntegerType, ShortType } + 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 => + val prod = prods.select(typ).as[Double](Encoders.scalaDouble).head + assert(prod === factorials(16)) + } + } + + test("windowed factorials") { + import org.apache.spark.sql.expressions.Window + + implicit val enc = Encoders.product[(Long, Double)] + val win = Window.partitionBy(lit(1)).orderBy("x") + + val prodFactorials = data16.withColumn("f", product(col("x")).over(win)) + + val prodMap = prodFactorials.as[(Long, Double)].collect.toMap + + assert(prodMap.size === 16) + + assert(prodMap(1) === 1.0) + assert(prodMap(2) === 2.0) + assert(prodMap(3) === 6.0) + assert(prodMap(4) === 24.0) + assert(prodMap(5) === 120.0) + + factorials.zipWithIndex.drop(1).foreach { case (expected, idx) => + assert(prodMap(idx) === expected) + } + } + + test("grouped factorials") { + implicit val enc = Encoders.scalaDouble + + val grouped = data16.groupBy((col("x") % 3) as "mod3") + .agg(product(col("x")) as "product", + product(col("x"), 0.5) as "product_scaled", + product(col("x"), 1.0) as "product_unity", + product(col("x"), -1.0) as "product_minus") + .orderBy("mod3") + + def col2seq(s: String): Seq[Double] = + grouped.select(s).as[Double].collect.toSeq + + val expectedBase = Seq((3 * 6 * 9 * 12 * 15), + (1 * 4 * 7 * 10 * 13 * 16), + (2 * 5 * 8 * 11 * 14)) Review comment: I think the original version is clearer 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
