[SPARK-35736][SPARK-35737][SQL][FOLLOWUP] Move a common logic to DayTimeIntervalType

### What changes were proposed in this pull request?

This is a followup PR for SPARK-35736(#32893) and SPARK-35737(#32892).
This PR moves a common logic to `object DayTimeIntervalType`.
That logic is like `val strToFieldIndex = DayTimeIntervalType.dayTimeFields.map(i => DayTimeIntervalType.fieldToString(i) -> (i).toMap`, a `Map` which maps each time unit to the corresponding day-time field index.

### Why are the changes needed?

That logic appeared in the change in SPARK-35736 and SPARK-35737 so it can be a common logic and it's better to avoid the similar logic scattered.

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Existing tests.

Closes #32905 from sarutak/followup-SPARK-35736-35737.

Authored-by: Kousuke Saruta <sarutak@oss.nttdata.com>
Signed-off-by: Max Gekk <max.gekk@gmail.com>
This commit is contained in:
Kousuke Saruta 2021-06-14 20:51:18 +03:00 committed by Max Gekk
parent 82af318c31
commit aab0c2bf66
2 changed files with 6 additions and 8 deletions

View file

@ -2357,13 +2357,11 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with SQLConfHelper with Logg
Literal(calendarInterval.months, YearMonthIntervalType)
} else {
assert(calendarInterval.months == 0)
val strToFieldIndex = DayTimeIntervalType.dayTimeFields.map(i =>
DayTimeIntervalType.fieldToString(i) -> i).toMap
val fromUnit =
ctx.errorCapturingUnitToUnitInterval.body.from.getText.toLowerCase(Locale.ROOT)
val micros = IntervalUtils.getDuration(calendarInterval, TimeUnit.MICROSECONDS)
val start = strToFieldIndex(fromUnit)
val end = strToFieldIndex(toUnit)
val start = DayTimeIntervalType.stringToField(fromUnit)
val end = DayTimeIntervalType.stringToField(toUnit)
Literal(micros, DayTimeIntervalType(start, end))
}
} else {
@ -2519,11 +2517,9 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with SQLConfHelper with Logg
}
override def visitDayTimeIntervalDataType(ctx: DayTimeIntervalDataTypeContext): DataType = {
val strToFieldIndex =
DayTimeIntervalType.dayTimeFields.map(i => DayTimeIntervalType.fieldToString(i) -> i).toMap
val start = strToFieldIndex(ctx.from.getText.toLowerCase(Locale.ROOT))
val start = DayTimeIntervalType.stringToField(ctx.from.getText.toLowerCase(Locale.ROOT))
val end = if (ctx.to != null ) {
strToFieldIndex(ctx.to.getText.toLowerCase(Locale.ROOT))
DayTimeIntervalType.stringToField(ctx.to.getText.toLowerCase(Locale.ROOT))
} else {
start
}

View file

@ -96,6 +96,8 @@ case object DayTimeIntervalType extends AbstractDataType {
case invalid => throw QueryCompilationErrors.invalidDayTimeField(invalid)
}
val stringToField: Map[String, Byte] = dayTimeFields.map(i => fieldToString(i) -> i).toMap
val DEFAULT = DayTimeIntervalType(DAY, SECOND)
def apply(): DayTimeIntervalType = DEFAULT