From 0ff1564fa7721c9bce7cf5d8935d0a8570f80779 Mon Sep 17 00:00:00 2001 From: Yuya Ebihara Date: Tue, 30 Apr 2024 14:58:31 +0900 Subject: [PATCH] Convert SheetsTable to record Also, remove SheetsColumn class which was used only from SheetsTable. --- .../plugin/google/sheets/SheetsClient.java | 8 +- .../plugin/google/sheets/SheetsColumn.java | 79 ------------------- .../plugin/google/sheets/SheetsMetadata.java | 22 ++---- .../google/sheets/SheetsSplitManager.java | 2 +- .../plugin/google/sheets/SheetsTable.java | 35 +++----- 5 files changed, 21 insertions(+), 125 deletions(-) delete mode 100644 plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsColumn.java diff --git a/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsClient.java b/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsClient.java index c7285d132c2..d7d2cafef5d 100644 --- a/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsClient.java +++ b/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsClient.java @@ -141,19 +141,19 @@ public Optional getTableFromValues(List> values) { List> stringValues = convertToStringValues(values); if (stringValues.size() > 0) { - ImmutableList.Builder columns = ImmutableList.builder(); + ImmutableList.Builder columns = ImmutableList.builder(); Set columnNames = new HashSet<>(); // Assuming 1st line is always header List header = stringValues.get(0); int count = 0; - for (String column : header) { - String columnValue = column.toLowerCase(ENGLISH); + for (int i = 0; i < header.size(); i++) { + String columnValue = header.get(i).toLowerCase(ENGLISH); // when empty or repeated column header, adding a placeholder column name if (columnValue.isEmpty() || columnNames.contains(columnValue)) { columnValue = "column_" + ++count; } columnNames.add(columnValue); - columns.add(new SheetsColumn(columnValue, VarcharType.VARCHAR)); + columns.add(new SheetsColumnHandle(columnValue, VarcharType.VARCHAR, i)); } List> dataValues = stringValues.subList(1, values.size()); // removing header info return Optional.of(new SheetsTable(columns.build(), dataValues)); diff --git a/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsColumn.java b/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsColumn.java deleted file mode 100644 index 4260349458c..00000000000 --- a/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsColumn.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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.google.sheets; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; -import io.trino.spi.type.Type; - -import java.util.Objects; - -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Strings.isNullOrEmpty; -import static java.util.Objects.requireNonNull; - -public final class SheetsColumn -{ - private final String name; - private final Type type; - - @JsonCreator - public SheetsColumn( - @JsonProperty("name") String name, - @JsonProperty("type") Type type) - { - checkArgument(!isNullOrEmpty(name), "name is null or is empty"); - this.name = name; - this.type = requireNonNull(type, "type is null"); - } - - @JsonProperty - public String getName() - { - return name; - } - - @JsonProperty - public Type getType() - { - return type; - } - - @Override - public int hashCode() - { - return Objects.hash(name, type); - } - - @Override - public boolean equals(Object obj) - { - if (this == obj) { - return true; - } - if (obj == null || getClass() != obj.getClass()) { - return false; - } - - SheetsColumn other = (SheetsColumn) obj; - return Objects.equals(this.name, other.name) && - Objects.equals(this.type, other.type); - } - - @Override - public String toString() - { - return name + ":" + type; - } -} diff --git a/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsMetadata.java b/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsMetadata.java index c87919d5f69..b33784e9751 100644 --- a/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsMetadata.java +++ b/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsMetadata.java @@ -35,13 +35,14 @@ import io.trino.spi.function.table.ConnectorTableFunctionHandle; import io.trino.spi.statistics.ComputedStatistics; -import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.function.Function; import static com.google.common.collect.ImmutableList.toImmutableList; +import static com.google.common.collect.ImmutableMap.toImmutableMap; import static com.google.common.collect.Iterables.getOnlyElement; import static io.trino.plugin.google.sheets.SheetsConnectorTableHandle.tableNotFound; import static io.trino.plugin.google.sheets.SheetsErrorCode.SHEETS_UNKNOWN_TABLE_ERROR; @@ -96,7 +97,7 @@ public ConnectorTableMetadata getTableMetadata(ConnectorSession session, Connect SheetsConnectorTableHandle tableHandle = (SheetsConnectorTableHandle) table; SheetsTable sheetsTable = sheetsClient.getTable(tableHandle) .orElseThrow(() -> new TrinoException(SHEETS_UNKNOWN_TABLE_ERROR, "Metadata not found for table " + tableNotFound(tableHandle))); - return new ConnectorTableMetadata(getSchemaTableName(tableHandle), sheetsTable.getColumnsMetadata()); + return new ConnectorTableMetadata(getSchemaTableName(tableHandle), sheetsTable.columnsMetadata()); } @Override @@ -106,13 +107,8 @@ public Map getColumnHandles(ConnectorSession session, Conn SheetsTable table = sheetsClient.getTable(sheetsTableHandle) .orElseThrow(() -> tableNotFound(sheetsTableHandle)); - ImmutableMap.Builder columnHandles = ImmutableMap.builder(); - int index = 0; - for (ColumnMetadata column : table.getColumnsMetadata()) { - columnHandles.put(column.getName(), new SheetsColumnHandle(column.getName(), column.getType(), index)); - index++; - } - return columnHandles.buildOrThrow(); + return table.columns().stream() + .collect(toImmutableMap(SheetsColumnHandle::columnName, Function.identity())); } @Override @@ -137,7 +133,7 @@ private Optional getTableMetadata(SchemaTableName tableN } Optional table = sheetsClient.getTable(tableName.getTableName()); if (table.isPresent()) { - return Optional.of(new ConnectorTableMetadata(tableName, table.get().getColumnsMetadata())); + return Optional.of(new ConnectorTableMetadata(tableName, table.get().columnsMetadata())); } return Optional.empty(); } @@ -181,11 +177,7 @@ public ConnectorInsertTableHandle beginInsert(ConnectorSession session, Connecto SheetsTable table = sheetsClient.getTable(namedTableHandle.tableName()) .orElseThrow(() -> new TableNotFoundException(namedTableHandle.getSchemaTableName())); - List columnHandles = new ArrayList<>(table.getColumnsMetadata().size()); - for (int id = 0; id < table.getColumnsMetadata().size(); id++) { - columnHandles.add(new SheetsColumnHandle(table.getColumnsMetadata().get(id).getName(), table.getColumnsMetadata().get(id).getType(), id)); - } - return new SheetsConnectorInsertTableHandle(namedTableHandle.tableName(), columnHandles); + return new SheetsConnectorInsertTableHandle(namedTableHandle.tableName(), table.columns()); } @Override diff --git a/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsSplitManager.java b/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsSplitManager.java index 51e2f342bae..2fbe012162e 100644 --- a/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsSplitManager.java +++ b/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsSplitManager.java @@ -56,7 +56,7 @@ public ConnectorSplitSource getSplits( .orElseThrow(() -> tableNotFound(tableHandle)); List splits = new ArrayList<>(); - splits.add(new SheetsSplit(table.getValues())); + splits.add(new SheetsSplit(table.values())); Collections.shuffle(splits); return new FixedSplitSource(splits); } diff --git a/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsTable.java b/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsTable.java index ac8bdc138c4..e46887dbf55 100644 --- a/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsTable.java +++ b/plugin/trino-google-sheets/src/main/java/io/trino/plugin/google/sheets/SheetsTable.java @@ -13,43 +13,26 @@ */ package io.trino.plugin.google.sheets; -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.collect.ImmutableList; import io.trino.spi.connector.ColumnMetadata; import java.util.List; +import static com.google.common.collect.ImmutableList.toImmutableList; import static java.util.Objects.requireNonNull; -public class SheetsTable +public record SheetsTable(List columns, List> values) { - private final List columnsMetadata; - private final List> values; - - @JsonCreator - public SheetsTable( - @JsonProperty("columns") List columns, - @JsonProperty("values") List> values) - { - requireNonNull(columns, "columns is null"); - - ImmutableList.Builder columnsMetadata = ImmutableList.builder(); - for (SheetsColumn column : columns) { - columnsMetadata.add(new ColumnMetadata(column.getName(), column.getType())); - } - this.columnsMetadata = columnsMetadata.build(); - this.values = values; - } - - @JsonProperty - public List> getValues() + public SheetsTable { - return values; + columns = ImmutableList.copyOf(requireNonNull(columns, "columns is null")); + values = ImmutableList.copyOf(requireNonNull(values, "values is null")); } - public List getColumnsMetadata() + List columnsMetadata() { - return columnsMetadata; + return columns.stream() + .map(SheetsColumnHandle::columnMetadata) + .collect(toImmutableList()); } }