Skip to content

Latest commit

 

History

History
168 lines (153 loc) · 6.2 KB

File metadata and controls

168 lines (153 loc) · 6.2 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
* 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
Jun 24, 2016
Jun 24, 2016
20
import org.apache.commons.lang3.StringUtils
Apr 5, 2016
Apr 5, 2016
22
import org.apache.spark.sql.AnalysisException
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.TypeCheckFailure
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode}
import org.apache.spark.sql.types._
import org.apache.spark.unsafe.types.CalendarInterval
case class TimeWindow(
timeColumn: Expression,
windowDuration: Long,
slideDuration: Long,
startTime: Long) extends UnaryExpression
with ImplicitCastInputTypes
with Unevaluable
with NonSQLExpression {
Apr 5, 2016
Apr 5, 2016
38
39
40
41
42
43
44
45
46
47
//////////////////////////
// SQL Constructors
//////////////////////////
def this(
timeColumn: Expression,
windowDuration: Expression,
slideDuration: Expression,
startTime: Expression) = {
this(timeColumn, TimeWindow.parseExpression(windowDuration),
Aug 2, 2016
Aug 2, 2016
48
TimeWindow.parseExpression(slideDuration), TimeWindow.parseExpression(startTime))
Apr 5, 2016
Apr 5, 2016
49
50
51
52
}
def this(timeColumn: Expression, windowDuration: Expression, slideDuration: Expression) = {
this(timeColumn, TimeWindow.parseExpression(windowDuration),
Aug 2, 2016
Aug 2, 2016
53
TimeWindow.parseExpression(slideDuration), 0)
Apr 5, 2016
Apr 5, 2016
54
55
56
57
58
59
}
def this(timeColumn: Expression, windowDuration: Expression) = {
this(timeColumn, windowDuration, windowDuration)
}
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
override def child: Expression = timeColumn
override def inputTypes: Seq[AbstractDataType] = Seq(TimestampType)
override def dataType: DataType = new StructType()
.add(StructField("start", TimestampType))
.add(StructField("end", TimestampType))
// This expression is replaced in the analyzer.
override lazy val resolved = false
/**
* Validate the inputs for the window duration, slide duration, and start time in addition to
* the input data type.
*/
override def checkInputDataTypes(): TypeCheckResult = {
val dataTypeCheck = super.checkInputDataTypes()
if (dataTypeCheck.isSuccess) {
if (windowDuration <= 0) {
return TypeCheckFailure(s"The window duration ($windowDuration) must be greater than 0.")
}
if (slideDuration <= 0) {
return TypeCheckFailure(s"The slide duration ($slideDuration) must be greater than 0.")
}
if (startTime < 0) {
return TypeCheckFailure(s"The start time ($startTime) must be greater than or equal to 0.")
}
if (slideDuration > windowDuration) {
return TypeCheckFailure(s"The slide duration ($slideDuration) must be less than or equal" +
s" to the windowDuration ($windowDuration).")
}
if (startTime >= slideDuration) {
return TypeCheckFailure(s"The start time ($startTime) must be less than the " +
s"slideDuration ($slideDuration).")
}
}
dataTypeCheck
}
}
object TimeWindow {
/**
* Parses the interval string for a valid time duration. CalendarInterval expects interval
* strings to start with the string `interval`. For usability, we prepend `interval` to the string
* if the user omitted it.
*
* @param interval The interval string
* @return The interval duration in microseconds. SparkSQL casts TimestampType has microsecond
* precision.
*/
private def getIntervalInMicroSeconds(interval: String): Long = {
if (StringUtils.isBlank(interval)) {
throw new IllegalArgumentException(
"The window duration, slide duration and start time cannot be null or blank.")
}
val intervalString = if (interval.startsWith("interval")) {
interval
} else {
"interval " + interval
}
val cal = CalendarInterval.fromString(intervalString)
if (cal == null) {
throw new IllegalArgumentException(
s"The provided interval ($interval) did not correspond to a valid interval string.")
}
if (cal.months > 0) {
throw new IllegalArgumentException(
s"Intervals greater than a month is not supported ($interval).")
}
cal.microseconds
}
Apr 5, 2016
Apr 5, 2016
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Parses the duration expression to generate the long value for the original constructor so
* that we can use `window` in SQL.
*/
private def parseExpression(expr: Expression): Long = expr match {
case NonNullLiteral(s, StringType) => getIntervalInMicroSeconds(s.toString)
case IntegerLiteral(i) => i.toLong
case NonNullLiteral(l, LongType) => l.toString.toLong
case _ => throw new AnalysisException("The duration and time inputs to window must be " +
"an integer, long or string literal.")
}
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
def apply(
timeColumn: Expression,
windowDuration: String,
slideDuration: String,
startTime: String): TimeWindow = {
TimeWindow(timeColumn,
getIntervalInMicroSeconds(windowDuration),
getIntervalInMicroSeconds(slideDuration),
getIntervalInMicroSeconds(startTime))
}
}
/**
* Expression used internally to convert the TimestampType to Long without losing
* precision, i.e. in microseconds. Used in time windowing.
*/
case class PreciseTimestamp(child: Expression) extends UnaryExpression with ExpectsInputTypes {
override def inputTypes: Seq[AbstractDataType] = Seq(TimestampType)
override def dataType: DataType = LongType
Apr 19, 2016
Apr 19, 2016
161
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
Apr 18, 2016
Apr 18, 2016
162
val eval = child.genCode(ctx)
Apr 19, 2016
Apr 19, 2016
163
ev.copy(code = eval.code +
164
165
s"""boolean ${ev.isNull} = ${eval.isNull};
|${ctx.javaType(dataType)} ${ev.value} = ${eval.value};
Apr 19, 2016
Apr 19, 2016
166
""".stripMargin)