From 99293673e7a3f3fd29f2528b9b7dbbe5f58291c5 Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Mon, 5 Dec 2022 23:52:17 +0100 Subject: [PATCH 1/2] Use switch expresion in HiveCompressionCodec Use switch expression to emphasize all options are covered. --- .../plugin/hive/HiveCompressionCodecs.java | 40 +++++++------------ 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveCompressionCodecs.java b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveCompressionCodecs.java index 8092db99bf73..fed4434d1c63 100644 --- a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveCompressionCodecs.java +++ b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveCompressionCodecs.java @@ -48,35 +48,23 @@ public static HiveCompressionCodec selectCompressionCodec(HiveCompressionOption private static HiveCompressionCodec selectCompressionCodec(HiveCompressionOption compressionOption) { - switch (compressionOption) { - case NONE: - return HiveCompressionCodec.NONE; - case SNAPPY: - return HiveCompressionCodec.SNAPPY; - case LZ4: - return HiveCompressionCodec.LZ4; - case ZSTD: - return HiveCompressionCodec.ZSTD; - case GZIP: - return HiveCompressionCodec.GZIP; - } - throw new IllegalArgumentException("Unknown compressionOption " + compressionOption); + return switch (compressionOption) { + case NONE -> HiveCompressionCodec.NONE; + case SNAPPY -> HiveCompressionCodec.SNAPPY; + case LZ4 -> HiveCompressionCodec.LZ4; + case ZSTD -> HiveCompressionCodec.ZSTD; + case GZIP -> HiveCompressionCodec.GZIP; + }; } private static HiveCompressionCodec selectCompressionCodecForUnknownStorageFormat(HiveCompressionOption compressionOption) { - switch (compressionOption) { - case NONE: - return HiveCompressionCodec.NONE; - case SNAPPY: - return HiveCompressionCodec.SNAPPY; - case LZ4: - return HiveCompressionCodec.LZ4; - case ZSTD: - return HiveCompressionCodec.ZSTD; - case GZIP: - return HiveCompressionCodec.GZIP; - } - throw new IllegalArgumentException("Unknown compressionOption " + compressionOption); + return switch (compressionOption) { + case NONE -> HiveCompressionCodec.NONE; + case SNAPPY -> HiveCompressionCodec.SNAPPY; + case LZ4 -> HiveCompressionCodec.LZ4; + case ZSTD -> HiveCompressionCodec.ZSTD; + case GZIP -> HiveCompressionCodec.GZIP; + }; } } From cdca24b9bcb9ceb12e36ec3a1b487a20c73e74a6 Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Mon, 5 Dec 2022 23:56:13 +0100 Subject: [PATCH 2/2] Use enhanced instanceof in Delta connector --- .../trino/plugin/deltalake/DeltaHiveTypeTranslator.java | 9 +++------ .../io/trino/plugin/deltalake/DeltaLakeInputInfo.java | 3 +-- .../transactionlog/DeltaLakeParquetStatisticsUtils.java | 3 +-- .../deltalake/transactionlog/DeltaLakeSchemaSupport.java | 9 +++------ .../checkpoint/CheckpointSchemaManager.java | 3 +-- .../transactionlog/checkpoint/TestCheckpointWriter.java | 7 +++---- 6 files changed, 12 insertions(+), 22 deletions(-) diff --git a/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/DeltaHiveTypeTranslator.java b/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/DeltaHiveTypeTranslator.java index 0c0fa73d7d81..67b6421a4387 100644 --- a/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/DeltaHiveTypeTranslator.java +++ b/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/DeltaHiveTypeTranslator.java @@ -96,8 +96,7 @@ public static TypeInfo translate(Type type) if (DOUBLE.equals(type)) { return HIVE_DOUBLE.getTypeInfo(); } - if (type instanceof VarcharType) { - VarcharType varcharType = (VarcharType) type; + if (type instanceof VarcharType varcharType) { if (varcharType.isUnbounded()) { return HIVE_STRING.getTypeInfo(); } @@ -106,8 +105,7 @@ public static TypeInfo translate(Type type) } throw new TrinoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported VARCHAR types: VARCHAR(<=%d), VARCHAR.", type, HiveVarchar.MAX_VARCHAR_LENGTH)); } - if (type instanceof CharType) { - CharType charType = (CharType) type; + if (type instanceof CharType charType) { int charLength = charType.getLength(); if (charLength <= HiveChar.MAX_CHAR_LENGTH) { return getCharTypeInfo(charLength); @@ -129,8 +127,7 @@ public static TypeInfo translate(Type type) verify(((TimestampType) type).getPrecision() == 3, "Unsupported type: %s", type); return HIVE_TIMESTAMP.getTypeInfo(); } - if (type instanceof DecimalType) { - DecimalType decimalType = (DecimalType) type; + if (type instanceof DecimalType decimalType) { return new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()); } if (isArrayType(type)) { diff --git a/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/DeltaLakeInputInfo.java b/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/DeltaLakeInputInfo.java index eb884add9290..e65b1cf0c3a3 100644 --- a/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/DeltaLakeInputInfo.java +++ b/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/DeltaLakeInputInfo.java @@ -40,10 +40,9 @@ public boolean equals(Object o) if (this == o) { return true; } - if (!(o instanceof DeltaLakeInputInfo)) { + if (!(o instanceof DeltaLakeInputInfo that)) { return false; } - DeltaLakeInputInfo that = (DeltaLakeInputInfo) o; return partitioned == that.partitioned; } diff --git a/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/transactionlog/DeltaLakeParquetStatisticsUtils.java b/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/transactionlog/DeltaLakeParquetStatisticsUtils.java index 7a294e9b7926..1953479bc4c9 100644 --- a/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/transactionlog/DeltaLakeParquetStatisticsUtils.java +++ b/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/transactionlog/DeltaLakeParquetStatisticsUtils.java @@ -184,8 +184,7 @@ private static Object toJsonValue(Type type, @Nullable Object value) if (type == DOUBLE) { return value; } - if (type instanceof DecimalType) { - DecimalType decimalType = (DecimalType) type; + if (type instanceof DecimalType decimalType) { if (decimalType.isShort()) { return Decimals.toString((long) value, decimalType.getScale()); } diff --git a/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/transactionlog/DeltaLakeSchemaSupport.java b/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/transactionlog/DeltaLakeSchemaSupport.java index ac4511642d64..eaf5a70e137b 100644 --- a/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/transactionlog/DeltaLakeSchemaSupport.java +++ b/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/transactionlog/DeltaLakeSchemaSupport.java @@ -254,8 +254,7 @@ private static Optional serializeSupportedPrimitiveType(Type type) if (type instanceof VarcharType) { return Optional.of("string"); } - if (type instanceof DecimalType) { - DecimalType decimalType = (DecimalType) type; + if (type instanceof DecimalType decimalType) { return Optional.of(String.format("decimal(%s,%s)", decimalType.getPrecision(), decimalType.getScale())); } return Optional.ofNullable(PRIMITIVE_TYPE_MAPPING.get(type)); @@ -288,14 +287,12 @@ private static void validateStructuralType(Optional rootType, Type type) validateType(rootType, ((ArrayType) type).getElementType()); } - if (type instanceof MapType) { - MapType mapType = (MapType) type; + if (type instanceof MapType mapType) { validateType(rootType, mapType.getKeyType()); validateType(rootType, mapType.getValueType()); } - if (type instanceof RowType) { - RowType rowType = (RowType) type; + if (type instanceof RowType rowType) { rowType.getFields().forEach(field -> validateType(rootType, field.getType())); } } diff --git a/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/transactionlog/checkpoint/CheckpointSchemaManager.java b/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/transactionlog/checkpoint/CheckpointSchemaManager.java index af99d18f3aa6..7666e9137d89 100644 --- a/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/transactionlog/checkpoint/CheckpointSchemaManager.java +++ b/plugin/trino-delta-lake/src/main/java/io/trino/plugin/deltalake/transactionlog/checkpoint/CheckpointSchemaManager.java @@ -156,8 +156,7 @@ public RowType getAddEntryType(MetadataEntry metadataEntry, boolean requireWrite private static RowType.Field buildNullCountType(Optional columnName, Type columnType) { - if (columnType instanceof RowType) { - RowType rowType = (RowType) columnType; + if (columnType instanceof RowType rowType) { if (columnName.isPresent()) { return RowType.field( columnName.get(), diff --git a/plugin/trino-delta-lake/src/test/java/io/trino/plugin/deltalake/transactionlog/checkpoint/TestCheckpointWriter.java b/plugin/trino-delta-lake/src/test/java/io/trino/plugin/deltalake/transactionlog/checkpoint/TestCheckpointWriter.java index 67bdf9d95972..ce1901ce63ff 100644 --- a/plugin/trino-delta-lake/src/test/java/io/trino/plugin/deltalake/transactionlog/checkpoint/TestCheckpointWriter.java +++ b/plugin/trino-delta-lake/src/test/java/io/trino/plugin/deltalake/transactionlog/checkpoint/TestCheckpointWriter.java @@ -454,8 +454,7 @@ private Optional> makeComparableStatistics(Optional comparableStats = ImmutableMap.builder(); for (String key : stats.keySet()) { Object statsValue = stats.get(key); - if (statsValue instanceof RowBlock) { - RowBlock rowBlock = (RowBlock) statsValue; + if (statsValue instanceof RowBlock rowBlock) { ColumnarRow columnarRow = toColumnarRow(rowBlock); int size = columnarRow.getFieldCount(); ImmutableList logicalSizes = IntStream.range(0, size) @@ -464,8 +463,8 @@ private Optional> makeComparableStatistics(Optional