avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nkol...@apache.org
Subject avro git commit: AVRO-1957: TimeConversions do not implement getRecommendedSchema()
Date Mon, 29 Jan 2018 10:38:06 GMT
Repository: avro
Updated Branches:
  refs/heads/branch-1.8 49471412e -> 3fe8df715


AVRO-1957: TimeConversions do not implement getRecommendedSchema()

This closes #154

Signed-off-by: Gabor Szadovszky <gabor@apache.org>
Signed-off-by: sacharya <suraj@apache.org>
Signed-off-by: Nandor Kollar <nkollar@apache.org>
(cherry picked from commit 027d196fd3f6f990977a4e86c1c07464506cabc3)


Project: http://git-wip-us.apache.org/repos/asf/avro/repo
Commit: http://git-wip-us.apache.org/repos/asf/avro/commit/3fe8df71
Tree: http://git-wip-us.apache.org/repos/asf/avro/tree/3fe8df71
Diff: http://git-wip-us.apache.org/repos/asf/avro/diff/3fe8df71

Branch: refs/heads/branch-1.8
Commit: 3fe8df71592b149500f2f5e1c666268ded63d1fc
Parents: 4947141
Author: Sean Timm <sean.timm@teamaol.com>
Authored: Thu Nov 10 17:07:39 2016 -0500
Committer: Nandor Kollar <nkollar@apache.org>
Committed: Mon Jan 29 11:29:39 2018 +0100

----------------------------------------------------------------------
 .../org/apache/avro/data/TimeConversions.java   | 30 ++++++++++++-
 .../apache/avro/data/TestTimeConversions.java   | 47 ++++++++++++++++++++
 2 files changed, 75 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/avro/blob/3fe8df71/lang/java/avro/src/main/java/org/apache/avro/data/TimeConversions.java
----------------------------------------------------------------------
diff --git a/lang/java/avro/src/main/java/org/apache/avro/data/TimeConversions.java b/lang/java/avro/src/main/java/org/apache/avro/data/TimeConversions.java
index b53bb1f..0ec1ecf 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/data/TimeConversions.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/data/TimeConversions.java
@@ -20,6 +20,7 @@ package org.apache.avro.data;
 
 import org.apache.avro.Conversion;
 import org.apache.avro.LogicalType;
+import org.apache.avro.LogicalTypes;
 import org.apache.avro.Schema;
 import org.joda.time.DateTime;
 import org.joda.time.DateTimeZone;
@@ -50,6 +51,11 @@ public class TimeConversions {
     public Integer toInt(LocalDate date, Schema schema, LogicalType type) {
       return Days.daysBetween(EPOCH_DATE, date).getDays();
     }
+
+    @Override
+    public Schema getRecommendedSchema() {
+      return LogicalTypes.date().addToSchema(Schema.create(Schema.Type.INT));
+    }
   }
 
   public static class TimeConversion extends Conversion<LocalTime> {
@@ -72,7 +78,12 @@ public class TimeConversions {
     public Integer toInt(LocalTime time, Schema schema, LogicalType type) {
       return time.millisOfDay().get();
     }
-  }
+
+    @Override
+    public Schema getRecommendedSchema() {
+      return LogicalTypes.timeMillis().addToSchema(Schema.create(Schema.Type.INT));
+    }
+}
 
   public static class TimeMicrosConversion extends Conversion<LocalTime> {
     @Override
@@ -89,6 +100,11 @@ public class TimeConversions {
     public LocalTime fromLong(Long microsFromMidnight, Schema schema, LogicalType type) {
       return LocalTime.fromMillisOfDay(microsFromMidnight / 1000);
     }
+
+    @Override
+    public Schema getRecommendedSchema() {
+      return LogicalTypes.timeMicros().addToSchema(Schema.create(Schema.Type.LONG));
+    }
   }
 
   public static class LossyTimeMicrosConversion extends TimeMicrosConversion {
@@ -118,6 +134,11 @@ public class TimeConversions {
     public Long toLong(DateTime timestamp, Schema schema, LogicalType type) {
       return timestamp.getMillis();
     }
+
+    @Override
+    public Schema getRecommendedSchema() {
+      return LogicalTypes.timestampMillis().addToSchema(Schema.create(Schema.Type.LONG));
+    }
   }
 
   public static class TimestampMicrosConversion extends Conversion<DateTime> {
@@ -135,7 +156,12 @@ public class TimeConversions {
     public DateTime fromLong(Long microsFromEpoch, Schema schema, LogicalType type) {
       return new DateTime(microsFromEpoch / 1000, DateTimeZone.UTC);
     }
-  }
+
+    @Override
+    public Schema getRecommendedSchema() {
+      return LogicalTypes.timestampMicros().addToSchema(Schema.create(Schema.Type.LONG));
+    }
+}
 
   public static class LossyTimestampMicrosConversion extends TimestampMicrosConversion {
     @Override

http://git-wip-us.apache.org/repos/asf/avro/blob/3fe8df71/lang/java/avro/src/test/java/org/apache/avro/data/TestTimeConversions.java
----------------------------------------------------------------------
diff --git a/lang/java/avro/src/test/java/org/apache/avro/data/TestTimeConversions.java b/lang/java/avro/src/test/java/org/apache/avro/data/TestTimeConversions.java
index 5e315cd..7ed6b80 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/data/TestTimeConversions.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/data/TestTimeConversions.java
@@ -18,6 +18,7 @@
 
 package org.apache.avro.data;
 
+import org.apache.avro.Conversion;
 import org.apache.avro.LogicalTypes;
 import org.apache.avro.Schema;
 import org.apache.avro.data.TimeConversions.DateConversion;
@@ -27,6 +28,7 @@ import org.apache.avro.data.TimeConversions.TimeMicrosConversion;
 import org.apache.avro.data.TimeConversions.TimestampMicrosConversion;
 import org.apache.avro.data.TimeConversions.TimeConversion;
 import org.apache.avro.data.TimeConversions.TimestampConversion;
+import org.apache.avro.reflect.ReflectData;
 import org.joda.time.DateTime;
 import org.joda.time.DateTimeZone;
 import org.joda.time.LocalDate;
@@ -34,6 +36,7 @@ import org.joda.time.LocalTime;
 import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
+
 import java.util.Date;
 
 public class TestTimeConversions {
@@ -244,6 +247,50 @@ public class TestTimeConversions {
             TIMESTAMP_MICROS_SCHEMA, LogicalTypes.timestampMicros()));
   }
 
+  /*
+  model.addLogicalTypeConversion(new TimeConversions.TimeMicrosConversion());
+  model.addLogicalTypeConversion(new TimeConversions.TimestampMicrosConversion());
+ */
+  @Test
+  public void testDynamicSchemaWithDateConversion() throws ClassNotFoundException {
+    Schema schema = getReflectedSchemaByName("org.joda.time.LocalDate", new TimeConversions.DateConversion());
+    Assert.assertEquals("Reflected schema should be logicalType date", DATE_SCHEMA, schema);
+  }
+
+  @Test
+  public void testDynamicSchemaWithTimeConversion() throws ClassNotFoundException {
+    Schema schema = getReflectedSchemaByName("org.joda.time.LocalTime", new TimeConversions.TimeConversion());
+    Assert.assertEquals("Reflected schema should be logicalType timeMillis", TIME_MILLIS_SCHEMA,
schema);
+  }
+
+  @Test
+  public void testDynamicSchemaWithTimeMicrosConversion() throws ClassNotFoundException {
+    Schema schema = getReflectedSchemaByName("org.joda.time.LocalTime", new TimeConversions.TimeMicrosConversion());
+    Assert.assertEquals("Reflected schema should be logicalType timeMicros", TIME_MICROS_SCHEMA,
schema);
+  }
+
+  @Test
+  public void testDynamicSchemaWithDateTimeConversion() throws ClassNotFoundException {
+    Schema schema = getReflectedSchemaByName("org.joda.time.DateTime", new TimeConversions.TimestampConversion());
+    Assert.assertEquals("Reflected schema should be logicalType timestampMillis", TIMESTAMP_MILLIS_SCHEMA,
schema);
+  }
+
+  @Test
+  public void testDynamicSchemaWithDateTimeMicrosConversion() throws ClassNotFoundException
{
+    Schema schema = getReflectedSchemaByName("org.joda.time.DateTime", new TimeConversions.TimestampMicrosConversion());
+    Assert.assertEquals("Reflected schema should be logicalType timestampMicros", TIMESTAMP_MICROS_SCHEMA,
schema);
+  }
+
+  private Schema getReflectedSchemaByName(String className, Conversion<?> conversion)
throws ClassNotFoundException {
+    // one argument: a fully qualified class name
+    Class<?> cls = Class.forName(className);
+
+    // get the reflected schema for the given class
+    ReflectData model = new ReflectData();
+    model.addLogicalTypeConversion(conversion);
+    return model.getSchema(cls);
+  }
+
   private long dropMicros(long micros) {
     return micros / 1000 * 1000;
   }


Mime
View raw message