Skip to content

Commit

Permalink
[SPARK-25856][SQL][MINOR] Remove AverageLike and CountLike classes
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?
These two classes were added for regr_ expression support (SPARK-23907). These have been removed and hence we can remove these base classes and inline the logic in the concrete classes.
## How was this patch tested?
Existing tests.

Closes #22856 from dilipbiswal/average_cleanup.

Authored-by: Dilip Biswal <dbiswal@us.ibm.com>
Signed-off-by: Sean Owen <sean.owen@databricks.com>
  • Loading branch information
dilipbiswal authored and srowen committed Oct 29, 2018
1 parent 7fe5cff commit 5e5d886
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,21 @@ import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.util.TypeUtils
import org.apache.spark.sql.types._

abstract class AverageLike(child: Expression) extends DeclarativeAggregate {
@ExpressionDescription(
usage = "_FUNC_(expr) - Returns the mean calculated from values of a group.")
case class Average(child: Expression) extends DeclarativeAggregate with ImplicitCastInputTypes {

override def prettyName: String = "avg"

override def children: Seq[Expression] = child :: Nil

override def inputTypes: Seq[AbstractDataType] = Seq(NumericType)

override def checkInputDataTypes(): TypeCheckResult =
TypeUtils.checkForNumericExpr(child.dataType, "function average")

override def nullable: Boolean = true

// Return data type.
override def dataType: DataType = resultType

Expand Down Expand Up @@ -63,28 +75,11 @@ abstract class AverageLike(child: Expression) extends DeclarativeAggregate {
sum.cast(resultType) / count.cast(resultType)
}

protected def updateExpressionsDef: Seq[Expression] = Seq(
override lazy val updateExpressions: Seq[Expression] = Seq(
/* sum = */
Add(
sum,
coalesce(child.cast(sumDataType), Literal(0).cast(sumDataType))),
/* count = */ If(child.isNull, count, count + 1L)
)

override lazy val updateExpressions = updateExpressionsDef
}

@ExpressionDescription(
usage = "_FUNC_(expr) - Returns the mean calculated from values of a group.")
case class Average(child: Expression)
extends AverageLike(child) with ImplicitCastInputTypes {

override def prettyName: String = "avg"

override def children: Seq[Expression] = child :: Nil

override def inputTypes: Seq[AbstractDataType] = Seq(NumericType)

override def checkInputDataTypes(): TypeCheckResult =
TypeUtils.checkForNumericExpr(child.dataType, "function average")
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.types._

/**
* Base class for all counting aggregators.
*/
abstract class CountLike extends DeclarativeAggregate {
// scalastyle:off line.size.limit
@ExpressionDescription(
usage = """
_FUNC_(*) - Returns the total number of retrieved rows, including rows containing null.
_FUNC_(expr[, expr...]) - Returns the number of rows for which the supplied expression(s) are all non-null.
_FUNC_(DISTINCT expr[, expr...]) - Returns the number of rows for which the supplied expression(s) are unique and non-null.
""")
// scalastyle:on line.size.limit
case class Count(children: Seq[Expression]) extends DeclarativeAggregate {
override def nullable: Boolean = false

// Return data type.
Expand All @@ -45,19 +52,6 @@ abstract class CountLike extends DeclarativeAggregate {
override lazy val evaluateExpression = count

override def defaultResult: Option[Literal] = Option(Literal(0L))
}

// scalastyle:off line.size.limit
@ExpressionDescription(
usage = """
_FUNC_(*) - Returns the total number of retrieved rows, including rows containing null.
_FUNC_(expr[, expr...]) - Returns the number of rows for which the supplied expression(s) are all non-null.
_FUNC_(DISTINCT expr[, expr...]) - Returns the number of rows for which the supplied expression(s) are unique and non-null.
""")
// scalastyle:on line.size.limit
case class Count(children: Seq[Expression]) extends CountLike {

override lazy val updateExpressions = {
val nullableChildren = children.filter(_.nullable)
Expand Down

0 comments on commit 5e5d886

Please sign in to comment.