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