private ConnectorMetadata getMetadata(Session session, ConnectorId connectorId) { return getCatalogMetadata(session, connectorId).getMetadataFor(connectorId); }
@Override public List<String> listSchemaNames(Session session, String catalogName) { Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, catalogName); ImmutableSet.Builder<String> schemaNames = ImmutableSet.builder(); if (catalog.isPresent()) { CatalogMetadata catalogMetadata = catalog.get(); ConnectorSession connectorSession = session.toConnectorSession(catalogMetadata.getConnectorId()); for (ConnectorId connectorId : catalogMetadata.listConnectorIds()) { ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId); metadata.listSchemaNames(connectorSession).stream() .map(schema -> schema.toLowerCase(Locale.ENGLISH)) .forEach(schemaNames::add); } } return ImmutableList.copyOf(schemaNames.build()); }
@Override public Optional<SystemTable> getSystemTable(Session session, QualifiedObjectName tableName) { requireNonNull(session, "session is null"); requireNonNull(tableName, "table is null"); Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, tableName.getCatalogName()); if (catalog.isPresent()) { CatalogMetadata catalogMetadata = catalog.get(); // we query only main connector for runtime system tables ConnectorId connectorId = catalogMetadata.getConnectorId(); ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId); return metadata.getSystemTable(session.toConnectorSession(connectorId), tableName.asSchemaTableName()); } return Optional.empty(); }
@Override public List<GrantInfo> listTablePrivileges(Session session, QualifiedTablePrefix prefix) { requireNonNull(prefix, "prefix is null"); SchemaTablePrefix tablePrefix = prefix.asSchemaTablePrefix(); Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, prefix.getCatalogName()); ImmutableSet.Builder<GrantInfo> grantInfos = ImmutableSet.builder(); if (catalog.isPresent()) { CatalogMetadata catalogMetadata = catalog.get(); ConnectorSession connectorSession = session.toConnectorSession(catalogMetadata.getConnectorId()); for (ConnectorId connectorId : catalogMetadata.listConnectorIds()) { ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId); grantInfos.addAll(metadata.listTablePrivileges(connectorSession, tablePrefix)); } } return ImmutableList.copyOf(grantInfos.build()); }
@Override public Optional<TableHandle> getTableHandle(Session session, QualifiedObjectName table) { requireNonNull(table, "table is null"); Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, table.getCatalogName()); if (catalog.isPresent()) { CatalogMetadata catalogMetadata = catalog.get(); ConnectorId connectorId = catalogMetadata.getConnectorId(session, table); ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId); ConnectorTableHandle tableHandle = metadata.getTableHandle(session.toConnectorSession(connectorId), table.asSchemaTableName()); if (tableHandle != null) { return Optional.of(new TableHandle(connectorId, tableHandle)); } } return Optional.empty(); }
@Override public List<QualifiedObjectName> listTables(Session session, QualifiedTablePrefix prefix) { requireNonNull(prefix, "prefix is null"); Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, prefix.getCatalogName()); Set<QualifiedObjectName> tables = new LinkedHashSet<>(); if (catalog.isPresent()) { CatalogMetadata catalogMetadata = catalog.get(); for (ConnectorId connectorId : catalogMetadata.listConnectorIds()) { ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId); ConnectorSession connectorSession = session.toConnectorSession(connectorId); metadata.listTables(connectorSession, prefix.getSchemaName()).stream() .map(convertFromSchemaTableName(prefix.getCatalogName())) .filter(prefix::matches) .forEach(tables::add); } } return ImmutableList.copyOf(tables); }
@Override public List<QualifiedObjectName> listViews(Session session, QualifiedTablePrefix prefix) { requireNonNull(prefix, "prefix is null"); Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, prefix.getCatalogName()); Set<QualifiedObjectName> views = new LinkedHashSet<>(); if (catalog.isPresent()) { CatalogMetadata catalogMetadata = catalog.get(); for (ConnectorId connectorId : catalogMetadata.listConnectorIds()) { ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId); ConnectorSession connectorSession = session.toConnectorSession(connectorId); metadata.listViews(connectorSession, prefix.getSchemaName()).stream() .map(convertFromSchemaTableName(prefix.getCatalogName())) .filter(prefix::matches) .forEach(views::add); } } return ImmutableList.copyOf(views); }
@Override public Optional<PartitioningHandle> getCommonPartitioning(Session session, PartitioningHandle left, PartitioningHandle right) { Optional<ConnectorId> leftConnectorId = left.getConnectorId(); Optional<ConnectorId> rightConnectorId = right.getConnectorId(); if (!leftConnectorId.isPresent() || !rightConnectorId.isPresent() || !leftConnectorId.equals(rightConnectorId)) { return Optional.empty(); } if (!left.getTransactionHandle().equals(right.getTransactionHandle())) { return Optional.empty(); } ConnectorId connectorId = leftConnectorId.get(); CatalogMetadata catalogMetadata = getCatalogMetadata(session, connectorId); ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId); Optional<ConnectorPartitioningHandle> commonHandle = metadata.getCommonPartitioningHandle(session.toConnectorSession(connectorId), left.getConnectorHandle(), right.getConnectorHandle()); return commonHandle.map(handle -> new PartitioningHandle(Optional.of(connectorId), left.getTransactionHandle(), handle)); }
@Override public Map<QualifiedObjectName, ViewDefinition> getViews(Session session, QualifiedTablePrefix prefix) { requireNonNull(prefix, "prefix is null"); Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, prefix.getCatalogName()); Map<QualifiedObjectName, ViewDefinition> views = new LinkedHashMap<>(); if (catalog.isPresent()) { CatalogMetadata catalogMetadata = catalog.get(); SchemaTablePrefix tablePrefix = prefix.asSchemaTablePrefix(); for (ConnectorId connectorId : catalogMetadata.listConnectorIds()) { ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId); ConnectorSession connectorSession = session.toConnectorSession(connectorId); for (Entry<SchemaTableName, ConnectorViewDefinition> entry : metadata.getViews(connectorSession, tablePrefix).entrySet()) { QualifiedObjectName viewName = new QualifiedObjectName( prefix.getCatalogName(), entry.getKey().getSchemaName(), entry.getKey().getTableName()); views.put(viewName, deserializeView(entry.getValue().getViewData())); } } } return ImmutableMap.copyOf(views); }
@Override public TableLayoutHandle getAlternativeLayoutHandle(Session session, TableLayoutHandle tableLayoutHandle, PartitioningHandle partitioningHandle) { checkArgument(partitioningHandle.getConnectorId().isPresent(), "Expect partitioning handle from connector, got system partitioning handle"); ConnectorId connectorId = partitioningHandle.getConnectorId().get(); checkArgument(connectorId.equals(tableLayoutHandle.getConnectorId()), "ConnectorId of tableLayoutHandle and partitioningHandle does not match"); CatalogMetadata catalogMetadata = getCatalogMetadata(session, connectorId); ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId); ConnectorTransactionHandle transaction = catalogMetadata.getTransactionHandleFor(connectorId); ConnectorTableLayoutHandle newTableLayoutHandle = metadata.getAlternativeLayoutHandle(session.toConnectorSession(connectorId), tableLayoutHandle.getConnectorHandle(), partitioningHandle.getConnectorHandle()); return new TableLayoutHandle(connectorId, transaction, newTableLayoutHandle); }
ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId);
@Override public Optional<ViewDefinition> getView(Session session, QualifiedObjectName viewName) { Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, viewName.getCatalogName()); if (catalog.isPresent()) { CatalogMetadata catalogMetadata = catalog.get(); ConnectorId connectorId = catalogMetadata.getConnectorId(session, viewName); ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId); Map<SchemaTableName, ConnectorViewDefinition> views = metadata.getViews( session.toConnectorSession(connectorId), viewName.asSchemaTableName().toSchemaTablePrefix()); ConnectorViewDefinition view = views.get(viewName.asSchemaTableName()); if (view != null) { ViewDefinition definition = deserializeView(view.getViewData()); if (view.getOwner().isPresent()) { definition = definition.withOwner(view.getOwner().get()); } return Optional.of(definition); } } return Optional.empty(); }
@Override public Optional<ResolvedIndex> resolveIndex(Session session, TableHandle tableHandle, Set<ColumnHandle> indexableColumns, Set<ColumnHandle> outputColumns, TupleDomain<ColumnHandle> tupleDomain) { ConnectorId connectorId = tableHandle.getConnectorId(); CatalogMetadata catalogMetadata = getCatalogMetadata(session, connectorId); ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId); ConnectorTransactionHandle transaction = catalogMetadata.getTransactionHandleFor(connectorId); ConnectorSession connectorSession = session.toConnectorSession(connectorId); Optional<ConnectorResolvedIndex> resolvedIndex = metadata.resolveIndex(connectorSession, tableHandle.getConnectorHandle(), indexableColumns, outputColumns, tupleDomain); return resolvedIndex.map(resolved -> new ResolvedIndex(tableHandle.getConnectorId(), transaction, resolved)); }
@Override public List<TableLayoutResult> getLayouts(Session session, TableHandle table, Constraint<ColumnHandle> constraint, Optional<Set<ColumnHandle>> desiredColumns) { if (constraint.getSummary().isNone()) { return ImmutableList.of(); } ConnectorId connectorId = table.getConnectorId(); ConnectorTableHandle connectorTable = table.getConnectorHandle(); CatalogMetadata catalogMetadata = getCatalogMetadata(session, connectorId); ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId); ConnectorTransactionHandle transaction = catalogMetadata.getTransactionHandleFor(connectorId); ConnectorSession connectorSession = session.toConnectorSession(connectorId); List<ConnectorTableLayoutResult> layouts = metadata.getTableLayouts(connectorSession, connectorTable, constraint, desiredColumns); return layouts.stream() .map(layout -> new TableLayoutResult(fromConnectorLayout(connectorId, transaction, layout.getTableLayout()), layout.getUnenforcedConstraint())) .collect(toImmutableList()); }
@Override public TableLayout getLayout(Session session, TableLayoutHandle handle) { ConnectorId connectorId = handle.getConnectorId(); CatalogMetadata catalogMetadata = getCatalogMetadata(session, connectorId); ConnectorMetadata metadata = catalogMetadata.getMetadataFor(connectorId); ConnectorTransactionHandle transaction = catalogMetadata.getTransactionHandleFor(connectorId); return fromConnectorLayout(connectorId, transaction, metadata.getTableLayout(session.toConnectorSession(connectorId), handle.getConnectorHandle())); }