Search in sources :

Example 1 with Builder

use of io.trino.spi.connector.InMemoryRecordSet.Builder in project trino by trinodb.

the class NodeSystemTable method cursor.

@Override
public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, ConnectorSession session, TupleDomain<Integer> constraint) {
    Builder table = InMemoryRecordSet.builder(NODES_TABLE);
    AllNodes allNodes = nodeManager.getAllNodes();
    addRows(table, allNodes.getActiveNodes(), ACTIVE);
    addRows(table, allNodes.getInactiveNodes(), INACTIVE);
    addRows(table, allNodes.getShuttingDownNodes(), SHUTTING_DOWN);
    return table.build().cursor();
}
Also used : TableMetadataBuilder.tableMetadataBuilder(io.trino.metadata.MetadataUtil.TableMetadataBuilder.tableMetadataBuilder) Builder(io.trino.spi.connector.InMemoryRecordSet.Builder) AllNodes(io.trino.metadata.AllNodes)

Example 2 with Builder

use of io.trino.spi.connector.InMemoryRecordSet.Builder in project trino by trinodb.

the class ColumnJdbcTable method applyFilter.

@Override
public TupleDomain<ColumnHandle> applyFilter(ConnectorSession connectorSession, Constraint constraint) {
    TupleDomain<ColumnHandle> tupleDomain = constraint.getSummary();
    if (tupleDomain.isNone() || constraint.predicate().isEmpty()) {
        return tupleDomain;
    }
    Predicate<Map<ColumnHandle, NullableValue>> predicate = constraint.predicate().get();
    Set<ColumnHandle> predicateColumns = constraint.getPredicateColumns().orElseThrow(() -> new VerifyException("columns not present for a predicate"));
    boolean hasSchemaPredicate = predicateColumns.contains(TABLE_SCHEMA_COLUMN);
    boolean hasTablePredicate = predicateColumns.contains(TABLE_NAME_COLUMN);
    if (!hasSchemaPredicate && !hasTablePredicate) {
        // No filter on schema name and table name at all.
        return tupleDomain;
    }
    Session session = ((FullConnectorSession) connectorSession).getSession();
    Optional<String> catalogFilter = tryGetSingleVarcharValue(tupleDomain, TABLE_CATALOG_COLUMN);
    Optional<String> schemaFilter = tryGetSingleVarcharValue(tupleDomain, TABLE_SCHEMA_COLUMN);
    Optional<String> tableFilter = tryGetSingleVarcharValue(tupleDomain, TABLE_NAME_COLUMN);
    if (schemaFilter.isPresent() && tableFilter.isPresent()) {
        // No need to narrow down the domain.
        return tupleDomain;
    }
    List<String> catalogs = listCatalogs(session, metadata, accessControl, catalogFilter).keySet().stream().filter(catalogName -> predicate.test(ImmutableMap.of(TABLE_CATALOG_COLUMN, toNullableValue(catalogName)))).collect(toImmutableList());
    List<CatalogSchemaName> schemas = catalogs.stream().flatMap(catalogName -> listSchemas(session, metadata, accessControl, catalogName, schemaFilter).stream().filter(schemaName -> !hasSchemaPredicate || predicate.test(ImmutableMap.of(TABLE_CATALOG_COLUMN, toNullableValue(catalogName), TABLE_SCHEMA_COLUMN, toNullableValue(schemaName)))).map(schemaName -> new CatalogSchemaName(catalogName, schemaName))).collect(toImmutableList());
    if (!hasTablePredicate) {
        return TupleDomain.withColumnDomains(ImmutableMap.<ColumnHandle, Domain>builder().put(TABLE_CATALOG_COLUMN, schemas.stream().map(CatalogSchemaName::getCatalogName).collect(toVarcharDomain()).simplify(MAX_DOMAIN_SIZE)).put(TABLE_SCHEMA_COLUMN, schemas.stream().map(CatalogSchemaName::getSchemaName).collect(toVarcharDomain()).simplify(MAX_DOMAIN_SIZE)).buildOrThrow());
    }
    List<CatalogSchemaTableName> tables = schemas.stream().flatMap(schema -> {
        QualifiedTablePrefix tablePrefix = tableFilter.isPresent() ? new QualifiedTablePrefix(schema.getCatalogName(), schema.getSchemaName(), tableFilter.get()) : new QualifiedTablePrefix(schema.getCatalogName(), schema.getSchemaName());
        return listTables(session, metadata, accessControl, tablePrefix).stream().filter(schemaTableName -> predicate.test(ImmutableMap.of(TABLE_CATALOG_COLUMN, toNullableValue(schema.getCatalogName()), TABLE_SCHEMA_COLUMN, toNullableValue(schemaTableName.getSchemaName()), TABLE_NAME_COLUMN, toNullableValue(schemaTableName.getTableName())))).map(schemaTableName -> new CatalogSchemaTableName(schema.getCatalogName(), schemaTableName.getSchemaName(), schemaTableName.getTableName()));
    }).collect(toImmutableList());
    return TupleDomain.withColumnDomains(ImmutableMap.<ColumnHandle, Domain>builder().put(TABLE_CATALOG_COLUMN, tables.stream().map(CatalogSchemaTableName::getCatalogName).collect(toVarcharDomain()).simplify(MAX_DOMAIN_SIZE)).put(TABLE_SCHEMA_COLUMN, tables.stream().map(catalogSchemaTableName -> catalogSchemaTableName.getSchemaTableName().getSchemaName()).collect(toVarcharDomain()).simplify(MAX_DOMAIN_SIZE)).put(TABLE_NAME_COLUMN, tables.stream().map(catalogSchemaTableName -> catalogSchemaTableName.getSchemaTableName().getTableName()).collect(toVarcharDomain()).simplify(MAX_DOMAIN_SIZE)).buildOrThrow());
}
Also used : FilterUtil.tryGetSingleVarcharValue(io.trino.connector.system.jdbc.FilterUtil.tryGetSingleVarcharValue) TableMetadataBuilder.tableMetadataBuilder(io.trino.metadata.MetadataUtil.TableMetadataBuilder.tableMetadataBuilder) TimestampWithTimeZoneType(io.trino.spi.type.TimestampWithTimeZoneType) Slices(io.airlift.slice.Slices) Map(java.util.Map) CatalogSchemaName(io.trino.spi.connector.CatalogSchemaName) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Collector(java.util.stream.Collector) INTEGER(io.trino.spi.type.IntegerType.INTEGER) ENGLISH(java.util.Locale.ENGLISH) SMALLINT(io.trino.spi.type.SmallintType.SMALLINT) MetadataListing.listCatalogs(io.trino.metadata.MetadataListing.listCatalogs) ImmutableMap(com.google.common.collect.ImmutableMap) TypeUtils.getDisplayLabel(io.trino.type.TypeUtils.getDisplayLabel) Predicate(java.util.function.Predicate) Domain(io.trino.spi.predicate.Domain) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) ArrayType(io.trino.spi.type.ArrayType) Math.min(java.lang.Math.min) Collectors(java.util.stream.Collectors) SchemaTableName(io.trino.spi.connector.SchemaTableName) ZoneId(java.time.ZoneId) List(java.util.List) AccessControl(io.trino.security.AccessControl) BIGINT(io.trino.spi.type.BigintType.BIGINT) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) Entry(java.util.Map.Entry) Optional(java.util.Optional) DecimalType(io.trino.spi.type.DecimalType) DATE(io.trino.spi.type.DateType.DATE) REAL(io.trino.spi.type.RealType.REAL) Session(io.trino.Session) TimeWithTimeZoneType(io.trino.spi.type.TimeWithTimeZoneType) Types(java.sql.Types) Constraint(io.trino.spi.connector.Constraint) TimeType(io.trino.spi.type.TimeType) FullConnectorSession(io.trino.FullConnectorSession) NullableValue(io.trino.spi.predicate.NullableValue) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) Type(io.trino.spi.type.Type) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) DatabaseMetaData(java.sql.DatabaseMetaData) TimestampType(io.trino.spi.type.TimestampType) Inject(javax.inject.Inject) VarcharType(io.trino.spi.type.VarcharType) Objects.requireNonNull(java.util.Objects.requireNonNull) ColumnHandle(io.trino.spi.connector.ColumnHandle) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) VARBINARY(io.trino.spi.type.VarbinaryType.VARBINARY) QualifiedTablePrefix(io.trino.metadata.QualifiedTablePrefix) MetadataListing.listTables(io.trino.metadata.MetadataListing.listTables) VerifyException(com.google.common.base.VerifyException) MetadataListing.listSchemas(io.trino.metadata.MetadataListing.listSchemas) RecordCursor(io.trino.spi.connector.RecordCursor) MetadataListing.listTableColumns(io.trino.metadata.MetadataListing.listTableColumns) ConnectorSession(io.trino.spi.connector.ConnectorSession) TupleDomain(io.trino.spi.predicate.TupleDomain) InMemoryRecordSet(io.trino.spi.connector.InMemoryRecordSet) Builder(io.trino.spi.connector.InMemoryRecordSet.Builder) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) SystemColumnHandle(io.trino.connector.system.SystemColumnHandle) CharType(io.trino.spi.type.CharType) SystemSessionProperties.isOmitDateTimeTypePrecision(io.trino.SystemSessionProperties.isOmitDateTimeTypePrecision) Metadata(io.trino.metadata.Metadata) FilterUtil.tablePrefix(io.trino.connector.system.jdbc.FilterUtil.tablePrefix) TINYINT(io.trino.spi.type.TinyintType.TINYINT) ConnectorTransactionHandle(io.trino.spi.connector.ConnectorTransactionHandle) ColumnHandle(io.trino.spi.connector.ColumnHandle) SystemColumnHandle(io.trino.connector.system.SystemColumnHandle) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) QualifiedTablePrefix(io.trino.metadata.QualifiedTablePrefix) VerifyException(com.google.common.base.VerifyException) CatalogSchemaName(io.trino.spi.connector.CatalogSchemaName) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Session(io.trino.Session) FullConnectorSession(io.trino.FullConnectorSession) ConnectorSession(io.trino.spi.connector.ConnectorSession) FullConnectorSession(io.trino.FullConnectorSession)

Example 3 with Builder

use of io.trino.spi.connector.InMemoryRecordSet.Builder in project trino by trinodb.

the class SchemaJdbcTable method cursor.

@Override
public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, ConnectorSession connectorSession, TupleDomain<Integer> constraint) {
    Session session = ((FullConnectorSession) connectorSession).getSession();
    Optional<String> catalogFilter = tryGetSingleVarcharValue(constraint, 1);
    Builder table = InMemoryRecordSet.builder(METADATA);
    for (String catalog : listCatalogs(session, metadata, accessControl, catalogFilter).keySet()) {
        for (String schema : listSchemas(session, metadata, accessControl, catalog)) {
            table.addRow(schema, catalog);
        }
    }
    return table.build().cursor();
}
Also used : TableMetadataBuilder.tableMetadataBuilder(io.trino.metadata.MetadataUtil.TableMetadataBuilder.tableMetadataBuilder) Builder(io.trino.spi.connector.InMemoryRecordSet.Builder) FullConnectorSession(io.trino.FullConnectorSession) ConnectorSession(io.trino.spi.connector.ConnectorSession) Session(io.trino.Session) FullConnectorSession(io.trino.FullConnectorSession)

Example 4 with Builder

use of io.trino.spi.connector.InMemoryRecordSet.Builder 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 Builder

use of io.trino.spi.connector.InMemoryRecordSet.Builder in project trino by trinodb.

the class TaskSystemTable method cursor.

@Override
public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, ConnectorSession session, TupleDomain<Integer> constraint) {
    Builder table = InMemoryRecordSet.builder(TASK_TABLE);
    for (TaskInfo taskInfo : taskManager.getAllTaskInfo()) {
        TaskStats stats = taskInfo.getStats();
        TaskStatus taskStatus = taskInfo.getTaskStatus();
        table.addRow(nodeId, taskStatus.getTaskId().toString(), taskStatus.getTaskId().getStageId().toString(), taskStatus.getTaskId().getQueryId().toString(), taskStatus.getState().toString(), (long) stats.getTotalDrivers(), (long) stats.getQueuedDrivers(), (long) stats.getRunningDrivers(), (long) stats.getCompletedDrivers(), toMillis(stats.getTotalScheduledTime()), toMillis(stats.getTotalCpuTime()), toMillis(stats.getTotalBlockedTime()), toBytes(stats.getRawInputDataSize()), stats.getRawInputPositions(), toBytes(stats.getProcessedInputDataSize()), stats.getProcessedInputPositions(), toBytes(stats.getOutputDataSize()), stats.getOutputPositions(), toBytes(stats.getPhysicalInputDataSize()), toBytes(stats.getPhysicalWrittenDataSize()), toTimestampWithTimeZoneMillis(stats.getCreateTime()), toTimestampWithTimeZoneMillis(stats.getFirstStartTime()), toTimestampWithTimeZoneMillis(taskInfo.getLastHeartbeat()), toTimestampWithTimeZoneMillis(stats.getEndTime()));
    }
    return table.build().cursor();
}
Also used : TaskInfo(io.trino.execution.TaskInfo) TableMetadataBuilder.tableMetadataBuilder(io.trino.metadata.MetadataUtil.TableMetadataBuilder.tableMetadataBuilder) Builder(io.trino.spi.connector.InMemoryRecordSet.Builder) TaskStats(io.trino.operator.TaskStats) TaskStatus(io.trino.execution.TaskStatus)

Aggregations

TableMetadataBuilder.tableMetadataBuilder (io.trino.metadata.MetadataUtil.TableMetadataBuilder.tableMetadataBuilder)10 Builder (io.trino.spi.connector.InMemoryRecordSet.Builder)10 FullConnectorSession (io.trino.FullConnectorSession)7 Session (io.trino.Session)7 ConnectorSession (io.trino.spi.connector.ConnectorSession)7 QualifiedTablePrefix (io.trino.metadata.QualifiedTablePrefix)4 SchemaTableName (io.trino.spi.connector.SchemaTableName)4 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 CatalogSchemaTableName (io.trino.spi.connector.CatalogSchemaTableName)2 Map (java.util.Map)2 VerifyException (com.google.common.base.VerifyException)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 Slices (io.airlift.slice.Slices)1 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)1 SystemSessionProperties.isOmitDateTimeTypePrecision (io.trino.SystemSessionProperties.isOmitDateTimeTypePrecision)1 SystemColumnHandle (io.trino.connector.system.SystemColumnHandle)1 FilterUtil.tablePrefix (io.trino.connector.system.jdbc.FilterUtil.tablePrefix)1 FilterUtil.tryGetSingleVarcharValue (io.trino.connector.system.jdbc.FilterUtil.tryGetSingleVarcharValue)1 QueryInfo (io.trino.execution.QueryInfo)1