Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make sql.GeneratorSuite and move the existing testcases.
  • Loading branch information
dongjoon-hyun committed Jun 29, 2016
commit c5dee492ff71c4127ed0a6bb0e818d61d95525bd
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,13 @@ abstract class ExplodeBase(child: Expression, position: Boolean)
usage = "_FUNC_(a) - Separates the elements of array a into multiple rows, or the elements of map a into multiple rows and columns.",
extended = "> SELECT _FUNC_(array(10,20));\n 10\n 20")
// scalastyle:on line.size.limit
case class Explode(child: Expression)
extends ExplodeBase(child, position = false) with Serializable {
}
case class Explode(child: Expression) extends ExplodeBase(child, position = false)

/**
* Given an input array produces a sequence of rows for each position and value in the array.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw since the expression description might be difficult to see without line wrapping, it'd also be better to put an example here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also you don't need the { }

*
* {{{
* SELECT explode(array(10,20)) ->
* SELECT posexplode(array(10,20)) ->
* 0 10
* 1 20
* }}}
Expand All @@ -196,6 +194,4 @@ case class Explode(child: Expression)
usage = "_FUNC_(a) - Separates the elements of array a into multiple rows with positions, or the elements of a map into multiple rows and columns with positions.",
extended = "> SELECT _FUNC_(array(10,20));\n 0\t10\n 1\t20")
// scalastyle:on line.size.limit
case class PosExplode(child: Expression)
extends ExplodeBase(child, position = true) with Serializable {
}
case class PosExplode(child: Expression) extends ExplodeBase(child, position = true)
Original file line number Diff line number Diff line change
Expand Up @@ -122,73 +122,6 @@ class ColumnExpressionSuite extends QueryTest with SharedSQLContext {
assert(newCol.expr.asInstanceOf[NamedExpression].metadata.getString("key") === "value")
}

test("single explode") {
val df = Seq((1, Seq(1, 2, 3))).toDF("a", "intList")
checkAnswer(
df.select(explode('intList)),
Row(1) :: Row(2) :: Row(3) :: Nil)
}

test("single posexplode") {
val df = Seq((1, Seq(1, 2, 3))).toDF("a", "intList")
checkAnswer(
df.select(posexplode('intList)),
Row(0, 1) :: Row(1, 2) :: Row(2, 3) :: Nil)
}

test("explode and other columns") {
val df = Seq((1, Seq(1, 2, 3))).toDF("a", "intList")

checkAnswer(
df.select($"a", explode('intList)),
Row(1, 1) ::
Row(1, 2) ::
Row(1, 3) :: Nil)

checkAnswer(
df.select($"*", explode('intList)),
Row(1, Seq(1, 2, 3), 1) ::
Row(1, Seq(1, 2, 3), 2) ::
Row(1, Seq(1, 2, 3), 3) :: Nil)
}

test("aliased explode") {
val df = Seq((1, Seq(1, 2, 3))).toDF("a", "intList")

checkAnswer(
df.select(explode('intList).as('int)).select('int),
Row(1) :: Row(2) :: Row(3) :: Nil)

checkAnswer(
df.select(explode('intList).as('int)).select(sum('int)),
Row(6) :: Nil)
}

test("explode on map") {
val df = Seq((1, Map("a" -> "b"))).toDF("a", "map")

checkAnswer(
df.select(explode('map)),
Row("a", "b"))
}

test("explode on map with aliases") {
val df = Seq((1, Map("a" -> "b"))).toDF("a", "map")

checkAnswer(
df.select(explode('map).as("key1" :: "value1" :: Nil)).select("key1", "value1"),
Row("a", "b"))
}

test("self join explode") {
val df = Seq((1, Seq(1, 2, 3))).toDF("a", "intList")
val exploded = df.select(explode('intList).as('i))

checkAnswer(
exploded.join(exploded, exploded("i") === exploded("i")).agg(count("*")),
Row(3) :: Nil)
}

test("collect on column produced by a binary operator") {
val df = Seq((1, 2, 3)).toDF("a", "b", "c")
checkAnswer(df.select(df("a") + df("b")), Seq(Row(3)))
Expand Down
92 changes: 92 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/GeneratorSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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._
import org.apache.spark.sql.test.SharedSQLContext

class GeneratorSuite extends QueryTest with SharedSQLContext {
import testImplicits._

test("single explode") {
val df = Seq((1, Seq(1, 2, 3))).toDF("a", "intList")
checkAnswer(
df.select(explode('intList)),
Row(1) :: Row(2) :: Row(3) :: Nil)
}

test("single posexplode") {
val df = Seq((1, Seq(1, 2, 3))).toDF("a", "intList")
checkAnswer(
df.select(posexplode('intList)),
Row(0, 1) :: Row(1, 2) :: Row(2, 3) :: Nil)
}

test("explode and other columns") {
val df = Seq((1, Seq(1, 2, 3))).toDF("a", "intList")

checkAnswer(
df.select($"a", explode('intList)),
Row(1, 1) ::
Row(1, 2) ::
Row(1, 3) :: Nil)

checkAnswer(
df.select($"*", explode('intList)),
Row(1, Seq(1, 2, 3), 1) ::
Row(1, Seq(1, 2, 3), 2) ::
Row(1, Seq(1, 2, 3), 3) :: Nil)
}

test("aliased explode") {
val df = Seq((1, Seq(1, 2, 3))).toDF("a", "intList")

checkAnswer(
df.select(explode('intList).as('int)).select('int),
Row(1) :: Row(2) :: Row(3) :: Nil)

checkAnswer(
df.select(explode('intList).as('int)).select(sum('int)),
Row(6) :: Nil)
}

test("explode on map") {
val df = Seq((1, Map("a" -> "b"))).toDF("a", "map")

checkAnswer(
df.select(explode('map)),
Row("a", "b"))
}

test("explode on map with aliases") {
val df = Seq((1, Map("a" -> "b"))).toDF("a", "map")

checkAnswer(
df.select(explode('map).as("key1" :: "value1" :: Nil)).select("key1", "value1"),
Row("a", "b"))
}

test("self join explode") {
val df = Seq((1, Seq(1, 2, 3))).toDF("a", "intList")
val exploded = df.select(explode('intList).as('i))

checkAnswer(
exploded.join(exploded, exploded("i") === exploded("i")).agg(count("*")),
Row(3) :: Nil)
}
}