Github user yhuai commented on a diff in the pull request:
https://github.com/apache/spark/pull/11583#discussion_r57541231
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/PivotFirst.scala
---
@@ -0,0 +1,141 @@
+/*
+ * 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 scala.collection.immutable.HashMap
+
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.expressions._
+import org.apache.spark.sql.catalyst.util.GenericArrayData
+import org.apache.spark.sql.types._
+
+object PivotFirst {
+
+ def supportsDataType(dataType: DataType): Boolean = {
+ try {
+ updateFunction(dataType)
+ true
+ } catch {
+ case _: UnsupportedOperationException => false
+ }
+ }
+
+ // Currently UnsafeRow does not support the generic update method (throws
+ // UnsupportedOperationException), so we need to explicitly support each
DataType.
+ private def updateFunction(dataType: DataType): (MutableRow, Int, Any)
=> Unit = dataType match {
+ case DoubleType =>
+ (row, offset, value) => row.setDouble(offset,
value.asInstanceOf[Double])
+ case IntegerType =>
+ (row, offset, value) => row.setInt(offset, value.asInstanceOf[Int])
+ case LongType =>
+ (row, offset, value) => row.setLong(offset, value.asInstanceOf[Long])
+ case FloatType =>
+ (row, offset, value) => row.setFloat(offset,
value.asInstanceOf[Float])
+ case BooleanType =>
+ (row, offset, value) => row.setBoolean(offset,
value.asInstanceOf[Boolean])
+ case ShortType =>
+ (row, offset, value) => row.setShort(offset,
value.asInstanceOf[Short])
+ case ByteType =>
+ (row, offset, value) => row.setByte(offset, value.asInstanceOf[Byte])
+ case d: DecimalType =>
+ (row, offset, value) => row.setDecimal(offset,
value.asInstanceOf[Decimal], d.precision)
+ case _ => throw new UnsupportedOperationException(
+ s"Unsupported datatype ($dataType) used in PivotFirst, this is a
bug."
+ )
+ }
+}
+
+case class PivotFirst(pivotColumn: Expression,
+ valueColumn: Expression,
+ pivotColumnValues: Seq[Any],
+ mutableAggBufferOffset: Int = 0,
+ inputAggBufferOffset: Int = 0) extends
ImperativeAggregate {
+
+ val pivotIndex = HashMap(pivotColumnValues.zipWithIndex: _*)
+
+ val valueDataType = valueColumn.dataType
+
+ val indexSize = pivotIndex.size
+
+ private val updateRow: (MutableRow, Int, Any) => Unit =
PivotFirst.updateFunction(valueDataType)
+
+ override def update(mutableAggBuffer: MutableRow, inputRow:
InternalRow): Unit = {
+ val pivotColValue = pivotColumn.eval(inputRow)
+ if (pivotColValue != null) {
+ val index = pivotIndex.getOrElse(pivotColValue, -1)
+ if (index >= 0) {
+ val value = valueColumn.eval(inputRow)
+ if (value != null) {
+ updateRow(mutableAggBuffer, mutableAggBufferOffset + index,
value)
+ }
+ }
+ }
+ }
+
+ override def merge(mutableAggBuffer: MutableRow, inputAggBuffer:
InternalRow): Unit = {
+ for (i <- 0 until indexSize) {
+ if (!inputAggBuffer.isNullAt(inputAggBufferOffset + i)) {
+ val value = inputAggBuffer.get(inputAggBufferOffset + i,
valueDataType)
+ updateRow(mutableAggBuffer, mutableAggBufferOffset + i, value)
+ }
+ }
+ }
+
+ override def initialize(mutableAggBuffer: MutableRow): Unit =
valueDataType match {
+ case d: DecimalType =>
+ for (i <- 0 until indexSize) {
+ mutableAggBuffer.setDecimal(mutableAggBufferOffset + i, null,
d.precision)
+ }
+ case _ =>
+ for (i <- 0 until indexSize) {
+ mutableAggBuffer.setNullAt(mutableAggBufferOffset + i)
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val result = new Array[Any](indexSize)
+ for (i <- 0 until indexSize) {
+ result(i) = input.get(mutableAggBufferOffset + i, valueDataType)
+ }
+ new GenericArrayData(result)
+ }
+
+ override def withNewInputAggBufferOffset(newInputAggBufferOffset: Int):
ImperativeAggregate =
+ copy(inputAggBufferOffset = newInputAggBufferOffset)
+
+ override def withNewMutableAggBufferOffset(newMutableAggBufferOffset:
Int): ImperativeAggregate =
+ copy(mutableAggBufferOffset = newMutableAggBufferOffset)
+
+
+ override lazy val aggBufferAttributes: Seq[AttributeReference] =
+ pivotIndex.toList.sortBy(_._2).map(kv =>
AttributeReference(kv._1.toString, valueDataType)())
+
+ override lazy val aggBufferSchema: StructType =
StructType.fromAttributes(aggBufferAttributes)
+
+ override lazy val inputAggBufferAttributes: Seq[AttributeReference] =
+ aggBufferAttributes.map(_.newInstance())
+
+ override lazy val inputTypes: Seq[AbstractDataType] =
children.map(_.dataType)
+
+ override val nullable: Boolean = false
+
+ override val dataType: DataType = ArrayType(valueDataType)
+
+ override val children: Seq[Expression] = pivotColumn :: valueColumn ::
Nil
--- End diff --
(I feel it will be better for readers if we can put `inputTypes`,
`nullable`, `dataType`, and `children` at the beginning o the class body. )
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]