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


##########
sdks/python/apache_beam/transforms/managed_iceberg_it_test.py:
##########
@@ -53,7 +54,10 @@ def _create_row(self, num: int):
         float_=(num + float(num) / 100),
         arr_=[num, num, num],
         date_=datetime.date.today() - datetime.timedelta(days=num),
-        timestamp_=Timestamp(123 * num, 456 * num))
+        timestamp_=Timestamp(123 * num, 456 * num),
+        datetime_=DateTime.from_datetime(

Review Comment:
   Was this test triggered?



##########
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);
+  }
+

Review Comment:
   Beam 2.78 and earlier write this type's argument as an empty string. Java on 
this branch can't read those schemas (`does not have a static 
of('java.lang.String') method`), while master can. That would break, for 
example, `--transformsToUpgrade` from an older pipeline. Python here already 
accepts the empty string. Would something like this work for Java?
   
   ```suggestion
   
     /**
      * Reads the STRING argument that Beam 2.78 and earlier write for this 
type, so their schemas
      * still decode. That argument is always empty, and means the default 
precision.
      */
     public static DateTime of(String legacyArgument) {
       checkArgument(
           legacyArgument.isEmpty(),
           "DateTime precision must be an INT32, but got the STRING %s.",
           legacyArgument);
       return MICROS;
     }
   
   ```



##########
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:
   Before this PR, `DateTime` had no precision and kept all 9 digits. When a 
`DateTime` saved by an earlier release (in a savepoint, for example) is loaded, 
this line makes it precision 6. Values that job already stored with 9 digits 
then can't be read?
   



##########
CHANGES.md:
##########
@@ -128,6 +129,7 @@
 
 [comment]: # "generic breaking changes if not covered in following sections."
 * (Java) Flink runner: batch pipelines now assign source splits to readers 
statically by default instead of lazily. Set `--sourceStaticSplitThresholdMb=0` 
to restore lazy assignment 
([#40607](https://github.com/apache/beam/issues/40607)).
+* (Java) `SqlTypes.DATETIME` is now `DateTime` with precision 6, so a 
`LocalDateTime` with digits below a microsecond throws instead of being stored. 
Use `DateTime.NANOS` for those values 
([#41550](https://github.com/apache/beam/pull/41550)).

Review Comment:
   On Linux with JDK 15+, `LocalDateTime.now()` has 9 digits, so this works on 
master and throws on this branch:
   
   ```java
   Row.withSchema(s).addValue(LocalDateTime.now()).build();  // s has a 
SqlTypes.DATETIME field
   ```
   
   POJOs with a `LocalDateTime` field get `SqlTypes.DATETIME` automatically and 
can't switch to `DateTime.NANOS`. 



-- 
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