Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -425,29 +425,32 @@ public HiveTableHandle getTableHandle(ConnectorSession session, SchemaTableName
if (isHiveSystemSchema(tableName.getSchemaName())) {
return null;
}
Optional<Table> table = metastore.getTable(new HiveIdentity(session), tableName.getSchemaName(), tableName.getTableName());
if (table.isEmpty()) {
Table table = metastore
.getTable(new HiveIdentity(session), tableName.getSchemaName(), tableName.getTableName())
.orElse(null);

if (table == null) {
return null;
}

if (isDeltaLakeTable(table.get())) {
throw new TrinoException(HIVE_UNSUPPORTED_FORMAT, "Cannot query Delta Lake table");
if (isDeltaLakeTable(table)) {
throw new TrinoException(HIVE_UNSUPPORTED_FORMAT, format("Cannot query Delta Lake table '%s'", tableName));
}

// we must not allow system tables due to how permissions are checked in SystemTableAwareAccessControl
if (getSourceTableNameFromSystemTable(systemTableProviders, tableName).isPresent()) {
throw new TrinoException(HIVE_INVALID_METADATA, "Unexpected table present in Hive metastore: " + tableName);
}

verifyOnline(tableName, Optional.empty(), getProtectMode(table.get()), table.get().getParameters());
verifyOnline(tableName, Optional.empty(), getProtectMode(table), table.getParameters());

return new HiveTableHandle(
tableName.getSchemaName(),
tableName.getTableName(),
table.get().getParameters(),
getPartitionKeyColumnHandles(table.get(), typeManager),
getRegularColumnHandles(table.get(), typeManager, getTimestampPrecision(session)),
getHiveBucketHandle(session, table.get(), typeManager));
table.getParameters(),
getPartitionKeyColumnHandles(table, typeManager),
getRegularColumnHandles(table, typeManager, getTimestampPrecision(session)),
getHiveBucketHandle(session, table, typeManager));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.common.collect.ImmutableSortedMap;
import io.trino.plugin.hive.authentication.HiveIdentity;
import io.trino.plugin.hive.metastore.Table;
import io.trino.spi.TrinoException;
import io.trino.spi.connector.ColumnMetadata;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.connector.ConnectorTableMetadata;
Expand All @@ -32,8 +33,11 @@
import java.util.Optional;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_UNSUPPORTED_FORMAT;
import static io.trino.plugin.hive.SystemTableHandler.PROPERTIES;
import static io.trino.plugin.hive.util.HiveUtil.isDeltaLakeTable;
import static io.trino.plugin.hive.util.SystemTables.createSystemTable;
import static java.lang.String.format;

public class PropertiesSystemTableProvider
implements SystemTableProvider
Expand All @@ -56,11 +60,14 @@ public Optional<SystemTable> getSystemTable(HiveMetadata metadata, ConnectorSess
}

SchemaTableName sourceTableName = PROPERTIES.getSourceTableName(tableName);
Optional<Table> table = metadata.getMetastore().getTable(new HiveIdentity(session), sourceTableName.getSchemaName(), sourceTableName.getTableName());
if (table.isEmpty()) {
throw new TableNotFoundException(tableName);
Table table = metadata.getMetastore()
.getTable(new HiveIdentity(session), sourceTableName.getSchemaName(), sourceTableName.getTableName())
.orElseThrow(() -> new TableNotFoundException(tableName));

if (isDeltaLakeTable(table)) {
throw new TrinoException(HIVE_UNSUPPORTED_FORMAT, format("Cannot query Delta Lake table '%s'", sourceTableName));
}
Map<String, String> sortedTableParameters = ImmutableSortedMap.copyOf(table.get().getParameters());
Map<String, String> sortedTableParameters = ImmutableSortedMap.copyOf(table.getParameters());
List<ColumnMetadata> columns = sortedTableParameters.keySet().stream()
.map(key -> new ColumnMetadata(key, VarcharType.VARCHAR))
.collect(toImmutableList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2876,6 +2876,7 @@ public void testHideDeltaLakeTables()
.setTableName(tableName.getTableName())
.setOwner(Optional.of(session.getUser()))
.setTableType(MANAGED_TABLE.name())
.setPartitionColumns(List.of(new Column("a_partition_column", HIVE_INT, Optional.empty())))
.setDataColumns(List.of(new Column("a_column", HIVE_STRING, Optional.empty())))
.setParameter(SPARK_TABLE_PROVIDER_KEY, DELTA_LAKE_PROVIDER);
table.getStorageBuilder()
Expand All @@ -2894,7 +2895,25 @@ public void testHideDeltaLakeTables()
ConnectorMetadata metadata = transaction.getMetadata();
metadata.beginQuery(session);
assertThatThrownBy(() -> getTableHandle(metadata, tableName))
.hasMessage("Cannot query Delta Lake table");
.hasMessage(format("Cannot query Delta Lake table '%s'", tableName));
}

// Verify the hidden `$properties` Delta Lake table handle can't be obtained within the hive connector
SchemaTableName propertiesTableName = new SchemaTableName(tableName.getSchemaName(), format("%s$properties", tableName.getTableName()));
try (Transaction transaction = newTransaction()) {
ConnectorMetadata metadata = transaction.getMetadata();
metadata.beginQuery(session);
assertThatThrownBy(() -> metadata.getSystemTable(newSession(), propertiesTableName))
.hasMessage(format("Cannot query Delta Lake table '%s'", tableName));
}

// Verify the hidden `$partitions` Delta Lake table handle can't be obtained within the hive connector
SchemaTableName partitionsTableName = new SchemaTableName(tableName.getSchemaName(), format("%s$partitions", tableName.getTableName()));
try (Transaction transaction = newTransaction()) {
ConnectorMetadata metadata = transaction.getMetadata();
metadata.beginQuery(session);
assertThatThrownBy(() -> metadata.getSystemTable(newSession(), partitionsTableName))
.hasMessage(format("Cannot query Delta Lake table '%s'", tableName));
}

// Assert that table is hidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public void testReadDeltaLakeTable()
"CREATE TABLE test_delta_lake_table (ignored int) " +
"TBLPROPERTIES ('spark.sql.sources.provider'='DELTA')");

assertQueryFailure(() -> onTrino().executeQuery("SELECT * FROM test_delta_lake_table")).hasMessageContaining("Cannot query Delta Lake table");
assertQueryFailure(() -> onTrino().executeQuery("SELECT * FROM test_delta_lake_table"))
.hasMessageContaining("Cannot query Delta Lake table 'default.test_delta_lake_table'");

onHive().executeQuery("DROP TABLE test_delta_lake_table");
}
Expand Down