Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ package object dsl {

implicit def symbolToUnresolvedAttribute(s: Symbol) = analysis.UnresolvedAttribute(s.name)

def sum(e: Expression) = Sum(e)
def sumDistinct(e: Expression) = SumDistinct(e)
def count(e: Expression) = Count(e)
def countDistinct(e: Expression*) = CountDistinct(e)
def avg(e: Expression) = Average(e)
def first(e: Expression) = First(e)
def min(e: Expression) = Min(e)
def max(e: Expression) = Max(e)
def upper(e: Expression) = Upper(e)
def lower(e: Expression) = Lower(e)

implicit class DslSymbol(sym: Symbol) extends ImplicitAttribute { def s = sym.name }
// TODO more implicit class for literal?
implicit class DslString(val s: String) extends ImplicitOperators {
Expand Down
9 changes: 7 additions & 2 deletions sql/core/src/main/scala/org/apache/spark/sql/SchemaRDD.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,13 @@ class SchemaRDD(
*
* @group Query
*/
def select(exprs: NamedExpression*): SchemaRDD =
new SchemaRDD(sqlContext, Project(exprs, logicalPlan))
def select(exprs: Expression*): SchemaRDD = {
val aliases = exprs.zipWithIndex.map {
case (ne: NamedExpression, _) => ne
case (e, i) => Alias(e, s"c$i")()
}
new SchemaRDD(sqlContext, Project(aliases, logicalPlan))
}

/**
* Filters the output, only returning those rows where `condition` evaluates to true.
Expand Down
32 changes: 26 additions & 6 deletions sql/core/src/test/scala/org/apache/spark/sql/DslQuerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ class DslQuerySuite extends QueryTest {
Seq(Seq("1")))
}

test("select with functions") {
checkAnswer(
testData.select(sum('value), avg('value), count(1)),
Seq(Seq(5050.0, 50.5, 100)))

checkAnswer(
testData2.select('a + 'b, 'a < 'b),
Seq(
Seq(2, false),
Seq(3, true),
Seq(3, false),
Seq(4, false),
Seq(4, false),
Seq(5, false)))

checkAnswer(
testData2.select(sumDistinct('a)),
Seq(Seq(6)))
}

test("sorting") {
checkAnswer(
testData2.orderBy('a.asc, 'b.asc),
Expand Down Expand Up @@ -111,17 +131,17 @@ class DslQuerySuite extends QueryTest {

test("average") {
checkAnswer(
testData2.groupBy()(Average('a)),
testData2.groupBy()(avg('a)),
2.0)
}

test("null average") {
checkAnswer(
testData3.groupBy()(Average('b)),
testData3.groupBy()(avg('b)),
2.0)

checkAnswer(
testData3.groupBy()(Average('b), CountDistinct('b :: Nil)),
testData3.groupBy()(avg('b), countDistinct('b)),
(2.0, 1) :: Nil)
}

Expand All @@ -131,17 +151,17 @@ class DslQuerySuite extends QueryTest {

test("null count") {
checkAnswer(
testData3.groupBy('a)('a, Count('b)),
testData3.groupBy('a)('a, count('b)),
Seq((1,0), (2, 1))
)

checkAnswer(
testData3.groupBy('a)('a, Count('a + 'b)),
testData3.groupBy('a)('a, count('a + 'b)),
Seq((1,0), (2, 1))
)

checkAnswer(
testData3.groupBy()(Count('a), Count('b), Count(1), CountDistinct('a :: Nil), CountDistinct('b :: Nil)),
testData3.groupBy()(count('a), count('b), count(1), countDistinct('a), countDistinct('b)),
(2, 1, 2, 2, 1) :: Nil
)
}
Expand Down