spark Mode 源码
spark Mode 代码
文件路径:/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/Mode.scala
/*
* 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.catalyst.expressions.aggregate
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.{Expression, ExpressionDescription, ImplicitCastInputTypes}
import org.apache.spark.sql.catalyst.trees.UnaryLike
import org.apache.spark.sql.catalyst.util.GenericArrayData
import org.apache.spark.sql.types.{AbstractDataType, AnyDataType, ArrayType, DataType}
import org.apache.spark.util.collection.OpenHashMap
// scalastyle:off line.size.limit
@ExpressionDescription(
usage = "_FUNC_(col) - Returns the most frequent value for the values within `col`. NULL values are ignored. If all the values are NULL, or there are 0 rows, returns NULL.",
examples = """
Examples:
> SELECT _FUNC_(col) FROM VALUES (0), (10), (10) AS tab(col);
10
> SELECT _FUNC_(col) FROM VALUES (INTERVAL '0' MONTH), (INTERVAL '10' MONTH), (INTERVAL '10' MONTH) AS tab(col);
0-10
> SELECT _FUNC_(col) FROM VALUES (0), (10), (10), (null), (null), (null) AS tab(col);
10
""",
group = "agg_funcs",
since = "3.4.0")
// scalastyle:on line.size.limit
case class Mode(
child: Expression,
mutableAggBufferOffset: Int = 0,
inputAggBufferOffset: Int = 0) extends TypedAggregateWithHashMapAsBuffer
with ImplicitCastInputTypes with UnaryLike[Expression] {
def this(child: Expression) = this(child, 0, 0)
// Returns null for empty inputs
override def nullable: Boolean = true
override def dataType: DataType = child.dataType
override def inputTypes: Seq[AbstractDataType] = Seq(AnyDataType)
override def prettyName: String = "mode"
override def update(
buffer: OpenHashMap[AnyRef, Long],
input: InternalRow): OpenHashMap[AnyRef, Long] = {
val key = child.eval(input).asInstanceOf[AnyRef]
if (key != null) {
buffer.changeValue(key, 1L, _ + 1L)
}
buffer
}
override def merge(
buffer: OpenHashMap[AnyRef, Long],
other: OpenHashMap[AnyRef, Long]): OpenHashMap[AnyRef, Long] = {
other.foreach { case (key, count) =>
buffer.changeValue(key, count, _ + count)
}
buffer
}
override def eval(buffer: OpenHashMap[AnyRef, Long]): Any = {
if (buffer.isEmpty) {
return null
}
buffer.maxBy(_._2)._1
}
override def withNewMutableAggBufferOffset(newMutableAggBufferOffset: Int): Mode =
copy(mutableAggBufferOffset = newMutableAggBufferOffset)
override def withNewInputAggBufferOffset(newInputAggBufferOffset: Int): Mode =
copy(inputAggBufferOffset = newInputAggBufferOffset)
override protected def withNewChildInternal(newChild: Expression): Expression =
copy(child = newChild)
}
/**
* Mode in Pandas' fashion. This expression is dedicated only for Pandas API on Spark.
* It has two main difference from `Mode`:
* 1, it accepts NULLs when `ignoreNA` is False;
* 2, it returns all the modes for a multimodal dataset;
*/
case class PandasMode(
child: Expression,
ignoreNA: Boolean = true,
mutableAggBufferOffset: Int = 0,
inputAggBufferOffset: Int = 0) extends TypedAggregateWithHashMapAsBuffer
with ImplicitCastInputTypes with UnaryLike[Expression] {
def this(child: Expression) = this(child, true, 0, 0)
// Returns empty array for empty inputs
override def nullable: Boolean = false
override def dataType: DataType = ArrayType(child.dataType, containsNull = !ignoreNA)
override def inputTypes: Seq[AbstractDataType] = Seq(AnyDataType)
override def prettyName: String = "pandas_mode"
override def update(
buffer: OpenHashMap[AnyRef, Long],
input: InternalRow): OpenHashMap[AnyRef, Long] = {
val key = child.eval(input).asInstanceOf[AnyRef]
if (key != null || !ignoreNA) {
buffer.changeValue(key, 1L, _ + 1L)
}
buffer
}
override def merge(
buffer: OpenHashMap[AnyRef, Long],
other: OpenHashMap[AnyRef, Long]): OpenHashMap[AnyRef, Long] = {
other.foreach { case (key, count) =>
buffer.changeValue(key, count, _ + count)
}
buffer
}
override def eval(buffer: OpenHashMap[AnyRef, Long]): Any = {
if (buffer.isEmpty) {
return new GenericArrayData(Seq.empty)
}
val modes = collection.mutable.ArrayBuffer.empty[AnyRef]
var maxCount = -1L
val iter = buffer.iterator
while (iter.hasNext) {
val (key, count) = iter.next()
if (maxCount < count) {
modes.clear()
modes.append(key)
maxCount = count
} else if (maxCount == count) {
modes.append(key)
}
}
new GenericArrayData(modes.toSeq)
}
override def withNewMutableAggBufferOffset(newMutableAggBufferOffset: Int): PandasMode =
copy(mutableAggBufferOffset = newMutableAggBufferOffset)
override def withNewInputAggBufferOffset(newInputAggBufferOffset: Int): PandasMode =
copy(inputAggBufferOffset = newInputAggBufferOffset)
override protected def withNewChildInternal(newChild: Expression): Expression =
copy(child = newChild)
}
相关信息
相关文章
spark ApproxCountDistinctForIntervals 源码
0
赞
- 所属分类: 前端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
7、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦