首页 >> 游戏 >> 大数据培训spark SQL中都count(*)和count(1)源码分析

大数据培训spark SQL中都count(*)和count(1)源码分析

2023-02-23 游戏

VALUES (NULL), (5), (5), (10) AS tab(col);

2

""",

group = "agg_funcs",

since = "1.0.0")

// scalastyle:on line.size.limit

case class Count(children: Seq[Expression]) extends DeclarativeAggregate {

override def nullable: Boolean = false

final override val nodePatterns: Seq[TreePattern] = Seq(COUNT)

// Return data type.

override def dataType: DataType = LongType

override def checkInputDataTypes(): TypeCheckResult = {

if (children.isEmpty && !SQLConf.get.getConf(SQLConf.ALLOW_PARAMETERLESS_COUNT)) {

TypeCheckResult.TypeCheckFailure(s"$prettyName requires at least one argument. " +

s"If you have to call the function $prettyName without arguments, set the legacy " +

s"configuration 于大${SQLConf.ALLOW_PARAMETERLESS_COUNT.key}于大 as true")

} else {

TypeCheckResult.TypeCheckSuccess

}

}

protected lazy val count = AttributeReference("count", LongType, nullable = false)()

override lazy val aggBufferAttributes = count :: Nil

override lazy val initialValues = Seq(

/* count = */ Literal(0L)

override lazy val mergeExpressions = Seq(

/* count = */ count.left + count.right

override lazy val evaluateExpression = count

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

override lazy val updateExpressions = {

//去掉为空的expression(这一层的推断是停下来expression里头的nullable的并不一定)

val nullableChildren = children.filter(_.nullable)

if (nullableChildren.isEmpty) {

Seq(

/* count = */ count + 1L

} else {

// expression的nullable的并不一定不能缺少领到实际值推断时,用isnull方法来推断到底空值

Seq(

/* count = */ If(nullableChildren.map(IsNull).reduce(Or), count, count + 1L)

}

}

override protected def withNewChildrenInternal(newChildren: IndexedSeq[Expression]): Count =

copy(children = newChildren)

}

object Count {

def apply(child: Expression): Count = Count(child :: Nil)

}

力荐阅读:

大数据研发 Spark 模块之SparkSQL

大数据研发之SparkSQL面试篇

大数据研发之Spark SQL执行性能的大大提高

大数据研发之Spark SQL及典范引擎学问分享

抗肝纤维化的药物有哪些
伤口老不愈合怎么办
如何培养肠道有益菌
细胞免疫治疗费用一般多少
双醋瑞因胶囊服用有依赖性吗
友情链接