julianpeeters / avrohugger

Generate Scala case class definitions from Avro schemas
Apache License 2.0
201 stars 120 forks source link

support 4 more logical types (time-micro, timestamp-micro, local-timestamp-micros, local-timestamp-millis) #183

Closed LeonPoon closed 11 months ago

LeonPoon commented 11 months ago
final case class LogicalSc(..., var timeMicros: java.time.LocalTime, var timestampMicros: java.time.ZonedDateTime, var localTimestampMicros: java.time.LocalDateTime, var localTimestampMillis: java.time.LocalDateTime) extends org.apache.avro.specific.SpecificRecordBase {
  def this() = this(..., java.time.LocalTime.MIDNIGHT, java.time.ZonedDateTime.of(java.time.LocalDateTime.MIN, java.time.ZoneId.of("UTC")), java.time.LocalDateTime.MIN, java.time.LocalDateTime.MIN)
  def get(field$: Int): AnyRef = {
    (field$: @switch) match {
      ...
      case 7 => {
        // avro time-micros long stores the number of microseconds after midnight, 00:00:00.000000
        {
          timeMicros.toNanoOfDay / 1000L
        }
      }.asInstanceOf[AnyRef]
      case 8 => {
        // avro timestamp-micros long stores the number of microseconds from the unix epoch, 1 January 1970 00:00:00.000000 UTC
        {
          timestampMicros.toEpochSecond * 1000000L + (timestampMicros.getNano / 1000L)
        }
      }.asInstanceOf[AnyRef]
      case 9 => {
        // avro local-timestamp-micros long stores the number of microseconds, from 1 January 1970 00:00:00.000000
        {
          localTimestampMicros.toEpochSecond(java.time.ZoneOffset.UTC) * 1000000L + (localTimestampMicros.getNano / 1000L)
        }
      }.asInstanceOf[AnyRef]
      case 10 => {
        // avro local-timestamp-millis long stores the number of millis, from 1 January 1970 00:00:00.000000
        {
          localTimestampMillis.toEpochSecond(java.time.ZoneOffset.UTC) * 1000L + (localTimestampMillis.getNano / 1000000L)
        }
      }.asInstanceOf[AnyRef]
      case _ => new org.apache.avro.AvroRuntimeException("Bad index")
    }
  }
  def put(field$: Int, value: Any): Unit = {
    (field$: @switch) match {
      ...
      case 7 => this.timeMicros = {
        // avro time-micros long stores the number of microseconds after midnight, 00:00:00.000000
        value match {
          case (l: Long) => {
            java.time.LocalTime.ofNanoOfDay(l * 1000L)
          }
        }
      }.asInstanceOf[java.time.LocalTime]
      case 8 => this.timestampMicros = {
        // avro timestamp-micros long stores the number of microseconds from the unix epoch, 1 January 1970 00:00:00.000000 UTC
        value match {
          case (l: Long) => {
            java.time.ZonedDateTime.of(java.time.LocalDateTime.ofEpochSecond(l / 1000000L, (l % 1000000L).toInt * 1000, java.time.ZoneOffset.UTC), java.time.ZoneId.of("UTC"))
          }
        }
      }.asInstanceOf[java.time.ZonedDateTime]
      case 9 => this.localTimestampMicros = {
        // avro local-timestamp-micros long stores the number of microseconds, from 1 January 1970 00:00:00.000000
        value match {
          case (l: Long) => {
            java.time.LocalDateTime.ofEpochSecond(l / 1000000L, (l % 1000000L).toInt * 1000, java.time.ZoneOffset.UTC)
          }
        }
      }.asInstanceOf[java.time.LocalDateTime]
      case 10 => this.localTimestampMillis = {
        // avro local-timestamp-millis long stores the number of millis, from 1 January 1970 00:00:00.000000
        value match {
          case (l: Long) => {
            java.time.LocalDateTime.ofEpochSecond(l / 1000L, (l % 1000L).toInt * 1000000, java.time.ZoneOffset.UTC)
          }
        }
      }.asInstanceOf[java.time.LocalDateTime]
      case _ => new org.apache.avro.AvroRuntimeException("Bad index")
    }
    ()
  }
}
julianpeeters commented 11 months ago

Thanks once again for the contribution. Released in avrohugger 1.6.0 and sbt-avrohugger 2.6.0