Skip to content

Commit

Permalink
Convert some classes to record in Hudi
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr committed Nov 8, 2024
1 parent b4e68e6 commit 1ead770
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,23 @@ public Optional<SystemTable> getSystemTable(ConnectorSession session, SchemaTabl
private Optional<SystemTable> getRawSystemTable(SchemaTableName tableName, ConnectorSession session)
{
HudiTableName name = HudiTableName.from(tableName.getTableName());
if (name.getTableType() == TableType.DATA) {
if (name.tableType() == TableType.DATA) {
return Optional.empty();
}

Optional<Table> tableOptional = metastore.getTable(tableName.getSchemaName(), name.getTableName());
Optional<Table> tableOptional = metastore.getTable(tableName.getSchemaName(), name.tableName());
if (tableOptional.isEmpty()) {
return Optional.empty();
}
if (!isHudiTable(tableOptional.get())) {
return Optional.empty();
}
return switch (name.getTableType()) {
return switch (name.tableType()) {
case DATA ->
// TODO (https://github.com/trinodb/trino/issues/17973) remove DATA table type
Optional.empty();
case TIMELINE -> {
SchemaTableName systemTableName = new SchemaTableName(tableName.getSchemaName(), name.getTableNameWithType());
SchemaTableName systemTableName = new SchemaTableName(tableName.getSchemaName(), name.tableNameWithType());
yield Optional.of(new TimelineTable(fileSystemFactory.create(session), systemTableName, tableOptional.get()));
}
};
Expand Down Expand Up @@ -228,9 +228,10 @@ public ColumnMetadata getColumnMetadata(ConnectorSession session, ConnectorTable
}

@Override
public Optional<Object> getInfo(ConnectorTableHandle table)
public Optional<Object> getInfo(ConnectorTableHandle tableHandle)
{
return Optional.of(HudiTableInfo.from((HudiTableHandle) table));
HudiTableHandle table = (HudiTableHandle) tableHandle;
return Optional.of(new HudiTableInfo(table.getSchemaTableName(), table.getTableType().name(), table.getBasePath()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,16 @@
*/
package io.trino.plugin.hudi;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.trino.spi.connector.SchemaTableName;

import static java.util.Objects.requireNonNull;

public class HudiTableInfo
public record HudiTableInfo(SchemaTableName table, String tableType, String basePath)
{
private final SchemaTableName table;
private final String tableType;
private final String basePath;

@JsonCreator
public HudiTableInfo(
@JsonProperty("table") SchemaTableName table,
@JsonProperty("tableType") String tableType,
@JsonProperty("basePath") String basePath)
{
this.table = requireNonNull(table, "table is null");
this.tableType = requireNonNull(tableType, "tableType is null");
this.basePath = requireNonNull(basePath, "basePath is null");
}

@JsonProperty
public SchemaTableName getTable()
{
return table;
}

@JsonProperty
public String getTableType()
{
return tableType;
}

@JsonProperty
public String getBasePath()
{
return basePath;
}

public static HudiTableInfo from(HudiTableHandle tableHandle)
public HudiTableInfo
{
requireNonNull(tableHandle, "tableHandle is null");
return new HudiTableInfo(
tableHandle.getSchemaTableName(),
tableHandle.getTableType().name(),
tableHandle.getBasePath());
requireNonNull(table, "table is null");
requireNonNull(tableType, "tableType is null");
requireNonNull(basePath, "basePath is null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,27 @@
import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;

public class HudiTableName
public record HudiTableName(String tableName, TableType tableType)
{
private static final Pattern TABLE_PATTERN = Pattern.compile("" +
"(?<table>[^$@]+)" +
"(?:\\$(?<type>[^@]+))?");

private final String tableName;
private final TableType tableType;

public HudiTableName(String tableName, TableType tableType)
{
this.tableName = requireNonNull(tableName, "tableName is null");
this.tableType = requireNonNull(tableType, "tableType is null");
}

public String getTableName()
{
return tableName;
}

public TableType getTableType()
{
return tableType;
}

public String getTableNameWithType()
public String tableNameWithType()
{
return tableName + "$" + tableType.name().toLowerCase(ENGLISH);
}

@Override
public String toString()
{
return getTableNameWithType();
return tableNameWithType();
}

public static HudiTableName from(String name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public CompactionOperation(

public String getPartitionPath()
{
return id.getPartitionPath();
return id.partitionPath();
}

public HudiFileGroupId getFileGroupId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void addLogFile(HudiLogFile logFile)

public String getPartitionPath()
{
return fileGroupId.getPartitionPath();
return fileGroupId.partitionPath();
}

public HudiFileGroupId getFileGroupId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,15 @@
*/
package io.trino.plugin.hudi.files;

import java.util.Objects;

import static java.util.Objects.requireNonNull;

public class HudiFileGroupId
public record HudiFileGroupId(String partitionPath, String fileId)
implements Comparable<HudiFileGroupId>
{
private final String partitionPath;

private final String fileId;

public HudiFileGroupId(String partitionPath, String fileId)
{
this.partitionPath = requireNonNull(partitionPath, "partitionPath is null");
this.fileId = requireNonNull(fileId, "partitionPath is null");
}

public String getPartitionPath()
{
return partitionPath;
}

public String getFileId()
{
return fileId;
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
HudiFileGroupId that = (HudiFileGroupId) o;
return Objects.equals(partitionPath, that.partitionPath) && Objects.equals(fileId, that.fileId);
}

@Override
public int hashCode()
public HudiFileGroupId
{
return Objects.hash(partitionPath, fileId);
requireNonNull(partitionPath, "partitionPath is null");
requireNonNull(fileId, "partitionPath is null");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
*/
package io.trino.plugin.hudi.timeline;

import java.util.Objects;

import static com.google.common.base.Preconditions.checkArgument;

public class TimelineLayoutVersion
public record TimelineLayoutVersion(Integer version)
implements Comparable<TimelineLayoutVersion>
{
public static final Integer VERSION_0 = 0; // pre 0.5.1 version format
Expand All @@ -26,37 +24,10 @@ public class TimelineLayoutVersion
private static final Integer CURRENT_VERSION = VERSION_1;
public static final TimelineLayoutVersion CURRENT_LAYOUT_VERSION = new TimelineLayoutVersion(CURRENT_VERSION);

private final Integer version;

public TimelineLayoutVersion(Integer version)
public TimelineLayoutVersion
{
checkArgument(version <= CURRENT_VERSION);
checkArgument(version >= VERSION_0);
this.version = version;
}

public Integer getVersion()
{
return version;
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TimelineLayoutVersion that = (TimelineLayoutVersion) o;
return Objects.equals(version, that.version);
}

@Override
public int hashCode()
{
return Objects.hash(version);
}

@Override
Expand Down

0 comments on commit 1ead770

Please sign in to comment.