Skip to content

Commit

Permalink
Convert SheetsTable to record
Browse files Browse the repository at this point in the history
Also, remove SheetsColumn class which was used
only from SheetsTable.
  • Loading branch information
ebyhr authored and wendigo committed Apr 30, 2024
1 parent b6e3f35 commit 0ff1564
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,19 @@ public Optional<SheetsTable> getTableFromValues(List<List<Object>> values)
{
List<List<String>> stringValues = convertToStringValues(values);
if (stringValues.size() > 0) {
ImmutableList.Builder<SheetsColumn> columns = ImmutableList.builder();
ImmutableList.Builder<SheetsColumnHandle> columns = ImmutableList.builder();
Set<String> columnNames = new HashSet<>();
// Assuming 1st line is always header
List<String> 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<List<String>> dataValues = stringValues.subList(1, values.size()); // removing header info
return Optional.of(new SheetsTable(columns.build(), dataValues));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -106,13 +107,8 @@ public Map<String, ColumnHandle> getColumnHandles(ConnectorSession session, Conn
SheetsTable table = sheetsClient.getTable(sheetsTableHandle)
.orElseThrow(() -> tableNotFound(sheetsTableHandle));

ImmutableMap.Builder<String, ColumnHandle> 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
Expand All @@ -137,7 +133,7 @@ private Optional<ConnectorTableMetadata> getTableMetadata(SchemaTableName tableN
}
Optional<SheetsTable> 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();
}
Expand Down Expand Up @@ -181,11 +177,7 @@ public ConnectorInsertTableHandle beginInsert(ConnectorSession session, Connecto
SheetsTable table = sheetsClient.getTable(namedTableHandle.tableName())
.orElseThrow(() -> new TableNotFoundException(namedTableHandle.getSchemaTableName()));

List<SheetsColumnHandle> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ConnectorSplitSource getSplits(
.orElseThrow(() -> tableNotFound(tableHandle));

List<ConnectorSplit> splits = new ArrayList<>();
splits.add(new SheetsSplit(table.getValues()));
splits.add(new SheetsSplit(table.values()));
Collections.shuffle(splits);
return new FixedSplitSource(splits);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SheetsColumnHandle> columns, List<List<String>> values)
{
private final List<ColumnMetadata> columnsMetadata;
private final List<List<String>> values;

@JsonCreator
public SheetsTable(
@JsonProperty("columns") List<SheetsColumn> columns,
@JsonProperty("values") List<List<String>> values)
{
requireNonNull(columns, "columns is null");

ImmutableList.Builder<ColumnMetadata> 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<List<String>> getValues()
public SheetsTable
{
return values;
columns = ImmutableList.copyOf(requireNonNull(columns, "columns is null"));
values = ImmutableList.copyOf(requireNonNull(values, "values is null"));
}

public List<ColumnMetadata> getColumnsMetadata()
List<ColumnMetadata> columnsMetadata()
{
return columnsMetadata;
return columns.stream()
.map(SheetsColumnHandle::columnMetadata)
.collect(toImmutableList());
}
}

0 comments on commit 0ff1564

Please sign in to comment.