JingsongLi commented on a change in pull request #10035: 
[FLINK-14080][table-planner-blink] Introduce PreciseTimestamp as inte…
URL: https://github.com/apache/flink/pull/10035#discussion_r340411700
 
 

 ##########
 File path: 
flink-table/flink-table-runtime-blink/src/main/java/org/apache/flink/table/dataformat/PreciseTimestamp.java
 ##########
 @@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.table.dataformat;
+
+import org.apache.flink.table.types.logical.TimestampType;
+import org.apache.flink.util.Preconditions;
+
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.util.Arrays;
+
+import static 
org.apache.calcite.avatica.util.DateTimeUtils.timestampStringToUnixDate;
+import static org.apache.calcite.avatica.util.DateTimeUtils.unixTimestamp;
+import static 
org.apache.calcite.avatica.util.DateTimeUtils.unixTimestampToString;
+
+/**
+ * Immutable SQL TIMESTAMP type with nanosecond precision.
+ */
+public class PreciseTimestamp implements Comparable<PreciseTimestamp> {
+
+       public static final int MAX_PRECISION = TimestampType.MAX_PRECISION;
+
+       public static final int MIN_PRECISION = TimestampType.MIN_PRECISION;
+
+       private final long milliseconds;
+
+       private final int nanoseconds;
+
+       private final int precision;
+
+       private PreciseTimestamp(long milliseconds, int nanoseconds, int 
precision) {
+               Preconditions.checkArgument(precision >= MIN_PRECISION && 
precision <= MAX_PRECISION);
+               Preconditions.checkArgument(nanoseconds >= 0 && nanoseconds <= 
999_999_999);
+               this.milliseconds = milliseconds;
+               this.nanoseconds = nanoseconds;
+               this.precision = precision;
+       }
+
+       @Override
+       public int compareTo(PreciseTimestamp that) {
+               return this.milliseconds == that.milliseconds ?
+                       Integer.compare(this.nanoseconds, that.nanoseconds) :
+                       Long.compare(this.milliseconds, that.milliseconds);
+       }
+
+       @Override
+       public boolean equals(Object obj) {
+               if (!(obj instanceof PreciseTimestamp)) {
+                       return false;
+               }
+               PreciseTimestamp that = (PreciseTimestamp) obj;
+               return this.compareTo(that) == 0;
+       }
+
+       @Override
+       public String toString() {
+               // yyyy-MM-dd HH:mm:ss
+               StringBuilder timestampString = new 
StringBuilder(unixTimestampToString(milliseconds));
+               if (precision > 0 && nanoseconds > 0) {
+                       // append fraction part
+                       
timestampString.append('.').append(nonosSecondsToString(nanoseconds, 
precision));
+               }
+               return timestampString.toString();
+       }
+
+       public long toLong() {
+               return milliseconds;
+       }
+
+       public static PreciseTimestamp fromLong(long milliseconds, int 
precision) {
+               Timestamp t = new Timestamp(milliseconds);
+               long millis = isCompact(precision) ?
+                       zeroLastNDigits(t.getTime(), 3 - precision) : 
t.getTime();
+               int nanos = (int) zeroLastNDigits((long) t.getNanos(), 9 - 
precision);
+               return new PreciseTimestamp(millis, nanos, precision);
+       }
+
+       public Timestamp toTimestamp() {
+               return Timestamp.valueOf(this.toString());
+       }
+
+       public static PreciseTimestamp fromTimestamp(Timestamp t, int 
precision) {
+               String timestampString = t.toString();
+               long millis = timestampStringToUnixDate(timestampString);
+               int nanos = timestampStringToNanoseconds(timestampString);
+               return new PreciseTimestamp(millis, nanos, precision);
+       }
+
+       public LocalDateTime toLocalDateTime() {
+               long epochSeconds = (milliseconds >= 0 || nanoseconds == 0) ?
 
 Review comment:
   ```
   public LocalDateTime toLocalDateTime() {
                int date = (int) (milliseconds / MILLIS_PER_DAY);
                int time = (int) (milliseconds % MILLIS_PER_DAY);
                if (time < 0) {
                        --date;
                        time += MILLIS_PER_DAY;
                }
                long nanoOfDay = time * 1000_000 + nanosOfMills;
                LocalDate localDate = LocalDate.ofEpochDay(date);
                LocalTime localTime = LocalTime.ofNanoOfDay(nanoOfDay);
                return LocalDateTime.of(localDate, localTime);
        }
   ```
   What do you think?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to