|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.facebook.presto.iceberg; |
| 15 | + |
| 16 | +import com.facebook.presto.Session; |
| 17 | +import com.facebook.presto.common.type.TimestampType; |
| 18 | +import com.facebook.presto.common.type.TimestampWithTimeZoneType; |
| 19 | +import com.facebook.presto.common.type.Type; |
| 20 | +import com.facebook.presto.testing.MaterializedResult; |
| 21 | +import com.facebook.presto.testing.MaterializedRow; |
| 22 | +import com.facebook.presto.testing.QueryRunner; |
| 23 | +import com.facebook.presto.tests.AbstractTestQueryFramework; |
| 24 | +import com.google.common.collect.ImmutableMap; |
| 25 | +import org.testng.annotations.DataProvider; |
| 26 | +import org.testng.annotations.Test; |
| 27 | + |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static com.facebook.presto.hive.HiveCommonSessionProperties.PARQUET_BATCH_READ_OPTIMIZATION_ENABLED; |
| 31 | +import static com.facebook.presto.iceberg.IcebergQueryRunner.createIcebergQueryRunner; |
| 32 | +import static org.testng.Assert.assertEquals; |
| 33 | +import static org.testng.Assert.assertTrue; |
| 34 | + |
| 35 | +public class TestIcebergTypes |
| 36 | + extends AbstractTestQueryFramework |
| 37 | +{ |
| 38 | + protected QueryRunner createQueryRunner() throws Exception |
| 39 | + { |
| 40 | + return createIcebergQueryRunner(ImmutableMap.of(), ImmutableMap.of()); |
| 41 | + } |
| 42 | + |
| 43 | + @DataProvider(name = "testTimestampWithTimezone") |
| 44 | + public Object[][] createTestTimestampWithTimezoneData() |
| 45 | + { |
| 46 | + return new Object[][] { |
| 47 | + {Session.builder(getSession()) |
| 48 | + .setCatalogSessionProperty("iceberg", PARQUET_BATCH_READ_OPTIMIZATION_ENABLED, "true") |
| 49 | + .build()}, |
| 50 | + {Session.builder(getSession()) |
| 51 | + .setCatalogSessionProperty("iceberg", PARQUET_BATCH_READ_OPTIMIZATION_ENABLED, "false") |
| 52 | + .build()} |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + @Test(dataProvider = "testTimestampWithTimezone") |
| 57 | + public void testTimestampWithTimezone(Session session) |
| 58 | + { |
| 59 | + QueryRunner runner = getQueryRunner(); |
| 60 | + String timestamptz = "TIMESTAMP '1984-12-08 00:10:00 America/Los_Angeles'"; |
| 61 | + String timestamp = "TIMESTAMP '1984-12-08 00:10:00'"; |
| 62 | + |
| 63 | + dropTableIfExists(runner, session.getCatalog().get(), session.getSchema().get(), "test_timestamptz"); |
| 64 | + assertQuerySucceeds(session, "CREATE TABLE test_timestamptz(a TIMESTAMP WITH TIME ZONE, b TIMESTAMP, c TIMESTAMP WITH TIME ZONE)"); |
| 65 | + |
| 66 | + String row = "(" + timestamptz + ", " + timestamp + ", " + timestamptz + ")"; |
| 67 | + for (int i = 0; i < 10; i++) { |
| 68 | + assertUpdate(session, "INSERT INTO test_timestamptz values " + row, 1); |
| 69 | + } |
| 70 | + |
| 71 | + MaterializedResult initialRows = runner.execute(session, "SELECT * FROM test_timestamptz"); |
| 72 | + |
| 73 | + List<Type> types = initialRows.getTypes(); |
| 74 | + assertTrue(types.get(0) instanceof TimestampWithTimeZoneType); |
| 75 | + assertTrue(types.get(1) instanceof TimestampType); |
| 76 | + |
| 77 | + List<MaterializedRow> rows = initialRows.getMaterializedRows(); |
| 78 | + for (int i = 0; i < 10; i++) { |
| 79 | + assertEquals("[1984-12-08T08:10Z[UTC], 1984-12-08T00:10, 1984-12-08T08:10Z[UTC]]", rows.get(i).toString()); |
| 80 | + } |
| 81 | + |
| 82 | + dropTableIfExists(runner, session.getCatalog().get(), session.getSchema().get(), "test_timestamptz_partition"); |
| 83 | + assertQuerySucceeds(session, "CREATE TABLE test_timestamptz_partition(a TIMESTAMP WITH TIME ZONE, b TIMESTAMP, c TIMESTAMP WITH TIME ZONE) " + |
| 84 | + "WITH (PARTITIONING = ARRAY['b'])"); |
| 85 | + assertUpdate(session, "INSERT INTO test_timestamptz_partition (a, b, c) SELECT a, b, c FROM test_timestamptz", 10); |
| 86 | + |
| 87 | + MaterializedResult partitionRows = runner.execute(session, "SELECT * FROM test_timestamptz"); |
| 88 | + |
| 89 | + List<Type> partitionTypes = partitionRows.getTypes(); |
| 90 | + assertTrue(partitionTypes.get(0) instanceof TimestampWithTimeZoneType); |
| 91 | + assertTrue(partitionTypes.get(1) instanceof TimestampType); |
| 92 | + |
| 93 | + rows = partitionRows.getMaterializedRows(); |
| 94 | + for (int i = 0; i < 10; i++) { |
| 95 | + assertEquals("[1984-12-08T08:10Z[UTC], 1984-12-08T00:10, 1984-12-08T08:10Z[UTC]]", rows.get(i).toString()); |
| 96 | + } |
| 97 | + |
| 98 | + String earlyTimestamptz = "TIMESTAMP '1980-12-08 00:10:00 America/Los_Angeles'"; |
| 99 | + dropTableIfExists(runner, session.getCatalog().get(), session.getSchema().get(), "test_timestamptz_filter"); |
| 100 | + assertQuerySucceeds(session, "CREATE TABLE test_timestamptz_filter(a TIMESTAMP WITH TIME ZONE)"); |
| 101 | + |
| 102 | + for (int i = 0; i < 5; i++) { |
| 103 | + assertUpdate(session, "INSERT INTO test_timestamptz_filter VALUES (" + earlyTimestamptz + ")", 1); |
| 104 | + } |
| 105 | + for (int i = 0; i < 5; i++) { |
| 106 | + assertUpdate(session, "INSERT INTO test_timestamptz_filter VALUES (" + timestamptz + ")", 1); |
| 107 | + } |
| 108 | + |
| 109 | + MaterializedResult lateRows = runner.execute(session, "SELECT a FROM test_timestamptz_filter WHERE a > " + earlyTimestamptz); |
| 110 | + assertEquals(lateRows.getMaterializedRows().size(), 5); |
| 111 | + |
| 112 | + MaterializedResult lateRowsFromEquals = runner.execute(session, "SELECT a FROM test_timestamptz_filter WHERE a = " + timestamptz); |
| 113 | + com.facebook.presto.testing.assertions.Assert.assertEquals(lateRows, lateRowsFromEquals); |
| 114 | + |
| 115 | + MaterializedResult earlyRows = runner.execute(session, "SELECT a FROM test_timestamptz_filter WHERE a < " + timestamptz); |
| 116 | + assertEquals(earlyRows.getMaterializedRows().size(), 5); |
| 117 | + |
| 118 | + MaterializedResult earlyRowsFromEquals = runner.execute(session, "SELECT a FROM test_timestamptz_filter WHERE a = " + earlyTimestamptz); |
| 119 | + com.facebook.presto.testing.assertions.Assert.assertEquals(earlyRows, earlyRowsFromEquals); |
| 120 | + } |
| 121 | +} |
0 commit comments