|
| 1 | +/** |
| 2 | + * Copyright (c) 2016-2019 "Neo4j Sweden, AB" [https://neo4j.com] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * Attribution Notice under the terms of the Apache License 2.0 |
| 17 | + * |
| 18 | + * This work was created by the collective efforts of the openCypher community. |
| 19 | + * Without limiting the terms of Section 6, any Derivative Work that is not |
| 20 | + * approved by the public consensus process of the openCypher Implementers Group |
| 21 | + * should not be described as “Cypher” (and Cypher® is a registered trademark of |
| 22 | + * Neo4j Inc.) or as "openCypher". Extensions by implementers or prototypes or |
| 23 | + * proposals for change that have been documented or implemented should only be |
| 24 | + * described as "implementation extensions to Cypher" or as "proposed changes to |
| 25 | + * Cypher that are not yet approved by the openCypher community". |
| 26 | + */ |
| 27 | +package org.opencypher.morpheus.impl.temporal |
| 28 | + |
| 29 | +import org.apache.logging.log4j.scala.Logging |
| 30 | +import org.apache.spark.sql.Row |
| 31 | +import org.apache.spark.sql.expressions.{MutableAggregationBuffer, UserDefinedAggregateFunction} |
| 32 | +import org.apache.spark.sql.types.{CalendarIntervalType, DataType, LongType, StructField, StructType} |
| 33 | +import org.apache.spark.unsafe.types.CalendarInterval |
| 34 | +import org.opencypher.okapi.impl.temporal.TemporalConstants |
| 35 | +import org.opencypher.morpheus.impl.temporal.TemporalConversions._ |
| 36 | + |
| 37 | +object TemporalUdafs extends Logging { |
| 38 | + |
| 39 | + abstract class SimpleDurationAggregation(aggrName: String) extends UserDefinedAggregateFunction { |
| 40 | + override def inputSchema: StructType = StructType(Array(StructField("duration", CalendarIntervalType))) |
| 41 | + override def bufferSchema: StructType = StructType(Array(StructField(aggrName, CalendarIntervalType))) |
| 42 | + override def dataType: DataType = CalendarIntervalType |
| 43 | + override def deterministic: Boolean = true |
| 44 | + override def initialize(buffer: MutableAggregationBuffer): Unit = { |
| 45 | + buffer(0) = new CalendarInterval(0, 0L) |
| 46 | + } |
| 47 | + override def evaluate(buffer: Row): Any = buffer.getAs[CalendarInterval](0) |
| 48 | + } |
| 49 | + |
| 50 | + class DurationSum extends SimpleDurationAggregation("sum") { |
| 51 | + override def update(buffer: MutableAggregationBuffer, input: Row): Unit = { |
| 52 | + buffer(0) = buffer.getAs[CalendarInterval](0).add(input.getAs[CalendarInterval](0)) |
| 53 | + } |
| 54 | + override def merge(buffer1: MutableAggregationBuffer, buffer2: Row): Unit = { |
| 55 | + buffer1(0) = buffer2.getAs[CalendarInterval](0).add(buffer1.getAs[CalendarInterval](0)) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + class DurationMax extends SimpleDurationAggregation("max") { |
| 60 | + override def update(buffer: MutableAggregationBuffer, input: Row): Unit = { |
| 61 | + val currMaxInterval = buffer.getAs[CalendarInterval](0) |
| 62 | + val inputInterval = input.getAs[CalendarInterval](0) |
| 63 | + buffer(0) = if (currMaxInterval.toDuration.compare(inputInterval.toDuration) >= 0) currMaxInterval else inputInterval |
| 64 | + } |
| 65 | + override def merge(buffer1: MutableAggregationBuffer, buffer2: Row): Unit = { |
| 66 | + val interval1 = buffer1.getAs[CalendarInterval](0) |
| 67 | + val interval2 = buffer2.getAs[CalendarInterval](0) |
| 68 | + buffer1(0) = if (interval1.toDuration.compare(interval2.toDuration) >= 0) interval1 else interval2 |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + class DurationMin extends SimpleDurationAggregation("min") { |
| 73 | + override def initialize(buffer: MutableAggregationBuffer): Unit = { |
| 74 | + buffer(0) = new CalendarInterval(Integer.MAX_VALUE, Long.MaxValue) |
| 75 | + } |
| 76 | + override def update(buffer: MutableAggregationBuffer, input: Row): Unit = { |
| 77 | + val currMinInterval = buffer.getAs[CalendarInterval](0) |
| 78 | + val inputInterval = input.getAs[CalendarInterval](0) |
| 79 | + buffer(0) = if (inputInterval.toDuration.compare(currMinInterval.toDuration) >= 0) currMinInterval else inputInterval |
| 80 | + } |
| 81 | + override def merge(buffer1: MutableAggregationBuffer, buffer2: Row): Unit = { |
| 82 | + val interval1 = buffer1.getAs[CalendarInterval](0) |
| 83 | + val interval2 = buffer2.getAs[CalendarInterval](0) |
| 84 | + buffer1(0) = if (interval2.toDuration.compare(interval1.toDuration) >= 0) interval1 else interval2 |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + class DurationAvg extends UserDefinedAggregateFunction { |
| 89 | + override def inputSchema: StructType = StructType(Array(StructField("duration", CalendarIntervalType))) |
| 90 | + override def bufferSchema: StructType = StructType(Array(StructField("sum", CalendarIntervalType), StructField("cnt", LongType))) |
| 91 | + override def dataType: DataType = CalendarIntervalType |
| 92 | + override def deterministic: Boolean = true |
| 93 | + override def initialize(buffer: MutableAggregationBuffer): Unit = { |
| 94 | + buffer(0) = new CalendarInterval(0, 0L) |
| 95 | + buffer(1) = 0L |
| 96 | + } |
| 97 | + override def update(buffer: MutableAggregationBuffer, input: Row): Unit = { |
| 98 | + buffer(0) = buffer.getAs[CalendarInterval](0).add(input.getAs[CalendarInterval](0)) |
| 99 | + buffer(1) = buffer.getLong(1) + 1 |
| 100 | + } |
| 101 | + override def merge(buffer1: MutableAggregationBuffer, buffer2: Row): Unit = { |
| 102 | + buffer1(0) = buffer2.getAs[CalendarInterval](0).add(buffer1.getAs[CalendarInterval](0)) |
| 103 | + buffer1(1) = buffer1.getLong(1) + buffer2.getLong(1) |
| 104 | + } |
| 105 | + override def evaluate(buffer: Row): Any = { |
| 106 | + val sumInterval = buffer.getAs[CalendarInterval](0) |
| 107 | + val cnt = buffer.getLong(1) |
| 108 | + new CalendarInterval((sumInterval.months / cnt).toInt, sumInterval.microseconds / cnt) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + val durationSum = new DurationSum() |
| 113 | + val durationAvg = new DurationAvg() |
| 114 | + val durationMin = new DurationMin() |
| 115 | + val durationMax = new DurationMax() |
| 116 | +} |
0 commit comments