flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [flink] JingsongLi commented on a change in pull request #10035: [FLINK-14080][table-planner-blink] Introduce PreciseTimestamp as inte…
Date Tue, 29 Oct 2019 14:25:54 GMT
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_r340101463
 
 

 ##########
 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) ?
+			(milliseconds / 1000) : (milliseconds / 1000 - 1);
+		return LocalDateTime.ofEpochSecond(epochSeconds, nanoseconds, ZoneOffset.UTC);
+	}
+
+	public static PreciseTimestamp fromLocalDateTime(LocalDateTime ldt, int precision) {
+		int nanos = (int) zeroLastNDigits((long) ldt.getNano(), 9 - precision);
+		long millis = unixTimestamp(
+			ldt.getYear(),
+			ldt.getMonthValue(),
+			ldt.getDayOfMonth(),
+			ldt.getHour(),
+			ldt.getMinute(),
+			ldt.getSecond()) + (long) (nanos / 1_000_000);
+		return new PreciseTimestamp(millis, nanos, precision);
+	}
+
+	public byte[] toUnscaledBytes() {
+		byte[] bytes8 = new byte[8];
+		long l = milliseconds;
+		for (int i = 0; i < 8; i++) {
+			bytes8[7 - i] = (byte) l;
+			l >>>= 8;
+		}
+
+		if (isCompact()) {
+			return bytes8;
+		}
+
+		byte[] bytes12 = new byte[12];
+		System.arraycopy(bytes8, 0, bytes12, 0, 8);
+		int n = nanoseconds;
+		for (int i = 0; i < 4; i++) {
+			bytes12[11 - i] = (byte) n;
+			n >>>= 8;
+		}
+
+		return bytes12;
+	}
+
+	public static PreciseTimestamp fromUnscaledBytes(byte[] bytes, int precision) {
 
 Review comment:
   There is no need to convert between bytes and Timestamp.
   We can write long and int directly.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message