|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.catalyst.expressions.aggregate |
| 19 | + |
| 20 | +import scala.collection.immutable.HashMap |
| 21 | + |
| 22 | +import org.apache.spark.sql.catalyst.InternalRow |
| 23 | +import org.apache.spark.sql.catalyst.expressions._ |
| 24 | +import org.apache.spark.sql.catalyst.util.GenericArrayData |
| 25 | +import org.apache.spark.sql.types._ |
| 26 | + |
| 27 | +object PivotFirst { |
| 28 | + |
| 29 | + def supportsDataType(dataType: DataType): Boolean = updateFunction.isDefinedAt(dataType) |
| 30 | + |
| 31 | + // Currently UnsafeRow does not support the generic update method (throws |
| 32 | + // UnsupportedOperationException), so we need to explicitly support each DataType. |
| 33 | + private val updateFunction: PartialFunction[DataType, (MutableRow, Int, Any) => Unit] = { |
| 34 | + case DoubleType => |
| 35 | + (row, offset, value) => row.setDouble(offset, value.asInstanceOf[Double]) |
| 36 | + case IntegerType => |
| 37 | + (row, offset, value) => row.setInt(offset, value.asInstanceOf[Int]) |
| 38 | + case LongType => |
| 39 | + (row, offset, value) => row.setLong(offset, value.asInstanceOf[Long]) |
| 40 | + case FloatType => |
| 41 | + (row, offset, value) => row.setFloat(offset, value.asInstanceOf[Float]) |
| 42 | + case BooleanType => |
| 43 | + (row, offset, value) => row.setBoolean(offset, value.asInstanceOf[Boolean]) |
| 44 | + case ShortType => |
| 45 | + (row, offset, value) => row.setShort(offset, value.asInstanceOf[Short]) |
| 46 | + case ByteType => |
| 47 | + (row, offset, value) => row.setByte(offset, value.asInstanceOf[Byte]) |
| 48 | + case d: DecimalType => |
| 49 | + (row, offset, value) => row.setDecimal(offset, value.asInstanceOf[Decimal], d.precision) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * PivotFirst is a aggregate function used in the second phase of a two phase pivot to do the |
| 55 | + * required rearrangement of values into pivoted form. |
| 56 | + * |
| 57 | + * For example on an input of |
| 58 | + * A | B |
| 59 | + * --+-- |
| 60 | + * x | 1 |
| 61 | + * y | 2 |
| 62 | + * z | 3 |
| 63 | + * |
| 64 | + * with pivotColumn=A, valueColumn=B, and pivotColumnValues=[z,y] the output is [3,2]. |
| 65 | + * |
| 66 | + * @param pivotColumn column that determines which output position to put valueColumn in. |
| 67 | + * @param valueColumn the column that is being rearranged. |
| 68 | + * @param pivotColumnValues the list of pivotColumn values in the order of desired output. Values |
| 69 | + * not listed here will be ignored. |
| 70 | + */ |
| 71 | +case class PivotFirst( |
| 72 | + pivotColumn: Expression, |
| 73 | + valueColumn: Expression, |
| 74 | + pivotColumnValues: Seq[Any], |
| 75 | + mutableAggBufferOffset: Int = 0, |
| 76 | + inputAggBufferOffset: Int = 0) extends ImperativeAggregate { |
| 77 | + |
| 78 | + override val children: Seq[Expression] = pivotColumn :: valueColumn :: Nil |
| 79 | + |
| 80 | + override lazy val inputTypes: Seq[AbstractDataType] = children.map(_.dataType) |
| 81 | + |
| 82 | + override val nullable: Boolean = false |
| 83 | + |
| 84 | + val valueDataType = valueColumn.dataType |
| 85 | + |
| 86 | + override val dataType: DataType = ArrayType(valueDataType) |
| 87 | + |
| 88 | + val pivotIndex = HashMap(pivotColumnValues.zipWithIndex: _*) |
| 89 | + |
| 90 | + val indexSize = pivotIndex.size |
| 91 | + |
| 92 | + private val updateRow: (MutableRow, Int, Any) => Unit = PivotFirst.updateFunction(valueDataType) |
| 93 | + |
| 94 | + override def update(mutableAggBuffer: MutableRow, inputRow: InternalRow): Unit = { |
| 95 | + val pivotColValue = pivotColumn.eval(inputRow) |
| 96 | + if (pivotColValue != null) { |
| 97 | + // We ignore rows whose pivot column value is not in the list of pivot column values. |
| 98 | + val index = pivotIndex.getOrElse(pivotColValue, -1) |
| 99 | + if (index >= 0) { |
| 100 | + val value = valueColumn.eval(inputRow) |
| 101 | + if (value != null) { |
| 102 | + updateRow(mutableAggBuffer, mutableAggBufferOffset + index, value) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + override def merge(mutableAggBuffer: MutableRow, inputAggBuffer: InternalRow): Unit = { |
| 109 | + for (i <- 0 until indexSize) { |
| 110 | + if (!inputAggBuffer.isNullAt(inputAggBufferOffset + i)) { |
| 111 | + val value = inputAggBuffer.get(inputAggBufferOffset + i, valueDataType) |
| 112 | + updateRow(mutableAggBuffer, mutableAggBufferOffset + i, value) |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + override def initialize(mutableAggBuffer: MutableRow): Unit = valueDataType match { |
| 118 | + case d: DecimalType => |
| 119 | + // Per doc of setDecimal we need to do this instead of setNullAt for DecimalType. |
| 120 | + for (i <- 0 until indexSize) { |
| 121 | + mutableAggBuffer.setDecimal(mutableAggBufferOffset + i, null, d.precision) |
| 122 | + } |
| 123 | + case _ => |
| 124 | + for (i <- 0 until indexSize) { |
| 125 | + mutableAggBuffer.setNullAt(mutableAggBufferOffset + i) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + override def eval(input: InternalRow): Any = { |
| 130 | + val result = new Array[Any](indexSize) |
| 131 | + for (i <- 0 until indexSize) { |
| 132 | + result(i) = input.get(mutableAggBufferOffset + i, valueDataType) |
| 133 | + } |
| 134 | + new GenericArrayData(result) |
| 135 | + } |
| 136 | + |
| 137 | + override def withNewInputAggBufferOffset(newInputAggBufferOffset: Int): ImperativeAggregate = |
| 138 | + copy(inputAggBufferOffset = newInputAggBufferOffset) |
| 139 | + |
| 140 | + override def withNewMutableAggBufferOffset(newMutableAggBufferOffset: Int): ImperativeAggregate = |
| 141 | + copy(mutableAggBufferOffset = newMutableAggBufferOffset) |
| 142 | + |
| 143 | + |
| 144 | + override lazy val aggBufferAttributes: Seq[AttributeReference] = |
| 145 | + pivotIndex.toList.sortBy(_._2).map(kv => AttributeReference(kv._1.toString, valueDataType)()) |
| 146 | + |
| 147 | + override lazy val aggBufferSchema: StructType = StructType.fromAttributes(aggBufferAttributes) |
| 148 | + |
| 149 | + override lazy val inputAggBufferAttributes: Seq[AttributeReference] = |
| 150 | + aggBufferAttributes.map(_.newInstance()) |
| 151 | +} |
| 152 | + |
0 commit comments