Skip to content

Commit 43a1662

Browse files
committed
Support bucket transform for columns of type TimeType in Iceberg table
1 parent 9abb67b commit 43a1662

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

presto-docs/src/main/sphinx/connector/iceberg.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,8 @@ Transform Name Source Types
12411241
``Identity`` ``boolean``, ``int``, ``bigint``, ``real``, ``double``, ``decimal``,
12421242
``varchar``, ``varbinary``, ``date``, ``time``, ``timestamp``
12431243

1244-
``Bucket`` ``int``, ``bigint``, ``decimal``, ``varchar``, ``varbinary``, ``date``
1244+
``Bucket`` ``int``, ``bigint``, ``decimal``, ``varchar``, ``varbinary``, ``date``,
1245+
``time``
12451246

12461247
``Truncate`` ``int``, ``bigint``, ``decimal``, ``varchar``, ``varbinary``
12471248

presto-iceberg/src/main/java/com/facebook/presto/iceberg/PartitionTransforms.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import static com.facebook.presto.common.type.Decimals.isShortDecimal;
4343
import static com.facebook.presto.common.type.Decimals.readBigDecimal;
4444
import static com.facebook.presto.common.type.IntegerType.INTEGER;
45+
import static com.facebook.presto.common.type.TimeType.TIME;
4546
import static com.facebook.presto.common.type.TimestampType.TIMESTAMP;
4647
import static com.facebook.presto.common.type.TypeUtils.readNativeValue;
4748
import static com.facebook.presto.common.type.VarbinaryType.VARBINARY;
@@ -51,6 +52,7 @@
5152
import static java.lang.Math.floorDiv;
5253
import static java.util.Objects.requireNonNull;
5354
import static java.util.concurrent.TimeUnit.DAYS;
55+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
5456
import static org.joda.time.chrono.ISOChronology.getInstanceUTC;
5557

5658
public final class PartitionTransforms
@@ -157,6 +159,11 @@ public static ColumnTransform getColumnTransform(PartitionField field, Type type
157159
block -> bucketDate(block, count),
158160
(block, position) -> bucketValueDate(block, position, count));
159161
}
162+
if (type.equals(TIME)) {
163+
return new ColumnTransform(transform, INTEGER,
164+
block -> bucketTime(block, count),
165+
(block, position) -> bucketValueTime(block, position, count));
166+
}
160167
if (type instanceof VarcharType) {
161168
return new ColumnTransform(transform, INTEGER,
162169
block -> bucketVarchar(block, count),
@@ -309,6 +316,16 @@ private static int bucketValueDate(Block block, int position, int count)
309316
return bucketValue(block, position, count, pos -> bucketHash(DATE.getLong(block, pos)));
310317
}
311318

319+
private static Block bucketTime(Block block, int count)
320+
{
321+
return bucketBlock(block, count, position -> bucketHash(MILLISECONDS.toMicros(TIME.getLong(block, position))));
322+
}
323+
324+
private static int bucketValueTime(Block block, int position, int count)
325+
{
326+
return bucketValue(block, position, count, pos -> bucketHash(MILLISECONDS.toMicros(TIME.getLong(block, pos))));
327+
}
328+
312329
private static Block bucketVarchar(Block block, int count)
313330
{
314331
return bucketBlock(block, count, position -> bucketHash(VARCHAR.getSlice(block, position)));

presto-iceberg/src/test/java/com/facebook/presto/iceberg/IcebergDistributedSmokeTestBase.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,12 +1537,39 @@ public void testPartitionTransformOnUUID()
15371537
@Test
15381538
public void testBucketTransformOnTime()
15391539
{
1540-
//TODO: Not yet support bucket transform for time, which was supported by Iceberg
1541-
assertUpdate("create table test_bucket_transform_time(col_time time)" +
1542-
"with (partitioning = ARRAY['bucket(col_time, 2)'])");
1543-
assertQueryFails("insert into test_bucket_transform_time values(time '01:02:03.123')",
1544-
"Unsupported type for 'bucket': 1000: col_time_bucket: bucket\\[2\\]\\(1\\)");
1545-
assertUpdate("drop table if exists test_bucket_transform_time");
1540+
testWithAllFileFormats(this::testBucketTransformsOnTimeForFormat);
1541+
}
1542+
1543+
private void testBucketTransformsOnTimeForFormat(Session session, FileFormat format)
1544+
{
1545+
String select = "SELECT a_bucket, row_count, a.min AS a_min, a.max AS a_max, b.min AS b_min, b.max AS b_max FROM \"test_bucket_transform_on_time$partitions\"";
1546+
1547+
assertUpdate(session, format("CREATE TABLE test_bucket_transform_on_time (a TIME, b BIGINT)" +
1548+
" WITH (\"write.format.default\" = '%s', partitioning = ARRAY['bucket(a, 4)'])", format.name()));
1549+
String insertSql = "INSERT INTO test_bucket_transform_on_time VALUES" +
1550+
"(time '01:02:03.123', 1)," +
1551+
"(time '21:22:50.002', 2)," +
1552+
"(time '12:13:14.345', 3)," +
1553+
"(time '00:00:01.001', 4)," +
1554+
"(time '23:23:59.999', 5)," +
1555+
"(time '00:00:00.000', 6)," +
1556+
"(time '07:31:55.425', 7)";
1557+
assertUpdate(session, insertSql, 7);
1558+
1559+
assertQuery(session, "SELECT COUNT(*) FROM \"test_bucket_transform_on_time$partitions\"", "SELECT 4");
1560+
1561+
assertQuery(session, select + " WHERE a_bucket = 0", "VALUES(0, 2, time '00:00:00.000', time '12:13:14.345', 3, 6)");
1562+
assertQuery(session, select + " WHERE a_bucket = 1", "VALUES(1, 1, time '23:23:59.999', time '23:23:59.999', 5, 5)");
1563+
assertQuery(session, select + " WHERE a_bucket = 2", "VALUES(2, 1, time '21:22:50.002', time '21:22:50.002', 2, 2)");
1564+
assertQuery(session, select + " WHERE a_bucket = 3", "VALUES(3, 3, time '00:00:01.001', time '07:31:55.425', 1, 7)");
1565+
1566+
assertQuery(session, "select * from test_bucket_transform_on_time where a = time '01:02:03.123'",
1567+
"VALUES(time '01:02:03.123', 1)");
1568+
assertQuery(session, "select * from test_bucket_transform_on_time where a > time '01:02:03.123' and a <= time '12:13:14.345'",
1569+
"VALUES(time '07:31:55.425', 7), (time '12:13:14.345', 3)");
1570+
assertQuery(session, "select * from test_bucket_transform_on_time where a in (time '00:00:01.001', time '21:22:50.002')",
1571+
"VALUES(time '00:00:01.001', 4), (time '21:22:50.002', 2)");
1572+
dropTable(session, "test_bucket_transform_on_time");
15461573
}
15471574

15481575
@Test

0 commit comments

Comments
 (0)