Search in sources :

Example 16 with ConnectorViewDefinition

use of io.prestosql.spi.connector.ConnectorViewDefinition in project hetu-core by openlookeng.

the class HiveMetadata method processHiveView.

// support hive view without no HQL and hive UDF
private ConnectorViewDefinition processHiveView(ConnectorSession session, Table view, SchemaTableName viewName) {
    // if the table has not set the schema,the viewOriginalText has no the schema,so we should read the viewExpandedText
    String hiveViewQuery = view.getViewExpandedText().orElseThrow(() -> new PrestoException(HiveErrorCode.HIVE_INVALID_METADATA, "No view original text: " + viewName));
    hiveViewQuery = hiveViewQuery.replace('`', '"');
    Optional<String> owner;
    if (view.getOwner() != null) {
        String fullNameOwner = view.getOwner();
        int domainIndex = fullNameOwner.indexOf('@');
        owner = Optional.of((domainIndex < 0) ? fullNameOwner : fullNameOwner.substring(0, domainIndex));
    } else {
        owner = Optional.empty();
    }
    // get column type from view
    List<ConnectorViewDefinition.ViewColumn> viewColumns = new ArrayList<>();
    for (Column item : view.getDataColumns()) {
        ConnectorViewDefinition.ViewColumn vc = new ConnectorViewDefinition.ViewColumn(item.getName(), item.getType().getTypeSignature());
        viewColumns.add(vc);
    }
    return new ConnectorViewDefinition(hiveViewQuery, Optional.of(getCatalogName(session)), Optional.of(view.getDatabaseName()), viewColumns, owner, !owner.isPresent());
}
Also used : SortingColumn(io.prestosql.plugin.hive.metastore.SortingColumn) Column(io.prestosql.plugin.hive.metastore.Column) ArrayList(java.util.ArrayList) PrestoException(io.prestosql.spi.PrestoException) Constraint(io.prestosql.spi.connector.Constraint) ConnectorViewDefinition(io.prestosql.spi.connector.ConnectorViewDefinition)

Example 17 with ConnectorViewDefinition

use of io.prestosql.spi.connector.ConnectorViewDefinition in project hetu-core by openlookeng.

the class MetadataManager method listTableColumns.

@Override
public Map<QualifiedObjectName, List<ColumnMetadata>> listTableColumns(Session session, QualifiedTablePrefix prefix) {
    requireNonNull(prefix, "prefix is null");
    Optional<CatalogMetadata> catalog = getOptionalCatalogMetadata(session, prefix.getCatalogName());
    Map<QualifiedObjectName, List<ColumnMetadata>> tableColumns = new HashMap<>();
    if (catalog.isPresent()) {
        CatalogMetadata catalogMetadata = catalog.get();
        SchemaTablePrefix tablePrefix = prefix.asSchemaTablePrefix();
        for (CatalogName catalogName : catalogMetadata.listConnectorIds()) {
            ConnectorMetadata metadata = catalogMetadata.getMetadataFor(catalogName);
            ConnectorSession connectorSession = session.toConnectorSession(catalogName);
            for (Entry<SchemaTableName, List<ColumnMetadata>> entry : metadata.listTableColumns(connectorSession, tablePrefix).entrySet()) {
                QualifiedObjectName tableName = new QualifiedObjectName(prefix.getCatalogName(), entry.getKey().getSchemaName(), entry.getKey().getTableName());
                tableColumns.put(tableName, entry.getValue());
            }
            // if table and view names overlap, the view wins
            for (Entry<QualifiedObjectName, ConnectorViewDefinition> entry : getViews(session, prefix).entrySet()) {
                ImmutableList.Builder<ColumnMetadata> columns = ImmutableList.builder();
                for (ViewColumn column : entry.getValue().getColumns()) {
                    try {
                        columns.add(new ColumnMetadata(column.getName(), getType(column.getType())));
                    } catch (TypeNotFoundException e) {
                        throw new PrestoException(INVALID_VIEW, format("Unknown type '%s' for column '%s' in view: %s", column.getType(), column.getName(), entry.getKey()));
                    }
                }
                tableColumns.put(entry.getKey(), columns.build());
            }
        }
    }
    return ImmutableMap.copyOf(tableColumns);
}
Also used : ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) ViewColumn(io.prestosql.spi.connector.ConnectorViewDefinition.ViewColumn) SchemaTablePrefix(io.prestosql.spi.connector.SchemaTablePrefix) PrestoException(io.prestosql.spi.PrestoException) MetadataUtil.toSchemaTableName(io.prestosql.metadata.MetadataUtil.toSchemaTableName) MetadataUtil.convertFromSchemaTableName(io.prestosql.metadata.MetadataUtil.convertFromSchemaTableName) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) ConnectorViewDefinition(io.prestosql.spi.connector.ConnectorViewDefinition) TypeNotFoundException(io.prestosql.spi.type.TypeNotFoundException) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) CatalogName(io.prestosql.spi.connector.CatalogName) ConnectorSession(io.prestosql.spi.connector.ConnectorSession) ConnectorMetadata(io.prestosql.spi.connector.ConnectorMetadata)

Example 18 with ConnectorViewDefinition

use of io.prestosql.spi.connector.ConnectorViewDefinition in project hetu-core by openlookeng.

the class AbstractTestHive method doCreateView.

private void doCreateView(SchemaTableName viewName, boolean replace) {
    String viewData = "test data";
    ConnectorViewDefinition definition = new ConnectorViewDefinition(viewData, Optional.empty(), Optional.empty(), ImmutableList.of(new ViewColumn("test", BIGINT.getTypeSignature())), Optional.empty(), true);
    try (Transaction transaction = newTransaction()) {
        transaction.getMetadata().createView(newSession(), viewName, definition, replace);
        transaction.commit();
    }
    try (Transaction transaction = newTransaction()) {
        ConnectorMetadata metadata = transaction.getMetadata();
        assertThat(metadata.getView(newSession(), viewName)).map(ConnectorViewDefinition::getOriginalSql).contains(viewData);
        Map<SchemaTableName, ConnectorViewDefinition> views = metadata.getViews(newSession(), Optional.of(viewName.getSchemaName()));
        assertEquals(views.size(), 1);
        assertEquals(views.get(viewName).getOriginalSql(), definition.getOriginalSql());
        assertTrue(metadata.listViews(newSession(), Optional.of(viewName.getSchemaName())).contains(viewName));
    }
}
Also used : ViewColumn(io.prestosql.spi.connector.ConnectorViewDefinition.ViewColumn) ConnectorMetadata(io.prestosql.spi.connector.ConnectorMetadata) SchemaTableName(io.prestosql.spi.connector.SchemaTableName) ConnectorViewDefinition(io.prestosql.spi.connector.ConnectorViewDefinition)

Example 19 with ConnectorViewDefinition

use of io.prestosql.spi.connector.ConnectorViewDefinition in project hetu-core by openlookeng.

the class VdmMetadata method getView.

@Override
public Optional<ConnectorViewDefinition> getView(ConnectorSession session, SchemaTableName viewName) {
    Optional<TableEntity> viewEntity = metastore.getTable(vdmName, viewName.getSchemaName(), viewName.getTableName());
    if (viewEntity.isPresent() && isHetuView(viewEntity.get().getParameters().get(HETU_VIEW_FLAG))) {
        TableEntity view = viewEntity.get();
        ConnectorViewDefinition definition = decodeViewData(view.getViewOriginalText());
        // use owner from view metadata if it exists
        if (view.getOwner() != null && !definition.isRunAsInvoker()) {
            definition = new ConnectorViewDefinition(definition.getOriginalSql(), definition.getCatalog(), definition.getSchema(), definition.getColumns(), Optional.of(view.getOwner()), false);
        }
        return Optional.of(definition);
    }
    return Optional.empty();
}
Also used : TableEntity(io.prestosql.spi.metastore.model.TableEntity) ConnectorViewDefinition(io.prestosql.spi.connector.ConnectorViewDefinition)

Example 20 with ConnectorViewDefinition

use of io.prestosql.spi.connector.ConnectorViewDefinition in project hetu-core by openlookeng.

the class TestMemoryMetadata method testViews.

@Test
public void testViews() {
    SchemaTableName test1 = new SchemaTableName("test", "test_view1");
    SchemaTableName test2 = new SchemaTableName("test", "test_view2");
    // create schema
    metadata.createSchema(SESSION, "test", ImmutableMap.of());
    // create views
    metadata.createView(SESSION, test1, testingViewDefinition("test1"), false);
    metadata.createView(SESSION, test2, testingViewDefinition("test2"), false);
    // verify listing
    List<SchemaTableName> list = metadata.listViews(SESSION, Optional.of("test"));
    assertEqualsIgnoreOrder(list, ImmutableList.of(test1, test2));
    // verify getting data
    Map<SchemaTableName, ConnectorViewDefinition> views = metadata.getViews(SESSION, Optional.of("test"));
    assertEquals(views.keySet(), ImmutableSet.of(test1, test2));
    assertEquals(views.get(test1).getOriginalSql(), "test1");
    assertEquals(views.get(test2).getOriginalSql(), "test2");
    // all schemas
    assertThat(metadata.getViews(SESSION, Optional.empty())).containsOnlyKeys(test1, test2);
    // exact match on one schema and table
    assertThat(metadata.getView(SESSION, new SchemaTableName("test", "test_view1"))).map(ConnectorViewDefinition::getOriginalSql).contains("test1");
    // non-existent table
    assertThat(metadata.getView(SESSION, new SchemaTableName("test", "nonexistenttable"))).isEmpty();
    // non-existent schema
    assertThat(metadata.getViews(SESSION, Optional.of("nonexistentschema"))).isEmpty();
    // drop first view
    metadata.dropView(SESSION, test1);
    assertThat(metadata.getViews(SESSION, Optional.of("test"))).containsOnlyKeys(test2);
    // drop second view
    metadata.dropView(SESSION, test2);
    assertThat(metadata.getViews(SESSION, Optional.of("test"))).isEmpty();
    // verify listing everything
    assertThat(metadata.getViews(SESSION, Optional.empty())).isEmpty();
}
Also used : SchemaTableName(io.prestosql.spi.connector.SchemaTableName) ConnectorViewDefinition(io.prestosql.spi.connector.ConnectorViewDefinition) Test(org.testng.annotations.Test)

Aggregations

ConnectorViewDefinition (io.prestosql.spi.connector.ConnectorViewDefinition)22 SchemaTableName (io.prestosql.spi.connector.SchemaTableName)14 Test (org.testng.annotations.Test)11 PrestoException (io.prestosql.spi.PrestoException)10 TableEntity (io.prestosql.spi.metastore.model.TableEntity)8 QualifiedObjectName (io.prestosql.spi.connector.QualifiedObjectName)7 ConnectorMetadata (io.prestosql.spi.connector.ConnectorMetadata)6 ImmutableList (com.google.common.collect.ImmutableList)5 DatabaseEntity (io.prestosql.spi.metastore.model.DatabaseEntity)5 BeforeClass (org.testng.annotations.BeforeClass)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 Session (io.prestosql.Session)4 ConnectorSession (io.prestosql.spi.connector.ConnectorSession)4 ViewColumn (io.prestosql.spi.connector.ConnectorViewDefinition.ViewColumn)4 ArrayList (java.util.ArrayList)4 Optional (java.util.Optional)4 ColumnEntity (io.prestosql.spi.metastore.model.ColumnEntity)3 AfterClass (org.testng.annotations.AfterClass)3 Throwables.throwIfUnchecked (com.google.common.base.Throwables.throwIfUnchecked)2 ImmutableSet (com.google.common.collect.ImmutableSet)2