-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added encoding for DatePeriod, DateTimePeriod, Instant, LocalDateTime…
…, and LocalDate, Duration not working
- Loading branch information
1 parent
48db819
commit ab4c455
Showing
8 changed files
with
288 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
kotlin-spark-api/src/main/kotlin/org/jetbrains/kotlinx/spark/api/udts/DatePeriodUdt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.jetbrains.kotlinx.spark.api.udts | ||
|
||
import kotlinx.datetime.DatePeriod | ||
import kotlinx.datetime.toJavaPeriod | ||
import kotlinx.datetime.toKotlinDatePeriod | ||
import org.apache.spark.sql.catalyst.util.IntervalUtils | ||
import org.apache.spark.sql.types.UserDefinedType | ||
import org.apache.spark.sql.types.YearMonthIntervalType | ||
|
||
/** | ||
* NOTE: Just like java.time.DatePeriod, this is truncated to months. | ||
*/ | ||
class DatePeriodUdt : UserDefinedType<DatePeriod>() { | ||
|
||
override fun userClass(): Class<DatePeriod> = DatePeriod::class.java | ||
override fun deserialize(datum: Any?): DatePeriod? = | ||
when (datum) { | ||
null -> null | ||
is Int -> IntervalUtils.monthsToPeriod(datum).toKotlinDatePeriod() | ||
else -> throw IllegalArgumentException("Unsupported datum: $datum") | ||
} | ||
|
||
override fun serialize(obj: DatePeriod?): Int? = | ||
obj?.let { IntervalUtils.periodToMonths(it.toJavaPeriod()) } | ||
|
||
override fun sqlType(): YearMonthIntervalType = YearMonthIntervalType.apply() | ||
} |
46 changes: 46 additions & 0 deletions
46
kotlin-spark-api/src/main/kotlin/org/jetbrains/kotlinx/spark/api/udts/DateTimePeriodUdt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.jetbrains.kotlinx.spark.api.udts | ||
|
||
import kotlinx.datetime.DateTimePeriod | ||
import org.apache.spark.sql.types.CalendarIntervalType | ||
import org.apache.spark.sql.types.`CalendarIntervalType$` | ||
import org.apache.spark.sql.types.UserDefinedType | ||
import org.apache.spark.unsafe.types.CalendarInterval | ||
import kotlin.time.Duration.Companion.hours | ||
import kotlin.time.Duration.Companion.minutes | ||
import kotlin.time.Duration.Companion.nanoseconds | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
/** | ||
* NOTE: Just like java.time.DatePeriod, this is truncated to months. | ||
*/ | ||
class DateTimePeriodUdt : UserDefinedType<DateTimePeriod>() { | ||
|
||
override fun userClass(): Class<DateTimePeriod> = DateTimePeriod::class.java | ||
override fun deserialize(datum: Any?): DateTimePeriod? = | ||
when (datum) { | ||
null -> null | ||
is CalendarInterval -> | ||
DateTimePeriod( | ||
months = datum.months, | ||
days = datum.days, | ||
nanoseconds = datum.microseconds * 1_000, | ||
) | ||
|
||
else -> throw IllegalArgumentException("Unsupported datum: $datum") | ||
} | ||
|
||
override fun serialize(obj: DateTimePeriod?): CalendarInterval? = | ||
obj?.let { | ||
CalendarInterval( | ||
/* months = */ obj.months + obj.years * 12, | ||
/* days = */ obj.days, | ||
/* microseconds = */ | ||
(obj.hours.hours + | ||
obj.minutes.minutes + | ||
obj.seconds.seconds + | ||
obj.nanoseconds.nanoseconds).inWholeMicroseconds, | ||
) | ||
} | ||
|
||
override fun sqlType(): CalendarIntervalType = `CalendarIntervalType$`.`MODULE$` | ||
} |
46 changes: 46 additions & 0 deletions
46
kotlin-spark-api/src/main/kotlin/org/jetbrains/kotlinx/spark/api/udts/DurationUdt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.jetbrains.kotlinx.spark.api.udts | ||
|
||
import org.apache.spark.sql.catalyst.util.IntervalUtils | ||
import org.apache.spark.sql.types.DataType | ||
import org.apache.spark.sql.types.DayTimeIntervalType | ||
import org.apache.spark.sql.types.UserDefinedType | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.milliseconds | ||
import kotlin.time.Duration.Companion.nanoseconds | ||
import kotlin.time.toJavaDuration | ||
import kotlin.time.toKotlinDuration | ||
|
||
// TODO Fails, likely because Duration is a value class. | ||
class DurationUdt : UserDefinedType<Duration>() { | ||
|
||
override fun userClass(): Class<Duration> = Duration::class.java | ||
override fun deserialize(datum: Any?): Duration? = | ||
when (datum) { | ||
null -> null | ||
is Long -> IntervalUtils.microsToDuration(datum).toKotlinDuration() | ||
// is Long -> IntervalUtils.microsToDuration(datum).toKotlinDuration().let { | ||
// // store in nanos | ||
// it.inWholeNanoseconds shl 1 | ||
// } | ||
else -> throw IllegalArgumentException("Unsupported datum: $datum") | ||
} | ||
|
||
// override fun serialize(obj: Duration): Long = | ||
// IntervalUtils.durationToMicros(obj.toJavaDuration()) | ||
|
||
fun serialize(obj: Long): Long? = | ||
obj?.let { rawValue -> | ||
val unitDiscriminator = rawValue.toInt() and 1 | ||
fun isInNanos() = unitDiscriminator == 0 | ||
val value = rawValue shr 1 | ||
val duration = if (isInNanos()) value.nanoseconds else value.milliseconds | ||
|
||
IntervalUtils.durationToMicros(duration.toJavaDuration()) | ||
} | ||
|
||
override fun serialize(obj: Duration): Long? = | ||
obj?.let { IntervalUtils.durationToMicros(it.toJavaDuration()) } | ||
|
||
|
||
override fun sqlType(): DataType = DayTimeIntervalType.apply() | ||
} |
26 changes: 26 additions & 0 deletions
26
kotlin-spark-api/src/main/kotlin/org/jetbrains/kotlinx/spark/api/udts/InstantUdt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.jetbrains.kotlinx.spark.api.udts | ||
|
||
import kotlinx.datetime.Instant | ||
import kotlinx.datetime.toJavaInstant | ||
import kotlinx.datetime.toKotlinInstant | ||
import org.apache.spark.sql.catalyst.util.DateTimeUtils | ||
import org.apache.spark.sql.types.DataType | ||
import org.apache.spark.sql.types.`TimestampType$` | ||
import org.apache.spark.sql.types.UserDefinedType | ||
|
||
|
||
class InstantUdt : UserDefinedType<Instant>() { | ||
|
||
override fun userClass(): Class<Instant> = Instant::class.java | ||
override fun deserialize(datum: Any?): Instant? = | ||
when (datum) { | ||
null -> null | ||
is Long -> DateTimeUtils.microsToInstant(datum).toKotlinInstant() | ||
else -> throw IllegalArgumentException("Unsupported datum: $datum") | ||
} | ||
|
||
override fun serialize(obj: Instant?): Long? = | ||
obj?.let { DateTimeUtils.instantToMicros(it.toJavaInstant()) } | ||
|
||
override fun sqlType(): DataType = `TimestampType$`.`MODULE$` | ||
} |
26 changes: 26 additions & 0 deletions
26
kotlin-spark-api/src/main/kotlin/org/jetbrains/kotlinx/spark/api/udts/LocalDateTimeUdt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.jetbrains.kotlinx.spark.api.udts | ||
|
||
import kotlinx.datetime.LocalDateTime | ||
import kotlinx.datetime.toJavaLocalDateTime | ||
import kotlinx.datetime.toKotlinLocalDateTime | ||
import org.apache.spark.sql.catalyst.util.DateTimeUtils | ||
import org.apache.spark.sql.types.DataType | ||
import org.apache.spark.sql.types.`TimestampNTZType$` | ||
import org.apache.spark.sql.types.UserDefinedType | ||
|
||
|
||
class LocalDateTimeUdt : UserDefinedType<LocalDateTime>() { | ||
|
||
override fun userClass(): Class<LocalDateTime> = LocalDateTime::class.java | ||
override fun deserialize(datum: Any?): LocalDateTime? = | ||
when (datum) { | ||
null -> null | ||
is Long -> DateTimeUtils.microsToLocalDateTime(datum).toKotlinLocalDateTime() | ||
else -> throw IllegalArgumentException("Unsupported datum: $datum") | ||
} | ||
|
||
override fun serialize(obj: LocalDateTime?): Long? = | ||
obj?.let { DateTimeUtils.localDateTimeToMicros(it.toJavaLocalDateTime()) } | ||
|
||
override fun sqlType(): DataType = `TimestampNTZType$`.`MODULE$` | ||
} |
26 changes: 26 additions & 0 deletions
26
kotlin-spark-api/src/main/kotlin/org/jetbrains/kotlinx/spark/api/udts/LocalDateUdt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.jetbrains.kotlinx.spark.api.udts | ||
|
||
import kotlinx.datetime.LocalDate | ||
import kotlinx.datetime.toJavaLocalDate | ||
import kotlinx.datetime.toKotlinLocalDate | ||
import org.apache.spark.sql.catalyst.util.DateTimeUtils | ||
import org.apache.spark.sql.types.DataType | ||
import org.apache.spark.sql.types.`DateType$` | ||
import org.apache.spark.sql.types.UserDefinedType | ||
|
||
|
||
class LocalDateUdt : UserDefinedType<LocalDate>() { | ||
|
||
override fun userClass(): Class<LocalDate> = LocalDate::class.java | ||
override fun deserialize(datum: Any?): LocalDate? = | ||
when (datum) { | ||
null -> null | ||
is Int -> DateTimeUtils.daysToLocalDate(datum).toKotlinLocalDate() | ||
else -> throw IllegalArgumentException("Unsupported datum: $datum") | ||
} | ||
|
||
override fun serialize(obj: LocalDate?): Int? = | ||
obj?.let { DateTimeUtils.localDateToDays(it.toJavaLocalDate()) } | ||
|
||
override fun sqlType(): DataType = `DateType$`.`MODULE$` | ||
} |
Oops, something went wrong.