io.trino.metastore.Partition Maven / Gradle / Ivy
/*
* 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.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
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;
import java.util.function.Consumer;
import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;
@Immutable
public class Partition
{
private final String databaseName;
private final String tableName;
private final List values;
private final Storage storage;
private final List columns;
private final Map parameters;
@JsonCreator
public Partition(
@JsonProperty("databaseName") String databaseName,
@JsonProperty("tableName") String tableName,
@JsonProperty("values") List values,
@JsonProperty("storage") Storage storage,
@JsonProperty("columns") List columns,
@JsonProperty("parameters") Map parameters)
{
this.databaseName = requireNonNull(databaseName, "databaseName is null");
this.tableName = requireNonNull(tableName, "tableName is null");
this.values = ImmutableList.copyOf(requireNonNull(values, "values is null"));
this.storage = requireNonNull(storage, "storage is null");
this.columns = ImmutableList.copyOf(requireNonNull(columns, "columns is null"));
this.parameters = ImmutableMap.copyOf(requireNonNull(parameters, "parameters is null"));
}
@JsonProperty
public String getDatabaseName()
{
return databaseName;
}
@JsonProperty
public String getTableName()
{
return tableName;
}
@JsonIgnore
public SchemaTableName getSchemaTableName()
{
return new SchemaTableName(databaseName, tableName);
}
@JsonProperty
public List getValues()
{
return values;
}
@JsonProperty
public Storage getStorage()
{
return storage;
}
@JsonProperty
public List getColumns()
{
return columns;
}
@JsonProperty
public Map getParameters()
{
return parameters;
}
@Override
public String toString()
{
return toStringHelper(this)
.add("databaseName", databaseName)
.add("tableName", tableName)
.add("values", values)
.toString();
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Partition partition = (Partition) o;
return Objects.equals(databaseName, partition.databaseName) &&
Objects.equals(tableName, partition.tableName) &&
Objects.equals(values, partition.values) &&
Objects.equals(storage, partition.storage) &&
Objects.equals(columns, partition.columns) &&
Objects.equals(parameters, partition.parameters);
}
@Override
public int hashCode()
{
return Objects.hash(databaseName, tableName, values, storage, columns, parameters);
}
public static Builder builder()
{
return new Builder();
}
public static Builder builder(Partition partition)
{
return new Builder(partition);
}
public Partition withParameters(Map parameters)
{
return builder(this)
.setParameters(parameters)
.build();
}
public static class Builder
{
private final Storage.Builder storageBuilder;
private String databaseName;
private String tableName;
private List values;
private List columns;
private Map parameters = ImmutableMap.of();
private Builder()
{
this.storageBuilder = Storage.builder();
}
private Builder(Partition partition)
{
this.storageBuilder = Storage.builder(partition.getStorage());
this.databaseName = partition.getDatabaseName();
this.tableName = partition.getTableName();
this.values = partition.getValues();
this.columns = partition.getColumns();
this.parameters = partition.getParameters();
}
public Builder setDatabaseName(String databaseName)
{
this.databaseName = databaseName;
return this;
}
public Builder setTableName(String tableName)
{
this.tableName = tableName;
return this;
}
public Builder setValues(List values)
{
this.values = values;
return this;
}
public Storage.Builder getStorageBuilder()
{
return storageBuilder;
}
public Builder withStorage(Consumer consumer)
{
consumer.accept(storageBuilder);
return this;
}
public Builder setColumns(List columns)
{
this.columns = columns;
return this;
}
public Builder setParameters(Map parameters)
{
this.parameters = parameters;
return this;
}
public Partition build()
{
return new Partition(databaseName, tableName, values, storageBuilder.build(), columns, parameters);
}
}
public static List toPartitionValues(String partitionName)
{
// mimics Warehouse.makeValsFromName
ImmutableList.Builder 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();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy