Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-23911][SQL] Add aggregate function. #21982

Closed
wants to merge 2 commits into from
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
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ object FunctionRegistry {
expression[ArrayRemove]("array_remove"),
expression[ArrayDistinct]("array_distinct"),
expression[ArrayTransform]("transform"),
expression[ArrayAggregate]("aggregate"),
CreateStruct.registryEntry,

// misc functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package org.apache.spark.sql.catalyst.expressions
import java.util.concurrent.atomic.AtomicReference

import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.analysis.{TypeCheckResult, UnresolvedAttribute}
import org.apache.spark.sql.catalyst.expressions.codegen._
import org.apache.spark.sql.catalyst.expressions.codegen.Block._
import org.apache.spark.sql.catalyst.util.{ArrayData, GenericArrayData}
Expand Down Expand Up @@ -74,6 +75,13 @@ case class LambdaFunction(
override def eval(input: InternalRow): Any = function.eval(input)
}

object LambdaFunction {
val identity: LambdaFunction = {
val id = UnresolvedAttribute.quoted("id")
LambdaFunction(id, Seq(id))
}
}

/**
* A higher order function takes one or more (lambda) functions and applies these to some objects.
* The function produces a number of variables which can be consumed by some lambda function.
Expand Down Expand Up @@ -140,6 +148,18 @@ trait ArrayBasedHigherOrderFunction extends HigherOrderFunction with ExpectsInpu
@transient lazy val functionForEval: Expression = functionsForEval.head
}

object ArrayBasedHigherOrderFunction {

def elementArgumentType(dt: DataType): (DataType, Boolean) = {
dt match {
case ArrayType(elementType, containsNull) => (elementType, containsNull)
case _ =>
val ArrayType(elementType, containsNull) = ArrayType.defaultConcreteType
(elementType, containsNull)
}
}
}

/**
* Transform elements in an array using the transform function. This is similar to
* a `map` in functional programming.
Expand All @@ -164,17 +184,12 @@ case class ArrayTransform(
override def dataType: ArrayType = ArrayType(function.dataType, function.nullable)

override def bind(f: (Expression, Seq[(DataType, Boolean)]) => LambdaFunction): ArrayTransform = {
val (elementType, containsNull) = input.dataType match {
case ArrayType(elementType, containsNull) => (elementType, containsNull)
case _ =>
val ArrayType(elementType, containsNull) = ArrayType.defaultConcreteType
(elementType, containsNull)
}
val elem = ArrayBasedHigherOrderFunction.elementArgumentType(input.dataType)
function match {
case LambdaFunction(_, arguments, _) if arguments.size == 2 =>
copy(function = f(function, (elementType, containsNull) :: (IntegerType, false) :: Nil))
copy(function = f(function, elem :: (IntegerType, false) :: Nil))
case _ =>
copy(function = f(function, (elementType, containsNull) :: Nil))
copy(function = f(function, elem :: Nil))
}
}

Expand Down Expand Up @@ -210,3 +225,90 @@ case class ArrayTransform(

override def prettyName: String = "transform"
}

/**
* Applies a binary operator to a start value and all elements in the array.
*/
@ExpressionDescription(
usage =
"""
_FUNC_(expr, start, merge, finish) - Applies a binary operator to an initial state and all
elements in the array, and reduces this to a single state. The final state is converted
into the final result by applying a finish function.
""",
examples = """
Examples:
> SELECT _FUNC_(array(1, 2, 3), (acc, x) -> acc + x);
Copy link
Member

Choose a reason for hiding this comment

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

@ueshin, would you mind if I ask to kindly double check if the example works? seems not in my local:

spark-sql> SELECT aggregate(array(1, 2, 3), (acc, x) -> acc + x);
2018-08-08 16:08:25 ERROR SparkSQLDriver:91 - Failed in [SELECT aggregate(array(1, 2, 3), (acc, x) -> acc + x)]
org.apache.spark.sql.catalyst.analysis.UnresolvedException: Invalid call to dataType on unresolved object, tree: 'acc
	at org.apache.spark.sql.catalyst.analysis.UnresolvedAttribute.dataType(unresolved.scala:105)
	at org.apache.spark.sql.catalyst.expressions.BinaryArithmetic.dataType(arithmetic.scala:119)
	at org.apache.spark.sql.catalyst.expressions.LambdaFunction.dataType(higherOrderFunctions.scala:72)
	at org.apache.spark.sql.hive.HiveSessionCatalog$$anonfun$1.apply(HiveSessionCatalog.scala:122)
	at org.apache.spark.sql.hive.HiveSessionCatalog$$anonfun$1.apply(HiveSessionCatalog.scala:121)
	at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
	at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
	at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
	at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
	at scala.collection.TraversableLike$class.map(TraversableLike.scala:234)
	at scala.collection.AbstractTraversable.map(Traversable.scala:104)

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, sorry, we need the second argument as an initial value.

SELECT aggregate(array(1, 2, 3), 0, (acc, x) -> acc + x);

I'll submit a follow-up pr soon.

Copy link
Member Author

Choose a reason for hiding this comment

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

Submitted #22035. Thanks!

6
> SELECT _FUNC_(array(1, 2, 3), (acc, x) -> acc + x, acc -> acc * 10);
60
""",
since = "2.4.0")
case class ArrayAggregate(
input: Expression,
zero: Expression,
merge: Expression,
finish: Expression)
extends HigherOrderFunction with CodegenFallback {

def this(input: Expression, zero: Expression, merge: Expression) = {
this(input, zero, merge, LambdaFunction.identity)
}

override def inputs: Seq[Expression] = input :: zero :: Nil

override def functions: Seq[Expression] = merge :: finish :: Nil

override def nullable: Boolean = input.nullable || finish.nullable

override def dataType: DataType = finish.dataType

override def checkInputDataTypes(): TypeCheckResult = {
if (!ArrayType.acceptsType(input.dataType)) {
TypeCheckResult.TypeCheckFailure(
s"argument 1 requires ${ArrayType.simpleString} type, " +
s"however, '${input.sql}' is of ${input.dataType.catalogString} type.")
} else if (!DataType.equalsStructurally(
zero.dataType, merge.dataType, ignoreNullability = true)) {
TypeCheckResult.TypeCheckFailure(
s"argument 3 requires ${zero.dataType.simpleString} type, " +
s"however, '${merge.sql}' is of ${merge.dataType.catalogString} type.")
} else {
TypeCheckResult.TypeCheckSuccess
}
}

override def bind(f: (Expression, Seq[(DataType, Boolean)]) => LambdaFunction): ArrayAggregate = {
// Be very conservative with nullable. We cannot be sure that the accumulator does not
// evaluate to null. So we always set nullable to true here.
val elem = ArrayBasedHigherOrderFunction.elementArgumentType(input.dataType)
val acc = zero.dataType -> true
val newMerge = f(merge, acc :: elem :: Nil)
val newFinish = f(finish, acc :: Nil)
copy(merge = newMerge, finish = newFinish)
}

@transient lazy val LambdaFunction(_,
Seq(accForMergeVar: NamedLambdaVariable, elementVar: NamedLambdaVariable), _) = merge
@transient lazy val LambdaFunction(_, Seq(accForFinishVar: NamedLambdaVariable), _) = finish

override def eval(input: InternalRow): Any = {
val arr = this.input.eval(input).asInstanceOf[ArrayData]
if (arr == null) {
null
} else {
val Seq(mergeForEval, finishForEval) = functionsForEval
accForMergeVar.value.set(zero.eval(input))
var i = 0
while (i < arr.numElements()) {
elementVar.value.set(arr.get(i, elementVar.dataType))
accForMergeVar.value.set(mergeForEval.eval(input))
i += 1
}
accForFinishVar.value.set(accForMergeVar.value.get)
finishForEval.eval(input)
}
}

override def prettyName: String = "aggregate"
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ class HigherOrderFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper
ArrayTransform(expr, createLambda(at.elementType, at.containsNull, IntegerType, false, f))
}

def aggregate(
expr: Expression,
zero: Expression,
merge: (Expression, Expression) => Expression,
finish: Expression => Expression): Expression = {
val at = expr.dataType.asInstanceOf[ArrayType]
val zeroType = zero.dataType
ArrayAggregate(
expr,
zero,
createLambda(zeroType, true, at.elementType, at.containsNull, merge),
createLambda(zeroType, true, finish))
}

def aggregate(
expr: Expression,
zero: Expression,
merge: (Expression, Expression) => Expression): Expression = {
aggregate(expr, zero, merge, identity)
}

test("ArrayTransform") {
val ai0 = Literal.create(Seq(1, 2, 3), ArrayType(IntegerType, containsNull = false))
val ai1 = Literal.create(Seq[Integer](1, null, 3), ArrayType(IntegerType, containsNull = true))
Expand Down Expand Up @@ -94,4 +115,33 @@ class HigherOrderFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper
checkEvaluation(transform(aai, array => Cast(transform(array, plusIndex), StringType)),
Seq("[1, 3, 5]", null, "[4, 6]"))
}

test("ArrayAggregate") {
val ai0 = Literal.create(Seq(1, 2, 3), ArrayType(IntegerType, containsNull = false))
val ai1 = Literal.create(Seq[Integer](1, null, 3), ArrayType(IntegerType, containsNull = true))
val ai2 = Literal.create(Seq.empty[Int], ArrayType(IntegerType, containsNull = false))
val ain = Literal.create(null, ArrayType(IntegerType, containsNull = false))

checkEvaluation(aggregate(ai0, 0, (acc, elem) => acc + elem, acc => acc * 10), 60)
checkEvaluation(aggregate(ai1, 0, (acc, elem) => acc + coalesce(elem, 0), acc => acc * 10), 40)
checkEvaluation(aggregate(ai2, 0, (acc, elem) => acc + elem, acc => acc * 10), 0)
checkEvaluation(aggregate(ain, 0, (acc, elem) => acc + elem, acc => acc * 10), null)

val as0 = Literal.create(Seq("a", "b", "c"), ArrayType(StringType, containsNull = false))
val as1 = Literal.create(Seq("a", null, "c"), ArrayType(StringType, containsNull = true))
val as2 = Literal.create(Seq.empty[String], ArrayType(StringType, containsNull = false))
val asn = Literal.create(null, ArrayType(StringType, containsNull = false))

checkEvaluation(aggregate(as0, "", (acc, elem) => Concat(Seq(acc, elem))), "abc")
checkEvaluation(aggregate(as1, "", (acc, elem) => Concat(Seq(acc, coalesce(elem, "x")))), "axc")
checkEvaluation(aggregate(as2, "", (acc, elem) => Concat(Seq(acc, elem))), "")
checkEvaluation(aggregate(asn, "", (acc, elem) => Concat(Seq(acc, elem))), null)

val aai = Literal.create(Seq[Seq[Integer]](Seq(1, 2, 3), null, Seq(4, 5)),
ArrayType(ArrayType(IntegerType, containsNull = false), containsNull = true))
checkEvaluation(
aggregate(aai, 0,
(acc, array) => coalesce(aggregate(array, acc, (acc, elem) => acc + elem), acc)),
15)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ select transform(ys, 0) as v from nested;

-- Transform a null array
select transform(cast(null as array<int>), x -> x + 1) as v;

-- Aggregate.
select aggregate(ys, 0, (y, a) -> y + a + x) as v from nested;

-- Aggregate average.
select aggregate(ys, (0 as sum, 0 as n), (acc, x) -> (acc.sum + x, acc.n + 1), acc -> acc.sum / acc.n) as v from nested;

-- Aggregate nested arrays
select transform(zs, z -> aggregate(z, 1, (acc, val) -> acc * val * size(z))) as v from nested;

-- Aggregate a null array
select aggregate(cast(null as array<int>), 0, (a, y) -> a + y + 1, a -> a + 2) as v;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 8
-- Number of queries: 12


-- !query 0
Expand Down Expand Up @@ -79,3 +79,41 @@ select transform(cast(null as array<int>), x -> x + 1) as v
struct<v:array<int>>
-- !query 7 output
NULL


-- !query 8
select aggregate(ys, 0, (y, a) -> y + a + x) as v from nested
-- !query 8 schema
struct<v:int>
-- !query 8 output
131
15
5


-- !query 9
select aggregate(ys, (0 as sum, 0 as n), (acc, x) -> (acc.sum + x, acc.n + 1), acc -> acc.sum / acc.n) as v from nested
-- !query 9 schema
struct<v:double>
-- !query 9 output
0.5
12.0
64.5


-- !query 10
select transform(zs, z -> aggregate(z, 1, (acc, val) -> acc * val * size(z))) as v from nested
-- !query 10 schema
struct<v:array<int>>
-- !query 10 output
[1010880,8]
[17]
[4752,20664,1]


-- !query 11
select aggregate(cast(null as array<int>), 0, (a, y) -> a + y + 1, a -> a + 2) as v
-- !query 11 schema
struct<v:int>
-- !query 11 output
NULL
Loading