Github user cloud-fan commented on a diff in the pull request:
https://github.com/apache/spark/pull/8224#discussion_r37383102
--- Diff: unsafe/src/main/java/org/apache/spark/unsafe/types/Interval.java
---
@@ -0,0 +1,330 @@
+/*
+ * 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.spark.unsafe.types;
+
+import java.io.Serializable;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public abstract class Interval implements Serializable {
+ public static final long MICROS_PER_MILLI = 1000L;
+ public static final long MICROS_PER_SECOND = MICROS_PER_MILLI * 1000;
+ public static final long MICROS_PER_MINUTE = MICROS_PER_SECOND * 60;
+ public static final long MICROS_PER_HOUR = MICROS_PER_MINUTE * 60;
+ public static final long MICROS_PER_DAY = MICROS_PER_HOUR * 24;
+ public static final long MICROS_PER_WEEK = MICROS_PER_DAY * 7;
+
+ /**
+ * A function to generate regex which matches interval string's unit
part like "3 years".
+ *
+ * First, we can leave out some units in interval string, and we only
care about the value of
+ * unit, so here we use non-capturing group to wrap the actual regex.
+ * At the beginning of the actual regex, we should match spaces before
the unit part.
+ * Next is the number part, starts with an optional "-" to represent
negative value. We use
+ * capturing group to wrap this part as we need the value later.
+ * Finally is the unit name, ends with an optional "s".
+ */
+ private static String unitRegex(String unit) {
+ return "(?:\\s+(-?\\d+)\\s+" + unit + "s?)?";
+ }
+
+ private static String dayTimePatternString =
+ unitRegex("week") + unitRegex("day") + unitRegex("hour") +
unitRegex("minute") +
+ unitRegex("second") + unitRegex("millisecond") +
unitRegex("microsecond");
+
+ private static Pattern calendarIntervalPattern =
+ Pattern.compile("interval" + unitRegex("year") + unitRegex("month") +
dayTimePatternString);
+
+ private static Pattern timeIntervalPattern = Pattern.compile("interval"
+ dayTimePatternString);
+
+ private static Pattern yearMonthPattern =
+ Pattern.compile("^(?:['|\"])?([+|-])?(\\d+)-(\\d+)(?:['|\"])?$");
+
+ private static Pattern dayTimePattern =
+ Pattern.compile("^(?:['|\"])?([+|-])?(\\d+)
(\\d+):(\\d+):(\\d+)(\\.(\\d+))?(?:['|\"])?$");
+
+ private static Pattern quoteTrimPattern =
Pattern.compile("^(?:['|\"])?(.*?)(?:['|\"])?$");
+
+ /**
+ * Parse interval term string like "interval 5 years 3 months 1 weeks 2
days 5 seconds" to its
+ * internal representation [[CalendarInterval]].
+ */
+ public static CalendarInterval stringToCalendarInterval(String s)
+ throws IllegalArgumentException {
+ if (s == null) {
+ return null;
+ }
+ s = s.trim();
+ Matcher m = calendarIntervalPattern.matcher(s);
+ if (!m.matches() || s.equals("interval")) {
+ throw new IllegalArgumentException("Invalid interval String " + s);
+ } else {
+ long months = toLong(m.group(1)) * 12 + toLong(m.group(2));
+ long microseconds = toLong(m.group(3)) * MICROS_PER_WEEK;
+ microseconds += toLong(m.group(4)) * MICROS_PER_DAY;
+ microseconds += toLong(m.group(5)) * MICROS_PER_HOUR;
+ microseconds += toLong(m.group(6)) * MICROS_PER_MINUTE;
+ microseconds += toLong(m.group(7)) * MICROS_PER_SECOND;
+ microseconds += toLong(m.group(8)) * MICROS_PER_MILLI;
+ microseconds += toLong(m.group(9));
+ return new CalendarInterval((int) months, microseconds);
+ }
+ }
+
+ /**
+ * Parse interval term string like "interval 1 weeks 2 days 5 seconds"
to its
+ * internal representation - a Long value for number of microseconds.
+ */
+ public static long stringToTimeInterval(String s) throws
IllegalArgumentException {
+ if (s == null) {
+ return 0;
+ }
+ s = s.trim();
+ Matcher m = timeIntervalPattern.matcher(s);
+ if (!m.matches() || s.equals("interval")) {
+ throw new IllegalArgumentException("Invalid interval String " + s);
+ } else {
+ long microseconds = toLong(m.group(1)) * MICROS_PER_WEEK;
+ microseconds += toLong(m.group(2)) * MICROS_PER_DAY;
+ microseconds += toLong(m.group(3)) * MICROS_PER_HOUR;
+ microseconds += toLong(m.group(4)) * MICROS_PER_MINUTE;
+ microseconds += toLong(m.group(5)) * MICROS_PER_SECOND;
+ microseconds += toLong(m.group(6)) * MICROS_PER_MILLI;
+ microseconds += toLong(m.group(7));
+ return microseconds;
+ }
+ }
+
+ public static long calendarIntervalToTimeInterval(CalendarInterval
calendarInterval) {
+ return calendarInterval.microseconds;
--- End diff --
I think `TimeInterval` should be a concept that we turn `CalendarInterval`
into microseconds in a certain time context(which month), but not just cut down
the microseconds part of it.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]