You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pinot.apache.org by GitBox <gi...@apache.org> on 2021/10/07 20:31:30 UTC

[GitHub] [pinot] ddcprg commented on a change in pull request #7541: [issue-7499]: Add date/time function tests

ddcprg commented on a change in pull request #7541:
URL: https://github.com/apache/pinot/pull/7541#discussion_r724510870



##########
File path: pinot-common/src/test/java/org/apache/pinot/common/function/scalar/DateTimeFunctionsTest.java
##########
@@ -0,0 +1,748 @@
+/**
+ * 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.pinot.common.function.scalar;
+
+import java.sql.Timestamp;
+import java.time.Clock;
+import java.time.Instant;
+import java.time.ZoneOffset;
+import org.joda.time.DateTimeZone;
+import org.testng.Assert;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
+import static java.util.concurrent.TimeUnit.*;
+
+
+public class DateTimeFunctionsTest {
+
+  @BeforeTest
+  public void init() {
+    DateTimeZone.setDefault(DateTimeZone.UTC);
+  }
+
+  @Test
+  public void testToEpochSeconds() {
+    Assert.assertEquals(DateTimeFunctions.toEpochSeconds(1609459200999L), 1609459200L);
+    Assert.assertEquals(DateTimeFunctions.toEpochSeconds(1646092799123L), 1646092799L);
+  }
+
+  @Test
+  public void testToEpochMinutes() {
+    Assert.assertEquals(DateTimeFunctions.toEpochMinutes(1609459200999L), 26824320L);
+    Assert.assertEquals(DateTimeFunctions.toEpochMinutes(1646092799123L), 27434879L);
+  }
+
+  @Test
+  public void testToEpochHours() {
+    Assert.assertEquals(DateTimeFunctions.toEpochHours(1609459200999L), 447072L);
+    Assert.assertEquals(DateTimeFunctions.toEpochHours(1646092799123L), 457247L);
+  }
+
+  @Test
+  public void testToEpochDays() {
+    Assert.assertEquals(DateTimeFunctions.toEpochDays(1609459200999L), 18628L);
+    Assert.assertEquals(DateTimeFunctions.toEpochDays(1646092799123L), 19051L);
+  }
+
+  @Test
+  public void testToEpochSecondsRounded() {
+    Assert.assertEquals(DateTimeFunctions.toEpochSecondsRounded(1609459200999L, 1000), 1609459000L);
+    Assert.assertEquals(DateTimeFunctions.toEpochSecondsRounded(1646092799123L, 5), 1646092795L);
+  }
+
+  @Test
+  public void testToEpochMinutesRounded() {
+    Assert.assertEquals(DateTimeFunctions.toEpochMinutesRounded(1609459200999L, 50), 26824300L);
+    Assert.assertEquals(DateTimeFunctions.toEpochMinutesRounded(1646092799123L, 2), 27434878L);
+  }
+
+  @Test
+  public void testToEpochHoursRounded() {
+    Assert.assertEquals(DateTimeFunctions.toEpochHoursRounded(1609459200999L, 2), 447072L);
+    Assert.assertEquals(DateTimeFunctions.toEpochHoursRounded(1646092799123L, 4), 457244L);
+  }
+
+  @Test
+  public void testToEpochDaysRounded() {
+    Assert.assertEquals(DateTimeFunctions.toEpochDaysRounded(1609459200999L, 32), 18624L);
+    Assert.assertEquals(DateTimeFunctions.toEpochDaysRounded(1646092799123L, 64), 19008L);
+  }
+
+  @Test
+  public void testToEpochSecondsBucket() {
+    Assert.assertEquals(DateTimeFunctions.toEpochSecondsBucket(1609459200999L, 1000), 1609459L);
+    Assert.assertEquals(DateTimeFunctions.toEpochSecondsBucket(1646092799123L, 5), 329218559L);
+  }
+
+  @Test
+  public void testToEpochMinutesBucket() {
+    Assert.assertEquals(DateTimeFunctions.toEpochMinutesBucket(1609459200999L, 50), 536486L);
+    Assert.assertEquals(DateTimeFunctions.toEpochMinutesBucket(1646092799123L, 2), 13717439L);
+  }
+
+  @Test
+  public void testToEpochHoursBucket() {
+    Assert.assertEquals(DateTimeFunctions.toEpochHoursBucket(1609459200999L, 2), 223536L);
+    Assert.assertEquals(DateTimeFunctions.toEpochHoursBucket(1646092799123L, 4), 114311L);
+  }
+
+  @Test
+  public void testToEpochDaysBucket() {
+    Assert.assertEquals(DateTimeFunctions.toEpochDaysBucket(1609459200999L, 32), 582L);
+    Assert.assertEquals(DateTimeFunctions.toEpochDaysBucket(1646092799123L, 64), 297L);
+  }
+
+  @Test
+  public void testFromEpochSeconds() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochSeconds(1609459200L), 1609459200000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochSeconds(1646092799L), 1646092799000L);
+  }
+
+  @Test
+  public void testFromEpochMinutes() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochMinutes(26824320L), 1609459200000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochMinutes(27434879L), 1646092740000L);
+  }
+
+  @Test
+  public void testFromEpochHours() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochHours(447072L), 1609459200000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochHours(457247L), 1646089200000L);
+  }
+
+  @Test
+  public void testFromEpochDays() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochDays(18628L), 1609459200000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochDays(19051L), 1646006400000L);
+  }
+
+  @Test
+  public void testFromEpochSecondsBucket() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochSecondsBucket(1609459L, 1000), 1609459000000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochSecondsBucket(329218559L, 5), 1646092795000L);
+  }
+
+  @Test
+  public void testFromEpochMinutesBucket() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochMinutesBucket(536486L, 50), 1609458000000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochMinutesBucket(13717439L, 2), 1646092680000L);
+  }
+
+  @Test
+  public void testFromEpochHoursBucket() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochHoursBucket(223536L, 2), 1609459200000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochHoursBucket(114311L, 4), 1646078400000L);
+  }
+
+  @Test
+  public void fromEpochDaysBucket() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochDaysBucket(582L, 32), 1609113600000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochDaysBucket(297L, 64), 1642291200000L);
+  }
+
+  @Test
+  public void testToTimestamp() {
+    Assert.assertEquals(
+        DateTimeFunctions.toTimestamp(1633375617536L), new Timestamp(1633375617536L));
+  }
+
+  @Test
+  public void testFromTimestamp() {
+    Assert.assertEquals(
+        DateTimeFunctions.fromTimestamp(new Timestamp(1633375617536L)), 1633375617536L);
+  }
+
+  @Test
+  public void toDateTime() {
+    Assert.assertEquals(
+        DateTimeFunctions.toDateTime(1633376098332L, "YYYY-MM-dd'T'HH:mm:ss.SSS z"),
+        "2021-10-04T19:34:58.332 UTC");
+    Assert.assertEquals(
+        DateTimeFunctions.toDateTime(1633376098332L, "yyyy-MM-dd'T'HH:mm:ss.SSSZ"),
+        "2021-10-04T19:34:58.332+0000");
+    Assert.assertEquals(
+        DateTimeFunctions.toDateTime(1633376098332L, "YYYY-MM-dd'T'HH:mm:ss.SSS z"),
+        "2021-10-04T19:34:58.332 UTC");
+    Assert.assertEquals(
+        DateTimeFunctions.toDateTime(1325376000000L, "xxxx"), "2011"); // 2012-01-01 00:00:00
+    Assert.assertEquals(DateTimeFunctions.toDateTime(1325376000000L, "yyyy"), "2012");
+    Assert.assertEquals(DateTimeFunctions.toDateTime(1325376000000L, "YYYY"), "2012");
+    // TODO more formats, all formats

Review comment:
       Add all possible formats

##########
File path: pinot-common/src/test/java/org/apache/pinot/common/function/scalar/DateTimeFunctionsTest.java
##########
@@ -0,0 +1,748 @@
+/**
+ * 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.pinot.common.function.scalar;
+
+import java.sql.Timestamp;
+import java.time.Clock;
+import java.time.Instant;
+import java.time.ZoneOffset;
+import org.joda.time.DateTimeZone;
+import org.testng.Assert;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
+import static java.util.concurrent.TimeUnit.*;
+
+
+public class DateTimeFunctionsTest {
+
+  @BeforeTest
+  public void init() {
+    DateTimeZone.setDefault(DateTimeZone.UTC);
+  }
+
+  @Test
+  public void testToEpochSeconds() {
+    Assert.assertEquals(DateTimeFunctions.toEpochSeconds(1609459200999L), 1609459200L);
+    Assert.assertEquals(DateTimeFunctions.toEpochSeconds(1646092799123L), 1646092799L);
+  }
+
+  @Test
+  public void testToEpochMinutes() {
+    Assert.assertEquals(DateTimeFunctions.toEpochMinutes(1609459200999L), 26824320L);
+    Assert.assertEquals(DateTimeFunctions.toEpochMinutes(1646092799123L), 27434879L);
+  }
+
+  @Test
+  public void testToEpochHours() {
+    Assert.assertEquals(DateTimeFunctions.toEpochHours(1609459200999L), 447072L);
+    Assert.assertEquals(DateTimeFunctions.toEpochHours(1646092799123L), 457247L);
+  }
+
+  @Test
+  public void testToEpochDays() {
+    Assert.assertEquals(DateTimeFunctions.toEpochDays(1609459200999L), 18628L);
+    Assert.assertEquals(DateTimeFunctions.toEpochDays(1646092799123L), 19051L);
+  }
+
+  @Test
+  public void testToEpochSecondsRounded() {
+    Assert.assertEquals(DateTimeFunctions.toEpochSecondsRounded(1609459200999L, 1000), 1609459000L);
+    Assert.assertEquals(DateTimeFunctions.toEpochSecondsRounded(1646092799123L, 5), 1646092795L);
+  }
+
+  @Test
+  public void testToEpochMinutesRounded() {
+    Assert.assertEquals(DateTimeFunctions.toEpochMinutesRounded(1609459200999L, 50), 26824300L);
+    Assert.assertEquals(DateTimeFunctions.toEpochMinutesRounded(1646092799123L, 2), 27434878L);
+  }
+
+  @Test
+  public void testToEpochHoursRounded() {
+    Assert.assertEquals(DateTimeFunctions.toEpochHoursRounded(1609459200999L, 2), 447072L);
+    Assert.assertEquals(DateTimeFunctions.toEpochHoursRounded(1646092799123L, 4), 457244L);
+  }
+
+  @Test
+  public void testToEpochDaysRounded() {
+    Assert.assertEquals(DateTimeFunctions.toEpochDaysRounded(1609459200999L, 32), 18624L);
+    Assert.assertEquals(DateTimeFunctions.toEpochDaysRounded(1646092799123L, 64), 19008L);
+  }
+
+  @Test
+  public void testToEpochSecondsBucket() {
+    Assert.assertEquals(DateTimeFunctions.toEpochSecondsBucket(1609459200999L, 1000), 1609459L);
+    Assert.assertEquals(DateTimeFunctions.toEpochSecondsBucket(1646092799123L, 5), 329218559L);
+  }
+
+  @Test
+  public void testToEpochMinutesBucket() {
+    Assert.assertEquals(DateTimeFunctions.toEpochMinutesBucket(1609459200999L, 50), 536486L);
+    Assert.assertEquals(DateTimeFunctions.toEpochMinutesBucket(1646092799123L, 2), 13717439L);
+  }
+
+  @Test
+  public void testToEpochHoursBucket() {
+    Assert.assertEquals(DateTimeFunctions.toEpochHoursBucket(1609459200999L, 2), 223536L);
+    Assert.assertEquals(DateTimeFunctions.toEpochHoursBucket(1646092799123L, 4), 114311L);
+  }
+
+  @Test
+  public void testToEpochDaysBucket() {
+    Assert.assertEquals(DateTimeFunctions.toEpochDaysBucket(1609459200999L, 32), 582L);
+    Assert.assertEquals(DateTimeFunctions.toEpochDaysBucket(1646092799123L, 64), 297L);
+  }
+
+  @Test
+  public void testFromEpochSeconds() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochSeconds(1609459200L), 1609459200000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochSeconds(1646092799L), 1646092799000L);
+  }
+
+  @Test
+  public void testFromEpochMinutes() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochMinutes(26824320L), 1609459200000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochMinutes(27434879L), 1646092740000L);
+  }
+
+  @Test
+  public void testFromEpochHours() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochHours(447072L), 1609459200000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochHours(457247L), 1646089200000L);
+  }
+
+  @Test
+  public void testFromEpochDays() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochDays(18628L), 1609459200000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochDays(19051L), 1646006400000L);
+  }
+
+  @Test
+  public void testFromEpochSecondsBucket() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochSecondsBucket(1609459L, 1000), 1609459000000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochSecondsBucket(329218559L, 5), 1646092795000L);
+  }
+
+  @Test
+  public void testFromEpochMinutesBucket() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochMinutesBucket(536486L, 50), 1609458000000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochMinutesBucket(13717439L, 2), 1646092680000L);
+  }
+
+  @Test
+  public void testFromEpochHoursBucket() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochHoursBucket(223536L, 2), 1609459200000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochHoursBucket(114311L, 4), 1646078400000L);
+  }
+
+  @Test
+  public void fromEpochDaysBucket() {
+    Assert.assertEquals(DateTimeFunctions.fromEpochDaysBucket(582L, 32), 1609113600000L);
+    Assert.assertEquals(DateTimeFunctions.fromEpochDaysBucket(297L, 64), 1642291200000L);
+  }
+
+  @Test
+  public void testToTimestamp() {
+    Assert.assertEquals(
+        DateTimeFunctions.toTimestamp(1633375617536L), new Timestamp(1633375617536L));
+  }
+
+  @Test
+  public void testFromTimestamp() {
+    Assert.assertEquals(
+        DateTimeFunctions.fromTimestamp(new Timestamp(1633375617536L)), 1633375617536L);
+  }
+
+  @Test
+  public void toDateTime() {
+    Assert.assertEquals(
+        DateTimeFunctions.toDateTime(1633376098332L, "YYYY-MM-dd'T'HH:mm:ss.SSS z"),
+        "2021-10-04T19:34:58.332 UTC");
+    Assert.assertEquals(
+        DateTimeFunctions.toDateTime(1633376098332L, "yyyy-MM-dd'T'HH:mm:ss.SSSZ"),
+        "2021-10-04T19:34:58.332+0000");
+    Assert.assertEquals(
+        DateTimeFunctions.toDateTime(1633376098332L, "YYYY-MM-dd'T'HH:mm:ss.SSS z"),
+        "2021-10-04T19:34:58.332 UTC");
+    Assert.assertEquals(
+        DateTimeFunctions.toDateTime(1325376000000L, "xxxx"), "2011"); // 2012-01-01 00:00:00
+    Assert.assertEquals(DateTimeFunctions.toDateTime(1325376000000L, "yyyy"), "2012");
+    Assert.assertEquals(DateTimeFunctions.toDateTime(1325376000000L, "YYYY"), "2012");
+    // TODO more formats, all formats
+  }
+
+  @Test
+  public void testFromDateTime() {
+    Assert.assertEquals(
+        DateTimeFunctions.fromDateTime("2021-10-04T19:34:58.332 UTC", "YYYY-MM-dd'T'HH:mm:ss.SSS z"),
+        1633376098332L);
+    Assert.assertEquals(
+        DateTimeFunctions.fromDateTime("2021-10-04T19:34:58.332+0000", "yyyy-MM-dd'T'HH:mm:ss.SSSZ"),
+        1633376098332L);
+    Assert.assertEquals(
+        DateTimeFunctions.fromDateTime("2021-10-04T19:34:58.332 UTC", "YYYY-MM-dd'T'HH:mm:ss.SSS z"),
+        1633376098332L);
+    Assert.assertEquals(
+        DateTimeFunctions.fromDateTime("2011", "xxxx"), 1294012800000L); // strange case, reverse operation is not exact
+    Assert.assertEquals(DateTimeFunctions.fromDateTime("2012", "yyyy"), 1325376000000L);
+    Assert.assertEquals(DateTimeFunctions.fromDateTime("2012", "YYYY"), 1325376000000L);
+    // TODO more formats, all formats

Review comment:
       Add all possible formats




-- 
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: commits-unsubscribe@pinot.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pinot.apache.org
For additional commands, e-mail: commits-help@pinot.apache.org