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());
}
}
}
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()));
}
}
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);
}
}
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();
}
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));
}
Aggregations