From ce41ce1b7e97d68fe7c54156f14b73713f275472 Mon Sep 17 00:00:00 2001 From: Kamil Endruszkiewicz Date: Mon, 20 Feb 2023 15:27:59 +0100 Subject: [PATCH] Extract parameter parsing from ThriftMetastoreUtil --- .../ThriftMetastoreParameterParserUtils.java | 37 +++++++++++++++++++ .../metastore/thrift/ThriftMetastoreUtil.java | 22 +++-------- 2 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/thrift/ThriftMetastoreParameterParserUtils.java diff --git a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/thrift/ThriftMetastoreParameterParserUtils.java b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/thrift/ThriftMetastoreParameterParserUtils.java new file mode 100644 index 000000000000..6517b5d45bda --- /dev/null +++ b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/thrift/ThriftMetastoreParameterParserUtils.java @@ -0,0 +1,37 @@ +/* + * Licensed 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 io.trino.plugin.hive.metastore.thrift; + +import com.google.common.primitives.Longs; + +import javax.annotation.Nullable; + +import java.util.OptionalLong; + +class ThriftMetastoreParameterParserUtils +{ + private ThriftMetastoreParameterParserUtils() {} + + static OptionalLong toLong(@Nullable String parameterValue) + { + if (parameterValue == null) { + return OptionalLong.empty(); + } + Long longValue = Longs.tryParse(parameterValue); + if (longValue == null || longValue < 0) { + return OptionalLong.empty(); + } + return OptionalLong.of(longValue); + } +} diff --git a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/thrift/ThriftMetastoreUtil.java b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/thrift/ThriftMetastoreUtil.java index d4cf8973f966..5cad97cfa4da 100644 --- a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/thrift/ThriftMetastoreUtil.java +++ b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/metastore/thrift/ThriftMetastoreUtil.java @@ -17,7 +17,6 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Streams; -import com.google.common.primitives.Longs; import com.google.common.primitives.Shorts; import io.trino.hive.thrift.metastore.BinaryColumnStatsData; import io.trino.hive.thrift.metastore.BooleanColumnStatsData; @@ -125,6 +124,7 @@ import static io.trino.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege.OWNERSHIP; import static io.trino.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege.SELECT; import static io.trino.plugin.hive.metastore.HivePrivilegeInfo.HivePrivilege.UPDATE; +import static io.trino.plugin.hive.metastore.thrift.ThriftMetastoreParameterParserUtils.toLong; import static io.trino.plugin.hive.type.Category.PRIMITIVE; import static io.trino.spi.security.PrincipalType.ROLE; import static io.trino.spi.security.PrincipalType.USER; @@ -742,25 +742,13 @@ public static Set parsePrivilege(PrivilegeGrantInfo userGrant public static HiveBasicStatistics getHiveBasicStatistics(Map parameters) { - OptionalLong numFiles = parse(parameters.get(NUM_FILES)); - OptionalLong numRows = parse(parameters.get(NUM_ROWS)); - OptionalLong inMemoryDataSizeInBytes = parse(parameters.get(RAW_DATA_SIZE)); - OptionalLong onDiskDataSizeInBytes = parse(parameters.get(TOTAL_SIZE)); + OptionalLong numFiles = toLong(parameters.get(NUM_FILES)); + OptionalLong numRows = toLong(parameters.get(NUM_ROWS)); + OptionalLong inMemoryDataSizeInBytes = toLong(parameters.get(RAW_DATA_SIZE)); + OptionalLong onDiskDataSizeInBytes = toLong(parameters.get(TOTAL_SIZE)); return new HiveBasicStatistics(numFiles, numRows, inMemoryDataSizeInBytes, onDiskDataSizeInBytes); } - private static OptionalLong parse(@Nullable String parameterValue) - { - if (parameterValue == null) { - return OptionalLong.empty(); - } - Long longValue = Longs.tryParse(parameterValue); - if (longValue == null || longValue < 0) { - return OptionalLong.empty(); - } - return OptionalLong.of(longValue); - } - public static Map updateStatisticsParameters(Map parameters, HiveBasicStatistics statistics) { ImmutableMap.Builder result = ImmutableMap.builder();