shay1bz commented on code in PR #41:
URL: https://github.com/apache/datafu/pull/41#discussion_r1439203585


##########
datafu-spark/src/test/scala/datafu/spark/TestAggregators.scala:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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 datafu.spark
+
+import com.holdenkarau.spark.testing.DataFrameSuiteBase
+import org.junit.Assert
+import org.junit.runner.RunWith
+import org.scalatest.FunSuite
+import org.scalatest.junit.JUnitRunner
+import org.apache.logging.log4j.LogManager
+import org.apache.spark.sql.functions.udaf
+import org.apache.spark.SparkConf
+import org.apache.spark.sql.{DataFrame, Row}
+import org.apache.spark.sql.internal.StaticSQLConf.CATALOG_IMPLEMENTATION
+import org.apache.spark.sql.types._
+import java.io.File
+import java.nio.file.{Path, Paths, Files, SimpleFileVisitor, FileVisitResult}
+import java.nio.file.attribute.BasicFileAttributes
+
+import datafu.spark.Aggregators._
+
+@RunWith(classOf[JUnitRunner])
+class TestAggregators extends FunSuite with DataFrameSuiteBase {
+
+  import spark.implicits._
+
+  /**
+    * taken from 
https://github.com/holdenk/spark-testing-base/issues/234#issuecomment-390150835
+    *
+    * Solves problem with Hive in Spark 2.3.0 in spark-testing-base
+    */
+  override def conf: SparkConf =
+    super.conf.set(CATALOG_IMPLEMENTATION.key, "hive")
+
+  val logger = LogManager.getLogger(this.getClass)
+
+  val inputSchema = List(
+    StructField("col_grp", StringType, true),
+    StructField("col_ord", IntegerType, false),
+    StructField("col_str", StringType, true)
+  )
+
+  lazy val inputRDD = sc.parallelize(
+    Seq(Row("a", 1, "asd1"),
+        Row("a", 2, "asd2"),
+        Row("a", 3, "asd3"),
+        Row("b", 1, "asd4")))
+
+  lazy val df =
+    sqlContext.createDataFrame(inputRDD, StructType(inputSchema)).cache
+
+  case class mapExp(map_col: Map[String, Int])
+  case class mapArrExp(map_col: Map[String, Array[String]])
+
+  lazy val defaultDbLocation = spark.sql("describe database default").toDF
+       .collect()
+       .filter(_.getString(0) == "Location")(0)(1)
+       .toString.replace("file:", "")
+
+  def deleteLeftoverFiles(table : String) : Unit = {
+   
+       val tablePath = Paths.get(defaultDbLocation + File.separator + table)
+       
+       // sanity check - only delete files if the path seems to be to a Hive 
warehouse
+       if (defaultDbLocation.endsWith("warehouse") && Files.exists(tablePath)) 
Files.walkFileTree(tablePath, new SimpleFileVisitor[Path] {
+               override def visitFile(path: Path, attrs: BasicFileAttributes): 
FileVisitResult = {
+                       Files.delete(path)
+                       FileVisitResult.CONTINUE
+               }
+       }
+  )
+ }
+
+  test("test multiset simple") {
+    val ms = udaf(new MultiSet())
+    val expected: DataFrame =
+      sqlContext.createDataFrame(List(mapExp(Map("b" -> 1, "a" -> 3))))
+    assertDataFrameEquals(expected, df.agg(ms($"col_grp").as("map_col")))
+  }
+
+  val mas = udaf(new MultiArraySet[String]())
+
+  test("test multiarrayset simple") {
+    assertDataFrameEquals(
+      sqlContext.createDataFrame(List(mapExp(Map("tre" -> 1, "asd" -> 2)))),
+      spark
+        .sql("select array('asd','tre','asd') arr")
+        .groupBy()
+        .agg(mas($"arr").as("map_col"))
+    )
+  }
+
+  test("test multiarrayset all nulls") {
+    // end case
+    spark.sql("drop table if exists mas_table")
+    deleteLeftoverFiles("mas_table")
+       
+    spark.sql("create table mas_table (arr array<string>)")
+    spark.sql(
+      "insert overwrite table mas_table select case when 1=2 then array('asd') 
end " +
+        "from (select 1)z")
+    spark.sql(
+      "insert into table mas_table select case when 1=2 then array('asd') end 
from (select 1)z")
+    spark.sql(
+      "insert into table mas_table select case when 1=2 then array('asd') end 
from (select 1)z")
+    spark.sql(
+      "insert into table mas_table select case when 1=2 then array('asd') end 
from (select 1)z")
+    spark.sql(
+      "insert into table mas_table select case when 1=2 then array('asd') end 
from (select 1)z")
+
+    val expected = sqlContext.createDataFrame(List(mapExp(Map())))
+
+    val actual =
+      spark.table("mas_table").groupBy().agg(mas($"arr").as("map_col"))
+
+    assertDataFrameEquals(expected, actual)
+  }
+
+  test("test multiarrayset max keys") {
+    // max keys case
+    spark.sql("drop table if exists mas_table2")
+    deleteLeftoverFiles("mas_table2")
+
+    spark.sql("create table mas_table2 (arr array<string>)")
+    spark.sql(
+      "insert overwrite table mas_table2 select array('asd','dsa') from 
(select 1)z")

Review Comment:
   Are all these 'z' aliases really needed?



-- 
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: dev-unsubscr...@datafu.apache.org

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

Reply via email to