From 74a4364b2af5d1d0b9ecf5e39f6660af25b596df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Natalie=20Klestrup=20R=C3=B6ijezon?= Date: Tue, 17 Oct 2023 13:39:45 +0200 Subject: [PATCH] Don't case-fold paths when using table syntax Fixes #239 --- .../ebyhr/trino/storage/StorageMetadata.java | 67 ++++++++++--------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/ebyhr/trino/storage/StorageMetadata.java b/src/main/java/org/ebyhr/trino/storage/StorageMetadata.java index 0b80464..ada381e 100644 --- a/src/main/java/org/ebyhr/trino/storage/StorageMetadata.java +++ b/src/main/java/org/ebyhr/trino/storage/StorageMetadata.java @@ -13,7 +13,6 @@ */ package org.ebyhr.trino.storage; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.inject.Inject; import io.trino.spi.connector.ColumnHandle; @@ -35,6 +34,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.stream.Stream; import static com.google.common.collect.ImmutableList.toImmutableList; import static java.util.Objects.requireNonNull; @@ -83,7 +83,7 @@ public StorageTableHandle getTableHandle(ConnectorSession session, SchemaTableNa public ConnectorTableMetadata getTableMetadata(ConnectorSession session, ConnectorTableHandle table) { StorageTableHandle storageTableHandle = (StorageTableHandle) table; - SchemaTableName tableName = new SchemaTableName(storageTableHandle.getSchemaName(), storageTableHandle.getTableName()); + RemoteTableName tableName = new RemoteTableName(storageTableHandle.getSchemaName(), storageTableHandle.getTableName()); return getStorageTableMetadata(session, tableName); } @@ -91,21 +91,10 @@ public ConnectorTableMetadata getTableMetadata(ConnectorSession session, Connect @Override public List listTables(ConnectorSession session, Optional schemaNameOrNull) { - List schemaNames; - if (schemaNameOrNull.isPresent()) { - schemaNames = List.of(schemaNameOrNull.get()); - } - else { - schemaNames = storageClient.getSchemaNames(); - } - - ImmutableList.Builder builder = ImmutableList.builder(); - for (String schemaName : schemaNames) { - for (String tableName : storageClient.getTableNames(schemaName)) { - builder.add(new SchemaTableName(schemaName, tableName)); - } - } - return builder.build(); + SchemaTablePrefix prefix = schemaNameOrNull + .map(SchemaTablePrefix::new) + .orElseGet(SchemaTablePrefix::new); + return listTables(prefix).map(RemoteTableName::toSchemaTableName).collect(toImmutableList()); } @Override @@ -130,11 +119,11 @@ public Map> listTableColumns(ConnectorSess { requireNonNull(prefix, "prefix is null"); ImmutableMap.Builder> columns = ImmutableMap.builder(); - for (SchemaTableName tableName : listTables(session, prefix)) { + for (RemoteTableName tableName : listTables(prefix).toList()) { ConnectorTableMetadata tableMetadata = getStorageTableMetadata(session, tableName); // table can disappear during listing operation if (tableMetadata != null) { - columns.put(tableName, tableMetadata.getColumns()); + columns.put(tableName.toSchemaTableName(), tableMetadata.getColumns()); } } return columns.build(); @@ -144,38 +133,45 @@ public Map> listTableColumns(ConnectorSess public Iterator streamTableColumns(ConnectorSession session, SchemaTablePrefix prefix) { requireNonNull(prefix, "prefix is null"); - return listTables(session, prefix).stream() + return listTables(prefix) .map(table -> TableColumnsMetadata.forTable( - table, + table.toSchemaTableName(), requireNonNull(getStorageTableMetadata(session, table), "tableMetadata is null") .getColumns())) .iterator(); } - private ConnectorTableMetadata getStorageTableMetadata(ConnectorSession session, SchemaTableName tableName) + private ConnectorTableMetadata getStorageTableMetadata(ConnectorSession session, RemoteTableName tableName) { - if (tableName.getSchemaName().equals(LIST_SCHEMA_NAME)) { - return new ConnectorTableMetadata(tableName, COLUMNS_METADATA); + if (tableName.schemaName().equals(LIST_SCHEMA_NAME)) { + return new ConnectorTableMetadata(tableName.toSchemaTableName(), COLUMNS_METADATA); } - if (!listSchemaNames().contains(tableName.getSchemaName())) { + if (!listSchemaNames().contains(tableName.schemaName())) { return null; } - StorageTable table = storageClient.getTable(session, tableName.getSchemaName(), tableName.getTableName()); + StorageTable table = storageClient.getTable(session, tableName.schemaName(), tableName.tableName()); if (table == null) { return null; } - return new ConnectorTableMetadata(tableName, table.getColumnsMetadata()); + return new ConnectorTableMetadata(tableName.toSchemaTableName(), table.getColumnsMetadata()); } - private List listTables(ConnectorSession session, SchemaTablePrefix prefix) + private Stream listTables(SchemaTablePrefix prefix) { if (prefix.getSchema().isPresent() && prefix.getTable().isPresent()) { - return List.of(new SchemaTableName(prefix.getSchema().get(), prefix.getTable().get())); + return Stream.of(new RemoteTableName(prefix.getSchema().get(), prefix.getTable().get())); } - return listTables(session, prefix.getSchema()); + + List schemaNames = prefix.getSchema() + .map(List::of) + .orElseGet(storageClient::getSchemaNames); + + return schemaNames.stream() + .flatMap(schemaName -> storageClient.getTableNames(schemaName).stream() + .map(tableName -> new RemoteTableName(LIST_SCHEMA_NAME, LIST_SCHEMA_NAME))); } @Override @@ -199,4 +195,15 @@ public Optional> applyTable } return Optional.empty(); } + + /** + * Simplified variant of {@link SchemaTableName} that doesn't case-fold. + */ + private record RemoteTableName(String schemaName, String tableName) + { + public SchemaTableName toSchemaTableName() + { + return new SchemaTableName(schemaName(), tableName()); + } + } }