Search in sources :

Example 31 with TableNotFoundException

use of io.trino.spi.connector.TableNotFoundException in project trino by trinodb.

the class CassandraSession method getTableMetadata.

private static AbstractTableMetadata getTableMetadata(KeyspaceMetadata keyspace, String caseInsensitiveTableName) {
    List<AbstractTableMetadata> tables = Stream.concat(keyspace.getTables().stream(), keyspace.getMaterializedViews().stream()).filter(table -> table.getName().equalsIgnoreCase(caseInsensitiveTableName)).collect(toImmutableList());
    if (tables.size() == 0) {
        throw new TableNotFoundException(new SchemaTableName(keyspace.getName(), caseInsensitiveTableName));
    }
    if (tables.size() == 1) {
        return tables.get(0);
    }
    String tableNames = tables.stream().map(AbstractTableMetadata::getName).sorted().collect(joining(", "));
    throw new TrinoException(NOT_SUPPORTED, format("More than one table has been found for the case insensitive table name: %s -> (%s)", caseInsensitiveTableName, tableNames));
}
Also used : CassandraType.isFullySupported(io.trino.plugin.cassandra.CassandraType.isFullySupported) QueryBuilder(com.datastax.driver.core.querybuilder.QueryBuilder) Iterables.transform(com.google.common.collect.Iterables.transform) CassandraType.toCassandraType(io.trino.plugin.cassandra.CassandraType.toCassandraType) RegularStatement(com.datastax.driver.core.RegularStatement) Clause(com.datastax.driver.core.querybuilder.Clause) SchemaNotFoundException(io.trino.spi.connector.SchemaNotFoundException) ByteBuffer(java.nio.ByteBuffer) Duration(io.airlift.units.Duration) ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) NOT_SUPPORTED(io.trino.spi.StandardErrorCode.NOT_SUPPORTED) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) Session(com.datastax.driver.core.Session) Map(java.util.Map) VersionNumber(com.datastax.driver.core.VersionNumber) ENGLISH(java.util.Locale.ENGLISH) CassandraCqlUtils.validSchemaName(io.trino.plugin.cassandra.util.CassandraCqlUtils.validSchemaName) TableMetadata(com.datastax.driver.core.TableMetadata) ImmutableSet(com.google.common.collect.ImmutableSet) ColumnMetadata(com.datastax.driver.core.ColumnMetadata) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) TrinoException(io.trino.spi.TrinoException) NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) Sets(com.google.common.collect.Sets) SchemaTableName(io.trino.spi.connector.SchemaTableName) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ProtocolVersion(com.datastax.driver.core.ProtocolVersion) List(java.util.List) Stream(java.util.stream.Stream) Cluster(com.datastax.driver.core.Cluster) Host(com.datastax.driver.core.Host) Optional(java.util.Optional) Select(com.datastax.driver.core.querybuilder.Select) Statement(com.datastax.driver.core.Statement) JsonCodec(io.airlift.json.JsonCodec) TokenRange(com.datastax.driver.core.TokenRange) Logger(io.airlift.log.Logger) NullableValue(io.trino.spi.predicate.NullableValue) Row(com.datastax.driver.core.Row) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) PRESTO_COMMENT_METADATA(io.trino.plugin.cassandra.CassandraMetadata.PRESTO_COMMENT_METADATA) AbstractTableMetadata(com.datastax.driver.core.AbstractTableMetadata) PreparedStatement(com.datastax.driver.core.PreparedStatement) HashSet(java.util.HashSet) CassandraCqlUtils.selectDistinctFrom(io.trino.plugin.cassandra.util.CassandraCqlUtils.selectDistinctFrom) ResultSet(com.datastax.driver.core.ResultSet) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) ColumnHandle(io.trino.spi.connector.ColumnHandle) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Comparator.comparing(java.util.Comparator.comparing) Suppliers.memoize(com.google.common.base.Suppliers.memoize) QueryBuilder.eq(com.datastax.driver.core.querybuilder.QueryBuilder.eq) TupleDomain(io.trino.spi.predicate.TupleDomain) IndexMetadata(com.datastax.driver.core.IndexMetadata) CassandraCqlUtils(io.trino.plugin.cassandra.util.CassandraCqlUtils) Collectors.toList(java.util.stream.Collectors.toList) MaterializedViewMetadata(com.datastax.driver.core.MaterializedViewMetadata) KeyspaceMetadata(com.datastax.driver.core.KeyspaceMetadata) Ordering(com.google.common.collect.Ordering) QueryBuilder.select(com.datastax.driver.core.querybuilder.QueryBuilder.select) DataType(com.datastax.driver.core.DataType) CASSANDRA_VERSION_ERROR(io.trino.plugin.cassandra.CassandraErrorCode.CASSANDRA_VERSION_ERROR) ReconnectionSchedule(com.datastax.driver.core.policies.ReconnectionPolicy.ReconnectionSchedule) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) AbstractTableMetadata(com.datastax.driver.core.AbstractTableMetadata) TrinoException(io.trino.spi.TrinoException) SchemaTableName(io.trino.spi.connector.SchemaTableName)

Example 32 with TableNotFoundException

use of io.trino.spi.connector.TableNotFoundException in project trino by trinodb.

the class TestHiveGlueMetastore method createDummyPartitionedTable.

private void createDummyPartitionedTable(SchemaTableName tableName, List<ColumnMetadata> columns, List<String> partitionColumnNames, List<PartitionValues> partitionValues) throws Exception {
    doCreateEmptyTable(tableName, ORC, columns, partitionColumnNames);
    HiveMetastoreClosure metastoreClient = new HiveMetastoreClosure(getMetastoreClient());
    Table table = metastoreClient.getTable(tableName.getSchemaName(), tableName.getTableName()).orElseThrow(() -> new TableNotFoundException(tableName));
    List<PartitionWithStatistics> partitions = new ArrayList<>();
    List<String> partitionNames = new ArrayList<>();
    partitionValues.stream().map(partitionValue -> makePartName(partitionColumnNames, partitionValue.values)).forEach(partitionName -> {
        partitions.add(new PartitionWithStatistics(createDummyPartition(table, partitionName), partitionName, PartitionStatistics.empty()));
        partitionNames.add(partitionName);
    });
    metastoreClient.addPartitions(tableName.getSchemaName(), tableName.getTableName(), partitions);
    partitionNames.forEach(partitionName -> metastoreClient.updatePartitionStatistics(tableName.getSchemaName(), tableName.getTableName(), partitionName, currentStatistics -> EMPTY_TABLE_STATISTICS));
}
Also used : Arrays(java.util.Arrays) DateType(io.trino.spi.type.DateType) Test(org.testng.annotations.Test) NO_ACID_TRANSACTION(io.trino.plugin.hive.acid.AcidTransaction.NO_ACID_TRANSACTION) NUMBER_OF_DISTINCT_VALUES(io.trino.spi.statistics.ColumnStatisticType.NUMBER_OF_DISTINCT_VALUES) ColumnStatisticMetadata(io.trino.spi.statistics.ColumnStatisticMetadata) DeleteTableRequest(com.amazonaws.services.glue.model.DeleteTableRequest) GetDatabasesResult(com.amazonaws.services.glue.model.GetDatabasesResult) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) BoundedExecutor(io.airlift.concurrent.BoundedExecutor) ConnectorOutputTableHandle(io.trino.spi.connector.ConnectorOutputTableHandle) Map(java.util.Map) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) PartitionWithStatistics(io.trino.plugin.hive.metastore.PartitionWithStatistics) ENGLISH(java.util.Locale.ENGLISH) HiveIdentity(io.trino.plugin.hive.authentication.HiveIdentity) Table(io.trino.plugin.hive.metastore.Table) Range(io.trino.spi.predicate.Range) Domain(io.trino.spi.predicate.Domain) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) TableInput(com.amazonaws.services.glue.model.TableInput) UpdateTableRequest(com.amazonaws.services.glue.model.UpdateTableRequest) HDFS_ENVIRONMENT(io.trino.plugin.hive.HiveTestUtils.HDFS_ENVIRONMENT) SchemaTableName(io.trino.spi.connector.SchemaTableName) EntityNotFoundException(com.amazonaws.services.glue.model.EntityNotFoundException) AWSGlueAsync(com.amazonaws.services.glue.AWSGlueAsync) PartitionStatistics(io.trino.plugin.hive.PartitionStatistics) Slice(io.airlift.slice.Slice) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) HiveBasicStatistics.createEmptyStatistics(io.trino.plugin.hive.HiveBasicStatistics.createEmptyStatistics) ArrayList(java.util.ArrayList) HiveType(io.trino.plugin.hive.HiveType) OptionalLong(java.util.OptionalLong) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) HiveMetastore(io.trino.plugin.hive.metastore.HiveMetastore) CreateTableRequest(com.amazonaws.services.glue.model.CreateTableRequest) TEXTFILE(io.trino.plugin.hive.HiveStorageFormat.TEXTFILE) NUMBER_OF_NON_NULL_VALUES(io.trino.spi.statistics.ColumnStatisticType.NUMBER_OF_NON_NULL_VALUES) HiveUtil.isIcebergTable(io.trino.plugin.hive.util.HiveUtil.isIcebergTable) Executor(java.util.concurrent.Executor) FileUtils.makePartName(org.apache.hadoop.hive.common.FileUtils.makePartName) SPARK_TABLE_PROVIDER_KEY(io.trino.plugin.hive.util.HiveUtil.SPARK_TABLE_PROVIDER_KEY) ConnectorSession(io.trino.spi.connector.ConnectorSession) MoreFutures.getFutureValue(io.airlift.concurrent.MoreFutures.getFutureValue) File(java.io.File) HiveMetastoreClosure(io.trino.plugin.hive.HiveMetastoreClosure) AwsSdkUtil.getPaginatedResults(io.trino.plugin.hive.metastore.glue.AwsSdkUtil.getPaginatedResults) MAX_VALUE(io.trino.spi.statistics.ColumnStatisticType.MAX_VALUE) Database(com.amazonaws.services.glue.model.Database) HiveUtil.isDeltaLakeTable(io.trino.plugin.hive.util.HiveUtil.isDeltaLakeTable) MaterializedResult(io.trino.testing.MaterializedResult) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EXTERNAL_TABLE(org.apache.hadoop.hive.metastore.TableType.EXTERNAL_TABLE) NO_RETRIES(io.trino.spi.connector.RetryMode.NO_RETRIES) ICEBERG_TABLE_TYPE_VALUE(io.trino.plugin.hive.util.HiveUtil.ICEBERG_TABLE_TYPE_VALUE) ICEBERG_TABLE_TYPE_NAME(io.trino.plugin.hive.util.HiveUtil.ICEBERG_TABLE_TYPE_NAME) GetDatabasesRequest(com.amazonaws.services.glue.model.GetDatabasesRequest) Block(io.trino.spi.block.Block) SmallintType(io.trino.spi.type.SmallintType) HiveBasicStatistics(io.trino.plugin.hive.HiveBasicStatistics) AWSGlueAsyncClientBuilder(com.amazonaws.services.glue.AWSGlueAsyncClientBuilder) ImmutableMap(com.google.common.collect.ImmutableMap) BeforeClass(org.testng.annotations.BeforeClass) Collection(java.util.Collection) ComputedStatistics(io.trino.spi.statistics.ComputedStatistics) TrinoException(io.trino.spi.TrinoException) String.format(java.lang.String.format) List(java.util.List) DECIMAL_TYPE(io.trino.plugin.hive.metastore.glue.PartitionFilterBuilder.DECIMAL_TYPE) BIGINT(io.trino.spi.type.BigintType.BIGINT) PartitionFilterBuilder.decimalOf(io.trino.plugin.hive.metastore.glue.PartitionFilterBuilder.decimalOf) Optional(java.util.Optional) ConnectorMetadata(io.trino.spi.connector.ConnectorMetadata) TableStatisticType(io.trino.spi.statistics.TableStatisticType) System.currentTimeMillis(java.lang.System.currentTimeMillis) Logger(io.airlift.log.Logger) MetastoreConfig(io.trino.plugin.hive.metastore.MetastoreConfig) Assert.assertEquals(org.testng.Assert.assertEquals) BigintType(io.trino.spi.type.BigintType) VarcharType(io.trino.spi.type.VarcharType) HiveColumnStatistics(io.trino.plugin.hive.metastore.HiveColumnStatistics) ImmutableList(com.google.common.collect.ImmutableList) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) DAYS(java.util.concurrent.TimeUnit.DAYS) GlueInputConverter(io.trino.plugin.hive.metastore.glue.converter.GlueInputConverter) DeleteDatabaseRequest(com.amazonaws.services.glue.model.DeleteDatabaseRequest) TinyintType(io.trino.spi.type.TinyintType) ConnectorPageSink(io.trino.spi.connector.ConnectorPageSink) IntegerType(io.trino.spi.type.IntegerType) DELTA_LAKE_PROVIDER(io.trino.plugin.hive.util.HiveUtil.DELTA_LAKE_PROVIDER) ORC(io.trino.plugin.hive.HiveStorageFormat.ORC) HiveColumnStatistics.createIntegerColumnStatistics(io.trino.plugin.hive.metastore.HiveColumnStatistics.createIntegerColumnStatistics) TupleDomain(io.trino.spi.predicate.TupleDomain) UUID.randomUUID(java.util.UUID.randomUUID) AbstractTestHiveLocal(io.trino.plugin.hive.AbstractTestHiveLocal) Assert.assertTrue(org.testng.Assert.assertTrue) Collections(java.util.Collections) MIN_VALUE(io.trino.spi.statistics.ColumnStatisticType.MIN_VALUE) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) Table(io.trino.plugin.hive.metastore.Table) HiveUtil.isIcebergTable(io.trino.plugin.hive.util.HiveUtil.isIcebergTable) HiveUtil.isDeltaLakeTable(io.trino.plugin.hive.util.HiveUtil.isDeltaLakeTable) PartitionWithStatistics(io.trino.plugin.hive.metastore.PartitionWithStatistics) ArrayList(java.util.ArrayList) HiveMetastoreClosure(io.trino.plugin.hive.HiveMetastoreClosure)

Example 33 with TableNotFoundException

use of io.trino.spi.connector.TableNotFoundException in project trino by trinodb.

the class InMemoryThriftMetastore method dropTable.

@Override
public synchronized void dropTable(HiveIdentity identity, String databaseName, String tableName, boolean deleteData) {
    List<String> locations = listAllDataPaths(identity, this, databaseName, tableName);
    SchemaTableName schemaTableName = new SchemaTableName(databaseName, tableName);
    Table table = relations.remove(schemaTableName);
    if (table == null) {
        throw new TableNotFoundException(schemaTableName);
    }
    views.remove(schemaTableName);
    partitions.keySet().removeIf(partitionName -> partitionName.matches(databaseName, tableName));
    // remove data
    if (deleteData && table.getTableType().equals(MANAGED_TABLE.name())) {
        for (String location : locations) {
            if (location != null) {
                File directory = new File(new Path(location).toUri());
                checkArgument(isParentDir(directory, baseDirectory), "Table directory must be inside of the metastore base directory");
                deleteDirectory(directory);
            }
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) Table(org.apache.hadoop.hive.metastore.api.Table) SchemaTableName(io.trino.spi.connector.SchemaTableName) File(java.io.File)

Example 34 with TableNotFoundException

use of io.trino.spi.connector.TableNotFoundException in project trino by trinodb.

the class HiveMetastoreBackedDeltaLakeMetastore method getTableLocation.

@Override
public String getTableLocation(SchemaTableName table, ConnectorSession session) {
    Map<String, String> serdeParameters = getTable(table.getSchemaName(), table.getTableName()).orElseThrow(() -> new TableNotFoundException(table)).getStorage().getSerdeParameters();
    String location = serdeParameters.get(PATH_PROPERTY);
    if (location == null) {
        throw new TrinoException(DELTA_LAKE_INVALID_SCHEMA, format("No %s property defined for table: %s", PATH_PROPERTY, table));
    }
    return location;
}
Also used : TableNotFoundException(io.trino.spi.connector.TableNotFoundException) TrinoException(io.trino.spi.TrinoException)

Example 35 with TableNotFoundException

use of io.trino.spi.connector.TableNotFoundException in project trino by trinodb.

the class BaseJdbcClient method getColumns.

@Override
public List<JdbcColumnHandle> getColumns(ConnectorSession session, JdbcTableHandle tableHandle) {
    if (tableHandle.getColumns().isPresent()) {
        return tableHandle.getColumns().get();
    }
    checkArgument(tableHandle.isNamedRelation(), "Cannot get columns for %s", tableHandle);
    SchemaTableName schemaTableName = tableHandle.getRequiredNamedRelation().getSchemaTableName();
    RemoteTableName remoteTableName = tableHandle.getRequiredNamedRelation().getRemoteTableName();
    try (Connection connection = connectionFactory.openConnection(session);
        ResultSet resultSet = getColumns(tableHandle, connection.getMetaData())) {
        int allColumns = 0;
        List<JdbcColumnHandle> columns = new ArrayList<>();
        while (resultSet.next()) {
            // skip if table doesn't match expected
            if (!(Objects.equals(remoteTableName, getRemoteTable(resultSet)))) {
                continue;
            }
            allColumns++;
            String columnName = resultSet.getString("COLUMN_NAME");
            JdbcTypeHandle typeHandle = new JdbcTypeHandle(getInteger(resultSet, "DATA_TYPE").orElseThrow(() -> new IllegalStateException("DATA_TYPE is null")), Optional.ofNullable(resultSet.getString("TYPE_NAME")), getInteger(resultSet, "COLUMN_SIZE"), getInteger(resultSet, "DECIMAL_DIGITS"), Optional.empty(), Optional.empty());
            Optional<ColumnMapping> columnMapping = toColumnMapping(session, connection, typeHandle);
            log.debug("Mapping data type of '%s' column '%s': %s mapped to %s", schemaTableName, columnName, typeHandle, columnMapping);
            boolean nullable = (resultSet.getInt("NULLABLE") != columnNoNulls);
            // Note: some databases (e.g. SQL Server) do not return column remarks/comment here.
            Optional<String> comment = Optional.ofNullable(emptyToNull(resultSet.getString("REMARKS")));
            // skip unsupported column types
            columnMapping.ifPresent(mapping -> columns.add(JdbcColumnHandle.builder().setColumnName(columnName).setJdbcTypeHandle(typeHandle).setColumnType(mapping.getType()).setNullable(nullable).setComment(comment).build()));
            if (columnMapping.isEmpty()) {
                UnsupportedTypeHandling unsupportedTypeHandling = getUnsupportedTypeHandling(session);
                verify(unsupportedTypeHandling == IGNORE, "Unsupported type handling is set to %s, but toColumnMapping() returned empty for %s", unsupportedTypeHandling, typeHandle);
            }
        }
        if (columns.isEmpty()) {
            // A table may have no supported columns. In rare cases (e.g. PostgreSQL) a table might have no columns at all.
            throw new TableNotFoundException(schemaTableName, format("Table '%s' has no supported columns (all %s columns are not supported)", schemaTableName, allColumns));
        }
        return ImmutableList.copyOf(columns);
    } catch (SQLException e) {
        throw new TrinoException(JDBC_ERROR, e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) SchemaTableName(io.trino.spi.connector.SchemaTableName) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) ResultSet(java.sql.ResultSet) TrinoException(io.trino.spi.TrinoException) TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling(io.trino.plugin.jdbc.TypeHandlingJdbcSessionProperties.getUnsupportedTypeHandling)

Aggregations

TableNotFoundException (io.trino.spi.connector.TableNotFoundException)84 SchemaTableName (io.trino.spi.connector.SchemaTableName)65 TrinoException (io.trino.spi.TrinoException)39 Table (io.trino.plugin.hive.metastore.Table)33 ImmutableMap (com.google.common.collect.ImmutableMap)27 ImmutableList (com.google.common.collect.ImmutableList)26 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)26 List (java.util.List)25 Optional (java.util.Optional)24 HdfsContext (io.trino.plugin.hive.HdfsEnvironment.HdfsContext)22 Path (org.apache.hadoop.fs.Path)22 ColumnHandle (io.trino.spi.connector.ColumnHandle)21 Map (java.util.Map)21 Objects.requireNonNull (java.util.Objects.requireNonNull)20 ConnectorSession (io.trino.spi.connector.ConnectorSession)19 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)18 TupleDomain (io.trino.spi.predicate.TupleDomain)18 Set (java.util.Set)18 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)17 CatalogSchemaTableName (io.trino.spi.connector.CatalogSchemaTableName)17