Search in sources :

Example 1 with TrinoException

use of io.trino.spi.TrinoException in project TiBigData by tidb-incubator.

the class TiDBPageSink method appendColumn.

private void appendColumn(Page page, int position, int channel) throws SQLException {
    Block block = page.getBlock(channel);
    int parameter = channel + 1;
    if (block.isNull(position)) {
        statement.setObject(parameter, null);
        return;
    }
    Type type = columnTypes.get(channel);
    switch(type.getDisplayName()) {
        case "boolean":
            statement.setBoolean(parameter, type.getBoolean(block, position));
            break;
        case "tinyint":
            statement.setByte(parameter, SignedBytes.checkedCast(type.getLong(block, position)));
            break;
        case "smallint":
            statement.setShort(parameter, Shorts.checkedCast(type.getLong(block, position)));
            break;
        case "integer":
            statement.setInt(parameter, toIntExact(type.getLong(block, position)));
            break;
        case "bigint":
            statement.setLong(parameter, type.getLong(block, position));
            break;
        case "real":
            statement.setFloat(parameter, intBitsToFloat(toIntExact(type.getLong(block, position))));
            break;
        case "double":
            statement.setDouble(parameter, type.getDouble(block, position));
            break;
        case "date":
            // convert to midnight in default time zone
            long utcMillis = DAYS.toMillis(type.getLong(block, position));
            long localMillis = getInstanceUTC().getZone().getMillisKeepLocal(DateTimeZone.getDefault(), utcMillis);
            statement.setDate(parameter, new Date(localMillis));
            break;
        case "varbinary":
            statement.setBytes(parameter, type.getSlice(block, position).getBytes());
            break;
        default:
            if (type instanceof DecimalType) {
                statement.setBigDecimal(parameter, readBigDecimal((DecimalType) type, block, position));
            } else if (type instanceof VarcharType || type instanceof CharType) {
                statement.setString(parameter, type.getSlice(block, position).toStringUtf8());
            } else if (type instanceof TimestampType) {
                statement.setTimestamp(parameter, new Timestamp(type.getLong(block, position) / 1000 - TimeZone.getDefault().getRawOffset()));
            } else if (type instanceof TimeType) {
                statement.setTime(parameter, new Time(type.getLong(block, position)));
            } else {
                throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
            }
    }
}
Also used : VarcharType(io.trino.spi.type.VarcharType) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.sql.Date) TimeType(io.trino.spi.type.TimeType) TimeType(io.trino.spi.type.TimeType) Type(io.trino.spi.type.Type) TimestampType(io.trino.spi.type.TimestampType) VarcharType(io.trino.spi.type.VarcharType) CharType(io.trino.spi.type.CharType) DecimalType(io.trino.spi.type.DecimalType) Block(io.trino.spi.block.Block) DecimalType(io.trino.spi.type.DecimalType) TimestampType(io.trino.spi.type.TimestampType) TrinoException(io.trino.spi.TrinoException) CharType(io.trino.spi.type.CharType)

Example 2 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class SystemPageSourceProvider method createPageSource.

@Override
public ConnectorPageSource createPageSource(ConnectorTransactionHandle transaction, ConnectorSession session, ConnectorSplit split, ConnectorTableHandle table, List<ColumnHandle> columns, DynamicFilter dynamicFilter) {
    requireNonNull(columns, "columns is null");
    SystemTransactionHandle systemTransaction = (SystemTransactionHandle) transaction;
    SystemSplit systemSplit = (SystemSplit) split;
    SchemaTableName tableName = ((SystemTableHandle) table).getSchemaTableName();
    SystemTable systemTable = tables.getSystemTable(session, tableName).orElseThrow(() -> new TrinoException(NOT_FOUND, format("Table '%s' not found", tableName)));
    List<ColumnMetadata> tableColumns = systemTable.getTableMetadata().getColumns();
    Map<String, Integer> columnsByName = new HashMap<>();
    for (int i = 0; i < tableColumns.size(); i++) {
        ColumnMetadata column = tableColumns.get(i);
        if (columnsByName.put(column.getName(), i) != null) {
            throw new TrinoException(GENERIC_INTERNAL_ERROR, "Duplicate column name: " + column.getName());
        }
    }
    ImmutableList.Builder<Integer> userToSystemFieldIndex = ImmutableList.builder();
    for (ColumnHandle column : columns) {
        String columnName = ((SystemColumnHandle) column).getColumnName();
        Integer index = columnsByName.get(columnName);
        if (index == null) {
            throw new TrinoException(GENERIC_INTERNAL_ERROR, format("Column does not exist: %s.%s", tableName, columnName));
        }
        userToSystemFieldIndex.add(index);
    }
    TupleDomain<ColumnHandle> constraint = systemSplit.getConstraint();
    if (constraint.isNone()) {
        return new EmptyPageSource();
    }
    TupleDomain<Integer> newConstraint = systemSplit.getConstraint().transformKeys(columnHandle -> columnsByName.get(((SystemColumnHandle) columnHandle).getColumnName()));
    try {
        return new MappedPageSource(systemTable.pageSource(systemTransaction.getConnectorTransactionHandle(), session, newConstraint), userToSystemFieldIndex.build());
    } catch (UnsupportedOperationException e) {
        return new RecordPageSource(new MappedRecordSet(toRecordSet(systemTransaction.getConnectorTransactionHandle(), systemTable, session, newConstraint), userToSystemFieldIndex.build()));
    }
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) SchemaTableName(io.trino.spi.connector.SchemaTableName) RecordPageSource(io.trino.spi.connector.RecordPageSource) MappedPageSource(io.trino.split.MappedPageSource) EmptyPageSource(io.trino.spi.connector.EmptyPageSource) MappedRecordSet(io.trino.split.MappedRecordSet) TrinoException(io.trino.spi.TrinoException) SystemTable(io.trino.spi.connector.SystemTable)

Example 3 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class KillQueryProcedure method killQuery.

@UsedByGeneratedCode
public void killQuery(String queryId, String message, ConnectorSession session) {
    QueryId query = parseQueryId(queryId);
    try {
        checkState(dispatchManager.isPresent(), "No dispatch manager is set. kill_query procedure should be executed on coordinator.");
        DispatchQuery dispatchQuery = dispatchManager.get().getQuery(query);
        checkCanKillQueryOwnedBy(((FullConnectorSession) session).getSession().getIdentity(), dispatchQuery.getSession().getIdentity(), accessControl);
        // check before killing to provide the proper error message (this is racy)
        if (dispatchQuery.isDone()) {
            throw new TrinoException(NOT_SUPPORTED, "Target query is not running: " + queryId);
        }
        dispatchQuery.fail(createKillQueryException(message));
        // verify if the query was killed (if not, we lost the race)
        checkState(dispatchQuery.isDone(), "Failure to fail the query: %s", query);
        if (!ADMINISTRATIVELY_KILLED.toErrorCode().equals(dispatchQuery.getErrorCode().orElse(null))) {
            throw new TrinoException(NOT_SUPPORTED, "Target query is not running: " + queryId);
        }
    } catch (NoSuchElementException e) {
        throw new TrinoException(NOT_FOUND, "Target query not found: " + queryId);
    }
}
Also used : DispatchQuery(io.trino.dispatcher.DispatchQuery) QueryId(io.trino.spi.QueryId) TrinoException(io.trino.spi.TrinoException) NoSuchElementException(java.util.NoSuchElementException) FullConnectorSession(io.trino.FullConnectorSession) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode)

Example 4 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class TableCommentSystemTable method cursor.

@Override
public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, ConnectorSession connectorSession, TupleDomain<Integer> constraint) {
    Optional<String> catalogFilter = tryGetSingleVarcharValue(constraint, 0);
    Optional<String> schemaFilter = tryGetSingleVarcharValue(constraint, 1);
    Optional<String> tableFilter = tryGetSingleVarcharValue(constraint, 2);
    Session session = ((FullConnectorSession) connectorSession).getSession();
    Builder table = InMemoryRecordSet.builder(COMMENT_TABLE);
    for (String catalog : listCatalogs(session, metadata, accessControl, catalogFilter).keySet()) {
        QualifiedTablePrefix prefix = tablePrefix(catalog, schemaFilter, tableFilter);
        Set<SchemaTableName> names = ImmutableSet.of();
        Map<SchemaTableName, ViewInfo> views = ImmutableMap.of();
        Map<SchemaTableName, ViewInfo> materializedViews = ImmutableMap.of();
        try {
            materializedViews = getMaterializedViews(session, metadata, accessControl, prefix);
            views = getViews(session, metadata, accessControl, prefix);
            // Some connectors like blackhole, accumulo and raptor don't return views in listTables
            // Materialized views are consistently returned in listTables by the relevant connectors
            names = union(listTables(session, metadata, accessControl, prefix), views.keySet());
        } catch (TrinoException e) {
            // listTables throws an exception if cannot connect the database
            LOG.warn(e, "Failed to get tables for catalog: %s", catalog);
        }
        for (SchemaTableName name : names) {
            Optional<String> comment = Optional.empty();
            try {
                comment = getComment(session, prefix, name, views, materializedViews);
            } catch (RuntimeException e) {
                // getTableHandle may throw an exception (e.g. Cassandra connector doesn't allow case insensitive column names)
                LOG.warn(e, "Failed to get metadata for table: %s", name);
            }
            table.addRow(prefix.getCatalogName(), name.getSchemaName(), name.getTableName(), comment.orElse(null));
        }
    }
    return table.build().cursor();
}
Also used : QualifiedTablePrefix(io.trino.metadata.QualifiedTablePrefix) TableMetadataBuilder.tableMetadataBuilder(io.trino.metadata.MetadataUtil.TableMetadataBuilder.tableMetadataBuilder) Builder(io.trino.spi.connector.InMemoryRecordSet.Builder) TrinoException(io.trino.spi.TrinoException) SchemaTableName(io.trino.spi.connector.SchemaTableName) FullConnectorSession(io.trino.FullConnectorSession) ConnectorSession(io.trino.spi.connector.ConnectorSession) Session(io.trino.Session) FullConnectorSession(io.trino.FullConnectorSession) ViewInfo(io.trino.metadata.ViewInfo)

Example 5 with TrinoException

use of io.trino.spi.TrinoException in project trino by trinodb.

the class MetadataManager method getRedirectedTableName.

private QualifiedObjectName getRedirectedTableName(Session session, QualifiedObjectName originalTableName) {
    requireNonNull(session, "session is null");
    requireNonNull(originalTableName, "originalTableName is null");
    if (originalTableName.getCatalogName().isEmpty() || originalTableName.getSchemaName().isEmpty() || originalTableName.getObjectName().isEmpty()) {
        // table cannot exist
        return originalTableName;
    }
    QualifiedObjectName tableName = originalTableName;
    Set<QualifiedObjectName> visitedTableNames = new LinkedHashSet<>();
    visitedTableNames.add(tableName);
    for (int count = 0; count < MAX_TABLE_REDIRECTIONS; count++) {
        Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, tableName.getCatalogName());
        if (catalog.isEmpty()) {
            // Stop redirection
            return tableName;
        }
        CatalogMetadata catalogMetadata = catalog.get();
        CatalogName catalogName = catalogMetadata.getConnectorId(session, tableName);
        ConnectorMetadata metadata = catalogMetadata.getMetadataFor(session, catalogName);
        Optional<QualifiedObjectName> redirectedTableName = metadata.redirectTable(session.toConnectorSession(catalogName), tableName.asSchemaTableName()).map(name -> convertFromSchemaTableName(name.getCatalogName()).apply(name.getSchemaTableName()));
        if (redirectedTableName.isEmpty()) {
            return tableName;
        }
        tableName = redirectedTableName.get();
        // Check for loop in redirection
        if (!visitedTableNames.add(tableName)) {
            throw new TrinoException(TABLE_REDIRECTION_ERROR, format("Table redirections form a loop: %s", Streams.concat(visitedTableNames.stream(), Stream.of(tableName)).map(QualifiedObjectName::toString).collect(Collectors.joining(" -> "))));
        }
    }
    throw new TrinoException(TABLE_REDIRECTION_ERROR, format("Table redirected too many times (%d): %s", MAX_TABLE_REDIRECTIONS, visitedTableNames));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TrinoException(io.trino.spi.TrinoException) CatalogName(io.trino.connector.CatalogName) ConnectorMetadata(io.trino.spi.connector.ConnectorMetadata) Constraint(io.trino.spi.connector.Constraint)

Aggregations

TrinoException (io.trino.spi.TrinoException)623 IOException (java.io.IOException)151 ImmutableList (com.google.common.collect.ImmutableList)105 List (java.util.List)100 Type (io.trino.spi.type.Type)93 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)90 SchemaTableName (io.trino.spi.connector.SchemaTableName)83 Path (org.apache.hadoop.fs.Path)83 Optional (java.util.Optional)79 ArrayList (java.util.ArrayList)77 Map (java.util.Map)76 ImmutableMap (com.google.common.collect.ImmutableMap)70 Objects.requireNonNull (java.util.Objects.requireNonNull)69 ConnectorSession (io.trino.spi.connector.ConnectorSession)63 TableNotFoundException (io.trino.spi.connector.TableNotFoundException)62 ImmutableSet (com.google.common.collect.ImmutableSet)56 VarcharType (io.trino.spi.type.VarcharType)54 Set (java.util.Set)54 Slice (io.airlift.slice.Slice)53 Table (io.trino.plugin.hive.metastore.Table)52