Search in sources :

Example 51 with ColumnMetadata

use of io.trino.spi.connector.ColumnMetadata in project trino by trinodb.

the class TestValidateLimitWithPresortedInput method createLocalQueryRunner.

@Override
protected LocalQueryRunner createLocalQueryRunner() {
    Session session = testSessionBuilder().setCatalog(MOCK_CATALOG).setSchema(TEST_SCHEMA).build();
    LocalQueryRunner queryRunner = LocalQueryRunner.builder(session).build();
    MockConnectorFactory mockFactory = MockConnectorFactory.builder().withGetTableProperties((connectorSession, handle) -> {
        MockConnectorTableHandle tableHandle = (MockConnectorTableHandle) handle;
        if (tableHandle.getTableName().equals(MOCK_TABLE_NAME)) {
            return new ConnectorTableProperties(TupleDomain.all(), Optional.empty(), Optional.empty(), Optional.empty(), ImmutableList.of(new SortingProperty<>(COLUMN_HANDLE_A, ASC_NULLS_FIRST), new SortingProperty<>(COLUMN_HANDLE_C, ASC_NULLS_FIRST)));
        }
        throw new IllegalArgumentException();
    }).withGetColumns(schemaTableName -> {
        if (schemaTableName.equals(MOCK_TABLE_NAME)) {
            return ImmutableList.of(new ColumnMetadata(COLUMN_NAME_A, VARCHAR), new ColumnMetadata(COLUMN_NAME_B, VARCHAR), new ColumnMetadata(COLUMN_NAME_C, VARCHAR));
        }
        throw new IllegalArgumentException();
    }).build();
    queryRunner.createCatalog(MOCK_CATALOG, mockFactory, ImmutableMap.of());
    return queryRunner;
}
Also used : TypeAnalyzer.createTestingTypeAnalyzer(io.trino.sql.planner.TypeAnalyzer.createTestingTypeAnalyzer) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) Test(org.testng.annotations.Test) Function(java.util.function.Function) PlanNode(io.trino.sql.planner.plan.PlanNode) CatalogName(io.trino.connector.CatalogName) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) MockConnectorFactory(io.trino.connector.MockConnectorFactory) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) LongLiteral(io.trino.sql.tree.LongLiteral) LocalQueryRunner(io.trino.testing.LocalQueryRunner) PlanBuilder(io.trino.sql.planner.iterative.rule.test.PlanBuilder) ColumnHandle(io.trino.spi.connector.ColumnHandle) BasePlanTest(io.trino.sql.planner.assertions.BasePlanTest) VerifyException(com.google.common.base.VerifyException) ImmutableMap(com.google.common.collect.ImmutableMap) MockConnectorColumnHandle(io.trino.connector.MockConnectorColumnHandle) TestingTransactionHandle(io.trino.testing.TestingTransactionHandle) TupleDomain(io.trino.spi.predicate.TupleDomain) SchemaTableName(io.trino.spi.connector.SchemaTableName) SortingProperty(io.trino.spi.connector.SortingProperty) ASC_NULLS_FIRST(io.trino.spi.connector.SortOrder.ASC_NULLS_FIRST) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) TableHandle(io.trino.metadata.TableHandle) ConnectorTableProperties(io.trino.spi.connector.ConnectorTableProperties) TypeAnalyzer(io.trino.sql.planner.TypeAnalyzer) BIGINT(io.trino.spi.type.BigintType.BIGINT) WarningCollector(io.trino.execution.warnings.WarningCollector) TypeProvider(io.trino.sql.planner.TypeProvider) Optional(java.util.Optional) PlanBuilder.expression(io.trino.sql.planner.iterative.rule.test.PlanBuilder.expression) PlanNodeIdAllocator(io.trino.sql.planner.PlanNodeIdAllocator) Session(io.trino.Session) MockConnectorFactory(io.trino.connector.MockConnectorFactory) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) SortingProperty(io.trino.spi.connector.SortingProperty) ConnectorTableProperties(io.trino.spi.connector.ConnectorTableProperties) LocalQueryRunner(io.trino.testing.LocalQueryRunner) Session(io.trino.Session)

Example 52 with ColumnMetadata

use of io.trino.spi.connector.ColumnMetadata in project trino by trinodb.

the class BaseJdbcClient method createTable.

protected JdbcOutputTableHandle createTable(ConnectorSession session, ConnectorTableMetadata tableMetadata, String targetTableName) throws SQLException {
    SchemaTableName schemaTableName = tableMetadata.getTable();
    ConnectorIdentity identity = session.getIdentity();
    if (!getSchemaNames(session).contains(schemaTableName.getSchemaName())) {
        throw new TrinoException(NOT_FOUND, "Schema not found: " + schemaTableName.getSchemaName());
    }
    try (Connection connection = connectionFactory.openConnection(session)) {
        String remoteSchema = identifierMapping.toRemoteSchemaName(identity, connection, schemaTableName.getSchemaName());
        String remoteTable = identifierMapping.toRemoteTableName(identity, connection, remoteSchema, schemaTableName.getTableName());
        String remoteTargetTableName = identifierMapping.toRemoteTableName(identity, connection, remoteSchema, targetTableName);
        String catalog = connection.getCatalog();
        ImmutableList.Builder<String> columnNames = ImmutableList.builder();
        ImmutableList.Builder<Type> columnTypes = ImmutableList.builder();
        ImmutableList.Builder<String> columnList = ImmutableList.builder();
        for (ColumnMetadata column : tableMetadata.getColumns()) {
            String columnName = identifierMapping.toRemoteColumnName(connection, column.getName());
            columnNames.add(columnName);
            columnTypes.add(column.getType());
            columnList.add(getColumnDefinitionSql(session, column, columnName));
        }
        RemoteTableName remoteTableName = new RemoteTableName(Optional.ofNullable(catalog), Optional.ofNullable(remoteSchema), remoteTargetTableName);
        String sql = createTableSql(remoteTableName, columnList.build(), tableMetadata);
        execute(connection, sql);
        return new JdbcOutputTableHandle(catalog, remoteSchema, remoteTable, columnNames.build(), columnTypes.build(), Optional.empty(), remoteTargetTableName);
    }
}
Also used : Type(io.trino.spi.type.Type) VarcharType.createUnboundedVarcharType(io.trino.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(io.trino.spi.type.VarcharType) JoinType(io.trino.spi.connector.JoinType) CharType(io.trino.spi.type.CharType) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) Connection(java.sql.Connection) ConnectorIdentity(io.trino.spi.security.ConnectorIdentity) TrinoException(io.trino.spi.TrinoException) SchemaTableName(io.trino.spi.connector.SchemaTableName)

Example 53 with ColumnMetadata

use of io.trino.spi.connector.ColumnMetadata in project trino by trinodb.

the class TestAccumuloClient method testCreateTableEmptyAccumuloColumn.

@Test
public void testCreateTableEmptyAccumuloColumn() {
    SchemaTableName tableName = new SchemaTableName("default", "test_create_table_empty_accumulo_column");
    try {
        List<ColumnMetadata> columns = ImmutableList.of(new ColumnMetadata("id", BIGINT), new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", BIGINT), new ColumnMetadata("c", BIGINT), new ColumnMetadata("d", BIGINT));
        Map<String, Object> properties = new HashMap<>();
        new AccumuloTableProperties().getTableProperties().forEach(meta -> properties.put(meta.getName(), meta.getDefaultValue()));
        properties.put("external", true);
        properties.put("column_mapping", "a:a:a,b::b,c:c:,d::");
        client.createTable(new ConnectorTableMetadata(tableName, columns, properties));
        assertNotNull(client.getTable(tableName));
    } finally {
        AccumuloTable table = zooKeeperMetadataManager.getTable(tableName);
        if (table != null) {
            client.dropTable(table);
        }
    }
}
Also used : AccumuloTable(io.trino.plugin.accumulo.metadata.AccumuloTable) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) HashMap(java.util.HashMap) AccumuloTableProperties(io.trino.plugin.accumulo.conf.AccumuloTableProperties) SchemaTableName(io.trino.spi.connector.SchemaTableName) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) Test(org.testng.annotations.Test)

Example 54 with ColumnMetadata

use of io.trino.spi.connector.ColumnMetadata in project trino by trinodb.

the class BigQueryMetadata method getViewDefinitionSystemTable.

private Optional<SystemTable> getViewDefinitionSystemTable(ConnectorSession session, SchemaTableName viewDefinitionTableName, SchemaTableName sourceTableName) {
    BigQueryClient client = bigQueryClientFactory.create(session);
    String projectId = getProjectId(client);
    String remoteSchemaName = client.toRemoteDataset(projectId, sourceTableName.getSchemaName()).map(RemoteDatabaseObject::getOnlyRemoteName).orElseThrow(() -> new TableNotFoundException(viewDefinitionTableName));
    String remoteTableName = client.toRemoteTable(projectId, remoteSchemaName, sourceTableName.getTableName()).map(RemoteDatabaseObject::getOnlyRemoteName).orElseThrow(() -> new TableNotFoundException(viewDefinitionTableName));
    TableInfo tableInfo = client.getTable(TableId.of(projectId, remoteSchemaName, remoteTableName)).orElseThrow(() -> new TableNotFoundException(viewDefinitionTableName));
    if (!(tableInfo.getDefinition() instanceof ViewDefinition)) {
        throw new TableNotFoundException(viewDefinitionTableName);
    }
    List<ColumnMetadata> columns = ImmutableList.of(new ColumnMetadata("query", VarcharType.VARCHAR));
    List<Type> types = columns.stream().map(ColumnMetadata::getType).collect(toImmutableList());
    Optional<String> query = Optional.ofNullable(((ViewDefinition) tableInfo.getDefinition()).getQuery());
    Iterable<List<Object>> propertyValues = ImmutableList.of(ImmutableList.of(query.orElse("NULL")));
    return Optional.of(createSystemTable(new ConnectorTableMetadata(sourceTableName, columns), constraint -> new InMemoryRecordSet(types, propertyValues).cursor()));
}
Also used : ViewDefinition(com.google.cloud.bigquery.ViewDefinition) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) TableId(com.google.cloud.bigquery.TableId) SchemaNotFoundException(io.trino.spi.connector.SchemaNotFoundException) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) VIEW(com.google.cloud.bigquery.TableDefinition.Type.VIEW) Schema(com.google.cloud.bigquery.Schema) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) Map(java.util.Map) ProjectionApplicationResult(io.trino.spi.connector.ProjectionApplicationResult) RemoteDatabaseObject(io.trino.plugin.bigquery.BigQueryClient.RemoteDatabaseObject) ENGLISH(java.util.Locale.ENGLISH) TABLE(com.google.cloud.bigquery.TableDefinition.Type.TABLE) Field(com.google.cloud.bigquery.Field) TableDefinition(com.google.cloud.bigquery.TableDefinition) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) TrinoException(io.trino.spi.TrinoException) Streams(com.google.common.collect.Streams) SchemaTableName(io.trino.spi.connector.SchemaTableName) Preconditions.checkState(com.google.common.base.Preconditions.checkState) List(java.util.List) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Stream(java.util.stream.Stream) BIGQUERY_LISTING_DATASET_ERROR(io.trino.plugin.bigquery.BigQueryErrorCode.BIGQUERY_LISTING_DATASET_ERROR) TrinoPrincipal(io.trino.spi.security.TrinoPrincipal) SchemaTablePrefix(io.trino.spi.connector.SchemaTablePrefix) Assignment(io.trino.spi.connector.Assignment) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) ConnectorMetadata(io.trino.spi.connector.ConnectorMetadata) SystemTable(io.trino.spi.connector.SystemTable) Constraint(io.trino.spi.connector.Constraint) Logger(io.airlift.log.Logger) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) Type(io.trino.spi.type.Type) BigQueryException(com.google.cloud.bigquery.BigQueryException) BigQueryType.toField(io.trino.plugin.bigquery.BigQueryType.toField) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) DatasetId(com.google.cloud.bigquery.DatasetId) Function(java.util.function.Function) Inject(javax.inject.Inject) VarcharType(io.trino.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) ColumnHandle(io.trino.spi.connector.ColumnHandle) Table(com.google.cloud.bigquery.Table) ConstraintApplicationResult(io.trino.spi.connector.ConstraintApplicationResult) RecordCursor(io.trino.spi.connector.RecordCursor) ConnectorSession(io.trino.spi.connector.ConnectorSession) TupleDomain(io.trino.spi.predicate.TupleDomain) InMemoryRecordSet(io.trino.spi.connector.InMemoryRecordSet) ConnectorTableProperties(io.trino.spi.connector.ConnectorTableProperties) ConnectorExpression(io.trino.spi.expression.ConnectorExpression) DatasetInfo(com.google.cloud.bigquery.DatasetInfo) TableInfo(com.google.cloud.bigquery.TableInfo) ConnectorTransactionHandle(io.trino.spi.connector.ConnectorTransactionHandle) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) ViewDefinition(com.google.cloud.bigquery.ViewDefinition) InMemoryRecordSet(io.trino.spi.connector.InMemoryRecordSet) TableNotFoundException(io.trino.spi.connector.TableNotFoundException) Type(io.trino.spi.type.Type) VarcharType(io.trino.spi.type.VarcharType) TableInfo(com.google.cloud.bigquery.TableInfo) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata)

Example 55 with ColumnMetadata

use of io.trino.spi.connector.ColumnMetadata in project trino by trinodb.

the class TestTypeConversions method testConvertOneLevelRecordColumn.

@Test
public void testConvertOneLevelRecordColumn() {
    BigQueryColumnHandle column = new BigQueryColumnHandle("rec", BigQueryType.RECORD, Field.Mode.NULLABLE, null, null, ImmutableList.of(new BigQueryColumnHandle("sub_s", BigQueryType.STRING, Field.Mode.NULLABLE, null, null, ImmutableList.of(), null), new BigQueryColumnHandle("sub_i", BigQueryType.INTEGER, Field.Mode.NULLABLE, null, null, ImmutableList.of(), null)), null);
    ColumnMetadata metadata = column.getColumnMetadata();
    RowType targetType = RowType.rowType(RowType.field("sub_s", VarcharType.VARCHAR), RowType.field("sub_i", BigintType.BIGINT));
    assertThat(metadata.getType()).isEqualTo(targetType);
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) RowType(io.trino.spi.type.RowType) Test(org.testng.annotations.Test)

Aggregations

ColumnMetadata (io.trino.spi.connector.ColumnMetadata)154 SchemaTableName (io.trino.spi.connector.SchemaTableName)75 ConnectorTableMetadata (io.trino.spi.connector.ConnectorTableMetadata)73 Test (org.testng.annotations.Test)64 ImmutableList (com.google.common.collect.ImmutableList)63 ImmutableMap (com.google.common.collect.ImmutableMap)55 List (java.util.List)45 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)43 Optional (java.util.Optional)43 ConnectorSession (io.trino.spi.connector.ConnectorSession)41 TrinoException (io.trino.spi.TrinoException)38 ColumnHandle (io.trino.spi.connector.ColumnHandle)38 Map (java.util.Map)38 Type (io.trino.spi.type.Type)35 Constraint (io.trino.spi.connector.Constraint)32 ConnectorTableHandle (io.trino.spi.connector.ConnectorTableHandle)31 ConnectorMetadata (io.trino.spi.connector.ConnectorMetadata)30 BIGINT (io.trino.spi.type.BigintType.BIGINT)30 HashMap (java.util.HashMap)27 Set (java.util.Set)27