aboutsummaryrefslogtreecommitdiff
path: root/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/TestNestedDateTimeTimestamp.java
blob: 6a0aa78fc1151b468728cc5f97a5446a01696df8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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.drill.exec.physical.impl;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.drill.exec.expr.fn.impl.DateUtility;
import org.apache.drill.exec.rpc.user.QueryDataBatch;
import org.apache.drill.test.BaseTestQuery;
import org.apache.drill.test.TestBuilder;
import org.junit.Assert;
import org.junit.Test;

/**
 * For DRILL-6242, output for Date, Time, Timestamp should use different classes
 */
public class TestNestedDateTimeTimestamp extends BaseTestQuery {
  private static final String              DATAFILE       = "cp.`datetime.parquet`";
  private static final Map<String, Object> expectedRecord = new TreeMap<String, Object>();

  static {
    /**
     * Data in the parquet file represents this equivalent JSON, but with typed
     * data, time, and timestamps: { "date" : "1970-01-11", "time" :
     * "00:00:03.600", "timestamp" : "2018-03-23T17:40:52.123Z", "date_list" : [
     * "1970-01-11" ], "time_list" : [ "00:00:03.600" ], "timestamp_list" : [
     * "2018-03-23T17:40:52.123Z" ], "time_map" : { "date" : "1970-01-11",
     * "time" : "00:00:03.600", "timestamp" : "2018-03-23T17:40:52.123Z" } }
     *
     * Note that when the above data is read in to Drill, Drill modifies the
     * timestamp to local time zone, and preserving the <date> and <time>
     * values. This effectively changes the timestamp, if the time zone is not
     * UTC.
     */

    LocalDate date = DateUtility.parseLocalDate("1970-01-11");
    LocalTime time = DateUtility.parseLocalTime("00:00:03.600");
    LocalDateTime timestamp = DateUtility.parseLocalDateTime("2018-03-23 17:40:52.123");
    expectedRecord.put("`date`", date);
    expectedRecord.put("`time`", time);
    expectedRecord.put("`timestamp`", timestamp);
    expectedRecord.put("`date_list`", Arrays.asList(date));
    expectedRecord.put("`time_list`", Arrays.asList(time));
    expectedRecord.put("`timestamp_list`", Arrays.asList(timestamp));
    Map<String, Object> nestedMap = new TreeMap<String, Object>();
    nestedMap.put("date", date);
    nestedMap.put("time", time);
    nestedMap.put("timestamp", timestamp);

    expectedRecord.put("`time_map`", nestedMap);
  }

  /**
   * Test reading of from the parquet file that contains nested time, date, and
   * timestamp
   */
  @Test
  public void testNested() throws Exception {
    String query = String.format("select * from %s limit 1", DATAFILE);
    testBuilder().sqlQuery(query).ordered().baselineRecords(Arrays.asList(expectedRecord)).build().run();
  }

  /**
   * Test timeofday() function.
   */
  @Test
  public void testTimeOfDay() throws Exception {
    ZonedDateTime now = ZonedDateTime.now();
    // check the time zone
    testBuilder().sqlQuery("select substr(timeofday(),25) as n from (values(1))").ordered().baselineColumns("n")
        .baselineValues(DateUtility.formatTimeStampTZ.format(now).substring(24)).build().run();
  }

  /**
   * Test the textual display to make sure it is consistent with actual JSON
   * output
   */
  @Test
  public void testNestedDateTimePrint() throws Exception {
    List<QueryDataBatch> resultList = testSqlWithResults(String.format("select * from %s limit 1", DATAFILE));
    String actual = getResultString(resultList, " | ");

    final String expected = "date | time | timestamp | date_list | time_list | timestamp_list | time_map\n"
        + "1970-01-11 | 00:00:03.600 | 2018-03-23 17:40:52.123 | [\"1970-01-11\"] | [\"00:00:03.600\"] | [\"2018-03-23 17:40:52.123\"] | {\"date\":\"1970-01-11\",\"time\":\"00:00:03.600\",\"timestamp\":\"2018-03-23 17:40:52.123\"}";

    Assert.assertEquals(expected.trim(), actual.trim());
  }

  /**
   * Test the json output is consistent as before
   */
  @Test
  public void testNestedDateTimeCTASJson() throws Exception {
    String query = String.format("select * from %s limit 1", DATAFILE);
    String testName = "ctas_nested_datetime";
    try {
      test("alter session set store.format = 'json'");
      test("alter session set store.json.extended_types = false");
      test("use dfs.tmp");
      test("create table " + testName + "_json as " + query);

      final String readQuery = "select * from `" + testName + "_json` t1 ";

      testBuilder().sqlQuery(readQuery).ordered().jsonBaselineFile("baseline_nested_datetime.json").build().run();
    } finally {
      test("drop table " + testName + "_json");
      test("alter session reset store.format ");
      test("alter session reset store.json.extended_types ");
    }
  }

  /**
   * Test the extended json output is consistent as before
   */
  @Test
  public void testNestedDateTimeCTASExtendedJson() throws Exception {
    String query = String.format("select * from %s limit 1", DATAFILE);
    String testName = "ctas_nested_datetime_extended";
    try {
      test("alter session set store.format = 'json'");
      test("alter session set store.json.extended_types = true");
      test("use dfs.tmp");
      test("create table " + testName + "_json as " + query);

      final String readQuery = "select * from `" + testName + "_json` t1 ";

      testBuilder().sqlQuery(readQuery).ordered().jsonBaselineFile("datetime.parquet").build().run();
    } finally {
      test("drop table " + testName + "_json");
      test("alter session reset store.format ");
      test("alter session reset store.json.extended_types ");
    }
  }

  /**
   * Test parquet output is consistent as before
   */
  @Test
  public void testNestedDateTimeCTASParquet() throws Exception {
    String query = String.format("select * from %s limit 1", DATAFILE);
    String testName = "ctas_nested_datetime_extended";
    try {
      test("alter session set store.format = 'parquet'");
      test("use dfs.tmp");
      test("create table " + testName + "_parquet as " + query);

      final String readQuery = "select * from `" + testName + "_parquet` t1 ";

      testBuilder().sqlQuery(readQuery).ordered().jsonBaselineFile("datetime.parquet").build().run();
    } finally {
      test("drop table " + testName + "_parquet");
      test("alter session reset store.format ");
    }
  }

  /**
   * Testing time zone change and revert
   */
  @Test
  public void testTimeZoneChangeAndReverse() throws Exception {
    long timeMillis[] = new long[] { 864000000L, 3600L, 1521826852123L };

    for (int i = 0; i < timeMillis.length; i++) {
      OffsetDateTime time1 = OffsetDateTime.ofInstant(Instant.ofEpochMilli(timeMillis[i]), ZoneOffset.UTC);
      OffsetDateTime time2 = time1.toLocalDateTime().atZone(ZoneOffset.systemDefault()).toOffsetDateTime();
      OffsetDateTime time3 = time2.toLocalDateTime().atOffset(ZoneOffset.UTC);

      Assert.assertEquals(time1.toString(), time3.toString());
      Assert.assertEquals(time1.toString().substring(0, 16), time2.toString().substring(0, 16));
    }
  }

  @Test
  public void testDateUtilityParser() {
    LocalDateTime timestamp3 = TestBuilder.convertToLocalDateTime("1970-01-01 00:00:00.000");
    Assert.assertEquals(timestamp3,
        ZonedDateTime.ofInstant(Instant.parse("1970-01-01T00:00:00Z"), ZoneOffset.systemDefault()).toLocalDateTime());

    String in[] = new String[] {
        "1970-01-01",
        "1970-01-01 20:12:32",
        "1970-01-01 20:12:32.32",
        "1970-01-01 20:12:32.032",
        "1970-01-01 20:12:32.32 +0800",
        "1970-1-01",
        "1970-01-1 2:12:32",
        "1970-01-01 20:12:3.32",
        "1970-01-01 20:12:32.032",
        "1970-01-01 20:2:32.32 +0800" };
    for (String i : in) {
      LocalDateTime parsed = DateUtility.parseBest(i);
      Assert.assertNotNull(parsed);
    }

    // parse iso parser
    String isoTimestamp[] = new String[] {
        "2015-03-12T21:54:31.809+0530",
        "2015-03-12T21:54:31.809Z",
        "2015-03-12T21:54:31.809-0000",
        "2015-03-12T21:54:31.809-0800"
    };

    for (String s : isoTimestamp) {
      OffsetDateTime t = OffsetDateTime.parse(s, DateUtility.isoFormatTimeStamp);
      Assert.assertNotNull(t);
      Assert.assertNotNull(DateUtility.isoFormatTimeStamp.format(t));
    }
  }
}