/
intervalExpressions.scala
440 lines (381 loc) · 17.6 KB
/
intervalExpressions.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* 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
import java.math.RoundingMode
import java.util.Locale
import com.google.common.math.{DoubleMath, IntMath, LongMath}
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, CodeGenerator, ExprCode}
import org.apache.spark.sql.catalyst.util.IntervalUtils
import org.apache.spark.sql.catalyst.util.IntervalUtils._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types._
import org.apache.spark.unsafe.types.CalendarInterval
abstract class ExtractIntervalPart(
child: Expression,
val dataType: DataType,
func: CalendarInterval => Any,
funcName: String)
extends UnaryExpression with ExpectsInputTypes with NullIntolerant with Serializable {
override def inputTypes: Seq[AbstractDataType] = Seq(CalendarIntervalType)
override protected def nullSafeEval(interval: Any): Any = {
func(interval.asInstanceOf[CalendarInterval])
}
override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
val iu = IntervalUtils.getClass.getName.stripSuffix("$")
defineCodeGen(ctx, ev, c => s"$iu.$funcName($c)")
}
}
case class ExtractIntervalYears(child: Expression)
extends ExtractIntervalPart(child, IntegerType, getYears, "getYears")
case class ExtractIntervalMonths(child: Expression)
extends ExtractIntervalPart(child, ByteType, getMonths, "getMonths")
case class ExtractIntervalDays(child: Expression)
extends ExtractIntervalPart(child, IntegerType, getDays, "getDays")
case class ExtractIntervalHours(child: Expression)
extends ExtractIntervalPart(child, LongType, getHours, "getHours")
case class ExtractIntervalMinutes(child: Expression)
extends ExtractIntervalPart(child, ByteType, getMinutes, "getMinutes")
case class ExtractIntervalSeconds(child: Expression)
extends ExtractIntervalPart(child, DecimalType(8, 6), getSeconds, "getSeconds")
object ExtractIntervalPart {
def parseExtractField(
extractField: String,
source: Expression,
errorHandleFunc: => Nothing): Expression = extractField.toUpperCase(Locale.ROOT) match {
case "YEAR" | "Y" | "YEARS" | "YR" | "YRS" => ExtractIntervalYears(source)
case "MONTH" | "MON" | "MONS" | "MONTHS" => ExtractIntervalMonths(source)
case "DAY" | "D" | "DAYS" => ExtractIntervalDays(source)
case "HOUR" | "H" | "HOURS" | "HR" | "HRS" => ExtractIntervalHours(source)
case "MINUTE" | "M" | "MIN" | "MINS" | "MINUTES" => ExtractIntervalMinutes(source)
case "SECOND" | "S" | "SEC" | "SECONDS" | "SECS" => ExtractIntervalSeconds(source)
case _ => errorHandleFunc
}
}
abstract class IntervalNumOperation(
interval: Expression,
num: Expression)
extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant with Serializable {
override def left: Expression = interval
override def right: Expression = num
protected val operation: (CalendarInterval, Double) => CalendarInterval
protected def operationName: String
override def inputTypes: Seq[AbstractDataType] = Seq(CalendarIntervalType, DoubleType)
override def dataType: DataType = CalendarIntervalType
override def nullable: Boolean = true
override def nullSafeEval(interval: Any, num: Any): Any = {
operation(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
}
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
val iu = IntervalUtils.getClass.getName.stripSuffix("$")
defineCodeGen(ctx, ev, (interval, num) => s"$iu.$operationName($interval, $num)")
}
override def prettyName: String = operationName.stripSuffix("Exact") + "_interval"
}
case class MultiplyInterval(
interval: Expression,
num: Expression,
failOnError: Boolean = SQLConf.get.ansiEnabled)
extends IntervalNumOperation(interval, num) {
override protected val operation: (CalendarInterval, Double) => CalendarInterval =
if (failOnError) multiplyExact else multiply
override protected def operationName: String = if (failOnError) "multiplyExact" else "multiply"
}
case class DivideInterval(
interval: Expression,
num: Expression,
failOnError: Boolean = SQLConf.get.ansiEnabled)
extends IntervalNumOperation(interval, num) {
override protected val operation: (CalendarInterval, Double) => CalendarInterval =
if (failOnError) divideExact else divide
override protected def operationName: String = if (failOnError) "divideExact" else "divide"
}
// scalastyle:off line.size.limit
@ExpressionDescription(
usage = "_FUNC_(years, months, weeks, days, hours, mins, secs) - Make interval from years, months, weeks, days, hours, mins and secs.",
arguments = """
Arguments:
* years - the number of years, positive or negative
* months - the number of months, positive or negative
* weeks - the number of weeks, positive or negative
* days - the number of days, positive or negative
* hours - the number of hours, positive or negative
* mins - the number of minutes, positive or negative
* secs - the number of seconds with the fractional part in microsecond precision.
""",
examples = """
Examples:
> SELECT _FUNC_(100, 11, 1, 1, 12, 30, 01.001001);
100 years 11 months 8 days 12 hours 30 minutes 1.001001 seconds
> SELECT _FUNC_(100, null, 3);
NULL
> SELECT _FUNC_(0, 1, 0, 1, 0, 0, 100.000001);
1 months 1 days 1 minutes 40.000001 seconds
""",
since = "3.0.0",
group = "datetime_funcs")
// scalastyle:on line.size.limit
case class MakeInterval(
years: Expression,
months: Expression,
weeks: Expression,
days: Expression,
hours: Expression,
mins: Expression,
secs: Expression,
failOnError: Boolean = SQLConf.get.ansiEnabled)
extends SeptenaryExpression with ImplicitCastInputTypes with NullIntolerant {
def this(
years: Expression,
months: Expression,
weeks: Expression,
days: Expression,
hours: Expression,
mins: Expression,
sec: Expression) = {
this(years, months, weeks, days, hours, mins, sec, SQLConf.get.ansiEnabled)
}
def this(
years: Expression,
months: Expression,
weeks: Expression,
days: Expression,
hours: Expression,
mins: Expression) = {
this(years, months, weeks, days, hours, mins, Literal(Decimal(0, Decimal.MAX_LONG_DIGITS, 6)),
SQLConf.get.ansiEnabled)
}
def this(
years: Expression,
months: Expression,
weeks: Expression,
days: Expression,
hours: Expression) = {
this(years, months, weeks, days, hours, Literal(0))
}
def this(years: Expression, months: Expression, weeks: Expression, days: Expression) =
this(years, months, weeks, days, Literal(0))
def this(years: Expression, months: Expression, weeks: Expression) =
this(years, months, weeks, Literal(0))
def this(years: Expression, months: Expression) = this(years, months, Literal(0))
def this(years: Expression) = this(years, Literal(0))
def this() = this(Literal(0))
override def children: Seq[Expression] = Seq(years, months, weeks, days, hours, mins, secs)
// Accept `secs` as DecimalType to avoid loosing precision of microseconds while converting
// them to the fractional part of `secs`.
override def inputTypes: Seq[AbstractDataType] = Seq(IntegerType, IntegerType, IntegerType,
IntegerType, IntegerType, IntegerType, DecimalType(Decimal.MAX_LONG_DIGITS, 6))
override def dataType: DataType = CalendarIntervalType
override def nullable: Boolean = if (failOnError) children.exists(_.nullable) else true
override def nullSafeEval(
year: Any,
month: Any,
week: Any,
day: Any,
hour: Any,
min: Any,
sec: Option[Any]): Any = {
try {
IntervalUtils.makeInterval(
year.asInstanceOf[Int],
month.asInstanceOf[Int],
week.asInstanceOf[Int],
day.asInstanceOf[Int],
hour.asInstanceOf[Int],
min.asInstanceOf[Int],
sec.map(_.asInstanceOf[Decimal]).getOrElse(Decimal(0, Decimal.MAX_LONG_DIGITS, 6)))
} catch {
case _: ArithmeticException if !failOnError => null
}
}
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
nullSafeCodeGen(ctx, ev, (year, month, week, day, hour, min, sec) => {
val iu = IntervalUtils.getClass.getName.stripSuffix("$")
val secFrac = sec.getOrElse("0")
val failOnErrorBranch = if (failOnError) "throw e;" else s"${ev.isNull} = true;"
s"""
try {
${ev.value} = $iu.makeInterval($year, $month, $week, $day, $hour, $min, $secFrac);
} catch (java.lang.ArithmeticException e) {
$failOnErrorBranch
}
"""
})
}
override def prettyName: String = "make_interval"
}
// Multiply an year-month interval by a numeric
case class MultiplyYMInterval(
interval: Expression,
num: Expression)
extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant with Serializable {
override def left: Expression = interval
override def right: Expression = num
override def inputTypes: Seq[AbstractDataType] = Seq(YearMonthIntervalType, NumericType)
override def dataType: DataType = YearMonthIntervalType
@transient
private lazy val evalFunc: (Int, Any) => Any = right.dataType match {
case ByteType | ShortType | IntegerType => (months: Int, num) =>
Math.multiplyExact(months, num.asInstanceOf[Number].intValue())
case LongType => (months: Int, num) =>
Math.toIntExact(Math.multiplyExact(months, num.asInstanceOf[Long]))
case FloatType | DoubleType => (months: Int, num) =>
DoubleMath.roundToInt(months * num.asInstanceOf[Number].doubleValue(), RoundingMode.HALF_UP)
case _: DecimalType => (months: Int, num) =>
val decimalRes = ((new Decimal).set(months) * num.asInstanceOf[Decimal]).toJavaBigDecimal
decimalRes.setScale(0, java.math.RoundingMode.HALF_UP).intValueExact()
}
override def nullSafeEval(interval: Any, num: Any): Any = {
evalFunc(interval.asInstanceOf[Int], num)
}
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = right.dataType match {
case ByteType | ShortType | IntegerType =>
defineCodeGen(ctx, ev, (m, n) => s"java.lang.Math.multiplyExact($m, $n)")
case LongType =>
val jlm = classOf[Math].getName
defineCodeGen(ctx, ev, (m, n) => s"$jlm.toIntExact($jlm.multiplyExact($m, $n))")
case FloatType | DoubleType =>
val dm = classOf[DoubleMath].getName
defineCodeGen(ctx, ev, (m, n) =>
s"$dm.roundToInt($m * (double)$n, java.math.RoundingMode.HALF_UP)")
case _: DecimalType =>
defineCodeGen(ctx, ev, (m, n) =>
s"((new Decimal()).set($m).$$times($n)).toJavaBigDecimal()" +
".setScale(0, java.math.RoundingMode.HALF_UP).intValueExact()")
}
override def toString: String = s"($left * $right)"
}
// Multiply a day-time interval by a numeric
case class MultiplyDTInterval(
interval: Expression,
num: Expression)
extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant with Serializable {
override def left: Expression = interval
override def right: Expression = num
override def inputTypes: Seq[AbstractDataType] = Seq(DayTimeIntervalType, NumericType)
override def dataType: DataType = DayTimeIntervalType
@transient
private lazy val evalFunc: (Long, Any) => Any = right.dataType match {
case _: IntegralType => (micros: Long, num) =>
Math.multiplyExact(micros, num.asInstanceOf[Number].longValue())
case _: DecimalType => (micros: Long, num) =>
val decimalRes = ((new Decimal).set(micros) * num.asInstanceOf[Decimal]).toJavaBigDecimal
decimalRes.setScale(0, RoundingMode.HALF_UP).longValueExact()
case _: FractionalType => (micros: Long, num) =>
DoubleMath.roundToLong(micros * num.asInstanceOf[Number].doubleValue(), RoundingMode.HALF_UP)
}
override def nullSafeEval(interval: Any, num: Any): Any = {
evalFunc(interval.asInstanceOf[Long], num)
}
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = right.dataType match {
case _: IntegralType =>
defineCodeGen(ctx, ev, (m, n) => s"java.lang.Math.multiplyExact($m, $n)")
case _: DecimalType =>
defineCodeGen(ctx, ev, (m, n) =>
s"((new Decimal()).set($m).$$times($n)).toJavaBigDecimal()" +
".setScale(0, java.math.RoundingMode.HALF_UP).longValueExact()")
case _: FractionalType =>
val dm = classOf[DoubleMath].getName
defineCodeGen(ctx, ev, (m, n) =>
s"$dm.roundToLong($m * (double)$n, java.math.RoundingMode.HALF_UP)")
}
override def toString: String = s"($left * $right)"
}
// Divide an year-month interval by a numeric
case class DivideYMInterval(
interval: Expression,
num: Expression)
extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant with Serializable {
override def left: Expression = interval
override def right: Expression = num
override def inputTypes: Seq[AbstractDataType] = Seq(YearMonthIntervalType, NumericType)
override def dataType: DataType = YearMonthIntervalType
@transient
private lazy val evalFunc: (Int, Any) => Any = right.dataType match {
case LongType => (months: Int, num) =>
// Year-month interval has `Int` as the internal type. The result of the divide operation
// of `Int` by `Long` must fit to `Int`. So, the casting to `Int` cannot cause overflow.
LongMath.divide(months, num.asInstanceOf[Long], RoundingMode.HALF_UP).toInt
case _: IntegralType => (months: Int, num) =>
IntMath.divide(months, num.asInstanceOf[Number].intValue(), RoundingMode.HALF_UP)
case _: DecimalType => (months: Int, num) =>
val decimalRes = ((new Decimal).set(months) / num.asInstanceOf[Decimal]).toJavaBigDecimal
decimalRes.setScale(0, java.math.RoundingMode.HALF_UP).intValueExact()
case _: FractionalType => (months: Int, num) =>
DoubleMath.roundToInt(months / num.asInstanceOf[Number].doubleValue(), RoundingMode.HALF_UP)
}
override def nullSafeEval(interval: Any, num: Any): Any = {
evalFunc(interval.asInstanceOf[Int], num)
}
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = right.dataType match {
case LongType =>
val math = classOf[LongMath].getName
val javaType = CodeGenerator.javaType(dataType)
defineCodeGen(ctx, ev, (m, n) =>
// Similarly to non-codegen code. The result of `divide(Int, Long, ...)` must fit to `Int`.
// Casting to `Int` is safe here.
s"($javaType)($math.divide($m, $n, java.math.RoundingMode.HALF_UP))")
case _: IntegralType =>
val math = classOf[IntMath].getName
defineCodeGen(ctx, ev, (m, n) => s"$math.divide($m, $n, java.math.RoundingMode.HALF_UP)")
case _: DecimalType =>
defineCodeGen(ctx, ev, (m, n) =>
s"((new Decimal()).set($m).$$div($n)).toJavaBigDecimal()" +
".setScale(0, java.math.RoundingMode.HALF_UP).intValueExact()")
case _: FractionalType =>
val math = classOf[DoubleMath].getName
defineCodeGen(ctx, ev, (m, n) =>
s"$math.roundToInt($m / (double)$n, java.math.RoundingMode.HALF_UP)")
}
override def toString: String = s"($left / $right)"
}
// Divide a day-time interval by a numeric
case class DivideDTInterval(
interval: Expression,
num: Expression)
extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant with Serializable {
override def left: Expression = interval
override def right: Expression = num
override def inputTypes: Seq[AbstractDataType] = Seq(DayTimeIntervalType, NumericType)
override def dataType: DataType = DayTimeIntervalType
@transient
private lazy val evalFunc: (Long, Any) => Any = right.dataType match {
case _: IntegralType => (micros: Long, num) =>
LongMath.divide(micros, num.asInstanceOf[Number].longValue(), RoundingMode.HALF_UP)
case _: DecimalType => (micros: Long, num) =>
val decimalRes = ((new Decimal).set(micros) / num.asInstanceOf[Decimal]).toJavaBigDecimal
decimalRes.setScale(0, java.math.RoundingMode.HALF_UP).longValueExact()
case _: FractionalType => (micros: Long, num) =>
DoubleMath.roundToLong(micros / num.asInstanceOf[Number].doubleValue(), RoundingMode.HALF_UP)
}
override def nullSafeEval(interval: Any, num: Any): Any = {
evalFunc(interval.asInstanceOf[Long], num)
}
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = right.dataType match {
case _: IntegralType =>
val math = classOf[LongMath].getName
defineCodeGen(ctx, ev, (m, n) => s"$math.divide($m, $n, java.math.RoundingMode.HALF_UP)")
case _: DecimalType =>
defineCodeGen(ctx, ev, (m, n) =>
s"((new Decimal()).set($m).$$div($n)).toJavaBigDecimal()" +
".setScale(0, java.math.RoundingMode.HALF_UP).longValueExact()")
case _: FractionalType =>
val math = classOf[DoubleMath].getName
defineCodeGen(ctx, ev, (m, n) =>
s"$math.roundToLong($m / (double)$n, java.math.RoundingMode.HALF_UP)")
}
override def toString: String = s"($left / $right)"
}