Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More metastore cleanup #22899

Merged
merged 12 commits into from
Jan 2, 2025
40 changes: 40 additions & 0 deletions lib/trino-metastore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,31 @@
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>concurrent</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>configuration</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>slice</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>units</artifactId>
</dependency>

<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
Expand All @@ -53,11 +73,31 @@
<artifactId>opentelemetry-semconv</artifactId>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-cache</artifactId>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-spi</artifactId>
</dependency>

<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
</dependency>

<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>

<dependency>
<groupId>org.weakref</groupId>
<artifactId>jmxutils</artifactId>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>junit-extensions</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.plugin.hive.metastore;
package io.trino.metastore;

import io.trino.metastore.HiveMetastore;
import io.trino.spi.security.ConnectorIdentity;

import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.google.errorprone.annotations.Immutable;
import io.trino.spi.connector.SchemaTableName;

import java.util.HexFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -221,67 +220,4 @@ public Partition build()
return new Partition(databaseName, tableName, values, storageBuilder.build(), columns, parameters);
}
}

public static List<String> toPartitionValues(String partitionName)
{
// mimics Warehouse.makeValsFromName
ImmutableList.Builder<String> resultBuilder = ImmutableList.builder();
int start = 0;
while (true) {
while (start < partitionName.length() && partitionName.charAt(start) != '=') {
start++;
}
start++;
int end = start;
while (end < partitionName.length() && partitionName.charAt(end) != '/') {
end++;
}
if (start > partitionName.length()) {
break;
}
resultBuilder.add(unescapePathName(partitionName.substring(start, end)));
start = end + 1;
}
return resultBuilder.build();
}

// copy of org.apache.hadoop.hive.common.FileUtils#unescapePathName
@SuppressWarnings("NumericCastThatLosesPrecision")
public static String unescapePathName(String path)
{
// fast path, no escaped characters and therefore no copying necessary
int escapedAtIndex = path.indexOf('%');
if (escapedAtIndex < 0 || escapedAtIndex + 2 >= path.length()) {
return path;
}

// slow path, unescape into a new string copy
StringBuilder sb = new StringBuilder();
int fromIndex = 0;
while (escapedAtIndex >= 0 && escapedAtIndex + 2 < path.length()) {
// preceding sequence without escaped characters
if (escapedAtIndex > fromIndex) {
sb.append(path, fromIndex, escapedAtIndex);
}
// try to parse the to digits after the percent sign as hex
try {
int code = HexFormat.fromHexDigits(path, escapedAtIndex + 1, escapedAtIndex + 3);
sb.append((char) code);
// advance past the percent sign and both hex digits
fromIndex = escapedAtIndex + 3;
}
catch (NumberFormatException e) {
// invalid escape sequence, only advance past the percent sign
sb.append('%');
fromIndex = escapedAtIndex + 1;
}
// find next escaped character
escapedAtIndex = path.indexOf('%', fromIndex);
}
// trailing sequence without escaped characters
if (fromIndex < path.length()) {
sb.append(path, fromIndex, path.length());
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package io.trino.metastore;

import static com.google.common.base.Preconditions.checkArgument;
import static io.trino.metastore.Partition.toPartitionValues;
import static io.trino.metastore.Partitions.toPartitionValues;
import static java.util.Objects.requireNonNull;

public class PartitionWithStatistics
Expand Down
154 changes: 154 additions & 0 deletions lib/trino-metastore/src/main/java/io/trino/metastore/Partitions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* 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.metastore;

import com.google.common.base.CharMatcher;
import com.google.common.collect.ImmutableList;

import java.util.HexFormat;
import java.util.List;

import static com.google.common.base.Strings.isNullOrEmpty;
import static java.util.Locale.ENGLISH;

public final class Partitions
{
public static final String HIVE_DEFAULT_DYNAMIC_PARTITION = "__HIVE_DEFAULT_PARTITION__";

private static final HexFormat HEX_UPPER_FORMAT = HexFormat.of().withUpperCase();

private static final CharMatcher PATH_CHAR_TO_ESCAPE = CharMatcher.inRange((char) 0, (char) 31)
.or(CharMatcher.anyOf("\"#%'*/:=?\\\u007F{[]^"))
.precomputed();

private Partitions() {}

public static List<String> toPartitionValues(String partitionName)
{
// mimics Warehouse.makeValsFromName
ImmutableList.Builder<String> resultBuilder = ImmutableList.builder();
int start = 0;
while (true) {
while (start < partitionName.length() && partitionName.charAt(start) != '=') {
start++;
}
start++;
int end = start;
while (end < partitionName.length() && partitionName.charAt(end) != '/') {
end++;
}
if (start > partitionName.length()) {
break;
}
resultBuilder.add(unescapePathName(partitionName.substring(start, end)));
start = end + 1;
}
return resultBuilder.build();
}

// copy of org.apache.hadoop.hive.common.FileUtils#unescapePathName
@SuppressWarnings("NumericCastThatLosesPrecision")
public static String unescapePathName(String path)
{
// fast path, no escaped characters and therefore no copying necessary
int escapedAtIndex = path.indexOf('%');
if (escapedAtIndex < 0 || escapedAtIndex + 2 >= path.length()) {
return path;
}

// slow path, unescape into a new string copy
StringBuilder sb = new StringBuilder();
int fromIndex = 0;
while (escapedAtIndex >= 0 && escapedAtIndex + 2 < path.length()) {
// preceding sequence without escaped characters
if (escapedAtIndex > fromIndex) {
sb.append(path, fromIndex, escapedAtIndex);
}
// try to parse the to digits after the percent sign as hex
try {
int code = HexFormat.fromHexDigits(path, escapedAtIndex + 1, escapedAtIndex + 3);
sb.append((char) code);
// advance past the percent sign and both hex digits
fromIndex = escapedAtIndex + 3;
}
catch (NumberFormatException e) {
// invalid escape sequence, only advance past the percent sign
sb.append('%');
fromIndex = escapedAtIndex + 1;
}
// find next escaped character
escapedAtIndex = path.indexOf('%', fromIndex);
}
// trailing sequence without escaped characters
if (fromIndex < path.length()) {
sb.append(path, fromIndex, path.length());
}
return sb.toString();
}

// copy of org.apache.hadoop.hive.common.FileUtils#escapePathName
public static String escapePathName(String path)
{
if (isNullOrEmpty(path)) {
return HIVE_DEFAULT_DYNAMIC_PARTITION;
}

// Fast-path detection, no escaping and therefore no copying necessary
int escapeAtIndex = PATH_CHAR_TO_ESCAPE.indexIn(path);
if (escapeAtIndex < 0) {
return path;
}

// slow path, escape beyond the first required escape character into a new string
StringBuilder sb = new StringBuilder();
int fromIndex = 0;
while (escapeAtIndex >= 0 && escapeAtIndex < path.length()) {
// preceding characters without escaping needed
if (escapeAtIndex > fromIndex) {
sb.append(path, fromIndex, escapeAtIndex);
}
// escape single character
char c = path.charAt(escapeAtIndex);
sb.append('%').append(HEX_UPPER_FORMAT.toHighHexDigit(c)).append(HEX_UPPER_FORMAT.toLowHexDigit(c));
// find next character to escape
fromIndex = escapeAtIndex + 1;
if (fromIndex < path.length()) {
escapeAtIndex = PATH_CHAR_TO_ESCAPE.indexIn(path, fromIndex);
}
else {
escapeAtIndex = -1;
}
}
// trailing characters without escaping needed
if (fromIndex < path.length()) {
sb.append(path, fromIndex, path.length());
}
return sb.toString();
}

// copy of org.apache.hadoop.hive.common.FileUtils#makePartName
public static String makePartName(List<String> columns, List<String> values)
{
StringBuilder name = new StringBuilder();
for (int i = 0; i < columns.size(); i++) {
if (i > 0) {
name.append('/');
}
name.append(escapePathName(columns.get(i).toLowerCase(ENGLISH)));
name.append('=');
name.append(escapePathName(values.get(i)));
}
return name.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.plugin.hive.metastore;
package io.trino.metastore;

import com.google.inject.BindingAnnotation;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.plugin.hive;
package io.trino.metastore;

import io.trino.spi.TrinoException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.plugin.hive;
package io.trino.metastore;

import io.trino.spi.TrinoException;
import io.trino.spi.connector.SchemaTableName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.plugin.hive.metastore.cache;
package io.trino.metastore.cache;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheLoader;
Expand Down Expand Up @@ -44,9 +44,6 @@
import io.trino.metastore.StatisticsUpdateMode;
import io.trino.metastore.Table;
import io.trino.metastore.TableInfo;
import io.trino.plugin.hive.metastore.HivePartitionName;
import io.trino.plugin.hive.metastore.HiveTableName;
import io.trino.plugin.hive.metastore.PartitionFilter;
import io.trino.spi.TrinoException;
import io.trino.spi.connector.SchemaTableName;
import io.trino.spi.function.LanguageFunction;
Expand Down Expand Up @@ -85,13 +82,13 @@
import static com.google.common.util.concurrent.Futures.immediateFuture;
import static io.trino.cache.CacheUtils.invalidateAllIf;
import static io.trino.cache.CacheUtils.uncheckedCacheGet;
import static io.trino.plugin.hive.metastore.HivePartitionName.hivePartitionName;
import static io.trino.plugin.hive.metastore.HiveTableName.hiveTableName;
import static io.trino.plugin.hive.metastore.PartitionFilter.partitionFilter;
import static io.trino.plugin.hive.metastore.cache.CachingHiveMetastore.ObjectType.OTHER;
import static io.trino.plugin.hive.metastore.cache.CachingHiveMetastore.ObjectType.PARTITION;
import static io.trino.plugin.hive.metastore.cache.CachingHiveMetastore.ObjectType.STATS;
import static io.trino.plugin.hive.util.HiveUtil.makePartName;
import static io.trino.metastore.Partitions.makePartName;
import static io.trino.metastore.cache.CachingHiveMetastore.ObjectType.OTHER;
import static io.trino.metastore.cache.CachingHiveMetastore.ObjectType.PARTITION;
import static io.trino.metastore.cache.CachingHiveMetastore.ObjectType.STATS;
import static io.trino.metastore.cache.HivePartitionName.hivePartitionName;
import static io.trino.metastore.cache.HiveTableName.hiveTableName;
import static io.trino.metastore.cache.PartitionFilter.partitionFilter;
import static java.util.Collections.unmodifiableSet;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.plugin.hive.metastore.cache;
package io.trino.metastore.cache;

import io.airlift.configuration.Config;
import io.airlift.units.Duration;
Expand Down
Loading
Loading