ahmedabu98 commented on code in PR #41550:
URL: https://github.com/apache/beam/pull/41550#discussion_r4762754352


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/logicaltypes/DateTime.java:
##########
@@ -36,29 +40,72 @@
  * that represents incrementing count of days where day 0 is 1970-01-01 (ISO). 
Time field is the
  * same as the base type of {@link Time}, which is a Long that represents a 
count of time in
  * nanoseconds.
+ *
+ * <p>The precision is the number of digits in the fraction of a second, from 
0 to 9, and defaults
+ * to 6. {@link #toBaseType} refuses a value with finer digits.
  */
 public class DateTime implements Schema.LogicalType<LocalDateTime, Row> {
-  public static final String IDENTIFIER = "beam:logical_type:datetime:v1";
+  // The UID from before DateTime had a precision, so instances that earlier 
versions serialized
+  // still deserialize.
+  private static final long serialVersionUID = -3033263475478247643L;
+
+  public static final String IDENTIFIER =
+      SchemaApi.LogicalTypes.Enum.DATETIME
+          .getValueDescriptor()
+          .getOptions()
+          .getExtension(RunnerApi.beamUrn);
   public static final String DATE_FIELD_NAME = "Date";
   public static final String TIME_FIELD_NAME = "Time";
   public static final Schema DATETIME_SCHEMA =
       
Schema.builder().addInt64Field(DATE_FIELD_NAME).addInt64Field(TIME_FIELD_NAME).build();
 
+  static final int MAX_PRECISION = 9;
+  static final int DEFAULT_PRECISION = 6;
+
+  public static final DateTime MILLIS = DateTime.of(3);
+  public static final DateTime MICROS = DateTime.of(6);
+  public static final DateTime NANOS = DateTime.of(9);
+
+  private final int precision;
+  private final int scalingFactor;
+
+  /** A DateTime with the default precision, 6. */
+  public DateTime() {
+    this(DEFAULT_PRECISION);
+  }
+
+  private DateTime(int precision) {
+    checkArgument(
+        precision >= 0 && precision <= MAX_PRECISION,
+        "DateTime precision must be between 0 and %s (inclusive), but was %s.",
+        MAX_PRECISION,
+        precision);
+    this.precision = precision;
+    this.scalingFactor = (int) Math.pow(10, MAX_PRECISION - precision);
+  }
+
+  public static DateTime of(int precision) {
+    return new DateTime(precision);
+  }
+
+  // Instances that earlier versions serialized have neither field set, and 
they meant micros.
+  private Object readResolve() {
+    return scalingFactor == 0 ? MICROS : this;

Review Comment:
   Changed it so that SqlTypes.DATETIME has no precision anymore, so it still 
keeps all 9 digits



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to